5
|
1 #purpose: python wrapper to run peak ranger
|
|
2 #author: Ziru Zhou
|
|
3 #Date: November 2012
|
|
4
|
|
5 import sys, subprocess, tempfile, shutil, glob, os, os.path, gzip
|
|
6 from galaxy import eggs
|
|
7 import pkg_resources
|
|
8 pkg_resources.require( "simplejson" )
|
|
9 import simplejson
|
|
10 import glob
|
|
11 import datetime
|
|
12
|
|
13 CHUNK_SIZE = 1024
|
|
14
|
|
15 def main():
|
|
16 options = simplejson.load( open( sys.argv[1] ) )
|
|
17 outputs = simplejson.load( open( sys.argv[2] ) )
|
|
18
|
|
19
|
|
20 #sets experiment name and sets the chip/input files
|
|
21 #========================================================================================
|
|
22 experiment_name = '_'.join( options['experiment_name'].split() ) #save experiment name
|
|
23
|
|
24 #cmdline = "bash /mnt/galaxyTools/galaxy-central/tools/modENCODE_DCC_tools/peakranger/peakranger %s -d %s --format bam" % ( options['action'], options['chip_file'] )
|
|
25 cmdline = "peakranger %s -d %s" % ( options['action'], options['chip_file'] )
|
|
26 if 'input_file' in options:
|
|
27 cmdline = "%s -c %s" % ( cmdline, options['input_file'] )
|
|
28
|
|
29 #set additional options
|
|
30 #========================================================================================
|
|
31 if (options['action'] == "nr"):
|
|
32 output_ranger_file = outputs['output_ranger_file']
|
|
33
|
|
34 cmdline = "%s --format bam -l %s --verbose > default_output.txt" % ( cmdline, options['extension'] )
|
|
35 elif (options['action'] == "lc"):
|
|
36 output_ranger_file = outputs['output_ranger_file']
|
|
37
|
|
38 cmdline = "%s --verbose > default_output.txt" % ( cmdline )
|
|
39 elif (options['action'] == "wig"):
|
|
40 output_wigzip_file = outputs['output_wigzip_file']
|
|
41
|
|
42 cmdline = "%s --format bam -l %s %s %s %s -o ranger_wig" % ( cmdline, options['extension'], options['split'], options['strand'], options['gzip'] )
|
|
43 elif (options['action'] == "wigpe"):
|
|
44 output_wigzip_file = outputs['output_wigzip_file']
|
|
45
|
|
46 cmdline = "%s -l %s %s %s %s -o ranger_wig" % ( cmdline, options['extension'], options['split'], options['strand'], options['gzip'] )
|
|
47 elif (options['action'] == "ranger"):
|
|
48 output_summit_file = outputs['output_summit_file']
|
|
49 output_region_file = outputs['output_region_file']
|
|
50 output_details_file = outputs['output_details_file']
|
|
51 output_report_file = outputs['output_report_file']
|
|
52
|
|
53 if (options['gene_annotate_file'] != "None"):
|
|
54 gene_annotate_file = "--gene_annot_file /mnt/galaxyTools/galaxy-central/tools/modENCODE_DCC_tools/peakranger/gene_annotation_files/%s" % options['gene_annotate_file']
|
|
55 report = "--report"
|
|
56 elif (options['gene_annotate_file'] == "Upload"):
|
|
57 gene_annotate_file = options['usr_annot_file']
|
|
58 report = "--report"
|
|
59 else:
|
|
60 gene_annotate_file = ""
|
|
61 report = ""
|
|
62
|
|
63 cmdline = "%s -t %s --format bam %s --plot_region %s -l %s -p %s -q %s -r %s -b %s %s %s -o ranger_peak" % ( cmdline, options['threads'], gene_annotate_file, options['plot_region'], options['extension'], options['pvalue'], options['fdr'], options['delta'], options['bandwith'], options['pad'], report )
|
|
64 elif (options['action'] == "ccat"):
|
|
65 output_summit_file = outputs['output_summit_file']
|
|
66 output_region_file = outputs['output_region_file']
|
|
67 output_details_file = outputs['output_details_file']
|
|
68 output_report_file = outputs['output_report_file']
|
|
69 output_ranger_file = outputs['output_ranger_file']
|
|
70
|
|
71 if (options['gene_annotate_file'] != "None"):
|
|
72 gene_annotate_file = "--gene_annot_file /mnt/galaxyTools/galaxy-central/tools/modENCODE_DCC_tools/peakranger/gene_annotation_files/%s" % options['gene_annotate_file']
|
|
73 report = "--report"
|
|
74 elif (options['gene_annotate_file'] == "Upload"):
|
|
75 gene_annotate_file = options['usr_annot_file']
|
|
76 report = "--report"
|
|
77 else:
|
|
78 gene_annotate_file = ""
|
|
79 report = ""
|
|
80
|
|
81 cmdline = "%s --format bam %s --plot_region %s -l %s -q %s --win_size %s --win_step %s --min_count %s --min_score %s %s -o ranger_peak > default_output.txt" % ( cmdline, gene_annotate_file, options['plot_region'], options['extension'], options['fdr'], options['winsize'], options['winstep'], options['mincount'], options['minscore'], report )
|
|
82
|
|
83 #run cmdline
|
|
84 #========================================================================================
|
|
85 tmp_dir = tempfile.mkdtemp()
|
|
86 stderr_name = tempfile.NamedTemporaryFile().name
|
|
87 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) )
|
|
88 proc.wait()
|
|
89
|
|
90 #Do not terminate if error code, allow dataset (e.g. log) creation and cleanup
|
|
91 #========================================================================================
|
|
92 if proc.returncode:
|
|
93 stderr_f = open( stderr_name )
|
|
94 while True:
|
|
95 chunk = stderr_f.read( CHUNK_SIZE )
|
|
96 if not chunk:
|
|
97 stderr_f.close()
|
|
98 break
|
|
99 sys.stderr.write( chunk )
|
|
100
|
|
101
|
|
102 #determine if the outputs are there, copy them to the appropriate dir and filename
|
|
103 #========================================================================================
|
|
104 if(options['action'] == "nr" or options['action'] == "lc" or options['action'] == "ccat"):
|
|
105 created_ranger_file = os.path.join( tmp_dir, "default_output.txt" )
|
|
106 if os.path.exists( created_ranger_file ):
|
|
107 shutil.move( created_ranger_file, output_ranger_file )
|
|
108
|
|
109 if(options['action'] == "wig" or options['action'] == "wigpe"):
|
|
110 if(options['split'] == "-s" or options['strand'] == "-x"):
|
|
111 if(options['gzip'] == "-z"):
|
|
112 files = str( glob.glob('%s/*.wig.gz' % tmp_dir) )
|
|
113 files = files.replace("[", "")
|
|
114 files = files.replace("]", "")
|
|
115 files = files.replace(",", "")
|
|
116 files = files.replace("'", "")
|
|
117 cmd = "zip -j %s/ranger_wig.zip %s > /dev/null" % (tmp_dir, files)
|
|
118 #cmd = "tar -czvf %s/ranger_wig.tar %s > /dev/null" % (tmp_dir, files)
|
|
119 os.system(cmd)
|
|
120 created_wigzip_file = os.path.join( tmp_dir, "ranger_wig.zip" )
|
|
121 else:
|
|
122 files = str( glob.glob('%s/*.wig' % tmp_dir) )
|
|
123 files = files.replace("[", "")
|
|
124 files = files.replace("]", "")
|
|
125 files = files.replace(",", "")
|
|
126 files = files.replace("'", "")
|
|
127 cmd = "zip -j %s/ranger_wig.zip %s > /dev/null" % (tmp_dir, files)
|
|
128 #cmd = "tar -czvf %s/ranger_wig.tar %s > /dev/null" % (tmp_dir, files)
|
|
129 os.system(cmd)
|
|
130 created_wigzip_file = os.path.join( tmp_dir, "ranger_wig.zip" )
|
|
131 else:
|
|
132 if(options['gzip'] == "-z"):
|
|
133 created_wigzip_file = os.path.join( tmp_dir, "ranger_wig.wig.gz" )
|
|
134 else:
|
|
135 created_wigzip_file = os.path.join( tmp_dir, "ranger_wig.wig" )
|
|
136
|
|
137 if os.path.exists( created_wigzip_file ):
|
|
138 shutil.move( created_wigzip_file, output_wigzip_file )
|
|
139
|
|
140 if(options['action'] == "ranger" or options['action'] == "ccat"):
|
|
141 created_summit_file = os.path.join( tmp_dir, "ranger_peak_summit.bed" )
|
|
142 if os.path.exists( created_summit_file ):
|
|
143 shutil.move( created_summit_file, output_summit_file )
|
|
144
|
|
145 created_region_file = os.path.join( tmp_dir, "ranger_peak_region.bed" )
|
|
146 if os.path.exists( created_region_file ):
|
|
147 shutil.move( created_region_file, output_region_file )
|
|
148
|
|
149 created_details_file = os.path.join( tmp_dir, "ranger_peak_details" )
|
|
150 if os.path.exists( created_details_file ):
|
|
151 shutil.move( created_details_file, output_details_file )
|
|
152
|
|
153 #zips the html report and puts it in history, whole report is too big and display in galaxy is very unformatted
|
|
154 filename = os.path.splitext(os.path.basename(options['chip_file']))[0]
|
|
155 filename = filename.upper()
|
|
156 extension = os.path.splitext(options['chip_file'])[1]
|
|
157 extension = extension.replace(".", "")
|
|
158 extension = extension.upper()
|
|
159 now = datetime.datetime.now()
|
|
160 date = now.strftime("%Y-%m-%d")
|
|
161 foldername = "%s_%s_REPORT_%s" % (filename, extension, date)
|
|
162
|
|
163 created_report_file = os.path.join( tmp_dir, foldername )
|
|
164 if os.path.exists ( created_report_file ):
|
|
165 #os.system("cp -rf %s %s" % (created_report_file, "/mnt/galaxyData/files/000/"))
|
|
166 os.system("cp -rf %s ." % created_report_file)
|
|
167 os.system("zip -r created_report.zip %s > /dev/null" % foldername)
|
|
168 #os.system("zip -r created_report.zip /mnt/galaxyData/files/000/%s > /dev/null" % foldername)
|
|
169 shutil.move( "created_report.zip", output_report_file)
|
|
170
|
|
171
|
|
172 #os.system("ln -fs %s/index.html %s" %( foldername, output_report_file ))
|
|
173 #datafoldername = os.path.splitext(os.path.basename(output_report_file))
|
|
174 #datafolder = os.path.join ("/mnt/galaxyData/files/000/" datafoldername)
|
|
175 #print "datafolder %s" % datafolder
|
|
176 #if os.path.exists( datafolder )
|
|
177 # os.system("rm -rf %s" % datafolder)
|
|
178 # os.system("cp -rf %s/%s/imgs /mnt/galaxyData/files/000/%s" % (tmp_dir, foldername, datafolder))
|
|
179 # os.system("cp -rf %s/%s/scripts /mnt/galaxyData/files/000/%s" % (tmp_dir, foldername, datafolder))
|
|
180
|
|
181 os.unlink( stderr_name )
|
|
182 shutil.rmtree( tmp_dir )
|
|
183
|
|
184 if __name__ == "__main__": main()
|