-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpamir.py
executable file
·1429 lines (1347 loc) · 66.8 KB
/
pamir.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
#!/usr/bin/env python
#786
import os, sys, errno, argparse, subprocess, fnmatch, ConfigParser, re
from time import time
#############################################################################################
# Class for colored texts and binary path
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class pipeline:
pamirpy = os.path.dirname(os.path.realpath(__file__)) + "/pamir.py"
pamir = os.path.dirname(os.path.realpath(__file__)) + "/pamir"
sga = os.path.dirname(os.path.realpath(__file__)) + "/sga.py"
minia = os.path.dirname(os.path.realpath(__file__)) + "/minia"
recalibrate = os.path.dirname(os.path.realpath(__file__)) + "/recalibrate"
pprocessor = os.path.dirname(os.path.realpath(__file__)) + "/partition_processor"
sortvcf = os.path.dirname(os.path.realpath(__file__)) + "/sort_vcf.py"
ext_sup = os.path.dirname(os.path.realpath(__file__)) + "/extract_support"
filtering = os.path.dirname(os.path.realpath(__file__)) + "/filtering.py"
gensetcov = os.path.dirname(os.path.realpath(__file__)) + "/generate_setcover_input.py"
smoother = os.path.dirname(os.path.realpath(__file__)) + "/smoother"
genotyping = os.path.dirname(os.path.realpath(__file__)) + "/genotyping.py"
filterbysetcover = os.path.dirname(os.path.realpath(__file__)) + "/filter_by_setcover.py"
sortfile = os.path.dirname(os.path.realpath(__file__)) + "/sort_file.pl"
clean = os.path.dirname(os.path.realpath(__file__)) + "/clean"
contaminantFinder = os.path.dirname(os.path.realpath(__file__)) + "/find_contaminations.py"
contaminantRemover = os.path.dirname(os.path.realpath(__file__)) + "/remove_contaminations.py"
workdir = os.path.dirname(os.path.realpath(__file__))
# example usage for help
example = "\tTo create a new project: specify (1) project name, (2) reference genomes and (3) input sequences (either --alignment, --fastq, or --mrsfast-best-search)\n"
example += "\n\t--starting from a sam or bam file\n"
example += "\t$ ./pamir.py -p my_project -r ref.fa --files alignment=my.sam\n"
example += "\n\t--starting from a gzipped fastq file\n"
example += "\t$ ./pamir.py -p my_project -r ref.fa --files fastq=my.fastq.gz\n"
example += "\n\t--starting from a remapping result\n"
example += "\t$ ./pamir.py -p my_project -r ref.fa --files mrsfast-best-search=my.best.sam\n"
example += "\n\t--starting with a masked file\n"
example += "\t$ ./pamir.py -p my_project -r ref.fa -m my-mask.txt --files alignment=my.sam\n"
example += "\n\n\tTo resume a project, just type project folder and \'--resume\'. pamir.py will automatically resume from the previous stages:\n"
example += "\t$ ./pamir.py -p my_project --resume\n"
example += "\t$ ./pamir.py -p /home/this/is/my/folder/project --resume\n"
#############################################################################################
# Default values will be set up later in check_proj_preq
def command_line_process():
parser = argparse.ArgumentParser(
description='Pamir: Insertion Discovery in Whole Genome Sequencing Data',
usage = pipeline.example,
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument('--project','-p',
required=True,
metavar='project',
help='The name of the project. Pamir creates the folder if it does not exist'
)
parser.add_argument('--dest','-d',
metavar='destination',
help='Directory that will be used for analysis. (default: ./)'
)
parser.add_argument('--splitandmap',
metavar='splitandmap',
help='Directory that will be used for mrsfast mapping. (default: ../)'
)
parser.add_argument('--reference','-r',
metavar='reference',
help='The path to the reference genome that should be used for analysis'
)
parser.add_argument('--assembler','-a',
metavar='assembler',
help='The type of the assembler for assembling orphans: sga/minia. (default:sga)'
)
parser.add_argument('--max-contig','-c',
type=int,
metavar='max_contig',
help="Maximum size of assembled contigs. (default: 25000)",
)
parser.add_argument('--donot-remove-contaminant',
action='store_true',
help='Remove contaminants by mapping them to BLAST NT library',
)
parser.add_argument('--mask-file', '-m',
help='The coordinates provided in this file will be masked from the reference genome.'
)
parser.add_argument('--invert-masker',
action='store_true',
help='The provided coordinates will be masked in the reference genome and ignored in mapping step. (dafault: False)',
)
parser.add_argument('--external-config', '-ec',
metavar='external_config',
help='The config file including external tools directories (default: pamir.config)',
)
parser.add_argument('--mrsfast-index-ws',
metavar='window_size',
help='Window size used by mrsFAST-Ultra for indexing the reference genome. (default: 12)',
)
parser.add_argument('--mrsfast-errors',
metavar='mrsfast_errors',
help='Number of the errors allowed by mrsFAST-Ultra for mapping (default: -1)',
)
parser.add_argument('--mrsfast-threads',
metavar='mrsfast_threads',
help='Number of the threads used by mrsFAST-Ultra for mapping (default: 1)',
)
parser.add_argument('--mrsfast-n',
metavar='mrsfast_n',
help='Maximum number of mapping loci of anchor of an OEA. Anchor with higher mapping location will be ignored. 0 for considering all mapping locations (default: 1)',
)
parser.add_argument('--mrsfast-min',
metavar='mrsfast_min',
help='Minimum insert size for mrsFAST-Ultra paired-end best mapping (default: -1)',
)
parser.add_argument('--mrsfast-max',
metavar='mrsfast_max',
help='Maximum insert size for mrsFAST-Ultra paired-end best mapping (default: -1)',
)
parser.add_argument('--resume',
nargs='?',
const="pamirpy",
help='Restart pipeline from the stage that has not been completed yet.',
)
parser.add_argument('--cluster',
metavar='cluster',
help='For input range x-y, report reads of cluster from x to y-1 and exit. Supported only in resume mode.',
)
parser.add_argument('--resume-force', '-f',
action='store_true',
help='Ignore existing files and restart the pipeline from stage specified in --resume.',
)
parser.add_argument('--num-worker',
type=int,
help='Number of independent prediction jobs to be created. (default: 1)',
)
parser.add_argument('--range',
help='Intervals of OEA clusters in partition to be analyzed.',
)
parser.add_argument('--worker-id',
help='Specific worker ID that the user wants to run prediction in the last stage. (default:-1, will run on all workers)',
)
parser.add_argument('--mode',
metavar='engine_mode',
help='Type for running indepedent jobs: normal, SGE, or PBS. (default: normal).',
default='normal'
)
parser.add_argument('--job-max-time',
help='Max job running time in PBS file. Read documents before changing its value! (default: 6 hour.)',
default='06:00:00'
)
parser.add_argument('--job-max-memory',
help='Max job memory in PBS file. Read documents before changing its value! (default: 16 GB.)',
default='16G'
)
parser.add_argument('--files',
metavar='files',
nargs='+',
type=str,
default=[]
)
return parser
#############################################################################################
### get the system shell to run the command and pipe stdout to control file when log==True
def log( msg, output=True ):
if output:
print "{0}".format(msg),
sys.stdout.flush()
#############################################################################################
### get the system shell to run the command and pipe stdout to control file when log==True
def logln( msg, output=True ):
if output:
print "{0}".format(msg)
sys.stdout.flush()
#############################################################################################
### get the system shell to run the command and pipe stdout to control file when log==True
def logOK():
logln(bcolors.OKGREEN+"OK"+bcolors.ENDC)
#############################################################################################
### get the system shell to run the command and pipe stdout to control file when log==True
def logWAR():
logln(bcolors.WARNING+"SKIPPING"+bcolors.ENDC)
#############################################################################################
### get the system shell to run the command and pipe stdout to control file when log==True
def logFAIL():
logln(bcolors.FAIL+"FAILED"+bcolors.ENDC)
#############################################################################################
### get the system shell to run the command and pipe stdout to control file when log==True
def shell(msg, run_command, command, control_file='', success_file='', success_msg='', shell_log=True):
if shell_log:
log("{0}...".format(msg))
if run_command and command =='':
print ""
if (not run_command):
logWAR()
return
if not shell_log or control_file == '':
p = subprocess.Popen(command, shell=True)
else:
f = open(control_file, 'w')
f.write("CMD:" + command + "\n")
f.flush() # so commands will show before actual progress
p = subprocess.Popen(command, shell=True, stdout=f, stderr=f)
ret = p.wait()
if shell_log and control_file != '':
f.close()
if ret != 0:
logFAIL()
raise subprocess.CalledProcessError(ret, command)
elif success_file != '':
with open (success_file, 'w') as suc_file:
suc_file.write(success_msg)
logOK()
#############################################################################################
########## Clean stage file for resuming
def clean_state_worker( workdir, config):
workdir = pipeline.workdir
stage_dir = workdir + "/stage/"
worker_stage_files = [f for f in os.listdir( stage_dir ) if (os.path.isfile( os.path.join( stage_dir , f)) and "30"==f[0:2])]
for item in worker_stage_files:
log(" + Removing " + item + "...")
os.remove( stage_dir + item)
logOK()
#############################################################################################
########## Clean stage file for resuming
def clean_state( mode_index, workdir, config ):
flag_clean = 0 # 1 only when we delete files due to changes in project.config
workdir = pipeline.workdir
#valid_state = [ '01.verify_sam', '02.mask', '03.mrsfast-index', '04.mrsfast.best', '05.remove_concordant', '06.mrsfast', '07.sorted', '08.oeaunm', '09.partition', '10.orphan_assembly', '10.orphan_assembly_2','11.index_orphan_ref','16.oea2orphan','17.split1_oea2orphan','18.split2_oea2orphan','19.concat_all_oea2orphan','20.all_oea2orphan_recal','21.insert_orphans_into_cluster','22.update_partition','23.concatenate_vcf','24.index_log','25.sort_vcf','26.filter_duplicate_calls','27.remove_partials','28.generate_loclen','normal']
valid_state = [ '01.verify_sam', '02.mask', '03.mrsfast-index', '04.mrsfast.best', '05.remove_concordant','normal']
for i in range( mode_index, len(valid_state)):
if ( 3==i and ""!=config.get("project","mrsfast-best-search")):
continue
if os.path.isfile(workdir + "/stage/" + valid_state[i] + ".finished" ):
#try:
if 0 == flag_clean:
logln("Removing old files due to change in project.config")
flag_clean = 1
log(" + Removing " + valid_state[i] + ".finished...")
os.remove( workdir + "/stage/" + valid_state[i] + ".finished" )
logOK()
# Clean all possible stages files of previous worker
clean_state_worker( workdir, config)
#except subprocess.CalledProcessError as e:
# print >>sys.stderr, "{0} failed with exit status {2} and message {1}".format(e.cmd, 'N/A', e.returncode)
#############################################################################################
###### Running commands for verify_sam
def verify_sam(config ):
#msg = "Extract softclip positions"
workdir = pipeline.workdir
input_file = "{0}/{1}".format(workdir, config.get("project","alignment"))
msg = "Sorting bam file"
output_file = "{0}/{1}".format(workdir, config.get("project","fastq"))
control_file = "{0}/log/01.verify_sam_1.log".format(workdir);
complete_file = "{0}/stage/01.verify_sam_1.finished".format(workdir);
freeze_arg = ""
cmd = pipeline.samtools + " sort -n -@ {1} -m 10G {0} {0}.sorted".format(input_file,config.get("project","num-worker"))
run_cmd = not (os.path.isfile(complete_file) )
shell( msg, run_cmd , cmd, control_file, complete_file, freeze_arg)
msg = "Extracting FASTQ from Alignment file"
input_file = "{0}/{1}.sorted.bam".format(workdir, config.get("project","alignment"))
output_file = "{0}/{1}".format(workdir, config.get("project","fastq"))
control_file = "{0}/log/01.verify_sam.log".format(workdir);
complete_file = "{0}/stage/01.verify_sam.finished".format(workdir);
freeze_arg = ""
# cmd = pipeline.bedtools + "bamtofastq -i {0}.sorted.bam -fq {1}/input_1.fq -fq2 {1}/input_2.fq".format(input_file,output_file)
cmd = pipeline.pamir + ' verify_sam {0} {1}'.format( input_file, output_file )
run_cmd = not (os.path.isfile(complete_file) )
shell( msg, run_cmd , cmd, control_file, complete_file, freeze_arg)
#############################################################################################
###### Running commands for mask
def mask(config):
msg = "Masking Reference Genome"
project_name = config.get("project", "name")
workdir = pipeline.workdir
input_file = "{0}/{1}".format(workdir, config.get("project","reference"))
mask_file = "{0}/{1}".format(workdir, config.get("pamir","mask-file"))
output_file = "{0}/{1}.masked".format(workdir, config.get("project","reference"))
mask_mode = "maski" if ( "True" == config.get("pamir","invert-masker") ) else "mask"
if ("maski" == mask_mode):
msg += " to Keep Only Regions Specified in File"
control_file = "{0}/log/02.mask.log".format(workdir);
complete_file = "{0}/stage/02.mask.finished".format(workdir);
freeze_arg = "invert" if "True" == config.get("pamir","invert-masker") else "mask"
cmd = pipeline.pamir +' {0} {1} {2} {3} 0'.format( mask_mode, mask_file, input_file, output_file )
run_cmd = not ( os.path.isfile(complete_file) and freeze_arg in open(complete_file).read())
if ( run_cmd ):
clean_state( 2, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
#############################################################################################
###### Running commands for each mode
def index(config):
msg = "Indexing the masked genome using mrsFAST-Ultra"
project_name = config.get("project", "name")
workdir = pipeline.workdir
input_file = "{0}/{1}.masked".format(workdir, config.get("project", "reference"))
control_file = "{0}/log/03.mrsfast-index.log".format(workdir);
complete_file = "{0}/stage/03.mrsfast-index.finished".format(workdir);
freeze_arg = "ws={0}".format(config.get("mrsfast", "window_size") )
cmd = pipeline.mrsfast + ' --index {0} --ws {1}'.format(input_file, config.get("mrsfast","window_size") )
run_cmd = not ( os.path.isfile(complete_file) and freeze_arg in open(complete_file).read())
if ( run_cmd ):
clean_state( 3, workdir, config)
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
#return msg, run_cmd, cmd, control_file, complete_file, freeze_arg
#############################################################################################
###### Running commands for each mode
def mrsfast_best_search(config):
workdir = pipeline.workdir
control_file = "{0}/log/01.mkdirbestsam.log".format(workdir)
complete_file = "{0}/stage/01.mkdirbestsam.log".format(workdir)
freeze_arg = ""
if( os.path.isdir("{0}/bestsam".format(workdir))):
cmd = "touch {0} {1}".format(control_file,complete_file)
else:
cmd = "mkdir {0}/bestsam/".format(workdir)
msg = "Making directory bestsam"
run_cmd = not (os.path.isfile(complete_file))
#if (run_cmd):
# clean_state(1,workdir,config)
shell( msg, run_cmd , cmd, control_file, complete_file, freeze_arg)
msg = "Mapping non-concordant reads using mrsFAST-Ultra"
index_file = "{0}/{1}.masked".format(workdir, config.get("project", "reference"))
input_file = "{0}/{1}".format(workdir, config.get("project","fastq"))
output_file = "{0}/bestsam/mrsfast.best.sam".format(workdir)
threads = config.get("mrsfast", "threads")
control_file = "{0}/log/04.mrsfast.best.log".format(workdir);
complete_file = "{0}/stage/04.mrsfast.best.finished".format(workdir);
freeze_arg = "ws={0}.error={1}".format(config.get("mrsfast", "window_size"), config.get("mrsfast", "errors"))
run_cmd = not ( os.path.isfile(complete_file) and freeze_arg in open(complete_file).read())
if run_cmd:
f = open(input_file,"r")
a = f.readline()
a = f.readline()
config.set("project","readlength", str(len(a.strip())))
cmd = pipeline.mrsfast +' --search {0} --threads {1} -n 0 --best --disable-sam-header --disable-nohits --pe --seq {2} --min {3} --max {4} --seqcomp -o {5}'.format(index_file, threads, input_file, config.get("mrsfast","min"), config.get("mrsfast","max"), output_file )
if(config.get("mrsfast","errors")!="-1"):
cmd+=" -e {0}".format(config.get("mrsfast","errors"))
#if ( run_cmd ):
# clean_state( 4, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
#############################################################################################
###### Running commands for remove_concordant
def remove_concordant(config,f):
workdir = pipeline.workdir
msg = "Extracting OEA and Orphan reads of {0}".format(f)
input_file = "{0}/mrsfast.best.sam".format(workdir)
config.set("project","readlength", str(len((open(input_file).readline()).split("\t")[10].strip())))
with open ( workdir +"/project.config", "w") as configFile:
config.write(configFile)
output_file = "{0}/".format(workdir)
control_file = "{0}/log/05.remove_concordant.log".format(workdir);
complete_file = "{0}/stage/05.remove_concordant.finished".format(workdir);
freeze_arg = ""
cmd = pipeline.pamir + ' remove_concordant {0} {1} 2 1 1'.format( input_file, output_file )
run_cmd = not (os.path.isfile(complete_file) )
if ( run_cmd ):
clean_state( 5, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
#############################################################################################
###### Running commands for mrsfast_search
def mrsfast_search(config ):
msg = "Mapping OEA reads to get anchor locations using mrsFAST-Ultra"
workdir = pipeline.workdir
ref_file = "{0}/{1}.masked".format(workdir, config.get("project", "reference"))
#input_file = "{0}/oea.mapped.fq".format(workdir)
output_file = "{0}/mrsfast.sam".format(workdir)
unmapped_file = "{0}/mrsfast.sam.unmap".format(workdir)
threads = config.get("mrsfast", "threads")
cutoff = config.get("mrsfast", "n")
input_file = "<(cat <(echo -ne @)"
for i in range(1, int(config.get("project","samplenum"))+1):
input_file = input_file + " {0}/{1}.oea.mapped.fq".format(workdir,str(i))
input_file=input_file+")"
control_file = "{0}/log/06.mrsfast.log".format(workdir);
complete_file = "{0}/stage/06.mrsfast.finished".format(workdir);
freeze_arg = "ws={0}.error={1}.cutoff={2}".format(config.get("mrsfast", "window_size"), config.get("mrsfast", "errors"), cutoff)
cmd = pipeline.mrsfast +' --search {0} --crop {4} --threads {1} -o {3} -u {2}'.format(ref_file, threads, unmapped_file, output_file, config.get("project", "readlength") )
if(config.get("mrsfast","errors")!="-1"):
cmd+=" -e {0}".format(config.get("mrsfast","errors"))
if ("0"!= cutoff):
cmd += " -n {0}".format(cutoff )
run_cmd = not ( os.path.isfile(complete_file) and freeze_arg in open(complete_file).read())
cmd+= " --seq {0}".format(input_file)
if ( run_cmd ):
clean_state( 6, workdir, config )
fname = "{0}/run_mrsfastsearch.sh".format(workdir)
f = open(fname,"w")
f.write("#!/bin/bash\n")
f.write(cmd+'\n')
f.close()
cmd = 'cat {0} | xargs -I CMD --max-procs=1 bash -c CMD'.format(fname)
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
###########################################################
# ref= os.path.abspath(workdir+"/"+config.get("project","reference"))+".masked"
# os.chdir(config.get("project","splitandmap"))
# cmd="./split_and_map.py -p {0}/multimap --files {0}/oea.mapped.fq -r {1} --chunksize 250000 --threads {2} --mrsfast {3} --mrsfast-threads 1 --mrsfast-min {4} --mrsfast-max {5} --mrsfast-crop {6}".format(workdir, ref, config.get("project","num-worker"),pipeline.mrsfast,config.get("mrsfast","min"),config.get("mrsfast","max"),config.get("project","readlength"))
# if(config.get("mrsfast","errors")!="-1"):
# cmd+=" --mrsfast-errors {0}".format(config.get("mrsfast","errors"))
# if(config.get("mrsfast","n")!="-1"):
# cmd+=" --mrsfast-n {0}".format(config.get("mrsfast","n"))
# run_cmd = not ( os.path.isfile(complete_file) and freeze_arg in open(complete_file).read())
# if ( run_cmd ):
# clean_state( 6, workdir, config )
# shell(msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
# cmd="mv {0}/multimap/multimap.sam {1}".format(workdir, output_file)
# os.chdir(str(os.path.abspath(os.path.dirname(pipeline.pamirpy))))
# control_file = "{0}/log/06_2.mvsam.log".format(workdir);
# complete_file = "{0}/stage/06_2.mvsam.finished".format(workdir);
# freeze_arg= ""
# msg = "Moving splitandmap sam to mrsfast.sam"
# run_cmd = not ( os.path.isfile(complete_file) and freeze_arg in open(complete_file).read())
# shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
###########################################################
###### Running commands for sorting all mapping output
def sort(config ):
msg = "Sorting the mapped mates"
project_name = config.get("project", "name")
workdir = pipeline.workdir
input_file = "{0}/mrsfast.sam".format( workdir )
output_file = "{0}/sorted.sam".format( workdir )
control_file = "{0}/log/07.sort.log".format( workdir );
complete_file = "{0}/stage/07.sort.finished".format( workdir );
freeze_arg = ""
cmd = pipeline.pamir +' sort {0} {1}'.format( input_file, output_file )
run_cmd = not (os.path.isfile(complete_file) )
shell( msg, run_cmd , cmd, control_file, complete_file, freeze_arg)
#############################################################################################
###### Assembling orphan reads into contigs
def orphan_assembly(config):
msg = "Creating Orphan contigs"
project_name = config.get("project", "name")
workdir = pipeline.workdir
control_file = "{0}/log/10.orphan_assembly.log".format(workdir);
complete_file = "{0}/stage/10.orphan_assembly.finished".format(workdir);
input_file = "<(cat"
for i in range(1, int(config.get("project","samplenum"))+1):
input_file = input_file + " {0}/{1}.orphan.fq".format(workdir,str(i))
input_file=input_file+")"
# input_file = "{0}/orphan.fq".format(workdir)
freeze_arg = ""
msg = "Creating Orphan contigs"
run_cmd = not (os.path.isfile(complete_file) )
#if ( run_cmd ):
# clean_state( 10, workdir, config )
cmd=""
if(config.get("project","assembler")=="velvet" and run_cmd):
cmd = pipeline.velveth + " {0}/velvetRes/ 31 -fastq -short {1}".format(workdir, input_file)
fname = "{0}/run_orphanassembly.sh".format(workdir)
f = open(fname,"w")
f.write("#!/bin/bash\n")
f.write(cmd+'\n')
f.close()
cmd = 'cat {0} | xargs -I CMD --max-procs=1 bash -c CMD'.format(fname)
shell(msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
control_file = "{0}/log/10.orphan_assembly_2.log".format(workdir);
complete_file = "{0}/stage/10.orphan_assembly_2.finished".format(workdir);
msg = "Running velvetg"
cmd = pipeline.velvetg + " {0}/velvetRes/ -min_contig_lgth 400 -cov_cutoff 2".format(workdir)
run_cmd = not (os.path.isfile(complete_file) )
shell(msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
contigfile = open("{0}/velvetRes/contigs.fa".format(workdir),"r").readlines()
fout = open("{0}/orphan.fq.contigs.fa".format(workdir),"w")
a=0
while a < len(contigfile):
content =""
if(contigfile[a][0]=='>'):
conname = contigfile[a].strip().split("_")
fout.write(">contig-"+conname[1]+"\n")
a+=1
while a < len(contigfile) and contigfile[a][0]!='>':
content+=contigfile[a].strip()
a+=1
fout.write(content+"\n")
fout.close()
elif(config.get("project","assembler")=="sga" and run_cmd):
cmd = "python " + pipeline.sga + " {0}".format(input_file)
shell(msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
elif(config.get("project","assembler")=="minia" and run_cmd):
cmd = "{0} -kmer-size 31 -abundance-min 3 -out {1}/orphan.fq_tmp -in {2}".format(pipeline.minia, workdir, input_file);
shell(msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
fin = open("{0}/orphan.fq_tmp.contigs.fa".format(workdir),"r")
fout = open("{0}/orphan.fq.contigs.fa".format(workdir),"w")
contigs = fin.readlines()
i =0
while i < len(contigs):
splitted=contigs[i].split("__")
if int(splitted[2]) > 400:
fout.write(">contig-"+splitted[0][1:]+ "\n" +contigs[i+1])
i+=2
fin.close()
fout.close()
#############################################################################################
###### Contamination removal orphan.contigs.fa.
def remove_contamination_orphan_contig(config):
workdir = pipeline.workdir
if config.get("project", "donot-remove-contaminant") == "True":
cmd = "cp {0}/orphan.fq.contigs.fa {0}/orphan.fq.contigs.wocontaminations.fa".format(workdir)
shell("Contaminations are not removed", True, cmd, "", "", "")
else:
msg = "Mapping orphan contigs onto BLAST NT database"
input_file = "{0}/orphan.fq.contigs.fa".format(workdir)
control_file = "{0}/log/11.blast_nt_orphan_contig.log".format(workdir);
complete_file = "{0}/stage/11.blast_nt_orphan_contig.finished".format(workdir);
freeze_arg = ""
NT = pipeline.blastdb + "/nt"
cmd = pipeline.blastn + ' -task megablast -query {0} -db {1} -num_threads 24 > {2}/orphan.fq.contigs.fa_2_NT.megablast'.format(input_file, NT, workdir)
run_cmd = not (os.path.isfile(complete_file) )
# if ( run_cmd ):
# clean_state( 11, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg )
msg = "Cleaning megablast output"
input_file = "{0}/orphan.fq.contigs.fa_2_NT.megablast".format(workdir)
control_file = "{0}/log/12.clean_orphancontig_megablast.log".format(workdir);
complete_file = "{0}/stage/12.clean_orphancontig_megablast.finished".format(workdir);
freeze_arg = ""
cmd = pipeline.clean + ' {0}/orphan.fq.contigs.fa_2_NT.megablast {0}/orphan.fq.contigs.fa_2_NT.clean'.format(workdir)
run_cmd = not (os.path.isfile(complete_file) )
# if ( run_cmd ):
# clean_state( 16, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg )
msg = "Finding contaminated contigs"
input_file = "{0}/orphan.fq.contigs.fa_2_NT.clean".format(workdir)
control_file = "{0}/log/13.find_contaminated_contigs.log".format(workdir);
complete_file = "{0}/stage/13.find_contaminated_contigs.finished".format(workdir);
freeze_arg = ""
cmd = "python " + pipeline.contaminantFinder + ' {0}/orphan.fq.contigs.fa_2_NT.clean {0}/orphan.fq.contigs.contaminations'.format(workdir)
run_cmd = not (os.path.isfile(complete_file) )
# if ( run_cmd ):
# clean_state( 16, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg )
msg = "Warning: Please check your orphan.fq.contigs.contaminations file if it includes anything other than a contamination. You can update the file and re-run the command with --resume"
cmd = "echo \"\""
control_file = "{0}/log/14.print_contamination_message.log".format(workdir);
complete_file = "{0}/stage/14.print_contamination_message.finished".format(workdir);
freeze_arg = ""
run_cmd = not(os.path.isfile(complete_file))
if run_cmd:
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg )
exit(1)
msg = "Excluding contaminated contigs"
contig_file = "{0}/orphan.fq.contigs.fa".format(workdir)
contamination_file = "{0}/orphan.fq.contigs.contaminations".format(workdir)
output_file = "{0}/orphan.fq.contigs.wocontamination.fa".format(workdir)
control_file = "{0}/log/15.remove_contaminations.log".format(workdir);
complete_file = "{0}/stage/15.remove_contaminations.finished".format(workdir);
freeze_arg = ""
cmd = "python " + pipeline.contaminantRemover + ' {0}/orphan.fq.contigs.contaminations {0}/orphan.fq.contigs.fa {0}/orphan.fq.contigs.wocontaminations.fa'.format(workdir)
run_cmd = not (os.path.isfile(complete_file) )
# if ( run_cmd ):
# clean_state( 16, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg )
#############################################################################################
###### Preparing necessary file for orphan.contigs.fa to make it a single line ref: Easier to map.
def prepare_orphan_contig(config):
msg = "Preparing orphan contigs for mapping"
workdir = pipeline.workdir
input_file = "{0}/orphan.fq.contigs.wocontaminations.fa".format(workdir)
output_file = "{0}/orphan.contigs.ref".format(workdir)
output_file2 = "{0}/orphan.contigs.single.ref".format(workdir)
coor_file = "{0}/orphan.contigs.single.coor".format(workdir)
f = open (input_file, "r")
fref = open(output_file, "w")
fsingleref = open(output_file2, "w")
fcoor = open(coor_file, "w")
line=f.readline()
fsingleref.write(">1\n")
cloc=1;
while(line!=""):
cname= line.split(" ")[0]
if cname.find('\n')==-1:
cname=cname+'\n'
fcoor.write(cname[1:len(cname)-1]+"\t"+str(cloc)+'\n')
fref.write(cname)
line=f.readline()
cloc+=len(line)-1
fref.write(line)
fsingleref.write(line)
line=f.readline()
f.close()
fref.close()
fsingleref.close()
fcoor.close()
msg = "Indexing orphan.contigs.single.ref"
cmd = pipeline.mrsfast + " --index {0}".format(output_file2)
control_file = "{0}/log/16.index_orphan_ref.log".format(workdir);
complete_file = "{0}/stage/16.index_orphan_ref.finished".format(workdir);
freeze_arg = ""
run_cmd = not (os.path.isfile(complete_file) )
# if ( run_cmd ):
# clean_state( 11, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
#############################################################################################
###### Map oea reads onto orphan contigs.
def oea_to_orphan(config):
msg = "Mapping oea reads onto orphan contigs"
workdir = pipeline.workdir
orphan_ref = "{0}/orphan.contigs.single.ref".format(workdir)
seq_fastq = "{0}/oea.unmapped.fq".format(workdir)
output_file = "{0}/oea2orphan.sam".format(workdir)
control_file = "{0}/log/17.oea2orphan.log".format(workdir);
complete_file = "{0}/stage/17.oea2orphan.finished".format(workdir);
freeze_arg = ""
unmapped_file = "<(cat"
for i in range(1, int(config.get("project","samplenum"))+1):
unmapped_file = unmapped_file + " {0}/{1}.oea.unmapped.fq".format(workdir,str(i))
unmapped_file=unmapped_file+")"
cmd = pipeline.mrsfast + ' --search {0} --crop {2} -o {3} -e 0 --disable-sam-header --threads {4} --seq {1}'.format(orphan_ref, unmapped_file, config.get("project","readlength"),output_file, config.get("mrsfast", "threads"))
run_cmd = not (os.path.isfile(complete_file) )
# if ( run_cmd ):
# clean_state( 16, workdir, config )
fname = "{0}/run_oea_to_orphan.sh".format(workdir)
f = open(fname,"w")
f.write("#!/bin/bash\n")
f.write(cmd+'\n')
f.close()
cmd = 'cat {0} | xargs -I CMD --max-procs=1 bash -c CMD'.format(fname)
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg )
################################################################################################
###### Map nohit oea reads as split reads onto orphan contigs.
def oea_to_orphan_split(config):
msg = "Mapping first readlength/2 of OEA reads as split onto orphan contigs"
workdir = pipeline.workdir
orphan_ref = "{0}/orphan.contigs.single.ref".format(workdir)
input_fastq = "{0}/oea2orphan.sam.nohit".format(workdir)
output_file = "{0}/split1_oea2orphan.sam".format(workdir)
control_file = "{0}/log/18.split1_oea2orphan.log".format(workdir);
complete_file = "{0}/stage/18.split1_oea2orphan.finished".format(workdir);
freeze_arg = ""
half = (int(config.get("project","readlength"))/2)
cmd = pipeline.mrsfast + ' --search {0} --seq {1} -o {2} --crop {3} -e 0 --disable-sam-header --threads {4}'.format(orphan_ref, input_fastq, output_file, str(half), config.get("mrsfast", "threads"))
run_cmd = not (os.path.isfile(complete_file) )
# if ( run_cmd ):
# clean_state( 17, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg )
msg = "Mapping last readlength/2 of OEA reads as split onto orphan contigs"
workdir = pipeline.workdir
orphan_ref = "{0}/orphan.contigs.single.ref".format(workdir)
input_fastq = "{0}/oea2orphan.sam.nohit".format(workdir)
output_file = "{0}/split2_oea2orphan.sam".format(workdir)
control_file = "{0}/log/19.split2_oea2orphan.log".format(workdir);
complete_file = "{0}/stage/19.split2_oea2orphan.finished".format(workdir);
freeze_arg = ""
cmd = pipeline.mrsfast + ' --search {0} --seq {1} -o {2} --tail-crop {3} -e 0 --disable-sam-header --threads {4}'.format(orphan_ref, input_fastq, output_file, str(half), config.get("mrsfast","threads"))
run_cmd = not (os.path.isfile(complete_file) )
# if ( run_cmd ):
# clean_state( 18, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg )
msg = "Concatenating all oea2orphan"
input_file1 = "{0}/split1_oea2orphan.sam".format(workdir)
input_file2 = "{0}/split2_oea2orphan.sam".format(workdir)
input_file3 = "{0}/oea2orphan.sam".format(workdir)
output_file = "{0}/all_oea2orphan.sam".format(workdir)
control_file = "{0}/log/20.concat_all_oea2orphan.log".format(workdir);
complete_file = "{0}/stage/20.concat_all_oea2orphan.finished".format(workdir);
freeze_arg = ""
cmd = "cat {0} {1} {2} > {3}".format(input_file1, input_file2, input_file3, output_file)
freeze_arg=""
run_cmd = not (os.path.isfile(complete_file) )
#if ( run_cmd ):
# clean_state( 19, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg )
#############################################################################################
###### Recalibrate all oea2orhan.sam.
def recalibrate_all_oea_to_orphan(config):
msg = "Recalibrating the all_oea_to_orphan.sam"
workdir = pipeline.workdir
input_file = "{0}/all_oea2orphan.sam".format(workdir)
output_file = "{0}/all_oea2orphan.recal.sam".format(workdir)
coor_file = "{0}/orphan.contigs.single.coor".format(workdir)
control_file = "{0}/log/21.all_oea2orphan_recal.log".format(workdir);
complete_file = "{0}/stage/21.all_oea2orphan_recal.finished".format(workdir);
freeze_arg = ""
cmd = "{0} {1} {2} {3}".format(pipeline.recalibrate, coor_file, input_file, output_file)
freeze_arg=""
run_cmd = not (os.path.isfile(complete_file) )
# if ( run_cmd ):
# clean_state( 20, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg )
#############################################################################################
###### Running commands for creating OEA clusters
def partition(config ):
msg = "Creating OEA clusters"
project_name = config.get("project", "name")
workdir = pipeline.workdir
input_file = "{0}/sorted.sam".format(workdir)
# unmapped_file = "{0}/oea.unmapped.fq".format(workdir )
output_file = "{0}/partitions".format(workdir )
control_file = "{0}/log/09.partition.log".format(workdir);
complete_file = "{0}/stage/09.partition.finished".format(workdir);
orphan_file = "{0}/orphan.contigs.ref".format(workdir);
oea2orphan = "{0}/all_oea2orphan.recal.sam".format(workdir);
freeze_arg = "1000"
clusterNumFile= "{0}/clusterNum".format(workdir)
unmapped_file = "<(cat"
for i in range(1, int(config.get("project","samplenum"))+1):
unmapped_file = unmapped_file + " {0}/{1}.oea.unmapped.fq".format(workdir,str(i))
unmapped_file=unmapped_file+")"
# cmd = pipeline.pamir + ' partition {0} {1} 1000 {2}'.format( input_file, output_file, unmapped_file)
cmd = pipeline.pamir + ' partition {0} {1} 1000 {2} {3} {4} > {5}'.format( input_file, output_file, orphan_file, oea2orphan, unmapped_file, clusterNumFile)
run_cmd = not (os.path.isfile(complete_file) )
if ( run_cmd ):
clean_state( 9, workdir, config )
fname = "{0}/run_partition.sh".format(workdir)
f = open(fname,"w")
f.write("#!/bin/bash\n")
f.write(cmd+'\n')
f.close()
cmd = 'cat {0} | xargs -I CMD --max-procs=1 bash -c CMD'.format(fname)
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
#############################################################################################
###### Insert orphan contigs into OEA clusters.
def orphans_into_oeacluster(config):
msg = "Inserting relevant orphan contigs into oea cluster"
workdir = pipeline.workdir
orphan_ref = "{0}/orphan.contigs.ref".format(workdir)
oea_to_orphan = "{0}/all_oea2orphan.recal.sam".format(workdir)
partition_file = "{0}/partitions".format(workdir)
upartition_file = "{0}/partitions_updated".format(workdir)
control_file = "{0}/log/22.insert_orphans_into_cluster.log".format(workdir)
complete_file = "{0}/stage/22.insert_orphans_into_cluster.finished".format(workdir)
clusterNumFile = "{0}/clusterNum".format(workdir);
freeze_arg = ""
cmd = pipeline.pprocessor + ' {0} {1} {2} {3} > {4}'.format(orphan_ref, oea_to_orphan, partition_file, upartition_file, clusterNumFile)
run_cmd = not (os.path.isfile(complete_file) )
if ( run_cmd ):
clean_state( 21, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg )
#############################################################################################
###### Print VCF header
def print_header(config):
msg = "Printing VCF header"
project_name = config.get("project", "name")
workdir = pipeline.workdir
header_file = "{0}/vcf_header".format(workdir)
ref_file = "{0}/{1}".format(workdir,config.get("project","reference"))
control_file = "{0}/log/23.printheader.log".format(workdir)
complete_file = "{0}/stage/23.printheader.finished".format(workdir)
cmd = pipeline.pamir + ' header {0} {1}'.format( header_file, ref_file)
run_cmd = not (os.path.isfile(complete_file))
if ( run_cmd ):
clean_state( 22, workdir, config )
freeze_arg = ""
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
######################################################################################
###### Assemble updated cluster
def update_partition(config ):
msg = "Assembling updated cluster"
project_name = config.get("project", "name")
workdir = pipeline.workdir
#input_file = "{0}/partitions_updated".format(workdir)
input_file = "{0}/partitions".format(workdir)
ref_file = "{0}/{1}".format(workdir,config.get("project","reference"))
control_file = "{0}/log/24.update_partition.log".format(workdir)
complete_file = "{0}/stage/24.update_partition.finished".format(workdir)
clusterNumFile= "{0}/clusterNum".format(workdir)
clusterNum=int(open(clusterNumFile).read())
perJob=clusterNum/int(config.get("project","num-worker"))
jobFileName= "{0}/run_pamir_assemble.sh".format(workdir)
f = open("{0}".format(jobFileName),"w")
f.write("#!/bin/bash\n")
i=0
workNum=1
while i<clusterNum:
output_file = "{0}/insertions.outx{1}".format(workdir,workNum)
output_log = "{0}/insertions.logx{1}".format(workdir,workNum)
cmd = pipeline.pamir + ' assemble {0} {1} {2}-{3} {4} 30000 {5} {7} > {6}'.format( input_file, ref_file, str(i),str(i+perJob), output_file, str(config.get("project","readlength")), output_log, workdir)
f.write(cmd+'\n')
i+=perJob
workNum+=1
f.close()
freeze_arg = ""
cmd = 'cat {0} | xargs -I CMD --max-procs={1} bash -c CMD'.format( jobFileName, config.get("project","num-worker"))
run_cmd = not (os.path.isfile(complete_file) )
if ( run_cmd ):
clean_state( 23, workdir, config )
shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
### concatenate outputs
i=1
msg = "Concatenating all vcf and files"
cmd="cat "
cmd2="cat "
while i< workNum:
cmd+="{0}/insertions.outx{1} ".format(workdir,i)
#cmd2+="{0}/insertions.logx{1} ".format(workdir,i)
i+=1
cmd+="> {0}/insertions.out".format(workdir)
cmd2+="> {0}/insertions.log".format(workdir)
cmdall=cmd#+";"+cmd2
freeze_arg=""
control_file = "{0}/log/25.concatenate_vcf.log".format(workdir)
complete_file = "{0}/stage/25.concatenate_vcf.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file) )
shell( msg, run_cmd, cmdall, control_file, complete_file, freeze_arg)
### index partitions_updated.log
msg = "Indexing log file"
cmd = pipeline.pamir + " index_log {0}/insertions.log".format(workdir)
freeze_arg=""
control_file = "{0}/log/26.index_log.log".format(workdir)
complete_file = "{0}/stage/26.index_log.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file) )
# shell( msg, run_cmd, cmd, control_file, complete_file, freeze_arg)
######################################################################################
#### sort the vcf and remove the duplications generate interleaved and paired fastq files from orphans and unmapped oeas.
def dupremoval_cleaning(config):
workdir = pipeline.workdir
freeze_arg=""
control_file = "{0}/log/27.sort_and_filter_duplicate_calls.log".format(workdir)
complete_file = "{0}/stage/27.sort_and_filter_duplicate_calls.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd = "python " + pipeline.sortvcf + " {0}/insertions.out {0}/insertions.out_wodups 1".format(workdir)
msg="Sorting VCF file and eliminating duplicated insertions"
shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
msg = "You can check output file now: insertions.out_wodups"
shell(msg,True,"")
control_file = "{0}/log/28.remove_partials.log".format(workdir)
complete_file = "{0}/stage/28.remove_partials.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd="rm {0}/insertions.outx* {0}/insertions.logx*".format(workdir)
msg="Deleting partial outputs"
shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
control_file = "{0}/log/29.vcf_insertions.log".format(workdir)
complete_file = "{0}/stage/29.vcf_insertions.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd="cat {0}/vcf_header {0}/insertions.out_wodups > {0}/insertions.vcf".format(workdir)
msg="Adding header to insertions"
shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
######################################################################################
#### filtering and postprocessing.
def post_processing(config):
workdir = pipeline.workdir
TLEN = 1000
freeze_arg=""
control_file = "{0}/log/30.filtering.log".format(workdir)
complete_file = "{0}/stage/30.filtering.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd = pipeline.filtering + " {0}/insertions.out_wodups {0}/{1}.masked {2} {3} {0} {4} {5} {6} {7}".format(workdir, config.get("project","reference"), config.get("mrsfast","min"), config.get("mrsfast","max"), str(TLEN),config.get("mrsfast","threads"),config.get("project","samplenum"), pipeline.mrsfast)
msg="Filtering insertion candidates"
shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
###### Make filtering output VCF
control_file = "{0}/log/31.vcf_filtering_output.log".format(workdir)
complete_file = "{0}/stage/31.vcf_filtering_output.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd = "cat {0}/vcf_header_f {0}/insertions.out_wodups_filtered > {0}/insertions_filtered.vcf".format(workdir)
msg="Making filtering output a VCF file"
shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
###### Prepare input file for setcover
control_file = "{0}/log/32.generate_set_cover_input.log".format(workdir)
complete_file = "{0}/stage/32.generate_set_cover_input.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd = pipeline.gensetcov + " {0}/insertions.out_wodups_filtered_for_setcov.sorted {0}/filtering/seq.mrsfast.recal.sam.sorted {0}/for_setcov".format(workdir)
msg="Preparing input file for setcover"
shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
###### Run setcover
control_file = "{0}/log/33.setcover.log".format(workdir)
complete_file = "{0}/stage/33.setcover.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd = pipeline.smoother + " {0}/for_setcov > {0}/from_setcov".format(workdir)
msg="Running setcover"
shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
###### Eliminate the ones removed by setcover
control_file = "{0}/log/34.filter_by_setcover.log".format(workdir)
complete_file = "{0}/stage/34.filter_by_setcover.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd = pipeline.filterbysetcover + " {0}/from_setcov {0}/insertions.out_wodups_filtered {0}/insertions.out_wodups_filtered_setcov".format(workdir)
msg="Filter removed calls by setcover"
shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
###### Grep PASS calls
control_file = "{0}/log/35.grepPASS.log".format(workdir)
complete_file = "{0}/stage/35.grepPASS.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd = "grep PASS {0}/insertions.out_wodups_filtered_setcov > {0}/insertions.out_wodups_filtered_setcov_PASS".format(workdir)
msg="Grep PASS calls"
shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
###### Sort setcover output
control_file = "{0}/log/36.sort_setcover.log".format(workdir)
complete_file = "{0}/stage/36.sort_setcover.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd = "perl " + pipeline.sortfile + " {0}/insertions.out_wodups_filtered_setcov_PASS".format(workdir)
msg="Sort setcover output"
shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
###### VCF setcover output
control_file = "{0}/log/37.vcf_setcover.log".format(workdir)
complete_file = "{0}/stage/37.vcf_setcover.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd = "cat {0}/vcf_header_f {0}/insertions.out_wodups_filtered_setcov_PASS > {0}/insertions_setcover.vcf".format(workdir)
msg="Make setcover output a VCF file"
shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
###### For evaluation purposes
fin = open("{0}/insertions.out_wodups_filtered_setcov_PASS.sorted".format(workdir),"r")
fout = open("{0}/insertions.out_wodups_filtered_setcov_PASS.sorted_loclen".format(workdir),"w")
for line in fin:
splittedline = line.split()
tmp = splittedline[7].split(";")
fout.write(splittedline[1]+"\t"+tmp[1].split("=")[1]+"\n")
fout.close()
###### Evaluate
control_file = "{0}/log/38.evaluate.log".format(workdir)
complete_file = "{0}/stage/38.evaluate.finished".format(workdir)
run_cmd = not (os.path.isfile(complete_file))
cmd = "z_py/eva2.py z_py/bp_solution.txt {0}/insertions.out_wodups_filtered_setcov_PASS.sorted_loclen".format(workdir)
msg="Evaluating"
#shell(msg,run_cmd,cmd,control_file,complete_file,freeze_arg)
#############################################################################################
###### Running commands for extracting clusters
def output_cluster(config, c_range ):
msg = "Extracting Clusters for range {0}".format(c_range)
workdir = pipeline.workdir
control_file = "{0}/log/output_cluster.log".format(workdir);
cmd = pipeline.pamir + " get_cluster {0}/partition {1}".format(workdir, c_range)
shell( msg, True, cmd, control_file, '', '')
#############################################################################################
def remove_concordant_for_each_bestsam(config):
workdir = pipeline.workdir
inputfiles = os.listdir(workdir+'/bestsam')
if not os.path.isfile("{0}/stage/05.all_remove_concordant.finished".format(workdir)):
cmd = "cat "
cmd2 = "cat "
cmd3 = "cat "
cmd4 = "cat "
a=1
for f in inputfiles:
if os.path.isfile("{0}/stage/05.remove_concordant.finished".format(workdir)):
os.system("rm {0}/stage/05.remove_concordant.finished".format(workdir))
if os.path.isfile("{0}/mrsfast.best.sam".format(workdir)):
os.system("rm {0}/mrsfast.best.sam".format(workdir))
if os.path.isfile(workdir+"/bestsam/"+f):
symlink_name(workdir+"/bestsam/"+f, workdir, "mrsfast.best.sam")
config.set("project", "mrsfast-best-search", os.path.abspath(f))
remove_concordant(config,f)
os.system("mv {0}/orphan.fq {0}/{1}.orphan.fq".format(workdir,a))
os.system("mv {0}/oea.mapped.fq {0}/{1}.oea.mapped.fq".format(workdir,a))
os.system("mv {0}/oea.unmapped.fq {0}/{1}.oea.unmapped.fq".format(workdir,a))
os.system("mv {0}/all_interleaved.fastq {0}/{1}.all_interleaved.fastq".format(workdir,a))
cmd+="{0}/{1}.orphan.fq ".format(workdir,a)
cmd2+="{0}/{1}.oea.mapped.fq ".format(workdir,a)
cmd3+="{0}/{1}.oea.unmapped.fq ".format(workdir,a)
cmd4+="{0}/{1}.all_interleaved.fastq ".format(workdir,a)
a+=1
cmd+= " > {0}/orphan.fq".format(workdir)
cmd2+= " > {0}/oea.mapped.fq".format(workdir)
cmd3+= " > {0}/oea.unmapped.fq".format(workdir)
cmd4+= " > {0}/all_interleaved.fastq".format(workdir)
#os.system(cmd)
#os.system(cmd2)
#os.system(cmd3)
#os.system(cmd4)
os.system("touch {0}/stage/05.all_remove_concordant.finished".format(workdir))
#############################################################################################
###### Running commands for each mode
def run_command(config, force=False):
s0=time()
verify_sam(config)
s1 = time()