comparison velvet_optimiser.py @ 0:50ae1360fbbe default tip

Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
author konradpaszkiewicz
date Tue, 07 Jun 2011 18:07:56 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:50ae1360fbbe
1 #!/usr/bin/env python
2
3 """
4 VelvetOptimiser Wrapper
5 Adapted from velveth and velvetg tools in Galaxy
6 Konrad Paszkiewicz University of Exeter, UK.
7
8 """
9 import pkg_resources;
10 import logging, os, string, sys, tempfile, glob, shutil, types, urllib
11 import shlex, subprocess
12 from optparse import OptionParser, OptionGroup
13 from stat import *
14
15
16 log = logging.getLogger( __name__ )
17
18 assert sys.version_info[:2] >= ( 2, 4 )
19
20 def stop_err( msg ):
21 sys.stderr.write( "%s\n" % msg )
22 sys.exit()
23
24 def __main__():
25 #Parse Command Line
26 s = 'velvetg_optimiser.py: argv = %s\n' % (sys.argv)
27 #print >> sys.stderr, s # so will appear as blurb for file
28 argcnt = len(sys.argv)
29 starthash = sys.argv[1]
30 endhash = sys.argv[2]
31 inputs = sys.argv[3]
32 threads = sys.argv[4]
33 afgFile = sys.argv[5]
34 kmeropt = sys.argv[6]
35 covopt = sys.argv[7]
36 contigs = sys.argv[8]
37 LastGraph = sys.argv[9]
38 velvet_asm = sys.argv[10]
39 unusedReadsFile = sys.argv[11]
40 stats = sys.argv[12]
41 othervelvetgoptions = sys.argv[13]
42 working_dir = ''
43
44 cmdline = '/usr/local/velvet/contrib/VelvetOptimiser-2.1.7/VelvetOptimiser.pl -s %s -e %s -f \' %s \' -t %s -a 1 -k %s -c %s -o \'-unused_reads yes %s\' 2&1>/dev/null' % (starthash, endhash, inputs, threads, kmeropt, covopt, othervelvetgoptions)
45 #print >> sys.stderr, cmdline # so will appear as blurb for file
46 try:
47 proc = subprocess.Popen( args=cmdline, shell=True, stderr=subprocess.PIPE )
48 returncode = proc.wait()
49 # get stderr, allowing for case where it's very large
50 stderr = ''
51 buffsize = 1048576
52 try:
53 while True:
54 stderr += proc.stderr.read( buffsize )
55 if not stderr or len( stderr ) % buffsize != 0:
56 break
57 except OverflowError:
58 pass
59 if returncode != 0:
60 raise Exception, stderr
61 except Exception, e:
62 stop_err( 'Error running velvet_optimiser.py' + str( e ) )
63
64 out = open(contigs,'w')
65 contigs_path = os.path.join(working_dir,'contigs.fa')
66 #print >> sys.stderr, contigs_path
67 for line in open(contigs_path ):
68 out.write( "%s" % (line) )
69 out.close()
70 out = open(stats,'w')
71 stats_path = os.path.join(working_dir,'stats.txt')
72 for line in open( stats_path ):
73 out.write( "%s" % (line) )
74 out.close()
75 if LastGraph != 'None':
76 out = open(LastGraph,'w')
77 LastGraph_path = os.path.join(working_dir,'LastGraph')
78 for line in open( LastGraph_path ):
79 out.write( "%s" % (line) )
80 out.close()
81 if afgFile != 'None':
82 out = open(afgFile,'w')
83 afgFile_path = os.path.join(working_dir,'velvet_asm.afg')
84 try:
85 for line in open( afgFile_path ):
86 out.write( "%s" % (line) )
87 except:
88 logging.warn( 'error reading %s' %(afgFile_path))
89 pass
90 out.close()
91 if unusedReadsFile != 'None':
92 out = open(unusedReadsFile,'w')
93 unusedReadsFile_path = os.path.join(working_dir,'UnusedReads.fa')
94 try:
95 for line in open( unusedReadsFile_path ):
96 out.write( "%s" % (line) )
97 except:
98 logging.info( 'error reading %s' %(unusedReadsFile_path))
99 pass
100 out.close()
101
102 if __name__ == "__main__": __main__()