解析用于GWAS挖掘的MEDLINE文件

2024-06-16 09:49:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将MedLine文件解析为0,1表,以执行一些统计下游分析:PCA、GWAS等。现在,我不知道该怎么继续。在

我需要将File 1-一个每行一张纸和制表符分隔的关键字的键值文件转换成一个包含折叠关键字和关键字存在/不存在的文件,显示为1或0个值。在

我想用Perl实现这一点,但也欢迎其他解决方案。在

谢谢,伯纳多

File 1

19801464    Animals Biodiversity    Computational Biology/methods   DNA
19696045    Environmental Microbiology  Computational Biology/methods   Software

期望输出:

^{pr2}$

Tags: 文件关键字解决方案gwas制表符perlfile键值
2条回答

可以使用Python和Pandas执行此操作:

In [1]: df = pd.read_table("file", header=None, sep="\t", names=["A", "B","C","D"], index_col=0)
In [2]: df
Out[2]: 
          A                           B                              C  \
0  19801464        Animals Biodiversity  Computational Biology/methods   
1  19696045  Environmental Microbiology  Computational Biology/methods   

          D  
0       DNA  
1  Software  

In [3]: b = pd.get_dummies(df.B)

In [4]: c = pd.get_dummies(df.C)

In [5]: d = pd.get_dummies(df.D)

In [6]: presence_absence = b.merge(c, right_index=True, left_index=True).merge(d,right_index=True, left_index=True)

In [7]: presence_absence
Out[7]: 
          Animals Biodiversity  Environmental Microbiology  \
A                                                            
19801464                     1                           0   
19696045                     0                           1   

          Computational Biology/methods  DNA  Software  
A                                                       
19801464                              1    1         0  
19696045                              1    0         1

希望这有帮助

perl脚本将生成一个您应该能够使用的哈希。为了方便起见,我使用^{}表示uniq,并使用^{}来转储数据结构:

#!/usr/bin/env perl
use strict;
use warnings;
use List::MoreUtils qw(uniq);
use DDP;

my %paper ;
my @categories;

while (<DATA>){
  chomp;
  my @record = split /\t/ ;
  $paper{$record[0]}  = { map { $_ => 1 } @record[1..$#record] } ;
  push @categories , @record[1..$#record] ;
}

@categories = uniq @categories; 

foreach (keys %paper) {
  foreach my $category(@categories) {
    $paper{$_}{$category} //= 0 ;
  } 
}; 

p %paper ;

__DATA__
19801464   Animals Biodiversity  Computational Biology/methods  DNA     
19696045   Environmental Microbiology   Computational Biology/methods Software

输出

^{pr2}$

从那里产生您想要的输出可能需要printf来正确格式化行。以下目的可能足够了:

print "\t", (join "  ", @categories); 
for (keys %paper) {
  print "\n", $_, "\t\t" ;
  for my $category(@categories) { 
    print $paper{$_}{$category}," "x17 ; 
  }  
}

编辑

一些格式化输出的方法。。。(我们使用x将格式节乘以@categories数组中元素的长度或数量,以便它们匹配):

使用format

my $format_line = 'format STDOUT =' ."\n"
                . '@# 'x ~~@categories . "\n" 
                . 'values %{ $paper{$num} }' . "\n"
                . '.'."\n"; 
for $num (keys %paper) {
  print $num ;
  no warnings 'redefine'; 
  eval $format_line;
write;
}

使用printf

print (" "x9, join "  ", @categories, "\n"); 
for $num (keys %paper) {
  print $num  ;
  map{ printf "%19d", $_ }  values %{ $paper{$num} } ;
  print "\n";   
}

使用form

use Perl6::Form;                                                              
for $num (keys %paper) {                                                       
  print form                                                         
  "{<<<<<<<<}" . "{>}" x ~~@categories ,                                      
    $num       , values %{ $paper{$num} }                                      
}

根据您计划如何处理数据,您可能能够用perl完成其余的分析工作,因此,在工作流程的后期阶段,打印的精确格式可能不是优先考虑的问题。请参见BioPerl获取想法。在

相关问题 更多 >