[Bioperl-l] capture of STDOUT from write_seq?

Neil Saunders neil.saunders at unsw.edu.au
Wed Feb 9 23:51:39 EST 2005


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


More information about the Bioperl-l mailing list