diff --git a/Bio/SeqIO/SffIO.py b/Bio/SeqIO/SffIO.py index a43acf976..feaf84400 100644 --- a/Bio/SeqIO/SffIO.py +++ b/Bio/SeqIO/SffIO.py @@ -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: diff --git a/Tests/Roche/E3MFGYR02_random_10_reads.sff.gz b/Tests/Roche/E3MFGYR02_random_10_reads.sff.gz new file mode 100644 index 000000000..c0539ab23 Binary files /dev/null and b/Tests/Roche/E3MFGYR02_random_10_reads.sff.gz differ diff --git a/Tests/test_SffIO.py b/Tests/test_SffIO.py index 7202ca20e..e0bc6656e 100644 --- a/Tests/test_SffIO.py +++ b/Tests/test_SffIO.py @@ -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