[BioPython] String instead of file
Petri Loukasmaki (Genomics)
ploukasm@fantti.btk.utu.fi
Tue, 11 Sep 2001 15:18:18 +0300 (EEST)
On Tue, 11 Sep 2001, Yair Benita wrote:
> Fasta files can be easily parsed like this:
>
> parser = Fasta.RecordParser()
> iterator = Fasta.Iterator(File.txt, parser)
> cur_record = iterator.next()
>
> But, how do I parse strings and not a file with this parser?
> I am creating sequences and I want to write the in a Fasta format.
> Simply making a handle does not work, the error is:
>
> ValueError: I expected a file handle of file-like object
If I remeber correct, the parameter must be a file-interface satisfying
object. With string, this can be archieved by using memoryfiles.
In python, this is implemented by StringIO class, which handles strings as
file-like objects.
So (in theory) following should work:
import StringIO
mystring="a fasta string..."
mystringfile=StringIO.StringIO(mystring)
iterator = Fasta.Iterator(mystringfile, parser)
...
cheers;
------ ---- --- -- -- -- - - - -
Petri Loukasmäki petri.loukasmaki@btk.utu.fi
Research Assistant / BioInformatics tel +358 (0)2 333 8622
Turku Centre for Biotechnology
Tykistökatu 6B, 5th floor
P.O.Box 123, BioCity
Turku, FIN-20521
FINLAND