diff span_wrapper.py @ 3:4130e95bd6c8 draft

Batch processing mode supported
author jetbrains
date Mon, 19 Nov 2018 08:24:04 -0500
parents 5b99943c4627
children 70500fd87b60
line wrap: on
line diff
--- a/span_wrapper.py	Sun Nov 18 08:20:27 2018 -0500
+++ b/span_wrapper.py	Mon Nov 19 08:24:04 2018 -0500
@@ -12,38 +12,36 @@
 print 'Using SPAN Peak Analyzer distributive file {0}'.format(SPAN_JAR)
 
 # #if str($action.action_selector) == "model"
-#     #if $control.control_selector
-#         span_wrapper.py model with_control
+#     #if str($control_file) != 'None':
+#         span_wrapper.py model_with_control
 #             "${genome_identifier}" "${genome_file}"
 #             "${treatment_identifier}" "${treatment_file}"
-#             "${bin}" "${action.model_file}"
-#             "${control_identifier}" "${control.control_file}"
+#             "${control_identifier}" "${control_file}"
+#             "${bin}"
+#
 #     #else
 #         span_wrapper.py model without_control
 #             "${genome_identifier}" "${genome_file}"
 #             "${treatment_identifier}" "${treatment_file}"
-#             "${bin}" "${action.model_file}"
+#             "${bin}"
 #     #end if
 # #else
-#     #if $control.control_selector
-#         span_wrapper.py peaks with_control
+#     #if str($control_file) != 'None':
+#         span_wrapper.py peaks_with_control
 #             "${genome_identifier}" "${genome_file}"
 #             "${treatment_identifier}" "${treatment_file}"
-#             "${bin}" "${action.model_file}"
-#             "${control_identifier}" "${control.control_file}"
-#             "${fdr}" "${gap}" "${action.peaks_file}"
+#             "${control_identifier}" "${control_file}"
+#             "${bin}"
+#             "${action.fdr}" "${action.gap}"
 #     #else
-#         span_wrapper.py peaks with_control
+#         span_wrapper.py peaks_without_control
 #             "${genome_identifier}" "${genome_file}"
 #             "${treatment_identifier}" "${treatment_file}"
-#             "${bin}" "${action.model_file}"
-#             "${fdr}" "${gap}" "${action.peaks_file}"
+#             "${bin}"
+#             "${action.fdr}" "${action.gap}"
 #     #end if
 # #end if
-
-# See https://research.jetbrains.org/groups/biolabs/tools/span-peak-analyzer for command line options
 action = argv[0]
-control = argv[1]
 
 working_dir = os.path.abspath('.')
 print 'WORKING DIRECTORY: {}'.format(working_dir)
@@ -52,82 +50,67 @@
 def link(name, f):
     """ SPAN uses file extension to detect input type, so original names are necessary, instead of Galaxy .dat files"""
     result = os.path.join(working_dir, name)
-    os.symlink(f, result)
+    if not os.path.exists(result):
+        os.symlink(f, result)
     return result
 
 
-if action == 'model':
-    if control == 'with_control':
-        (chrom_sizes, chrom_sizes_file,
-         treatment, treatment_file,
-         bin, model_file,
-         control, control_file) = argv[2:]
-        cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --control {} --bin {}'.format(
-            SPAN_JAR,
-            link(chrom_sizes, chrom_sizes_file),
-            link(treatment, treatment_file),
-            link(control, control_file),
-            bin
-        )
-    elif control == 'without_control':
-        (chrom_sizes, chrom_sizes_file,
-         treatment, treatment_file,
-         bin, model_file) = argv[2:]
-        cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --bin {}'.format(
-            SPAN_JAR,
-            link(chrom_sizes, chrom_sizes_file),
-            link(treatment, treatment_file),
-            bin
-        )
-    else:
-        raise Exception("Unknown control option {}".format(control))
-
-elif action == "peaks":
-    if control == 'with_control':
-        (chrom_sizes, chrom_sizes_file,
-         treatment, treatment_file,
-         bin, model_file,
-         control, control_file,
-         fdr, gap, peaks_file) = argv[2:]
-        cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --control {} --bin {} --fdr {} --gap {} --peaks {}'.format(
-            SPAN_JAR,
-            link(chrom_sizes, chrom_sizes_file),
-            link(treatment, treatment_file),
-            link(control, control_file),
-            bin, fdr, gap,
-            os.path.join(working_dir, peaks_file)
-        )
-    elif control == 'without_control':
-        (chrom_sizes, chrom_sizes_file,
-         treatment, treatment_file,
-         bin, model_file,
-         fdr, gap, peaks_file) = argv[2:]
-        cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --bin {} --fdr {} --gap {} --peaks {}'.format(
-            SPAN_JAR,
-            link(chrom_sizes, chrom_sizes_file),
-            link(treatment, treatment_file),
-            bin, fdr, gap,
-            os.path.join(working_dir, peaks_file)
-        )
-    else:
-        raise Exception("Unknown control option {}".format(control))
+if action == 'model_with_control':
+    (chrom_sizes, chrom_sizes_file,
+     treatment, treatment_file,
+     control, control_file,
+     bin) = argv[1:]
+    cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --control {} --bin {}'.format(
+        SPAN_JAR,
+        link(chrom_sizes, chrom_sizes_file),
+        link(treatment, treatment_file),
+        link(control, control_file),
+        bin)
+elif action == 'model_without_control':
+    (chrom_sizes, chrom_sizes_file,
+     treatment, treatment_file,
+     bin) = argv[1:]
+    cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --bin {}'.format(
+        SPAN_JAR,
+        link(chrom_sizes, chrom_sizes_file),
+        link(treatment, treatment_file),
+        bin)
+elif action == "peaks_with_control":
+    (chrom_sizes, chrom_sizes_file,
+     treatment, treatment_file,
+     control, control_file,
+     bin,
+     fdr, gap) = argv[1:]
+    cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --control {} ' \
+          '--bin {} --fdr {} --gap {} --peaks {}'.format(
+        SPAN_JAR,
+        link(chrom_sizes, chrom_sizes_file),
+        link(treatment, treatment_file),
+        link(control, control_file),
+        bin, fdr, gap,
+        os.path.join(working_dir, 'result.peak'))
+elif action == 'peaks_without_control':
+    (chrom_sizes, chrom_sizes_file,
+     treatment, treatment_file,
+     bin,
+     fdr, gap) = argv[1:]
+    cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} -' \
+          '-bin {} --fdr {} --gap {} --peaks {}'.format(
+        SPAN_JAR,
+        link(chrom_sizes, chrom_sizes_file),
+        link(treatment, treatment_file),
+        bin, fdr, gap,
+        os.path.join(working_dir, 'result.peak'))
 else:
     raise Exception("Unknown action command {}".format(action))
 
-
 print 'Launching SPAN: {}'.format(cmd)
-print 'Model file: {}'.format(model_file)
-try:
-    print 'Peaks file: {}'.format(peaks_file)
-except NameError:
-    pass
-
 subprocess.check_call(cmd, cwd=None, shell=True)
 
 # Move model to the the working dir with given name
 fit_dir = os.path.join(working_dir, 'fit')
 model_original = os.path.join(fit_dir, os.listdir(fit_dir)[0])
-shutil.move(model_original, os.path.join(working_dir, model_file))
+shutil.move(model_original, os.path.join(working_dir, 'model.span'))
 
 # Move log file
 logs_dir = os.path.join(working_dir, 'logs')