0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 import optparse, os, shutil, subprocess, sys, tempfile
|
|
4
|
|
5 def stop_err( msg ):
|
|
6 sys.stderr.write( "%s\n" % msg )
|
|
7 sys.exit()
|
|
8
|
|
9 # Copied from sam_to_bam.py:
|
|
10 def check_seq_file( dbkey, cached_seqs_pointer_file ):
|
|
11 seq_path = ''
|
|
12 for line in open( cached_seqs_pointer_file ):
|
|
13 line = line.rstrip( '\r\n' )
|
|
14 if line and not line.startswith( '#' ) and line.startswith( 'index' ):
|
|
15 fields = line.split( '\t' )
|
|
16 if len( fields ) < 3:
|
|
17 continue
|
|
18 if fields[1] == dbkey:
|
|
19 seq_path = fields[2].strip()
|
|
20 break
|
|
21 return seq_path
|
|
22
|
|
23 def __main__():
|
|
24 #Parse Command Line
|
|
25 parser = optparse.OptionParser()
|
|
26 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.' )
|
|
27 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.' )
|
|
28 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.' )
|
|
29 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.' )
|
|
30 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%.' )
|
|
31 parser.add_option( '-p', '--num-threads', dest='num_threads', help='Use this many threads to align reads. The default is 1.' )
|
|
32 parser.add_option( '-m', '--inner-mean-dist', dest='inner_mean_dist', help='This is the expected (mean) inner distance between mate pairs. \
|
|
33 For, example, for paired end runs with fragments selected at 300bp, \
|
|
34 where each end is 50bp, you should set -r to be 200. The default is 45bp.')
|
|
35 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.' )
|
|
36 parser.add_option( '-g', '--GTF-guide', dest='GTFguide', help='use reference transcript annotation to guide assembly' )
|
|
37
|
|
38 # Normalization options.
|
|
39 parser.add_option( "-N", "--quartile-normalization", dest="do_normalization", action="store_true" )
|
|
40
|
|
41 # Wrapper / Galaxy options.
|
|
42 parser.add_option( '-A', '--assembled-isoforms-output', dest='assembled_isoforms_output_file', help='Assembled isoforms output file; formate is GTF.' )
|
|
43
|
|
44 # Advanced Options:
|
|
45 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' )
|
|
46 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' )
|
|
47
|
|
48 # Bias correction options.
|
|
49 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.')
|
|
50 parser.add_option( '', '--dbkey', dest='dbkey', help='The build of the reference dataset' )
|
|
51 parser.add_option( '', '--index_dir', dest='index_dir', help='GALAXY_DATA_INDEX_DIR' )
|
|
52 parser.add_option( '', '--ref_file', dest='ref_file', help='The reference dataset from the history' )
|
|
53
|
|
54 (options, args) = parser.parse_args()
|
|
55
|
|
56 # output version # of tool
|
|
57 try:
|
|
58 tmp = tempfile.NamedTemporaryFile().name
|
|
59 tmp_stdout = open( tmp, 'wb' )
|
|
60 proc = subprocess.Popen( args='cufflinks --no-update-check 2>&1', shell=True, stdout=tmp_stdout )
|
|
61 tmp_stdout.close()
|
|
62 returncode = proc.wait()
|
|
63 stdout = None
|
|
64 for line in open( tmp_stdout.name, 'rb' ):
|
|
65 if line.lower().find( 'cufflinks v' ) >= 0:
|
|
66 stdout = line.strip()
|
|
67 break
|
|
68 if stdout:
|
|
69 sys.stdout.write( '%s\n' % stdout )
|
|
70 else:
|
|
71 raise Exception
|
|
72 except:
|
|
73 sys.stdout.write( 'Could not determine Cufflinks version\n' )
|
|
74
|
|
75 # If doing bias correction, set/link to sequence file.
|
|
76 if options.do_bias_correction:
|
|
77 cached_seqs_pointer_file = os.path.join( options.index_dir, 'sam_fa_indices.loc' )
|
|
78 if not os.path.exists( cached_seqs_pointer_file ):
|
|
79 stop_err( 'The required file (%s) does not exist.' % cached_seqs_pointer_file )
|
|
80 # If found for the dbkey, seq_path will look something like /galaxy/data/equCab2/sam_index/equCab2.fa,
|
|
81 # and the equCab2.fa file will contain fasta sequences.
|
|
82 seq_path = check_seq_file( options.dbkey, cached_seqs_pointer_file )
|
|
83 if options.ref_file != 'None':
|
|
84 # Create symbolic link to ref_file so that index will be created in working directory.
|
|
85 seq_path = "ref.fa"
|
|
86 os.symlink( options.ref_file, seq_path )
|
|
87
|
|
88 # Build command.
|
|
89
|
|
90 # Base; always use quiet mode to avoid problems with storing log output.
|
|
91 cmd = "cufflinks -q --no-update-check"
|
|
92
|
|
93 # Add options.
|
|
94 if options.inner_dist_std_dev:
|
|
95 cmd += ( " -s %i" % int ( options.inner_dist_std_dev ) )
|
|
96 if options.max_intron_len:
|
|
97 cmd += ( " -I %i" % int ( options.max_intron_len ) )
|
|
98 if options.min_isoform_fraction:
|
|
99 cmd += ( " -F %f" % float ( options.min_isoform_fraction ) )
|
|
100 if options.pre_mrna_fraction:
|
|
101 cmd += ( " -j %f" % float ( options.pre_mrna_fraction ) )
|
|
102 if options.num_threads:
|
|
103 cmd += ( " -p %i" % int ( options.num_threads ) )
|
|
104 if options.inner_mean_dist:
|
|
105 cmd += ( " -m %i" % int ( options.inner_mean_dist ) )
|
|
106 if options.GTF:
|
|
107 cmd += ( " -G %s" % options.GTF )
|
|
108 if options.GTFguide:
|
|
109 cmd += ( " -g %s" % options.GTFguide )
|
|
110 if options.num_importance_samples:
|
|
111 cmd += ( " --num-importance-samples %i" % int ( options.num_importance_samples ) )
|
|
112 if options.max_mle_iterations:
|
|
113 cmd += ( " --max-mle-iterations %i" % int ( options.max_mle_iterations ) )
|
|
114 if options.do_normalization:
|
|
115 cmd += ( " -N" )
|
|
116 if options.do_bias_correction:
|
|
117 cmd += ( " -b %s" % seq_path )
|
|
118
|
|
119 # Debugging.
|
|
120 print cmd
|
|
121
|
|
122 # Add input files.
|
|
123 cmd += " " + options.input
|
|
124
|
|
125 # Run command.
|
|
126 try:
|
|
127 tmp_name = tempfile.NamedTemporaryFile( dir="." ).name
|
|
128 tmp_stderr = open( tmp_name, 'wb' )
|
|
129 proc = subprocess.Popen( args=cmd, shell=True, stderr=tmp_stderr.fileno() )
|
|
130 returncode = proc.wait()
|
|
131 tmp_stderr.close()
|
|
132
|
|
133 # Get stderr, allowing for case where it's very large.
|
|
134 tmp_stderr = open( tmp_name, 'rb' )
|
|
135 stderr = ''
|
|
136 buffsize = 1048576
|
|
137 try:
|
|
138 while True:
|
|
139 stderr += tmp_stderr.read( buffsize )
|
|
140 if not stderr or len( stderr ) % buffsize != 0:
|
|
141 break
|
|
142 except OverflowError:
|
|
143 pass
|
|
144 tmp_stderr.close()
|
|
145
|
|
146 # Copy outputs.
|
|
147 shutil.copyfile( "transcripts.gtf" , options.assembled_isoforms_output_file )
|
|
148
|
|
149 # Error checking.
|
|
150 if returncode != 0:
|
|
151 raise Exception, stderr
|
|
152 except Exception, e:
|
|
153 stop_err( 'Error running cufflinks. ' + str( e ) )
|
|
154
|
|
155 if __name__=="__main__": __main__()
|