changeset 2:14567aa2be12 draft default tip

Added wrapper script for correct stderr handling
author geert-vandeweyer
date Thu, 10 Apr 2014 09:44:09 -0400
parents 3182c7fac413
children
files bcftools_cat.xml bcftools_index.xml bcftools_view.xml bcftools_wrapper.py
diffstat 4 files changed, 62 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/bcftools_cat.xml	Thu Jul 11 14:27:32 2013 -0500
+++ b/bcftools_cat.xml	Thu Apr 10 09:44:09 2014 -0400
@@ -5,7 +5,7 @@
         <requirements type="package">samtools</requirements>
   </requirements>
 
-  <command interpreter="python">bcftools cat $rinput > $output  </command>
+  <command interpreter='python'>bcftools_wrapper.py bcftools cat $rinput > $output  </command>
 
   <inputs>
     <repeat name="rinput" title="BCF File">
@@ -39,4 +39,4 @@
 
     </help>
 
-</tool>
\ No newline at end of file
+</tool>
--- a/bcftools_index.xml	Thu Jul 11 14:27:32 2013 -0500
+++ b/bcftools_index.xml	Thu Apr 10 09:44:09 2014 -0400
@@ -5,7 +5,7 @@
         <requirement type="package" version="0.1.18">samtools</requirement>
     </requirements>
 
-  <command interpreter="python">bcftools index $input > $output </command>
+  <command interpreter='python' >bcftools_wrapper.py bcftools index $input > $output </command>
 
   <inputs>
     <param  name="input" type="data" format="bcf" label="BCF File"/>
@@ -37,4 +37,4 @@
 
     </help>
 
-</tool>
\ No newline at end of file
+</tool>
--- a/bcftools_view.xml	Thu Jul 11 14:27:32 2013 -0500
+++ b/bcftools_view.xml	Thu Apr 10 09:44:09 2014 -0400
@@ -3,8 +3,8 @@
     <requirements>
         <requirement type="package" version="0.1.18">samtools</requirement>
     </requirements>
-    <command interpreter="python">
-        bcftools view 
+    <command interpreter='python'>
+        bcftools_wrapper.py bcftools view 
             #if str( $A ) == "true": 
                 -A
             #end if
@@ -51,7 +51,7 @@
                 -v
 	    #end if	
 	$input
-        > $output
+        > $output 
     </command>
     <inputs>
         <param name="input" type="data" format="bcf" label="Choose a bcf file to view" />
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bcftools_wrapper.py	Thu Apr 10 09:44:09 2014 -0400
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+
+"""
+Wrapper that execute a program and its arguments but reports standard error
+messages only if the program exit status was not 0
+Example: ./stderr_wrapper.py myprog arg1 -f arg2
+
+Taken from bcftools_view package of gregory-minevich
+"""
+
+import sys, subprocess
+
+assert sys.version_info[:2] >= ( 2, 4 )
+
+def stop_err( msg ):
+    sys.stderr.write( "%s\n" % msg )
+    sys.exit()
+
+def __main__():
+    # Get command-line arguments
+    args = sys.argv
+    # Remove name of calling program, i.e. ./stderr_wrapper.py
+    args.pop(0)
+    # If there are no arguments left, we're done
+    if len(args) == 0:
+        return
+
+    # If one needs to silence stdout 
+    #args.append( ">" )
+    #args.append( "/dev/null" )
+
+    cmdline = " ".join(args)
+    try:
+        # Run program
+        proc = subprocess.Popen( args=cmdline, shell=True, stderr=subprocess.PIPE )
+        returncode = proc.wait()
+        # Capture stderr, allowing for case where it's very large
+        stderr = ''
+        buffsize = 1048576
+        try:
+            while True:
+                stderr += proc.stderr.read( buffsize )
+                if not stderr or len( stderr ) % buffsize != 0:
+                    break
+        except OverflowError:
+            pass
+        # Running BCFtools failed: write error message to stderr
+        if returncode != 0:
+            raise Exception, stderr
+    except Exception, e:
+        # Running BCFtools failed: write error message to stderr
+        stop_err( 'Error:\n' + str( e ) )
+
+
+if __name__ == "__main__": __main__()