我有两个文件:使用Perl散列处理制表符分隔文件
- file_1有三列(标记(SNP),染色体和位置)
- file_2有三列(染色体,peak_start,和peak_end)。
除SNP列外,所有列都是数字。
文件排列如屏幕截图所示。 file_1有几百个SNP作为行,而file_2有61个峰。每个峰都由一个peak_start和peak_end标记。可以有任何一个文件中的23条染色体,file_2每个染色体有几个峰。
我想查找file_1中SNP的位置是否落入file_2中的peak_start和peak_end,以确定每个匹配的染色体。如果是这样,我想显示哪个SNP落在哪个峰值(最好将输出写入制表符分隔的文件)。
我宁愿分割文件,并使用散列,其中染色体是关键。我只发现了几个与此类似的问题,但我无法很好地理解所提出的解决方案。
这是我的代码的例子。这只是为了说明我的问题,到目前为止没有做任何事情,所以把它想成“伪代码”。
#!usr/bin/perl
use strict;
use warnings;
my (%peaks, %X81_05);
my @array;
# Open file or die
unless (open (FIRST_SAMPLE, "X81_05.txt")) {
die "Could not open X81_05.txt";
}
# Split the tab-delimited file into respective fields
while (<FIRST_SAMPLE>) {
chomp $_;
next if (m/Chromosome/); # Skip the header
@array = split("\t", $_);
($chr1, $pos, $sample) = @array;
$X81_05{'$array[0]'} = (
'position' =>'$array[1]'
)
}
close (FIRST_SAMPLE);
# Open file using file handle
unless (open (PEAKS, "peaks.txt")) {
die "could not open peaks.txt";
}
my ($chr, $peak_start, $peak_end);
while (<PEAKS>) {
chomp $_;
next if (m/Chromosome/); # Skip header
($chr, $peak_start, $peak_end) = split(/\t/);
$peaks{$chr}{'peak_start'} = $peak_start;
$peaks{$chr}{'peak_end'} = $peak_end;
}
close (PEAKS);
for my $chr1 (keys %X81_05) {
my $val = $X81_05{$chr1}{'position'};
for my $chr (keys %peaks) {
my $min = $peaks{$chr}{'peak_start'};
my $max = $peaks{$chr}{'peak_end'};
if (($val > $min) and ($val < $max)) {
#print $val, " ", "lies between"," ", $min, " ", "and", " ", $max, "\n";
}
else {
#print $val, " ", "does not lie between"," ", $min, " ", "and", " ", $max, "\n";
}
}
}
更多真棒代码:
听起来像是[文字:: CSV]任务(http://search.cpan.org/perldoc?Text::CSV)..重新发明轮子是不是真棒;) –
有多少行(线)在每个文件?在文件2中染色体是否可以出现一次以上,每个染色体是否都有其自己的峰值范围?如果是这样,你可以读入文件2并运行文件1 ... –
这些是制表符分隔的,而不是制表符分隔的,你知道的。 – tchrist