Skip to content

Commit

Permalink
Merge pull request #495 from GavinHuttley/develop
Browse files Browse the repository at this point in the history
app.evo model and hypothesis consistency checks and tests
  • Loading branch information
GavinHuttley committed Jan 16, 2020
2 parents e3abdf2 + c91f52c commit 3add97f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/cogent3/app/evo.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ def _fit_aln(
return lf

def fit(self, aln, initialise=None, construct=True, **opt_args):
moltypes = {aln.moltype.label, self._sm.moltype.label}
if moltypes == {"protein", "dna"} or moltypes == {"protein", "rna"}:
msg = (
f"substitution model moltype '{self._sm.moltype.label}' and"
f" alignment moltype '{aln.moltype.label}' are incompatible"
)
return NotCompleted("ERROR", self, msg, source=aln)

evaluation_limit = opt_args.get("max_evaluations", None)
if self._tree is None:
assert len(aln.names) == 3
Expand Down Expand Up @@ -295,11 +303,21 @@ def test_hypothesis(self, aln):
except ValueError as err:
msg = f"Hypothesis null had bounds error {aln.info.source}"
return NotCompleted("ERROR", self, msg, source=aln)

if not null:
return null

try:
alts = [alt for alt in self._initialised_alt_from_null(null, aln)]
except ValueError as err:
msg = f"Hypothesis alt had bounds error {aln.info.source}"
return NotCompleted("ERROR", self, msg, source=aln)

# check if any did not complete
for alt in alts:
if not alt:
return alt

results = {alt.name: alt for alt in alts}
results.update({null.name: null})

Expand Down
44 changes: 44 additions & 0 deletions tests/test_app/test_evo.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,50 @@ def test_select_model(self):
expect = set(hyp.values())
self.assertEqual(set(got), expect)

def test_null_hyp_fail_error(self):
"""if null fails NotCompleted.origin should be model"""
_data = {
"Human": "ATGCGGCTCGCGGAGGCCGCGCTCGCGGAG",
"Mouse": "ATGCCCGGCGCCAAGGCAGCGCTGGCGGAG",
"Opossum": "ATGCCAGTGAAAGTGGCGGCGGTGGCTGAG",
}
aln = make_aligned_seqs(data=_data, moltype="dna")
tree = "((Mouse,Rat),Human,Opossum)"
m1 = evo_app.model("F81", tree=tree)
m2 = evo_app.model("GTR", tree=tree)
hyp = evo_app.hypothesis(m1, m2)
r = hyp(aln)
self.assertEqual(r.origin, "model")

def test_alt_hyp_fail_error(self):
"""if alt fails NotCompleted.origin should be model"""
_data = {
"Human": "ATGCGGCTCGCGGAGGCCGCGCTCGCGGA",
"Mouse": "ATGCCCGGCGCCAAGGCAGCGCTGGCGGA",
"Opossum": "TGACCAGTGAAAGTGGCGGCGGTGGCTGA",
}
aln = make_aligned_seqs(data=_data, moltype="dna")
tree = "(Mouse,Human,Opossum)"
m1 = evo_app.model("F81", tree=tree)
m2 = evo_app.model("MG94HKY", tree=tree)
hyp = evo_app.hypothesis(m1, m2)
r = hyp(aln)
self.assertEqual(r.origin, "model")

def test_model_moltype_mismatch(self):
"""if model and alignment moltypes incompatible"""
_data = {
"Human": "ATGCGGCTCGCGGAGGCCGCGCTCGCGGA",
"Mouse": "ATGCCCGGCGCCAAGGCAGCGCTGGCGGA",
"Opossum": "TGACCAGTGAAAGTGGCGGCGGTGGCTGA",
}
aln = make_aligned_seqs(data=_data, moltype="dna")
tree = "(Mouse,Human,Opossum)"
m1 = evo_app.model("JTT92", tree=tree)
r = m1(aln)
print(r)
self.assertEqual(r.origin, "model")


class TestAncestralStates(TestCase):
def test_ancestral(self):
Expand Down

0 comments on commit 3add97f

Please sign in to comment.