diff jammwrapper.py @ 1:243f75d0ed6e draft default tip

Uploaded. Includes new release 1.0.7 with fixed optional controls.
author messersc
date Thu, 19 Feb 2015 05:39:45 -0500
parents d42f4d78c85e
children
line wrap: on
line diff
--- a/jammwrapper.py	Wed Dec 17 10:40:23 2014 -0500
+++ b/jammwrapper.py	Thu Feb 19 05:39:45 2015 -0500
@@ -6,7 +6,7 @@
 # map the files (from Galaxy history) to a directory
 # pass the parameters from the GUI to JAMM.sh
 # call JAMM.sh
-# maybe map the resulting tabular files back to history items, the XML could take care of that if output is fixed
+# map the resulting tabular files back to history items
 
 #import optparse
 import argparse, os, shutil, subprocess, sys, tempfile
@@ -22,48 +22,77 @@
 
     #Read command line arguments
     parser = argparse.ArgumentParser()
-    parser.add_argument('-i', dest = 'input', nargs='+')
+    parser.add_argument('-i', dest = 'input', nargs='+') # + allows for 1 or more arguments
+    # parser.add_argument('-c', dest = 'control', nargs='*', default=None) # allow for not supplying -c
+    parser.add_argument('-c', dest = 'control', action='append') # allow for not supplying -c
     parser.add_argument('-g', dest = 'gsize')
-    parser.add_argument('-z', dest = 'peakfile')
+    parser.add_argument('-o', dest = 'peakfile')
+    parser.add_argument('-of', dest = 'filteredpeakfile')
     
     parser.add_argument('-m', dest = 'mode')
     parser.add_argument('-r', dest = 'resolution')
     parser.add_argument('-p', dest = 'processes')
-    #parser.add_argument('-b', dest = 'binSize')
+    parser.add_argument('-t', dest = 'type')
+    parser.add_argument('-f', dest = 'fraglen')
+    parser.add_argument('-b', dest = 'binsize')
     
     args = parser.parse_args()
    
-    print "\n"
+    print "################################################" 
     print "Wrapper debugging" 
-    print "Files to be used:"
+    print "################################################" 
+    print "Sample files:"
     for j in args.input:
         print j
+    if args.control is not None:
+        print "Control files:"
+        for j in args.control:
+            print j
 
-    print "output file:"
+    print "output files:"
     print args.peakfile
+    print args.filteredpeakfile
+    print "current working dir:"
+    print os.getcwd() 
+    print "dir with jammwrapper in it:"
+    path = os.path.abspath(os.path.dirname(sys.argv[0]))
+    print path
 
-    # depracted, can still be found in may of the example wrappers    
+    # optparse was depracted, can still be found in may of the example wrappers
     #parser = optparse.OptionParser()
     #parser.add_option( '-i',  dest='input', help='input bed files' )
     #parser.add_option( '-c',  dest='csize', help='chr sizes' )
     #(options, args) = parser.parse_args()	
-   
+    
     # create temp dir
     tmp_dir = tempfile.mkdtemp()
-    os.chdir(tmp_dir)
+    os.mkdir(tmp_dir + "/sample")
     # symlink creation
     for file in args.input:
-        filen =  tmp_dir + "/" + os.path.basename(os.path.splitext(file)[0])+".bed"
+        filen =  tmp_dir + "/sample/" + os.path.basename(os.path.splitext(file)[0])+".bed"
+        print "input files mapped: %s" % filen
         os.symlink(file, filen)
-        
-        # in case temp files should have random names
-        # ref_file = tempfile.NamedTemporaryFile( dir=tmp_dir )
-        # ref_file_name = ref_file.name
-        # ref_file.close()
-        # os.symlink( file, ref_file_name )
-    
-    command = ( "/home/cmesser/galaxy/tools/jamm/JAMM.sh -s %s -g %s -o results -m %s -r %s -p %s"
-     % ( tmp_dir, args.gsize, args.mode, args.resolution, args.processes ) ) 
+   
+    # Here comes some unnecessary repetition
+    # if control files are supplied, we make another tmp subdir and put those files there.
+    # JAMM.sh is then called with one additional switch -c
+    if args.control is not None:
+        # symlink creation
+        os.mkdir(tmp_dir + "/control")
+        for file in args.control:
+            filen =  tmp_dir + "/control/" + os.path.basename(os.path.splitext(file)[0])+".bed"
+            print "input files mapped: %s" % filen
+            os.symlink(file, filen)
+        command = ( "bash %s/JAMM.sh -s %s/sample -c %s/control -g %s -o results -m %s -r %s -p %s -t %s -f %s -b %s"
+         % ( path, tmp_dir, tmp_dir, args.gsize, args.mode, args.resolution, args.processes, \
+             args.type, args.fraglen, args.binsize ) ) 
+    else:
+        command = ( "bash %s/JAMM.sh -s %s/sample -g %s -o results -m %s -r %s -p %s -t %s -f %s -b %s"
+         % ( path, tmp_dir, args.gsize, args.mode, args.resolution, args.processes, \
+             args.type, args.fraglen, args.binsize ) ) 
+
+         
+    print "Command called by bash:"     
     print command
     # depending on how your programm is called, it may be necessary to use shlex.split on the command string before
     # in this case, this was actually harmful. idk why
@@ -75,13 +104,15 @@
     returncode = proc.wait()
     
     #mv files to a place where galaxy wrapper can find them
-    mvcommand = "mv %s/results/peaks/all.peaks.narrowPeak %s" % ( tmp_dir, args.peakfile ) 
+    # mvcommand = "mv %s/results/peaks/all.peaks.narrowPeak %s" % ( tmp_dir, args.peakfile )
+    mvcommand = "mv results/peaks/all.peaks.narrowPeak %s" % args.peakfile
+    os.system(mvcommand)
+    mvcommand = "mv results/peaks/filtered.peaks.narrowPeak %s" % args.filteredpeakfile
     os.system(mvcommand)
 
 # clean up temp dir
     if os.path.exists( tmp_dir ):
         shutil.rmtree( tmp_dir )    
-
     
 if __name__ == "__main__":
     main()