annotate bismark_wrapper.py @ 3:91f07ff056ca draft

Uploaded
author bgruening
date Mon, 14 Apr 2014 16:43:14 -0400
parents 82814a8a2395
children 243e8f9fb75b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
1 #!/usr/bin/env python
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
2
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
3 import argparse
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
4 import os
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
5 import shutil
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
6 import subprocess
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
7 import sys
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
8 import shlex
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
9 import tempfile
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
10 import fileinput
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
11 import fileinput
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
12 from glob import glob
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
13
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
14 def stop_err( msg ):
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
15 sys.stderr.write( "%s\n" % msg )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
16 sys.exit()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
17
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
18 def __main__():
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
19
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
20 print 'tempfile_location',tempfile.gettempdir()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
21 #Parse Command Line
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
22 parser = argparse.ArgumentParser(description='Wrapper for the bismark bisulfite mapper.')
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
23 parser.add_argument( '-p', '--num-threads', dest='num_threads',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
24 type=int, default=4, help='Use this many threads to align reads. The default is 4.' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
25
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
26 parser.add_argument( '--bismark_path', dest='bismark_path', help='Path to the bismark perl scripts' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
27
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
28 parser.add_argument( '--bowtie2', action='store_true', default=False, help='Running bismark with bowtie2 and not with bowtie.' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
29
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
30 # input options
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
31 parser.add_argument( '--own-file', dest='own_file', help='' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
32 parser.add_argument( '-D', '--indexes-path', dest='index_path', help='Indexes directory; location of .ebwt and .fa files.' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
33 parser.add_argument( '-O', '--output', dest='output' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
34
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
35
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
36 parser.add_argument( '--output-report-file', dest='output_report_file' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
37 parser.add_argument( '--suppress-header', dest='suppress_header', action="store_true" )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
38
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
39 parser.add_argument( '--mate-paired', dest='mate_paired', action='store_true', help='Reads are mate-paired', default=False)
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
40
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
41
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
42 parser.add_argument( '-1', '--mate1', dest='mate1',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
43 help='The forward reads file in Sanger FASTQ or FASTA format.' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
44 parser.add_argument( '-2', '--mate2', dest='mate2',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
45 help='The reverse reads file in Sanger FASTQ or FASTA format.' )
3
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
46 parser.add_argument( '--sort-bam', dest='sort_bam', action="store_true" )
0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
47
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
48 parser.add_argument( '--output-unmapped-reads', dest='output_unmapped_reads',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
49 help='Additional output file with unmapped reads (single-end).' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
50 parser.add_argument( '--output-unmapped-reads-l', dest='output_unmapped_reads_l',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
51 help='File name for unmapped reads (left, paired-end).' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
52 parser.add_argument( '--output-unmapped-reads-r', dest='output_unmapped_reads_r',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
53 help='File name for unmapped reads (right, paired-end).' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
54
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
55
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
56 parser.add_argument( '--output-suppressed-reads', dest='output_suppressed_reads',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
57 help='Additional output file with suppressed reads (single-end).' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
58 parser.add_argument( '--output-suppressed-reads-l', dest='output_suppressed_reads_l',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
59 help='File name for suppressed reads (left, paired-end).' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
60 parser.add_argument( '--output-suppressed-reads-r', dest='output_suppressed_reads_r',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
61 help='File name for suppressed reads (right, paired-end).' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
62 parser.add_argument( '--stdout', dest='output_stdout',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
63 help='File name for the standard output of bismark.' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
64
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
65
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
66 parser.add_argument( '--single-paired', dest='single_paired',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
67 help='The single-end reads file in Sanger FASTQ or FASTA format.' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
68
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
69 parser.add_argument( '--fastq', action='store_true', help='Query filetype is in FASTQ format')
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
70 parser.add_argument( '--fasta', action='store_true', help='Query filetype is in FASTA format')
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
71 parser.add_argument( '--phred64-quals', dest='phred64', action="store_true" )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
72
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
73
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
74 parser.add_argument( '--skip-reads', dest='skip_reads', type=int )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
75 parser.add_argument( '--qupto', type=int)
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
76
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
77
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
78 # paired end options
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
79 parser.add_argument( '-I', '--minins', dest='min_insert' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
80 parser.add_argument( '-X', '--maxins', dest='max_insert' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
81 parser.add_argument( '--no-mixed', dest='no_mixed', action="store_true" )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
82 parser.add_argument( '--no-discordant', dest='no_discordant', action="store_true" )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
83
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
84 #parse general options
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
85 # default 20
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
86 parser.add_argument( '--seed-len', dest='seed_len', type=int)
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
87 # default 15
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
88 parser.add_argument( '--seed-extention-attempts', dest='seed_extention_attempts', type=int )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
89 # default 0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
90 parser.add_argument( '--seed-mismatches', dest='seed_mismatches', type=int )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
91 # default 2
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
92 parser.add_argument( '--max-reseed', dest='max_reseed', type=int )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
93 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
94 # default 70
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
95 parser.add_argument( '--maqerr', dest='maqerr', type=int )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
96 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
97
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
98 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
99 The number of megabytes of memory a given thread is given to store path
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
100 descriptors in --best mode. Best-first search must keep track of many paths
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
101 at once to ensure it is always extending the path with the lowest cumulative
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
102 cost. Bowtie tries to minimize the memory impact of the descriptors, but
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
103 they can still grow very large in some cases. If you receive an error message
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
104 saying that chunk memory has been exhausted in --best mode, try adjusting
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
105 this parameter up to dedicate more memory to the descriptors. Default: 512.
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
106 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
107 parser.add_argument( '--chunkmbs', type=int, default=512 )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
108
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
109 args = parser.parse_args()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
110
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
111 # Create bismark index if necessary.
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
112 index_dir = ""
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
113 if args.own_file:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
114 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
115 Create a temporary index with the offered files from the user.
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
116 Utilizing the script: bismark_genome_preparation
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
117 bismark_genome_preparation --bowtie2 hg19/
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
118 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
119 tmp_index_dir = tempfile.mkdtemp()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
120 index_path = os.path.join( tmp_index_dir, '.'.join( os.path.split( args.own_file )[1].split( '.' )[:-1] ) )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
121 try:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
122 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
123 Create a hard link pointing to args.own_file named 'index_path'.fa.
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
124 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
125 os.symlink( args.own_file, index_path + '.fa' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
126 except Exception, e:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
127 if os.path.exists( tmp_index_dir ):
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
128 shutil.rmtree( tmp_index_dir )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
129 stop_err( 'Error in linking the reference database.\n' + str( e ) )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
130 # bismark_genome_preparation needs the complete path to the folder in which the database is stored
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
131 if args.bowtie2:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
132 cmd_index = 'bismark_genome_preparation --bowtie2 %s ' % ( tmp_index_dir )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
133 else:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
134 cmd_index = 'bismark_genome_preparation %s ' % ( tmp_index_dir )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
135 if args.bismark_path:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
136 if os.path.exists(args.bismark_path):
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
137 # add the path to the bismark perl scripts, that is needed for galaxy
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
138 cmd_index = os.path.join(args.bismark_path, cmd_index)
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
139 else:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
140 # assume the same directory as that script
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
141 cmd_index = 'perl %s' % os.path.join(os.path.realpath(os.path.dirname(__file__)), cmd_index)
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
142 try:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
143 tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
144 tmp_stderr = open( tmp, 'wb' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
145 proc = subprocess.Popen( args=cmd_index, shell=True, cwd=tmp_index_dir, stdout=open(os.devnull, 'wb'), stderr=tmp_stderr.fileno() )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
146 returncode = proc.wait()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
147 tmp_stderr.close()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
148 # get stderr, allowing for case where it's very large
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
149 tmp_stderr = open( tmp, 'rb' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
150 stderr = ''
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
151 buffsize = 1048576
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
152 try:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
153 while True:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
154 stderr += tmp_stderr.read( buffsize )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
155 if not stderr or len( stderr ) % buffsize != 0:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
156 break
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
157 except OverflowError:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
158 pass
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
159 tmp_stderr.close()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
160 if returncode != 0:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
161 raise Exception, stderr
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
162 except Exception, e:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
163 if os.path.exists( tmp_index_dir ):
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
164 shutil.rmtree( tmp_index_dir )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
165 stop_err( 'Error indexing reference sequence\n' + str( e ) )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
166 index_dir = tmp_index_dir
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
167 else:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
168 # bowtie path is the path to the index directory and the first path of the index file name
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
169 index_dir = os.path.dirname( args.index_path )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
170
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
171 # Build bismark command
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
172
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
173 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
174 Bismark requires a large amount of temporary disc space. If that is not available, for example on a cluster you can hardcode the
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
175 TMP to some larger space. It's not recommended but it works.
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
176 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
177 #tmp_bismark_dir = tempfile.mkdtemp( dir='/data/0/galaxy_db/tmp/' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
178 tmp_bismark_dir = tempfile.mkdtemp()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
179 output_dir = os.path.join( tmp_bismark_dir, 'results')
3
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
180 cmd = 'bismark %(args)s --bam --temp_dir %(tmp_bismark_dir)s --gzip -o %(output_dir)s --quiet %(genome_folder)s %(reads)s'
0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
181
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
182 if args.fasta:
3
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
183 # the query input files (specified as mate1,mate2 or singles) are FastA
0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
184 cmd = '%s %s' % (cmd, '--fasta')
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
185 elif args.fastq:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
186 cmd = '%s %s' % (cmd, '--fastq')
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
187
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
188 if args.bismark_path:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
189 # add the path to the bismark perl scripts, that is needed for galaxy
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
190 if os.path.exists(args.bismark_path):
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
191 cmd = os.path.join(args.bismark_path, cmd)
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
192 else:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
193 # assume the same directory as that script
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
194 cmd = 'perl %s' % os.path.join(os.path.realpath(os.path.dirname(__file__)), cmd)
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
195
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
196 arguments = {
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
197 'genome_folder': index_dir,
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
198 'args': '',
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
199 'tmp_bismark_dir': tmp_bismark_dir,
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
200 'output_dir': output_dir,
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
201 }
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
202
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
203 additional_opts = ''
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
204 # Set up the reads
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
205 if args.mate_paired:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
206 # paired-end reads library
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
207 reads = '-1 %s ' % ( args.mate1 )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
208 reads += ' -2 %s ' % ( args.mate2 )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
209 additional_opts += ' -I %s -X %s ' % (args.min_insert, args.max_insert)
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
210 else:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
211 # single paired reads library
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
212 reads = ' %s ' % ( args.single_paired )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
213
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
214
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
215 if not args.bowtie2:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
216 # use bowtie specific options
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
217 #additional_opts += ' --best ' # bug in bismark, --best is not available as option. Only --non-best, best-mode is activated by default
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
218 if args.seed_mismatches:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
219 # --seedmms
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
220 additional_opts += ' -n %s ' % args.seed_mismatches
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
221 if args.seed_len:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
222 # --seedlen
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
223 additional_opts += ' -l %s ' % args.seed_len
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
224
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
225 # alignment options
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
226 if args.bowtie2:
3
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
227 additional_opts += ' -p %s --bowtie2 ' % (int(args.num_threads/2)) #divides by 2 here since bismark will spawn 2 (original top and original bottom) jobs with -p threads each
0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
228 if args.seed_mismatches:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
229 additional_opts += ' -N %s ' % args.seed_mismatches
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
230 if args.seed_len:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
231 additional_opts += ' -L %s ' % args.seed_len
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
232 if args.seed_extention_attempts:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
233 additional_opts += ' -D %s ' % args.seed_extention_attempts
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
234 if args.max_reseed:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
235 additional_opts += ' -R %s ' % args.max_reseed
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
236 if args.no_discordant:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
237 additional_opts += ' --no-discordant '
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
238 if args.no_mixed:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
239 additional_opts += ' --no-mixed '
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
240 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
241 if args.maqerr:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
242 additional_opts += ' --maqerr %s ' % args.maqerr
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
243 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
244 if args.skip_reads:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
245 additional_opts += ' --skip %s ' % args.skip_reads
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
246 if args.qupto:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
247 additional_opts += ' --qupto %s ' % args.qupto
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
248 if args.phred64:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
249 additional_opts += ' --phred64-quals '
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
250 if args.suppress_header:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
251 additional_opts += ' --sam-no-hd '
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
252 if args.output_unmapped_reads or ( args.output_unmapped_reads_l and args.output_unmapped_reads_r):
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
253 additional_opts += ' --un '
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
254 if args.output_suppressed_reads or ( args.output_suppressed_reads_l and args.output_suppressed_reads_r):
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
255 additional_opts += ' --ambiguous '
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
256
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
257 arguments.update( {'args': additional_opts, 'reads': reads} )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
258
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
259 # Final bismark command:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
260 cmd = cmd % arguments
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
261 print 'bismark_cmd:', cmd
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
262 #sys.stderr.write( cmd )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
263 #sys.exit(1)
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
264 # Run
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
265 try:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
266 tmp_out = tempfile.NamedTemporaryFile().name
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
267 tmp_stdout = open( tmp_out, 'wb' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
268 tmp_err = tempfile.NamedTemporaryFile().name
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
269 tmp_stderr = open( tmp_err, 'wb' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
270 proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stdout=tmp_stdout, stderr=tmp_stderr )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
271 returncode = proc.wait()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
272
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
273 if returncode != 0:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
274 tmp_stdout.close()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
275 tmp_stderr.close()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
276 # get stderr, allowing for case where it's very large
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
277 tmp_stderr = open( tmp_err, 'rb' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
278 stderr = ''
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
279 buffsize = 1048576
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
280 try:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
281 while True:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
282 stderr += tmp_stderr.read( buffsize )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
283 if not stderr or len( stderr ) % buffsize != 0:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
284 break
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
285 except OverflowError:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
286 pass
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
287
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
288 raise Exception, stderr
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
289 tmp_stdout.close()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
290 tmp_stderr.close()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
291
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
292 # TODO: look for errors in program output.
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
293 except Exception, e:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
294 stop_err( 'Error in bismark:\n' + str( e ) )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
295
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
296 # collect and copy output files
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
297 if args.output_report_file:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
298 output_report_file = open(args.output_report_file, 'w+')
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
299 for line in fileinput.input(glob( os.path.join( output_dir, '*report.txt') )):
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
300 output_report_file.write(line)
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
301 output_report_file.close()
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
302
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
303
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
304 if args.output_suppressed_reads:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
305 shutil.move( glob(os.path.join( output_dir, '*ambiguous_reads.txt'))[0], args.output_suppressed_reads )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
306 if args.output_suppressed_reads_l:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
307 shutil.move( glob(os.path.join( output_dir, '*ambiguous_reads_1.txt'))[0], args.output_suppressed_reads_l )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
308 if args.output_suppressed_reads_r:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
309 shutil.move( glob(os.path.join( output_dir, '*ambiguous_reads_2.txt'))[0], args.output_suppressed_reads_r )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
310
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
311 if args.output_unmapped_reads:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
312 shutil.move( glob(os.path.join( output_dir, '*unmapped_reads.txt'))[0], args.output_unmapped_reads )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
313 if args.output_unmapped_reads_l:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
314 shutil.move( glob(os.path.join( output_dir, '*unmapped_reads_1.txt'))[0], args.output_unmapped_reads_l )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
315 if args.output_unmapped_reads_r:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
316 shutil.move( glob(os.path.join( output_dir, '*unmapped_reads_2.txt'))[0], args.output_unmapped_reads_r )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
317
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
318 try:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
319 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
320 merge all bam files
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
321 """
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
322 #tmp_out = tempfile.NamedTemporaryFile( dir=output_dir ).name
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
323 tmp_stdout = open( tmp_out, 'wab' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
324 tmp_err = tempfile.NamedTemporaryFile( dir=output_dir ).name
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
325 tmp_stderr = open( tmp_err, 'wb' )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
326
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
327 tmp_res = tempfile.NamedTemporaryFile( dir= output_dir).name
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
328
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
329 bam_files = glob( os.path.join( output_dir, '*.bam') )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
330 if len( bam_files ) > 1:
3
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
331 cmd = 'samtools merge -@ %s -f %s %s ' % ( args.num_threads, tmp_res, ' '.join( bam_files ) )
0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
332
3
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
333 proc = subprocess.Popen( args=shlex.split( cmd ), stdout=subprocess.PIPE )
0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
334
3
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
335 returncode = proc.wait()
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
336 tmp_stdout.close()
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
337 tmp_stderr.close()
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
338 if returncode != 0:
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
339 raise Exception, open( tmp_stderr.name ).read()
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
340 else:
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
341 tmp_res = bam_files[0]
0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
342
3
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
343 bam_path = "%s" % tmp_res
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
344
0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
345 if os.path.exists( bam_path ):
3
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
346 if args.sort_bam:
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
347 cmd = 'samtools sort -@ %s %s %s' % (args.num_threads, bam_path, args.output)
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
348 else:
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
349 shutil.copy( bam_path, args.output )
0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
350 else:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
351 stop_err( 'BAM file no found:\n' + str( bam_path ) )
3
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
352
91f07ff056ca Uploaded
bgruening
parents: 2
diff changeset
353
0
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
354
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
355 # TODO: look for errors in program output.
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
356 except Exception, e:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
357 stop_err( 'Error in merging bam files:\n' + str( e ) )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
358
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
359
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
360 if args.output_stdout:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
361 # copy the temporary saved stdout from bismark
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
362 shutil.move( tmp_out, args.output_stdout )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
363
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
364 # Clean up temp dirs
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
365 if args.own_file:
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
366 if os.path.exists( tmp_index_dir ):
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
367 shutil.rmtree( tmp_index_dir )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
368 if os.path.exists( tmp_bismark_dir ):
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
369 shutil.rmtree( tmp_bismark_dir )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
370 if os.path.exists( output_dir ):
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
371 shutil.rmtree( output_dir )
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
372
62c6da72dd4a Uploaded
bgruening
parents:
diff changeset
373 if __name__=="__main__": __main__()