-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAMAS.py
1487 lines (1246 loc) · 56.7 KB
/
AMAS.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 python3
# Program to calculate various statistics on a multiple sequence alignment
# and allow efficient manipulation of phylogenomic data sets
# Copyright (C) 2015 Marek Borowiec
# 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/>.
"""
This stand-alone program allows manipulations of multiple sequence
alignments. It supports sequential FASTA, PHYLIP, NEXUS, and interleaved PHYLIP
and NEXUS formats for DNA and aino acid sequences. It can print summary statistics,
convert among formats, and concatenate alignments.
Current statistics include the number of taxa, alignment length, total number
of matrix cells, overall number of undetermined characters, percent of missing
data, AT and GC contents (for DNA alignments), number and proportion of
variable sites, number and proportion of parsimony informative sites,
and counts of all characters present in the relevant (nucleotide or amino acid) alphabet.
"""
import argparse, multiprocessing as mp, re, sys
from random import sample
from os import path
from collections import defaultdict, Counter
class ParsedArgs:
def __init__(self):
parser = argparse.ArgumentParser(
usage='''AMAS <command> [<args>]
The AMAS commands are:
concat Concatenate input alignments
convert Convert to other file format
replicate Create replicate data sets for phylogenetic jackknife
split Split alignment according to a partitions file
summary Write alignment summary
remove Remove taxa from alignment
Use AMAS <command> -h for help with arguments of the command of interest
'''
)
parser.add_argument(
"command",
help="Subcommand to run"
)
# parse_args defaults to [1:] for args, but you need to
# exclude the rest of the args too, or validation will fail
self.args = parser.parse_args(sys.argv[1:2])
if not hasattr(self, self.args.command):
print('Unrecognized command')
parser.print_help()
exit(1)
# use dispatch pattern to invoke method with same name
getattr(self, self.args.command)()
def add_common_args(self, parser):
# define required arguments for every command
requiredNamed = parser.add_argument_group('required arguments')
parser.add_argument(
"-e",
"--check-align",
dest = "check_align",
action = "store_true",
default = False,
help = "Check if input sequences are aligned. Default: no check"
)
parser.add_argument(
# parallelization is used for file parsing and calculating summary stats
"-c",
"--cores",
dest = "cores",
default = 1,
help = "Number of cores used. Default: 1"
)
requiredNamed.add_argument(
"-i",
"--in-files",
nargs = "+",
type = str,
dest = "in_files",
required = True,
help = """Alignment files to be taken as input.
You can specify multiple files using wildcards (e.g. --in-files *fasta)"""
)
requiredNamed.add_argument(
"-f",
"--in-format",
dest = "in_format",
required = True,
choices = ["fasta", "phylip", "nexus", "phylip-int", "nexus-int"],
help = "The format of input alignment"
)
requiredNamed.add_argument(
"-d",
"--data-type",
dest = "data_type",
required = True,
choices = ["aa", "dna"],
help = "Type of data"
)
def summary(self):
# summary command
parser = argparse.ArgumentParser(
description="Write alignment summary",
)
parser.add_argument(
"-o",
"--summary-out",
dest = "summary_out",
default = "summary.txt",
help = "File name for the alignment summary. Default: 'summary.txt'"
)
parser.add_argument(
"-s",
"--by-taxon",
dest = "by_taxon_summary",
action = "store_true",
default = False,
help = "In addition to alignment summary, write by sequence/taxon summaries. Default: Don't write"
)
# add shared arguments
self.add_common_args(parser)
args = parser.parse_args(sys.argv[2:])
return args
def concat(self):
# concat command
parser = argparse.ArgumentParser(
description="Concatenate input alignments"
)
parser.add_argument(
"-p",
"--concat-part",
dest = "concat_part",
default = "partitions.txt",
help = "File name for th0e concatenated alignment partitions. Default: 'partitions.txt'"
)
parser.add_argument(
"-t",
"--concat-out",
dest = "concat_out",
default = "concatenated.out",
help = "File name for the concatenated alignment. Default: 'concatenated.out'"
)
parser.add_argument(
"-u",
"--out-format",
dest = "out_format",
choices = ["fasta", "phylip", "nexus", "phylip-int", "nexus-int"],
default = "fasta",
help = "File format for the output alignment. Default: fasta"
)
# add shared arguments
self.add_common_args(parser)
args = parser.parse_args(sys.argv[2:])
return args
def convert(self):
# convert command
parser = argparse.ArgumentParser(
description="Convert to other file format",
)
parser.add_argument(
"-u",
"--out-format",
dest = "out_format",
choices = ["fasta", "phylip", "nexus", "phylip-int", "nexus-int"],
default = "fasta",
help = "File format for the output alignment. Default: fasta"
)
# add shared arguments
self.add_common_args(parser)
args = parser.parse_args(sys.argv[2:])
return args
def replicate(self):
# replicate command
parser = argparse.ArgumentParser(
description="Create replicate datasets for phylogenetic jackknife",
)
parser.add_argument(
"-r",
"--rep-aln",
nargs = 2,
type = int,
dest = "replicate_args",
help = "Create replicate data sets for phylogenetic jackknife [replicates, no alignments for each replicate]",
required = True
)
parser.add_argument(
"-u",
"--out-format",
dest = "out_format",
choices = ["fasta", "phylip", "nexus", "phylip-int", "nexus-int"],
default = "fasta",
help = "File format for the output alignment. Default: fasta"
)
# add shared arguments
self.add_common_args(parser)
args = parser.parse_args(sys.argv[2:])
return args
def split(self):
# split command
parser = argparse.ArgumentParser(
description="Split alignment according to a partitions file",
)
parser.add_argument(
"-l",
"--split-by",
dest = "split_by",
help = "File name for partitions to be used for alignment splitting.",
required = True
)
parser.add_argument(
"-j",
"--remove-empty",
dest = "remove_empty",
action = "store_true",
default = False,
help = "Remove taxa with sequences composed of only undetermined characters? Default: Don't remove"
)
parser.add_argument(
"-u",
"--out-format",
dest = "out_format",
choices = ["fasta", "phylip", "nexus", "phylip-int", "nexus-int"],
default = "fasta",
help = "File format for the output alignment. Default: fasta"
)
# add shared arguments
self.add_common_args(parser)
args = parser.parse_args(sys.argv[2:])
return args
def remove(self):
# remove taxa command
parser = argparse.ArgumentParser(
description="Remove taxa from alignment",
)
parser.add_argument(
"-x",
"--taxa-to-remove",
nargs = "+",
type = str,
dest = "taxa_to_remove",
help = "Taxon/sequence names to be removed.",
required = True
)
parser.add_argument(
"-u",
"--out-format",
dest = "out_format",
choices = ["fasta", "phylip", "nexus", "phylip-int", "nexus-int"],
default = "fasta",
help = "File format for the output alignment. Default: fasta"
)
parser.add_argument(
"-g",
"--out-prefix",
dest = "out_prefix",
default = "reduced_",
help = "File name prefix for the concatenated alignment. Default: 'reduced_'"
)
# add shared arguments
self.add_common_args(parser)
args = parser.parse_args(sys.argv[2:])
return args
def get_args_dict(self):
# store arguments in a dictionary
command = self.args.__dict__
arguments = getattr(self, self.args.command)().__dict__
argument_dictionary = command.copy()
argument_dictionary.update(arguments)
return argument_dictionary
class FileHandler:
"""Define file handle that closes when out of scope"""
def __init__(self, file_name):
self.file_name = file_name
def __enter__(self):
try:
self.in_file = open(self.file_name, "r")
except FileNotFoundError:
print("ERROR: File '" + self.file_name + "' not found.")
sys.exit()
return self.in_file
def __exit__(self, *args):
self.in_file.close()
def get_file_name(self):
return self.file_name
class FileParser:
"""Parse file contents and return sequences and sequence names"""
def __init__(self, in_file):
self.in_file = in_file
with FileHandler(in_file) as handle:
self.in_file_lines = handle.read().rstrip("\r\n")
def fasta_parse(self):
# use regex to parse names and sequences in sequential fasta files
matches = re.finditer(
r"^>(.*[^$])([^>]*)",
self.in_file_lines, re.MULTILINE
)
records = {}
for match in matches:
name_match = match.group(1).replace("\n","")
seq_match = match.group(2).replace("\n","").upper()
seq_match = self.translate_ambiguous(seq_match)
records[name_match] = seq_match
return records
def phylip_parse(self):
# use regex to parse names and sequences in sequential phylip files
matches = re.finditer(
r"^(\s+)?(\S+)\s+([A-Za-z*?.{}-]+)",
self.in_file_lines, re.MULTILINE
)
records = {}
for match in matches:
name_match = match.group(2).replace("\n","")
seq_match = match.group(3).replace("\n","").upper()
seq_match = self.translate_ambiguous(seq_match)
records[name_match] = seq_match
return records
def phylip_interleaved_parse(self):
# use regex to parse names and sequences in interleaved phylip files
name_matches = re.finditer(
r"^(\s+)?(\S+)[ \t]+[A-Za-z*?.{}-]+",
self.in_file_lines, re.MULTILINE
)
seq_matches = re.finditer(
r"(^(\s+)?\S+[ \t]+|^)([A-Za-z*?.{}-]+)$",
self.in_file_lines, re.MULTILINE
)
# initiate lists for taxa names and sequence strings on separate lines
taxa = []
sequences = []
# initiate a dictionary for the name:sequence records
records = {}
# initiate a counter to keep track of sequences strung together
# from separate lines
counter = 0
for match in name_matches:
name_match = match.group(2).replace("\n","")
taxa.append(name_match)
for match in seq_matches:
seq_match = match.group(3).replace("\n","").upper()
seq_match = self.translate_ambiguous(seq_match)
sequences.append(seq_match)
for taxon_no in range(len(taxa)):
sequence = ""
for index in range(counter,len(sequences),len(taxa)):
sequence += sequences[index]
records[taxa[taxon_no]] = sequence
counter += 1
return records
def nexus_parse(self):
# use regex to parse names and sequences in sequential nexus files
# find the matrix block
matches = re.finditer(
r"(\s+)?(MATRIX\n|matrix\n|MATRIX\r\n|matrix\r\n)(.*?;)",
self.in_file_lines, re.DOTALL
)
records = {}
# get names and sequences from the matrix block
for match in matches:
matrix_match = match.group(3)
seq_matches = re.finditer(
r"^(\s+)?[']?(\S+\s\S+|\S+)[']?\s+([A-Za-z*?.{}-]+)($|\s+\[[0-9]+\]$)",
matrix_match, re.MULTILINE
)
for match in seq_matches:
name_match = match.group(2).replace("\n","")
seq_match = match.group(3).replace("\n","").upper()
seq_match = self.translate_ambiguous(seq_match)
records[name_match] = seq_match
return records
def nexus_interleaved_parse(self):
# use regex to parse names and sequences in sequential nexus files
# find the matrix block
matches = re.finditer(
r"(\s+)?(MATRIX\n|matrix\n|MATRIX\r\n|matrix\r\n)(.*?;)",
self.in_file_lines, re.DOTALL
)
# initiate lists for taxa names and sequence strings on separate lines
taxa = []
sequences = []
# initiate a dictionary for the name:sequence records
records = {}
for match in matches:
matrix_match = match.group(3)
# get names and sequences from the matrix block
seq_matches = re.finditer(
r"^(\s+)?[']?(\S+\s\S+|\S+)[']?\s+([A-Za-z*?.{}-]+)($|\s+\[[0-9]+\]$)",
matrix_match, re.MULTILINE
)
for match in seq_matches:
name_match = match.group(2)
if name_match not in taxa:
taxa.append(name_match)
seq_match = match.group(3)
sequences.append(seq_match)
# initiate a counter to keep track of sequences strung together
# from separate lines
counter = 0
for taxon_no in range(len(taxa)):
full_length_sequence = "".join([sequences[index] for index in range(counter,len(sequences),len(taxa))])
records[taxa[taxon_no]] = self.translate_ambiguous(full_length_sequence).replace("\n","").upper()
counter += 1
return records
def translate_ambiguous(self, seq):
# translate ambiguous characters from curly bracket format
# to single letter format
seq = seq.replace("{GT}","K")
seq = seq.replace("{AC}","M")
seq = seq.replace("{AG}","R")
seq = seq.replace("{CT}","Y")
seq = seq.replace("{CG}","S")
seq = seq.replace("{AT}","W")
seq = seq.replace("{CGT}","B")
seq = seq.replace("{ACG}","V")
seq = seq.replace("{ACT}","H")
seq = seq.replace("{AGT}","D")
seq = seq.replace("{GATC}","N")
return seq
def partitions_parse(self):
# parse partitions file using regex
matches = re.finditer(r"^(\s+)?([^ =]+)[ =]+([\\0-9, -]+)", self.in_file_lines, re.MULTILINE)
# initiate list to store dictionaries with lists
# of slice positions as values
partitions = []
add_to_partitions = partitions.append
for match in matches:
# initiate dictionary of partition name as key
dict_of_dicts = {}
# and list of dictionaries with slice positions
list_of_dicts = []
add_to_list_of_dicts = list_of_dicts.append
# get parition name and numbers from parsed partition strings
partition_name = match.group(2)
numbers = match.group(3)
# find all numbers that will be used to parse positions
positions = re.findall(r"([^ ,]+)", numbers)
for position in positions:
# create dictionary for slicing input sequence
# conditioning on whether positions are represented
# by range, range with stride, or single number
pos_dict = {}
if "-" in position:
m = re.search(r"([0-9]+)-([0-9]+)", position)
pos_dict["start"] = int(m.group(1)) - 1
pos_dict["stop"] = int(m.group(2))
else:
pos_dict["start"] = int(position) - 1
pos_dict["stop"] = int(position)
if "\\" in position:
pos_dict["stride"] = 3
elif "\\" not in position:
pos_dict["stride"] = 1
add_to_list_of_dicts(pos_dict)
dict_of_dicts[partition_name] = list_of_dicts
add_to_partitions(dict_of_dicts)
return partitions
class Alignment:
"""Gets in parsed sequences as input and summarizes their stats"""
def __init__(self, in_file, in_format, data_type):
# initialize alignment class with parsed records and alignment name as arguments,
# create empty lists for list of sequences, sites without
# ambiguous or missing characters, and initialize variable for the number
# of parsimony informative sites
self.in_file = in_file
self.in_format = in_format
self.data_type = data_type
self.parsed_aln = self.get_parsed_aln()
def __str__(self):
return self.get_name
def get_aln_input(self):
# open and parse input file
aln_input = FileParser(self.in_file)
return aln_input
def get_parsed_aln(self):
# parse according to the given format
aln_input = self.get_aln_input()
if self.in_format == "fasta":
parsed_aln = aln_input.fasta_parse()
elif self.in_format == "phylip":
parsed_aln = aln_input.phylip_parse()
elif self.in_format == "phylip-int":
parsed_aln = aln_input.phylip_interleaved_parse()
elif self.in_format == "nexus":
parsed_aln = aln_input.nexus_parse()
elif self.in_format == "nexus-int":
parsed_aln = aln_input.nexus_interleaved_parse()
return parsed_aln
def summarize_alignment(self):
# call methods to create sequences list, matrix, sites without ambiguous or
# missing characters; get and summarize alignment statistics
summary = []
self.length = str(self.get_alignment_length())
self.matrix = self.matrix_creator()
self.no_missing_ambiguous = self.get_sites_no_missing_ambiguous()
self.variable_sites = self.get_variable()
self.prop_variable = self.get_prop_variable()
self.parsimony_informative = self.get_parsimony_informative()
self.prop_parsimony = self.get_prop_parsimony()
self.char_count_records = self.get_counts_from_parsed()
self.missing_records = self.get_missing_from_parsed()
name = str(self.get_name())
taxa_no = str(self.get_taxa_no())
cells = str(self.get_matrix_cells())
missing = str(self.get_missing())
missing_percent = str(self.get_missing_percent())
self.check_data_type()
summary = [name, taxa_no, self.length, cells, missing, missing_percent, \
str(self.variable_sites), str(self.prop_variable), str(self.parsimony_informative), str(self.prop_parsimony)]
return summary
def summarize_alignment_by_taxa(self):
# get summary for all taxa/sequences in alignment
per_taxon_summary = []
taxa_no = self.get_taxa_no()
self.length = self.get_alignment_length()
lengths = (self.length for i in range(taxa_no))
name = self.get_name()
names = (name for i in range(taxa_no))
taxa_names = (taxon.replace(" ","_").replace(".","_").replace("'","") \
for taxon, missing_count, missing_percent in self.missing_records)
missing = (missing_count for taxon, missing_count, missing_percent in self.missing_records)
missing_percent = (missing_percent for taxon, missing_count, missing_percent in self.missing_records)
self.check_data_type()
per_taxon_summary = (names, taxa_names, lengths, missing, missing_percent)
zipped = list(zip(*per_taxon_summary))
return zipped
def get_char_summary(self):
# get summary of frequencies for all characters
characters = []
counts = []
add_to_chars = characters.append
add_to_counts = counts.append
char_count_dicts = self.get_counts()
for char in self.alphabet:
add_to_chars(char)
if char in char_count_dicts.keys():
add_to_counts(str(char_count_dicts[char]))
else:
add_to_counts("0")
return characters, counts
def get_taxon_char_summary(self):
# get summary of frequencies for all characters
records = (self.append_count(char_dict) for taxon, char_dict in self.char_count_records)
return records
def append_count(self, char_dict):
count_list = []
for char in self.alphabet:
if char in char_dict.keys():
count_list.append(char_dict[char])
else:
count_list.append(0)
return count_list
def matrix_creator(self):
# decompose character matrix into a two-dimensional list
matrix = [list(sequence) for sequence in self.parsed_aln.values()]
return matrix
def get_column(self, i):
# get site from the character matrix
return [row[i] for row in self.matrix]
def all_same(self, site):
# check if all elements of a site are the same
return not site or site.count(site[0]) == len(site)
def get_sites_no_missing_ambiguous(self):
# get each site without missing or ambiguous characters
no_missing_ambiguous_sites = []
add_to_no_mis_amb = no_missing_ambiguous_sites.append
for column in range(self.get_alignment_length()):
site = self.get_column(column)
new_site = [char for char in site if char not in self.missing_ambiguous_chars]
add_to_no_mis_amb(new_site)
return no_missing_ambiguous_sites
def get_variable(self):
# if all elements of a site without missing or ambiguous characters
# are not the same, consider it variable
variable = len([site for site in self.no_missing_ambiguous \
if not self.all_same(site)])
return variable
def get_parsimony_informative(self):
# if the count for a unique character in a site is at least two,
# and there are at least two such characters in a site without missing
# or ambiguous characters, consider it parsimony informative
parsimony_informative = 0
for site in self.no_missing_ambiguous:
unique_chars = set(site)
pattern = [base for base in unique_chars if site.count(base) >= 2]
no_patterns = len(pattern)
if no_patterns >= 2:
parsimony_informative += 1
return parsimony_informative
def get_prop_variable(self):
# get proportion of variable sites to all sites
prop_variable = self.variable_sites / int(self.length)
return round(prop_variable, 3)
def get_prop_parsimony(self):
# get proportion of parsimony informative sites to all sites
prop_parsimony = self.parsimony_informative / int(self.length)
return round(prop_parsimony, 3)
def get_name(self):
# get input file name
in_filename = path.basename(self.in_file)
return in_filename
def get_taxa_no(self):
# get number of taxa
return len(self.parsed_aln.values())
def get_alignment_length(self):
# get alignment length by just checking the first seq length
# this assumes that all sequences are of equal length
return len(next(iter(self.parsed_aln.values())))
def get_matrix_cells(self):
# count all matrix cells
self.all_matrix_cells = len(self.parsed_aln.values()) \
* int(self.length)
return self.all_matrix_cells
def get_missing(self):
# count missing characters from the list of missing for all sequences
self.missing = sum(count for taxon, count, percent in self.missing_records)
return self.missing
def get_missing_percent(self):
# get missing percent
missing_percent = round((self.missing / self.all_matrix_cells * 100), 3)
return missing_percent
def get_missing_from_parsed(self):
# get missing count and percent from parsed alignment
# return a list of tuples with taxon name, count, and percent missing
self.missing_records = sorted([(taxon, self.get_missing_from_seq(seq), self.get_missing_percent_from_seq(seq)) \
for taxon, seq in self.parsed_aln.items()])
return self.missing_records
def get_missing_from_seq(self, seq):
# count missing characters for individual sequence
missing_count = sum(seq.count(char) for char in self.missing_chars)
return missing_count
def get_missing_percent_from_seq(self, seq):
# get missing percent from individual sequence
missing_seq_percent = round((self.get_missing_from_seq(seq) / self.get_alignment_length() * 100), 3)
return missing_seq_percent
def get_counts(self):
# get counts of each character in the used alphabet for all sequences
counters = [Counter(chars) for taxon, chars in self.char_count_records]
all_counts = sum(counters, Counter())
counts_dict = dict(all_counts)
return counts_dict
def get_counts_from_parsed(self):
# get counts of all characters from parsed alignment
# return a list of tuples with taxon name and counts
self.char_count_records = sorted([(taxon, self.get_counts_from_seq(seq)) \
for taxon, seq in self.parsed_aln.items()])
return self.char_count_records
def get_counts_from_seq(self, seq):
# get all alphabet chars count for individual sequence
char_counts = {char : seq.count(char) for char in self.alphabet}
return char_counts
def check_data_type(self):
# check if the data type is correct
self.check = any(any(char in self.non_alphabet for char in seq) \
for seq in self.parsed_aln.values())
if self.check is True:
print("WARNING: found non-" + self.data_type + " characters. "\
"Are you sure you specified the right data type?")
class AminoAcidAlignment(Alignment):
"""Alphabets specific to amino acid alignments"""
alphabet = ["A","C","D","E","F","G","H","I","K","L","M","N","P","Q","R", \
"S","T","V","W","Y","B","J","Z","X",".","*","-","?"]
missing_ambiguous_chars = ["B","J","Z","X",".","*","-","?"]
missing_chars = ["X",".","*","-","?"]
non_alphabet = ["O"]
def get_summary(self):
# get alignment summary specific to amino acids
data = self.summarize_alignment()
new_data = data + list(self.get_char_summary()[1])
return new_data
def get_taxa_summary(self):
# get per-taxon/sequence alignment summary specific to amino acids
data = self.summarize_alignment_by_taxa()
aa_summary = (data, self.get_taxon_char_summary())
zipped_list = list(zip(*aa_summary))
new_data = [list(data_tupl) + chars for data_tupl, chars in zipped_list]
return new_data
class DNAAlignment(Alignment):
"""Alphabets specific to DNA alignments"""
alphabet = ["A","C","G","T","K","M","R","Y","S","W","B","V","H","D","X", \
"N", "O", "-","?"]
missing_ambiguous_chars = ["K","M","R","Y","S","W","B","V","H","D","X", \
"N", "O", "-","?"]
missing_chars = ["X","N","O","-","?"]
non_alphabet = ["E", "F", "I", "L", "P", "Q", "J", "Z", ".", "*"]
def get_summary(self):
# get alignment summarry specific to nucleotide
data = self.summarize_alignment()
new_data = data + self.get_atgc_content() \
+ list(self.get_char_summary()[1])
return new_data
def get_taxa_summary(self):
# get per-taxon/sequence alignment summary specific to nucleotides
data = self.summarize_alignment_by_taxa()
dna_summary = (data, self.get_list_from_atgc(), self.get_taxon_char_summary())
zipped_list = list(zip(*dna_summary))
new_data = [list(data_tupl) + list(atgc) + chars for data_tupl, atgc, chars in zipped_list]
return new_data
def get_atgc_content(self):
# get AC and GC contents for all sequences
# AT content is the first element of AT, GC content tuple
# returned by get_atgc_from_seq()
self.get_atgc_from_parsed()
at_content = round(sum(atgc[0] for taxon, atgc in self.atgc_records) \
/ self.get_taxa_no(), 3)
gc_content = round(1 - float(at_content), 3)
atgc_content = [str(at_content), str(gc_content)]
return atgc_content
def get_list_from_atgc(self):
records = (atgc for taxon, atgc in self.atgc_records)
return records
def get_atgc_from_parsed(self):
# get AT and GC contents from parsed alignment dictionary
# return a list of tuples with taxon name, AT content, and GC content
self.atgc_records = sorted([(taxon, self.get_atgc_from_seq(seq)) \
for taxon, seq in self.parsed_aln.items()])
return self.atgc_records
def get_atgc_from_seq(self, seq):
# get AT and GC contents from individual sequences
at_count = seq.count("A") + seq.count("T") + seq.count("W")
gc_count = seq.count("G") + seq.count("C") + seq.count("S")
try:
at_content = round(at_count / (at_count + gc_count), 3)
gc_content = round(1 - float(at_content), 3)
except ZeroDivisionError:
at_content = 0
gc_content = 0
return at_content, gc_content
class MetaAlignment():
"""Class of multiple sequence alignments"""
def __init__(self, **kwargs):
# set defaults and get values from kwargs
self.in_files = kwargs.get("in_files")
self.in_format = kwargs.get("in_format")
self.data_type = kwargs.get("data_type")
self.command = kwargs.get("command")
self.concat_out = kwargs.get("concat_out", "concatenated.out")
self.check_align = kwargs.get("check_align", False)
self.cores = kwargs.get("cores")
self.by_taxon_summary = kwargs.get("by_taxon_summary")
if self.command == "replicate":
self.no_replicates = kwargs.get("replicate_args")[0]
self.no_loci = kwargs.get("replicate_args")[1]
if self.command == "split":
self.split = kwargs.get("split_by")
self.remove_empty = kwargs.get("remove_empty", False)
if self.command == "remove":
self.species_to_remove = kwargs.get("taxa_to_remove")
self.reduced_file_prefix = kwargs.get("out_prefix")
self.check_taxa = kwargs.get("check_taxa", False)
self.alignment_objects = self.get_alignment_objects()
self.parsed_alignments = self.get_parsed_alignments()
def remove_unknown_chars(self, seq):
# remove unknown characters from sequence
new_seq = seq.replace("?","").replace("-","")
return new_seq
def remove_empty_sequences(self, split_alignment):
# remove taxa from alignment if they are composed of only empty sequences
new_alignment = {taxon : seq for taxon, seq in split_alignment.items() if self.remove_unknown_chars(seq)}
return new_alignment
def get_partitions(self, partitions_file):
# parse and get partitions from partitions file
partitions = FileParser(partitions_file)
parsed_partitions = partitions.partitions_parse()
return parsed_partitions
def get_alignment_object(self, alignment):
# parse according to the given alphabet
if self.data_type == "aa":
aln = AminoAcidAlignment(alignment, self.in_format, self.data_type)
elif self.data_type == "dna":
aln = DNAAlignment(alignment, self.in_format, self.data_type)
return aln
def get_alignment_objects(self):
# get alignment objects on which statistics can be computed
# use multiprocessing if more than one core specified
if int(self.cores) == 1:
alignments = [self.get_alignment_object(alignment) for alignment in self.in_files]
elif int(self.cores) > 1:
pool = mp.Pool(int(self.cores))
alignments = pool.map(self.get_alignment_object, self.in_files)
return alignments
def get_parsed_alignments(self):
# get parsed dictionaries with taxa and sequences
parsed_alignments = []
add_to_parsed_alignments = parsed_alignments.append
for alignment in self.alignment_objects:
parsed = alignment.parsed_aln
add_to_parsed_alignments(parsed)
# checking if every seq has the same length or if parsed is not empty; exit if false
if self.check_align == True:
equal = all(x == [len(list(parsed.values())[i]) for i in range(0,len(list(parsed.values())))][0]
for x in [len(list(parsed.values())[i]) for i in range(0,len(list(parsed.values())))])
if equal is False:
print("ERROR: Sequences in input are of varying lengths. Be sure to align them first.")
sys.exit()
if not parsed.keys():
print("ERROR: Parsed sequences are empty. "\
"Are you sure you specified the right input format and/or that all input files are valid alignments?")
sys.exit()
return parsed_alignments
def get_partitioned(self, partitions_file):
# partition alignment according to a partitions file
partitions = self.get_partitions(partitions_file)
alignment = self.parsed_alignments[0]
# initiate list of newly partitioned alignments
list_of_parts = []
add_to_list_of_parts = list_of_parts.append
for partition in partitions:
# loop over all parsed partitions, adding taxa and sliced sequences
for name, elements in partition.items():
new_dict = {}
for taxon, seq in alignment.items():
new_seq = ""
for dictionary in elements:
new_seq = new_seq + seq[dictionary["start"]:dictionary["stop"]:dictionary["stride"]]
new_dict[taxon] = new_seq
if self.remove_empty:
# check if remove empty sequences
no_empty_dict = self.remove_empty_sequences(new_dict)
add_to_list_of_parts({name : no_empty_dict})
else:
# add partition name : dict of taxa and sequences to the list
add_to_list_of_parts({name : new_dict})
return list_of_parts
def get_summaries(self):
# get summaries for all alignment objects
# define different headers for aa and dna alignments
aa_header = [
"Alignment_name",
"No_of_taxa",
"Alignment_length",
"Total_matrix_cells",
"Undetermined_characters",
"Missing_percent",
"No_variable_sites",
"Proportion_variable_sites",
"Parsimony_informative_sites",
"Proportion_parsimony_informative"
]
dna_header = [
"Alignment_name",
"No_of_taxa",
"Alignment_length",
"Total_matrix_cells",
"Undetermined_characters",
"Missing_percent",
"No_variable_sites",
"Proportion_variable_sites",
"Parsimony_informative_sites",
"Proportion_parsimony_informative",