Perl用于循环和作用域变量

时间:2013-10-27 18:11:29

标签: arrays perl loops

我正在尝试循环一个数组,递增200.我在200内构建一个子数组,在子数组上运行计算,然后将读取帧移动到下一组200并重复。但是,每当我进行第二次计算时,新的子数组都包含第一个子数组的值。这就像我无法重置或undef子数组在for循环的下一次迭代中重用它。

完整代码可在github上找到:https://github.com/bsima/yeast-TRX

要运行代码,您需要在与脚本相同的目录中使用以下基因组文件:https://raw.github.com/bsima/yeast-TRX/master/data/bayanus/genome.csv

以下是相关代码:

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;
use Statistics::Descriptive;

my $species = "bayanus";

open(SPECIES, "<./genome.csv") || die "Cannot open file: $!\n";
my @text = <SPECIES>;

my $geneNameRe  = qr/(eY\w{5}[CW])/;
my $geneRe      = qr/,([atgcATGC-]+)/;

foreach my $line (@text) {
    chomp($line);
    if ( defined $line && $line =~ m/e.{4,},[acgtACGT-]+/ ) {

        my $gene     = match($geneRe,$line);
        my $geneName = match($geneNameRe,$line);

        # Smoothing function
        #
        # This moves through the gene data and counts the position until it arrives
        # at the end of the smoothing window (e.g. 200). Then it calculates the average
        # of the selected data set and outputs it into the respecive `smooth.csv` file 
        # in the following format, to be read later by R's graphing functions:
        #       gene,position,trx.mean,energy.mean
        open(my $smooth, ">>./smooth.csv") || die "Cannot open file $!";
        my $smoothingWindow = 200;
        # Loop through every 200 characters
        for ( my $smoothing = 0; $smoothing < length($gene); $smoothing=$smoothing+$smoothingWindow ) {

            my @trxValues;
            my @energyScores;

            # Loop through every character
            for ( my $position = 0; $position <= $smoothingWindow; $position++ ) {
                my $dinucleotide = substr($gene,$position,2); 

                if ( $dinucleotide =~ m/[actgACTG]{2}/ ) {     
                    my $trxValue = trxScore($dinucleotide);
                    my $energyScore = energyScore($dinucleotide);

                    #print "trxValue = $trxValue\n"; 
                    push @trxValues, $trxValue;
                    push @energyScores, $energyScore;
                }                    
            }

            # Now run the calculations and print to SMOOTH
            my $trxStat = Statistics::Descriptive::Full->new();
            $trxStat->add_data(@trxValues);
            my $trxMean = $trxStat->mean();
            my $energyStat = Statistics::Descriptive::Full->new();
            $energyStat->add_data(@energyScores);
            my $energyMean = $energyStat->mean();

            print "$geneName $smoothing: TRX Values @trxValues\n"; 
            print "$geneName $smoothing: TRX Mean is $trxMean.\n";

            print $smooth $geneName . "," . $smoothing . "," . $trxMean . "," . $energyMean . "\n";

        }
        close $smooth;
    }
}

# This just makes it easy to do regex matches
sub match {
        my ( $re, $text ) = @_;
        if ( $text =~ $re ) {
                return $1;
        }
}

# @name trxScore
# @description Calculates and returns the TRX value of a given phosphate linkage
# @param $dinucleotide {string} The nucleotide to be checked
# @return {integer} The TRX value
sub trxScore {

    my ( $dinucleotide ) = @_;

        my %trxScores = (
            qr/(CG)/ => 43,
            qr/(CA)/ => 42,
            qr/(TG)/ => 42,
            qr/(GG)/ => 42,
            qr/(CC)/ => 42,
            qr/(GC)/ => 25,
            qr/(GA)/ => 22,
            qr/(TC)/ => 22,
            qr/(TA)/ => 14,
            qr/(AG)/ =>  9,
            qr/(CT)/ =>  9,
            qr/(AA)/ =>  5,
            qr/(TT)/ =>  5,
            qr/(AC)/ =>  4,
            qr/(GT)/ =>  4,
            qr/(AT)/ =>  0,
            qr/(.-)/ =>  0,
            qr/(-.)/ =>  0   # These last two are necessary for dealing with missing values in the genomes
        );

    foreach my $re (keys %trxScores) {
        if ( match($re,$dinucleotide) ) {
            return $trxScores{$re};
        } 
    }
    return 0;
}

# @name energyScore
# @description Calculates and returns the delta-E value of a given phosphate linkage
# @param $dinucleotide {string} The nucleotide to be checked
# @return {integer} The delta-E value
sub energyScore {

    my ( $dinucleotide ) = @_;

        my %energyScores = (
            qr/(AA)/ => -18.5,
            qr/(AC)/ => -19.0,
            qr/(AG)/ => -23.6,
            qr/(AU)/ => -15.7,
            qr/(CA)/ => -20.0,
            qr/(CC)/ => -21.4,
            qr/(CG)/ => -26.9,
            qr/(CU)/ => -17.2,
            qr/(GA)/ => -23.7,
            qr/(GC)/ => -22.9,
            qr/(GG)/ => -24.3,
            qr/(GU)/ => -18.9,
            qr/(UA)/ => -19.6,
            qr/(UC)/ => -28.2,
            qr/(UG)/ => -23.3,
            qr/(UU)/ => -15.8,
            qr/(.-)/ =>     0,
            qr/(-.)/ =>     0   # These last two are necessary for dealing with missing values in the genomes
        );

    foreach my $re (keys %energyScores) {
        if ( match($re,$dinucleotide) ) {
            return $energyScores{$re};
        } 
    }
    return 0;
}

如果您运行该代码块所属的较大脚本,它会将@trxValues的内容打印到终端,您将看到$smoothing变量正在递增,所以for循环的步骤部分没有任何问题,但@trxValues的内容永远不会改变。因此,在第一个for循环结束和第二个循环开始之间的某个位置,数组@trxValues无法正确清除。同样的事情发生在@energyScores

关于这个的任何想法?我尝试在undef循环结束时使用for,并在循环结束时将@trxValues设置为零,两者都不起作用。我完全难过了。

编辑:将代码块更改为可自行运行。

1 个答案:

答案 0 :(得分:0)

您似乎始终在内部for循环中使用相同的第一个平滑窗口。

尝试更改:

for ( my $position = 0; $position <= $smoothingWindow; $position++ ) {

for ( my $position = $smoothing; $position < $smoothing + $smoothingWindow; $position++ ) {

虽然内循环应检查平滑窗口和length($gene)(除非你知道它总是200的偶数倍?)

您一次最多只能length($gene)个位置,但您的substr正在提取两个字符,这似乎很奇怪。