-
Notifications
You must be signed in to change notification settings - Fork 3
/
RNAseq_annotate.py
executable file
·589 lines (569 loc) · 27.7 KB
/
RNAseq_annotate.py
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#!/usr/bin/env python3
import os
from os import path
from rnannot.parser import parse_args
from sys import argv, exit
from rnannot.utils import get_trimmomatic_jar_path, get_fastqc_path, get_trimmomatic_adapter_path, get_hisat2_command_path, get_bbmap_command_path, get_bbmap_adapter_path, get_gatk_jar_path, get_picard_jar_path, get_bam_to_bigwig_path, get_regtools_path
import subprocess
from zipfile import ZipFile
import gzip
import shutil
from itertools import islice
from six.moves import urllib
import datetime
import random
import glob
def run_pipeline(run, genome, outdir, layout, platform, model):
# create the output folder
output_prefix = path.join(outdir, run)
os.mkdir(output_prefix)
if platform == 'ABI_SOLID':
return (
False,
'Currently, the colorspace data from ABI_SOLID is not supported', '')
# download SRA files and convert SRA file to fastq file(s)
print('downloading sra files...')
sra_file_name = run
f_stdout = open(
path.join(output_prefix, sra_file_name + '.fastq-dump.log'), 'w')
f_stderr = open(
path.join(output_prefix, sra_file_name + '.fastq-dump.errlog'), 'w')
if layout == 'SINGLE':
subprocess.run(
[
'fastq-dump', '--dumpbase', '-O', output_prefix,
run
],
stdout=f_stdout,
stderr=f_stderr)
f_stdout.close()
f_stderr.close()
elif layout == 'PAIRED':
subprocess.run(
[
'fastq-dump', '--dumpbase', '--split-files', '-O', output_prefix,
run
],
stdout=f_stdout,
stderr=f_stderr)
f_stdout.close()
f_stderr.close()
# Check if the SRA file is correct or not first
if layout == 'PAIRED' and (not path.exists(
path.join(
output_prefix, sra_file_name + '_1.fastq')) or not path.exists(
path.join(output_prefix, sra_file_name + '_2.fastq'))):
return (
False,
"run {} doesn't have paired data. It's not processed.".format(run), '')
# Run FastQC first
# Then, use Trimmomatic to do trimming
# In the last step, perfom normalizing using bbnorm
fastqc_path = get_fastqc_path()
trimmomatic_jar_path = get_trimmomatic_jar_path()
if layout == 'SINGLE':
print('QC ...')
f_stdout = open(
path.join(output_prefix, sra_file_name + '.fastqc.log'), 'w')
f_stderr = open(
path.join(output_prefix, sra_file_name + '.fastqc.errlog'), 'w')
subprocess.run(
[
fastqc_path, '--outdir', output_prefix,
path.join(output_prefix, sra_file_name + '.fastq')
],
stdout=f_stdout,
stderr=f_stderr)
with ZipFile(
path.join(output_prefix, sra_file_name + '_fastqc.zip'),
'r') as zip_ref:
zip_ref.extractall(output_prefix)
f_stdout.close()
f_stderr.close()
# Trimming with trimmomatic
print('Trimming ...')
f_stdout = open(
path.join(output_prefix, sra_file_name + '.trimmomatic.log'), 'w')
f_stderr = open(
path.join(output_prefix, sra_file_name + '.trimmomatic.errlog'),
'w')
if platform == 'ILLUMINA' and (model.startswith('Illumina HiSeq')
or model.startswith('Illumina MiSeq')):
subprocess.run(
[
'java', '-jar', trimmomatic_jar_path, 'SE',
path.join(output_prefix, sra_file_name + '.fastq'),
path.join(output_prefix, 'output.fastq'), 'ILLUMINACLIP:' +
get_trimmomatic_adapter_path('TruSeq3-SE.fa') + ':2:30:10',
'LEADING:30', 'TRAILING:30', 'SLIDINGWINDOW:4:15',
'MINLEN:36', 'TOPHRED33'
],
stdout=f_stdout,
stderr=f_stderr)
elif platform == 'ILLUMINA' and model.startswith(
'Illumina Genome Analyzer II'):
subprocess.run(
[
'java', '-jar', trimmomatic_jar_path, 'SE',
path.join(output_prefix, sra_file_name + '.fastq'),
path.join(output_prefix, 'output.fastq'), 'ILLUMINACLIP:' +
get_trimmomatic_adapter_path('TruSeq2-SE.fa') + ':2:30:10',
'LEADING:30', 'TRAILING:30', 'SLIDINGWINDOW:4:15',
'MINLEN:36', 'TOPHRED33'
],
stdout=f_stdout,
stderr=f_stderr)
else:
# Use adapter file from BBMap for other platforms and models.
subprocess.run(
[
'java', '-jar', trimmomatic_jar_path, 'SE',
path.join(output_prefix, sra_file_name + '.fastq'),
path.join(output_prefix, 'output.fastq'),
'ILLUMINACLIP:' + get_bbmap_adapter_path() + ':2:30:10',
'LEADING:30', 'TRAILING:30', 'SLIDINGWINDOW:4:15',
'MINLEN:36', 'TOPHRED33'
],
stdout=f_stdout,
stderr=f_stderr)
f_stdout.close()
f_stderr.close()
# Normalizing
print('Normalizing ...')
subprocess.run([get_bbmap_command_path('bbnorm.sh'), 'in1=' + path.join(output_prefix, 'output.fastq'),
'out=' + path.join(output_prefix, 'normalized.fastq'), 'target=' + '100', 'threads=auto'])
return (True, '', 'single')
elif layout == 'PAIRED':
print('QC ...')
f_stdout = open(
path.join(output_prefix, sra_file_name + '_1.fastqc.log'), 'w')
f_stderr = open(
path.join(output_prefix, sra_file_name + '_1.fastqc.errlog'), 'w')
subprocess.run(
[
fastqc_path, '--outdir', output_prefix,
path.join(output_prefix, sra_file_name + '_1.fastq')
],
stdout=f_stdout,
stderr=f_stderr)
f_stdout.close()
f_stderr.close()
f_stdout = open(
path.join(output_prefix, sra_file_name + '_2.fastqc.log'), 'w')
f_stderr = open(
path.join(output_prefix, sra_file_name + '_2.fastqc.errlog'), 'w')
subprocess.run(
[
fastqc_path, '--outdir', output_prefix,
path.join(output_prefix, sra_file_name + '_2.fastq')
],
stdout=f_stdout,
stderr=f_stderr)
with ZipFile(
path.join(output_prefix, sra_file_name + '_1_fastqc.zip'),
'r') as zip_ref:
zip_ref.extractall(output_prefix)
with ZipFile(
path.join(output_prefix, sra_file_name + '_2_fastqc.zip'),
'r') as zip_ref:
zip_ref.extractall(output_prefix)
# Trimming with trimmomatic
print('Trimming ...')
f_stdout = open(
path.join(output_prefix, sra_file_name + '.trimmomatic.log'), 'w')
f_stderr = open(
path.join(output_prefix, sra_file_name + '.trimmomatic.errlog'),
'w')
if platform == 'ILLUMINA' and (model.startswith('Illumina HiSeq')
or model.startswith('Illumina MiSeq')):
subprocess.run(
[
'java', '-jar', trimmomatic_jar_path, 'PE',
path.join(output_prefix, sra_file_name + '_1.fastq'),
path.join(output_prefix, sra_file_name + '_2.fastq'),
path.join(output_prefix, 'output_1.fastq'),
path.join(output_prefix, 'output_1_un.fastq'),
path.join(output_prefix, 'output_2.fastq'),
path.join(output_prefix,
'output_2_un.fastq'), 'ILLUMINACLIP:' +
get_trimmomatic_adapter_path('TruSeq3-PE.fa') + ':2:30:10',
'LEADING:30', 'TRAILING:30', 'SLIDINGWINDOW:4:15',
'MINLEN:36', 'TOPHRED33'
],
stdout=f_stdout,
stderr=f_stderr)
elif platform == 'ILLUMINA' and model.startswith(
'Illumina Genome Analyzer II'):
subprocess.run(
[
'java', '-jar', trimmomatic_jar_path, 'PE',
path.join(output_prefix, sra_file_name + '_1.fastq'),
path.join(output_prefix, sra_file_name + '_2.fastq'),
path.join(output_prefix, 'output_1.fastq'),
path.join(output_prefix, 'output_1_un.fastq'),
path.join(output_prefix, 'output_2.fastq'),
path.join(output_prefix,
'output_2_un.fastq'), 'ILLUMINACLIP:' +
get_trimmomatic_adapter_path('TruSeq2-PE.fa') + ':2:30:10',
'LEADING:30', 'TRAILING:30', 'SLIDINGWINDOW:4:15',
'MINLEN:36', 'TOPHRED33'
],
stdout=f_stdout,
stderr=f_stderr)
else:
# use BBTool (BBMerge) to determine the adapter first, then run the Trimmomatic
f_bbmap_stdout = open(
path.join(output_prefix, sra_file_name + '.bbmap.log'), 'w')
f_bbmap_stderr = open(
path.join(output_prefix, sra_file_name + '.bbmap.errlog'), 'w')
subprocess.run(
[
get_bbmap_command_path('bbmerge.sh'), 'in1=' + path.join(
output_prefix, sra_file_name + '_1.fastq'), 'in2=' +
path.join(output_prefix, sra_file_name + '_2.fastq'),
'outa=' + path.join(output_prefix, 'adapters.fa')
],
stdout=f_bbmap_stdout,
stderr=f_bbmap_stderr)
f_bbmap_stdout.close()
f_bbmap_stderr.close()
subprocess.run(
[
'java', '-jar', trimmomatic_jar_path, 'PE',
path.join(output_prefix, sra_file_name + '_1.fastq'),
path.join(output_prefix, sra_file_name + '_2.fastq'),
path.join(output_prefix, 'output_1.fastq'),
path.join(output_prefix, 'output_1_un.fastq'),
path.join(output_prefix, 'output_2.fastq'),
path.join(output_prefix, 'output_2_un.fastq'),
'ILLUMINACLIP:' + path.join(output_prefix, 'adapters.fa') +
':2:30:10', 'LEADING:30', 'TRAILING:30',
'SLIDINGWINDOW:4:15', 'MINLEN:36', 'TOPHRED33'
],
stdout=f_stdout,
stderr=f_stderr)
f_stdout.close()
f_stderr.close()
# Normalizing
print('Normalizing ...')
subprocess.run([get_bbmap_command_path('bbnorm.sh'), 'in1=' + path.join(output_prefix,'output_1.fastq'), 'in2=' + path.join(output_prefix, 'output_2.fastq'),
'out1=' + path.join(output_prefix, 'normalized_1.fastq'), 'out2=' + path.join(output_prefix, 'normalized_2.fastq'), 'target=' + '100', 'threads=auto'])
return (True, '', 'paired')
def merge_files(files, outdir): # merge sam files
print('Combing the sam/bam files ...')
f_stdout = open(path.join(outdir, 'out.log'), 'a')
f_stderr = open(path.join(outdir, 'out.errlog'), 'a')
args = [
'java', '-jar',
get_picard_jar_path(), 'MergeSamFiles',
'O=' + path.join(outdir, 'output.bam')
]
args += ['I=' + f for f in files]
subprocess.run(args, stdout=f_stdout, stderr=f_stderr)
print('Finished combining the sam/bam files')
def check_ref_files(ref_path):
if path.exists(ref_path + '.fai') and path.exists('.dict'):
return True
else:
return False
def read_sam_errors(file_path):
warns = set()
errors = set()
with open(file_path) as f:
for line in islice(f, 4, None):
temp = line.split('\t')[0]
if 'ERROR' in temp:
errors.add(temp.lstrip('ERROR:'))
elif 'WARNING' in temp:
warns.add(temp.lstrip('WARNING:'))
elif temp == '\n':
break
return (errors, warns)
if __name__ == '__main__':
print("new")
# parse the arguments, exclude the script name
args = parse_args(argv[1:])
# convert many arguments to absolute path
if not path.isabs(args.outdir):
args.outdir = path.abspath(args.outdir)
if not path.isabs(args.input):
args.input = path.abspath(args.input)
if not path.isabs(args.genome):
args.genome = path.abspath(args.genome)
os.mkdir(path.join(args.outdir, args.name))
# Decompress the gz file, becasue some of tools don't accept .gz compressed files
genome = args.genome
if genome.endswith('.gz'):
new_genome_file_name = path.join(args.outdir, args.name,
path.basename(genome).rstrip('.gz'))
with gzip.open(genome, 'rb') as f_in:
with open(new_genome_file_name, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
genome = new_genome_file_name
genome_file_name = path.basename(genome)
with open(args.input) as f:
col_names = f.readline().rstrip('\n').split('\t')
run_ind = col_names.index('Run')
platform_ind = col_names.index('Platform')
model_ind = col_names.index('Model')
layout_ind = col_names.index('LibraryLayout')
scientific_name_ind = col_names.index('ScientificName')
print('Checking the input tsv file: {}'.format(args.input))
for ind, name in zip([run_ind, platform_ind, model_ind, layout_ind, scientific_name_ind],
['Run', 'Platform', 'Model', 'LibraryLayout', 'ScientificName']):
if ind == -1:
print('{} column is missing in input tsv file.'.format(name))
exit(1)
runs = []
platforms = []
models = []
layouts = []
scientific_names = []
for line in f:
temp = line.rstrip('\n').split('\t')
runs.append(temp[run_ind])
platforms.append(temp[platform_ind])
models.append(temp[model_ind])
layouts.append(temp[layout_ind])
scientific_names.append(temp[scientific_name_ind])
# check the amount of sra files in tsv
if len(runs) > args.MaximumSRA:
print('The amount of sra files is more than {}'.format(args.MaximumSRA))
print('Randomly pick {} sra files for downloading'.format(args.MaximumSRA))
runs_temp = []
platforms_temp = []
models_temp = []
layouts_temp = []
scientific_names_temp = []
random_sra = random.sample(range(0,len(runs)), args.MaximumSRA)
# randomly pick the maximum amount of sra files to download
for i in random_sra:
runs_temp.append(runs[i])
platforms_temp.append(platforms[i])
models_temp.append(models[i])
layouts_temp.append(layouts[i])
scientific_names_temp.append(scientific_names[i])
runs = runs_temp
platforms = platforms_temp
models = models_temp
layouts = layouts_temp
scientific_names = scientific_names_temp
# output runs/scientific_name/assembly_name to Source.txt
date = datetime.datetime.now().strftime("%Y-%m-%d")
with open(path.join(args.outdir, args.name, 'Source.txt'), 'w') as outfile:
outfile.write(scientific_names[0] + '\n')
outfile.write(args.assembly + '\n')
outfile.write(date + '\n')
for run in runs:
outfile.write(run + '\n')
single_files_for_merge = []
paired1_files_for_merge = []
paired2_files_for_merge = []
for run, platform, model, layout in zip(runs, platforms, models, layouts):
print('Processing the file: {}'.format(run))
return_status, err_message, return_layout = run_pipeline(
run=run,
genome=genome,
outdir=path.join(args.outdir, args.name),
layout=layout,
platform=platform,
model=model,
)
if return_status:
if return_layout == 'single':
single_files_for_merge.append(
path.join(args.outdir, args.name, run, 'normalized.fastq'))
if return_layout == 'paired':
paired1_files_for_merge.append(
path.join(args.outdir, args.name, run, 'normalized_1.fastq'))
paired2_files_for_merge.append(
path.join(args.outdir, args.name, run, 'normalized_2.fastq'))
else:
print(err_message)
# merge normalized fastq files together
if len(single_files_for_merge) > 0:
with open(path.join(args.outdir, args.name, 'merged_normalized.fastq'), 'w') as outfile:
for fname in single_files_for_merge:
with open(fname, 'r') as infile:
for line in infile:
outfile.write(line)
if len(paired1_files_for_merge) > 0 and len(paired2_files_for_merge) > 0:
with open(path.join(args.outdir, args.name, 'merged_normalized_1.fastq'), 'w') as outfile:
for fname in paired1_files_for_merge:
with open(fname, 'r') as infile:
for line in infile:
outfile.write(line)
with open(path.join(args.outdir, args.name, 'merged_normalized_2.fastq'), 'w') as outfile:
for fname in paired2_files_for_merge:
with open(fname, 'r') as infile:
for line in infile:
outfile.write(line)
# Aligning SINGLE fastq with HISAT2
file_for_aligned = path.join(args.outdir, args.name, 'merged_normalized.fastq')
if path.exists(file_for_aligned):
print('Aligning single file...')
f_stdout = open(
path.join(args.outdir, args.name, genome_file_name + '_single.hisat2.log'), 'w')
f_stderr = open(
path.join(args.outdir, args.name, genome_file_name + '_single.hisat2.errlog'), 'w')
subprocess.run(
[
get_hisat2_command_path('hisat2-build'), genome,
path.join(args.outdir, args.name, genome_file_name)
],
stdout=f_stdout,
stderr=f_stderr)
subprocess.run(
[
get_hisat2_command_path('hisat2'), '--dta-cufflinks', '--no-mixed', '--no-discordant', '-p', args.threads_num, '-x',
path.join(args.outdir, args.name, genome_file_name), '-U',
path.join(args.outdir, args.name, 'merged_normalized.fastq'), '-S',
path.join(args.outdir, args.name, 'single_output.sam')
],
stdout=f_stdout,
stderr=f_stderr)
f_stdout.close()
f_stderr.close()
# Aligning PAIRED fastq with HISAT2
file_for_aligned_1 = path.join(args.outdir, args.name, 'merged_normalized_1.fastq')
file_for_aligned_2 = path.join(args.outdir, args.name, 'merged_normalized_2.fastq')
if path.exists(file_for_aligned_1) and path.exists(file_for_aligned_2):
print('Aligning paired file...')
f_stdout = open(
path.join(args.outdir, args.name, genome_file_name + '_paired.hisat2-build.log'), 'w')
f_stderr = open(
path.join(args.outdir, args.name, genome_file_name + '_paired.hisat2-build.errlog'), 'w')
subprocess.run(
[
get_hisat2_command_path('hisat2-build'), genome,
path.join(args.outdir, args.name, genome_file_name)
],
stdout=f_stdout,
stderr=f_stderr)
f_stdout.close()
f_stderr.close()
f_stdout = open(
path.join(args.outdir, args.name, genome_file_name + '_paired.hisat2.log'), 'w')
f_stderr = open(
path.join(args.outdir, args.name, genome_file_name + '_paired.hisat2.errlog'), 'w')
subprocess.run(
[
get_hisat2_command_path('hisat2'), '--dta-cufflinks', '--no-mixed', '--no-discordant', '-p', args.threads_num, '-x',
path.join(args.outdir, args.name, genome_file_name), '-1',
path.join(args.outdir, args.name, 'merged_normalized_1.fastq'), '-2',
path.join(args.outdir, args.name, 'merged_normalized_2.fastq'), '-S',
path.join(args.outdir, args.name, 'paired_output.sam')
],
stdout=f_stdout,
stderr=f_stderr)
f_stdout.close()
f_stderr.close()
# sort and convert to the bam file-single
if path.exists(path.join(args.outdir, args.name, 'single_output.sam')):
f_stdout = open(
path.join(args.outdir, args.name, genome_file_name + '_single.samtools.log'), 'w')
f_stderr = open(
path.join(args.outdir, args.name, genome_file_name + '_single.samtools.errlog'), 'w')
subprocess.run(
[
'samtools', 'sort', '-@', args.threads_num, '-o',
path.join(args.outdir, args.name, 'single_output.bam'), '-O', 'bam', '-T',
path.join(args.outdir, args.name, 'single_output'),
path.join(args.outdir, args.name, 'single_output.sam')
],
stdout=f_stdout,
stderr=f_stderr)
f_stdout.close()
f_stderr.close()
# sort and convert to the bam file-paired
if path.exists(path.join(args.outdir, args.name, 'paired_output.sam')):
f_stdout = open(
path.join(args.outdir, args.name, genome_file_name + '_paired.samtools.log'), 'w')
f_stderr = open(
path.join(args.outdir, args.name, genome_file_name + '_paired.samtools.errlog'), 'w')
subprocess.run(
[
'samtools', 'sort', '-@', args.threads_num, '-o',
path.join(args.outdir, args.name, 'paired_output.bam'), '-O', 'bam', '-T',
path.join(args.outdir, args.name, 'paired_output'),
path.join(args.outdir, args.name, 'paired_output.sam')
],
stdout=f_stdout,
stderr=f_stderr)
f_stdout.close()
f_stderr.close()
# combine the sam/bam files together and convert to BAM file
if path.exists(path.join(args.outdir, args.name, 'single_output.bam')) and path.exists(path.join(args.outdir, args.name, 'paired_output.bam')):
print('Start merging single and paired output files...')
files_for_merge = [path.join(args.outdir, args.name, 'single_output.bam'), path.join(args.outdir, args.name, 'paired_output.bam')]
merge_files(files_for_merge, path.join(args.outdir, args.name))
elif path.exists(path.join(args.outdir, args.name, 'single_output.bam')):
print('The layout of all files is SINGLE, skip merging process...')
os.rename(path.join(args.outdir, args.name, 'single_output.bam'), path.join(args.outdir, args.name, 'output.bam'))
elif path.exists(path.join(args.outdir, args.name, 'paired_output.bam')):
print('The layout of all files is PAIRED, skip merging process...')
os.rename(path.join(args.outdir, args.name, 'paired_output.bam'), path.join(args.outdir, args.name, 'output.bam'))
# sorting bam file
print('Start sorting bam file...')
bam_dir = path.join(args.outdir, args.name, 'output.bam')
output_dir = path.join(args.outdir, args.name, 'output.sorted.bam')
subprocess.run(['samtools', 'sort', '-@', args.threads_num, bam_dir, '-o', output_dir])
subprocess.run(['samtools', 'index', '-c@', args.threads_num, output_dir]) #Creating a .csi index immediately so that bam_to_bigwig.py doesn't try to make a .bai index
# converting dowsampled bam to bigwig (includes indexing bam file)
print('Generating bigwig file from bam file...')
bam_dir = path.join(args.outdir, args.name, 'output.sorted.bam')
bigwig_dir = path.join(args.outdir, args.name, 'output.bigwig')
subprocess.run(['python3', get_bam_to_bigwig_path(), bam_dir, '-o', bigwig_dir])
# rename bam and bigwig file to [gggsss]_[assembly_name]_RNA-Seq-alignments_[datetime]
temp = scientific_names[0].split(" ")
gene_name = temp[0]
species_name = temp[1]
if not args.Run_prefix:
new_name = gene_name[0:3] + species_name[0:3] + '_' + args.assembly + '_RNA-Seq-alignments_' + date
if args.Run_prefix:
new_name = runs[0]
os.rename(path.join(args.outdir, args.name, 'output.sorted.bam'), path.join(args.outdir, args.name, new_name + '.bam'))
os.rename(path.join(args.outdir, args.name, 'output.sorted.bam.csi'), path.join(args.outdir, args.name, new_name + '.bam.csi')) #Changed from .bai to .csi
os.rename(path.join(args.outdir, args.name, 'output.bigwig'), path.join(args.outdir, args.name, new_name + '.bigwig'))
# generate bed file
subprocess.run([get_regtools_path(), 'junctions', 'extract', '-m', '20', '-s', 'XS', '-o', path.join(args.outdir, args.name, new_name + '.bed'), path.join(args.outdir, args.name, new_name + '.bam')])
# remove intermediate files
if not args.tempFile:
os.remove(path.join(args.outdir, args.name, 'output.bam'))
if path.exists(path.join(args.outdir, args.name, 'single_output.sam')):
os.remove(path.join(args.outdir, args.name, 'single_output.sam'))
if path.exists(path.join(args.outdir, args.name, 'single_output.bam')):
os.remove(path.join(args.outdir, args.name, 'single_output.bam'))
if path.exists(path.join(args.outdir, args.name, 'paired_output.sam')):
os.remove(path.join(args.outdir, args.name, 'paired_output.sam'))
if path.exists(path.join(args.outdir, args.name, 'paired_output.bam')):
os.remove(path.join(args.outdir, args.name, 'paired_output.bam'))
if path.exists(path.join(args.outdir, args.name,'merged_normalized_1.fastq')):
os.remove(path.join(args.outdir, args.name,'merged_normalized_1.fastq'))
os.remove(path.join(args.outdir, args.name,'merged_normalized_2.fastq'))
if path.exists(path.join(args.outdir, args.name,'merged_normalized.fastq')):
os.remove(path.join(args.outdir, args.name,'merged_normalized.fastq'))
hs2_filelist = glob.glob(path.join(args.outdir, args.name, genome_file_name + '.*'))
for hs2_file in hs2_filelist:
os.remove(hs2_file)
# remove fastq files in SRR subdirectories
for run in runs:
if path.exists(path.join(args.outdir, args.name, run, 'output.fastq')):
os.remove(path.join(args.outdir, args.name, run, 'output.fastq'))
os.remove(path.join(args.outdir, args.name, run, 'normalized.fastq'))
if path.exists(path.join(args.outdir, args.name, run, 'output_1.fastq')):
os.remove(path.join(args.outdir, args.name, run, 'output_1.fastq'))
os.remove(path.join(args.outdir, args.name, run, 'output_1_un.fastq'))
os.remove(path.join(args.outdir, args.name, run, 'normalized_1.fastq'))
os.remove(path.join(args.outdir, args.name, run, 'output_2.fastq'))
os.remove(path.join(args.outdir, args.name, run, 'output_2_un.fastq'))
os.remove(path.join(args.outdir, args.name, run, 'normalized_2.fastq'))
if path.exists(path.join(args.outdir, args.name, run, run + '_1.fastq')):
os.remove(path.join(args.outdir, args.name, run, run + '_1.fastq'))
os.remove(path.join(args.outdir, args.name, run, run + '_1_fastqc.zip'))
shutil.rmtree(path.join(args.outdir, args.name, run, run + '_1_fastqc'))
if path.exists(path.join(args.outdir, args.name, run, run + '_2.fastq')):
os.remove(path.join(args.outdir, args.name, run, run + '_2.fastq'))
os.remove(path.join(args.outdir, args.name, run, run + '_2_fastqc.zip'))
shutil.rmtree(path.join(args.outdir, args.name, run, run + '_2_fastqc'))
print('Finished processing')