[Biopython] File format autodetection.

Peter Cock p.j.a.cock at googlemail.com
Tue Jun 24 21:09:42 UTC 2014


On Tue, Jun 24, 2014 at 7:39 PM, Ivan Gregoretti <ivangreg at gmail.com> wrote:
> OK. I figured it out.

Well done :)

> It is used like this
>
> from Bio.File import UndoHandle
> fileN = 'myfile.fq' # this would be a STDIN stream in my case.
> fileH = UndoHandle(open(fileN_i,'r'))
>
> # and then to get the first line, do
> first_line = fileH.peekline()
>
>
> So, now to figure out whether the stream is FASTA or FASTQ, I will
> take the first character of that first line (first_line[0]). If that
> character is "@", then the stream's format is FASTQ; if it is ">",
> then the format is FASTA.
>
> Thank you gentlemen for your help.
>
> Ivan

Yes, something like that, or perhaps first_line.startswith(">")
which shouldn't give you an exception if the file is empty ;)

Peter


More information about the Biopython mailing list