1
|
|
|
import pytest |
|
|
|
|
2
|
|
|
|
3
|
|
|
from mandos import MandosResources |
4
|
|
|
from mandos.model.caches import TaxonomyFactories |
5
|
|
|
from mandos.model.taxonomy import Taxon, Taxonomy, _Taxon |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestFind: |
|
|
|
|
9
|
|
|
def test_find(self): |
|
|
|
|
10
|
|
|
tax = TaxonomyFactories.from_vertebrata().load(7742) |
11
|
|
|
assert len(tax) == 100670 |
12
|
|
|
assert tax.roots == [Taxon(7742, "Vertebrata", None, set())] |
13
|
|
|
assert len(tax.roots[0].descendents) == 100669 |
14
|
|
|
assert tax[7742] is not None |
15
|
|
|
assert tax[7742].name == "Vertebrata" |
16
|
|
|
assert tax[7742].parent is None |
17
|
|
|
assert tax[117571].id == 117571 |
18
|
|
|
assert tax[117571].name == "Euteleostomi" |
19
|
|
|
assert tax[10116].name == "Rattus norvegicus" |
20
|
|
|
assert 117571 in tax |
21
|
|
|
assert [c.id for c in tax[117571].children] == [7898, 8287] |
22
|
|
|
with pytest.raises(KeyError): |
23
|
|
|
assert tax[3343463643446436347457475] |
24
|
|
|
assert tax.get(3343463643446436347457475) is None |
25
|
|
|
assert tax.get(117571).id == 117571 |
26
|
|
|
assert 117571 in tax |
27
|
|
|
assert 13745754745745 not in tax |
28
|
|
|
|
29
|
|
|
def test_empty(self): |
|
|
|
|
30
|
|
|
tax = Taxonomy({}) |
31
|
|
|
assert len(tax) == 0 |
32
|
|
|
assert tax.roots == [] |
33
|
|
|
|
34
|
|
|
def test_root_leaf(self): |
|
|
|
|
35
|
|
|
taxon = Taxon(1, "abc", None, set()) |
36
|
|
|
tax = Taxonomy.from_list([taxon]) |
37
|
|
|
assert len(tax) == 1 |
38
|
|
|
assert tax.roots == [taxon] |
39
|
|
|
assert tax.leaves == [taxon] |
40
|
|
|
|
41
|
|
|
def test_double(self): |
|
|
|
|
42
|
|
|
a = _Taxon(1, "a", None, set()) |
|
|
|
|
43
|
|
|
b = _Taxon(2, "b", a, set()) |
|
|
|
|
44
|
|
|
a.add_child(b) |
45
|
|
|
tax = Taxonomy.from_list([a, b]) |
46
|
|
|
assert len(tax) == 2 |
47
|
|
|
assert tax.roots == [a] |
48
|
|
|
assert tax.leaves == [b] |
49
|
|
|
assert set(tax.taxa) == {a, b} |
50
|
|
|
under = tax.subtree(1) |
51
|
|
|
assert len(under) == 2 |
52
|
|
|
assert under[1] == a |
53
|
|
|
assert under[2] == b |
54
|
|
|
under = tax.subtree(2) |
55
|
|
|
assert len(under) == 1 |
56
|
|
|
assert under[2] == b |
57
|
|
|
|
58
|
|
|
def test_sort(self): |
|
|
|
|
59
|
|
|
a = _Taxon(10, "z", None, set()) |
|
|
|
|
60
|
|
|
b = _Taxon(2, "a", a, set()) |
|
|
|
|
61
|
|
|
c = _Taxon(4, "b", a, set()) |
|
|
|
|
62
|
|
|
assert b < c, f"{b} vs {c}" |
63
|
|
|
assert b < c < a, f"{a} vs {b} vs {c}" |
64
|
|
|
|
65
|
|
|
def test_real(self): |
|
|
|
|
66
|
|
|
path = MandosResources.path("7742.tab.gz") |
67
|
|
|
tax = Taxonomy.from_path(path) |
68
|
|
|
assert len(tax) == 100670 |
69
|
|
|
tax = tax.subtree(117571) |
70
|
|
|
# number from https://www.uniprot.org/taxonomy/117571 |
71
|
|
|
assert len(tax) == 97993 |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
if __name__ == "__main__": |
75
|
|
|
pytest.main() |
76
|
|
|
|