forked from motu-tool/mOTUs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motus
executable file
·1802 lines (1520 loc) · 103 KB
/
motus
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
# ============================================================================ #
# motus - a tool for marker gene-based OTU (mOTU) profiling of metagenomes
#
# Authors: Alessio Milanese ([email protected]),
# Daniel R. Mende ([email protected]),
# Georg Zeller ([email protected]),
# Shinichi Sunagawa([email protected])
#
# Type "motus" for usage help
#
# LICENSE:
# motus - a tool for marker gene-based OTU (mOTU) profiling
# Copyright (C) 2018 A. Milanese, D. R. Mende, G. Zeller & S. Sunagawa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ============================================================================ #
import os
import sys
import argparse
import shlex
import shutil
import time
import subprocess
import glob
import multiprocessing
import tempfile
# position of the script -------------------------------------------------------
path_mOTUs = os.path.realpath(__file__)
path_array = path_mOTUs.split("/")
relative_path = "/".join(path_array[0:-1])
relative_path = relative_path + "/"
# check if setup.py has been ran already ---------------------------------------
if not(os.path.isdir(relative_path+'db_mOTU')):
sys.stderr.write("[E::main] Error: database has not been downloaded. Run setup.py before using the motus profiler\n\n")
sys.exit(1)
#-------------------------------------------------------------------------------
# tool version
path_info_version = relative_path + "db_mOTU/versions"
try:
location = open(path_info_version,'r')
versions = dict()
for line in location:
l = line.rstrip().split('\t')
if l[0] != "#":
versions[l[0]] = l[1]
location.close()
except:
sys.stderr.write("[E::main] Error loading file: "+path_info_version+"\n[E::main] Try to download again the motus profiler\n\n")
sys.exit(1)
version_tool = versions["motus"]
git_commit_id = "# git tag version "+version_tool
# add /bin to the path ---------------------------------------------------------
try:
if os.path.isdir(relative_path+'bin'):
sys.path.insert(0, relative_path+'bin')
else:
sys.stderr.write("[E::main] Error: "+relative_path+"bin directory is missing.\n[E::main] Download the motus profiler again\n\n")
sys.exit(1)
except:
sys.stderr.write("[E::main] Error: "+relative_path+"bin directory is missing.\n[E::main] Download the motus profiler again\n\n")
sys.exit(1)
try:
import map_genes_to_mOTUs as map_motu
except:
sys.stderr.write("[E::main] Error: fail to load the script: "+relative_path+"bin/map_genes_to_mOTUs.py\n[E::main] Download the motus profiler again\n\n")
sys.exit(1)
try:
import runBWA as runbwa
except ImportError:
sys.stderr.write("[E::main] Error: fail to load the script: "+relative_path+"bin/runBWA.py\n[E::main] Download the motus profiler again\n\n")
sys.exit(1)
try:
import runBWA_for_snv as runbwa_snv
except ImportError:
sys.stderr.write("[E::main] Error: fail to load the script: "+relative_path+"bin/runBWA_for_snv.py\n[E::main] Download the motus profiler again\n\n")
sys.exit(1)
try:
import map_mOTUs_to_LGs as map_lgs
except:
sys.stderr.write("[E::main] Error: fail to load the script: "+relative_path+"bin/map_mOTUs_to_LGs.py\n[E::main] Download the motus profiler again\n\n")
sys.exit(1)
try:
import append
except:
sys.stderr.write("[E::main] Error: fail to load the script: "+relative_path+"bin/append.py\n[E::main] Download the motus profiler again\n\n")
sys.exit(1)
try:
import msamtools_python as filter_sam
except:
sys.stderr.write("[E::main] Error: fail to load the script: "+relative_path+"bin/msamtools_python.py\n[E::main] Download the motus profiler again\n\n")
sys.exit(1)
try:
import motu_utilities
except:
sys.stderr.write("[E::main] Error: fail to load the script: "+relative_path+"bin/motu_utilities.py\n[E::main] Download the motus profiler again\n\n")
sys.exit(1)
try:
import PEfiltering
except:
sys.stderr.write("[E::main] Error: fail to load the script: "+relative_path+"bin/PEfiltering.py\n[E::main] Download the motus profiler again\n\n")
sys.exit(1)
# ------------------------------------------------------------------------------
# print the help informations
# ------------------------------------------------------------------------------
class CapitalisedHelpFormatter(argparse.HelpFormatter):
def add_usage(self, usage, actions, groups, prefix=None):
if prefix is None:
prefix = ''
return super(CapitalisedHelpFormatter, self).add_usage(usage, actions, groups, prefix)
def msg(name=None):
str_msg = '''
\00
Program: motus - a tool for marker gene-based OTU (mOTU) profiling
Version: '''+version_tool+'''
Usage: motus <command> [options]
Command:
-- Taxonomic profiling
profile Perform a taxonomic profiling (map_tax + calc_mgc + calc_motu)
merge Merge different taxonomic profiles to create a table
map_tax Map reads to the marker gene database
calc_mgc Aggregate reads from the same marker gene cluster (mgc)
calc_motu From a mgc abundance table, produce the mOTU abundance table
-- SNV calling
map_snv Map reads to create bam/sam file for snv calling
snv_call SNV calling using metaSNV
Type motus <command> to print the help for a specific command
'''
return str_msg
# ------------------------------------------------------------------------------
def print_menu_profile():
sys.stderr.write("\n")
sys.stderr.write("Usage: motus profile [options]\n\n")
sys.stderr.write("Input options:\n")
sys.stderr.write(" -f FILE[,FILE] input file(s) for reads in forward orientation, fastq formatted\n")
sys.stderr.write(" -r FILE[,FILE] input file(s) for reads in reverse orientation, fastq formatted\n")
sys.stderr.write(" -s FILE[,FILE] input file(s) for reads without mate, fastq formatted\n")
sys.stderr.write(" -n STR sample name\n")
sys.stderr.write(" -i FILE[,FILE] provide a sam or bam input file\n")
sys.stderr.write(" -m FILE provide a mgc reads count file\n\n")
sys.stderr.write("Output options:\n")
sys.stderr.write(" -o FILE output file name [stdout]\n")
sys.stderr.write(" -I FILE save the result of bwa in bam format (intermediate step) [None]\n")
sys.stderr.write(" -M FILE save the mgc reads count (intermediate step) [None]\n")
sys.stderr.write(" -e profile only reference species (ref_mOTUs)\n")
#sys.stderr.write(" -p visualize the specI ID instead of the species name\n") # alway visualize
sys.stderr.write(" -c print result as counts instead of relative abundances\n")
sys.stderr.write(" -p print NCBI id\n")
sys.stderr.write(" -u print the full name of the species\n")
sys.stderr.write(" -q print the full rank taxonomy\n")
sys.stderr.write(" -B print result in BIOM format\n")
sys.stderr.write(" -k STR taxonomic level [mOTU]\n")
sys.stderr.write(" Values: [kingdom, phylum, class, order, family, genus, mOTU]\n\n")
sys.stderr.write("Algorithm options:\n")
sys.stderr.write(" -g INT number of marker genes cutoff: 1=higher recall, 6=higher precision [3]\n")
sys.stderr.write(" -l INT min. length of alignment for the reads (number of nucleotides) [75]\n")
#sys.stderr.write(" -L FLOAT min. length of alignment for the reads (percentage of the average read length) [None]\n")
#sys.stderr.write(" -C INT number of cores for bwa (max one per lane) [1]\n")
sys.stderr.write(" -t INT number of threads [1]\n")
sys.stderr.write(" -v INT verbose level: 1=error, 2=warning, 3=message, 4+=debugging [3]\n")
sys.stderr.write(" -y STR type of read counts [insert.scaled_counts]\n")
sys.stderr.write(" Values: [base.coverage, insert.raw_counts, insert.scaled_counts]\n\n")
# ------------------------------------------------------------------------------
def print_menu_map_snv():
sys.stderr.write("\n")
sys.stderr.write("Usage: motus map_snv [options]\n\n")
sys.stderr.write("Input options:\n")
sys.stderr.write(" -f FILE[,FILE] input file(s) for reads in forward orientation, fastq formatted\n")
sys.stderr.write(" -r FILE[,FILE] input file(s) for reads in reverse orientation, fastq formatted\n")
sys.stderr.write(" -s FILE[,FILE] input file(s) for reads without mate, fastq formatted\n\n")
sys.stderr.write("Output options:\n")
sys.stderr.write(" -o FILE output bam file name [stdout]\n\n")
sys.stderr.write("Algorithm options:\n")
sys.stderr.write(" -l INT min. length of alignment for the reads (number of nucleotides) [75]\n")
#sys.stderr.write(" -C INT number of cores for bwa (max one per lane) [1]\n")
sys.stderr.write(" -t INT number of threads [1]\n")
sys.stderr.write(" -v INT verbose level: 1=error, 2=warning, 3=message, 4+=debugging [3]\n\n")
# ------------------------------------------------------------------------------
def print_menu_snv_call():
sys.stderr.write("\n")
sys.stderr.write("Usage: motus snv_call -d Directory -o Directory [options]\n\n")
sys.stderr.write("Input options:\n")
sys.stderr.write(" -d DIR Call metaSNV on all bam files in the directory. [Mandatory]\n")
sys.stderr.write(" -fb FLOAT Coverage breadth: minimal horizontal genome coverage percentage per sample per species. Default=80.0\n")
sys.stderr.write(" -fd FLOAT Coverage depth: minimal average vertical genome coverage per sample per species. Default=5.0\n")
sys.stderr.write(" -fm INT Minimum number of samples per species. Default=2\n")
sys.stderr.write(" -fp FLOAT FILTERING STEP II: Required proportion of informative samples (coverage non-zero) per position. Default=0.50\n")
sys.stderr.write(" -fc FLOAT FILTERING STEP II: Minimum coverage per position per sample per species. Default=5.0\n")
sys.stderr.write(" -t INT Number of threads. Default=1\n\n")
sys.stderr.write("Output options:\n")
sys.stderr.write(" -o DIR Output directory. Will fail if already exists. [Mandatory]\n\n")
sys.stderr.write("Algorithm options:\n")
sys.stderr.write(" -v INT Verbose level: 1=error, 2=warning, 3=message, 4+=debugging. Default=3\n\n")
# ------------------------------------------------------------------------------
def print_menu_bwa():
sys.stderr.write("\n")
sys.stderr.write("Usage: motus map_tax [options]\n\n")
sys.stderr.write("Input options:\n")
sys.stderr.write(" -f FILE[,FILE] input file(s) for reads in forward orientation, fastq formatted\n")
sys.stderr.write(" -r FILE[,FILE] input file(s) for reads in reverse orientation, fastq formatted\n")
sys.stderr.write(" -s FILE[,FILE] input file(s) for reads without mate, fastq formatted\n\n")
sys.stderr.write("Output options:\n")
sys.stderr.write(" -o FILE output file name [stdout]\n")
sys.stderr.write(" -b save the result of bwa in bam format\n\n")
sys.stderr.write("Algorithm options:\n")
sys.stderr.write(" -l INT min. length of alignment for the reads (number of nucleotides) [75]\n")
#sys.stderr.write(" -C INT number of cores for bwa (max one per lane) [1]\n")
sys.stderr.write(" -t INT number of threads [1]\n")
sys.stderr.write(" -v INT verbose level: 1=error, 2=warning, 3=message, 4+=debugging [3]\n\n")
# ------------------------------------------------------------------------------
def print_menu_map_genes():
sys.stderr.write("\n")
sys.stderr.write("Usage: motus calc_mgc [options]\n\n")
sys.stderr.write("Input options:\n")
sys.stderr.write(" -n STR sample name\n")
sys.stderr.write(" -i FILE[,FILE] provide a sam or bam input file (or list of files)\n\n")
sys.stderr.write("Output options:\n")
sys.stderr.write(" -o FILE output file name [stdout]\n\n")
sys.stderr.write("Algorithm options:\n")
sys.stderr.write(" -l INT min. length of alignment for the reads (number of nucleotides) [75]\n")
#sys.stderr.write(" -L FLOAT min. length of alignment for the reads (percentage of the average read length) [None]\n")
sys.stderr.write(" -v INT verbose level: 1=error, 2=warning, 3=message, 4+=debugging [3]\n")
sys.stderr.write(" -y STR type of read counts [insert.scaled_counts]\n")
sys.stderr.write(" Values: [base.coverage, insert.raw_counts, insert.scaled_counts]\n\n")
# ------------------------------------------------------------------------------
def print_menu_map_lgs():
sys.stderr.write("\n")
sys.stderr.write("Usage: motus calc_motu [options]\n\n")
sys.stderr.write("Input options:\n")
sys.stderr.write(" -n STR sample name\n")
sys.stderr.write(" -i FILE provide the mgc abundance table\n\n")
sys.stderr.write("Output options:\n")
sys.stderr.write(" -o FILE output file name [stdout]\n")
sys.stderr.write(" -e profile only reference species (ref_mOTUs)\n")
sys.stderr.write(" -B print result in BIOM format\n")
sys.stderr.write(" -c print result as counts instead of relative abundances\n")
sys.stderr.write(" -p print NCBI id\n")
sys.stderr.write(" -u print the full name of the species\n")
sys.stderr.write(" -q print the full rank taxonomy\n")
sys.stderr.write(" -k STR taxonomic level [mOTU]\n")
sys.stderr.write(" Values: [kingdom, phylum, class, order, family, genus, mOTU]\n\n")
sys.stderr.write("Algorithm options:\n")
sys.stderr.write(" -g INT number of marker genes cutoff: 1=higher recall, 6=higher precision [3]\n")
sys.stderr.write(" -v INT verbose level: 1=error, 2=warning, 3=message, 4+=debugging [3]\n\n")
# ------------------------------------------------------------------------------
def print_menu_append():
sys.stderr.write("\n")
sys.stderr.write("Usage: motus merge [options]\n\n")
sys.stderr.write("Input options:\n")
sys.stderr.write(" -i STR[,STR] list of files (comma separated)\n")
sys.stderr.write(" -d DIR merge all the files in the directory\n\n")
sys.stderr.write("Output options:\n")
sys.stderr.write(" -o FILE output file name [stdout]\n")
sys.stderr.write(" -B print result in BIOM format\n\n")
sys.stderr.write("Algorithm options:\n")
sys.stderr.write(" -v INT verbose level: 1=error, 2=warning, 3=message, 4+=debugging [3]\n\n")
# ------------------------------------------------------------------------------
# function for multiple cores for bwa
def run_bwa_multiple_cores(forward_reads, reverse_reads, single_reads, reference, threads, output, bamOutput, msam_script, technology, verbose, profile_mode, lane_id, result,default_min_len_align_length_map_tax):
result1 = runbwa.runBWAmapping( forward_reads, reverse_reads, single_reads, reference, threads, output, bamOutput, msam_script, technology, verbose, profile_mode, lane_id,default_min_len_align_length_map_tax)
for i in result1:
result.append(i)
def prepare_output_bwa(output,outPutIsBam,header_file,all_sam_lines,str_end_header,verbose,str_info_min_len,str_perc_id,str_min_perc_query):
if not(is_tool("samtools")) and outPutIsBam:
sys.stderr.write(" [E::map_db] Error: samtools is not in the path. Saving .sam format instead of .bam format\n")
outPutIsBam = False
error_save_intermediate_bam_file = False
# load the header
try:
h_file = open(header_file,'r')
except:
sys.stderr.write("[E::map_db] Error loading file: "+header_file+"\n[E::map_db] Try to download again the motus profiler\n\n")
sys.exit(1)
# create the temp sam file with the result
try:
if output != "" or (output == "" and outPutIsBam):
sam_temp_file = tempfile.NamedTemporaryFile(delete=False, mode="w")
os.chmod(sam_temp_file.name, 0o644)
else:
sam_temp_file = sys.stdout
if verbose>4: sys.stderr.write(" \n[map_db] saving intermediate sam file in "+sam_temp_file.name + "\n")
sam_temp_file.write(str_end_header)
sam_temp_file.write(str_info_min_len)
sam_temp_file.write(str_perc_id)
sam_temp_file.write(str_min_perc_query)
for i in h_file:
sam_temp_file.write(i)
for i in all_sam_lines:
sam_temp_file.write(i)
except:
sys.stderr.write(" [W::map_db] Warning: failed to save intermediate sam file\n")
if verbose>4: sys.stderr.write("Error when saving the intermediate sam file\n")
error_save_intermediate_bam_file = True
sys.exit(1)
# close the sam file
if output == "" and (not outPutIsBam):
return 0
else:
try:
sam_temp_file.flush()
os.fsync(sam_temp_file.fileno())
sam_temp_file.close()
except:
if verbose>4: sys.stderr.write("Error when closing sam file\n")
if not error_save_intermediate_bam_file:
sys.stderr.write(" [W::map_db] Warning: failed to save intermediate bam file\n")
sys.exit(1)
#IF WE RETURN SAM
if not(outPutIsBam):
# move the temp file to the final destination
try:
#os.rename(bam_temp_file.name,args.profile_bam_file) # atomic operation
shutil.move(sam_temp_file.name,output) #It is not atomic if the files are on different filsystems.
except:
if verbose>4: sys.stderr.write("Error when copying intermediate sam to the final destination\n")
if not error_save_intermediate_bam_file:
sys.stderr.write("[E::map_db] Error: failed to save the sam file\n")
sys.stderr.write("[E::map_db] you can find the file here:\n"+sam_temp_file.name+"\n")
sys.exit(1)
return 0
#IF WE RETURN BAM
try:
if output != "":
bam_temp_file = tempfile.NamedTemporaryFile(delete=False, mode="w")
os.chmod(bam_temp_file.name, 0o644)
else:
bam_temp_file = sys.stdout
convertCMD = "samtools view -b -Sh "+ sam_temp_file.name
convert_popenCMD = shlex.split(convertCMD)
convert_cmd = subprocess.Popen(convert_popenCMD,stdout=bam_temp_file,)
stdout_s,stderr_s = convert_cmd.communicate()
if convert_cmd.returncode:
if not error_save_intermediate_bam_file:
sys.stderr.write("[E::map_db] Error: failed to save intermediate bam file\n")
sys.stderr.write(stderr_s.decode('ascii'))
sys.exit(1)
except:
if verbose>4: sys.stderr.write("Error when converting to bam\n")
if not error_save_intermediate_bam_file:
sys.stderr.write(" [W::map_db] Warning: failed to save intermediate bam file\n")
sys.exit(1)
# move the temp file to the final destination
if output != "":
try:
#os.rename(bam_temp_file.name,args.profile_bam_file) # atomic operation
shutil.move(bam_temp_file.name,output) #It is not atomic if the files are on different filsystems.
except:
if verbose>4: sys.stderr.write("Error when copying intermediate bam to the final destination\n")
if not error_save_intermediate_bam_file:
sys.stderr.write("[E::map_db] Error: failed to save intermediate bam file\n")
sys.stderr.write("[E::map_db] you can find the file here:\n"+bam_temp_file.name+"\n")
# remove the temporary sam file
os.remove(sam_temp_file.name)
sys.exit(1)
# remove the temporary sam file
os.remove(sam_temp_file.name)
return 0
# ------------------------------------------------------------------------------
# function to check if a specific tool exists
def is_tool(name):
try:
devnull = open(os.devnull)
subprocess.Popen([name], stdout=devnull, stderr=devnull).communicate()
except OSError as e:
if e.errno == os.errno.ENOENT:
return False
return True
# ------------------------------------------------------------------------------
# MAIN
# ------------------------------------------------------------------------------
def main(argv=None):
motu_call = "python "+(" ".join(sys.argv))
parser = argparse.ArgumentParser(usage=msg(), formatter_class=CapitalisedHelpFormatter,add_help=False)
#parser = argparse.ArgumentParser(description='This program calculates mOTU-LGs and specI abundances for one sample', add_help = True)
parser.add_argument('command', action="store", default=None, help='mode to use the mOTU tool',choices=['profile','map_tax','calc_mgc','calc_motu','merge','map_snv','snv_call'])
parser.add_argument('-f', action="store", default=None,dest='forwardReads', help='name of input file for reads in forward orientation, fastq formatted, can be gzipped')
parser.add_argument('-r', action="store", default=None,dest='reverseReads', help='name of input file for reads in reverse orientation, fastq formatted, can be gzipped')
parser.add_argument('-s', action="store", default=None,dest='singleReads', help='name of input file for reads without mate, fastq formatted, can be gzipped')
parser.add_argument('-o', action="store", dest='output', default=None, help='name of output file')
parser.add_argument('-t', type=int, action="store", dest='threads', default=None, help='Number of threads to be used.')
parser.add_argument('-e', action='store_true', default=None, dest='onlySpecI', help='Set if you want to profile only specI (mOTU-LGs will go in -1)')
parser.add_argument('-b', action="store_true", default=None, dest='outPutIsBam', help='Specify if the final output should be a bam formatted file')
parser.add_argument('-y', action="store", dest='type_output', default=None, help='type of output that you want to print',choices=['base.coverage', 'insert.raw_counts', 'insert.scaled_counts'])
parser.add_argument('-n', action="store", dest='sampleName', default=None, help='sample name for the current mapping')
parser.add_argument('-i', action="store", dest='listInputFiles', default=None, help='name of input file(s); sam or bam formatted files. If it is a list: insert all files separated by a comma')
parser.add_argument('-m', action="store", dest='motu_read_counts_file', default=None, help='name of input file; for profiling. It is the file of mOTU read count')
parser.add_argument('-v', action='store', type=int, default=None, dest='verbose', help='Verbose levels')
parser.add_argument('-d', action="store", default=None, dest='directory_append', help='directory from where to take the files to append')
parser.add_argument('-B', action='store_true', default=None, dest='BIOM_output', help='print output in BIOM format')
parser.add_argument('-c', action='store_true', default=None, dest='print_rel_ab', help='print result as count instead of relative abundance')
parser.add_argument('-u', action='store_true', default=None, dest='print_long_names', help='print names of the species in long format')
parser.add_argument('-q', action='store_true', default=None, dest='print_full_rank', help='print the full rank of the taxonomy')
parser.add_argument('-p', action='store_true', default=None, dest='print_NCBI_id', help='print NCBI id')
parser.add_argument('-k', action="store", default=None, dest='taxonomic_level', help='Taxonomic level for the profiling',choices=['kingdom', 'phylum', 'class', 'order', 'family', 'genus', 'mOTU'])
parser.add_argument('-I', action="store", default=None, dest='profile_bam_file', help='name of the bam file to save the intermediate bam file during profiling')
parser.add_argument('-M', action="store", default=None, dest='profile_mOTU_reads_file', help='name of the output file when profiling, relative to the mOTU read counts')
parser.add_argument('-g', type=int, action="store", dest='map_lgs_cutoff', default=None, help='cutoff when profiling for the number of genes')
parser.add_argument('-l', type=int, action="store", dest='min_len_align_length', default=None, help='Minimum length of the alignment')
#parser.add_argument('-L', type=float, action="store", dest='min_len_align_perc', default=None, help='Minimum length of the alignment, as percentage of the average read length')
# develope, but to be checked
parser.add_argument('-C', type=int, action="store", dest='number_of_cores', default=None, help='Number of cores to use for bwa')
# db has to be deleted
#parser.add_argument('-db', action="store", default=None, dest='db', help='database of marker genes',choices=['nr', 'aug.cen', 'cen'])
parser.add_argument('-save_sam_lines', action="store", dest='save_sam_lines', default=None, help='name of output file')
parser.add_argument('-load_sam_lines', action="store", dest='load_sam_lines', default=None, help='name of output file')
parser.add_argument('-min_perc_id', action="store", default=None, dest='min_perc_id', help='minimum percentage of identity when filtering - choose between 97 and 100')
parser.add_argument('-min_clip_length', action="store", default=None, dest='min_clip_length', help='min. length of alignment when clipped')
parser.add_argument('-min_perc_align', action="store", default=None, dest='min_perc_align', help='min. percent of the query that must be aligned, between 0 and 100')
parser.add_argument('-fb', metavar='FLOAT', type=float, default=None,
help="Coverage breadth: minimal horizontal genome coverage percentage per sample per species")
parser.add_argument('-fd', metavar='FLOAT', type=float, default=None,
help="Coverage depth: minimal average vertical genome coverage per sample per species")
parser.add_argument('-fm', metavar='INT', type=int, help="Minimum number of samples per species", default=None)
parser.add_argument('-fc', metavar='FLOAT', type=float,
help="FILTERING STEP II:"
"minimum coverage per position per sample per species", default=None)
parser.add_argument('-fp', metavar='FLOAT', type=float,
help="FILTERING STEP II:"
"required proportion of informative samples (coverage non-zero) per position",
default=None)
args = parser.parse_args()
# print menus ----------------------------------------------------------------
if (args.forwardReads is None) and (args.reverseReads is None) and (args.singleReads is None) and (args.output is None) and (args.threads is None) and (args.onlySpecI is None) and (args.outPutIsBam is None):
if (args.type_output is None) and (args.sampleName is None) and (args.listInputFiles is None) and (args.motu_read_counts_file is None) and (args.verbose is None) and (args.directory_append is None):
if (args.BIOM_output is None) and (args.taxonomic_level is None) and (args.profile_bam_file is None) and (args.profile_mOTU_reads_file is None) and (args.map_lgs_cutoff is None) and (args.number_of_cores is None):
if (args.print_rel_ab is None) and (args.print_NCBI_id is None) and (args.print_long_names is None) and (args.fb is None) and (args.fd is None) and (args.fm is None) and (args.fc is None) and (args.fp is None):
if args.command == 'profile': print_menu_profile()
if args.command == 'map_snv': print_menu_map_snv()
if args.command == 'map_tax': print_menu_bwa()
if args.command == 'calc_mgc': print_menu_map_genes()
if args.command == 'calc_motu': print_menu_map_lgs()
if args.command == 'merge': print_menu_append()
if args.command == 'snv_call': print_menu_snv_call()
sys.exit(1)
# set default for args.verbose
if (args.verbose is None): args.verbose = 3
# print parameters that are ignored -------------------------------------------
if args.command == 'profile':
if ((args.outPutIsBam is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -b ignored\n")
if ((args.directory_append is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -d ignored\n")
if ((args.fb is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fb ignored\n")
if ((args.fd is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fd ignored\n")
if ((args.fm is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fm ignored\n")
if ((args.fp is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fp ignored\n")
if ((args.fc is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fc ignored\n")
if args.command == 'map_tax':
if ((args.directory_append is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -d ignored\n")
if ((args.onlySpecI is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -e ignored\n")
if ((args.type_output is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -y ignored\n")
if ((args.sampleName is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -n ignored\n")
if ((args.listInputFiles is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -i ignored\n")
if ((args.motu_read_counts_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -m ignored\n")
if ((args.BIOM_output is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -B ignored\n")
if ((args.print_rel_ab is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -c ignored\n")
if ((args.print_full_rank is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -q ignored\n")
if ((args.print_NCBI_id is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -p ignored\n")
if ((args.print_long_names is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -u ignored\n")
if ((args.taxonomic_level is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -k ignored\n")
if ((args.profile_bam_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -I ignored\n")
if ((args.profile_mOTU_reads_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -M ignored\n")
if ((args.map_lgs_cutoff is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -g ignored\n")
if ((args.fb is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fb ignored\n")
if ((args.fd is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fd ignored\n")
if ((args.fm is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fm ignored\n")
if ((args.fp is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fp ignored\n")
if ((args.fc is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fc ignored\n")
if args.command == 'merge':
if ((args.onlySpecI is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -e ignored\n")
if ((args.type_output is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -y ignored\n")
if ((args.sampleName is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -n ignored\n")
if ((args.motu_read_counts_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -m ignored\n")
if ((args.print_rel_ab is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -c ignored\n")
if ((args.print_full_rank is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -q ignored\n")
if ((args.print_NCBI_id is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -p ignored\n")
if ((args.print_long_names is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -u ignored\n")
if ((args.taxonomic_level is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -k ignored\n")
if ((args.profile_bam_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -I ignored\n")
if ((args.profile_mOTU_reads_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -M ignored\n")
if ((args.map_lgs_cutoff is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -g ignored\n")
if ((args.forwardReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -f ignored\n")
if ((args.reverseReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -r ignored\n")
if ((args.singleReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -s ignored\n")
if ((args.threads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -t ignored\n")
if ((args.outPutIsBam is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -b ignored\n")
if ((args.min_len_align_length is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -l ignored\n")
if ((args.fb is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fb ignored\n")
if ((args.fd is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fd ignored\n")
if ((args.fm is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fm ignored\n")
if ((args.fp is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fp ignored\n")
if ((args.fc is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fc ignored\n")
if args.command == 'map_snv':
if ((args.directory_append is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -d ignored\n")
if ((args.onlySpecI is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -e ignored\n")
if ((args.type_output is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -y ignored\n")
if ((args.sampleName is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -n ignored\n")
if ((args.listInputFiles is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -i ignored\n")
if ((args.motu_read_counts_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -m ignored\n")
if ((args.BIOM_output is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -B ignored\n")
if ((args.print_rel_ab is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -c ignored\n")
if ((args.print_full_rank is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -q ignored\n")
if ((args.print_NCBI_id is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -p ignored\n")
if ((args.print_long_names is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -u ignored\n")
if ((args.taxonomic_level is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -k ignored\n")
if ((args.profile_bam_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -I ignored\n")
if ((args.profile_mOTU_reads_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -M ignored\n")
if ((args.map_lgs_cutoff is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -g ignored\n")
if ((args.outPutIsBam is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -b ignored\n")
if ((args.fb is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fb ignored\n")
if ((args.fd is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fd ignored\n")
if ((args.fm is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fm ignored\n")
if ((args.fp is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fp ignored\n")
if ((args.fc is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fc ignored\n")
if args.command == 'calc_mgc':
if ((args.outPutIsBam is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -b ignored\n")
if ((args.directory_append is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -d ignored\n")
if ((args.forwardReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -f ignored\n")
if ((args.reverseReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -r ignored\n")
if ((args.singleReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -s ignored\n")
if ((args.threads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -t ignored\n")
if ((args.onlySpecI is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -e ignored\n")
if ((args.motu_read_counts_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -m ignored\n")
if ((args.BIOM_output is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -B ignored\n")
if ((args.print_rel_ab is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -c ignored\n")
if ((args.print_full_rank is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -q ignored\n")
if ((args.print_NCBI_id is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -p ignored\n")
if ((args.print_long_names is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -u ignored\n")
if ((args.taxonomic_level is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -k ignored\n")
if ((args.profile_bam_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -I ignored\n")
if ((args.profile_mOTU_reads_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -M ignored\n")
if ((args.map_lgs_cutoff is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -g ignored\n")
if ((args.fb is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fb ignored\n")
if ((args.fd is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fd ignored\n")
if ((args.fm is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fm ignored\n")
if ((args.fp is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fp ignored\n")
if ((args.fc is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fc ignored\n")
if args.command == 'calc_motu':
if ((args.outPutIsBam is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -b ignored\n")
if ((args.directory_append is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -d ignored\n")
if ((args.profile_bam_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -I ignored\n")
if ((args.profile_mOTU_reads_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -M ignored\n")
if ((args.min_len_align_length is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -l ignored\n")
if ((args.forwardReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -f ignored\n")
if ((args.reverseReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -r ignored\n")
if ((args.singleReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -s ignored\n")
if ((args.threads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -t ignored\n")
if ((args.type_output is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -y ignored\n")
if ((args.motu_read_counts_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -m ignored\n")
if ((args.fb is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fb ignored\n")
if ((args.fd is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fd ignored\n")
if ((args.fm is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fm ignored\n")
if ((args.fp is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fp ignored\n")
if ((args.fc is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -fc ignored\n")
if args.command == 'snv_call':
if ((args.onlySpecI is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -e ignored\n")
if ((args.type_output is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -y ignored\n")
if ((args.sampleName is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -n ignored\n")
if ((args.motu_read_counts_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -m ignored\n")
if ((args.print_rel_ab is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -c ignored\n")
if ((args.print_full_rank is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -q ignored\n")
if ((args.print_NCBI_id is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -p ignored\n")
if ((args.print_long_names is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -u ignored\n")
if ((args.taxonomic_level is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -k ignored\n")
if ((args.profile_bam_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -I ignored\n")
if ((args.profile_mOTU_reads_file is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -M ignored\n")
if ((args.map_lgs_cutoff is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -g ignored\n")
if ((args.forwardReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -f ignored\n")
if ((args.reverseReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -r ignored\n")
if ((args.singleReads is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -s ignored\n")
if ((args.outPutIsBam is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -b ignored\n")
if ((args.min_len_align_length is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -l ignored\n")
if ((args.BIOM_output is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -B ignored\n")
if ((args.listInputFiles is not None) and (args.verbose>=2)): sys.stderr.write("[main] Warning: -i ignored\n")
# set the default
if (args.threads is None): args.threads = 1
if (args.verbose is None): args.verbose = 3
if (args.type_output is None): args.type_output = 'insert.scaled_counts'
if (args.map_lgs_cutoff is None): args.map_lgs_cutoff = 3
if (args.number_of_cores is None): args.number_of_cores = 1
if (args.taxonomic_level is None): args.taxonomic_level = "mOTU"
if (args.sampleName is None): args.sampleName = "unnamed sample"
if (args.outPutIsBam is None): args.outPutIsBam = False
if (args.onlySpecI is None): args.onlySpecI = False
if (args.print_full_rank is None): args.print_full_rank = False
if (args.BIOM_output is None): args.BIOM_output = False
if (args.print_rel_ab is None): args.print_rel_ab = False
if (args.print_NCBI_id is None): args.print_NCBI_id = False
if (args.print_long_names is None): args.print_long_names = False
if (args.output is None): args.output = ""
# default for snv_call
if (args.fb is None): args.fb = 80.0
if (args.fd is None): args.fd = 5.0
if (args.fm is None): args.fm = 2
if (args.fc is None): args.fc = 5.0
if (args.fp is None): args.fp = 0.9
# check min length alignment
default_min_len_align_length = 75 # min length align that we use to filter for profiling
default_min_len_align_length_map_tax = 75 # min length align that we use in map_tax
if (args.command == 'map_tax') and (args.min_len_align_length is not None): # map_tax default is 45, unless we set -l
default_min_len_align_length_map_tax = args.min_len_align_length
if (args.min_len_align_length is None): args.min_len_align_length = default_min_len_align_length
if (args.min_len_align_length < 20):
sys.stderr.write("[E::main] Error: -l should be greater than 20\n")
sys.exit(1)
if (args.min_len_align_length < 45) and (args.verbose>=2):
sys.stderr.write("[W::main] Warning: Minimum suggested value for -l is 45\n")
if (args.min_len_align_length < default_min_len_align_length_map_tax):
default_min_len_align_length_map_tax = args.min_len_align_length
if args.command == 'map_snv': # if we use map_snv then we filter with the value that was inserted
default_min_len_align_length_map_tax = args.min_len_align_length
info_parameters_p = "-l "+str(args.min_len_align_length)
# set default for parameters that are not displayed
if (args.min_perc_id is None): args.min_perc_id = 97
if (args.min_perc_align is None): args.min_perc_align = 45
if (args.min_clip_length is None): args.min_clip_length = 60
args.min_perc_id = float(args.min_perc_id)
args.min_perc_align = float(args.min_perc_align)
args.min_clip_length = float(args.min_clip_length)
# check parameters that are not displayed
if (args.min_perc_id < 97 or args.min_perc_id >100):
sys.stderr.write("[E::main] Error: -min_perc_id should be between 97 and 100\n")
sys.exit(1)
if (args.min_perc_align < 45 or args.min_perc_align >100):
sys.stderr.write("[E::main] Error: -min_perc_align should be between 45 and 100\n")
sys.exit(1)
if (args.min_clip_length < 60):
sys.stderr.write("[E::main] Error: -min_clip_length should be greater than 60\n")
sys.exit(1)
# ---------------------- check general parameters --------------------------
if args.verbose < 1:
sys.stderr.write("[E::main] Error: verbose level (-v) is less than 1\n")
sys.exit(1)
if args.threads < 1:
sys.stderr.write("[E::main] Error: number of threads (-t) is less than 1\n")
sys.exit(1)
if args.number_of_cores < 1:
sys.stderr.write("[E::main] Error: number of cores (-C) is less than 1\n")
sys.exit(1)
if args.map_lgs_cutoff<1 or args.map_lgs_cutoff>6:
sys.stderr.write("[E::main] Error: invalid number of marker genes cutoff (-g): "+str(args.map_lgs_cutoff)+" (possible values: from 1 to 6)\n")
sys.exit(1)
# --------------------------------------------------------------------------
# SNVCALL
# Begin edit hans 8.3.2018
# I leave all settings here. They might need to be put to a different location later.
# --------------------------------------------------------------------------
if args.command == 'snv_call':
if args.directory_append is None:
sys.stderr.write("[E::main] Error: -d is missing\n")
sys.exit(1)
if args.output == "":
sys.stderr.write("[E::main] Error: -o is missing\n")
sys.exit(1)
if (args.verbose>2): sys.stderr.write("[main] Begin metaSNV\n")
# --------------------------------------------------------------------------
# Settings/Params
# --------------------------------------------------------------------------
reference = relative_path+"db_mOTU/mOTU.v2b.centroids.reformatted.padded"
annotation = relative_path+"db_mOTU/mOTU.v2b.centroids.reformatted.padded.annotations"
if not os.path.isfile(annotation):
sys.stderr.write("[E::main] The annotation file %s does not exist. Create annotation file with the get.annotations.py command from the metaSNV package\n" % (annotation))
sys.exit(1)
bamdir = args.directory_append
fb = args.fb
fd = args.fd
fm = args.fm
fp = args.fp
fc = args.fc
paramsdir = '-m{}-d{}-b{}-c{}-p{}'.format(int(args.fm), int(args.fd), int(args.fb), int(args.fc), float(args.fp))
# threads = args.threads
sample_file = os.path.abspath(os.path.join(bamdir, 'sample_list'))
outdir = os.path.abspath(args.output)
if os.path.exists(outdir):
sys.stderr.write("[E::main] The output directory already exists: %s Existing. Delete and restart\n" % (outdir))
sys.exit(1)
msnv_log_file = os.path.abspath(os.path.join(outdir, 'metaSNV.log'))
if (args.verbose>=2): sys.stderr.write("[main] metaSNV stderr/stdout log file: %s\n" % (msnv_log_file))
# --------------------------------------------------------------------------
# create sample_list file
# --------------------------------------------------------------------------
if args.verbose>=4:
sys.stderr.write("[main] Creating sample list file for metaSNV: %s\n" % (sample_file))
bamfiles = [os.path.abspath(p) for p in glob.glob(os.path.join(bamdir, '*bam'))]
if len(bamfiles) == 0:
sys.stderr.write("[E::main] No BAM files found in the input folder. Exiting\n")
sys.exit(0)
with open(sample_file, 'w') as handle:
for bamfile in bamfiles:
handle.write(bamfile + '\n')
# --------------------------------------------------------------------------
# metaSNV
# --------------------------------------------------------------------------
if args.verbose>=4:
sys.stderr.write("[main] Executing metaSNV main routine.\n")
metaSNV_command = 'metaSNV.py %s %s %s --db_ann %s --threads %d --n_splits %d' % (outdir, sample_file, reference, annotation, args.threads, args.threads)
if args.verbose>=4:
sys.stderr.write("[main] Command: %s\n" % metaSNV_command)
metaSNV_popenCMD = shlex.split(metaSNV_command)
p = subprocess.Popen(metaSNV_popenCMD, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
mlf = open(msnv_log_file, 'w')
mlf.write(metaSNV_command + '\n')
mlf.write(stdout.decode('ascii') + '\n')
mlf.write(stderr.decode('ascii') + '\n')
if p.returncode:
sys.stderr.write("[E::main] metaSNV finished with an error. Exiting\n")
sys.stderr.write("[E::main] %s\n" %(stderr))
sys.stderr.write("[E::main] %s\n" %(stdout))
mlf.close()
sys.exit(1)
# --------------------------------------------------------------------------
# FILTER
# --------------------------------------------------------------------------
if args.verbose>=4:
sys.stderr.write("[main] Executing metaSNV filter routine.\n")
# params = '-m 5 -d 10 -b 80 -p 0.9'
metaSNVFilter_command = 'python '+relative_path+'bin/metaSNV_Filtering_2.0.py %s -m %d -d %f -b %f -p %f -c %f --n_threads %d' %(outdir, fm, fd, fb, fp, fc, args.threads)
if args.verbose>=4:
sys.stderr.write("[main] Command: %s\n" % metaSNVFilter_command)
metaSNVFilter_popenCMD = shlex.split(metaSNVFilter_command)
p = subprocess.Popen(metaSNVFilter_popenCMD, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
mlf.write(metaSNVFilter_command + '\n')
mlf.write(stdout.decode('ascii') + '\n')
mlf.write(stderr.decode('ascii') + '\n')
if p.returncode:
sys.stderr.write("[E::main] metaSNVFilter finished with an error. Exiting\n")
sys.stderr.write("[E::main] %s\n" %(stderr))
sys.stderr.write("[E::main] %s\n" %(stdout))
mlf.close()
sys.exit(1)
# --------------------------------------------------------------------------
# REMOVE PADDING
# --------------------------------------------------------------------------
if args.verbose>=4:
sys.stderr.write("[main] Executing metaSNV remove padded routine.\n")
metaSNVRMPadded_command = 'bash '+relative_path+'bin/motus.remove.padded.sh %s/filtered%s/' % (outdir, paramsdir)
if args.verbose>=4:
sys.stderr.write("[main] Command: %s\n" % metaSNVRMPadded_command)
metaSNVRMPadded_popenCMD = shlex.split(metaSNVRMPadded_command)
p = subprocess.Popen(metaSNVRMPadded_popenCMD, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
mlf.write(metaSNVRMPadded_command + '\n')
mlf.write(stdout.decode('ascii') + '\n')
mlf.write(stderr.decode('ascii') + '\n')
if p.returncode:
sys.stderr.write("[E::main] metaSNVRMPadded finished with an error. Exiting\n")
sys.stderr.write("[E::main] %s\n" %(stderr))
sys.stderr.write("[E::main] %s\n" %(stdout))
mlf.close()
sys.exit(1)
# --------------------------------------------------------------------------
# DISTANCE
# --------------------------------------------------------------------------
if args.verbose>=4:
sys.stderr.write("[main] Executing metaSNV distance routine.\n")
metaSNVDist_command = 'python '+relative_path+'bin/metaSNV_DistDiv.py --filt %s/filtered%s --dist --n_threads %d' % (outdir, paramsdir, args.threads)
if args.verbose>=4:
sys.stderr.write("[main] Command: %s\n" % metaSNVDist_command)
metaSNVDist_popenCMD = shlex.split(metaSNVDist_command)
p = subprocess.Popen(metaSNVDist_popenCMD, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
mlf.write(metaSNVDist_command + '\n')
mlf.write(stdout.decode('ascii') + '\n')
mlf.write(stderr.decode('ascii') + '\n')
if p.returncode:
sys.stderr.write("[E::main] metaSNVDist finished with an error. Exiting\n")
sys.stderr.write("[E::main] %s\n" %(stderr))
sys.stderr.write("[E::main] %s\n" %(stdout))
mlf.close()
sys.exit(1)
# --------------------------------------------------------------------------
# KEEP ONLY INTERESTING FILES
# --------------------------------------------------------------------------
# remove files
os.remove(os.path.abspath(os.path.join(outdir, 'all_samples')))
os.remove(os.path.abspath(os.path.join(outdir, 'bed_header')))
shutil.rmtree(os.path.abspath(os.path.join(outdir, 'bestsplits')))
shutil.rmtree(os.path.abspath(os.path.join(outdir, 'cov')))
shutil.rmtree(os.path.abspath(os.path.join(outdir, 'distances')))
shutil.rmtree(os.path.abspath(os.path.join(outdir, 'filtered')))
shutil.rmtree(os.path.abspath(os.path.join(outdir, 'snpCaller')))
# check if the 'distances' dir is empty
dist_files = [os.path.abspath(p) for p in glob.glob(os.path.join(outdir, 'dist*/*dist'))]
if len(dist_files) == 0 and args.verbose >= 2:
sys.stderr.write("[W::main] The result is empty\n")
if (args.verbose>=2): sys.stderr.write("[main] End metaSNV\n")
mlf.close()
sys.exit(0)
# --------------------------------------------------------------------------
# END snv_call
# --------------------------------------------------------------------------
#set database of sequences
reference = relative_path+"db_mOTU/mOTU.v2b.nr.padded" # must keep this
header_file = relative_path+"db_mOTU/bam_header_nr" # must keep this
args_db = 'nr' # must keep this
# if we use snv, then we use centroids
if args.command == 'map_snv':
reference = relative_path+"db_mOTU/mOTU.v2b.centroids.reformatted.padded"
header_file = relative_path+"db_mOTU/bam_header_cen"
args_db == 'cen'
# --------------------------------------------------------------------------
# VERSION of DB,scripts,taxonomy
# --------------------------------------------------------------------------
# db -----------
version_db = args_db+versions[args_db]
if (args.verbose >= 5): sys.stderr.write("[main] Map reads to db "+version_db+"\n")
# scripts
version_map_lgs = "calc_motu "+str(versions["map_mOTUs_to_LGs"])
version_map_lgs = version_map_lgs + " -k "+args.taxonomic_level
version_map_lgs = version_map_lgs + " -g "+str(args.map_lgs_cutoff)
if args.onlySpecI: version_map_lgs = version_map_lgs + " -e"
if args.print_rel_ab: version_map_lgs = version_map_lgs + " -c"
if args.print_NCBI_id: version_map_lgs = version_map_lgs + " -p"
if args.print_long_names: version_map_lgs = version_map_lgs + " -u"
version_map_lgs = version_map_lgs + " | taxonomy: ref_mOTU_"+str(versions["specI_tax"])
version_map_lgs = version_map_lgs + " meta_mOTU_"+str(versions["mOTULG_tax"])
version_append = "# motus version "+str(version_tool)+" | merge "+str(versions["append"])
# --------------------------------------------------------------------------
# PROFILE
# --------------------------------------------------------------------------
if args.command == 'profile' or args.command == 'map_snv':
# ---------------- check input -----------------------------------------
if args.verbose>2:
initial_start_time = time.time()
time_after_bwa = time.time() # for now we initialize it, so that if we skip bwa, then we use the initial time
time_after_map_genes = time.time() # for now we initialize it, so that if we skip calc_mgc, then we use the initial time
if args.command == 'profile' and (args.listInputFiles is None and args.motu_read_counts_file is None):
sys.stderr.write("[main] MAP_TAX -----------\n")
if args.command == 'map_snv':
sys.stderr.write("[main] MAP_SNV -----------\n")
# check that there is at least one file with reads
if (args.forwardReads is None) and (args.reverseReads is None) and (args.singleReads is None) and (args.listInputFiles is None) and (args.motu_read_counts_file is None):
sys.stderr.write("[E::map_db] Error: input is missing. Please provide -f,-r or -s or -i or -m.\n")
sys.exit(1)
# check that for and rev reads are present togehter
if ((args.forwardReads is not None) and (args.reverseReads is None) and (args.listInputFiles is None) and (args.motu_read_counts_file is None)):
sys.stderr.write("[E::map_db] Error: reverse reads (-r) missing\n")
sys.exit(1)
if ((args.forwardReads is None) and (args.reverseReads is not None) and (args.listInputFiles is None) and (args.motu_read_counts_file is None)):
sys.stderr.write("[E::map_db] Error: forward reads (-f) missing\n")
sys.exit(1)
# if we have -i bam/sam file, then we ignore the reads input
if (args.listInputFiles is not None and (args.motu_read_counts_file is None)):
if (args.verbose >= 2) and (args.forwardReads is not None): sys.stderr.write("[W::main] Warning: -f FILE ignored since there is -i\n")
if (args.verbose >= 2) and (args.reverseReads is not None): sys.stderr.write("[W::main] Warning: -r FILE ignored since there is -i\n")
if (args.verbose >= 2) and (args.singleReads is not None): sys.stderr.write("[W::main] Warning: -s FILE ignored since there is -i\n")
# if we have -m, then we ignore other inputs
if (args.motu_read_counts_file is not None):
if (args.verbose >= 2) and (args.forwardReads is not None): sys.stderr.write("[W::main] Warning: -f FILE ignored since there is -m\n")
if (args.verbose >= 2) and (args.reverseReads is not None): sys.stderr.write("[W::main] Warning: -r FILE ignored since there is -m\n")
if (args.verbose >= 2) and (args.singleReads is not None): sys.stderr.write("[W::main] Warning: -s FILE ignored since there is -m\n")
if (args.verbose >= 2) and (args.listInputFiles is not None): sys.stderr.write("[W::main] Warning: -i FILE ignored since there is -m\n")
if (args.listInputFiles is None and args.motu_read_counts_file is None): ## run BWA
# ---------------------- divide the lanes ------------------------------
singles = list()
forw = list()
reve = list()
if (args.singleReads is not None): singles = args.singleReads.split(",")
if (args.forwardReads is not None): forw = args.forwardReads.split(",")
if (args.reverseReads is not None): reve = args.reverseReads.split(",")
number_of_lanes = max(len(singles),len(forw),len(reve))
if args.verbose > 2: sys.stderr.write("[main] Number of detected lanes: "+str(number_of_lanes)+"\n")
# ----check input: check number of files for forw and rev --------------
if (args.forwardReads is not None):
if len(forw) != len(reve):
sys.stderr.write("[E::map_db] Error: number of files for forward reads ("+str(len(forw))+") is different from number of files for reverse reads ("+str(len(reve))+")\n")
sys.exit(1)
# ----check if use multicores and prepere data for multicores ----------
if args.number_of_cores > 1:
multiple_cores = True
else:
multiple_cores = False
# check that we have the same number
#of cores as the number of lanes
if args.number_of_cores > number_of_lanes:
if args.verbose >= 2: sys.stderr.write(" [W::map_db] Warning: We use only "+str(number_of_lanes)+" out of "+str(args.number_of_cores)+" cores\n")
if args.number_of_cores < number_of_lanes and args.number_of_cores != 1:
if args.verbose >= 2: sys.stderr.write(" [W::map_db] Warning: multiple cores computation is implemented only if the number of cores is equal to the number of lanes ("+str(number_of_lanes)+"). Set -C "+str(number_of_lanes)+"\n")
multiple_cores = False
#prepare data
if multiple_cores:
processes = list()
results_bwa = list()
# ------------ execute bwa ---------------------------------------------
all_sam_lines = list()
avg_length_reads = list()
for i in range(number_of_lanes):
if args.verbose>2: sys.stderr.write("[main] Run bwa on lane "+str(i+1)+"\n")