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_natsel_timehet(self):
"""natsel_timehet works"""
opt = dict(max_evaluations=2, limit_action="ignore")
aln = load_aligned_seqs("data/primate_brca1.fasta", moltype="dna")
natsel = evo_app.natsel_timehet(
"MG94HKY",
tree="data/primate_brca1.tree",
tip1="Human",
tip2="Chimpanzee",
opt_args=opt,
)
result = natsel(aln)
self.assertEqual(result.df, 1)
# the naming scheme is model name followed by null/alt
self.assertTrue("MG94HKY-null" in result)
self.assertTrue("MG94HKY-alt" in result)
# that is_independent works
natsel = evo_app.natsel_timehet(
"MG94HKY",
tree="data/primate_brca1.tree",
def test_load_aligned_seqs_from_json(self):
"""tests loading an aligned object from json file"""
with TemporaryDirectory(dir=".") as dirname:
path = os.path.join(data_path, "brca1_5.paml")
alignment = load_aligned_seqs(path, array_align=False, moltype="dna")
alignment_json_path = os.path.join(dirname, "alignment.json")
alignment.write(alignment_json_path)
array_alignment = load_aligned_seqs(path, moltype="dna")
array_alignment_json_path = os.path.join(dirname, "array_alignment.json")
array_alignment.write(array_alignment_json_path)
# tests case Alignment
got = load_aligned_seqs(alignment_json_path)
self.assertIsInstance(got, Alignment)
self.assertEqual(got.moltype.label, "dna")
self.assertTrue("Human" in got.to_dict())
self.assertEqual(got.info["source"], alignment_json_path)
# tests case ArrayAlignment
got = load_aligned_seqs(array_alignment_json_path)
self.assertIsInstance(got, ArrayAlignment)
self.assertEqual(got.moltype.label, "dna")
self.assertTrue("Mouse" in got.to_dict())
self.assertEqual(got.info["source"], array_alignment_json_path)
# tests json generated by make_record_for_json
uncompleted_record = make_record_for_json("delme", got, False)
completed_record = make_record_for_json("delme", got, True)
uncompleted_record_path = os.path.join(dirname, "uncompleted_record.json")
completed_record_path = os.path.join(dirname, "completed_record.json")
def test_deepcopy(self):
"""correctly deepcopy Aligned objects in an alignment"""
path = "data/brca1_5.paml"
# generates an annotatable Alignment object
aln = load_aligned_seqs(path, array_align=False, moltype="dna")
# when the annotation is outside(before) boundary of the slice
aln.named_seqs["NineBande"].data.add_annotation(
Feature, "exon", "annot1", [(0, 10)]
)
# when the annotation is across boundary of the slice
aln.named_seqs["Mouse"].data.add_annotation(
Feature, "exon", "annot2", [(10, 21)]
)
# when the annotation is within boundary of the slice
aln.named_seqs["Human"].data.add_annotation(
Feature, "exon", "annot3", [(20, 25)]
)
# when the annotation is across boundary of the slice
aln.named_seqs["HowlerMon"].data.add_annotation(
Feature, "exon", "annot4", [(25, 32)]
)
def test_load_seqs_interface(self):
"""load_aligned_seqs correctly loads nexus alignments"""
aln = load_aligned_seqs("data/nexus_mixed.nex")
self.assertEqual(aln.num_seqs, 4)
self.assertEqual(len(aln), 20)
aln = load_aligned_seqs("data/nexus_aa.nxs")
self.assertEqual(aln.num_seqs, 10)
self.assertEqual(len(aln), 234)
def test_zhang_mtseq(self):
"""genetic code setting should work"""
from cogent3.app.composable import NotCompleted
opt = dict(max_evaluations=20, limit_action="ignore")
aln = load_aligned_seqs("data/ENSG00000198712.fa", moltype="dna")
natsel = evo_app.natsel_zhang("CNFGTR", tip1="Human", opt_args=opt, gc=2)
result = natsel(aln)
self.assertEqual(result.df, 3)
# but if provide wrong gc, get NotCompleted
natsel = evo_app.natsel_zhang("CNFGTR", tip1="Human", opt_args=opt, gc=1)
result = natsel(aln)
self.assertIsInstance(result, NotCompleted)
def test_time_rate_het(self):
"""recap the zhang model"""
aln = load_aligned_seqs("data/primate_brca1.fasta", moltype=DNA)
tree = load_tree("data/primate_brca1.tree")
cnf = get_model("CNFGTR")
lf = cnf.make_likelihood_function(
tree, bins=["0", "1", "2a", "2b"], digits=2, space=3
)
lf.set_alignment(aln)
epsilon = 1e-6
lf.set_param_rule("omega", bins=["0", "2a"], upper=1.0, init=1 - epsilon)
lf.set_param_rule("omega", bins=["1", "2b"], is_constant=True, value=1.0)
lf.set_param_rule(
"omega",
bins=["2a", "2b"],
edges=["Chimpanzee", "Human"],
init=99,
lower=1.0,
upper=100.0,
def test_simple_codon(self):
"""return same likelihood for simple codon model"""
# from docs/examples/neutral_test
aln = load_aligned_seqs("data/long_testseqs.fasta", moltype="dna")
tree = load_tree("data/long_testseqs.tree")
sm = get_model("MG94GTR")
lf = sm.make_likelihood_function(tree, digits=3, space=2)
lf.set_alignment(aln)
mprobs = {
"T": 0.23167456556082147,
"C": 0.18775671406003158,
"A": 0.36808846761453395,
"G": 0.21248025276461296,
}
lengths = {
"Human": 0.0930121148197949,
"HowlerMon": 0.12455050902011401,
"edge.0": 0.11566361642563996,
"Mouse": 0.8352888057852214,
"edge.1": 0.05801595370263309,
with TemporaryDirectory(dir=".") as dirname:
path = os.path.join(data_path, "brca1_5.paml")
alignment = load_aligned_seqs(path, array_align=False, moltype="dna")
alignment_json_path = os.path.join(dirname, "alignment.json")
alignment.write(alignment_json_path)
array_alignment = load_aligned_seqs(path, moltype="dna")
array_alignment_json_path = os.path.join(dirname, "array_alignment.json")
array_alignment.write(array_alignment_json_path)
# tests case Alignment
got = load_aligned_seqs(alignment_json_path)
self.assertIsInstance(got, Alignment)
self.assertEqual(got.moltype.label, "dna")
self.assertTrue("Human" in got.to_dict())
self.assertEqual(got.info["source"], alignment_json_path)
# tests case ArrayAlignment
got = load_aligned_seqs(array_alignment_json_path)
self.assertIsInstance(got, ArrayAlignment)
self.assertEqual(got.moltype.label, "dna")
self.assertTrue("Mouse" in got.to_dict())
self.assertEqual(got.info["source"], array_alignment_json_path)
# tests json generated by make_record_for_json
uncompleted_record = make_record_for_json("delme", got, False)
completed_record = make_record_for_json("delme", got, True)
uncompleted_record_path = os.path.join(dirname, "uncompleted_record.json")
completed_record_path = os.path.join(dirname, "completed_record.json")
with open(uncompleted_record_path, "w") as out:
out.write(json.dumps(uncompleted_record))
with open(completed_record_path, "w") as out:
out.write(json.dumps(completed_record))
# tests when provided record json file is uncompleted
with self.assertRaises(TypeError):
def test_dotplot_regression(self):
"""Tests whether dotplot produces traces and in correct ordering."""
aln = load_aligned_seqs("data/brca1.fasta", moltype="dna")
aln = aln.take_seqs(["Human", "Chimpanzee"])
aln = aln[:200]
dp = aln.dotplot()
_ = dp.figure
trace_names = [tr.name for tr in dp.traces]
self.assertTrue(
[tr.name for tr in dp.traces] != [] and len(trace_names) == len(dp.traces),
"No traces found for dotplot",
)
self.assertTrue(
[trace_names[i] == dp.traces[i]["name"] for i in range(len(trace_names))],
"Order of traces don't match with dp traces",
)
for trace_name in trace_names:
def setUp(self):
self.alignment = load_aligned_seqs(
filename=os.path.join(data_path, "brca1_5.paml")
)