annotate bismark_wrapper.py @ 2:82814a8a2395 draft

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