[Bioperl-l] capture of STDOUT from write_seq?

Jason Stajich jason.stajich at duke.edu
Thu Feb 10 09:00:11 EST 2005


You can't easily open bidirectional pipes with the standard open 
because it requires you to fork()/ threading and have two processes, 
one doing the writing and another doing the reading.  That is what UNIX 
does when you have multiple pipes and one process will block while it 
waits for output from the other one.  So I don't imagine a 
Bioperl-specific solution to any of this, it will have to be a Perl 
solution.  In general I'd have to see a big win for this type of thing 
over just redirecting output to a temp file and then open the tempfile 
as a second step rather than trying to do it all simultaneously.

I can imagine some UNIX specific soln's like binding sockets to files 
so the STDOUT redirection is to a virtual file which can be treated as 
a filehandle in perl.  I think this is what you can do with IO::Pipe 
but just didn't have time to really get it working.  Either way, you 
still have to do some sort of fork()/threading if you really want to 
write / read at the same time.  My feeling is this starts to be a bit 
more complicated than most people want, esp if you can just redirect 
output to a file and do things sequentially.  That's the soln we have 
chosen for most of Tools::Run apps where we pipe input and grab the 
output from a file.

I'm sure we can try and whip up example code that actually works, but 
in general this will not be a bioperl solution as the objects just talk 
to generic filehandles and don't care how the underlying processes are 
being executed (STDOUT redirection or not).

Happy for other people to weigh in on pros/cons.

-jason

--
Jason Stajich
jason.stajich at duke.edu
http://www.duke.edu/~jes12/

On Feb 9, 2005, at 11:51 PM, Neil Saunders wrote:

> thanks guys,
>
> with Jason's extra pipe, a '2>/dev/null' to remove STDERR and a 'close
> $fh', this works nicely.  I'm also glad to see I'm not the only one who
> finds IPC:: a bit tricky!
>
> On a related note, there seem to be a lot of tools in Bioperl to pipe
> data to an external process (e.g. Bio::SeqIO as described here, and
> Bio::Tools), but getting data back from that process requires either
> writing to then reading from a file or the filehandle tricks we have
> discussed.  I wonder if building in the code to easily get STDOUT from 
> a
> system call back into a Bioperl object is on the cards for future
> releases?
>
>
> Neil
>
> On Wed, Feb 09, 2005 at 11:03:00AM -0500, Jason Stajich wrote:
>> I think you want a pipe at the end of that open - however, this might
>> not work on long sequences though...
>> open $fh, "echo '$seq' | hmmtop -if=-- -sf=FAS -pi=spred -is=pseudo 
>> |";
>>
>> Because what you want is reading and writing to the same filehandle 
>> you
>> have to play some tricks.  I think IO::Pipe or IPC::Run3 might help,
>> but I couldn't get it to work right in my little bit of playing 
>> around.
>>
>> -jason
>> --
>> Jason Stajich
>> jason.stajich at duke.edu
>> http://www.duke.edu/~jes12/
>>
>> On Feb 9, 2005, at 8:43 AM, Marc Logghe wrote:
>>
>>> It is probably not really optimal, but this should work:
>>>
>>> use IO::String;
>>>
>>> while(my $seqin = $seqio->next_seq) {
>>>  my $seq = '';
>>>  my $io = IO::String->new($seq);
>>>  my $fh;
>>>
>>>  # print fasta to scalar $seq
>>>  my $seqout = Bio::SeqIO->new(-fh => $io, '-format' => 'Fasta');
>>>  $seqout->write_seq($seqin);
>>>
>>>  # pipe fasta to hmmtop and catch output
>>>  open $fh, "echo '$seq' | hmmtop -if=-- -sf=FAS -pi=spred 
>>> -is=pseudo";
>>>  my ($s) = <$fh>;
>>>  print $s;
>>> }
>>>
>>> HTH,
>>> Marc
>>>
>>>> -----Original Message-----
>>>> From: bioperl-l-bounces at portal.open-bio.org
>>>> [mailto:bioperl-l-bounces at portal.open-bio.org]On Behalf Of Heikki
>>>> Lehvaslaiho
>>>> Sent: Wednesday, February 09, 2005 12:23 PM
>>>> To: bioperl-l at portal.open-bio.org
>>>> Cc: Neil Saunders; bioperl-l at bioperl.org
>>>> Subject: Re: [Bioperl-l] capture of STDOUT from write_seq?
>>>>
>>>>
>>>> Neil,
>>>>
>>>> In this case the best place to look inot is the Bio::SeqIO module
>>>> documentation. From the SYNOPSIS:
>>>>
>>>>    # The SeqIO system does have a filehandle binding. Most
>>>> people find this
>>>>    # a little confusing, but it does mean you write the
>>>> world's smallest
>>>>    # reformatter
>>>>
>>>>    use Bio::SeqIO;
>>>>    $in  = Bio::SeqIO->newFh(-file => "inputfilename" ,
>>>>                             -format => 'Fasta');
>>>>    $out = Bio::SeqIO->newFh(-format => 'EMBL');
>>>>    # World's shortest Fasta<->EMBL format converter:
>>>>    print $out $_ while <$in>;
>>>>
>>>> In other words, if you do not specify a filename the newFh
>>>> method defaults to
>>>> STDOUT for output.
>>>>
>>>> Yours,
>>>>       -Heikki
>>>>
>>>>
>>>> On Wednesday 09 February 2005 01:47, Neil Saunders wrote:
>>>>> dear all,
>>>>>
>>>>> I'm using Bio::SeqIO to pipe fasta sequences to an external program
>>>>> (hmmtop), as follows:
>>>>>
>>>>> while(my $seqin = $seqio->next_seq) {
>>>>> my $seqout = Bio::SeqIO->new('-file' => "| hmmtop -if=--
>>>> -sf=FAS -pi=spred
>>>>> -is=pseudo", '-format' => 'Fasta'); $seqout->write_seq($seqin);
>>>>>
>>>>>
>>>>> This works nicely and spits out the 1 line STDOUT from
>>>> hmmtop for each
>>>>> sequence.  What I would really like to do is capture the STDOUT as 
>>>>> a
>>>>> variable in my Perl script.  I've read numerous Perl docs
>>>> (e.g. the FAQ
>>>>> on capturing STDERR/STDOUT, perlipc, use of backticks
>>>> versus open versus
>>>>> system and so on), but I'm unclear on how these relate to
>>>> the example
>>>>> above.  Is there some cunning bioperl-specific way of
>>>> directing STDOUT
>>>>> from an external call to a perl variable, perhaps via a filehandle?
>>>>>
>>>>> your help much appreciated,
>>>>> Neil
>>>>
>>>> -- 
>>>> ______ _/      
>>>> _/_____________________________________________________
>>>>      _/      _/                      http://www.ebi.ac.uk/mutations/
>>>>     _/  _/  _/  Heikki Lehvaslaiho    heikki at_ebi _ac _uk
>>>>    _/_/_/_/_/  EMBL Outstation, European Bioinformatics Institute
>>>>   _/  _/  _/  Wellcome Trust Genome Campus, Hinxton
>>>>  _/  _/  _/  Cambridge, CB10 1SD, United Kingdom
>>>>     _/      Phone: +44 (0)1223 494 644   FAX: +44 (0)1223 494 468
>>>> ___ 
>>>> _/_/_/_/_/________________________________________________________
>>>> _______________________________________________
>>>> Bioperl-l mailing list
>>>> Bioperl-l at portal.open-bio.org
>>>> http://portal.open-bio.org/mailman/listinfo/bioperl-l
>>>>
>>>
>>> _______________________________________________
>>> Bioperl-l mailing list
>>> Bioperl-l at portal.open-bio.org
>>> http://portal.open-bio.org/mailman/listinfo/bioperl-l
>>>
>
>
>
> -- 
>  School of Biotechnology and Biomolecular Sciences,
>  The University of New South Wales,
>  Sydney 2052,
>  Australia
>
> http://psychro.bioinformatics.unsw.edu.au/neil/index.php
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l at portal.open-bio.org
> http://portal.open-bio.org/mailman/listinfo/bioperl-l
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : http://portal.open-bio.org/pipermail/bioperl-l/attachments/20050210/c9e1a15c/PGP.bin


More information about the Bioperl-l mailing list