Fixes an issue with how mode is detected from input streams for SFF file parsing. Closes #440

This commit is contained in:
Tyghe Vallard
2014-12-10 15:08:16 -05:00
parent 7c1b492cc9
commit e05cbaf744
3 changed files with 28 additions and 12 deletions

View File

@ -253,6 +253,24 @@ _flag = b"\xff"
__docformat__ = "restructuredtext en"
def _check_mode(handle):
'''
Ensures mode is not set for Universal new line
and ensures mode is binary for Windows
'''
mode = ''
if hasattr(handle,'mode'):
mode = str(handle.mode)
if mode and "U" in mode.upper():
raise ValueError("SFF files must NOT be opened in universal new "
"lines mode. Binary mode is recommended (although "
"on Unix the default mode is also fine).")
elif mode and "B" not in mode.upper() \
and sys.platform == "win32":
raise ValueError("SFF files must be opened in binary mode on Windows")
def _sff_file_header(handle):
"""Read in an SFF file header (PRIVATE).
@ -278,13 +296,7 @@ def _sff_file_header(handle):
'TCAG'
"""
if hasattr(handle, "mode") and "U" in handle.mode.upper():
raise ValueError("SFF files must NOT be opened in universal new "
"lines mode. Binary mode is recommended (although "
"on Unix the default mode is also fine).")
elif hasattr(handle, "mode") and "B" not in handle.mode.upper() \
and sys.platform == "win32":
raise ValueError("SFF files must be opened in binary mode on Windows")
_check_mode(handle)
# file header (part one)
# use big endiean encdoing >
# magic_number I
@ -1009,11 +1021,7 @@ class SffWriter(SequenceWriter):
- xml - Optional string argument, xml manifest to be recorded in the index
block (see function ReadRocheXmlManifest for reading this data).
"""
if hasattr(handle, "mode") and "U" in handle.mode.upper():
raise ValueError("SFF files must NOT be opened in universal new "
"lines mode. Binary mode is required")
elif hasattr(handle, "mode") and "B" not in handle.mode.upper():
raise ValueError("SFF files must be opened in binary mode")
_check_mode(handle)
self.handle = handle
self._xml = xml
if index:

Binary file not shown.

View File

@ -86,6 +86,14 @@ class TestUAN(unittest.TestCase):
class TestConcatenated(unittest.TestCase):
def test_parses_gzipped_stream(self):
import gzip
count = 0
fh = gzip.open("Roche/E3MFGYR02_random_10_reads.sff.gz",'rb')
for record in SeqIO.parse(fh, 'sff'):
count += 1
self.assertEqual(10, count)
def test_parse1(self):
count = 0
caught = False