1

我有一个格式如下所示的大文本文件 -

ID  SNP
FT  SNP 433
FT      /note="refAllele: T SNPstrains: 7083_1#5=C 7414_8#8=C 7480_8#49=C "
FT      /colour=1
FT  SNP 442
FT      /note="refAllele: T SNPstrains: 7065_8#2=C 7065_8#94=C 7083_1#2=C 7083_1#3=C 7083_1#41=C 7083_1#42=C 7083_1#43=C "
FT      /colour=1
FT  SNP 460
FT      /note="refAllele: T SNPstrains: 7564_8#14=C "
FT      /colour=1
FT  SNP 703
FT      /note="refAllele: G SNPstrains: 7521_5#39=A (non-synonymous) (AA Ala->Thr) "
FT      /colour=2
FT  SNP 937
FT      /note="refAllele: G SNPstrains: 7414_8#30=T (non-synonymous) (AA Val->Leu) "
FT      /colour=2
FT  SNP 1269
FT      /note="refAllele: G SNPstrains: 7480_7#22=A (synonymous) 7480_7#62=A (synonymous) "
FT      /colour=3
FT  SNP 1804
FT      /note="refAllele: T SNPstrains: 7414_7#66=A (non-synonymous) (AA Ser->Thr) 7414_8#44=A (non-synonymous) (AA Ser->Thr) 7521_6#54=A (non-synonymous) (AA Ser->Thr) "
FT      /colour=2

这是我使用的代码 -

$file="input file";
open IN, "$file";
open OUT, ">output file";
print OUT "Coordinate   No of Strains   AA Change\n";
while(<IN>){
    if(m/^FT\s+SNP\s+(\d+)/){
            $SNP=$1;        
    }elsif(m/^FT\s+\/note="(.*)"/){
        $line=$1;
        $count = ($line =~ tr/=/=/);
        $line =~ m/\((AA \w+->\w+)\)\s*$/;
        $change = $1 || "";
    }elsif(m/^FT\s+\/colour=(\d+)/){
        print OUT "$SNP $count $change\n" if $cod{$1} eq "non";
    }
}

目的是有一个类似于下面的输出文件(对于文本文件的上述部分)

Coordinates No of Strains AA Change
703         1             AA Ala->Thr
937         1             AA Val->Leu
1804        3             AA Ser->Thr

但是,当我将代码应用于文本文件时,我收到一个错误:Use of uninitialised values ($count or $change) in concatenation (.) or string at line 23。有问题的行是

print OUT "$SNP $count $change\n" if $cod{$1} eq "non";

该错误是指包含的行

FT      /colour=2

即,显示非同义突变的行,例如示例文本中的第 13 行。

请注意,我是编程新手,大部分代码不是我编写的。任何帮助,将不胜感激!

提前谢谢了。

4

1 回答 1

1

我使用更现代的编码标准重写了您的程序:

  • use strict;我用过use warnings;
  • 我使用空格来帮助澄清代码
  • 我对循环和 if/elsif 语句等内容使用了更现代的语法

我发现了一些小故障,但我很快就解决了这些问题。这是你的代码:

use strict;
use warnings;
use feature qw(say);

my $file = "input.txt";
my %cod = ( 1 => "red", 2 => "non", 3 => "green" );
open my $in, "<", "$file";
open my $out, ">", "output.txt";
say $out "Coordinate   No of Strains   AA Change";

my $SNP;
my $count;
my $change;
while ( my $line = <$in> ) {
    chomp $line;
    say qq(DEBUG: Line = "$line");
    if ( $line =~ /^FT\s+SNP\s+(\d+)/ ){
        $SNP = $1;        
        say qq(\$SNP = $1;);
    } 
    elsif ( $line =~ /^FT\s+\/note="(.*)"/) {
        my $note = $1;
        say qq(my \$note = $1);
        $count = ($note =~ tr/=/=/);
        $note =~ /\((AA \w+->\w+)\)\s*$/;
        $change = $1 || "";
    }
    elsif ( $line =~ /^FT\s+\/colour=(\d+)/ ) {
        say qq(Code = $1);
        if ( $cod{$1} eq "non" ) {
            printf $out "%-12.12s %-15.15s %s\n",  $SNP, $count, $change;
        }
    }
}

这是输出:

Coordinate   No of Strains   AA Change
703          1               AA Ala->Thr
937          1               AA Val->Leu
1804         3               AA Ser->Thr

这是你要找的吗?

于 2012-11-07T04:20:14.807 回答