diff util/subtools.py @ 13:25809f699cb3 draft

planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 65ab931ef2b05a5acf06cbde3a746c94a0a0a4cb
author rmarenco
date Thu, 11 Aug 2016 19:02:29 -0400
parents acc233161f50
children 3233451a3bd6
line wrap: on
line diff
--- a/util/subtools.py	Wed Jul 27 10:43:58 2016 -0400
+++ b/util/subtools.py	Thu Aug 11 19:02:29 2016 -0400
@@ -6,9 +6,21 @@
 in HubArchiveCreator
 """
 
+import logging
 import os
 import subprocess
+import sys
 
+class PopenError(Exception):
+    def __init__(self, cmd, error, return_code):
+        self.cmd = cmd
+        self.error = error
+        self.return_code = return_code
+
+    def __str__(self):
+        message = "The subprocess {0} has returned the error: {1}.".format(self.cmd, self.return_code)
+        message = ','.join((message, "Its error message is: {0}".format(self.error)))
+        return repr(message)
 
 def _handleExceptionAndCheckCall(array_call, **kwargs):
     """
@@ -20,10 +32,47 @@
     stdout = kwargs.get('stdout')
     stderr = kwargs.get('stderr')
     shell = kwargs.get('shell')
+
+    cmd = array_call[0]
+
+    output = None
+    error = None
+
+    # TODO: Check the value of array_call and <=[0]
+    logging.debug("Calling {0}:".format(cmd))
+
+    logging.debug("---------")
+
+    # TODO: Use universal_newlines option from Popen?
     try:
-        p = subprocess.check_call(array_call, stdin=stdin, stdout=stdout, stderr=stderr, shell=shell)
-    except subprocess.CalledProcessError:
-        raise
+        p = subprocess.Popen(array_call, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell)
+        output, error = p.communicate()
+
+        logging.debug("\t{0}".format(output))
+        # If we detect an error from the subprocess, then we raise an exception
+        # TODO: Manage if we raise an exception for everything, or use CRITICAL etc... but not stop process
+        # TODO: The responsability of returning a sys.exit() should not be there, but up in the app.
+        if p.returncode:
+            raise PopenError(cmd, error, p.returncode)
+
+    except OSError as e:
+        message = "The subprocess {0} has encountered an OSError: {1}".format(cmd, e.strerror)
+        if e.filename:
+            message = '\n'.join((message, ", against this file: {0}".format(e.filename)))
+        logging.error(message)
+        sys.exit(-1)
+    except PopenError as p:
+        message = "The subprocess {0} has returned the error: {1}.".format(p.cmd, p.return_code)
+        message = '\n'.join((message, "Its error message is: {0}".format(p.error)))
+
+        logging.exception(message)
+
+        sys.exit(p.return_code)
+    except Exception as e:
+        message = "The subprocess {0} has encountered an unknown error: {1}".format(cmd, e)
+        logging.exception(message)
+
+        sys.exit(-1)
     return p
 
 
@@ -76,6 +125,18 @@
     p = _handleExceptionAndCheckCall(array_call)
     return p
 
+def genePredToBigGenePred(gene_pred_file_name, unsorted_bigGenePred_file_name):
+    """
+    Call genePredToBigGenePred and write the result into unsorted_bigGenePred_file_name
+    :param gene_pred_file_name:
+    :param unsorted_bigGenePred_file_name:
+    :return:
+    """
+    array_call = ['genePredToBigGenePred',
+                  gene_pred_file_name,
+                  unsorted_bigGenePred_file_name]
+    p = _handleExceptionAndCheckCall(array_call)
+    return p
 
 def genePredToBed(gene_pred_file_name, unsorted_bed_file_name):
     """
@@ -113,7 +174,8 @@
     return p
 
 
-def bedToBigBed(sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name, typeOption=None, autoSql=None):
+def bedToBigBed(sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name,
+                typeOption=None, autoSql=None, tab=False):
     """
     Call bedToBigBed on sorted_bed_file_name, using chrom_sizes_file_name and write the result into big_bed_file_name
     :param sorted_bed_file_name:
@@ -121,11 +183,25 @@
     :param big_bed_file_name:
     :return:
     """
+
+    # TODO: Move this into the _handleExceptionAndCheckCall function
+    # Parse the array
+    logging.debug("sorted_bed_file_name: {0}".format(sorted_bed_file_name))
+    logging.debug("chrom_sizes_file_name: {0}".format(chrom_sizes_file_name))
+    logging.debug("big_bed_file_name: {0}".format(big_bed_file_name))
+    logging.debug("typeOption: {0}".format(typeOption))
+    logging.debug("autoSql: {0}".format(autoSql))
+    logging.debug("tab option: {0}".format(tab))
+
     array_call = ['bedToBigBed', sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name]
     if typeOption:
+        typeOption = ''.join(['-type=', typeOption])
         array_call.append(typeOption)
     if autoSql:
+        autoSql = ''.join(['-as=', autoSql])
         array_call.append(autoSql)
+    if tab:
+        array_call.append('-tab')
 
     p = _handleExceptionAndCheckCall(array_call)
     return p