Perl 将一个列表与另一个列表匹配
我有一个包含如下数据的txt文件(测试): 以及另一个包含如下数据的txt文件(数据库): 我希望获得一个输出文件,其中测试与数据库匹配,给出如下内容:Perl 将一个列表与另一个列表匹配,perl,unix,Perl,Unix,我有一个包含如下数据的txt文件(测试): 以及另一个包含如下数据的txt文件(数据库): 我希望获得一个输出文件,其中测试与数据库匹配,给出如下内容: chr1_8383722 12_Repressed chr1_201327234 13_Heterochrom/lo chr2_123123 1_Active Promoter 这可以在perl上完成吗?谢谢大家! 试试这个: #!/usr/bin/perl use warnings; use strict; open(my $db, "
chr1_8383722 12_Repressed
chr1_201327234 13_Heterochrom/lo
chr2_123123 1_Active Promoter
这可以在perl上完成吗?谢谢大家! 试试这个:
#!/usr/bin/perl
use warnings;
use strict;
open(my $db, "<", "database.txt") or die "Cannot open < database.txt: $!";
open(my $tst, "<", "test.txt") or die "Cannot open < test.txt: $!";
my @database;
while (<$db>) {
chomp;
my @fields = split;
push @database, \@fields;
}
while (my $line = <$tst>) {
chomp($line);
my ($chr, $pos) = split /_/, $line;
# There is not unique key can be used to detect whether an entry is exist
# in the database.
foreach my $entry (@database) {
if ($chr eq $entry->[0] && $entry->[1] <= $pos && $pos <= $entry->[2]) {
print "$line $entry->[3]\n";
}
}
}
#/usr/bin/perl
使用警告;
严格使用;
打开(我的$db,“[3]\n”;
}
}
}
以下内容可能会有所帮助:
use strict;
use warnings;
my %hash;
local $" = '_';
while (<>) {
chomp;
$hash{$_} = undef;
last if eof;
}
while (<>) {
my @cols = split;
print "@cols[ 0, 1 ] $cols[-1]\n" if exists $hash{"@cols[ 0, 1 ]"};
}
使用严格;
使用警告;
我的%hash;
本地$“='"”;
而(){
咀嚼;
$hash{$\}=undf;
最后如果eof;
}
而(){
我的@cols=split;
如果存在$hash{“@cols[0,1]”,则打印“@cols[0,1]$cols[-1]\n”;
}
命令行用法:perlscript.pl测试数据库[>outFile]
最后一个可选参数(不带方括号)将输出指向一个文件。如何将
TEST
与DATABASE
匹配以生成所需的输出?TEST
基本上是一个带有“_“分离基因组位置。如果测试
条目对应于数据库
条目范围(包括chromStart和chromEnd),我希望获得输出输出将提供来自TEST
的原始条目和来自数据库的相应名称。请注意,TEST
中有2个具有不同chr的MILL+条目(例如chr1 chr22等)。我希望我的解释不会太混乱。
测试中的任何条目都不能用于生成您给出的输出。如果测试输入正确,并且输入和输出之间的关系正确,请更新您的问题。很抱歉,这已更正
#!/usr/bin/perl
use warnings;
use strict;
open(my $db, "<", "database.txt") or die "Cannot open < database.txt: $!";
open(my $tst, "<", "test.txt") or die "Cannot open < test.txt: $!";
my @database;
while (<$db>) {
chomp;
my @fields = split;
push @database, \@fields;
}
while (my $line = <$tst>) {
chomp($line);
my ($chr, $pos) = split /_/, $line;
# There is not unique key can be used to detect whether an entry is exist
# in the database.
foreach my $entry (@database) {
if ($chr eq $entry->[0] && $entry->[1] <= $pos && $pos <= $entry->[2]) {
print "$line $entry->[3]\n";
}
}
}
use strict;
use warnings;
my %hash;
local $" = '_';
while (<>) {
chomp;
$hash{$_} = undef;
last if eof;
}
while (<>) {
my @cols = split;
print "@cols[ 0, 1 ] $cols[-1]\n" if exists $hash{"@cols[ 0, 1 ]"};
}