Indicate when desplit_fastq didn't get any files to process
[htsworkflow.git] / htsworkflow / pipelines / desplit_fastq.py
index 0624aaa290a170a57593d85f07b0b642a1743e46..c9947a2ef2317b0a05f7c36e966eb152949fc1f3 100644 (file)
@@ -1,13 +1,14 @@
 #!/usr/bin/env python
 """Write fastq data from multiple compressed files into a single file
 """
-
+import bz2
+import gzip
 from glob import glob
 import os
 from optparse import OptionParser
 import sys
 
-from htsworkflow.version import version
+from htsworkflow.util.version import version
 from htsworkflow.util.opener import autoopen
 from htsworkflow.util.conversion import parse_slice
 
@@ -29,7 +30,12 @@ def main(cmdline=None):
         return 0
 
     if opts.output is not None:
-        output = open(opts.output, 'w')
+        if opts.bzip:
+            output = bz2.BZ2File(opts.output,'w')
+        elif opts.gzip:
+            output = gzip.GzipFile(opts.output, 'w')
+        else:
+            output = open(opts.output, 'w')
     else:
         output = sys.stdout
 
@@ -51,6 +57,10 @@ def make_parser():
     parser.add_option('-s', '--slice',
                       help="specify python slice, e.g. 0:75, 0:-1",
                       default=None)
+    parser.add_option('--gzip', default=False, action='store_true',
+                      help='gzip output')
+    parser.add_option('--bzip', default=False, action='store_true',
+                      help='bzip output')
     parser.add_option("--version", default=False, action="store_true",
                       help="report software version")
     return parser
@@ -79,7 +89,9 @@ class DesplitFastq(object):
         This is here so we can run via threading/multiprocessing APIs
         """
         state = SEQ_HEADER
+        files_read = 0
         for stream in self.sources:
+            files_read += 1
             for line in stream:
                 line = line.rstrip()
                 if state == SEQ_HEADER:
@@ -96,6 +108,8 @@ class DesplitFastq(object):
                     state = SEQ_HEADER
                 self.destination.write(os.linesep)
 
+        if files_read == 0:
+            raise RuntimeError("No files processed")
 
 if __name__ == "__main__":
     main()