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 # Copied from sam_to_bam.py:
|
|
12 def check_seq_file( dbkey, cached_seqs_pointer_file ):
|
|
13 seq_path = ''
|
|
14 for line in open( cached_seqs_pointer_file ):
|
|
15 line = line.rstrip( '\r\n' )
|
|
16 if line and not line.startswith( '#' ) and line.startswith( 'index' ):
|
|
17 fields = line.split( '\t' )
|
|
18 if len( fields ) < 3:
|
|
19 continue
|
|
20 if fields[1] == dbkey:
|
|
21 seq_path = fields[2].strip()
|
|
22 break
|
|
23 return seq_path
|
|
24
|
|
25 def __main__():
|
|
26 #Parse Command Line
|
|
27 parser = optparse.OptionParser()
|
|
28 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.' )
|
|
29 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' )
|
|
30 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.')
|
|
31
|
|
32 # Wrapper / Galaxy options.
|
|
33 parser.add_option( '', '--dbkey', dest='dbkey', help='The build of the reference dataset' )
|
|
34 parser.add_option( '', '--index_dir', dest='index_dir', help='GALAXY_DATA_INDEX_DIR' )
|
|
35 parser.add_option( '', '--ref_file', dest='ref_file', help='The reference dataset from the history' )
|
|
36
|
|
37 # Outputs.
|
|
38 parser.add_option( '', '--combined-transcripts', dest='combined_transcripts' )
|
|
39
|
|
40 (options, args) = parser.parse_args()
|
|
41
|
|
42 # output version # of tool
|
|
43 try:
|
|
44 tmp = tempfile.NamedTemporaryFile().name
|
|
45 tmp_stdout = open( tmp, 'wb' )
|
|
46 proc = subprocess.Popen( args='cuffcompare 2>&1', shell=True, stdout=tmp_stdout )
|
|
47 tmp_stdout.close()
|
|
48 returncode = proc.wait()
|
|
49 stdout = None
|
|
50 for line in open( tmp_stdout.name, 'rb' ):
|
|
51 if line.lower().find( 'cuffcompare v' ) >= 0:
|
|
52 stdout = line.strip()
|
|
53 break
|
|
54 if stdout:
|
|
55 sys.stdout.write( '%s\n' % stdout )
|
|
56 else:
|
|
57 raise Exception
|
|
58 except:
|
|
59 sys.stdout.write( 'Could not determine Cuffcompare version\n' )
|
|
60
|
|
61 # Set/link to sequence file.
|
|
62 if options.use_seq_data:
|
|
63 if options.ref_file != 'None':
|
|
64 # Sequence data from history.
|
|
65 # Create symbolic link to ref_file so that index will be created in working directory.
|
|
66 seq_path = "ref.fa"
|
|
67 os.symlink( options.ref_file, seq_path )
|
|
68 else:
|
|
69 # Sequence data from loc file.
|
|
70 cached_seqs_pointer_file = os.path.join( options.index_dir, 'sam_fa_indices.loc' )
|
|
71 if not os.path.exists( cached_seqs_pointer_file ):
|
|
72 stop_err( 'The required file (%s) does not exist.' % cached_seqs_pointer_file )
|
|
73 # If found for the dbkey, seq_path will look something like /galaxy/data/equCab2/sam_index/equCab2.fa,
|
|
74 # and the equCab2.fa file will contain fasta sequences.
|
|
75 seq_path = check_seq_file( options.dbkey, cached_seqs_pointer_file )
|
|
76 if seq_path == '':
|
|
77 stop_err( 'No sequence data found for dbkey %s, so sequence data cannot be used.' % options.dbkey )
|
|
78
|
|
79 # Build command.
|
|
80
|
|
81 # Base.
|
|
82 cmd = "cuffcompare -o cc_output "
|
|
83
|
|
84 # Add options.
|
|
85 if options.ref_annotation:
|
|
86 cmd += " -r %s " % options.ref_annotation
|
|
87 if options.ignore_nonoverlap:
|
|
88 cmd += " -R "
|
|
89 if options.use_seq_data:
|
|
90 cmd += " -s %s " % seq_path
|
|
91
|
|
92 # Add input files.
|
|
93
|
|
94 # Need to symlink inputs so that output files are written to temp directory.
|
|
95 for i, arg in enumerate( args ):
|
|
96 input_file_name = "./input%i" % ( i+1 )
|
|
97 os.symlink( arg, input_file_name )
|
|
98 cmd += "%s " % input_file_name
|
|
99
|
|
100 # Debugging.
|
|
101 print cmd
|
|
102
|
|
103 # Run command.
|
|
104 try:
|
|
105 tmp_name = tempfile.NamedTemporaryFile( dir="." ).name
|
|
106 tmp_stderr = open( tmp_name, 'wb' )
|
|
107 proc = subprocess.Popen( args=cmd, shell=True, stderr=tmp_stderr.fileno() )
|
|
108 returncode = proc.wait()
|
|
109 tmp_stderr.close()
|
|
110
|
|
111 # Get stderr, allowing for case where it's very large.
|
|
112 tmp_stderr = open( tmp_name, 'rb' )
|
|
113 stderr = ''
|
|
114 buffsize = 1048576
|
|
115 try:
|
|
116 while True:
|
|
117 stderr += tmp_stderr.read( buffsize )
|
|
118 if not stderr or len( stderr ) % buffsize != 0:
|
|
119 break
|
|
120 except OverflowError:
|
|
121 pass
|
|
122 tmp_stderr.close()
|
|
123
|
|
124 # Error checking.
|
|
125 if returncode != 0:
|
|
126 raise Exception, stderr
|
|
127
|
|
128 # Copy outputs.
|
|
129 shutil.copyfile( "cc_output.combined.gtf" , options.combined_transcripts )
|
|
130
|
|
131 # check that there are results in the output file
|
|
132 cc_output_fname = "cc_output.stats"
|
|
133 if len( open( cc_output_fname, 'rb' ).read().strip() ) == 0:
|
|
134 raise Exception, 'The main output file is empty, there may be an error with your input file or settings.'
|
|
135 except Exception, e:
|
|
136 stop_err( 'Error running cuffcompare. ' + str( e ) )
|
|
137
|
|
138 if __name__=="__main__": __main__()
|