From 1b8ed5e9010708808dc03681296c486737e54ab6 Mon Sep 17 00:00:00 2001
From: Martijn Vermaat <martijn@vermaat.name>
Date: Thu, 9 Jul 2015 12:14:16 +0200
Subject: [PATCH] Improve file type detection when reading DNA

---
 mutalyzer/util.py | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/mutalyzer/util.py b/mutalyzer/util.py
index 4a3ac13c..1ef1987f 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'
-- 
GitLab