2016-08-13 160 views
0

我与DNA序列工作的文件,这个文件的格式是这样的,虽然有超过一个序列:的Perl:字符串中子字符串或子字符串中

>name of sequence 
EXAMPLESEQUENCEATCGATCGATCG 

我需要能告诉我们,如果一个变量(这也是一个序列)匹配任何序列的文件中,以及它匹配序列的名称,如果有的话,是。由于这些序列的性质,我的整个变量可能包含在文件的一行中,或者变量的一行可能是我变量的一部分。 现在我的代码看起来是这样的:

use warnings; 
use strict; 
my $filename = "https://stackoverflow.com/users/me/file/path/file.txt"; 
my $exampleentry = "ATCG"; 
my $returnval = "The sequence does not match any in the file"; 
open file, "<$filename" or die "Can't find file"; 
my @Name; 
my @Sequence; 
my $inx = 0; 
while (<file>){ 
    $Name[$inx] = <file>; 
    $Sequence[$inx] = <file>; 
    $indx++; 
}unless(index($Sequence[$inx], $exampleentry) != -1 || index($exampleentry, $Sequence[$inx]) != -1){ 
    $returnval = "The sequence matches: ". $Name[$inx]; 
} 
print $returnval; 

然而,即使我故意设置$条目从文件中比赛,我还是回到The sequence does not match any in the file。此外,当运行代码时,我得到Use of uninitialized value in index at thiscode.pl line 14, <file> line 3002.以及Use of uninitialized value within @Name in concatenation (.) or string at thiscode.pl line 15, <file> line 3002.

我怎么能执行这个搜索?

回答

1

我会认为这个脚本的目的是确定是否$exampleentry文件file.txt任何记录匹配。一个记录这里描述的DNA序列,并且对应于文件中的三个连续的线。如果变量$exampleentry与记录的第三行相匹配,它将匹配该序列。的匹配这里意味着要么

  • $exampleentry$line子串,或
  • $line$exampleentry子串,

其中$line referes到该文件中的相应的行。

首先,考虑输入文件file.txt:在您尝试阅读这些线方案

>name of sequence 
EXAMPLESEQUENCEATCGATCGATCG 

,使用调用readline。因此,最后一次调用readline将返回undef,因为没有更多行可读。

因此,它似乎是合理的,在file.txt的最后两行是畸形的,而正确的格式应该是:

>name of sequence 
EXAMPLESEQUENCE 
ATCGATCGATCG 

如果我现在理解错的话,我希望这能解决你的问题:

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

my $filename = "file.txt"; 
my $exampleentry = "ATCG"; 
my $returnval = "The sequence does not match any in the file"; 
open (my $fh, '<', $filename) or die "Can't find file: $!"; 
my @name; 
my @sequence; 
my $inx = 0; 
while (<$fh>) { 
    chomp ($name[$inx] = <$fh>); 
    chomp ($sequence[$inx] = <$fh>); 
    if (
     index($sequence[$inx], $exampleentry) != -1 
     || index($exampleentry, $sequence[$inx]) != -1 
    ) { 
     $returnval = "The sequence matches: ". $name[$inx]; 
     last; 
    } 
} 
say $returnval; 

注:

  • 我已经改变了变量名称遵循snake_case convention。例如,可变@Name使用所有小写作为@name更好写入。

  • 我更改了open()呼叫以遵循新推荐的3参数样式,请参阅Don't Open Files in the old way以获取更多信息。

  • 二手特征say代替print

  • 添加的每个的readline以避免在阵列存储换行符后chomp

+0

谢谢!对不起,在这个问题上我可怜的措词。 –