diff util/subtools.py @ 16:3233451a3bd6 draft

planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit fc73ec22a0db3ab09c4ac13dc58f0b54ae37845c
author rmarenco
date Sun, 25 Sep 2016 11:25:38 -0400
parents 25809f699cb3
children 884ee2a71680
line wrap: on
line diff
--- a/util/subtools.py	Fri Sep 02 15:41:51 2016 -0400
+++ b/util/subtools.py	Sun Sep 25 11:25:38 2016 -0400
@@ -28,10 +28,9 @@
     It maps the signature of subprocess.check_call:
     See https://docs.python.org/2/library/subprocess.html#subprocess.check_call
     """
-    stdin = kwargs.get('stdin')
-    stdout = kwargs.get('stdout')
-    stderr = kwargs.get('stderr')
-    shell = kwargs.get('shell')
+    stdout = kwargs.get('stdout', subprocess.PIPE)
+    stderr = kwargs.get('stderr', subprocess.PIPE)
+    shell = kwargs.get('shell', False)
 
     cmd = array_call[0]
 
@@ -45,15 +44,26 @@
 
     # TODO: Use universal_newlines option from Popen?
     try:
-        p = subprocess.Popen(array_call, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell)
+        p = subprocess.Popen(array_call, stdout=stdout, stderr=stderr, shell=shell)
+
+        # TODO: Change this because of possible memory issues => https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate
+
         output, error = p.communicate()
 
-        logging.debug("\t{0}".format(output))
+        if stdout == subprocess.PIPE:
+            logging.debug("\t{0}".format(output))
+        else:
+            logging.debug("\tOutput in file {0}".format(stdout.name))
         # 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)
+            if stderr == subprocess.PIPE:
+                raise PopenError(cmd, error, p.returncode)
+            else:
+                # TODO: To Handle properly with a design behind, if we received a option as a file for the error
+                raise Exception("Error when calling {0}. Error as been logged in your file {1}. Error code: {2}"\
+                                .format(cmd, stderr.name, p.returncode))
 
     except OSError as e:
         message = "The subprocess {0} has encountered an OSError: {1}".format(cmd, e.strerror)
@@ -75,7 +85,6 @@
         sys.exit(-1)
     return p
 
-
 def twoBitInfo(two_bit_file_name, two_bit_info_file):
     """
     Call twoBitInfo and write the result into twoBit_info_file
@@ -87,7 +96,6 @@
     p = _handleExceptionAndCheckCall(array_call)
     return p
 
-
 def faToTwoBit(fasta_file_name, twoBitFile):
     """
     This function call faToTwoBit UCSC tool, and return the twoBitFile
@@ -101,7 +109,6 @@
 
     return twoBitFile
 
-
 def gtfToGenePred(input_gtf_file_name, gene_pred_file_name):
     """
     Call gtfToGenePred and write the result into gene_pred_file_name
@@ -113,7 +120,6 @@
     p = _handleExceptionAndCheckCall(array_call)
     return p
 
-
 def gff3ToGenePred(input_gff3_file_name, gene_pred_file_name):
     """
     Call gff3ToGenePred and write the result into gene_pred_file_name
@@ -149,7 +155,6 @@
     p = _handleExceptionAndCheckCall(array_call)
     return p
 
-
 def sort(unsorted_bed_file_name, sorted_bed_file_name):
     """
     Call sort with -k1,1 -k2,2n on unsorted_bed_file_name and write the result into sorted_bed_file_name
@@ -161,7 +166,6 @@
     p = _handleExceptionAndCheckCall(array_call)
     return p
 
-
 def sortChromSizes(two_bit_info_file_name, chrom_sizes_file_name):
     """
     Call sort with -k2rn on two_bit_info_file_name and write the result into chrom_sizes_file_name
@@ -173,7 +177,6 @@
     p = _handleExceptionAndCheckCall(array_call)
     return p
 
-
 def bedToBigBed(sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name,
                 typeOption=None, autoSql=None, tab=False):
     """
@@ -206,7 +209,6 @@
     p = _handleExceptionAndCheckCall(array_call)
     return p
 
-
 def sortBam(input_bam_file_name, output_sorted_bam_name):
     """
     Call samtools on input_bam_file_name and output the result in output_sorted_bam_name
@@ -218,7 +220,6 @@
     p = _handleExceptionAndCheckCall(array_call)
     return p
 
-
 def createBamIndex(input_sorted_bam_file_name, output_name_index_name):
     """
     Call `samtools index` on imput_sorted_bam_file_name and output the result in output_name_index_name
@@ -229,3 +230,16 @@
     array_call = ['samtools', 'index', input_sorted_bam_file_name, output_name_index_name]
     p = _handleExceptionAndCheckCall(array_call)
     return p
+
+def pslToBigPsl(input_psl_file_name, output_bed12_file_name):
+    """
+    Call `pslToBigPsl` on input_psl_file_name and output the result in output_bed12_file_name
+    :param input_psl_file_name: Name of the psl input file
+    :param output_bed12_file_name: Name of the output file where to store the result of the cmd
+    :return:
+    """
+    # The command to send
+    array_call = ['pslToBigPsl', input_psl_file_name, output_bed12_file_name]
+
+    p = _handleExceptionAndCheckCall(array_call)
+    return p