仅从fasta文件中提取第一个序列

时间:2015-03-24 18:40:28

标签: perl loops sequence bioinformatics fasta

我想仅从多个序列的fasta文件中提取第一个序列。 我在下面有这个代码,但我不能让循环恰到好处地互相合作。

while (my $line = <$in_fh>) {
    chomp $line;
    for (my $i = 1; $i <= 1; $i++) {
        print $out_fh $line;
    }
}

close $out_fh;

我认为它在while循环中变得混乱,但无论我怎么做都不正确。我尝试在外面移动for循环,但它没有用。它是循环的类型吗? 非常感谢所有指针。

3 个答案:

答案 0 :(得分:4)

如果您只想输入文件的第一行,那么您就不需要while循环。

my $line = <$in_fh>;
print $out_fh $line;

编辑:

在研究FASTA format之后,我认为它足够复杂,你不应该手动解析它。相反,您应该使用BioPerl

编辑2:

这是一个使用BioPerl的工作示例:

#!/usr/bin/perl

use strict;
use warnings;

use Bio::Seq;
use Bio::SeqIO;

my $fasta_file = shift @ARGV or die "Usage: $0 FASTA_FILE\n";

my $seqin = Bio::SeqIO->new( -format => 'Fasta', -file => $fasta_file )
  or die "can't load fasta file: $fasta_file\n";

my $seqobj = $seqin->next_seq();

my $sequence = $seqobj->seq();

print $sequence;

答案 1 :(得分:3)

由于每个fasta记录头都以>开头,因此序列中不应包含该字符。在看到以>开头的第2行之前,保持阅读行应该是安全的。

my $line = <$in_fh>;
#print first line no matter what
print $line;

while($line = <$in_fh>){
  #line must start with ">"
  unless( $line =~/^>.+/){
     print $line;
  }else{
    last;  #skip to the end
 }

}

答案 2 :(得分:1)

我现在你想要perl,但awk解决方案更短:

awk '/^>/{if(N)exit;++N;} {print;}' in.fa