查找模式并打印下一列(两个文件)

时间:2014-02-11 13:05:18

标签: python perl bash awk bioinformatics

我想在File2中的File1的第1列中查找模式,然后在File2旁边打印File1的第二列:

File1(两列标签分隔):

APBW    lung
APCA    non virulent
ABKM    lung
APBX    lung
KK020   -
APBZ    non virulent
AOSU    lung
APBY    non virulent
APBV    joint; lung; CNS
CP001321    virulent
APBT    virulent
APBU    non-virulent
APCB    moderadamente virulenta (nose)
CP005384    -

File2(两列标签分隔):

HS372_00243 gi|219690483|gb|CP001321.1|
HS372_00436 gi|529264994|gb|APBX01000055.1|
HS372_00445 gi|529256455|gb|APBT01000061.1|
HS372_00544 gi|529259149|gb|APBV01000035.1|
HS372_00545 gi|529259149|gb|APBV01000035.1|
HS372_00546 gi|529259149|gb|APBV01000035.1|

所需的输出(三列标签分隔):

HS372_00243 gi|219690483|gb|CP001321.1| virulent
HS372_00436 gi|529264994|gb|APBX01000055.1| lung
HS372_00445 gi|529256455|gb|APBT01000061.1| virulent
HS372_00544 gi|529259149|gb|APBV01000035.1| jointlungCNS
HS372_00545 gi|529259149|gb|APBV01000035.1| jointlungCNS
HS372_00546 gi|529259149|gb|APBV01000035.1| jointlungCNS

临时bash代码(不工作),但对其他语言开放:

while read vl; do grep "$vl" File2 ; done < File1

还尝试使用awk(因为看起来它正在寻找完全匹配并且我在File2中的字符串被其他东西包围),所以不能正常工作:

awk 'BEGIN { FS = OFS = "\t" } FNR==NR{a[$1]=$0;next}($1 in a){print a[$1],$2,$3}' File1 File2

谢谢,Bernardo

3 个答案:

答案 0 :(得分:1)

这样的事情听起来就像你要求的那样:

awk '
BEGIN { FS=OFS="\t" }
NR==FNR { map[$1] = $2; next }
{
    for (key in map)
        if ($0 ~ key)
            $0 = $0 OFS map[key]
    print
}
' file1 file2

答案 1 :(得分:0)

希望这可以帮到你

file2_contents = [i.strip() for i in open("file2.txt").readlines()]

with open("file1.txt") as file1:
    for each_line in file1.readlines():
        code=each_line.split('\t')[0].strip()
        part=each_line.split('\t')[1].strip()
        for each_file2_contents in file2_contents:
            if code in each_file2_contents:
                print  each_file2_contents+'\t'+part

答案 2 :(得分:0)

Perl:

use warnings;
use strict;
use Data::Dumper;

my $str1 = <<"EOS1";
APBW    lung
APCA    non virulent
ABKM    lung
APBX    lung
KK020   -
APBZ    non virulent
AOSU    lung
APBY    non virulent
APBV    joint; lung; CNS
CP001321    virulent
APBT    virulent
APBU    non-virulent
APCB    moderadamente virulenta (nose)
CP005384    -
EOS1

my $str2 = <<"EOS2";
HS372_00243 gi|219690483|gb|CP001321.1|
HS372_00436 gi|529264994|gb|APBX01000055.1|
HS372_00445 gi|529256455|gb|APBT01000061.1|
HS372_00544 gi|529259149|gb|APBV01000035.1|
HS372_00545 gi|529259149|gb|APBV01000035.1|
HS372_00546 gi|529259149|gb|APBV01000035.1|
EOS2

#HS372_00243 gi|219690483|gb|CP001321.1| virulent

my %data1;my %data2;
foreach my $line ( split(/\n+/,$str1) ){
    my @f = split(/\t/,$line);
    $data1{$f[0]} = $f[1];
}
my @data2 = split(/\n+/,$str2);
foreach my $line ( split(/\n+/,$str2) ){
    my @f  = split(/\t/,$line);
    my @sf = split(/\|/,$f[1]);

    $data2{$sf[3]} = $line;
}
#print Dumper(\%data2);
foreach my $s1 ( sort { length($b) <=> length($a) } keys %data1){
    foreach my $d2 (@data2){
        my @f  = split(/\t/,$d2);
        my @sf = split(/\|/,$f[1]);
        if ($sf[3] =~ m!^$s1!is){
            #warn "found $s1 in $d2\n";
            print "$d2\t$data1{$s1}\n";
        }
    }
}

output:
HS372_00243     gi|219690483|gb|CP001321.1|     virulent
HS372_00445     gi|529256455|gb|APBT01000061.1| virulent
HS372_00544     gi|529259149|gb|APBV01000035.1| joint; lung; CNS
HS372_00545     gi|529259149|gb|APBV01000035.1| joint; lung; CNS
HS372_00546     gi|529259149|gb|APBV01000035.1| joint; lung; CNS
HS372_00436     gi|529264994|gb|APBX01000055.1| lung