回复: Re: use Bio::SeqIO to read Fasta sequence from pipe, or @ARGV, like "while (<>) {....}"

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

回复: Re: use Bio::SeqIO to read Fasta sequence from pipe, or @ARGV, like "while (<>) {....}"

Haiyan Lin
Hi,paul,

That is what I want. Thank you for you advice and kind reminding on how to 
ask for help. I will remember. Thanks and wishes to all again.


Haiyan 




从三星手机发送



-------- 原始邮件 --------
发件人: Paul Cantalupo <[hidden email]>
日期: 2014-07-14 PM 8:50 (GMT+08:00)
收件人: Haiyan Lin <[hidden email]>
抄送: [hidden email]
主题: Re: [Bioperl-l] use Bio::SeqIO to read Fasta sequence from pipe, or @ARGV, like "while (<>) {....}"


Hi Haiyan,

Ah your specifications are now clearer! I use the '-p' file test to check if STDIN is a named pipe (http://perldoc.perl.org/functions/-X.html) to do the same thing that you are trying to do:

use strict;
use warnings;
use Bio::SeqIO;
my $in;
if (-p STDIN) {
  $in = Bio::SeqIO->new(-fh => \*STDIN, -format => 'fasta');
}
else {
  $in = Bio::SeqIO->new(-file => shift);
}
while (my $seq = $in->next_seq) {
  print $seq->length,"\n";
}


Here is my input file and results when running both ways:

$ cat foo.fa
>foo1
tgtagtc
>foo2
taaaacgtgtcat

$ cat foo.fa | both.pl
7
13

$ both.pl foo.fa
7
13


Hope this helps,

Paul


P.S. Next time you ask a question, your reply to Chris Fields is exactly how you should ask a question. It will enable us to better help you   ;)






Paul Cantalupo
University of Pittsburgh


On Mon, Jul 14, 2014 at 1:26 AM, Haiyan Lin <[hidden email]> wrote:
Thanks all reply and advice first.

I want to handle a single can handle data provided in two ways:

1)Read data from a file, such as "perl fastaLen.pl try.fa". No problem
for "-fh=>$filename".

2)Read data from output of cmmand, such as "less try.fa | perl
fastaLen.pl". No probelm when using "-fh=>\*STDIN".


And, I can do this by first read data through <>, and write into a tmp
file, and
creat instance of Bio::SeqIO with "-fh=>\$tmpFile", then remove the tmp
file.  I'm looking for a way to avoiding writing/removing tmp file.

Thanks.

Haiyan






On Sun, 2014-07-13 at 15:12 +0000, Fields, Christopher J wrote:
> Haiyan,
>
> Do you want a test script that uses the DATA handle or a script that can pull in data from STDIN (e.g. from outside)?  They are not the same.  It’s possible to do both but I think you’re conflating purposes here; someone using this script might not expect it to behave both ways.
>
> chris
>
> On Jul 12, 2014, at 9:23 PM, Haiyan Lin <[hidden email]> wrote:
>
> > Hi, geogre,
> >
> > Thanks your code is working for data embedded in code with "__DATA__".
> > But, it failed to hand data from outside. Following is the data and
> > result.
> >
> > [linhy@bioinfo1 Script]$ more try.fa
> >> Contig000001
> > CCACGTAAGAGCACCTGGGTCCCCGCCCGCCAAGCGCCGCGAGCGCCAGCAGCAGCTCGC
> >> hello
> > ATATATTTTT
> > [linhy@bioinfo1 Script]$ cat try.fa | perl try.pl
> > seq is AGAGAGAGA
> > seq is ATATATAT
> >
> > Thanks
> >
> > Haiyan
> >
> >
> >
> > On Sat, 2014-07-12 at 18:36 -0700, george hartzell wrote:
> >> I just did this on my Mac OS X 10.9.4 system with perl 5.18.2:
> >>
> >> cd tmp
> >> mkdir haiyan
> >> cd haiyan
> >> cpanm -n -L local Bio::SeqIO
> >>
> >> perl -Ilocal/lib/perl5 foo.pl
> >>
> >> With the following little program in foo.pl:
> >>
> >> use Bio::SeqIO ;
> >>
> >> my $in = Bio::SeqIO->new(-format=>"Fasta", -fh => \*DATA);
> >> while(my $s = $in->next_seq()){
> >>    print "seq is " . $s->seq . "\n"
> >> }
> >>
> >> __DATA__
> >>> ct1
> >> AGAGAGAGA
> >>> ctg2
> >> ATATATAT
> >>
> >> and it does this when I run it:
> >>
> >> (alacrity)[18:22:58]haiyan>>perl -Ilocal/lib/perl5 foo.pl
> >> seq is AGAGAGAGA
> >> seq is ATATATAT
> >> (alacrity)[18:24:42]haiyan>>
> >>
> >> Can you get the same series of things to work?
> >>
> >> If you’re doing a bunch of this kind of stuff, you might want to look
> >> at Data::Section; rjbs discusses it
> >> here. It’s warmer and fuzzier than dealing with the DATA handle by
> >> yourself.
> >>
> >> g.
> >>
> >> ​
> >
> >
> > _______________________________________________
> > Bioperl-l mailing list
> > [hidden email]
> > http://mailman.open-bio.org/mailman/listinfo/bioperl-l
>




_______________________________________________
Bioperl-l mailing list
[hidden email]
http://mailman.open-bio.org/mailman/listinfo/bioperl-l