annotate ranger_wrapper.py @ 17:d2319b4df472 draft

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