Mercurial > repos > devteam > cuffcompare
annotate cuffcompare_wrapper.py @ 5:67695d7ff787 draft
Replace sam_fa_indices.loc with fasta_indexes.loc in fasta_indexes.loc.sample.
author | devteam |
---|---|
date | Thu, 09 Jan 2014 14:27:37 -0500 |
parents | 8b22e9adae34 |
children | 8e534225baa9 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 # Supports Cuffcompare versions v1.3.0 and newer. | |
4 | |
5 import optparse, os, shutil, subprocess, sys, tempfile | |
6 | |
7 def stop_err( msg ): | |
8 sys.stderr.write( '%s\n' % msg ) | |
9 sys.exit() | |
10 | |
11 def __main__(): | |
12 #Parse Command Line | |
13 parser = optparse.OptionParser() | |
14 parser.add_option( '-r', dest='ref_annotation', help='An optional "reference" annotation GTF. Each sample is matched against this file, and sample isoforms are tagged as overlapping, matching, or novel where appropriate. See the refmap and tmap output file descriptions below.' ) | |
15 parser.add_option( '-R', action="store_true", dest='ignore_nonoverlap', help='If -r was specified, this option causes cuffcompare to ignore reference transcripts that are not overlapped by any transcript in one of cuff1.gtf,...,cuffN.gtf. Useful for ignoring annotated transcripts that are not present in your RNA-Seq samples and thus adjusting the "sensitivity" calculation in the accuracy report written in the transcripts accuracy file' ) | |
16 parser.add_option( '-s', dest='use_seq_data', action="store_true", help='Causes cuffcompare to look into for fasta files with the underlying genomic sequences (one file per contig) against which your reads were aligned for some optional classification functions. For example, Cufflinks transcripts consisting mostly of lower-case bases are classified as repeats. Note that <seq_dir> must contain one fasta file per reference chromosome, and each file must be named after the chromosome, and have a .fa or .fasta extension.') | |
17 | |
18 # Wrapper / Galaxy options. | |
2
8b22e9adae34
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
19 parser.add_option( '', '--index', dest='index', help='The path of the reference genome' ) |
0 | 20 parser.add_option( '', '--ref_file', dest='ref_file', help='The reference dataset from the history' ) |
21 | |
22 # Outputs. | |
23 parser.add_option( '', '--combined-transcripts', dest='combined_transcripts' ) | |
24 | |
25 (options, args) = parser.parse_args() | |
26 | |
27 # output version # of tool | |
28 try: | |
29 tmp = tempfile.NamedTemporaryFile().name | |
30 tmp_stdout = open( tmp, 'wb' ) | |
31 proc = subprocess.Popen( args='cuffcompare 2>&1', shell=True, stdout=tmp_stdout ) | |
32 tmp_stdout.close() | |
33 returncode = proc.wait() | |
34 stdout = None | |
35 for line in open( tmp_stdout.name, 'rb' ): | |
36 if line.lower().find( 'cuffcompare v' ) >= 0: | |
37 stdout = line.strip() | |
38 break | |
39 if stdout: | |
40 sys.stdout.write( '%s\n' % stdout ) | |
41 else: | |
42 raise Exception | |
43 except: | |
44 sys.stdout.write( 'Could not determine Cuffcompare version\n' ) | |
45 | |
46 # Set/link to sequence file. | |
47 if options.use_seq_data: | |
2
8b22e9adae34
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
48 if options.ref_file: |
0 | 49 # Sequence data from history. |
50 # Create symbolic link to ref_file so that index will be created in working directory. | |
51 seq_path = "ref.fa" | |
52 os.symlink( options.ref_file, seq_path ) | |
53 else: | |
2
8b22e9adae34
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
54 if not os.path.exists( options.index ): |
8b22e9adae34
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
55 stop_err( 'Reference genome %s not present, request it by reporting this error.' % options.index ) |
8b22e9adae34
Update to the new data table specification.
Dave Bouvier <dave@bx.psu.edu>
parents:
0
diff
changeset
|
56 seq_path = options.index |
0 | 57 |
58 # Build command. | |
59 | |
60 # Base. | |
61 cmd = "cuffcompare -o cc_output " | |
62 | |
63 # Add options. | |
64 if options.ref_annotation: | |
65 cmd += " -r %s " % options.ref_annotation | |
66 if options.ignore_nonoverlap: | |
67 cmd += " -R " | |
68 if options.use_seq_data: | |
69 cmd += " -s %s " % seq_path | |
70 | |
71 # Add input files. | |
72 | |
73 # Need to symlink inputs so that output files are written to temp directory. | |
74 for i, arg in enumerate( args ): | |
75 input_file_name = "./input%i" % ( i+1 ) | |
76 os.symlink( arg, input_file_name ) | |
77 cmd += "%s " % input_file_name | |
78 | |
79 # Debugging. | |
80 print cmd | |
81 | |
82 # Run command. | |
83 try: | |
84 tmp_name = tempfile.NamedTemporaryFile( dir="." ).name | |
85 tmp_stderr = open( tmp_name, 'wb' ) | |
86 proc = subprocess.Popen( args=cmd, shell=True, stderr=tmp_stderr.fileno() ) | |
87 returncode = proc.wait() | |
88 tmp_stderr.close() | |
89 | |
90 # Get stderr, allowing for case where it's very large. | |
91 tmp_stderr = open( tmp_name, 'rb' ) | |
92 stderr = '' | |
93 buffsize = 1048576 | |
94 try: | |
95 while True: | |
96 stderr += tmp_stderr.read( buffsize ) | |
97 if not stderr or len( stderr ) % buffsize != 0: | |
98 break | |
99 except OverflowError: | |
100 pass | |
101 tmp_stderr.close() | |
102 | |
103 # Error checking. | |
104 if returncode != 0: | |
105 raise Exception, stderr | |
106 | |
107 # Copy outputs. | |
108 shutil.copyfile( "cc_output.combined.gtf" , options.combined_transcripts ) | |
109 | |
110 # check that there are results in the output file | |
111 cc_output_fname = "cc_output.stats" | |
112 if len( open( cc_output_fname, 'rb' ).read().strip() ) == 0: | |
113 raise Exception, 'The main output file is empty, there may be an error with your input file or settings.' | |
114 except Exception, e: | |
115 stop_err( 'Error running cuffcompare. ' + str( e ) ) | |
116 | |
117 if __name__=="__main__": __main__() |