【问题标题】:Perl compilation errorPerl 编译错误
【发布时间】:2015-06-24 12:55:14
【问题描述】:

对不起,如果这看起来很明显,但我在 Perl 和编程方面还很新,我已经工作了一个多星期,但无法完成。 我的想法很简单。我有一个 .csv,其中第一列中有名称,第二列中有一个从 -1 到 1 的数字,第三列中有一个位置。然后是另一个文件,其中我有名称(行以 > 开头)和每行 80 个字符的信息。

我想要做的是保留第一个文件的名称行并获取从 -20 到 +60 给出的“位置”。但我无法让它发挥作用,而且我已经到了不知道该去哪里的地步。

use strict;                 #read file line by line
use warnings;
my $outputfile = "Output1.txt";
my $filename = "InputP.txt";
my $inputfasta = "Inputfasta.txt";
open my $fh, '<', $filename or die "Couldn't open '$filename'";
open my $fh2, '>', $outputfile or die "Couldn't create '$outputfile'";
open my $fh3, '<', $inputfasta or die "Couldn't open '$inputfasta'";
my $Psequence = 0;
my $seqname = 0;

while (my $line = <$fh>) {
chomp $line;
my $length = index ($line, ",");
$seqname = substr ($line, 0, $length);  

my $length2 = index ($line, ",", $length);
my $score = substr ($line, $length +1, $length2);   

my $length3 = index ($line, ",", $length2);
my $position = substr ($line, $length2 +1, $length3);   

#print $fh2 "$seqname"."\t"."$score"."\t"."$position"."\n";  }

my $Rlength2 = index ($score, ",");
my $Rscore = substr ($score, 0, $Rlength2);
#print "$Rscore"."\n";}
        while (my $linea = <$fh3>){  #same order.

            chomp $linea;
                if ($linea=~/^>(.+)/) { 
                    print $fh3 "\n"."$linea"."\n"; }
                else { $linea =~ /^\s*(.*)\s*$/;
                    chomp $linea;
                    print $fh3 "$linea". "\n"; }   
                    }
                if ($Rscore >= 0.5){
                    $Psequence = substr ($linea, -20, 81); 
                    print "$seqname"."\n"."$Psequence";}
        }

【问题讨论】:

    标签: perl csv bioinformatics strict


    【解决方案1】:

    请学习正确缩进代码。那么错误会更加明显:

    while (my $linea = <$fh3>){  #same order.
        chomp $linea;
        if ($linea =~ /^>(.+)/) { 
            print $fh3 "\n$linea\n";
        } else {
            # Commented out as it does nothing.
            # $linea =~ /^\s*(.*)\s*$/;
            # chomp $linea;
            print $fh3 "$linea\n";
        }   
    }
    
    if ($Rscore >= 0.5){
        $Psequence = substr $linea, -20, 81; 
        print "$seqname\n$Psequence";
    }
    

    $linea 仅存在于 while 循环中,但您也尝试在下一段中使用它。循环结束时变量消失。

    【讨论】:

    • Perl::Tidy 可能对他有帮助。
    【解决方案2】:

    从 CSV 创建一个哈希,其中键是名称,值是位置。

    use Text::CSV_XS qw( );
    
    my %pos_by_name;
    {
       open(my $fh, '<', $input_qfn)
          or die("Can't open $input_qfn: $!\n");
    
       my $csv = Text::CSV_XS->new({ auto_diag => 1, binary => 1 });
       while (my $row = $csv_in->getline($fh)) {
          $pos_by_name{ $row->[0] } = $row->[2];
       }
    }
    

    然后,这只是从另一个文件中提取名称,并使用散列找到相关位置的问题。

    open(my $fh, '<', $fasta_qfn)
       or die("Can't open $fasta_qfn: $!\n");
    
    while (<$fh>) {
       chomp;
       my ($name) = /^>(.*)/
          or next;
    
       my $pos = $pos_by_name{$name};
       if (!defined($pos)) {
          die("Can't find position for $name\n");
       }
    
       ... Do something with $name and $pos ...
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-14
      • 2016-03-09
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多