Mercurial > repos > devteam > cufflinks
annotate cufflinks_wrapper.py @ 6:9aab29e159a7 draft
Convert tool to use $GALAXY_SLOTS if available.
author | Nate Coraor <nate@bx.psu.edu> |
---|---|
date | Thu, 16 Jan 2014 13:14:05 -0500 |
parents | da11bfc10e81 |
children | 5346d5eea8b1 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 # Supports Cufflinks versions 1.3 and newer. | |
4 | |
5 import optparse, os, shutil, subprocess, sys, tempfile | |
6 from galaxy import eggs | |
7 from galaxy.datatypes.util.gff_util import parse_gff_attributes, gff_attributes_to_str | |
8 | |
9 def stop_err( msg ): | |
10 sys.stderr.write( "%s\n" % msg ) | |
11 sys.exit() | |
12 | |
13 def __main__(): | |
14 #Parse Command Line | |
15 parser = optparse.OptionParser() | |
16 parser.add_option( '-1', '--input', dest='input', help=' file of RNA-Seq read alignments in the SAM format. SAM is a standard short read alignment, that allows aligners to attach custom tags to individual alignments, and Cufflinks requires that the alignments you supply have some of these tags. Please see Input formats for more details.' ) | |
17 parser.add_option( '-s', '--inner-dist-std-dev', dest='inner_dist_std_dev', help='The standard deviation for the distribution on inner distances between mate pairs. The default is 20bp.' ) | |
18 parser.add_option( '-I', '--max-intron-length', dest='max_intron_len', help='The minimum intron length. Cufflinks will not report transcripts with introns longer than this, and will ignore SAM alignments with REF_SKIP CIGAR operations longer than this. The default is 300,000.' ) | |
19 parser.add_option( '-F', '--min-isoform-fraction', dest='min_isoform_fraction', help='After calculating isoform abundance for a gene, Cufflinks filters out transcripts that it believes are very low abundance, because isoforms expressed at extremely low levels often cannot reliably be assembled, and may even be artifacts of incompletely spliced precursors of processed transcripts. This parameter is also used to filter out introns that have far fewer spliced alignments supporting them. The default is 0.05, or 5% of the most abundant isoform (the major isoform) of the gene.' ) | |
20 parser.add_option( '-j', '--pre-mrna-fraction', dest='pre_mrna_fraction', help='Some RNA-Seq protocols produce a significant amount of reads that originate from incompletely spliced transcripts, and these reads can confound the assembly of fully spliced mRNAs. Cufflinks uses this parameter to filter out alignments that lie within the intronic intervals implied by the spliced alignments. The minimum depth of coverage in the intronic region covered by the alignment is divided by the number of spliced reads, and if the result is lower than this parameter value, the intronic alignments are ignored. The default is 5%.' ) | |
21 parser.add_option( '-p', '--num-threads', dest='num_threads', help='Use this many threads to align reads. The default is 1.' ) | |
22 parser.add_option( '-m', '--inner-mean-dist', dest='inner_mean_dist', help='This is the expected (mean) inner distance between mate pairs. \ | |
23 For, example, for paired end runs with fragments selected at 300bp, \ | |
24 where each end is 50bp, you should set -r to be 200. The default is 45bp.') | |
25 parser.add_option( '-G', '--GTF', dest='GTF', help='Tells Cufflinks to use the supplied reference annotation to estimate isoform expression. It will not assemble novel transcripts, and the program will ignore alignments not structurally compatible with any reference transcript.' ) | |
26 parser.add_option( '-g', '--GTF-guide', dest='GTFguide', help='use reference transcript annotation to guide assembly' ) | |
27 parser.add_option( '-u', '--multi-read-correct', dest='multi_read_correct', action="store_true", help='Tells Cufflinks to do an initial estimation procedure to more accurately weight reads mapping to multiple locations in the genome') | |
28 | |
29 # Normalization options. | |
30 parser.add_option( "-N", "--quartile-normalization", dest="do_normalization", action="store_true" ) | |
31 parser.add_option( "--no-effective-length-correction", dest="no_effective_length_correction", action="store_true" ) | |
32 | |
33 # Wrapper / Galaxy options. | |
34 parser.add_option( '-A', '--assembled-isoforms-output', dest='assembled_isoforms_output_file', help='Assembled isoforms output file; formate is GTF.' ) | |
35 | |
36 # Advanced Options: | |
37 parser.add_option( '--num-importance-samples', dest='num_importance_samples', help='Sets the number of importance samples generated for each locus during abundance estimation. Default: 1000' ) | |
38 parser.add_option( '--max-mle-iterations', dest='max_mle_iterations', help='Sets the number of iterations allowed during maximum likelihood estimation of abundances. Default: 5000' ) | |
39 | |
40 # Bias correction options. | |
41 parser.add_option( '-b', dest='do_bias_correction', action="store_true", help='Providing Cufflinks with a multifasta file via this option instructs it to run our new bias detection and correction algorithm which can significantly improve accuracy of transcript abundance estimates.') | |
2
da11bfc10e81
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
42 parser.add_option( '', '--index', dest='index', help='The path of the reference genome' ) |
0 | 43 parser.add_option( '', '--ref_file', dest='ref_file', help='The reference dataset from the history' ) |
44 | |
45 # Global model. | |
46 parser.add_option( '', '--global_model', dest='global_model_file', help='Global model used for computing on local data' ) | |
47 | |
48 (options, args) = parser.parse_args() | |
49 | |
50 # output version # of tool | |
51 try: | |
52 tmp = tempfile.NamedTemporaryFile().name | |
53 tmp_stdout = open( tmp, 'wb' ) | |
54 proc = subprocess.Popen( args='cufflinks --no-update-check 2>&1', shell=True, stdout=tmp_stdout ) | |
55 tmp_stdout.close() | |
56 returncode = proc.wait() | |
57 stdout = None | |
58 for line in open( tmp_stdout.name, 'rb' ): | |
59 if line.lower().find( 'cufflinks v' ) >= 0: | |
60 stdout = line.strip() | |
61 break | |
62 if stdout: | |
63 sys.stdout.write( '%s\n' % stdout ) | |
64 else: | |
65 raise Exception | |
66 except: | |
67 sys.stdout.write( 'Could not determine Cufflinks version\n' ) | |
68 | |
69 # If doing bias correction, set/link to sequence file. | |
70 if options.do_bias_correction: | |
2
da11bfc10e81
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
71 if options.ref_file: |
0 | 72 # Sequence data from history. |
73 # Create symbolic link to ref_file so that index will be created in working directory. | |
74 seq_path = "ref.fa" | |
75 os.symlink( options.ref_file, seq_path ) | |
76 else: | |
2
da11bfc10e81
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
77 if not os.path.exists( options.index ): |
da11bfc10e81
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
78 stop_err( 'Reference genome %s not present, request it by reporting this error.' % options.index ) |
da11bfc10e81
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
79 seq_path = options.index |
da11bfc10e81
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
80 |
0 | 81 # Build command. |
82 | |
83 # Base; always use quiet mode to avoid problems with storing log output. | |
84 cmd = "cufflinks -q --no-update-check" | |
85 | |
86 # Add options. | |
87 if options.inner_dist_std_dev: | |
88 cmd += ( " -s %i" % int ( options.inner_dist_std_dev ) ) | |
89 if options.max_intron_len: | |
90 cmd += ( " -I %i" % int ( options.max_intron_len ) ) | |
91 if options.min_isoform_fraction: | |
92 cmd += ( " -F %f" % float ( options.min_isoform_fraction ) ) | |
93 if options.pre_mrna_fraction: | |
94 cmd += ( " -j %f" % float ( options.pre_mrna_fraction ) ) | |
95 if options.num_threads: | |
96 cmd += ( " -p %i" % int ( options.num_threads ) ) | |
97 if options.inner_mean_dist: | |
98 cmd += ( " -m %i" % int ( options.inner_mean_dist ) ) | |
99 if options.GTF: | |
100 cmd += ( " -G %s" % options.GTF ) | |
101 if options.GTFguide: | |
102 cmd += ( " -g %s" % options.GTFguide ) | |
103 if options.multi_read_correct: | |
104 cmd += ( " -u" ) | |
105 if options.num_importance_samples: | |
106 cmd += ( " --num-importance-samples %i" % int ( options.num_importance_samples ) ) | |
107 if options.max_mle_iterations: | |
108 cmd += ( " --max-mle-iterations %i" % int ( options.max_mle_iterations ) ) | |
109 if options.do_normalization: | |
110 cmd += ( " -N" ) | |
111 if options.do_bias_correction: | |
112 cmd += ( " -b %s" % seq_path ) | |
113 if options.no_effective_length_correction: | |
114 cmd += ( " --no-effective-length-correction" ) | |
115 | |
116 # Debugging. | |
117 print cmd | |
118 | |
119 # Add input files. | |
120 cmd += " " + options.input | |
121 | |
122 # | |
123 # Run command and handle output. | |
124 # | |
125 try: | |
126 # | |
127 # Run command. | |
128 # | |
129 tmp_name = tempfile.NamedTemporaryFile( dir="." ).name | |
130 tmp_stderr = open( tmp_name, 'wb' ) | |
131 proc = subprocess.Popen( args=cmd, shell=True, stderr=tmp_stderr.fileno() ) | |
132 returncode = proc.wait() | |
133 tmp_stderr.close() | |
134 | |
135 # Error checking. | |
136 if returncode != 0: | |
137 raise Exception, "return code = %i" % returncode | |
138 | |
139 # | |
140 # Handle output. | |
141 # | |
142 | |
143 # Read standard error to get total map/upper quartile mass. | |
144 total_map_mass = -1 | |
145 tmp_stderr = open( tmp_name, 'r' ) | |
146 for line in tmp_stderr: | |
147 if line.lower().find( "map mass" ) >= 0 or line.lower().find( "upper quartile" ) >= 0: | |
148 total_map_mass = float( line.split(":")[1].strip() ) | |
149 break | |
150 tmp_stderr.close() | |
151 | |
152 # | |
153 # If there's a global model provided, use model's total map mass | |
154 # to adjust FPKM + confidence intervals. | |
155 # | |
156 if options.global_model_file: | |
157 # Global model is simply total map mass from original run. | |
158 global_model_file = open( options.global_model_file, 'r' ) | |
159 global_model_total_map_mass = float( global_model_file.readline() ) | |
160 global_model_file.close() | |
161 | |
162 # Ratio of global model's total map mass to original run's map mass is | |
163 # factor used to adjust FPKM. | |
164 fpkm_map_mass_ratio = total_map_mass / global_model_total_map_mass | |
165 | |
166 # Update FPKM values in transcripts.gtf file. | |
167 transcripts_file = open( "transcripts.gtf", 'r' ) | |
168 tmp_transcripts = tempfile.NamedTemporaryFile( dir="." ).name | |
169 new_transcripts_file = open( tmp_transcripts, 'w' ) | |
170 for line in transcripts_file: | |
171 fields = line.split( '\t' ) | |
172 attrs = parse_gff_attributes( fields[8] ) | |
173 attrs[ "FPKM" ] = str( float( attrs[ "FPKM" ] ) * fpkm_map_mass_ratio ) | |
174 attrs[ "conf_lo" ] = str( float( attrs[ "conf_lo" ] ) * fpkm_map_mass_ratio ) | |
175 attrs[ "conf_hi" ] = str( float( attrs[ "conf_hi" ] ) * fpkm_map_mass_ratio ) | |
176 fields[8] = gff_attributes_to_str( attrs, "GTF" ) | |
177 new_transcripts_file.write( "%s\n" % '\t'.join( fields ) ) | |
178 transcripts_file.close() | |
179 new_transcripts_file.close() | |
180 shutil.copyfile( tmp_transcripts, "transcripts.gtf" ) | |
181 | |
182 # TODO: update expression files as well. | |
183 | |
184 # Set outputs. Transcript and gene expression handled by wrapper directives. | |
185 shutil.copyfile( "transcripts.gtf" , options.assembled_isoforms_output_file ) | |
186 if total_map_mass > -1: | |
187 f = open( "global_model.txt", 'w' ) | |
188 f.write( "%f\n" % total_map_mass ) | |
189 f.close() | |
190 except Exception, e: | |
191 # Read stderr so that it can be reported: | |
192 tmp_stderr = open( tmp_name, 'rb' ) | |
193 stderr = '' | |
194 buffsize = 1048576 | |
195 try: | |
196 while True: | |
197 stderr += tmp_stderr.read( buffsize ) | |
198 if not stderr or len( stderr ) % buffsize != 0: | |
199 break | |
200 except OverflowError: | |
201 pass | |
202 tmp_stderr.close() | |
203 | |
204 stop_err( 'Error running cufflinks.\n%s\n%s' % ( str( e ), stderr ) ) | |
205 | |
206 if __name__=="__main__": __main__() |