我有一个大的选项卡分隔文件,如下所示:
contig04733 contig00012 77 contig00546 contig01344 12 contig08943 contig00001 14 contig00765 contig03125 88 等
我有一个单独的制表符分隔文件,只有这些连体对的子集,如下所示:
contig04733 contig00012 contig08943 contig00001 等
我想将第一个文件中与第二个文件中列出的行对应的行提取到一个新文件中。在这个特定的数据集中,我认为在这两个文件中,每对应该是相同的。但也想知道是否说:
file1 contig08943 contig00001 14
但在file2中
contig00001 contig08943
我还是想要那个组合,也可以写点什么吗?
我的密码在下面。
use strict;
use warnings;
#open the contig pairs list
open (PAIRS, "$ARGV[0]") or die "Error opening the input file with contig pairs";
#hash to store contig IDs - I think?!
my $pairs;
#read through the pairs list and read into memory?
while(<PAIRS>){
chomp $_; #get rid of ending whitepace
$pairs->{$_} = 1;
}
close(PAIRS);
#open data file
open(DATA, "$ARGV[1]") or die "Error opening the sequence pairs file\n";
while(<DATA>){
chomp $_;
my ($contigs, $identity) = split("\t", $_);
if (defined $pairs->{$contigs}) {
print STDOUT "$_\n";
}
}
close(DATA);发布于 2013-03-08 16:48:09
尝试使用基于这两个键的散列哈希(在拆分之后)。
use strict;
use warnings;
#open the contig pairs list
open (PAIRS, "$ARGV[0]") or die "Error opening the input file with contig pairs";
#hash to store contig IDs - I think?!
#my $pairs;
#read through the pairs list and read into memory?
my %all_configs;
while(<PAIRS>){
chomp $_; #get rid of ending whitepace
my @parts = split("\t", $_); #split into ['contig04733', 'contig00012', 77]
#store the entire row as a hash of hashes
$all_configs{$parts[0]}{$parts[1]} = $_;
#$pairs->{$_} = 1;
}
close(PAIRS);
#open data file
open(DATA, "$ARGV[1]") or die "Error opening the sequence pairs file\n";
while(<DATA>){
chomp $_;
my ($contigs, $identity) = split("\t", $_);
#see if we find a row, one way, or the other
my $found_row = $all_configs{$contigs}{$identity}
|| $all_configs{$identity}{$contigs};
#if found, the split, and handle each part
if ($found_row) {
my @parts = split("\t", $found_row);
#same sequence as in first file
my $modified_row = $parts[0]."\t".$parts[1].(sqrt($parts[2]/100));
#if you want to keep the same sequence as found in second file
my $modified_row = $contigs."\t".$identity.(sqrt($parts[2]/100));
print STDOUT $found_row."\n"; #or
print STDOUT $modified_row."\n";
}
}
close(DATA);https://stackoverflow.com/questions/15295831
复制相似问题