-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpherical.py
More file actions
409 lines (301 loc) · 13.3 KB
/
Spherical.py
File metadata and controls
409 lines (301 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python
from __future__ import division
from subprocess import call
import HTSeq
import random
import argparse
import numpy as np
import subprocess
import os
import sys
from datetime import datetime
parser = argparse.ArgumentParser() #simplifys the wording of using argparse as stated in the python tutorial
# Basic input output files
parser.add_argument("-i", type=str, action='store', dest='input', help="Input the FASTA/Q file") # allows input of the forward read
parser.add_argument("-o", type=str, action='store', dest='output', help="Name for the output files") # allows input of the forward read
# Input file type
parser.add_argument('-fasta', action='store_true', default=False, dest='fasta_switch', help='Input is fasta')
parser.add_argument('-fastq', action='store_true', default=False, dest='fastq_switch', help='Input is fastq')
# Limit assembly or not
parser.add_argument('-limit', action='store_true', default=False, dest='limit_switch', help='Limit velvet to produce contigs large than 300bp.')
# Choose assembler
parser.add_argument('-velvet', action='store_true', default=False, dest='velvet_switch', help='Choose assembler Velvet')
parser.add_argument('-abyss', action='store_true', default=False, dest='abyss_switch', help='Choose assembler ABYSS')
parser.add_argument('-megahit', action='store_true', default=False, dest='megahit_switch', help='Choose assembler Megahit')
# Choose aligner
parser.add_argument('-bwa', action='store_true', default=False, dest='bwa_switch', help='Choose aligner BWA')
parser.add_argument('-bowtie2', action='store_true', default=False, dest='bowtie_switch', help='Choose aligner Bowtie2')
# More indepth choices
parser.add_argument('-align', action='store', default= '70', dest='alignmentrate', help='The alignment rate wanted before ending, default is 70%.')
parser.add_argument('-iter', action='store', default= '5', dest='iterations', help='Number of iterations before ending, default is 5.')
parser.add_argument('-m', action='store_true', default=True, dest='merge_switch', help='Merges all contig files into a singluar assembly, default is true.')
parser.add_argument('-k', action='store', default= '31', dest='kmer', help='Enter Kmer size of choice, default is 31.')
parser.add_argument('-R', action='store', dest='RAM', help='Enter percentage of file to be used as sub-sample e.g. if -R 0.25 is used 25% of the reads will be used in the sub-sample, no default')
parser.add_argument("-x1", type=str, action='store',default= '', dest='extra1', help="Allows additional options for assembly to be used in Velveth or ABYSS steps, used for options starting with -")
parser.add_argument("-x2", type=str, action='store',default= '', dest='extra2', help="Allows additional options for assembly to be used in Velveth or ABYSS steps, used for options starting with --")
parser.add_argument("-u", type=str, action='store',default= ' ', dest='bowtie_extra', help="Allows additional options for alignment to be used in Bowtie2")
parser.add_argument('-f', action='store_true', default=False, dest='scaffold_switch', help='Conducts a final assembly of the produced contigs.')
args = parser.parse_args()
# Place each of the input into a simple variable to call
INPUT = str(args.input)
OUTPUT = str(args.output)
EXTRA1 = str(args.extra1)
EXTRA2 = str(args.extra2)
BOWTIE_EXTRA = str(args.bowtie_extra)
iterations = int(args.iterations)
alignmentwanted = int(args.alignmentrate)
ksize = str(args.kmer)
RAM = float(args.RAM)
EXTRA = ''
temp = ''
if EXTRA1 != '':
temp = EXTRA + '-' + EXTRA1
if EXTRA2 != '':
EXTRA = temp + ' --' + EXTRA2
scaffold_switch = False
if args.scaffold_switch == True:
scaffold_switch = True
limit = False
if args.limit_switch == True:
limit = True
# Provide an initial count of the raw reads so we can work out the alignment rate
totalreads = 0
currentiter = 0
filetype = 'fasta'
currentalignrate = 0
if args.fasta_switch == True:
newinput = INPUT
filetype = 'fasta'
if args.fastq_switch == True:
inputfile = HTSeq.FastqReader( INPUT, "phred")
filetype = 'fasta'
fastafile = open('Converted_input.fa','w')
for read in inputfile:
fastafile.write( ">" + read.name + '\n')
fastafile.write(read.seq + '\n')
fastafile.close()
newinput = 'Converted_input.fa'
failed = False
failedfirst = False
unalignedfile = newinput
currentfile = newinput
print "Using file;" , currentfile
sys.stdout.flush()
# Provide an initial count of the raw reads so we can work out the alignment rate
totalreads = 0
for read in HTSeq.FastaReader(currentfile):
totalreads += 1
currentiter = 0
unalignedfile = currentfile
RAM2 = RAM*100
while currentiter < iterations:
if currentalignrate >= alignmentwanted:
break
else:
currentiter +=1
print "Starting sub-sampling; " + str(datetime.now())
sys.stdout.flush()
# Subsample from current file
subsample = open('subsample.fa','w')
currentfasta = HTSeq.FastaReader(unalignedfile)
#print RAM
for read in currentfasta:
random_num = random.randint(0,100)
if RAM2 >= random_num:
subsample.write('>' + read.name + '\n')
subsample.write(read.seq + '\n')
else:
continue
subsample.close()
currentfile = 'subsample.fa'
print "Subsampling completed."
sys.stdout.flush()
print "Ending sub-sampling; " + str(datetime.now())
sys.stdout.flush()
# Run assembly
print "Starting Assembly; " + str(datetime.now())
if args.velvet_switch == True:
bashCommand = 'velveth out-dir ' + str(ksize) + ' -' + filetype + ' ' + currentfile + ' ' + EXTRA + ' | cat > Assembly_log'
notneeded = call(bashCommand, shell=True)
if limit == True:
bashCommand = 'velvetg out-dir -exp_cov auto -min_contig_lgth 300 | cat > Assembly_log'
else:
bashCommand = 'velvetg out-dir -exp_cov auto | cat > Assembly_log'
notneeded = call(bashCommand, shell=True)
# Run velvet code
elif args.abyss_switch == True:
# Run ABYSS code
bashCommand = 'ABYSS -k' + str(ksize) + ' ' + currentfile + ' -o temp_contigs.fa ' + ' ' + EXTRA + ' | cat > Assembly_log'
notneeded = call(bashCommand, shell=True)
elif args.megahit_switch == True:
# Run Megahit code
contigfilename = OUTPUT + '.' + str(currentiter)
bashCommand = 'megahit ' + ' -r ' + currentfile + ' -o Iteration_' + str(currentiter) + ' ' + EXTRA + ' | cat > Assembly_log'
notneeded = call(bashCommand, shell=True)
bashCommand = 'cp Iteration_' + str(currentiter) + '/final.contigs.fa ' + contigfilename
notneeded = call(bashCommand, shell=True)
print "Assembly complete."
sys.stdout.flush()
print "Ending Assembly; " + str(datetime.now())
sys.stdout.flush()
# Check if assembly produced anything
# Run alignment
print "Starting Alignment; " + str(datetime.now())
sys.stdout.flush()
if args.bowtie_switch == True:
contigfilename = OUTPUT + '.' + str(currentiter)
if args.velvet_switch == True:
bashCommand = 'mv out-dir/contigs.fa ' + contigfilename # Move file and rename so it isnt deleted
notneeded = call(bashCommand, shell=True)
if os.stat(contigfilename).st_size == 0:
print "Assembly failed to produce any contigs."
sys.stdout.flush()
print "Spherical will now exit."
sys.stdout.flush()
failed = True
bashCommand = 'rm ' + contigfilename
notneeded = call(bashCommand, shell=True)
if currentiter == 1:
failedfirst = True
sys.exit()
elif args.abyss_switch == True:
bashCommand = 'mv temp_contigs.fa ' + contigfilename # Move file and rename so it isnt deleted
notneeded = call(bashCommand, shell=True)
if os.stat(contigfilename).st_size == 0:
print "Assembly failed to produce any contigs."
sys.stdout.flush()
print "Spherical will now exit."
sys.stdout.flush()
failed = True
bashCommand = 'rm ' + contigfilename
notneeded = call(bashCommand, shell=True)
if currentiter == 1:
failedfirst = True
sys.exit()
bashCommand = 'bowtie2-build -f ' + contigfilename + ' ' + 'Current_round_index | cat > Index_log ' # Creates the bowtie index
notneeded = call(bashCommand, shell=True)
bashCommand = 'bowtie2 -f -N 1 --un Unaligned.fa.' + str(currentiter) + ' -U ' + unalignedfile + ' ' + BOWTIE_EXTRA + ' --al /dev/null -x Current_round_index -S /dev/null | cat > Alignment_log ' # Runs bowtie itself
notneeded = call(bashCommand, shell=True)
unalignedfile = 'Unaligned.fa.' + str(currentiter)
# Run bowtie code
elif args.bwa_switch == True:
# Run BWA code
print "Not yet available"
print "Alignment complete."
sys.stdout.flush()
print "Ending Alignment; " + str(datetime.now())
sys.stdout.flush()
# Calculate current alignment rate
unalignedreads = 0
for reaf in HTSeq.FastaReader(unalignedfile):
unalignedreads +=1
print 'Unaligned reads; ', unalignedreads
sys.stdout.flush()
alignmentrate = (totalreads - unalignedreads) / totalreads * 100
currentalignrate = alignmentrate
print 'Percentage alignment; ', alignmentrate
sys.stdout.flush()
# Check if failed before output could be made or not
if failedfirst == False:
# Merge output files
if args.merge_switch == True:
# Run code to merge all the contig files from each assembly into a single file where all reads have been renamed.
bashCommand = 'cat ' + OUTPUT + '* > ' + OUTPUT + '.combined.fa'
notneeded = call(bashCommand, shell=True)
cfile = OUTPUT + '.combined.fa'
currentfile = HTSeq.FastaReader(cfile)
listolengths = []
for read in currentfile:
listolengths.append(len(read.seq))
print "Statistics for merged file."
print 'Mean contig length; ', np.mean(listolengths)
print '25 percentile; ' , np.percentile(listolengths,25)
print '75 percentile; ', np.percentile(listolengths,75)
print 'Standard deviation; ', np.std(listolengths)
print "Final alignment rate; ", alignmentrate
print '\n'
sys.stdout.flush()
if args.scaffold_switch == True:
print "Starting Scaffold step; " + str(datetime.now())
if args.velvet_switch == True:
bashCommand = 'velveth out-dir ' + str(ksize) + ' -' + filetype + ' ' + '*.combined.fa' + ' ' + EXTRA + ' | cat > Assembly_log'
notneeded = call(bashCommand, shell=True)
if limit == True:
bashCommand = 'velvetg out-dir -exp_cov auto -min_contig_lgth 300 | cat > Assembly_log'
else:
bashCommand = 'velvetg out-dir -exp_cov auto | cat > Assembly_log'
notneeded = call(bashCommand, shell=True)
# Run velvet code
elif args.abyss_switch == True:
# Run ABYSS code
bashCommand = 'ABYSS -k' + str(ksize) + ' ' + currentfile + ' -o temp_contigs.fa ' + ' ' + EXTRA + ' | cat > Assembly_log'
notneeded = call(bashCommand, shell=True)
elif args.megahit_switch == True:
print "Scaffoling is not avaialble for Megahit"
print "Scaffold complete."
sys.stdout.flush()
print "Ending Scaffold; " + str(datetime.now())
sys.stdout.flush()
# Run alignment
print "Starting Alignment; " + str(datetime.now())
sys.stdout.flush()
if args.bowtie_switch == True:
contigfilename = OUTPUT + '.' + 'scaffold.fa'
if args.velvet_switch == True:
bashCommand = 'mv out-dir/contigs.fa ' + contigfilename # Move file and rename so it isnt deleted
notneeded = call(bashCommand, shell=True)
if os.stat(contigfilename).st_size == 0:
print "Scaffold failed to produce any contigs."
sys.stdout.flush()
print "Spherical will now exit."
sys.stdout.flush()
failed = True
bashCommand = 'rm ' + contigfilename
notneeded = call(bashCommand, shell=True)
if currentiter == 1:
failedfirst = True
sys.exit()
elif args.abyss_switch == True:
bashCommand = 'mv temp_contigs.fa ' + contigfilename # Move file and rename so it isnt deleted
notneeded = call(bashCommand, shell=True)
if os.stat(contigfilename).st_size == 0:
print "Assembly failed to produce any contigs."
sys.stdout.flush()
print "Spherical will now exit."
sys.stdout.flush()
failed = True
bashCommand = 'rm ' + contigfilename
notneeded = call(bashCommand, shell=True)
if currentiter == 1:
failedfirst = True
sys.exit()
elif args.abyss_switch == True:
print "Scaffolding not available, no alignment conducted. Program will now likely crash due to bad programming. Sorry."
bashCommand = 'bowtie2-build -f ' + contigfilename + ' ' + 'Current_round_index | cat > Index_log ' # Creates the bowtie index
notneeded = call(bashCommand, shell=True)
bashCommand = 'bowtie2 -f -N 1 --un Non_scaffolded_contigs.fa ' + BOWTIE_EXTRA + ' --al Scaffolded_contigs.fa -x Current_round_index -S /dev/null | cat > Alignment_log ' # Runs bowtie itself
notneeded = call(bashCommand, shell=True)
print "Ending Scaffold alignment; " + str(datetime.now())
sys.stdout.flush()
# Provide final statistics on every round
if failed == True:
currentiter = currentiter - 1
for cround in range(0,currentiter):
cround +=1
cfile = OUTPUT + '.' + str(cround)
currentfile = HTSeq.FastaReader(cfile)
print "For iteration ; ", cround
listolengths = []
for read in currentfile:
listolengths.append(len(read.seq))
print "Statistics for merged file."
print 'Mean contig length; ', np.mean(listolengths)
print '25 percentile; ' , np.percentile(listolengths,25)
print '75 percentile; ', np.percentile(listolengths,75)
print 'Standard deviation; ', np.std(listolengths)
print '\n'
sys.stdout.flush()
else:
print "Spherical failed with no successful assembly produced."
print "Please try again with a different kmer size."
sys.stdout.flush()