-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetPhylogeny.py
55 lines (51 loc) · 2.19 KB
/
GetPhylogeny.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
from sys import stderr, argv
from Bio import SeqIO
from dendropy import Tree
species_replacements = [
("Nomascus_leucogenys", "Hylobates_gabriellae"),
("Piliocolobus_tephrosceles", "Procolobus_verus"),
("Rhinopithecus_bieti", "Pygathrix_bieti"),
("Rhinopithecus_roxellana", "Pygathrix_roxellana"),
("Pongo_abelii", "Pongo_pygmaeus"),
("Papio_anubis", "Papio_hamadryas"),
("Propithecus_coquereli", "Propithecus_tattersalli"),
("Carlito_syrichta", "Tarsius_syrichta"),
("Chlorocebus_sabaeus", "Cercopithecus_mitis"),
("Cercocebus_atys", "Cercocebus_galeritus"),
("Fukomys_damarensis", "Cryptomys_damarensis"),
("Ictidomys_tridecemlineatus", "Spermophilus_tridecemlineatus"),
("Cricetulus_griseus", "Cricetulus_barabensis"),
("Urocitellus_parryii", "Spermophilus_parryii"),
("Nannospalax_galili", "Nannospalax_ehrenbergi"),
("Plecturocebus_moloch", "Callicebus_moloch"),
]
if __name__ == "__main__":
recs = {rec.id: rec for rec in SeqIO.parse(argv[1], "fasta")}
species = {recid for recid in recs if not recid[-1].isnumeric()}
for from_spec, to_spec in species_replacements:
if from_spec in species:
species.remove(from_spec)
species.add(to_spec)
tree = Tree.get_from_path(argv[2], schema="nexus")
tree_taxa = {tx.label.replace(" ", "_") for tx in tree.taxon_namespace}
print(species.difference(tree_taxa), file=stderr)
tree.retain_taxa_with_labels([s.replace("_", " ") for s in species])
for to_spec, from_spec in species_replacements:
n = tree.find_node_with_taxon_label(from_spec.replace("_", " "))
if n:
n.taxon.label = to_spec
print(
tree.as_string("newick").replace("*", "").replace('"', "").replace("'", ""),
file=open(argv[3], "w"),
)
tree.scale_edges(1 / 91.6) # Approximate age when rodents diverged from primates
print(
tree.as_string("newick").replace("*", "").replace('"', "").replace("'", ""),
file=open(argv[4], "w"),
)
tree.taxon_namespace.clear()
tree.update_taxon_namespace()
print(
tree.as_string("nexus").replace("*", "").replace('"', "").replace("'", ""),
file=open(argv[5], "w"),
)