如何为每次迭代打印搜索序列的第一个结果?

时间:2022-11-29 21:36:50

How can I print the first occurrence of a protein sequence? For this query I get four results and I want only the first.

如何打印第一次出现的蛋白质序列?对于此查询,我得到四个结果,我只想要第一个。

use Bio::DB::GenBank;
use Bio::DB::Query::GenBank;

$query     = "LEGK";
$query_obj = Bio::DB::Query::GenBank->new(
    -db    => 'protein',
    -query => $query
);

$gb_obj = Bio::DB::GenBank->new;

$stream_obj = $gb_obj->get_Stream_by_query( $query_obj );

while ( $seq_obj = $stream_obj->next_seq ) {

    # do something with the sequence object
    print
        ">$query", ' ',
        $seq_obj->display_id, ' ',
        $seq_obj->desc, "\n",
        $seq_obj->seq[, '\n';

That while loop should look like this

while循环应该是这样的

while ( $seq_obj = $stream_obj->next_seq ) {

    # do something with the sequence object
    print $seq_obj->display_id, "\t", $seq_obj->length, "\n";
}

1 个解决方案

#1


1  

The main problem I see with your snippet is that it does not compile. Put use strict; use warnings; at the beginning of all your perl programs. This will alert you to syntax errors.

我看到你的代码片段的主要问题是它无法编译。使用严格;使用警告;在所有perl程序的开头。这将提醒您语法错误。

I do not know much about biology, however, you are iterating over sequence objects, but then making a rather strange call with $seq_obj->seq[,'\n'

我对生物学知之甚少,但是,你正在迭代序列对象,但随后用$ seq_obj-> seq [,'\ n'进行一个相当奇怪的调用

First of all, to call a function, use (), not [], [] indicates a reference to an array. Secondly, seq seems to be used to set or get a sequence value, and I do not see how '\n' would be a valid value.

首先,要调用函数,使用(),而不是[],[]表示对数组的引用。其次,seq似乎用于设置或获取序列值,我不知道'\ n'将如何成为有效值。

So

所以

while ($seq_obj = $stream_obj->next_seq) {
    print join(' ', $seq_obj->display_id, $seq_obj->desc)."\n"; # or use 'say'
    print $seq_obj->seq() . "\n";
}

should print all sequences. To get just the first, simply don't iterate through all results ( That's how I understood your question ):

应打印所有序列。要获得第一个,只需不要遍历所有结果(这就是我理解你的问题):

replace the while (){} with:

将while(){}替换为:

my $first_seq_obj = $stream_obj->next_seq;
print join(' ', $first_seq_obj->display_id, $first_seq_obj->desc)."\n"; # or use 'say'
print $first_seq_obj->seq() . "\n";

#1


1  

The main problem I see with your snippet is that it does not compile. Put use strict; use warnings; at the beginning of all your perl programs. This will alert you to syntax errors.

我看到你的代码片段的主要问题是它无法编译。使用严格;使用警告;在所有perl程序的开头。这将提醒您语法错误。

I do not know much about biology, however, you are iterating over sequence objects, but then making a rather strange call with $seq_obj->seq[,'\n'

我对生物学知之甚少,但是,你正在迭代序列对象,但随后用$ seq_obj-> seq [,'\ n'进行一个相当奇怪的调用

First of all, to call a function, use (), not [], [] indicates a reference to an array. Secondly, seq seems to be used to set or get a sequence value, and I do not see how '\n' would be a valid value.

首先,要调用函数,使用(),而不是[],[]表示对数组的引用。其次,seq似乎用于设置或获取序列值,我不知道'\ n'将如何成为有效值。

So

所以

while ($seq_obj = $stream_obj->next_seq) {
    print join(' ', $seq_obj->display_id, $seq_obj->desc)."\n"; # or use 'say'
    print $seq_obj->seq() . "\n";
}

should print all sequences. To get just the first, simply don't iterate through all results ( That's how I understood your question ):

应打印所有序列。要获得第一个,只需不要遍历所有结果(这就是我理解你的问题):

replace the while (){} with:

将while(){}替换为:

my $first_seq_obj = $stream_obj->next_seq;
print join(' ', $first_seq_obj->display_id, $first_seq_obj->desc)."\n"; # or use 'say'
print $first_seq_obj->seq() . "\n";