annotate tmap_wrapper_0.3.3/tmap_wrapper.py @ 2:e2640d81157a default tip

* moving to TMAP 0.3.3
author Nils Homer <nilshomer@gmail.com>
date Sun, 26 Feb 2012 20:28:27 -0500
parents tmap_wrapper_0.0.19/tmap_wrapper.py@de2efe4dda3f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
1 #!/usr/bin/env python
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
2
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
3 # TODO
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
4 # - map1/map2/map3 specific options
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
5
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
6 """
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
7 Runs TMAP on Ion Torrent data.
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
8 Produces a SAM file containing the mappings.
2
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
9 Works with TMAP version 0.3.3 or higher.
0
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
10
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
11 usage: tmap_wrapper.py [options]
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
12 --threads: The number of threads to use
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
13 --ref: The reference genome to use or index
2
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
14 --input: The input FASTQ/SFF file to use for the mapping
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
15 --inputtype: The input type (FASTQ/SFF)
0
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
16 --output: The file to save the output (SAM format)
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
17 --params: Parameter setting to use (pre_set or full)
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
18 --fileSource: Whether to use a previously indexed reference sequence or one from history (indexed or history)
2
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
19 --algorithm: The algorithm (ex. mapall, map1, map2, map3, map4, ...)
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
20 --globalOptions: The global options
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
21 --flowspaceOptions: The flowspace options
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
22 --pairingOptions: The pairing options
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
23 --algorithmOptions: The algorithm options
0
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
24 --suppressHeader: Suppress header
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
25 --dbkey: Dbkey for reference genome
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
26 --do_not_build_index: Flag to specify that provided file is already indexed and to just use 'as is'
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
27 """
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
28
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
29 import optparse, os, shutil, subprocess, sys, tempfile
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
30
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
31 def stop_err( msg ):
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
32 sys.stderr.write( '%s\n' % msg )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
33 sys.exit()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
34
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
35 def __main__():
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
36 #Parse Command Line
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
37 parser = optparse.OptionParser()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
38 # Global options
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
39 parser.add_option( '--threads', dest='threads', help='The number of threads to use' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
40 parser.add_option( '--ref', dest='ref', help='The reference genome to use or index' )
2
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
41 parser.add_option( '--input', dest='input', help='The input file to use for the mapping' )
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
42 parser.add_option( '--inputtype', dest='inputtype', help='The input file type' )
0
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
43 parser.add_option( '--output', dest='output', help='The file to save the output (SAM format)' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
44 parser.add_option( '--params', dest='params', help='Parameter setting to use (pre_set or full)' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
45 parser.add_option( '--fileSource', dest='fileSource', help='Whether to use a previously indexed reference sequence or one from history (indexed or history)' )
2
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
46 parser.add_option( '--algorithm', dest='algorithm', help='The algorithm (ex. mapall, map1, map2, map3, map4, ...)')
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
47 parser.add_option( '--globalOptions', dest='globalOptions', help='The global options ' )
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
48 parser.add_option( '--flowspaceOptions', dest='flowspaceOptions', help='The flowspace options' )
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
49 parser.add_option( '--pairingOptions', dest='pairingOptions', help='The pairing options' )
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
50 parser.add_option( '--algorithmOptions', dest='algorithmOptions', help='The algorithm options' )
0
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
51 parser.add_option( '--suppressHeader', dest='suppressHeader', help='Suppress header' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
52 parser.add_option( '--dbkey', dest='dbkey', help='Dbkey for reference genome' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
53 parser.add_option( '--do_not_build_index', dest='do_not_build_index', action='store_true', help="Don't build index" )
2
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
54
0
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
55 # parse the options
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
56 (options, args) = parser.parse_args()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
57
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
58 # output version # of tool
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
59 try:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
60 tmp = tempfile.NamedTemporaryFile().name
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
61 tmp_stdout = open( tmp, 'wb' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
62 proc = subprocess.Popen( args='tmap --version 2>&1', shell=True, stdout=tmp_stdout )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
63 tmp_stdout.close()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
64 returncode = proc.wait()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
65 stdout = None
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
66 for line in open( tmp_stdout.name, 'rb' ):
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
67 if line.lower().find( 'version' ) >= 0:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
68 stdout = line.strip()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
69 break
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
70 if stdout:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
71 sys.stdout.write( 'TMAP %s\n' % stdout )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
72 else:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
73 raise Exception
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
74 except:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
75 sys.stdout.write( 'Could not determine TMAP version\n' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
76
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
77 # make temp directory for placement of indices
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
78 tmp_index_dir = tempfile.mkdtemp()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
79 tmp_dir = tempfile.mkdtemp()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
80
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
81 # index if necessary
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
82 if options.fileSource == 'history' and not options.do_not_build_index:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
83 ref_file = tempfile.NamedTemporaryFile( dir=tmp_index_dir )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
84 ref_file_name = ref_file.name
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
85 ref_file.close()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
86 os.symlink( options.ref, ref_file_name )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
87 cmd1 = 'tmap index -f %s -v ' % ( ref_file_name )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
88 try:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
89 tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
90 tmp_stderr = open( tmp, 'wb' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
91 proc = subprocess.Popen( args=cmd1, shell=True, cwd=tmp_index_dir, stderr=tmp_stderr.fileno() )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
92 returncode = proc.wait()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
93 tmp_stderr.close()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
94 # get stderr, allowing for case where it's very large
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
95 tmp_stderr = open( tmp, 'rb' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
96 stderr = ''
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
97 buffsize = 1048576
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
98 try:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
99 while True:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
100 stderr += tmp_stderr.read( buffsize )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
101 if not stderr or len( stderr ) % buffsize != 0:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
102 break
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
103 except OverflowError:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
104 pass
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
105 tmp_stderr.close()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
106 if returncode != 0:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
107 raise Exception, stderr
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
108 except Exception, e:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
109 # clean up temp dirs
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
110 if os.path.exists( tmp_index_dir ):
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
111 shutil.rmtree( tmp_index_dir )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
112 if os.path.exists( tmp_dir ):
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
113 shutil.rmtree( tmp_dir )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
114 stop_err( 'Error indexing reference sequence. ' + str( e ) )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
115 else:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
116 ref_file_name = options.ref
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
117
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
118 # set up mapping and generate mapping command options
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
119 if options.params == 'pre_set':
2
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
120 options.algorithm = 'mapall'
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
121 options.flowspaceOptions = ''
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
122 options.pairingOptions = ''
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
123 options.algorithmOptions = 'stage1 map1 map2 map3'
0
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
124
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
125 #mapping_cmds
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
126 # prepare actual mapping and generate mapping commands
2
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
127 cmd = 'tmap %s -f %s -r %s -i %s -n %s %s %s %s > %s' % \
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
128 ( options.algorithm, options.ref, options.input, options.inputtype, \
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
129 options.threads, options.flowspaceOptions, \
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
130 options.pairingOptions, options.algorithmOptions, options.output )
0
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
131 # perform alignments
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
132 buffsize = 1048576
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
133 try:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
134 # need to nest try-except in try-finally to handle 2.4
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
135 try:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
136 # align
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
137 try:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
138 tmp = tempfile.NamedTemporaryFile( dir=tmp_dir ).name
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
139 tmp_stderr = open( tmp, 'wb' )
2
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
140 proc = subprocess.Popen( args=cmd, shell=True, cwd=tmp_dir, stderr=tmp_stderr.fileno() )
0
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
141 returncode = proc.wait()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
142 tmp_stderr.close()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
143 # get stderr, allowing for case where it's very large
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
144 tmp_stderr = open( tmp, 'rb' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
145 stderr = ''
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
146 try:
2
e2640d81157a * moving to TMAP 0.3.3
Nils Homer <nilshomer@gmail.com>
parents: 0
diff changeset
147 stderr += cmd + '\n'
0
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
148 while True:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
149 stderr += tmp_stderr.read( buffsize )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
150 if not stderr or len( stderr ) % buffsize != 0:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
151 break
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
152 except OverflowError:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
153 pass
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
154 tmp_stderr.close()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
155 if returncode != 0:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
156 raise Exception, stderr
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
157 except Exception, e:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
158 raise Exception, 'Error mapping sequence. ' + str( e )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
159 # remove header if necessary
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
160 if options.suppressHeader == 'true':
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
161 tmp_out = tempfile.NamedTemporaryFile( dir=tmp_dir)
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
162 tmp_out_name = tmp_out.name
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
163 tmp_out.close()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
164 try:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
165 shutil.move( options.output, tmp_out_name )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
166 except Exception, e:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
167 raise Exception, 'Error moving output file before removing headers. ' + str( e )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
168 fout = file( options.output, 'w' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
169 for line in file( tmp_out.name, 'r' ):
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
170 if not ( line.startswith( '@HD' ) or line.startswith( '@SQ' ) or line.startswith( '@RG' ) or line.startswith( '@PG' ) or line.startswith( '@CO' ) ):
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
171 fout.write( line )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
172 fout.close()
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
173 # check that there are results in the output file
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
174 if os.path.getsize( options.output ) > 0:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
175 sys.stdout.write( 'TMAP completed' )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
176 else:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
177 raise Exception, 'The output file is empty. You may simply have no matches, or there may be an error with your input file or settings.'
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
178 except Exception, e:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
179 stop_err( 'The alignment failed.\n' + str( e ) )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
180 finally:
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
181 # clean up temp dir
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
182 if os.path.exists( tmp_index_dir ):
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
183 shutil.rmtree( tmp_index_dir )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
184 if os.path.exists( tmp_dir ):
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
185 shutil.rmtree( tmp_dir )
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
186
de2efe4dda3f Migrated tool version 0.0.19 from old tool shed archive to new tool shed repository
nilshomer
parents:
diff changeset
187 if __name__=="__main__": __main__()