我再次需要你的幫助!如何優化內循環搜索?
有這樣的分隔的文件標籤:
chr10.10.2 scaffold1116 94.92 394 13 1 16 409 10474 10860 4.1e-201 697.0
chr10.10.2 scaffold1116 100.00 14 0 0 1 14 10453 10466 1.9e+01 27.0
…………………………
和類似的其他文件:
chr10.10.1 283
chr10.10.2 409
chr10.10.3 572
chr10.10.4 248
chr10.10.5 143
…………………………
我想保持從第一個文件中的某些特定行的基礎上的數字第二個。
例如,如果我必須保留「chr10.10.2」這一行,我必須檢查「chr10.10.2」在第二個文件中的編號。 我寫了一個腳本,但由於這兩個文件相當大,需要很長時間。 (對於第一個文件的每一行,它將搜索第二個文件的所有行)。 有沒有什麼辦法以更有效的方式搜索第二個文件?
這裏是我的代碼:
#!/usr/bin/perl
use strict;
use warnings;
my $blat_out = $ARGV[0];
my $sizes = $ARGV[1];
#Cheking the output of "HCEs Vs Genomes" alignments (blat) based on the sizes of the HCEs....
open my $blat_file, $blat_out or die "Could not open $blat_out: $!";
while (my $line = <$blat_file>) {
chomp $line;
# while(my $size_line = <$size_file>) {
if ($line =~ m/^chr/) {
my @lines = split('\t', $line);
#my @size_lines = split('\t', $size_line);
my $hce = $lines[0];
#print "$hce\n";
my $scaf = $lines[1];
my $persent = $lines[2];
my $al_length = $lines[3];
my $hce_start = $lines[6];
my $hce_end = $lines[7];
my $scaf_start = $lines[8];
my $scaf_end = $lines[9];
my $score = $lines[10];
open my $size_file, $sizes or die "Could not open $sizes: $!";
while (my $size_line = <$size_file>) {
chomp $size_line;
my @size_lines = split('\t', $size_line);
my $hce_name = $size_lines[0];
my $hce_size = $size_lines[1];
#print "$hce_size\n";
if ($hce eq $hce_name) {
my $al_ratio = $al_length/$hce_size;
if (($persent >= 98) && ($al_ratio >= 0.9)) {
print "$line\n"; #print only the lines that satisfies the previous criteria
}
}
}
#close $size_file;
}
}
非常感謝你提前, 瓦西利斯。
是的,把第二個文件做成一個散列,儘管它不清楚你想用它做什麼。 – 2014-09-05 11:05:48
讀入並存儲'size_file'的內容 - 目前,在讀取blat文件的while循環中有open/parse,這意味着它將爲blat文件的每一行執行。難怪腳本運行緩慢! – 2014-09-05 11:38:48
可能的錯誤:你在單引號的'\ t''上'拆分'。這可能不會達到你想要的。你可能想要雙引號'「\ t」' – Miller 2014-09-05 19:27:05