annotate tools/sr_assembly/velvetg_wrapper.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
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
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Classes encapsulating decypher tool.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 James E Johnson - University of Minnesota
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import pkg_resources;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 import logging, os, string, sys, tempfile, glob, shutil, types, urllib
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 import shlex, subprocess
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 from optparse import OptionParser, OptionGroup
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 from stat import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 log = logging.getLogger( __name__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 sys.stderr.write( "%s\n" % msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 #Parse Command Line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 s = 'velvetg_wrapper.py: argv = %s\n' % (sys.argv)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 # print >> sys.stderr, s # so will appear as blurb for file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 argcnt = len(sys.argv)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 working_dir = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 contigs = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 stats = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 LastGraph = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 afgFile = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 unusedReadsFile = sys.argv[6]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 inputs = string.join(sys.argv[7:],' ')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 cmdline = 'velvetg %s %s > /dev/null' % (working_dir, inputs)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 # print >> sys.stderr, cmdline # so will appear as blurb for file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 proc = subprocess.Popen( args=cmdline, shell=True, stderr=subprocess.PIPE )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 returncode = proc.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 # get stderr, allowing for case where it's very large
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 stderr = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 buffsize = 1048576
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 while True:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 stderr += proc.stderr.read( buffsize )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 if not stderr or len( stderr ) % buffsize != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 except OverflowError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 if returncode != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 raise Exception, stderr
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 stop_err( 'Error running velvetg ' + str( e ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 out = open(contigs,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 contigs_path = os.path.join(working_dir,'contigs.fa')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 for line in open( contigs_path ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 out.write( "%s" % (line) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 out = open(stats,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 stats_path = os.path.join(working_dir,'stats.txt')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 for line in open( stats_path ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 out.write( "%s" % (line) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 if LastGraph != 'None':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 out = open(LastGraph,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 LastGraph_path = os.path.join(working_dir,'LastGraph')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 for line in open( LastGraph_path ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 out.write( "%s" % (line) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 if afgFile != 'None':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 out = open(afgFile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 afgFile_path = os.path.join(working_dir,'velvet_asm.afg')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 for line in open( afgFile_path ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 out.write( "%s" % (line) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 logging.warn( 'error reading %s' %(afgFile_path))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 if unusedReadsFile != 'None':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 out = open(unusedReadsFile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 unusedReadsFile_path = os.path.join(working_dir,'UnusedReads.fa')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 for line in open( unusedReadsFile_path ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 out.write( "%s" % (line) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 logging.info( 'error reading %s' %(unusedReadsFile_path))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 if __name__ == "__main__": __main__()