annotate tools/meme/fimo_wrapper.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 #Dan Blankenberg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 Read text output from FIMO and create an interval file.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import sys, tempfile, subprocess, shutil, os
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 from galaxy_utils.sequence.transform import DNA_reverse_complement
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 buffsize = 1048576
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 sys.stderr.write( msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 assert len( sys.argv ) == 8, "Wrong number of arguments"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 sys.argv.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 fimo_cmd = sys.argv.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 html_path = sys.argv.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 html_out = sys.argv.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 interval_out = sys.argv.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 txt_out = sys.argv.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 xml_out = sys.argv.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 gff_out = sys.argv.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 #run fimo
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 tmp_stderr = tempfile.NamedTemporaryFile()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 #tmp_stderr = open( tmp_filename, 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 proc = subprocess.Popen( args=fimo_cmd, shell=True, stderr=tmp_stderr )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 returncode = proc.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 #tmp_stderr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 # get stderr, allowing for case where it's very large
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 #tmp_stderr = open( tmp, 'rb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 tmp_stderr.seek(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 stderr = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 while True:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 stderr += tmp_stderr.read( buffsize )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 if not stderr or len( stderr ) % buffsize != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 except OverflowError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 if returncode != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 raise Exception, stderr
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 raise Exception, 'Error running FIMO:\n' + str( e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 shutil.move( os.path.join( html_path, 'fimo.txt' ), txt_out )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 shutil.move( os.path.join( html_path, 'fimo.gff' ), gff_out )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 shutil.move( os.path.join( html_path, 'fimo.xml' ), xml_out )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 shutil.move( os.path.join( html_path, 'fimo.html' ), html_out )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 out_file = open( interval_out, 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 out_file.write( "#%s\n" % "\t".join( ( "chr", "start", "end", "pattern name", "score", "strand", "matched sequence", "p-value", "q-value" ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 for line in open( txt_out ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 if line.startswith( '#' ): continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 fields = line.rstrip( "\n\r" ).split( "\t" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 start, end = int( fields[2] ), int( fields[3] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 sequence = fields[7]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 if start > end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 start, end = end, start #flip start and end, and set strand
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 strand = "-"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 sequence = DNA_reverse_complement( sequence ) #we want sequences relative to strand; FIMO always provides + stranded sequence
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 strand = "+"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 start -= 1 #make 0-based start position
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 out_file.write( "%s\n" % "\t".join( [ fields[1], str( start ), str( end ), fields[0], fields[4], strand, sequence, fields[5], fields[6] ] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 if __name__ == "__main__": main()