-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalcD1D2stat.py
182 lines (164 loc) · 5.75 KB
/
calcD1D2stat.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 25 16:48:06 2018
@author: scott
"""
from __future__ import print_function
from __future__ import division
import numpy as np
from ete3 import PhyloTree
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-t', "--treefile", type=str, required=True,
help="treefile in newick, 1 per line")
parser.add_argument('-g', "--groups", nargs='+', action='append',
required=True, help="quartet of species to calculate,"
" assumes form: P1 P2 P3. can be given multiple times")
parser.add_argument("--dlm", type=str, default="_",
help="delimeter denoting species")
parser.add_argument("-o", "--outgroup", type=str,
help="outgroup species for rooting if tree are unrooted")
parser.add_argument("--windows", type=int, help="sliding windows")
parser.add_argument("--mono", action="store_true", help="enforce monophyly for"
" trees with >1 individual per species")
args = parser.parse_args()
def LoadTrees(treefile, outgroup, dlm):
"""Reads and stores phylogenetic trees from a file
Parameters
------
treefile: file, file of newick trees, 1 per line
outgroup: str, last entry from quartet
Returns
------
treelist: obj, ete3 object of trees
"""
print("loading trees...")
treelist = []
with open(treefile, 'r') as newick:
for line in newick:
if not line.startswith("NA"):
t = PhyloTree(line)
t.set_species_naming_function(lambda node: node.name.split(dlm)[0])
if outgroup:
t.set_outgroup( t&outgroup )
treelist.append(t)
return(treelist)
def cMono(tree, taxon):
"""Checks if samples are monophyletic in tree
"""
return(tree.check_monophyly(values=taxon, target_attr="species")[0])
def pairwiseDistance(tree, taxon):
"""Iterative distance for each individual regardless of monophyly
"""
pwlist = []
sp1 = tree.search_nodes(species=taxon[0])
sp2 = tree.search_nodes(species=taxon[1])
for s1 in sp1:
for s2 in sp2:
pwlist.append(tree.get_distance(s1, s2))
return(np.nanmean(pwlist))
def DistABC(treelist, taxon, mono):
"""Calculates the support and node age if groups in taxon are monophyletic
"""
# only A/C when (A,B) and (B,C)
A = [taxon[0]]
B = [taxon[1]]
C = [taxon[2]]
AC_AB = []
AC_BC = []
AB_AB = []
BC_BC = []
for t in treelist:
ass = t.search_nodes(species=A[0])
bss = t.search_nodes(species=B[0])
css = t.search_nodes(species=C[0])
t.prune(ass+bss+css, preserve_branch_length=True)
distBC = (pairwiseDistance(t, B+C))
distAC = (pairwiseDistance(t, A+C))
distAB = (pairwiseDistance(t, A+B))
distAC = (pairwiseDistance(t, A+C))
if mono:
if cMono(t, B+C):
if distBC < distAB and distBC < distAC:
AB_AB.append(distBC)
AC_AB.append(distAC)
if cMono(t, A+B):
if distAB < distBC and distAB < distAC:
AC_BC.append(distAC)
BC_BC.append(distAB)
else:
# this should produce different trees and just not count the ones that are wrong
if distBC < distAB and distBC < distAC:
AB_AB.append(distBC)
AC_AB.append(distAC)
if distAB < distBC and distAB < distAC:
AC_BC.append(distAC)
BC_BC.append(distAB)
return(AC_AB, AC_BC, AB_AB, BC_BC)
if __name__ == "__main__":
quarts = args.groups
quart = quarts[0]
taxon = [quart[0], quart[1], quart[2]]
treelist = LoadTrees(args.treefile, args.outgroup, args.dlm)
dac_ab, dac_bc, dab_ab, dbc_bc= DistABC(treelist, taxon, args.mono)
A = quart[0][0]
B = quart[1][0]
C = quart[2][0]
step = args.windows
### D1
d1_m = np.mean(dab_ab) - np.mean(dbc_bc)
if step:
# windowed D1
d1SE = []
# D1 sliding windows
i = 0
j = step
f = open("{}{}{}.D1.{}.txt".format(A, B, C, step), 'w')
while j < len(dab_ab):
d1_win = np.mean(dab_ab[i:j]) - np.mean(dbc_bc[i:j])
d1SE.append(d1_win)
f.write("{}\n".format(d1_win))
i = j
j += step
f.close()
# D1 SE
n = len(d1SE)
try:
sv = ((n - 1) / n) * np.nansum((np.array(d1SE) - d1_m) ** 2)
se = np.sqrt(sv)
except ZeroDivisionError:
se = 0.0000000001
else:
se = np.nan
# print D1
print("D1 ns from 0: speciation + introgression\nD1 sig +pos speciation followed by introgression\nincreasing D1 is more recent introgression")
print("D1: {}, {}\n".format(d1_m, se))
### D2
d2_m = np.mean(dac_ab) - np.mean(dac_bc)
if step:
# windowed D2
d2SE = []
# D2 calculate sliding window by 100 trees or such
i = 0
j = step
f = open("{}{}{}.D2.{}.txt".format(A, B, C, step), 'w')
while j < len(dac_ab):
d2_win = np.mean(dac_ab[i:j]) - np.mean(dac_bc[i:j])
d2SE.append(d2_win)
f.write("{}\n".format(d2_win))
i = j
j += step
f.close()
# D2 SE
n = len(d2SE)
try:
sv = ((n - 1) / n) * np.nansum((d2SE - d2_m) ** 2)
se = np.sqrt(sv)
except ZeroDivisionError:
se = 0.0000000001
else:
se = np.nan
# print D2
print("D2 ns from 0: C->B\nD2 sig +pos B->C\nincreasing +pos w/ dist from speciation")
print("D2: {}, {}".format(d2_m, se))