Skip to content

Commit 9d7c56e

Browse files
committed
Fixes from autopep8
1 parent 0b5ec88 commit 9d7c56e

12 files changed

+84
-15
lines changed

tact/cli_add_taxa.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
global mrca_rates
3939
mrca_rates = {}
4040

41+
4142
def search_ancestors_for_valid_backbone_node(taxonomy_node, backbone_tips, ccp):
4243
global invalid_map
4344
seen = []
@@ -69,6 +70,7 @@ def search_ancestors_for_valid_backbone_node(taxonomy_node, backbone_tips, ccp):
6970
invalid_map[x] = taxonomy_target
7071
return (taxonomy_target, backbone_target)
7172

73+
7274
def get_new_branching_times(backbone_node, taxonomy_node, backbone_tree, told=None, tyoung=0, min_ccp=0.8, num_new_times=None):
7375
"""
7476
Get `n_total` new branching times for a `node`.
@@ -108,6 +110,7 @@ def get_new_branching_times(backbone_node, taxonomy_node, backbone_tree, told=No
108110
logger.debug((" {}: " + ", ".join(["{:.2f}" for x in times])).format(taxon, *times))
109111
return times
110112

113+
111114
def fill_new_taxa(namespace, node, new_taxa, times, stem=False, excluded_nodes=None):
112115
for new_species, new_age in zip(new_taxa, times):
113116
new_node = dendropy.Node()
@@ -123,6 +126,7 @@ def fill_new_taxa(namespace, node, new_taxa, times, stem=False, excluded_nodes=N
123126

124127
return node
125128

129+
126130
def graft_node(graft_recipient, graft, stem=False):
127131
"""
128132
Grafts a node `graft` randomly in the subtree below node
@@ -182,6 +186,7 @@ def filter_fn(x):
182186
return graft
183187
return graft_recipient
184188

189+
185190
def create_clade(namespace, species, ages):
186191
tree = dendropy.Tree(taxon_namespace=namespace)
187192
species = list(species)
@@ -220,6 +225,7 @@ def create_clade(namespace, species, ages):
220225
logger.warning("{} short branches detected".format(len(list(get_short_branches(tree.seed_node)))))
221226
return tree
222227

228+
223229
def lock_clade(node):
224230
pre = count_locked(node)
225231
for edge in edge_iter(node):
@@ -228,18 +234,22 @@ def lock_clade(node):
228234
if pre != post:
229235
logger.debug("locking clade: {} => {}".format(pre, post))
230236

237+
231238
def count_locked(node):
232239
sum([x.label == "locked" for x in edge_iter(node)])
233240

241+
234242
def is_fully_locked(node):
235243
return all([x.label == "locked" for x in edge_iter(node)])
236244

245+
237246
def get_min_age(node):
238247
try:
239248
return min([x.head_node.age for x in edge_iter(node) if x.label != "locked"])
240249
except ValueError:
241250
return 0.0
242251

252+
243253
def process_node(backbone_tree, backbone_bitmask, all_possible_tips, taxon_node, min_ccp, default_birth, default_death, yule=False):
244254
# TODO: Fix all the returns and refactor this into something sane
245255
global mrca_rates
@@ -286,6 +296,7 @@ def process_node(backbone_tree, backbone_bitmask, all_possible_tips, taxon_node,
286296
logger.debug("MRCA: {} b={}, d={}, sf={}, ccp={}".format(taxon, birth, death, sf, ccp))
287297
mrca_rates[taxon] = (birth, death, ccp, "computed")
288298

299+
289300
def run_precalcs(taxonomy_tree, backbone_tree, min_ccp=0.8, min_extant=3, yule=False):
290301
global mrca_rates
291302
tree_tips = get_tip_labels(backbone_tree)
@@ -311,12 +322,14 @@ def run_precalcs(taxonomy_tree, backbone_tree, min_ccp=0.8, min_extant=3, yule=F
311322
logger.debug("FastMRCA calculation time: {:.1f} seconds".format(diff))
312323
return mrca_rates
313324

325+
314326
def update_tree_view(tree):
315327
# Stuff that DendroPy needs to keep a consistent view of the phylgoeny
316328
tree.calc_node_ages()
317329
tree.update_bipartitions()
318330
return get_tip_labels(tree)
319331

332+
320333
def compute_node_depths(tree):
321334
res = dict()
322335
for leaf in tree.leaf_node_iter():
@@ -327,6 +340,7 @@ def compute_node_depths(tree):
327340
res[leaf.taxon.label] = cnt
328341
return res
329342

343+
330344
@click.command()
331345
@click.option("--taxonomy", help="a taxonomy tree", type=click.File("r"), required=True)
332346
@click.option("--backbone", help="the backbone tree to attach the taxonomy tree to", type=click.File("r"), required=True)
@@ -411,9 +425,9 @@ def main(taxonomy, backbone, outgroups, output, min_ccp, verbose, yule):
411425
initial_length = len(tree_tips)
412426

413427
bar = click.progressbar(label="TACT",
414-
length=len(all_possible_tips) - initial_length,
415-
show_pos=True,
416-
item_show_func=lambda x: x)
428+
length=len(all_possible_tips) - initial_length,
429+
show_pos=True,
430+
item_show_func=lambda x: x)
417431

418432
def bar_update():
419433
bar.pos = len(tree_tips) - initial_length
@@ -537,5 +551,6 @@ def bar_update():
537551
tree.write(path=output + ".nexus.tre", schema="nexus")
538552
print()
539553

554+
540555
if __name__ == '__main__':
541556
main()

tact/cli_check_trees.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from .lib import get_monophyletic_node, get_birth_death_rates, get_tree, get_tip_labels
1818

19+
1920
def analyze_taxon(bb_tips, st_tips, backbone, simtaxed, taxon_node):
2021
taxon = taxon_node.label
2122
if not taxon:
@@ -103,5 +104,6 @@ def main(simulated, backbone, taxonomy, output, cores, chunksize):
103104
if result:
104105
writer.writerow(result)
105106

107+
106108
if __name__ == '__main__':
107109
main()

tact/cli_taxonomy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def wc(filename):
1414
pass
1515
return ii + 1
1616

17+
1718
def build_taxonomic_tree(filename):
1819
"""
1920
Builds a taxonomic tree given a filename. Last column is assumed to
@@ -60,6 +61,7 @@ def build_taxonomic_tree(filename):
6061
tn.is_mutable = False
6162
return tree
6263

64+
6365
@click.command()
6466
@click.argument("taxonomy", type=click.Path(exists=True, dir_okay=False, readable=True))
6567
@click.option("--output", help="name of the output taxonomic tree", required=True, type=click.Path(writable=True))

tact/fastmrca.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
global tree
1313

14+
1415
def initialize(phy):
1516
"""
1617
Initialize the fastmrca singleton with a tree.
1718
"""
1819
global tree
1920
tree = phy
2021

22+
2123
def bitmask(labels):
2224
"""
2325
Gets a bitmask for the taxa in `labels`, potentially in parallel.
@@ -26,6 +28,7 @@ def bitmask(labels):
2628
tn = tree.taxon_namespace
2729
return tn.taxa_bitmask(labels=labels)
2830

31+
2932
def get(labels):
3033
"""Pulls a MRCA node out for the taxa in `labels`."""
3134
global tree
@@ -36,6 +39,7 @@ def get(labels):
3639
if mrca and labels.issuperset(get_tip_labels(mrca)):
3740
return mrca
3841

42+
3943
def fastmrca_getter(tn, x):
4044
"""Helper function for submitting stuff."""
4145
taxa = tn.get_taxa(labels=x)

tact/lib.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
deaths = np.linspace(0, 5, num=100)
2222
params = [(x, y) for (x, y) in itertools.product(births, deaths) if x > y]
2323

24+
2425
def get_bd(r, a):
2526
"""Converts turnover and relative extinction to birth and death rates."""
2627
return -r / (a - 1), -a * r / (a - 1)
2728

29+
2830
def get_ra(b, d):
2931
return (b - d, d / b)
3032

33+
3134
def optim_bd_r(ages, sampling):
3235
"""Optimizes birth death using TreePar and R"""
3336
script = """cat(optim(c({birth}, {death}), function(v, ...) TreePar::LikConstant(v[1], v[2], ...), x = c({ages}), sampling = {sampling}, lower=c(.Machine$double.xmin,0), method = "L-BFGS-B")$par, " dum")"""
@@ -36,18 +39,21 @@ def optim_bd_r(ages, sampling):
3639
b, d, _ = output.split(None, 2)
3740
return float(b), float(d)
3841

42+
3943
def optim_bd_grid(ages, sampling):
4044
"""Optimizes birth death using a grid search"""
4145
res = [lik_constant(x, sampling, ages) for x in params]
4246
return params[np.argmin(res)]
4347

48+
4449
def update_multiplier_freq(q, d=1.1):
4550
u = np.random.uniform(0, 1, 2)
4651
l = 2 * log(d)
4752
m = np.exp(l * (u - 0.5))
4853
new_q = q * m
4954
return new_q
5055

56+
5157
def optim_bd_mcmc(ages, sampling):
5258
"""Optimizes birth death using a cheap MCMC-like algorithm"""
5359
new_vec = [0, 0]
@@ -66,6 +72,7 @@ def optim_bd_mcmc(ages, sampling):
6672
vec = new_vec
6773
return vec
6874

75+
6976
def wrapped_lik_constant(x, sampling, ages):
7077
return lik_constant(get_bd(*x), sampling, ages)
7178

@@ -80,10 +87,12 @@ def optim_bd_scipy(ages, sampling):
8087
bounds = ((1e-6, None), (0, 1 - 1e-6))
8188
return get_bd(*minimize(wrapped_lik_constant, (init_r, 0.0), args=(sampling, ages), bounds=bounds, method="TNC")["x"].tolist())
8289

90+
8391
def optim_bd(ages, sampling):
8492
return optim_bd_scipy(ages, sampling)
8593

86-
def optim_yule(ages,sampling):
94+
95+
def optim_yule(ages, sampling):
8796
"""Optimizes a Yule model using Scipy"""
8897
if max(ages) < 0.000001:
8998
init_r = 1e-3
@@ -93,6 +102,7 @@ def optim_yule(ages,sampling):
93102
bounds = ((1e-6, None), (0, 0))
94103
return get_bd(*minimize(wrapped_lik_constant, (init_r, 0.0), args=(sampling, ages), bounds=bounds, method="TNC")["x"].tolist())
95104

105+
96106
def get_lik(vec, rho, x):
97107
l = vec[0]
98108
m = vec[1]
@@ -102,19 +112,22 @@ def get_lik(vec, rho, x):
102112
lik3 = - (root + 1) * np.log(1 - p0(x[0], l, m, rho))
103113
return lik1 + lik2 + lik3
104114

115+
105116
def p0_exact(t, l, m, rho):
106117
t = D(t)
107118
l = D(l)
108119
m = D(m)
109120
rho = D(rho)
110121
return D(1) - rho * (l - m) / (rho * l + (l * (D(1) - rho) - m) * (-(l - m) * t).exp())
111122

123+
112124
def p0(t, l, m, rho):
113125
try:
114126
return 1 - rho * (l - m) / (rho * l + (l * (1 - rho) - m) * exp(-(l - m) * t))
115127
except FloatingPointError:
116128
return float(p0_exact(t, l, m, rho))
117129

130+
118131
def p1_exact(t, l, m, rho):
119132
"""Exact version of p1 using Decimal math."""
120133
t = D(t)
@@ -125,6 +138,7 @@ def p1_exact(t, l, m, rho):
125138
denom = (rho * l + (l * (1 - rho) - m) * (-(l - m) * t).exp()) ** D(2)
126139
return num / denom
127140

141+
128142
def p1_orig(t, l, m, rho):
129143
try:
130144
num = rho * (l - m) ** 2 * np.exp(-(l - m) * t)
@@ -133,6 +147,7 @@ def p1_orig(t, l, m, rho):
133147
except (OverflowError, FloatingPointError):
134148
return float(p1_exact(t, l, m, rho))
135149

150+
136151
def p1(t, l, m, rho):
137152
# Optimized version of p1 using common subexpression elimination and strength reduction from
138153
# exponentiation to multiplication.
@@ -144,6 +159,7 @@ def p1(t, l, m, rho):
144159
except (OverflowError, FloatingPointError):
145160
return float(p1_exact(t, l, m, rho))
146161

162+
147163
def intp1_exact(t, l, m):
148164
"""Exact version of intp1 using Decimal math."""
149165
l = D(l)
@@ -153,12 +169,14 @@ def intp1_exact(t, l, m):
153169
denom = (l - m * (-(l - m) * t).exp())
154170
return num / denom
155171

172+
156173
def intp1(t, l, m):
157174
try:
158-
return (1 - exp(-(l - m) * t))/(l - m * exp(-(l - m) * t))
175+
return (1 - exp(-(l - m) * t)) / (l - m * exp(-(l - m) * t))
159176
except OverflowError:
160177
return float(intp1_exact(t, l, m))
161178

179+
162180
def lik_constant(vec, rho, t, root=1, survival=1, p1=p1):
163181
"""
164182
Calculates the likelihood of a constant-rate birth-death process, conditioned
@@ -210,6 +228,7 @@ def crown_capture_probability(n, k):
210228
return 0 # not technically correct but it works for our purposes
211229
return 1 - 2 * (n - k) / ((n - 1) * (k + 1))
212230

231+
213232
def get_monophyletic_node(tree, species):
214233
"""Returns the node or None that is the MRCA of the `species` in `tree`."""
215234
mrca = tree.mrca(taxon_labels=species)
@@ -218,6 +237,7 @@ def get_monophyletic_node(tree, species):
218237
if mrca and species.issuperset(get_tip_labels(mrca)):
219238
return mrca
220239

240+
221241
def get_birth_death_rates(node, sampfrac, yule=False, include_root=False):
222242
"""
223243
Estimates the birth and death rates for the subtree descending from
@@ -229,18 +249,21 @@ def get_birth_death_rates(node, sampfrac, yule=False, include_root=False):
229249
else:
230250
return optim_bd(get_ages(node, include_root), sampfrac)
231251

252+
232253
def get_ages(node, include_root=False):
233254
ages = [x.age for x in node.ageorder_iter(include_leaves=False, descending=True)]
234255
if include_root:
235256
ages += [node.age]
236257
return ages
237258

259+
238260
def get_tip_labels(tree_or_node):
239261
try:
240262
return set([x.taxon.label for x in tree_or_node.leaf_node_iter()])
241263
except AttributeError:
242264
return set([x.taxon.label for x in tree_or_node.leaf_iter()])
243265

266+
244267
def edge_iter(node, filter_fn=None):
245268
"""
246269
Iterates over the child edge of `node` and all its descendants.
@@ -253,6 +276,7 @@ def edge_iter(node, filter_fn=None):
253276
yield edge
254277
stack.extend(edge.head_node.child_edge_iter())
255278

279+
256280
def get_tree(path, namespace=None):
257281
"""
258282
Gets a DendroPy tree from a path and precalculate its node ages and bipartition bitmask.
@@ -262,19 +286,23 @@ def get_tree(path, namespace=None):
262286
tree.encode_bipartitions()
263287
return tree
264288

289+
265290
def is_binary(node):
266291
"""Is the subtree under `node` a fully bifurcating tree?"""
267292
for x in node.preorder_internal_node_iter():
268293
if len(x.child_nodes()) != 2:
269294
return False
270295
return True
271296

297+
272298
def get_short_branches(node):
273299
for edge in edge_iter(node):
274300
if edge.length <= 0.001:
275301
yield edge
276302

277303
# TODO: This could probably be optimized
304+
305+
278306
def get_new_times(ages, birth, death, missing, told=None, tyoung=None):
279307
"""
280308
Simulates new speciation events in an incomplete phylogeny assuming a

0 commit comments

Comments
 (0)