繁体   English   中英

Perl编译错误

[英]Perl compilation error

很抱歉,如果它看起来很明显,但是我在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";}
        }

请学习正确缩进代码。 然后错误将更加明显:

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循环中,但是您也尝试在以下段落中使用它。 循环结束后,变量消失。

从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 ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM