我有一个大文本文件,格式如下 -
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
但是,当我将代码应用于文本文件时,我收到一个错误:在连接(。)或第23行的字符串中使用未初始化的值($ count或$ change)。有问题的行是
print OUT "$SNP $count $change\n" if $cod{$1} eq "non";
错误是指包含
的行FT /colour=2
,即显示非同义突变的行,例如示例文本中的第13行。
请注意,我是编程的新手,这段代码的很大一部分不是由我编写的。任何帮助将不胜感激!
非常感谢提前。
答案 0 :(得分:1)
我使用更现代的编码标准重写了您的程序:
use strict;
和use warnings;
我发现了一些小问题,但我没有花太多时间来解决问题。这是你的代码:
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
这是你要找的吗?