需要优化perl脚本

时间:2015-07-22 21:35:02

标签: perl optimization

我有两个文件,每个大约700,000行,看起来像:

File1中

1 rs58108140 0 10583 A G
1 rs189107123 0 10611 G C
1 rs180734498 0 13302 T C
1 rs144762171 0 13327 C G
22 rs1001008 0 44928369 G A

文件2

hg19chrc snpid a1 a2 bp info or se p ngt
1 rs4951859 C G 729679 0.631 0.97853 0.0173 0.2083 0
1 rs142557973 T C 731718 0.665 1.01949 0.0198 0.3298 0
22 rs1001008 A G 44928369 0.969 0.98649 0.0107 0.2023 0

我有一个脚本,我在其中查找文件1中的字段1和4以及文件2中的字段1和5匹配的行。然后,如果文件1中的字段5和6与文件2中的字段3和4匹配,则我只是从文件2中打印出该行。但是,如果字段3和4(File2)相对于字段5和6(File1),我从File2获取字段7的倒数,并打印带有调整字段7的行:

#! perl -w                                                                                                           
use strict;
use warnings;

my @kgloci;
open( my $loci_in, "<", "File1" ) or die $!;
while (<$loci_in>) {
    my ($chr, $snpID, $dist, $bp, $A1, $A2) = split;
    next if m/Chromosome/;
    push @kgloci, [$chr, $snpID, $dist, $bp, $A1, $A2];
}
close $loci_in;

my $filename = shift @ARGV;
open( my $input, "<", "File2" ) or die $!;
while (<$input>) {
    next if m/hg19chrc/;
    my ($chr, $snpID, $A1, $A2, $bp, $info, $or, $se, $p, $ngt) = split;
    foreach my $kglocus (@kgloci) {
        if (    $chr == $kglocus->[0]
                and $bp == $kglocus->[3]
                and $A1 eq $kglocus->[4] ){
        print "$chr $snpID $A1 $A2 $bp $info $or $se $p $ngt\n";
            next;
        }
        elsif ( $chr == $kglocus->[0]
            and $bp == $kglocus->[3]
            and $A1 eq $kglocus->[5]){
        my $new_or = 1/$or;
        print "$chr $snpID $A1 $A2 $bp $info $new_or $se $p $ngt\n";  
        next;
        }
    }
}
close($input);

原样,脚本将运行数天。有人能指出提高效率的方法吗?

1 个答案:

答案 0 :(得分:4)

使用哈希而不是数组。

以下脚本应该提供与您相同的输出(除非文件1中的A1和A2相同):

#! /usr/bin/perl
use warnings;
use strict;

my %lookup;
open my $LOCI, '<', 'File1' or die $!;
while (<$LOCI>) {
    next if /Chromosome/;
    my ($chr, $snpID, $dist, $bp, $A1, $A2) = split;
    $lookup{"$chr:$bp:$A1"} = 1;
    $lookup{"$chr:$bp:$A2"} = 2;
}

open my $IN, '<', 'File2' or die $!;
while (<$IN>) {
    next if m/hg19chrc/;
    my ($chr, $snpID, $A1, $A2, $bp, $info, $or, $se, $p, $ngt) = split;
    my $new_or = ( sub {},
                   sub { shift },
                   sub { 1 / shift },
                 )[ $lookup{"$chr:$bp:$A1"} || 0 ]->($or);
    print "$chr $snpID $A1 $A2 $bp $info $new_or $se $p $ngt\n"
        if defined $new_or;
}

这就是我创建测试数据的方式:

perl -MList::Util=shuffle -wE '
    $i = 1;
    $pos = 1;
    for (1 .. 10000) {
        say join " ", $i, "rs$pos", int rand 5, 1000 + int rand 10000,
            (shuffle(qw(A C T G)))[0,1];
        ++$i if rand 1 > .99;
        $pos += int rand 20;
    }' > File1

perl -wE '
    $i = 1;
    $pos = 1;
    for (1 .. 10000) {
        say join " ", $i, "rs$pos", (map qw(A C T G)[rand 4],1,2),
            1000 + int rand 10000, rand 1, rand 5, rand 1, rand 1,
            int rand 3;
        ++$i if rand 1 > .99;
        $pos += int rand 20;
    }' > File2

结果:旧脚本需要16秒,新脚本需要不到0.1秒。对于较大的文件(700_000行),新脚本需要4s。