-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdogmap.py
More file actions
869 lines (745 loc) · 31.8 KB
/
dogmap.py
File metadata and controls
869 lines (745 loc) · 31.8 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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# functions for mapping Illumina WGS data
# goes from fastq.gz to CRAM + GVCF
import sys
import subprocess
import os
import argparse
import time
import socket
import shutil
###############################################################################
def read_fasta_file_to_dict(fastaFile):
myDict = {}
inFile = open(fastaFile,'r')
line = inFile.readline()
line = line.rstrip()
if line[0] != '>':
print('ERROR, FILE DOESNNOT START WITH >')
sys.exit()
myName = line[1:].split()[0] # ignore other info
myDict[myName] = {}
myDict[myName]['seq'] = ''
myDict[myName]['seqLen'] = 0
mySeq = ''
while True:
line = inFile.readline()
if line == '':
myDict[myName]['seq'] = mySeq
myDict[myName]['seqLen'] = len(myDict[myName]['seq'])
break
line = line.rstrip()
if line[0] == '>':
myDict[myName]['seq'] = mySeq
myDict[myName]['seqLen'] = len(myDict[myName]['seq'])
myName = line[1:]
myDict[myName] = {}
myDict[myName]['seq'] = ''
myDict[myName]['seqLen'] = 0
mySeq = ''
continue
mySeq += line
inFile.close()
return myDict
###############################################################################
# Helper function to run commands, handle return values and print to log file
def runCMD(cmd):
val = subprocess.Popen(cmd, shell=True).wait()
if val == 0:
pass
else:
print('command failed')
print(cmd)
sys.exit(1)
###############################################################################
# Helper function to run commands, handle return values and print to log file
def runCMD_output(cmd):
val = subprocess.Popen(cmd, text=True, shell=True, stdout = subprocess.PIPE)
resLines = []
for i in val.stdout:
i = i.rstrip()
resLines.append(i)
return resLines
#############################################################################
# Helper function to read in information from genome .fai file and return
# a dictionary containing chrom names and lengths
def read_chrom_len(faiFileName):
chromLens = {}
inFile = open(faiFileName,'r')
for line in inFile:
line = line.rstrip()
line = line.split()
chromLens[line[0]] = int(line[1])
inFile.close()
return chromLens
#############################################################################
# setup paths to default programs to use and checks for required programs
def check_prog_paths(myData):
myData['logFile'].write('\nChecking for required programs...\n')
for p in ['bwa-mem2','gatk','samtools','parallel']:
if shutil.which(p) is None:
s = p + ' not found in path! please fix (module load?)'
print(s, flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].close()
sys.exit()
else:
myData['logFile'].write('%s\t%s\n' % (p,shutil.which(p)))
myData['logFile'].flush()
#############################################################################
def init_log(myData):
k = list(myData.keys())
k.sort()
myData['startTime'] = time.localtime()
myData['tStart'] = time.time()
t = time.strftime("%a, %d %b %Y %H:%M:%S", myData['startTime'])
myData['logFile'].write(t + '\n')
hn = socket.gethostname()
myData['logFile'].write('Host name: %s\n' % hn)
print('Host name: %s\n' % hn,flush=True)
myData['logFile'].write('\nInput options:\n')
for i in k:
if i in ['logFile']:
continue
myData['logFile'].write('%s\t%s\n' % (i,myData[i]))
myData['logFile'].flush()
#############################################################################
def setup_sample_table(myData):
# read in sample info
# table is sampleName ReadGroupID LibraryName fastq1 fastq2
myData['sampleTableList'] = []
inFile = open(myData['sampleTable'],'r')
for line in inFile:
line = line.rstrip()
line = line.split()
myData['sampleTableList'].append(line)
inFile.close()
s = 'read in %i lines from %s' % (len(myData['sampleTableList']),myData['sampleTable'])
print(s,flush=True)
myData['logFile'].write(s + '\n')
sampleName = {}
for i in myData['sampleTableList']:
s = '\t'.join(i)
print(s,flush=True)
myData['logFile'].write(s + '\n')
sampleName[i[0]] = 1
if len(sampleName) != 1:
s = 'ERROR! there are multiple sample names entered!'
print(s,flush=True)
myData['logFile'].write(s + '\n')
sys.exit()
k = list(sampleName.keys())
myData['sampleName'] = k[0]
#############################################################################
def check_dir_space(myData,checkSize = True):
myData['logFile'].write('\nchecking file systems\n')
# check tmp dir
if os.path.isdir(myData['tmpDir']) is False:
s = myData['tmpDir'] + ' is not found! making it'
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
cmd = 'mkdir -p %s ' % myData['tmpDir']
print(cmd,flush=True)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
if os.path.isdir(myData['finalDir']) is False:
s = myData['finalDir'] + ' is not found! please check'
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
sys.exit()
cmd = 'df -h %s' % myData['tmpDir']
o = runCMD_output(cmd)
myData['logFile'].write(cmd + '\n')
myData['logFile'].write(o[0] + '\n')
myData['logFile'].write(o[1] + '\n')
myData['logFile'].write('\n')
cmd = 'df -h %s' % myData['finalDir']
o = runCMD_output(cmd)
myData['logFile'].write(cmd + '\n')
myData['logFile'].write(o[0] + '\n')
myData['logFile'].write(o[1] + '\n')
stats = os.statvfs(myData['tmpDir'])
freeSpace = stats.f_frsize * stats.f_bavail
freeSpaceGb = freeSpace / (1024**3)
if freeSpaceGb < 200.0:
s = 'less than 200 Gb free in tmpDir! %f\n' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
if checkSize is True: # kill job
s = 'ERROR!! less than 200 Gb free in tmpDir! %f\n Exiting Script!' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
myData['logFile'].close()
sys.exit()
else:
s = 'more than 200 Gb free in tmpDir! %f\n Ok!' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
stats = os.statvfs(myData['finalDir'])
freeSpace = stats.f_frsize * stats.f_bavail
freeSpaceGb = freeSpace / (1024**3)
if freeSpaceGb < 50.0:
s = 'less than 50 Gb free in final dir! %f\n!' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
if checkSize is True: # kill job
s = 'ERROR less than 50 Gb free in final dir! %f\n! Exiting!' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
myData['logFile'].close()
sys.exit()
else:
s = 'more than 50 Gb free in final dir! %f\n Ok!' % freeSpaceGb
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
myData['logFile'].flush()
#############################################################################
def run_bwa_mem2(myData,run=True):
# run bwa mem2, only option is to not run program, use in reset mode
s = 'Starting bwa mem'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
myData['workingBaseDir'] = myData['tmpDir'] + myData['sampleName']
if os.path.isdir(myData['workingBaseDir']) is True:
s = '%s exists!' % (myData['workingBaseDir'])
print(s,flush=True)
myData['logFile'].write(s + '\n')
else:
cmd = 'mkdir %s' % myData['workingBaseDir']
myData['logFile'].write(cmd + '\n')
runCMD(cmd)
myData['workingBaseDir'] += '/'
myData['bwaMEMBam'] = myData['workingBaseDir'] + myData['sampleName'] + '.bam'
cmd = 'bwa-mem2 mem -K 100000000 -t %i -Y ' % (myData['threads'])
rg = '\'@RG\\tID:%s\\tSM:%s\\tLB:%s\\tPL:ILLUMINA\'' % (myData['sampleName']+'_'+myData['libName'],myData['sampleName'],myData['libName'])
cmd += ' -R %s ' % rg
cmd += '%s %s %s' % (myData['refBWA'], myData['fq1'],myData['fq2'])
cmd += ' | samtools view -bS - > %s ' % myData['bwaMEMBam']
if run is True:
print(cmd)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
else:
s = 'skipping run_bwa_mem2'
print(s,flush=True)
myData['logFile'].write(s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
#############################################################################
def run_bwa_mem2_table(myData,run=True):
s = 'Starting bwa mem'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
myData['workingBaseDir'] = myData['tmpDir'] + myData['sampleName']
if os.path.isdir(myData['workingBaseDir']) is True:
s = '%s exists!' % (myData['workingBaseDir'])
print(s,flush=True)
myData['logFile'].write(s + '\n')
else:
cmd = 'mkdir %s' % myData['workingBaseDir']
myData['logFile'].write(cmd + '\n')
runCMD(cmd)
myData['workingBaseDir'] += '/'
# final BAM
myData['bwaMEMBam'] = myData['workingBaseDir'] + myData['sampleName'] + '.bam'
if run is False:
s = 'skipping run_bwa_mem2'
print(s,flush=True)
myData['logFile'].write(s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
return
# ok, now here to do the runs
# table is sampleName LibraryName ReadGroupID fastq1 fastq2
if len(myData['sampleTableList']) == 1: # just single file
row = myData['sampleTableList'][0]
cmd = 'bwa-mem2 mem -K 100000000 -t %i -Y ' % (myData['threads'])
rg = '\'@RG\\tID:%s\\tSM:%s\\tLB:%s\\tPL:ILLUMINA\'' % (row[2],row[0],row[1] )
cmd += ' -R %s ' % rg
cmd += '%s %s %s' % (myData['refBWA'], row[3],row[4])
cmd += ' | samtools view -bS - > %s ' % myData['bwaMEMBam']
print(cmd,flush=True)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
else: # multiple, need to be merged
tmpBamList = []
for row_i in range(len(myData['sampleTableList'])):
row = myData['sampleTableList'][row_i]
newBamName = myData['workingBaseDir'] + myData['sampleName'] + '.row.%i.bam' % (row_i)
cmd = 'bwa-mem2 mem -K 100000000 -t %i -Y ' % (myData['threads'])
rg = '\'@RG\\tID:%s\\tSM:%s\\tLB:%s\\tPL:ILLUMINA\'' % (row[2],row[0],row[1] )
cmd += ' -R %s ' % rg
cmd += '%s %s %s' % (myData['refBWA'], row[3],row[4])
cmd += ' | samtools view -bS - > %s ' % newBamName
print(cmd,flush=True)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
tmpBamList.append(newBamName)
# here, ran for each separately
# merge together the bams
# first, make the header
for i in tmpBamList:
h = i + '.header'
cmd = 'samtools view -H %s > %s ' % (i,h)
print(cmd,flush=True)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
# make the new header
newHeader = myData['workingBaseDir'] + myData['sampleName'] + '.sam'
# copy header 0
i = tmpBamList[0]
h = i + '.header'
cmd = 'cp %s %s ' % (h,newHeader)
print(cmd,flush=True)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
# copy over the rest of the headers
for i in tmpBamList[1:]:
h = i + '.header'
cmd = 'grep \'^@RG\' %s >> %s ' % (h,newHeader)
print(cmd,flush=True)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
# cat bams
cmd = 'samtools cat -h %s ' % newHeader
cmd += ' -o %s ' % myData['bwaMEMBam']
for i in tmpBamList:
cmd += ' %s ' % i
print(cmd,flush=True)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
#############################################################################
def run_mdspark(myData,run=True):
s = 'Starting run_mdspark'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
myData['MDbam'] = myData['workingBaseDir'] + myData['sampleName'] + '.sort.md.bam'
myData['dupMetsFile'] = myData['finalDir'] + myData['sampleName'] + '.sort.md.metricts.txt'
cmd = 'gatk MarkDuplicatesSpark -I %s -O %s -M %s ' % (myData['bwaMEMBam'],myData['MDbam'],myData['dupMetsFile'])
cmd += ' --tmp-dir %s ' % myData['tmpDir']
cmd += ' --conf ''spark.executor.cores=%i'' ' % myData['threads']
cmd += ' --conf ''spark.local.dir=%s'' ' % myData['tmpDir']
if run is True:
print(cmd,flush=True)
myData['logFile'].write(cmd + '\n')
myData['logFile'].flush()
runCMD(cmd)
else:
s = 'skipping run_mdspark'
print(s,flush=True)
myData['logFile'].write(s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
#############################################################################
def run_bqsr(myData,run=True):
#setup, run, and apply BQSR
s = 'starting run_bqsr'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
# first, need to make list of intervals to procsess!
setup_intervals(myData)
# now, can write out cmd for each intervals
# make intervals files
myData['bqsrDataDir'] = myData['workingBaseDir'] + 'bqsr-recal'
if os.path.isdir(myData['bqsrDataDir']) is True:
s = '%s exists!' % (myData['bqsrDataDir'])
print(s,flush=True)
myData['logFile'].write(s + '\n')
else:
cmd = 'mkdir %s' % myData['bqsrDataDir']
myData['logFile'].write(cmd + '\n')
runCMD(cmd)
myData['bqsrDataDir'] += '/'
myData['bqsrJobsFileName'] = myData['workingBaseDir'] + 'baserecal.jobs.txt'
outFile = open(myData['bqsrJobsFileName'],'w')
listOfBQSRRecalFiles = []
for intervalFileName in myData['bqsrIntervalsFiles']:
cName = intervalFileName.split('/')[-1].split('.')[0]
outDataName = myData['bqsrDataDir'] + cName + '.recal_data.table'
cmd = 'gatk --java-options "-Xmx4G" BaseRecalibrator '
cmd += ' --tmp-dir %s ' % myData['tmpDir']
cmd += ' -I %s ' % myData['MDbam']
cmd += ' -R %s' % myData['ref']
cmd += ' --intervals %s ' % intervalFileName
cmd += ' --known-sites %s ' % myData['knownSitesVCF']
cmd += ' -O %s ' % outDataName
cmd += '\n'
outFile.write(cmd)
listOfBQSRRecalFiles.append(outDataName)
outFile.close()
s = 'list of BaseRecalibrator written to %s' % myData['bqsrJobsFileName']
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
myData['logFile'].flush()
cmd = 'parallel --jobs %i < %s' % (myData['threads'],myData['bqsrJobsFileName'])
if run is True:
print(cmd)
myData['logFile'].write(cmd + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
runCMD(cmd)
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
else:
s = 'skipping parallel BaseRecalibrator'
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
# next, need to gather up the BaseRecalibrator reports
myData['bqsrReportsFileList'] = myData['bqsrJobsFileName'] + '.reports.list'
myData['bqsrReportsGatheredFile'] = myData['workingBaseDir'] + 'bqsrgathered.reports.list'
outFile = open( myData['bqsrReportsFileList'],'w')
for f in listOfBQSRRecalFiles:
outFile.write('%s\n' % f)
outFile.close()
cmd = 'gatk --java-options "-Xmx6G" GatherBQSRReports '
cmd += ' --input %s ' % myData['bqsrReportsFileList']
cmd += ' --output %s ' % myData['bqsrReportsGatheredFile']
if run is True:
print(cmd)
myData['logFile'].write(cmd + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
runCMD(cmd)
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
else:
s = 'skipping GatherBQSRReports'
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
# now, apply BQSR by chromosome again...
myData['ApplyBQSRJobsFileName'] = myData['workingBaseDir'] + 'applyBQSR.jobs.txt'
outFile = open(myData['ApplyBQSRJobsFileName'],'w')
for intervalFileName in myData['bqsrIntervalsFiles']:
cName = intervalFileName.split('/')[-1].split('.')[0]
outBAMName = myData['bqsrDataDir'] + cName + '.bqsr.bam'
cmd = 'gatk --java-options "-Xmx4G" ApplyBQSR '
cmd += ' --tmp-dir %s ' % myData['tmpDir']
cmd += ' -I %s ' % myData['MDbam']
cmd += ' -R %s' % myData['ref']
cmd += ' -O %s ' % outBAMName
cmd += ' --intervals %s ' % intervalFileName
cmd += ' --bqsr-recal-file %s ' % myData['bqsrReportsGatheredFile']
cmd += ' --preserve-qscores-less-than 6 --static-quantized-quals 10 --static-quantized-quals 20 --static-quantized-quals 30 '
cmd += '\n'
outFile.write(cmd)
# do unmapped
outBAMName = myData['bqsrDataDir'] + 'unmapped' + '.bqsr.bam'
cmd = 'gatk --java-options "-Xmx4G" ApplyBQSR '
cmd += ' --tmp-dir %s ' % myData['tmpDir']
cmd += ' -I %s ' % myData['MDbam']
cmd += ' -R %s' % myData['ref']
cmd += ' -O %s ' % outBAMName
cmd += ' --intervals %s ' % 'unmapped'
cmd += ' --bqsr-recal-file %s ' % myData['bqsrReportsGatheredFile']
cmd += ' --preserve-qscores-less-than 6 --static-quantized-quals 10 --static-quantized-quals 20 --static-quantized-quals 30 '
cmd += '\n'
outFile.write(cmd)
outFile.close()
s = 'list of ApplyBQSRJobs written to %s' % myData['ApplyBQSRJobsFileName']
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
myData['logFile'].flush()
cmd = 'parallel --jobs %i < %s' % (myData['threads'],myData['ApplyBQSRJobsFileName'])
if run is True:
print(cmd)
myData['logFile'].write(cmd + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
runCMD(cmd)
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
myData['logFile'].flush()
else:
s = 'skipping parallel ApplyBQSRJobsFileName'
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
# next, need to merge together CRAM files
# GatherBamFiles, need them in order to be used
s = 'starting GatherBamFiles'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
myData['logFile'].flush()
bamFileOrder = get_order_for_GatherBamFiles(myData)
myData['recalBamFile'] = myData['workingBaseDir'] + myData['sampleName'] + '.recal.bam'
cmd = 'gatk --java-options "-Xmx6G" GatherBamFiles'
cmd += ' --CREATE_INDEX true '
for i in bamFileOrder:
cmd += ' -I %s ' % i
cmd += ' -O %s ' % myData['recalBamFile']
if run is True:
print(cmd)
myData['logFile'].write(cmd + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
runCMD(cmd)
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
else:
s = 'skipping gather BAM files'
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
#############################################################################
def get_order_for_GatherBamFiles(myData):
# this is the order to combine the cram files into
# note, this is hard coded for the dog genome here..., so
# will have to be smart if using on human or other.
bamFileOrder = []
for i in range(1,39):
c = 'chr' + str(i)
bam = myData['bqsrDataDir'] + c + '.bqsr.bam'
bamFileOrder.append(bam)
c = 'chrX'
bam = myData['bqsrDataDir'] + c + '.bqsr.bam'
bamFileOrder.append(bam)
c = 'other'
bam = myData['bqsrDataDir'] + c + '.bqsr.bam'
bamFileOrder.append(bam)
c = 'unmapped'
bam = myData['bqsrDataDir'] + c + '.bqsr.bam'
bamFileOrder.append(bam)
return bamFileOrder
#############################################################################
def get_order_for_GatherGVCFFiles(myData):
# this is the order to combine the g.vcf files into
# note, this is hard coded for the dog genome here..., so
# will have to be smart if using on human or other.
vcfFileOrder = []
for i in range(1,39):
c = 'chr' + str(i)
outVCFName = myData['gvcfDir'] + c + '.g.vcf.gz'
vcfFileOrder.append(outVCFName)
c = 'chrX'
outVCFName = myData['gvcfDir'] + c + '.g.vcf.gz'
vcfFileOrder.append(outVCFName)
c = 'other'
outVCFName = myData['gvcfDir'] + c + '.g.vcf.gz'
vcfFileOrder.append(outVCFName)
return vcfFileOrder
#############################################################################
def setup_intervals(myData):
# read in contig names
contigNames = []
inFile = open(myData['ref'] + '.fai')
for line in inFile:
line = line.rstrip()
line = line.split()
c = line[0]
contigNames.append(c)
inFile.close()
s = 'read in %i chrom names, working on assiging to intervals' % len(contigNames)
print(s,flush=True)
myData['logFile'].write(s + '\n')
# chrom to do separate
#int names
chromNames = []
chromNames.append('chrX') # put X first, so start with X, chr1 -- longest chroms start first
for i in range(1,39):
c = 'chr' + str(i)
chromNames.append(c)
s = 'have %i chroms to do individually' % len(chromNames)
print(s,flush=True)
myData['logFile'].write(s + '\n')
toDoTogether = []
for c in contigNames:
if c not in chromNames:
toDoTogether.append(c)
s = 'have %i chroms to do together' % len(toDoTogether)
print(s,flush=True)
myData['logFile'].write(s + '\n')
# make intervals files
myData['bqsrIntervalsDir'] = myData['workingBaseDir'] + 'bqsr-intervals'
if os.path.isdir(myData['bqsrIntervalsDir']) is True:
s = '%s exists!' % (myData['bqsrIntervalsDir'])
print(s,flush=True)
myData['logFile'].write(s + '\n')
else:
cmd = 'mkdir %s' % myData['bqsrIntervalsDir']
myData['logFile'].write(cmd + '\n')
runCMD(cmd)
myData['bqsrIntervalsDir'] += '/'
myData['bqsrIntervalsFiles'] = []
for c in chromNames:
fn = myData['bqsrIntervalsDir'] + c + '.list'
outFile = open(fn,'w')
outFile.write('%s\n' % c)
outFile.close()
myData['bqsrIntervalsFiles'].append(fn)
# then add the rest
fn = myData['bqsrIntervalsDir'] + 'other' + '.list'
outFile = open(fn,'w')
for c in toDoTogether:
outFile.write('%s\n' % c)
outFile.close()
myData['bqsrIntervalsFiles'].insert(0,fn) # so that starts with the other, this is large want more run time
s = 'have setup %i bqsrIntervalsFiles files' % len(myData['bqsrIntervalsFiles'])
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
#############################################################################
def run_haplotypecaller(myData,run=True):
#setup, run, and apply BQSR
s = 'starting run_haplotypecaller'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
# make intervals files
myData['gvcfDir'] = myData['workingBaseDir'] + 'gvcfDir'
if os.path.isdir(myData['gvcfDir']) is True:
s = '%s exists!' % (myData['gvcfDir'])
print(s,flush=True)
myData['logFile'].write(s + '\n')
else:
cmd = 'mkdir %s' % myData['gvcfDir']
myData['logFile'].write(cmd + '\n')
runCMD(cmd)
myData['gvcfDir'] += '/'
myData['runHaplotypeCallerJobsFileName'] = myData['workingBaseDir'] + 'hapcaller.jobs.txt'
outFile = open(myData['runHaplotypeCallerJobsFileName'],'w')
# add write CRAM at this step, to get rid of dead CPU time...
# change to make CRAM as first job
myData['finalCram'] = myData['finalDir'] + myData['sampleName'] + '.cram'
cmd = 'gatk --java-options "-Xmx6G" PrintReads '
cmd += ' -R %s' % myData['ref']
cmd += ' -I %s -O %s ' % (myData['recalBamFile'], myData['finalCram'])
cmd += '\n'
outFile.write(cmd)
for intervalFileName in myData['bqsrIntervalsFiles']:
cName = intervalFileName.split('/')[-1].split('.')[0]
outBAMName = myData['gvcfDir'] + cName + '.g.vcf.gz'
cmd = 'gatk --java-options "-Xmx4G" HaplotypeCaller '
cmd += ' --tmp-dir %s ' % myData['tmpDir']
cmd += ' -I %s ' % myData['recalBamFile']
cmd += ' -R %s' % myData['ref']
cmd += ' --intervals %s ' % intervalFileName
cmd += ' -O %s ' % outBAMName
cmd += ' -ERC GVCF '
cmd += '\n'
outFile.write(cmd)
outFile.close()
s = 'list of HaplotypeCaller written to %s' % myData['runHaplotypeCallerJobsFileName']
s += ' this includes write CRAM! '
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
myData['logFile'].flush()
cmd = 'parallel --jobs %i < %s' % (myData['threads'],myData['runHaplotypeCallerJobsFileName'])
if run is True:
print(cmd)
myData['logFile'].write(cmd + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
runCMD(cmd)
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
myData['logFile'].flush()
else:
s = 'skipping parallel runHaplotypeCallerJobsFileName'
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
# now gather gvcf files into one
myData['combinedGVCF'] = myData['finalDir'] + myData['sampleName'] + '.g.vcf.gz'
vcfFileOrder = get_order_for_GatherGVCFFiles(myData)
cmd = 'gatk --java-options "-Xmx6G" GatherVcfs'
for f in vcfFileOrder:
cmd += ' -I %s ' % f
cmd += ' -O %s ' % myData['combinedGVCF']
if run is True:
print(cmd)
myData['logFile'].write(cmd + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
runCMD(cmd)
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
myData['logFile'].flush()
else:
s = 'skipping combinedGVCF'
print(s,flush=True)
myData['logFile'].write(s + '\n')
myData['logFile'].flush()
#############################################################################
def remove_tmp_dir(myData,run=True):
#setup, run, and apply BQSR
s = 'starting remove tmp dir: %s ' % (myData['tmpDir'])
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
check_dir_space(myData,checkSize = False)
if run is True:
shutil.rmtree(myData['tmpDir'])
s = 'removed!'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
else:
s = 'skipping rmtree!'
print(s,flush=True)
myData['logFile'].write('\n' + s + '\n')
t = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
myData['logFile'].write(t + '\n')
myData['logFile'].flush()
#############################################################################