diff --git a/mutalyzer/util.py b/mutalyzer/util.py index 4a3ac13c1dc73d165cff23a68571341629e29dfa..1ef1987f2c9747b2ac25ad6b31b8344fbf50b0de 100644 --- a/mutalyzer/util.py +++ b/mutalyzer/util.py @@ -357,15 +357,28 @@ def guess_file_type(handle): """ Guess the file type of an NGS data file. - We assume that the stream is rewinded before use, after use, the input - stream will be rewinded. - - :arg stream handle: Open readable handle to an NGS data file. + :arg file handle: Open readable handle to an NGS data file. :returns unicode: Either 'fasta', 'fastq' or 'text'. """ + try: + extension = getattr(handle, 'name').split('.')[-1] + except AttributeError: + pass + else: + if extension in ('fastq', 'fq'): + return 'fastq' + elif extension in ('fasta', 'fa'): + return 'fasta' + + try: + position = handle.tell() + handle.seek(0) + except IOError: + return 'text' + token = handle.read(1) - handle.seek(0) + handle.seek(position) if token == '>': return 'fasta'