Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_model(self):
"""get_models successfully creates model instances"""
# just returns query if it's already a substitution model
for mod in (CNFGTR(), WG01(), GN()):
got = get_model(mod)
self.assertEqual(id(got), id(mod))
with self.assertRaises(ValueError):
# unknown model raises exception
_ = get_model("blah")
def _make_model_cache(self):
# constructs all the substitution models
if hasattr(self, "_cached_models"):
return
cache = {}
for name in models:
cache[name] = get_model(name)
self._cached_models = cache
def test_bin_probs(self):
"""posterior bin probs same length as aln for rate-het model"""
aln = load_aligned_seqs("data/primates_brca1.fasta", moltype="dna")
tree = load_tree("data/primates_brca1.tree")
sm = get_model("HKY85", ordered_param="rate", distribution="gamma")
lf = sm.make_likelihood_function(tree, bins=4, sites_independent=False)
lf.set_alignment(aln)
bprobs = lf.get_bin_probs()
self.assertEqual(bprobs.shape[1], len(aln))
def test_get_param_rules_discrete(self):
"""discrete time models produce valid rules"""
sm = get_model("BH")
aln = self.data.take_seqs(self.data.names[:3])
tree = self.tree.get_sub_tree(aln.names)
lf = sm.make_likelihood_function(tree)
lf.set_alignment(aln)
lf.optimise(max_evaluations=100, limit_action="ignore")
rules = lf.get_param_rules()
new_lf = sm.make_likelihood_function(tree)
new_lf.set_alignment(aln)
new_lf.apply_param_rules(rules)
assert_allclose(new_lf.get_motif_probs().array, lf.get_motif_probs().array)
for edge in tree.preorder():
if edge.is_root():
continue
orig_p = lf.get_psub_for_edge(edge.name)
new_p = new_lf.get_psub_for_edge(edge.name)
def _test_aln(self, seqs, model=dna_model, param_vals=None, **kw):
orig = {n: s.replace("-", "") for (n, s) in list(seqs.items())}
aln = self._make_aln(orig, model=model, param_vals=param_vals, **kw)
result = {n: s.lower() for (n, s) in list(aln.to_dict().items())}
# assert the alignment result is correct
self.assertEqual(seqs, result)
# and the moltype matches the model
model = get_model(model)
self.assertIs(aln.moltype, model.moltype)
# assert the returned alignment has the correct parameter values in the
# align.info object.
if param_vals:
for param, val in param_vals:
self.assertEqual(aln.info.align_params[param], val)
def test_roundtrip_from_file(self):
"""correctly roundtrips a likelihood function fro json file"""
_data = {
"Human": "ATGCGGCTCGCGGAGGCCGCGCTCGCGGAG",
"Mouse": "ATGCCCGGCGCCAAGGCAGCGCTGGCGGAG",
"Opossum": "ATGCCAGTGAAAGTGGCGGCGGTGGCTGAG",
}
aln = make_aligned_seqs(data=_data, moltype="dna")
tree = make_tree(tip_names=aln.names)
sm = get_model("HKY85")
lf = sm.make_likelihood_function(tree)
lf.set_alignment(aln)
edge_vals = zip(aln.names, (2, 3, 4))
for edge, val in edge_vals:
lf.set_param_rule("kappa", edge=edge, init=val)
lnL = lf.get_log_likelihood()
data = lf.to_json()
with TemporaryDirectory(dir=".") as dirname:
outpath = dirname + "/delme.json"
with open(outpath, "w") as outfile:
outfile.write(data)
got = deserialise_object(outpath)
self.assertFloatEqual(got.get_log_likelihood(), lnL)
def test_codon_rate_het(self):
"""recap rate het likelihoods"""
aln = load_aligned_seqs("data/primate_brca1.fasta", moltype=DNA)
tree = load_tree("data/primate_brca1.tree")
cnf = get_model("CNFGTR")
rate_lf = cnf.make_likelihood_function(
tree, bins=["neutral", "adaptive"], digits=2, space=3
)
rate_lf.set_alignment(aln)
rules = [
{
"par_name": "mprobs",
"edges": None,
"value": {
"TTT": 0.018732866280840695,
"TTC": 0.007767286018885166,
"TTA": 0.02116966189460859,
"TTG": 0.010813280536095034,
"TCT": 0.02512945476698142,
"TCC": 0.008224185196466647,
def test_roundtrip_submod(self):
"""substitution model to_json enables roundtrip"""
sm = get_model("HKY85")
data = sm.to_json()
got = deserialise_object(data)
self.assertEqual(got.to_rich_dict(), sm.to_rich_dict())
sm = get_model("GN")
data = sm.to_json()
got = deserialise_object(data)
self.assertEqual(got.to_rich_dict(), sm.to_rich_dict())
sm = get_model("CNFGTR")
data = sm.to_json()
got = deserialise_object(data)
self.assertEqual(got.to_rich_dict(), sm.to_rich_dict())
def test_roundtrip_model_result(self):
"""mode_result.to_json enables roundtrip and lazy evaluation"""
_data = {
"Human": "ATGCGGCTCGCGGAGGCCGCGCTCGCGGAG",
"Mouse": "ATGCCCGGCGCCAAGGCAGCGCTGGCGGAG",
"Opossum": "ATGCCAGTGAAAGTGGCGGCGGTGGCTGAG",
}
aln = make_aligned_seqs(data=_data, moltype="dna")
tree = make_tree(tip_names=aln.names)
sm = get_model("HKY85")
lf = sm.make_likelihood_function(tree)
lf.set_alignment(aln)
edge_vals = zip(aln.names, (2, 3, 4))
for edge, val in edge_vals:
lf.set_param_rule("kappa", edge=edge, init=val)
result = model_result(name="test")
result[1] = lf
self.assertIs(result[1], lf)
self.assertEqual(result.nfp, lf.nfp)
self.assertEqual(result.lnL, lf.lnL)
data = result.to_json()
got_obj = deserialise_object(data)
# lazy evaluation means initially, the value is a dict
self.assertIsInstance(got_obj[1], dict)
# and properties match original