-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGMH_pipe.py
1961 lines (1878 loc) · 116 KB
/
GMH_pipe.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
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ===================================
# MG data analysis pipeline
# by R.Gacesa, UMCG (2022, Dec)
# Version GMH pipeline v0.9 (beta)
# ===================================
# UPDATES (GMH 0.9.0):
# > fixed bugs with humann3.6 checkjobs
# > fixed bugs with sorting strainphlan4 jobs
# > added support for GTDBTK, CheckM
# > misc other minor changes
# > renamed the version to Groningen Microbiome Hub (GMH pipeline)
# > codes are now on github
# UPDATES (03/h):
# > added support for node tmp for jobs 0,1a,1b
# > moved merging to tmp nodes [if usetmp != 0]
# UPDATES (03/g):
# > added support for Panphlan, Metawrap binning, Metawrap refining
# UPDATES (03/f):
# > added supprot for Metaphlan4
# UPDATES (03/e):
# > updated support for SVFINDER to two-step job
# for improved parallelization and job management
# UPDATES (03/d):
# > added support for SVFINDER (experimental)
# UPDATES (03/c):
# > added support for BBduk
#
# includes:
# -> raw data qc (fastQC -> kneaddata -> fastQC)
# -> profiling (metaphlan3, humann3, kraken2)
# -> targeted analysis:
# - virulence factors (VFDB)
# - resistance genes (CARD (WIP), ResFinder DB)
# - strain profiling (strainphlan)
# -> assembly:
# - metaspades
# - megahit
#
# -> job management:
# - job writing
# - result collection
#
# ==================================
# helper functions
# ==================================
# buffered counter for read number
def bufcount(filename):
f = open(filename)
lines = 0
buf_size = 1024 * 1024
read_f = f.read
buf = read_f(buf_size)
while buf:
lines += buf.count('\n')
buf = read_f(buf_size)
return lines
# config loader
# ================================
def loadConfig():
print (' --> loading config file',args.jobConfig)
if not os.path.isfile(args.jobConfig):
print (" ERROR: config file not found! make sure --jobConfig points to config file")
exit(1)
else:
cfg = configparser.RawConfigParser()
cfg.read(args.jobConfig)
return cfg
# job writer modules
# ================================
# next job writer
def writeNextJob(pipeSection, jobID):
jT = cfg.get(pipeSection,'time').split(':')
# print('<'+cfg.get('PIPELINE','usePeregrineJobQueues')+'>',cfg.get('PIPELINE','usePeregrineJobQueues')=="1")
# print('<'+cfg.get('PIPELINE','useSlurm')+'>',cfg.get('PIPELINE','useSlurm')=="1")
# print(jT[-3],int(jT[-3]),int(jT[-3])==0)
# print(jT[-2],int(jT[-2])<=30)
oF.write('# --- SUBMIT '+pipeSection+' (job '+jobID+') ---- \n')
oF.write('# echo "Submitting '+pipeSection+' (job '+jobID+')" \n')
if '-' in cfg.get(pipeSection,'time'):
nJ = nextjob+' '+smpl+'_'+jobID+'.sh \n'
elif int(jT[-3]) == 0 and int(jT[-2]) <= 30 and cfg.get('PIPELINE','usePeregrineJobQueues') == "1" and cfg.get('PIPELINE','useSlurm') == "1":
nJ = nextjob+' --partition=short '+smpl+'_'+jobID+'.sh \n'
else:
nJ = nextjob+' '+smpl+'_'+jobID+'.sh \n'
oF.write(nJ)
# basic job parameters writer:
# > writes basic part of SLURM job (memory,cpus,time...)
# > also adds module loading, paths and conda
def writeBasics(mdlName,jobName,purgeConda=True,purgeModsAfterConda=False,writeSBATCH=True):
if writeSBATCH:
oF.write('#!/bin/bash\n')
oF.write('#SBATCH --job-name='+jobName+'_'+smpl+'\n')
oF.write('#SBATCH --error='+cfg.get('PIPELINE','jobOut')+'/'+'__'+smpl+'_'+jobName+'.err\n')
oF.write('#SBATCH --output='+cfg.get('PIPELINE','jobOut')+'/'+'__'+smpl+'_'+jobName+'.out\n')
oF.write('#SBATCH --mem='+cfg.get(mdlName,'memory')+'\n')
oF.write('#SBATCH --time='+cfg.get(mdlName,'time')+'\n')
oF.write('#SBATCH --cpus-per-task='+cfg.get(mdlName,'cpus')+'\n')
oF.write('#SBATCH --open-mode=truncate\n')
if cfg.get('PIPELINE','doProfiling',fallback='0') == '1':
oF.write('#SBATCH --profile=task'+'\n')
oF.write('# ==== JOB '+mdlName+' ('+jobName+')'+' ==== \n')
oF.write('echo "========================================== " \n')
oF.write('echo "> STARTING JOB '+mdlName+' (job '+jobName+')"'+' \n')
oF.write('echo "========================================== " \n')
# purge existing stuff to avoid weird roll-overs and bugs
oF.write('# PURGING ENVIRUMENT \n')
oF.write('echo "> purging environment "\n')
oF.write('module purge \n')
# load modules
if not cfg.get(mdlName,'modules').strip() == '':
oF.write('echo "> loading modules "\n')
oF.write('# --- LOAD MODULES --- \n')
for m in cfg.get(mdlName,'modules').split(','):
oF.write('module load '+m+'\n')
# load CONDA
if not cfg.get(mdlName,'condaEnv').strip() == '':
oF.write('# --- CLEAN CONDA --- \n')
oF.write('echo "> cleaning conda env. "\n')
if purgeConda:
oF.write('conda deactivate\n')
oF.write('# --- LOAD CONDA --- \n')
oF.write('echo "> loading conda env. "\n')
#oF.write('conda init bash \n')
oF.write('conda activate '+cfg.get(mdlName,'condaEnv').strip()+'\n')
if purgeModsAfterConda:
oF.write('module purge'+'\n')
# add paths
if not cfg.get(mdlName,'paths').strip() == '':
oF.write('echo "> loading sys paths "\n')
oF.write('# --- LOAD PATHS --- \n')
for p in cfg.get(mdlName,'paths').split(','):
#oF.write('export PATH="$PATH":'+p+'\n')
oF.write('PATH=${PATH}:'+p+'\n')
# imports
# =================================
import copy
import argparse
import os
import csv
import shutil
import glob
import configparser
import subprocess
import random
# ============================================================
# ============================================================
# MAIN
# ============================================================
# ============================================================
# process CL args
parser = argparse.ArgumentParser()
parser.add_argument("cmd",
help=''' command for pipeline, as follows:
checkjobs : checks which input files in _datalist.tsv are done by checking (--tgt) for results
writejobs : prepares jobs for _datalist.tsv
sortresults : sorts results and moves them to new location; has to be used with --outPath <target location>
runjobs : runs all samples (sbatch queues *_p1.sh jobs)
''')
parser.add_argument('tgt',help='where [def= current folder (".") ]',default='.',nargs='?')
parser.add_argument('--outPath',help='where to put results',default='.')
parser.add_argument('--inPath',help='where are results',default='.')
parser.add_argument('--jobConfig',help='master config for making jobs',default='GMH_pipe.cfg')
parser.add_argument('--version', action='version', version='GMH pipeline v.0.9.0 (15/02/2023)')
args = parser.parse_args()
# set-up and define allowed commands
cmd = args.cmd
tgt = args.tgt
cmdlist = ['runjobs',
'writejobs',
'checkjobs',
'sortresults',
'mergeresults']
# ================= START THE PIPELINE =======================
print (' ----------------------------------------')
print (' DAG3 pipeline V3 invoked ')
print (' ----------------------------------------')
print (' >>> running command <',cmd,'> on target <',tgt,'>')
# check if command is actually allowed
if args.cmd in cmdlist:
print (' --> Executing',args.cmd,'command!')
else:
print (' --> ERROR: command',args.cmd,'not recognized!')
print (' --> NOTE: run --help to see supported commands')
exit(1)
# ============================================================
# runjobs command:
# ============================================================
#
# > runs all startup jobs (<xyz>_p1.sh in tgt folder)
# ============================================================
if args.cmd == 'runjobs':
print (' --> starting pipeline for all samples in ',tgt)
torun = set()
for l in os.listdir(tgt):
if os.path.isfile(l) and l.endswith('_p0.sh'):
torun.add(l)
if len(torun) == 0:
print(' --> WARNING: No jobs to run (jobs should end in _p0.sh)')
print(' Check if writejobs was run or <tgt> is set properly')
else:
print(' --> Submitting',len(torun),'jobs!')
for j in torun:
os.system('sbatch '+j)
# ============================================================
# cleantmp command:
#
# > goes through <tgt> and cleans stuff
# -> does not clean tmp files of jobs in progress
# -> otherwise:
# --> cleans humann temp
# --> cleans clean_reads
# --> cleans filtered reads
# ============================================================
if args.cmd == 'cleantmp':
print (' --> Performing cleanup of temporary files in ',tgt)
print (' --> looking for jobs in progress')
jlist = subprocess.check_output('sacct --format="JobName%30" -n --allusers', shell=True)
jhesh = set()
for j in jlist.split('\n'):
j = j.strip()
if not j == '': jhesh.add(j)
for l in os.listdir(tgt):
if os.path.isdir(l):
runna = False
for j in jhesh:
if l.strip() in j:
print (' W: ',l,'is running (',j,')')
runna = True
break
if not runna:
print (' > ',l,'not running, cleaning')
if os.path.isdir(l+'/clean_reads'): os.system('rm -r '+l+'/clean_reads')
if os.path.isdir(l+'/filtering_data'): os.system('rm -r '+l+'/filtering_data')
if os.path.isdir(l+'/humann3'):
for hl in os.listdir(l+'/humann3'):
if 'temp' in hl:
os.system('rm -r '+l+'/humann3/'+hl)
# ============================================================
# checkjobs command:
# ============================================================
# - takes target (either folder or datafile) and compares to target folder(s) to identify
# a) which samples have been processed [def output: _done_mp.tsv, _done_hu.tsv]
# b) which samples have not been processed [def output: _ndone_mp.tsv,ndone_hu.tsv']
# NYI: b) which step was done for which sample
# ===========================================================
if args.cmd == 'checkjobs':
print (' > Executing "',args.cmd,'" command!')
inPath = args.inPath
# load config
cfg = loadConfig()
print (' --> identifying which samples in <',inPath,'> have results in <',tgt,'>')
# find samples
smpls = set()
smpls_all = {}
dFileIn = False
if os.path.isdir(inPath):
for l in os.listdir(inPath):
if os.path.isdir(l) and not l == cfg.get('PIPELINE','jobOut'):
smpls.add(l)
if len(smpls) == 0:
print (' ERROR: no samples found!')
exit(1)
print (' --> found',len(smpls),'samples in',inPath)
print (' --> scanning ',tgt)
# done
kneadDone = set()
humann3Done = set()
humann36Done = set()
metaPhlan3Done = set()
strainPhlan3Done = set()
strainPhlan4Done = set()
sbVFDB_done = set()
sbCARD_done = set()
assMetaDone = set()
assMegaDone = set()
kraken2Done = set()
metaPhlan4Done = set()
svDone = set()
binMwRefDone = set()
binAnnotCmDone = set()
binAnnotGtDone = set()
binQuantifyDone = set()
# not done
kneadND = set()
metaPhlan3ND = set()
metaPhlan4ND = set()
strainPhlan3ND = set()
strainPhlan4ND = set()
humann3ND = set()
humann36ND = set()
sbVFDB_ND = set()
sbCARD_ND = set()
assMetaND = set()
assMegaND = set()
kraken2ND = set()
svND = set()
binMwRefND = set()
binAnnotCmND = set()
binAnnotGtND = set()
binQuantifyND = set()
for root, dirpath, files in os.walk(tgt):
for fl in files:
#print (fl)
aPathFl = os.path.abspath(root)+'/'+fl
# find kneaddata results
if cfg.get('PIPELINE','doKnead') == '1':
if cfg.get('KNEAD','mergeResults') == '1':
if fl.endswith('_kneaddata_cleaned_paired_merged.fastq'):
if fl.replace('_kneaddata_cleaned_paired_merged.fastq','') in smpls:
if os.path.getsize(aPathFl) > 10000:
kneadDone.add(fl.replace('_kneaddata_cleaned_paired_merged.fastq',''))
else:
if fl.endswith('_kneaddata_cleaned_pair_2.fastq'):
if fl.replace('_kneaddata_cleaned_pair_2.fastq','') in smpls:
if os.path.getsize(aPathFl) > 10000:
kneadDone.add(fl.replace('_kneaddata_cleaned_pair_2.fastq',''))
# find humann results [H3]
if cfg.get('PIPELINE','doHumann3') == '1':
if fl.endswith('pathabundance.tsv') and root.endswith('humann3'):
if fl.replace('_kneaddata_cleaned_paired_merged_pathabundance.tsv','') in smpls:
#print (fl)
#print (dirs)
humann3Done.add(fl.replace('_kneaddata_cleaned_paired_merged_pathabundance.tsv',''))
# find humann results [H3.6]
if cfg.get('PIPELINE','doHumann3.6') == '1' and root.endswith('humann3.6'):
if fl.endswith('pathabundance.tsv'):
if fl.replace('_kneaddata_cleaned_paired_merged_pathabundance.tsv','') in smpls:
humann36Done.add(fl.replace('_kneaddata_cleaned_paired_merged_pathabundance.tsv',''))
# find metaphlan
if cfg.get('PIPELINE','doMeta3') == '1':
if fl.endswith('_metaphlan.txt') and 'metaphlan3' in root:
if fl.replace('_metaphlan.txt','') in smpls:
if os.path.getsize(aPathFl) > 100:
metaPhlan3Done.add(fl.replace('_metaphlan.txt',''))
# find metaphlan
if cfg.get('PIPELINE','doMeta4') == '1':
if fl.endswith('_metaphlan.txt') and 'metaphlan4' in root:
if fl.replace('_metaphlan.txt','') in smpls:
if os.path.getsize(aPathFl) > 100:
metaPhlan4Done.add(fl.replace('_metaphlan.txt',''))
# find strainphlan markers
if cfg.get('PIPELINE','doStrainPhlan3') == '1':
if fl.endswith('_metaphlan3.pkl'):
if fl.replace('_metaphlan3.pkl','') in smpls:
if os.path.getsize(aPathFl) > 0:
strainPhlan3Done.add(fl.replace('_metaphlan3.pkl',''))
# find strainphlan markers
if cfg.get('PIPELINE','doStrainPhlan4') == '1':
if fl.endswith('_metaphlan4.pkl'):
if fl.replace('_metaphlan4.pkl','') in smpls:
if os.path.getsize(aPathFl) > 0:
strainPhlan4Done.add(fl.replace('_metaphlan4.pkl',''))
# find CARD
if cfg.get('PIPELINE','doCARD_SB') == '1':
if fl.endswith('_CARD_sb.txt'):
if fl.replace('_CARD_sb.txt','') in smpls:
if os.path.getsize(aPathFl) > 0:
sbCARD_done.add(fl.replace('_CARD_sb.txt',''))
# find VFDB
if cfg.get('PIPELINE','doVFDB_SB') == '1':
if fl.endswith('_vfdb_sb.txt'):
if fl.replace('_vfdb_sb.txt','') in smpls:
if os.path.getsize(aPathFl) > 0:
sbVFDB_done.add(fl.replace('_vfdb_sb.txt',''))
# find MEGAHIT
if cfg.get('PIPELINE','doAssemblyMegaHit') == '1':
if fl.endswith('_megahit_contigs.fa'):
if fl.replace('_megahit_contigs.fa','') in smpls:
if os.path.getsize(aPathFl) > 0:
assMegaDone.add(fl.replace('_megahit_contigs.fa',''))
# find META-SPADES
if cfg.get('PIPELINE','doAssemblyMetaSpades') == '1':
if fl.endswith('_metaspades_scaffolds.fa'):
if fl.replace('_metaspades_scaffolds.fa','') in smpls:
if os.path.getsize(aPathFl) > 0:
assMetaDone.add(fl.replace('_metaspades_scaffolds.fa',''))
# find KRAKEN
if cfg.get('PIPELINE','doKraken2') == '1':
if fl.endswith('.bracken.result.kr'):
if fl.replace('.bracken.result.kr','') in smpls:
if os.path.getsize(aPathFl) > 0:
kraken2Done.add(fl.replace('.bracken.result.kr',''))
# find SVs
if cfg.get('PIPELINE','doSVFinder',fallback='0') == '1':
if fl.endswith('_SVs.mapped.jsdel'):
if fl.replace('_SVs.mapped.jsdel','') in smpls:
if os.path.getsize(aPathFl) > 0:
svDone.add(fl.replace('_SVs.mapped.jsdel',''))
# find quantified bins
if cfg.get('PIPELINE','doBinQuantification',fallback='0') == '1':
if fl.endswith('_bin_abundances.txt') and 'bins_quantification' in root:
if os.path.getsize(aPathFl) > 50:
binQuantifyDone.add(fl.replace('_bin_abundances.txt',''))
# find annotation (checkM)
if cfg.get('PIPELINE','doBinAnnotationCheckM',fallback='0') == '1':
if fl.endswith('_checkM_results_parsed.csv') and 'bins_checkM' in root:
if os.path.getsize(aPathFl) > 50:
binAnnotCmDone.add(fl.replace('_checkM_results_parsed.csv',''))
# find annotation (GTDBK)
if cfg.get('PIPELINE','doBinAnnotationGTDBK',fallback='0') == '1':
if fl.endswith('.gtdbk.bac120.summary.tsv') and 'bins_GTDBK' in root:
if os.path.getsize(aPathFl) > 50:
binAnnotGtDone.add(fl.replace('.gtdbk.bac120.summary.tsv',''))
# find refined Bins
if cfg.get('PIPELINE','doBinRefiningMWrap',fallback='0') == '1':
if 'bins_metawrap_refined' in root and len(files) > 0:
fldr = root.split('/')[-2]
binMwRefDone.add(fldr)
# get failed samples
humann3ND = smpls - humann3Done
humann36ND = smpls - humann36Done
kneadND = smpls - kneadDone
metaPhlan3ND = smpls - metaPhlan3Done
metaPhlan4ND = smpls - metaPhlan4Done
strainPhlan3ND = smpls - strainPhlan3Done
strainPhlan4ND = smpls - strainPhlan4Done
sbVFDB_ND = smpls - sbVFDB_done
sbCARD_ND = smpls - sbCARD_done
assMegaND = smpls - assMegaDone
assMetaND = smpls - assMetaDone
kraken2ND = smpls - kraken2Done
svND = smpls - svDone
binMwRefND = smpls - binMwRefDone
binAnnotCmND = smpls - binAnnotCmDone
binAnnotGtND = smpls - binAnnotGtDone
binQuantifyND = smpls - binQuantifyDone
# output
# > Knead
if cfg.get('PIPELINE','doKnead') == '1':
print (' -> KneadData DONE :',len(kneadDone),'samples [saving to _done_kn.tsv]')
print (' NOT :',len(kneadND),'samples [saving to _ndone_kn.tsv]')
with open('_done_kn.tsv','w') as oF:
for s in kneadDone: oF.write(s+'\n')
with open('_ndone_kn.tsv','w') as oF:
for s in kneadND: oF.write(s+'\n')
# > Humann3
if cfg.get('PIPELINE','doHumann3') == '1':
print (' -> Humann3 DONE :',len(humann3Done),'samples [saving to _done_hum3.tsv]')
print (' NOT :',len(humann3ND),'samples [saving to _ndone_hum3.tsv]')
with open('_done_hum3.tsv','w') as oF:
for s in humann3Done: oF.write(s+'\n')
with open('_ndone_hum3.tsv','w') as oF:
for s in humann3ND: oF.write(s+'\n')
# > Humann3.6
if cfg.get('PIPELINE','doHumann3.6') == '1':
print (' -> Humann3.6 DONE :',len(humann36Done),'samples [saving to _done_hum36.tsv]')
print (' NOT :',len(humann36ND),'samples [saving to _ndone_hum36.tsv]')
with open('_done_hum36.tsv','w') as oF:
for s in humann36Done: oF.write(s+'\n')
with open('_ndone_hum36.tsv','w') as oF:
for s in humann36ND: oF.write(s+'\n')
if cfg.get('PIPELINE','doMeta3') == '1':
print (' -> Metaphlan3 DONE :',len(metaPhlan3Done),'samples [saving to _done_mph3.tsv]')
print (' NOT :',len(metaPhlan3ND),'samples [saving to _ndone_mph3.tsv]')
with open('_done_mph3.tsv','w') as oF:
for s in metaPhlan3Done: oF.write(s+'\n')
with open('_ndone_mph3.tsv','w') as oF:
for s in metaPhlan3ND: oF.write(s+'\n')
# >> metaphlan 4
if cfg.get('PIPELINE','doMeta4') == '1':
print (' -> Metaphlan4 DONE :',len(metaPhlan4Done),'samples [saving to _done_mph4.tsv]')
print (' NOT :',len(metaPhlan4ND),'samples [saving to _ndone_mph4.tsv]')
with open('_done_mph4.tsv','w') as oF:
for s in metaPhlan4Done: oF.write(s+'\n')
with open('_ndone_mph4.tsv','w') as oF:
for s in metaPhlan4ND: oF.write(s+'\n')
# >> strainphlan 3
if cfg.get('PIPELINE','doStrainPhlan3') == '1':
print (' -> Strainphlan3 markers DONE :',len(strainPhlan3Done),'samples [saving to _done_sph3.tsv]')
print (' NOT :',len(strainPhlan3ND),'samples [saving to _ndone_sph3.tsv]')
with open('_done_sph3.tsv','w') as oF:
for s in strainPhlan3Done: oF.write(s+'\n')
with open('_ndone_sph3.tsv','w') as oF:
for s in strainPhlan3ND: oF.write(s+'\n')
# >> strainphlan 4
if cfg.get('PIPELINE','doStrainPhlan4') == '1':
print (' -> Strainphlan4 markers DONE :',len(strainPhlan4Done),'samples [saving to _done_sph4.tsv]')
print (' NOT :',len(strainPhlan4ND),'samples [saving to _ndone_sph4.tsv]')
with open('_done_sph4.tsv','w') as oF:
for s in strainPhlan4Done: oF.write(s+'\n')
with open('_ndone_sph4.tsv','w') as oF:
for s in strainPhlan4ND: oF.write(s+'\n')
# >> VFDB
if cfg.get('PIPELINE','doVFDB_SB') == '1':
print (' -> ShortBRED VFDB DONE :',len(sbVFDB_done),'samples [saving to _done_vfdb.tsv]')
print (' NOT :',len(sbVFDB_ND),'samples [saving to _ndone_vfdb.tsv]')
with open('_done_vfdb.tsv','w') as oF:
for s in sbVFDB_done: oF.write(s+'\n')
with open('_ndone_vfdb.tsv','w') as oF:
for s in sbVFDB_ND: oF.write(s+'\n')
if cfg.get('PIPELINE','doCARD_SB') == '1':
print (' -> ShortBRED CARD DONE :',len(sbCARD_done),'samples [saving to _done_card.tsv]')
print (' NOT :',len(sbCARD_ND),'samples [saving to _ndone_card.tsv]')
with open('_done_card.tsv','w') as oF:
for s in sbCARD_done: oF.write(s+'\n')
with open('_ndone_card.tsv','w') as oF:
for s in sbCARD_ND: oF.write(s+'\n')
if cfg.get('PIPELINE','doAssemblyMegaHit',fallback=0) == '1':
print (' -> MetaHIT Assembly DONE :',len(assMegaDone),'samples [saving to _done_mega.tsv]')
print (' NOT :',len(assMegaND),'samples [saving to _ndone_mega.tsv]')
with open('_done_mega.tsv','w') as oF:
for s in assMegaDone: oF.write(s+'\n')
with open('_ndone_mega.tsv','w') as oF:
for s in assMegaND: oF.write(s+'\n')
if cfg.get('PIPELINE','doAssemblyMetaSpades',fallback=0) == '1':
print (' -> MetaSPADES DONE :',len(assMetaDone),'samples [saving to _done_mspades.tsv]')
print (' NOT :',len(assMetaND),'samples [saving to _ndone_mspades.tsv]')
with open('_done_mspades.tsv','w') as oF:
for s in assMetaDone: oF.write(s+'\n')
with open('_ndone_mspades.tsv','w') as oF:
for s in assMetaND: oF.write(s+'\n')
if cfg.get('PIPELINE','doKraken2',fallback=0) == '1':
print (' -> KRAKEN2 DONE :',len(kraken2Done),'samples [saving to _done_kraken2.tsv]')
print (' NOT :',len(kraken2ND),'samples [saving to _ndone_kraken2.tsv]')
with open('_done_kraken2.tsv','w') as oF:
for s in kraken2Done: oF.write(s+'\n')
with open('_ndone_kraken2.tsv','w') as oF:
for s in kraken2ND: oF.write(s+'\n')
if cfg.get('PIPELINE','doSVFinder',fallback=0) == '1':
print (' -> SV FINDER DONE :',len(svDone),'samples [saving to _done_sv.tsv]')
print (' NOT :',len(svND),'samples [saving to _ndone_sv.tsv]')
with open('_done_sv.tsv','w') as oF:
for s in svDone: oF.write(s+'\n')
with open('_ndone_sv.tsv','w') as oF:
for s in svND: oF.write(s+'\n')
# > Binning Metawrap
if cfg.get('PIPELINE','doBinRefiningMWrap',fallback=0) == '1':
print (' -> BIN refining DONE :',len(binMwRefDone),'samples [saving to _done_bin_ref.tsv]')
print (' NOT :',len(binMwRefND),'samples [saving to _ndone_bin_ref.tsv]')
with open('_done_bin_ref.tsv','w') as oF:
for s in binMwRefDone: oF.write(s+'\n')
with open('_ndone_bin_ref.tsv','w') as oF:
for s in binMwRefND: oF.write(s+'\n')
# > Bin checkM
if cfg.get('PIPELINE','doBinAnnotationCheckM',fallback=0) == '1':
print (' -> BIN QC DONE :',len(binAnnotCmDone),'samples [saving to _done_bin_checkm.tsv]')
print (' NOT :',len(binAnnotCmND),'samples [saving to _ndone_bin_checkm.tsv]')
with open('_done_bin_checkm.tsv','w') as oF:
for s in binAnnotCmDone: oF.write(s+'\n')
with open('_ndone_bin_checkm.tsv','w') as oF:
for s in binAnnotCmND: oF.write(s+'\n')
# > Bin GTDBTK
if cfg.get('PIPELINE','doBinAnnotationGTDBK',fallback=0) == '1':
print (' -> BIN TAXONOMY DONE :',len(binAnnotGtDone),'samples [saving to _done_bin_gtdbtk.tsv]')
print (' NOT :',len(binAnnotGtND),'samples [saving to _ndone_bin_gtdbtk.tsv]')
with open('_done_bin_gtdbtk.tsv','w') as oF:
for s in binAnnotGtDone: oF.write(s+'\n')
with open('_ndone_bin_gtdbtk.tsv','w') as oF:
for s in binAnnotGtND: oF.write(s+'\n')
# > Bin QUANTIFICATION
if cfg.get('PIPELINE','doBinQuantification',fallback=0) == '1':
print (' -> BIN ABUNDANCE DONE:',len(binQuantifyDone),'samples [saving to _done_bin_abundance.tsv]')
print (' NOT :',len(binQuantifyND),'samples [saving to _ndone_bin_abundance.tsv]')
with open('_done_bin_abundance.tsv','w') as oF:
for s in binQuantifyDone: oF.write(s+'\n')
with open('_ndone_bin_abundance.tsv','w') as oF:
for s in binQuantifyND: oF.write(s+'\n')
# ================== END OF CHECKJOBS ========================
# ============================================================
# Result merger command:
# ============================================================
# - looks for sorted results in <tgt> [def = .]
# - merges data layers using appropriate tools
# (e.g. merge tables for metaphlan)
# =============================================================
if args.cmd == 'mergeresults':
inFolder = tgt
print (' > Executing "mergeresults" command')
print (' ==============================================================')
print (' WARNING: THIS COMMAND IS UNDER DEVELOPMENT AND MIGHT NOT WORK!')
print (' ==============================================================')
# load config
cfg = loadConfig()
with open('mergercommand.sh','w') as oF:
oF.write('#!/bin/bash\n')
oF.write('echo "MERGING RESULTS"\n')
oF.write('echo "==============="\n')
oF.write('echo ""\n')
# merge metaphlan 3
if cfg.get('PIPELINE','doMeta3') == '1':
if (os.path.isdir(tgt+'/metaphlan3')):
print ('prepping code for merging metaphlan3 results in '+tgt+'/metaphlan3')
oF.write('echo "MERGING METAPHLAN3 RESULTS"\n')
oF.write('echo "==========================================="\n')
oF.write('mkdir -p '+tgt+'/results_merged\n')
oF.write('echo "> initializing conda module"\n')
oF.write('module purge\n')
oF.write('module load '+cfg.get('MERGE_RESULTS','condaModule').strip()+' \n')
oF.write('echo "> cleaning conda env"\n')
oF.write('conda deactivate\n')
oF.write('echo "> loading biobakery conda"\n')
oF.write('conda activate '+cfg.get('MERGE_RESULTS','condaBiobakery').strip()+'\n')
oF.write('echo "> merging tables... "\n')
oF.write('merge_metaphlan_tables.py '+tgt+'/metaphlan3/*.txt > '+tgt+'/results_merged/results_metaphlan3.txt\n')
oF.write('conda deactivate\n')
oF.write('echo "================ DONE ====================="\n')
oF.write('echo ""\n')
else:
print ('WARNING: metaphlan3 results are not in '+tgt)
# merge metaphlan 4
if cfg.get('PIPELINE','doMeta4') == '1':
if (os.path.isdir(tgt+'/metaphlan4')):
print ('prepping code for merging metaphlan4 results in '+tgt+'/metaphlan4')
oF.write('echo "MERGING METAPHLAN4 RESULTS"\n')
oF.write('echo "==========================================="\n')
oF.write('mkdir -p '+tgt+'/results_merged\n')
oF.write('echo "> initializing conda module"\n')
oF.write('module purge\n')
oF.write('module load '+cfg.get('MERGE_RESULTS','condaModule').strip()+' \n')
oF.write('echo "> cleaning conda env"\n')
oF.write('conda deactivate\n')
oF.write('echo "> loading biobakery conda"\n')
oF.write('conda activate '+cfg.get('MERGE_RESULTS','condaBiobakery').strip()+'\n')
oF.write('echo "> merging tables... "\n')
oF.write('merge_metaphlan_tables.py '+tgt+'/metaphlan4/*.txt > '+tgt+'/results_merged/results_metaphlan4.txt\n')
oF.write('conda deactivate\n')
oF.write('echo "================ DONE ====================="\n')
oF.write('echo ""\n')
else:
print ('WARNING: metaphlan4 results are not in '+tgt)
# merge humann 3
# ===================================
if cfg.get('PIPELINE','doHumann3') == '1':
if (os.path.isdir(tgt+'/humann3')):
print ('prepping code for merging humann3 results in '+tgt+'/humann3')
oF.write('echo "MERGING HUMANN3 RESULTS"\n')
oF.write('echo "==========================================="\n')
oF.write('mkdir -p '+tgt+'/results_merged\n')
oF.write('echo "> initializing conda module"\n')
oF.write('module purge\n')
oF.write('module load '+cfg.get('MERGE_RESULTS','condaModule').strip()+' \n')
oF.write('echo "> cleaning conda env"\n')
oF.write('conda deactivate\n')
oF.write('echo "> loading biobakery conda"\n')
oF.write('conda activate '+cfg.get('MERGE_RESULTS','condaBiobakery').strip()+'\n')
oF.write('echo "> merging tables (gene families)... "\n')
oF.write('humann_join_tables -i '+tgt+'/humann3/gene_families/ -o '+tgt+'/results_merged/results_humann3_genefamilies.txt\n')
oF.write('echo "> merging tables (pathway abundances)... "\n')
oF.write('humann_join_tables -i '+tgt+'/humann3/path_abundances/ -o '+tgt+'/results_merged/results_humann3_pathway_abundances_metacyc.txt\n')
oF.write('echo "> merging tables (pathway coverage)... "\n')
oF.write('humann_join_tables -i '+tgt+'/humann3/path_coverage/ -o '+tgt+'/results_merged/results_humann3_pathway_coverages_metacyc.txt\n')
oF.write('conda deactivate\n')
oF.write('echo "================ DONE ====================="\n')
oF.write('echo ""\n')
else:
print ('WARNING: humann3 results are not in '+tgt)
# merge humann 3.6
# =============================================
if cfg.get('PIPELINE','doHumann3.6') == '1':
if (os.path.isdir(tgt+'/humann3.6')):
print ('prepping code for merging humann3.6 results in '+tgt+'/humann3.6')
oF.write('echo "MERGING HUMANN3.6 RESULTS"\n')
oF.write('echo "==========================================="\n')
oF.write('mkdir -p '+tgt+'/results_merged\n')
oF.write('echo "> initializing conda module"\n')
oF.write('module purge\n')
oF.write('module load '+cfg.get('MERGE_RESULTS','condaModule').strip()+' \n')
oF.write('echo "> cleaning conda env"\n')
oF.write('conda deactivate\n')
oF.write('echo "> loading biobakery conda"\n')
oF.write('conda activate '+cfg.get('MERGE_RESULTS','condaBiobakery').strip()+'\n')
oF.write('echo "> merging tables (gene families)... "\n')
oF.write('humann_join_tables -i '+tgt+'/humann3.6/gene_families/ -o '+tgt+'/results_merged/results_humann3.6_genefamilies.txt\n')
oF.write('echo "> merging tables (pathway abundances)... "\n')
oF.write('humann_join_tables -i '+tgt+'/humann3.6/path_abundances/ -o '+tgt+'/results_merged/results_humann3.6_pathway_abundances_metacyc.txt\n')
oF.write('echo "> merging tables (pathway coverage)... "\n')
oF.write('humann_join_tables -i '+tgt+'/humann3.6/path_coverage/ -o '+tgt+'/results_merged/results_humann3.6_pathway_coverages_metacyc.txt\n')
oF.write('conda deactivate\n')
oF.write('echo "================ DONE ====================="\n')
oF.write('echo ""\n')
else:
print ('WARNING: humann3.6 results are not in '+tgt)
# merge CARDs
if cfg.get('PIPELINE','doCARD_SB') == '1':
if (os.path.isdir(tgt+'/CARD_SB')):
print ('prepping code for merging shortbred (CARD) results in '+tgt+'/CARD_SB')
oF.write('echo "MERGING shortBRED CARD RESULTS"\n')
oF.write('echo "==========================================="\n')
oF.write('mkdir -p '+tgt+'/results_merged\n')
oF.write('echo "> initializing conda module"\n')
oF.write('module purge\n')
oF.write('module load '+cfg.get('MERGE_RESULTS','condaModule').strip()+' \n')
oF.write('echo "> cleaning conda env"\n')
oF.write('conda deactivate\n')
oF.write('echo "> loading biobakery conda"\n')
oF.write('conda activate '+cfg.get('MERGE_RESULTS','condaBiobakery').strip()+'\n')
oF.write('echo "> merging shortbred results... "\n')
oF.write('python '+cfg.get('MERGE_RESULTS','cardSbMerger')+' --infolder '+tgt+'/CARD_SB'+' --out '+tgt+'/results_merged/results_card_shortbred.txt'+'\n')
oF.write('conda deactivate\n')
oF.write('echo "================ DONE ====================="\n')
oF.write('echo ""\n')
else:
print ('WARNING: CARD shortbred results are not in '+tgt)
# merge VFs
if cfg.get('PIPELINE','doVFDB_SB') == '1':
if (os.path.isdir(tgt+'/VFDB_SB')):
print ('prepping code for merging shortbred (VFDB) results in '+tgt+'/VFDB_SB')
oF.write('echo "MERGING shortBRED VFDB RESULTS"\n')
oF.write('echo "==========================================="\n')
oF.write('mkdir -p '+tgt+'/results_merged\n')
oF.write('echo "> initializing conda module"\n')
oF.write('module purge\n')
oF.write('module load '+cfg.get('MERGE_RESULTS','condaModule').strip()+' \n')
oF.write('echo "> cleaning conda env"\n')
oF.write('conda deactivate\n')
oF.write('echo "> loading biobakery conda"\n')
oF.write('conda activate '+cfg.get('MERGE_RESULTS','condaBiobakery').strip()+'\n')
oF.write('echo "> merging shortbred results... "\n')
oF.write('python '+cfg.get('MERGE_RESULTS','vfdbSbMerger')+' --infolder '+tgt+'/VFDB_SB'+' --out '+tgt+'/results_merged/results_vfdb_shortbred.txt'+'\n')
oF.write('conda deactivate\n')
oF.write('echo "================ DONE ====================="\n')
oF.write('echo ""\n')
else:
print ('WARNING: VFDB shortbred results are not in '+tgt)
# merge kraken2
if cfg.get('PIPELINE','doKraken2') == '1':
if (os.path.isdir(tgt+'/kraken2')):
print ('prepping code for merging kraken2 results in '+tgt+'/kraken2')
oF.write('echo "MERGING KRAKEN2 and KRAKEN2-BRACKEN RESULTS"\n')
oF.write('echo "==========================================="\n')
oF.write('mkdir -p '+tgt+'/results_merged\n')
oF.write('echo "> initializing conda module"\n')
oF.write('module purge\n')
oF.write('module load '+cfg.get('MERGE_RESULTS','condaModule').strip()+' \n')
oF.write('echo "> cleaning conda env"\n')
oF.write('conda deactivate\n')
oF.write('echo "> loading biobakery conda"\n')
oF.write('conda activate '+cfg.get('MERGE_RESULTS','condaBiobakery').strip()+'\n')
oF.write('echo "> merging Kraken-Bracken results... "\n')
oF.write('merge_metaphlan_tables.py '+tgt+'/kraken2/*bracken.result.mp3.txt > '+tgt+'/results_merged/results_kraken2_bracken.txt\n')
#oF.write('echo "> merging Kraken-only results... "\n')
#oF.write('merge_metaphlan_tables.py '+tgt+'/kraken2/*.kreport.bc.mp3.txt > '+tgt+'/results_merged/results_kraken2.txt\n')
oF.write('conda deactivate\n')
oF.write('echo "================ DONE ====================="\n')
oF.write('echo ""\n')
else:
print ('WARNING: kraken2 results are not in '+tgt)
# merge kneaddata stats
if cfg.get('PIPELINE','doKnead') == '1':
if (os.path.isdir(tgt+'/kneaddata')):
print ('prepping code for merging kneaddata read statistics in '+tgt+'/kneaddata')
oF.write('echo "MERGING KNEADDATA REPORTS"\n')
oF.write('echo "==========================================="\n')
oF.write('mkdir -p '+tgt+'/results_merged\n')
oF.write('echo "> initializing conda module"\n')
oF.write('module purge\n')
oF.write('module load '+cfg.get('MERGE_RESULTS','condaModule').strip()+' \n')
oF.write('echo "> cleaning conda env"\n')
oF.write('conda deactivate\n')
oF.write('echo "> loading biobakery conda"\n')
oF.write('conda activate '+cfg.get('MERGE_RESULTS','condaBiobakery').strip()+'\n')
oF.write('echo "> merging tables... "\n')
oF.write('python '+cfg.get('MERGE_RESULTS','kneaddataMerger')+' --infolder '+tgt+'/kneaddata'+' --out '+tgt+'/results_merged/results_kneaddata_readdepth.csv'+'\n')
oF.write('conda deactivate\n')
oF.write('echo "================ DONE ====================="\n')
oF.write('echo ""\n')
else:
print ('WARNING: kneaddata results are not in '+tgt)
print (' code prep done, run: "bash ./mergercommand.sh" to start result merge')
# ============================================================
# Result sorter command:
# ============================================================
# - looks for pieces of completed jobs in <tgt> [def = .]
# - puts results into appropriate subfolders under --outPath
# example: all metaphlan results are put into outPath/metaphlan
# =============================================================
if args.cmd == 'sortresults':
outPath = args.outPath
outFolder = outPath
inFolder = tgt
print (' > Executing "sortresults" command')
if outPath == '.':
print ('ERROR: incorrect parameters: call with <tgt> --outPath; <tgt> = where to find results, --outPath = where to put results')
exit(1)
if not os.path.isdir(outPath):
print ('NOTE: folder',outPath,'does not exist, making it!')
try:
os.mkdir(outPath)
except:
print ('ERROR: making folder failed! Quitting!')
print (' --> output folders, making new ones as necessary')
# load config
cfg = loadConfig()
if cfg.get('PIPELINE','doHumann3',fallback=0) == '1':
if not os.path.isdir(outFolder+'/humann3'):
os.mkdir(outFolder+'/humann3')
if not os.path.isdir(outFolder+'/humann3/gene_families'):
os.mkdir(outFolder+'/humann3/gene_families')
if not os.path.isdir(outFolder+'/humann3/path_abundances'):
os.mkdir(outFolder+'/humann3/path_abundances')
if not os.path.isdir(outFolder+'/humann3/path_coverage'):
os.mkdir(outFolder+'/humann3/path_coverage')
if cfg.get('PIPELINE','doHumann3.6',fallback=0) == '1':
if not os.path.isdir(outFolder+'/humann3.6'):
os.mkdir(outFolder+'/humann3.6')
if not os.path.isdir(outFolder+'/humann3.6/gene_families'):
os.mkdir(outFolder+'/humann3.6/gene_families')
if not os.path.isdir(outFolder+'/humann3.6/path_abundances'):
os.mkdir(outFolder+'/humann3.6/path_abundances')
if not os.path.isdir(outFolder+'/humann3.6/path_coverage'):
os.mkdir(outFolder+'/humann3.6/path_coverage')
if cfg.get('PIPELINE','doQC',fallback=0) == '1':
if not os.path.isdir(outFolder+'/qc_preclean'):
os.mkdir(outFolder+'/qc_preclean')
if not os.path.isdir(outFolder+'/qc_postclean'):
os.mkdir(outFolder+'/qc_postclean')
if cfg.get('PIPELINE','doMeta3',fallback=0) == '1':
if not os.path.isdir(outFolder+'/metaphlan3'):
os.mkdir(outFolder+'/metaphlan3')
if cfg.get('PIPELINE','doMeta4',fallback=0) == '1':
if not os.path.isdir(outFolder+'/metaphlan4'):
os.mkdir(outFolder+'/metaphlan4')
if cfg.get('PIPELINE','doStrainPhlan3',fallback=0) == '1':
if not os.path.isdir(outFolder+'/strainphlan3'):
os.mkdir(outFolder+'/strainphlan3')
if cfg.get('PIPELINE','doStrainPhlan4',fallback=0) == '1':
if not os.path.isdir(outFolder+'/strainphlan4'):
os.mkdir(outFolder+'/strainphlan4')
if not os.path.isdir(outFolder+'/logs'):
os.mkdir(outFolder+'/logs')
if cfg.get('PIPELINE','doCARD_SB',fallback=0) == '1':
if not os.path.isdir(outFolder+'/CARD_SB'):
os.mkdir(outFolder+'/CARD_SB')
if cfg.get('PIPELINE','doVFDB_SB',fallback=0) == '1':
if not os.path.isdir(outFolder+'/VFDB_SB'):
os.mkdir(outFolder+'/VFDB_SB')
if cfg.get('PIPELINE','doKraken2',fallback=0) == '1':
if not os.path.isdir(outFolder+'/kraken2'):
os.mkdir(outFolder+'/kraken2')
if cfg.get('PIPELINE','doAssemblyMetaSpades',fallback=0) == '1':
if not os.path.isdir(outFolder+'/assembly_metaspades'):
os.mkdir(outFolder+'/assembly_metaspades')
if cfg.get('PIPELINE','doAssemblyMegaHit',fallback=0) == '1':
if not os.path.isdir(outFolder+'/assembly_megahit'):
os.mkdir(outFolder+'/assembly_megahit')
if cfg.get('PIPELINE','doKnead',fallback=0) == '1':
if not os.path.isdir(outFolder+'/kneaddata'):
os.mkdir(outFolder+'/kneaddata')
if cfg.get('PIPELINE','doSVFinder',fallback=0) == '1':
if not os.path.isdir(outFolder+'/SVfinder'):
os.mkdir(outFolder+'/SVfinder')
if cfg.get('PIPELINE','doBinRefiningMWrap',fallback=0) == '1' :
if not os.path.isdir(outFolder+'/bins_refined'):
os.mkdir(outFolder+'/bins_refined')
if cfg.get('PIPELINE','doBinAnnotationGTDBK',fallback=0) == '1':
if not os.path.isdir(outFolder+'/bins_refined_GTDBK'):
os.mkdir(outFolder+'/bins_refined_GTDBK')
if cfg.get('PIPELINE','doBinAnnotationCheckM',fallback=0) == '1':
if not os.path.isdir(outFolder+'/bins_refined_checkM'):
os.mkdir(outFolder+'/bins_refined_checkM')
if cfg.get('PIPELINE','doBinQuantification',fallback=0) == '1':
if not os.path.isdir(outFolder+'/bins_refined_quantified'):
os.mkdir(outFolder+'/bins_refined_quantified')
print (' --> Preparing to copy files')
c = 0
for l in os.listdir(inFolder):
if os.path.isdir(l) and not l == cfg.get('PIPELINE','jobOut'):
c+=1
print (' --> sorting ',l)
# sort metaphlan results
if cfg.get('PIPELINE','doMeta3',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/metaphlan3/*metaphlan.txt'):
shutil.copy2(f, outFolder+'/metaphlan3')
if cfg.get('PIPELINE','doMeta4',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/metaphlan4/*metaphlan.txt'):
shutil.copy2(f, outFolder+'/metaphlan4')
# sort strainphlan 3 results
if cfg.get('PIPELINE','doStrainPhlan3',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/strainphlan3/*.pkl'):
shutil.copy2(f, outFolder+'/strainphlan3')
# sort strainphlan 4 results
if cfg.get('PIPELINE','doStrainPhlan4',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/strainphlan4/*.pkl'):
shutil.copy2(f, outFolder+'/strainphlan4')
# sort QC
if cfg.get('PIPELINE','doQC',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/qc_preclean/*'):
shutil.copy2(f, outFolder+'/qc_preclean')
for f in glob.glob(inFolder+'/'+l+'/qc_postclean/*'):
shutil.copy2(f, outFolder+'/qc_postclean')
# sort HUMANN3:
if cfg.get('PIPELINE','doHumann3',fallback=0) == '1':
# sort humann gene families
for f in glob.glob(inFolder+'/'+l+'/humann3/*_genefamilies.tsv'):
shutil.copy2(f, outFolder+'/humann3/gene_families')
# sort humann gene pathway abundances
for f in glob.glob(inFolder+'/'+l+'/humann3/*_pathabundance.tsv'):
shutil.copy2(f, outFolder+'/humann3/path_abundances')
# sort humann gene pathway coverage
for f in glob.glob(inFolder+'/'+l+'/humann3/*_pathcoverage.tsv'):
shutil.copy2(f, outFolder+'/humann3/path_coverage')
if cfg.get('PIPELINE','doHumann3.6',fallback=0) == '1':
# sort humann gene families
for f in glob.glob(inFolder+'/'+l+'/humann3.6/*_genefamilies.tsv'):
shutil.copy2(f, outFolder+'/humann3.6/gene_families')
# sort humann gene pathway abundances
for f in glob.glob(inFolder+'/'+l+'/humann3.6/*_pathabundance.tsv'):
shutil.copy2(f, outFolder+'/humann3.6/path_abundances')
# sort humann gene pathway coverage
for f in glob.glob(inFolder+'/'+l+'/humann3.6/*_pathcoverage.tsv'):
shutil.copy2(f, outFolder+'/humann3.6/path_coverage')
# sort logs
for f in glob.glob(inFolder+'/'+l+'/*.log'):
shutil.copy2(f, outFolder+'/logs')
# kneaddata stats
if cfg.get('PIPELINE','doKnead',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/*_kneaddata_reads_stats.csv'):
shutil.copy2(f, outFolder+'/kneaddata')
# assembly [metaspades]
if cfg.get('PIPELINE','doAssemblyMetaSpades',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/assembly_metaspades/*scaffolds.fa'):
shutil.copy2(f, outFolder+'/assembly_metaspades')
# assembly [megahit]
if cfg.get('PIPELINE','doAssemblyMegaHit',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/assembly_megahit/*.fa'):
shutil.copy2(f, outFolder+'/assembly_megahit')
# VFs
if cfg.get('PIPELINE','doVFDB_SB',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/VFDB_SB/*.txt'):
shutil.copy2(f, outFolder+'/VFDB_SB')
# CARDs
if cfg.get('PIPELINE','doCARD_SB',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/CARD_SB/*.txt'):
shutil.copy2(f, outFolder+'/CARD_SB')
# KRAKEN2
if cfg.get('PIPELINE','doKraken2',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/kraken2/'+l+'*'):
shutil.copy2(f, outFolder+'/kraken2')
# SVs
if cfg.get('PIPELINE','doSVFinder',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/SVs/*.mapped.jsdel'):
shutil.copy2(f, outFolder+'/SVfinder')
# bins [refined]
if cfg.get('PIPELINE','doBinRefiningMWrap',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/bins_metawrap_refined/*.fa'):
if not os.path.isdir(outFolder+'/bins_refined/'+l):
os.mkdir(outFolder+'/bins_refined/'+l)
shutil.copy2(f, outFolder+'/bins_refined/'+l+'/')
# bins [quantification, GTDBK, checkM)
if cfg.get('PIPELINE','doBinAnnotationGTDBK',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/bins_GTDBK/'+l+'.gtdbk.bac120.summary.tsv'):
shutil.copy2(f, outFolder+'/bins_refined_GTDBK')
if cfg.get('PIPELINE','doBinAnnotationCheckM',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/bins_checkM/'+l+'*'):
shutil.copy2(f, outFolder+'/bins_refined_checkM')
if cfg.get('PIPELINE','doBinQuantification',fallback=0) == '1':
for f in glob.glob(inFolder+'/'+l+'/bins_quantification/'+l+'*'):
shutil.copy2(f, outFolder+'/bins_refined_quantified')
print ('ALL DONE, sorted through',c,'results!')
# =============================================================================================================
# =============================================================================================================
# MASTER JOB WRITER FUNCTION:
#
# - writes slurm jobs for each input file in --inPath which might have output files in --outPath
# -> if --inPath points to file, then it parses through this as if it was _datafile.tsv
# -> if it points to folder, then it makes jobs for fastq/fq OR fastq.gz/fq.gz OR .bam (pairs) in folder
# =============================================================================================================
# =============================================================================================================
if args.cmd == 'writejobs':
print (' >>> Executing "writejobs" command')
# check for config
cfg = loadConfig()
# BASIC PARAMETERS
# ============================