[Bioperl-l] seqIO and file handle

Jason Stajich jason@cgt.mc.duke.edu
Thu, 25 Jul 2002 18:33:51 -0400 (EDT)


Damien -

Glad you got it to work with the suggestions, I think the IO::String
approach is much better.

In the future you may still need to write temp seq files to disk - want to
make sure you do it correctly then.  Your code below almost would have
been okay with the code below but you close a filehandle that is not used
as you ended up passing in the filename rather than the tempfile handle
that is created so you effectively create a second filehandle for the same
filename.

To use the tempfilehandle that comes back from tempfile routine you do:

my ($fh,$tempname) = tempfile()

my $seqio= Bio::SeqIO->new('-fh' => $fh);

And insure it is closed you do:
... do something ...
$seqio->close();
 _OR_
close($fh);

This is why you weren't getting the flushed data even though you were
closing the FH explicitly.

Note, that we (Aaron Mackey actually) have fixed the SeqIO module to flush
after each sequence write by default.


-jason

On Thu, 25 Jul 2002, Damien Mattei wrote:

>
> i have a problem writing sequence in a temp file with seqIO
> and reading it immediately the end of file is truncated even if it is
> not on disk.
>
> here is my code:
>
> # create temporary sequence file in specific format
> # (genbank, fasta or embl) and write it in text widget
> # param 1 : format (Fasta,GenBank,EMBL)
> # param 2 : sequence
> sub  seqtmptxt {
>
>      my ($format,$seq) = @_;
>      my ($fh, $name_file) = tempfile( SUFFIX => '.' . $format);
>      print $name_file . "\n" ;
>      my $seqio = Bio::SeqIO->new('-format' => $format,-file =>
> ">$name_file");
>      $seqio->write_seq($seq);
>      close($fh);
>      print "Sequence written in tmp file\n";
>      #$scrolled_texte->configure(-state => 'normal');
>      #$scrolled_texte->delete("1.0","end");
>
>      #`tail $name_file`;
>
>      open(TMPFILE,"< $name_file") or goto NOOPEN;
>      my $buffer;
>      while (my $byteread = read(TMPFILE, $buffer, 16384)) {
>      #while (my $record = <TMPFILE> ) {
> 	#print "byte read = $byteread\n";
> 	#$scrolled_texte->insert('end', $buffer);
> 	print $buffer;
>
>      }
>      print $buffer;
>      close TMPFILE or print "impossible to close $name_file : $!";
>      #$scrolled_texte->configure(-state => 'disabled');
>      unlink $name_file;
>      return;
>    NOOPEN:
>      print "impossible to open $name_file : $!";
> }
>
> i comment out all the Tk relative code to don't annoy readers and assume
> the problem is that the problem is with file handler.
> If i add `tail $name_file`; (see in code where it is commented) the file
> display well, so there should be a way to flush this file but i don't
> know how.
>
> Damien
>
> --------------------------------------
> Damien Mattei
> C.N.R.S / U.N.S.A - UMR 6549
> mailto:mattei@unice.fr
> http://www-iag.unice.fr/
> --------------------------------------
>
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l@bioperl.org
> http://bioperl.org/mailman/listinfo/bioperl-l
>

-- 
Jason Stajich
Duke University
jason at cgt.mc.duke.edu