Mercurial > repos > siyuan > prada
comparison pyPRADA_1.2/prada-preprocess-bi @ 0:acc2ca1a3ba4
Uploaded
author | siyuan |
---|---|
date | Thu, 20 Feb 2014 00:44:58 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:acc2ca1a3ba4 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 #This is a program to implement the PRADA pipeline "process subsection", based on the work of Rahul Vegesna and Wandaliz Torres-Garcia | |
4 #It merely generates a PBS file, depending on the user input entry point (step) | |
5 #Author: Siyuan Zheng, szheng2@mdanderson.org | |
6 #Copy right belongs to Roel Verhaak's lab from MD Anderson Cancer Center, Department of Bioinformatics and Computational Biology. | |
7 #Last revision: 02/17/2014 | |
8 | |
9 import subprocess | |
10 import os,os.path | |
11 import sys | |
12 import time | |
13 import ioprada | |
14 import re | |
15 | |
16 ######################################################################################## | |
17 args=sys.argv | |
18 | |
19 help_menu='''\nPipeline for RNAseq Data Analaysis - preprocessing pipeline (PRADA). | |
20 \t**Usage**: | |
21 \tprada-preprocess-bi -conf xx.txt -inputdir .. -sample XX -tag TCGA-XX -platform illumina -step 1_1 -intermediate no -pbs xxx -outdir ... -submit no | |
22 \t**Parameters**: | |
23 \t-h print help message | |
24 \t-step_info print complete steps curated in the module. | |
25 \t-inputdir the dir where the input bam or fastq can be found. | |
26 \t-sample input sample name. PRADA searches for sample.bam or sample.end1.fastq/sample.end2.fastq, etc, depending on the | |
27 \t initiating step number. See step_info for more information. | |
28 \t-conf config file for references and parameters. Default is conf.txt in py-PRADA installation folder. | |
29 \t-tag a tag to describe the sample, likely sample ID, such as TCGA-LGG-01; no default. | |
30 \t-platform only illumina at present (default). | |
31 \t-step values: 1_1/2,2_e1/2_1/2/3/4,3_e1/2_1/2,4_1/2,5,6_1/2,7,8; example 2_e1_1; no default. | |
32 \t-outdir output dir. Default is the directory where the input bam is. | |
33 \t-pbs name for output pbs file and log file. Default (time-stamp) is used if no input. | |
34 \t-intermediate values:yes/no; if intermediate files should be kept. Default is not. | |
35 \t-submit if submit the job to HPC, default is no. If yes, ppn is set to 12. | |
36 \t-v print version information. | |
37 ''' | |
38 | |
39 steps_info=''' | |
40 Command orders (sample XX) | |
41 step 1_1 --> XX.sorted.bam [sort input bam by name] | |
42 step 1_2 --> XX.end1/2.fastq [extract reads from bam] | |
43 step 2_e1_1 --> XX.end1.sai [realign end1 reads to composite reference] | |
44 step 2_e1_2 --> XX.end1.sam [generate sam] | |
45 step 2_e1_3 --> XX.end1.bam [generate bam] | |
46 step 2_e1_4 --> XX.end1.sorted.bam [sort bam] | |
47 step 2_e2_1 --> XX.end2.sai [realign end2 reads to composite reference] | |
48 step 2_e2_2 --> XX.end2.sam [generate sam] | |
49 step 2_e2_3 --> XX.end2.bam [generate bam] | |
50 step 2_e2_4 --> XX.end2.sorted.bam [sort bam] | |
51 step 3_e1_1 --> XX.end1.remapped.bam [remap end1 to genome] | |
52 step 3_e1_2 --> XX.end1.remapped.sorted.bam [sort remapped bam] | |
53 step 3_e2_1 --> XX.end2.remapped.bam [remap end2 to genome] | |
54 step 3_e2_2 --> XX.end2.remapped.sorted.bam [sort remapped bam] | |
55 step 4_1 --> XX.paired.bam [pair end1 and end1] | |
56 step 4_2 --> XX.paired.sorted.bam [sort paired bam] | |
57 step 5 --> XX.withRG.paired.sorted.bam [add read group] | |
58 step 6_1 --> XX.orig.csv [prepair recalibration table] | |
59 step 6_2 --> XX.withRG.GATKRecalibrated.bam [recalibration] | |
60 step 7 --> XX.withRG.GATKRecalibrated.flagged.bam [flag duplication reads] | |
61 step 8 --> folder XX for gene expression, QC metrics etc. [generate QC and expression] | |
62 ''' | |
63 | |
64 if '-h' in args or '-help' in args or len(args)==1: | |
65 print help_menu | |
66 sys.exit(0) | |
67 if '-step_info' in args: | |
68 print steps_info | |
69 sys.exit(0) | |
70 if '-v' in args: | |
71 import version | |
72 print version.version | |
73 sys.exit(0) | |
74 | |
75 if '-sample' not in args: | |
76 sys.exit('ERROR: Sample name is needed') | |
77 if '-step' not in args: | |
78 sys.exit('ERROR: Step number is needed') | |
79 if '-tag' not in args: | |
80 sys.exit('ERROR: A tag is needed') | |
81 | |
82 i=args.index('-sample') | |
83 sample=args[i+1] | |
84 if '-inputdir' not in args: | |
85 inputpath=os.path.abspath('./') | |
86 else: | |
87 i=args.index('-inputdir') | |
88 inputpath=args[i+1] | |
89 bampath=os.path.abspath(inputpath)+'/%s.bam'%sample | |
90 fq1path=os.path.abspath(inputpath)+'/%s.end1.fastq'%sample | |
91 fq2path=os.path.abspath(inputpath)+'/%s.end2.fastq'%sample | |
92 | |
93 if '-outdir' not in args: | |
94 outpath=os.path.dirname(bampath) | |
95 else: | |
96 i=args.index('-outdir') | |
97 outpath=os.path.abspath(args[i+1]) | |
98 if not os.path.exists(outpath): | |
99 os.mkdir(outpath) | |
100 | |
101 #bam=os.path.basename(bampath) | |
102 #sample=bam[:-4] | |
103 i=args.index('-step') | |
104 step=args[i+1] | |
105 i=args.index('-tag') | |
106 tag=args[i+1] | |
107 | |
108 prada_path=os.path.dirname(os.path.abspath(__file__)) #### | |
109 ref_search_path=[prada_path,os.getcwd()] #search path for ref file if not specified in command line | |
110 | |
111 if '-conf' in args: | |
112 i=args.index('-conf') | |
113 reffile=args[i+1] | |
114 if os.path.exists(reffile): | |
115 pass | |
116 else: | |
117 for pth in ref_search_path: | |
118 new_reffile='%s/%s'%(pth, os.path.basename(reffile)) | |
119 if os.path.exists(new_reffile): | |
120 reffile=new_reffile | |
121 break | |
122 else: | |
123 sys.exit('ERROR: conf file %s not found'%reffile) | |
124 else: | |
125 reffile='%s/conf.txt'%prada_path | |
126 if not os.path.exists(reffile): | |
127 sys.exit('ERROR: No default conf.txt found and none specified') | |
128 | |
129 if '-platform' in args: | |
130 i=args.index('-platform') | |
131 plat=args[i+1] | |
132 else: | |
133 plat='illumina' | |
134 if '-submit' in args: | |
135 i=args.index('-submit') | |
136 submit=args[i+1] | |
137 else: | |
138 submit='no' | |
139 if '-intermediate' in args: | |
140 i=args.index('-intermediate') | |
141 keepmed=args[i+1] | |
142 else: | |
143 keepmed='False' | |
144 | |
145 if keepmed in ['False','FALSE','false','F','NO','No','no','n']: | |
146 keepmed_flag='no' | |
147 elif keepmed in ['True','TRUE','true','T','YES','Yes','yes','y']: | |
148 keepmed_flag='yes' | |
149 else: | |
150 sys.exit('ERROR: -intermediate value not recognized') | |
151 | |
152 if '-pbs' in args: | |
153 i=args.index('-pbs') | |
154 docstr=args[i+1] | |
155 else: | |
156 a=time.ctime().split() | |
157 b=time.time() | |
158 timestamp='_'.join([a[-1],a[1],a[2]])+'.'+str(b) | |
159 docstr='prada_prep_'+timestamp | |
160 logfilename=docstr+'.log' | |
161 pbsfilename=docstr+'.pbs' | |
162 pbspath=outpath+'/'+pbsfilename | |
163 logpath=outpath+'/'+logfilename | |
164 ######################################################################################## | |
165 | |
166 ######################################################################################## | |
167 #underlying utilities, automatically detected | |
168 samtools='%s/tools/samtools-0.1.16/samtools'%prada_path | |
169 bwa='%s/tools/bwa-0.5.7-mh/bwa'%prada_path | |
170 gatk='%s/tools/GATK/'%prada_path | |
171 picard='%s/tools/Picard/'%prada_path | |
172 seqc='%s/tools/RNA-SeQC_v1.1.7.jar'%prada_path | |
173 #Default uses 12 nodes in HPC | |
174 ######################################################################################### | |
175 | |
176 ######################################################################################### | |
177 #reference files | |
178 refdict=ioprada.read_conf(reffile) | |
179 genome_gtf=refdict['--REF--']['genome_gtf'] | |
180 compdb_fasta=refdict['--REF--']['compdb_fasta'] | |
181 compdb_map=refdict['--REF--']['compdb_map'] | |
182 genome_fasta=refdict['--REF--']['genome_fasta'] | |
183 dbsnp_vcf=refdict['--REF--']['dbsnp_vcf'] | |
184 select_tx=refdict['--REF--']['select_tx'] | |
185 pat=re.compile('ppn=(\d*)') | |
186 parallel_n=pat.search(refdict['--PBS--']['-l']).groups()[0] | |
187 ######################################################################################### | |
188 | |
189 ######################################################################################### | |
190 #pipeline command lines. | |
191 ##Cleaning up steps: if -intermediate is yes, none will be executed. | |
192 step_1_1_cmd=['%s sort -n -m 1000000000 %s %s.sorted'%(samtools,bampath,sample)] | |
193 post_1_1_clean=[] | |
194 step_1_2_cmd=['java -Xmx8g -jar %s/SamToFastq.jar INPUT=%s.sorted.bam FASTQ=%s.end1.fastq SECOND_END_FASTQ=%s.end2.fastq INCLUDE_NON_PF_READS=true VALIDATION_STRINGENCY=SILENT TMP_DIR=tmp/'%(picard,sample,sample,sample)] | |
195 post_1_2_clean=['rm -f %s.sorted.bam'%sample] | |
196 alnparams=' '.join([' '.join(x) for x in refdict['--BWA aln--'].items()]) | |
197 samseparams=' '.join([' '.join(x) for x in refdict['--BWA samse--'].items()]) | |
198 if step == '2_e1_1' or step == '2_e2_1': | |
199 step_2_e1_1_cmd=['%s aln %s %s %s > %s.end1.sai'%(bwa,alnparams,compdb_fasta,fq1path,sample)] | |
200 post_2_e1_1_clean=[] | |
201 step_2_e1_2_cmd=['%s samse -s %s %s %s.end1.sai %s > %s.end1.sam'%(bwa,samseparams,compdb_fasta,sample,fq1path,sample)] | |
202 post_2_e1_2_clean=['rm -f %s.end1.sai'%sample] | |
203 step_2_e2_1_cmd=['%s aln %s %s %s > %s.end2.sai'%(bwa,alnparams,compdb_fasta,fq2path,sample)] | |
204 post_2_e2_1_clean=[] | |
205 step_2_e2_2_cmd=['%s samse -s %s %s %s.end2.sai %s > %s.end2.sam'%(bwa,samseparams,compdb_fasta,sample,fq2path,sample)] | |
206 post_2_e2_2_clean=['rm -f %s.end2.sai'%sample] | |
207 else: | |
208 step_2_e1_1_cmd=['%s aln %s %s %s.end1.fastq > %s.end1.sai'%(bwa,alnparams,compdb_fasta,sample,sample)] | |
209 post_2_e1_1_clean=[] | |
210 step_2_e1_2_cmd=['%s samse -s %s %s %s.end1.sai %s.end1.fastq > %s.end1.sam'%(bwa,samseparams,compdb_fasta,sample,sample,sample)] | |
211 post_2_e1_2_clean=['rm -f %s.end1.sai'%sample,'rm -f %s.end1.fastq'%sample] | |
212 step_2_e2_1_cmd=['%s aln %s %s %s.end2.fastq > %s.end2.sai'%(bwa,alnparams,compdb_fasta,sample,sample)] | |
213 post_2_e2_1_clean=[] | |
214 step_2_e2_2_cmd=['%s samse -s %s %s %s.end2.sai %s.end2.fastq > %s.end2.sam'%(bwa,samseparams,compdb_fasta,sample,sample,sample)] | |
215 post_2_e2_2_clean=['rm -f %s.end2.sai'%sample,'rm -f %s.end2.fastq'%sample] | |
216 step_2_e1_3_cmd=['%s view -bS -o %s.end1.bam %s.end1.sam'%(samtools,sample,sample)] | |
217 post_2_e1_3_clean=['rm -f %s.end1.sam'%sample] | |
218 step_2_e1_4_cmd=['%s sort -n -m 1000000000 %s.end1.bam %s.end1.sorted'%(samtools,sample,sample)] | |
219 post_2_e1_4_clean=['rm -f %s.end1.bam'%sample] | |
220 step_2_e2_3_cmd=['%s view -bS -o %s.end2.bam %s.end2.sam'%(samtools,sample,sample)] | |
221 post_2_e2_3_clean=['rm -f %s.end2.sam'%sample] | |
222 step_2_e2_4_cmd=['%s sort -n -m 1000000000 %s.end2.bam %s.end2.sorted'%(samtools,sample,sample)] | |
223 post_2_e2_4_clean=['rm -f %s.end2.bam'%sample] | |
224 step_3_e1_1_cmd=['java -Djava.io.tmpdir=tmp/ -cp %s/RemapAlignments.jar -Xmx8g org.broadinstitute.cga.tools.gatk.rna.RemapAlignments M=%s IN=%s.end1.sorted.bam OUT=%s.end1.remapped.bam R=%s REDUCE=TRUE'%(gatk,compdb_map,sample,sample,genome_fasta)] | |
225 post_3_e1_1_clean=['rm -f %s.end1.sorted.bam'%sample] | |
226 step_3_e1_2_cmd=['%s sort -n -m 1000000000 %s.end1.remapped.bam %s.end1.remapped.sorted'%(samtools,sample,sample)] | |
227 post_3_e1_2_clean=['rm -f %s.end1.remapped.bam'%sample] | |
228 step_3_e2_1_cmd=['java -Djava.io.tmpdir=tmp/ -cp %s/RemapAlignments.jar -Xmx8g org.broadinstitute.cga.tools.gatk.rna.RemapAlignments M=%s IN=%s.end2.sorted.bam OUT=%s.end2.remapped.bam R=%s REDUCE=TRUE'%(gatk,compdb_map,sample,sample,genome_fasta)] | |
229 post_3_e2_1_clean=['rm -f %s.end2.sorted.bam'%sample] | |
230 step_3_e2_2_cmd=['%s sort -n -m 1000000000 %s.end2.remapped.bam %s.end2.remapped.sorted'%(samtools,sample,sample)] | |
231 post_3_e2_2_clean=['rm -f %s.end2.remapped.bam'%sample] | |
232 step_4_1_cmd=['java -Djava.io.tmpdir=tmp/ -Xmx8g -jar %s/PairMaker.jar IN1=%s.end1.remapped.sorted.bam IN2=%s.end2.remapped.sorted.bam OUTPUT=%s.paired.bam TMP_DIR=tmp/'%(gatk,sample,sample,sample)] | |
233 post_4_1_clean=['rm -f %s.end1.remapped.sorted.bam'%sample,'rm -f %s.end2.remapped.sorted.bam'%sample] | |
234 step_4_2_cmd=['%s sort -m 1000000000 %s.paired.bam %s.paired.sorted'%(samtools,sample,sample)] | |
235 post_4_2_clean=['rm -f %s.paired.bam'%sample] | |
236 step_5_cmd=['java -Xmx8g -jar %s/AddOrReplaceReadGroups.jar I=%s.paired.sorted.bam O=%s.withRG.paired.sorted.bam RGLB=%s RGPL=%s RGPU=%s RGSM=%s'%(picard,sample,sample,tag,plat,tag,tag)] | |
237 post_5_clean=['rm -f %s.paired.sorted.bam'%sample] | |
238 step_6_1_cmd=['%s index %s.withRG.paired.sorted.bam'%(samtools,sample),'java -Xmx8g -jar %s/GenomeAnalysisTK.jar -l INFO -R %s --default_platform %s --knownSites %s -I %s.withRG.paired.sorted.bam --downsample_to_coverage 10000 -T CountCovariates -cov ReadGroupCovariate -cov QualityScoreCovariate -cov CycleCovariate -cov DinucCovariate -nt %s -recalFile %s.orig.csv'%(gatk,genome_fasta,plat,dbsnp_vcf,sample,parallel_n,sample)] | |
239 post_6_1_clean=[] | |
240 step_6_2_cmd=['java -Xmx8g -jar %s/GenomeAnalysisTK.jar -l INFO -R %s --default_platform %s -I %s.withRG.paired.sorted.bam -T TableRecalibration --out %s.withRG.GATKRecalibrated.bam -recalFile %s.orig.csv'%(gatk,genome_fasta,plat,sample,sample,sample)] | |
241 post_6_2_clean=['rm -f %s.withRG.paired.sorted.bam'%sample,'rm -f %s.withRG.paired.sorted.bam.bai'%sample,'rm -f %s.orig.csv'%sample] | |
242 step_7_cmd=['java -Xmx8g -jar %s/MarkDuplicates.jar I=%s.withRG.GATKRecalibrated.bam O=%s.withRG.GATKRecalibrated.flagged.bam METRICS_FILE=%s.Duplicates_metrics.txt VALIDATION_STRINGENCY=SILENT TMP_DIR=tmp/'%(picard,sample,sample,sample),'%s index %s.withRG.GATKRecalibrated.flagged.bam'%(samtools,sample)] | |
243 post_7_clean=['rm -f %s.withRG.GATKRecalibrated.bam'%sample,'rm -f %s.withRG.GATKRecalibrated.bai'%sample,'rm -f %s.Duplicates_metrics.txt'%sample] | |
244 step_8_cmd=["java -Xmx8g -jar %s -ttype 2 -t %s -r %s -s '%s|%s.withRG.GATKRecalibrated.flagged.bam|Disc' -o %s/"%(seqc,genome_gtf,genome_fasta,sample,sample,sample)] | |
245 post_8_clean=[] | |
246 | |
247 cmdset=[] | |
248 for item in globals().keys(): | |
249 if item.endswith('_cmd'): | |
250 cmdset.append(item) | |
251 cmdset.sort() | |
252 | |
253 ######################################################################################### | |
254 #write PBS file | |
255 | |
256 ###############Entry Point | |
257 try: | |
258 cmd_entry=cmdset.index('step_'+step+'_cmd') ##entry point | |
259 except ValueError: | |
260 sys.exit('ERROR: STEP not recognized') | |
261 | |
262 def _parsecmd(cmd): | |
263 '''pase cmd str for step information''' | |
264 info=cmd.split('_') | |
265 cstep='_'.join(info[1:-1]) | |
266 return cstep | |
267 | |
268 ########################### | |
269 ##headers | |
270 outfile=open(pbspath,'w') | |
271 outfile.write('#! /bin/sh\n') | |
272 outfile.write('#PBS -V\n') | |
273 outfile.write('#PBS -N %s\n'%tag) | |
274 outfile.write('#PBS -j oe\n') | |
275 outfile.write('#PBS -o %s\n'%logpath) | |
276 for item in refdict['--PBS--']: | |
277 outfile.write('#PBS %s %s\n'%(item,refdict['--PBS--'][item])) | |
278 outfile.write('#PBS -d %s\n'%outpath) | |
279 | |
280 #commands | |
281 outfile.write('echo "Job start: `date`"\n') | |
282 | |
283 for i in range(cmd_entry,len(cmdset)): | |
284 cmd=cmdset[i] | |
285 cstep=_parsecmd(cmd) | |
286 clean_flag=1 | |
287 outfile.write('echo "step %s start: `date`"\n'%cstep) | |
288 outfile.write('if\n') | |
289 outfile.write('\t%s\n'%('\n'.join(eval(cmd)))) | |
290 outfile.write('then\n') | |
291 outfile.write('\techo "step %s done: `date`"\n'%cstep) | |
292 outfile.write('else\n') | |
293 outfile.write('\techo "step %s ERROR"\n'%cstep) | |
294 outfile.write('\texit\n') | |
295 outfile.write('fi\n') | |
296 | |
297 if keepmed_flag=='yes': #if so, keep files by force | |
298 clean_flag=0 | |
299 | |
300 if clean_flag==1: | |
301 outfile.write('\n'.join(eval('post_%s_clean'%cstep))) | |
302 outfile.write('\n') | |
303 | |
304 outfile.write('echo "PIPELINE FINISHED"\n') | |
305 outfile.close() | |
306 ###################################################################### | |
307 | |
308 if submit in ['False','FALSE','false','F','NO','No','no','n']: | |
309 jid='Not_Submitted' | |
310 logpath='None' | |
311 elif submit in ['True','TRUE','true','T','YES','Yes','yes','y']: | |
312 cmdstr='qsub %s'%pbspath | |
313 cmd=cmdstr.split() | |
314 cmdout=subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) | |
315 jid=cmdout.stdout.read().strip() ##JOB ID | |
316 else: | |
317 sys.exit('ERROR: submit parameter not recognized') | |
318 | |
319 print '#!#%s'%tag | |
320 print 'BAM\t%s'%bampath | |
321 print 'Entry\t%s'%step | |
322 print 'PBS\t%s'%pbspath | |
323 print 'JOB\t%s'%jid | |
324 print 'LOG\t%s'%logpath | |
325 |