<div dir="ltr"><div><div>Hi Haiyan,<br><br></div>Ah your specifications are now clearer! I use the '-p' file test to check if STDIN is a named pipe (<a href="http://perldoc.perl.org/functions/-X.html">http://perldoc.perl.org/functions/-X.html</a>) to do the same thing that you are trying to do:<br>
<br><span style="font-family:courier new,monospace">use strict;<br>use warnings;<br>use Bio::SeqIO;<br>my $in;<br>if (-p STDIN) {<br> $in = Bio::SeqIO->new(-fh => \*STDIN, -format => 'fasta');<br>}<br>else {<br>
$in = Bio::SeqIO->new(-file => shift);<br>}<br>while (my $seq = $in->next_seq) {<br> print $seq->length,"\n";<br>}<br><br></span><br>Here is my input file and results when running both ways:<br><br>
<span style="font-family:courier new,monospace">$ cat foo.fa<br>>foo1<br>tgtagtc<br>>foo2<br>taaaacgtgtcat<br><br>$ cat foo.fa | <a href="http://both.pl">both.pl</a><br>7<br>13<br><br>$ <a href="http://both.pl">both.pl</a> foo.fa<br>
7<br>13</span><br><br></div><div>Hope this helps, <br><br>Paul<br></div><div><br><br></div><div>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 ;)<br>
<br><br></div><br><br><br></div><div class="gmail_extra"><br clear="all"><div>Paul Cantalupo<br>University of Pittsburgh<br></div>
<br><br><div class="gmail_quote">On Mon, Jul 14, 2014 at 1:26 AM, Haiyan Lin <span dir="ltr"><<a href="mailto:linhy0120@gmail.com" target="_blank">linhy0120@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Thanks all reply and advice first.<br>
<br>
I want to handle a single can handle data provided in two ways:<br>
<br>
1)Read data from a file, such as "perl fastaLen.pl try.fa". No problem<br>
for "-fh=>$filename".<br>
<br>
2)Read data from output of cmmand, such as "less try.fa | perl<br>
fastaLen.pl". No probelm when using "-fh=>\*STDIN".<br>
<br>
<br>
And, I can do this by first read data through <>, and write into a tmp<br>
file, and<br>
creat instance of Bio::SeqIO with "-fh=>\$tmpFile", then remove the tmp<br>
file. I'm looking for a way to avoiding writing/removing tmp file.<br>
<br>
Thanks.<br>
<span class="HOEnZb"><font color="#888888"><br>
Haiyan<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
<br>
<br>
<br>
<br>
On Sun, 2014-07-13 at 15:12 +0000, Fields, Christopher J wrote:<br>
> Haiyan,<br>
><br>
> 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.<br>
><br>
> chris<br>
><br>
> On Jul 12, 2014, at 9:23 PM, Haiyan Lin <<a href="mailto:linhy0120@gmail.com">linhy0120@gmail.com</a>> wrote:<br>
><br>
> > Hi, geogre,<br>
> ><br>
> > Thanks your code is working for data embedded in code with "__DATA__".<br>
> > But, it failed to hand data from outside. Following is the data and<br>
> > result.<br>
> ><br>
> > [linhy@bioinfo1 Script]$ more try.fa<br>
> >> Contig000001<br>
> > CCACGTAAGAGCACCTGGGTCCCCGCCCGCCAAGCGCCGCGAGCGCCAGCAGCAGCTCGC<br>
> >> hello<br>
> > ATATATTTTT<br>
> > [linhy@bioinfo1 Script]$ cat try.fa | perl <a href="http://try.pl" target="_blank">try.pl</a><br>
> > seq is AGAGAGAGA<br>
> > seq is ATATATAT<br>
> ><br>
> > Thanks<br>
> ><br>
> > Haiyan<br>
> ><br>
> ><br>
> ><br>
> > On Sat, 2014-07-12 at 18:36 -0700, george hartzell wrote:<br>
> >> I just did this on my Mac OS X 10.9.4 system with perl 5.18.2:<br>
> >><br>
> >> cd tmp<br>
> >> mkdir haiyan<br>
> >> cd haiyan<br>
> >> cpanm -n -L local Bio::SeqIO<br>
> >><br>
> >> perl -Ilocal/lib/perl5 <a href="http://foo.pl" target="_blank">foo.pl</a><br>
> >><br>
> >> With the following little program in <a href="http://foo.pl" target="_blank">foo.pl</a>:<br>
> >><br>
> >> use Bio::SeqIO ;<br>
> >><br>
> >> my $in = Bio::SeqIO->new(-format=>"Fasta", -fh => \*DATA);<br>
> >> while(my $s = $in->next_seq()){<br>
> >> print "seq is " . $s->seq . "\n"<br>
> >> }<br>
> >><br>
> >> __DATA__<br>
> >>> ct1<br>
> >> AGAGAGAGA<br>
> >>> ctg2<br>
> >> ATATATAT<br>
> >><br>
> >> and it does this when I run it:<br>
> >><br>
> >> (alacrity)[18:22:58]haiyan>>perl -Ilocal/lib/perl5 <a href="http://foo.pl" target="_blank">foo.pl</a><br>
> >> seq is AGAGAGAGA<br>
> >> seq is ATATATAT<br>
> >> (alacrity)[18:24:42]haiyan>><br>
> >><br>
> >> Can you get the same series of things to work?<br>
> >><br>
> >> If you’re doing a bunch of this kind of stuff, you might want to look<br>
> >> at Data::Section; rjbs discusses it<br>
> >> here. It’s warmer and fuzzier than dealing with the DATA handle by<br>
> >> yourself.<br>
> >><br>
> >> g.<br>
> >><br>
> >> <br>
> ><br>
> ><br>
> > _______________________________________________<br>
> > Bioperl-l mailing list<br>
> > <a href="mailto:Bioperl-l@mailman.open-bio.org">Bioperl-l@mailman.open-bio.org</a><br>
> > <a href="http://mailman.open-bio.org/mailman/listinfo/bioperl-l" target="_blank">http://mailman.open-bio.org/mailman/listinfo/bioperl-l</a><br>
><br>
<br>
<br>
</div></div></blockquote></div><br></div>