comparison 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
comparison
equal deleted inserted replaced
2:5b99943c4627 3:4130e95bd6c8
10 10
11 SPAN_JAR = os.environ.get("SPAN_JAR") 11 SPAN_JAR = os.environ.get("SPAN_JAR")
12 print 'Using SPAN Peak Analyzer distributive file {0}'.format(SPAN_JAR) 12 print 'Using SPAN Peak Analyzer distributive file {0}'.format(SPAN_JAR)
13 13
14 # #if str($action.action_selector) == "model" 14 # #if str($action.action_selector) == "model"
15 # #if $control.control_selector 15 # #if str($control_file) != 'None':
16 # span_wrapper.py model with_control 16 # span_wrapper.py model_with_control
17 # "${genome_identifier}" "${genome_file}" 17 # "${genome_identifier}" "${genome_file}"
18 # "${treatment_identifier}" "${treatment_file}" 18 # "${treatment_identifier}" "${treatment_file}"
19 # "${bin}" "${action.model_file}" 19 # "${control_identifier}" "${control_file}"
20 # "${control_identifier}" "${control.control_file}" 20 # "${bin}"
21 #
21 # #else 22 # #else
22 # span_wrapper.py model without_control 23 # span_wrapper.py model without_control
23 # "${genome_identifier}" "${genome_file}" 24 # "${genome_identifier}" "${genome_file}"
24 # "${treatment_identifier}" "${treatment_file}" 25 # "${treatment_identifier}" "${treatment_file}"
25 # "${bin}" "${action.model_file}" 26 # "${bin}"
26 # #end if 27 # #end if
27 # #else 28 # #else
28 # #if $control.control_selector 29 # #if str($control_file) != 'None':
29 # span_wrapper.py peaks with_control 30 # span_wrapper.py peaks_with_control
30 # "${genome_identifier}" "${genome_file}" 31 # "${genome_identifier}" "${genome_file}"
31 # "${treatment_identifier}" "${treatment_file}" 32 # "${treatment_identifier}" "${treatment_file}"
32 # "${bin}" "${action.model_file}" 33 # "${control_identifier}" "${control_file}"
33 # "${control_identifier}" "${control.control_file}" 34 # "${bin}"
34 # "${fdr}" "${gap}" "${action.peaks_file}" 35 # "${action.fdr}" "${action.gap}"
35 # #else 36 # #else
36 # span_wrapper.py peaks with_control 37 # span_wrapper.py peaks_without_control
37 # "${genome_identifier}" "${genome_file}" 38 # "${genome_identifier}" "${genome_file}"
38 # "${treatment_identifier}" "${treatment_file}" 39 # "${treatment_identifier}" "${treatment_file}"
39 # "${bin}" "${action.model_file}" 40 # "${bin}"
40 # "${fdr}" "${gap}" "${action.peaks_file}" 41 # "${action.fdr}" "${action.gap}"
41 # #end if 42 # #end if
42 # #end if 43 # #end if
43
44 # See https://research.jetbrains.org/groups/biolabs/tools/span-peak-analyzer for command line options
45 action = argv[0] 44 action = argv[0]
46 control = argv[1]
47 45
48 working_dir = os.path.abspath('.') 46 working_dir = os.path.abspath('.')
49 print 'WORKING DIRECTORY: {}'.format(working_dir) 47 print 'WORKING DIRECTORY: {}'.format(working_dir)
50 48
51 49
52 def link(name, f): 50 def link(name, f):
53 """ SPAN uses file extension to detect input type, so original names are necessary, instead of Galaxy .dat files""" 51 """ SPAN uses file extension to detect input type, so original names are necessary, instead of Galaxy .dat files"""
54 result = os.path.join(working_dir, name) 52 result = os.path.join(working_dir, name)
55 os.symlink(f, result) 53 if not os.path.exists(result):
54 os.symlink(f, result)
56 return result 55 return result
57 56
58 57
59 if action == 'model': 58 if action == 'model_with_control':
60 if control == 'with_control': 59 (chrom_sizes, chrom_sizes_file,
61 (chrom_sizes, chrom_sizes_file, 60 treatment, treatment_file,
62 treatment, treatment_file, 61 control, control_file,
63 bin, model_file, 62 bin) = argv[1:]
64 control, control_file) = argv[2:] 63 cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --control {} --bin {}'.format(
65 cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --control {} --bin {}'.format( 64 SPAN_JAR,
66 SPAN_JAR, 65 link(chrom_sizes, chrom_sizes_file),
67 link(chrom_sizes, chrom_sizes_file), 66 link(treatment, treatment_file),
68 link(treatment, treatment_file), 67 link(control, control_file),
69 link(control, control_file), 68 bin)
70 bin 69 elif action == 'model_without_control':
71 ) 70 (chrom_sizes, chrom_sizes_file,
72 elif control == 'without_control': 71 treatment, treatment_file,
73 (chrom_sizes, chrom_sizes_file, 72 bin) = argv[1:]
74 treatment, treatment_file, 73 cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --bin {}'.format(
75 bin, model_file) = argv[2:] 74 SPAN_JAR,
76 cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --bin {}'.format( 75 link(chrom_sizes, chrom_sizes_file),
77 SPAN_JAR, 76 link(treatment, treatment_file),
78 link(chrom_sizes, chrom_sizes_file), 77 bin)
79 link(treatment, treatment_file), 78 elif action == "peaks_with_control":
80 bin 79 (chrom_sizes, chrom_sizes_file,
81 ) 80 treatment, treatment_file,
82 else: 81 control, control_file,
83 raise Exception("Unknown control option {}".format(control)) 82 bin,
84 83 fdr, gap) = argv[1:]
85 elif action == "peaks": 84 cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --control {} ' \
86 if control == 'with_control': 85 '--bin {} --fdr {} --gap {} --peaks {}'.format(
87 (chrom_sizes, chrom_sizes_file, 86 SPAN_JAR,
88 treatment, treatment_file, 87 link(chrom_sizes, chrom_sizes_file),
89 bin, model_file, 88 link(treatment, treatment_file),
90 control, control_file, 89 link(control, control_file),
91 fdr, gap, peaks_file) = argv[2:] 90 bin, fdr, gap,
92 cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --control {} --bin {} --fdr {} --gap {} --peaks {}'.format( 91 os.path.join(working_dir, 'result.peak'))
93 SPAN_JAR, 92 elif action == 'peaks_without_control':
94 link(chrom_sizes, chrom_sizes_file), 93 (chrom_sizes, chrom_sizes_file,
95 link(treatment, treatment_file), 94 treatment, treatment_file,
96 link(control, control_file), 95 bin,
97 bin, fdr, gap, 96 fdr, gap) = argv[1:]
98 os.path.join(working_dir, peaks_file) 97 cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} -' \
99 ) 98 '-bin {} --fdr {} --gap {} --peaks {}'.format(
100 elif control == 'without_control': 99 SPAN_JAR,
101 (chrom_sizes, chrom_sizes_file, 100 link(chrom_sizes, chrom_sizes_file),
102 treatment, treatment_file, 101 link(treatment, treatment_file),
103 bin, model_file, 102 bin, fdr, gap,
104 fdr, gap, peaks_file) = argv[2:] 103 os.path.join(working_dir, 'result.peak'))
105 cmd = 'java -jar {} analyze --chrom.sizes {} --treatment {} --bin {} --fdr {} --gap {} --peaks {}'.format(
106 SPAN_JAR,
107 link(chrom_sizes, chrom_sizes_file),
108 link(treatment, treatment_file),
109 bin, fdr, gap,
110 os.path.join(working_dir, peaks_file)
111 )
112 else:
113 raise Exception("Unknown control option {}".format(control))
114 else: 104 else:
115 raise Exception("Unknown action command {}".format(action)) 105 raise Exception("Unknown action command {}".format(action))
116 106
117
118 print 'Launching SPAN: {}'.format(cmd) 107 print 'Launching SPAN: {}'.format(cmd)
119 print 'Model file: {}'.format(model_file)
120 try:
121 print 'Peaks file: {}'.format(peaks_file)
122 except NameError:
123 pass
124
125 subprocess.check_call(cmd, cwd=None, shell=True) 108 subprocess.check_call(cmd, cwd=None, shell=True)
126 109
127 # Move model to the the working dir with given name 110 # Move model to the the working dir with given name
128 fit_dir = os.path.join(working_dir, 'fit') 111 fit_dir = os.path.join(working_dir, 'fit')
129 model_original = os.path.join(fit_dir, os.listdir(fit_dir)[0]) 112 model_original = os.path.join(fit_dir, os.listdir(fit_dir)[0])
130 shutil.move(model_original, os.path.join(working_dir, model_file)) 113 shutil.move(model_original, os.path.join(working_dir, 'model.span'))
131 114
132 # Move log file 115 # Move log file
133 logs_dir = os.path.join(working_dir, 'logs') 116 logs_dir = os.path.join(working_dir, 'logs')
134 log_original = os.path.join(logs_dir, os.listdir(logs_dir)[0]) 117 log_original = os.path.join(logs_dir, os.listdir(logs_dir)[0])
135 shutil.move(log_original, os.path.join(working_dir, "span.log")) 118 shutil.move(log_original, os.path.join(working_dir, "span.log"))