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