root/galaxy-central/tools/ngs_rna/cufflinks_wrapper.py @ 2

リビジョン 2, 7.1 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

行番号 
1#!/usr/bin/env python
2
3import optparse, os, shutil, subprocess, sys, tempfile
4
5def stop_err( msg ):
6    sys.stderr.write( "%s\n" % msg )
7    sys.exit()
8
9def __main__():
10    #Parse Command Line
11    parser = optparse.OptionParser()
12    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.' )
13    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.' )
14    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.' )
15    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.' )
16    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%.' )
17    parser.add_option( '-p', '--num-threads', dest='num_threads', help='Use this many threads to align reads. The default is 1.' )
18    parser.add_option( '-m', '--inner-mean-dist', dest='inner_mean_dist', help='This is the expected (mean) inner distance between mate pairs. \
19                                                                                For, example, for paired end runs with fragments selected at 300bp, \
20                                                                                where each end is 50bp, you should set -r to be 200. The default is 45bp.')
21    parser.add_option( '-Q', '--min-mapqual', dest='min_mapqual', help='Instructs Cufflinks to ignore alignments with a SAM mapping quality lower than this number. The default is 0.' )
22    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.' )
23
24    # Advanced Options:
25    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' )
26    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' )
27   
28    # Wrapper / Galaxy options.
29    parser.add_option( '-A', '--assembled-isoforms-output', dest='assembled_isoforms_output_file', help='Assembled isoforms output file; formate is GTF.' )
30    parser.add_option( '-T', '--transcripts-expression-output', dest='transcripts_expression_output_file', help='TODO' )
31    parser.add_option( '-Z', '--genes-expression-output', dest='genes_expression_output_file', help='TODO' )
32   
33    (options, args) = parser.parse_args()
34   
35    # Make temp directory for output.
36    tmp_output_dir = tempfile.mkdtemp()
37   
38    # Build command.
39   
40    # Base.
41    cmd = "cufflinks"
42   
43    # Add options.
44    if options.inner_dist_std_dev:
45        cmd += ( " -s %i" % int ( options.inner_dist_std_dev ) )
46    if options.max_intron_len:
47        cmd += ( " -I %i" % int ( options.max_intron_len ) )
48    if options.min_isoform_fraction:
49        cmd += ( " -F %f" % float ( options.min_isoform_fraction ) )
50    if options.pre_mrna_fraction:
51        cmd += ( " -j %f" % float ( options.pre_mrna_fraction ) )   
52    if options.num_threads:
53        cmd += ( " -p %i" % int ( options.num_threads ) )
54    if options.inner_mean_dist:
55        cmd += ( " -m %i" % int ( options.inner_mean_dist ) )
56    if options.min_mapqual:
57        cmd += ( " -Q %i" % int ( options.min_mapqual ) )
58    if options.GTF:
59        cmd += ( " -G %s" % options.GTF )
60    if options.num_importance_samples:
61        cmd += ( " --num-importance-samples %i" % int ( options.num_importance_samples ) )
62    if options.max_mle_iterations:
63        cmd += ( " --max-mle-iterations %i" % int ( options.max_mle_iterations ) )
64       
65    # Add input files.
66    cmd += " " + options.input
67   
68    # Run command.
69    try:
70        tmp_name = tempfile.NamedTemporaryFile( dir=tmp_output_dir ).name
71        tmp_stderr = open( tmp_name, 'wb' )
72        proc = subprocess.Popen( args=cmd, shell=True, cwd=tmp_output_dir, stderr=tmp_stderr.fileno() )
73        returncode = proc.wait()
74        tmp_stderr.close()
75       
76        # Get stderr, allowing for case where it's very large.
77        tmp_stderr = open( tmp_name, 'rb' )
78        stderr = ''
79        buffsize = 1048576
80        try:
81            while True:
82                stderr += tmp_stderr.read( buffsize )
83                if not stderr or len( stderr ) % buffsize != 0:
84                    break
85        except OverflowError:
86            pass
87        tmp_stderr.close()
88       
89        # Error checking.
90        if returncode != 0:
91            raise Exception, stderr
92           
93        # check that there are results in the output file
94        if len( open( tmp_output_dir + "/transcripts.gtf", 'rb' ).read().strip() ) == 0:
95            raise Exception, 'The main output file is empty, there may be an error with your input file or settings.'
96    except Exception, e:
97        stop_err( 'Error running cufflinks. ' + str( e ) )
98       
99    # Copy output files from tmp directory to specified files.
100    try:
101        try:
102            shutil.copyfile( tmp_output_dir + "/transcripts.gtf", options.assembled_isoforms_output_file )
103            shutil.copyfile( tmp_output_dir + "/transcripts.expr", options.transcripts_expression_output_file )
104            shutil.copyfile( tmp_output_dir + "/genes.expr", options.genes_expression_output_file )
105        except Exception, e:
106            stop_err( 'Error in tophat:\n' + str( e ) )
107    finally:
108        # Clean up temp dirs
109        if os.path.exists( tmp_output_dir ):
110            shutil.rmtree( tmp_output_dir )
111
112if __name__=="__main__": __main__()
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。