|
1 | 1 | import unittest |
| 2 | +from collections import defaultdict |
| 3 | +from unittest import mock |
2 | 4 |
|
3 | 5 | from gff3tool.lib import id_processor |
4 | 6 |
|
5 | 7 |
|
6 | 8 | class DummyGFF: |
7 | 9 | def __init__(self, line_count=0): |
8 | 10 | self.lines = [{} for _ in range(line_count)] |
| 11 | + self.features = defaultdict(list) |
| 12 | + self.removed = [] |
| 13 | + |
| 14 | + def remove(self, model): |
| 15 | + self.removed.append(model) |
9 | 16 |
|
10 | 17 |
|
11 | 18 | class TestIdProcessor(unittest.TestCase): |
@@ -58,6 +65,101 @@ def test_new_child_model_resets_parent_links_and_children(self): |
58 | 65 | self.assertEqual(nchild["attributes"]["Name"], "LOC0002-RA") |
59 | 66 | self.assertEqual(nchild["children"], []) |
60 | 67 |
|
| 68 | + def test_idprocessing_removes_models_marked_removed(self): |
| 69 | + root = { |
| 70 | + "line_type": "feature", |
| 71 | + "attributes": {"ID": "LOC0001"}, |
| 72 | + "children": [], |
| 73 | + } |
| 74 | + removed_model = { |
| 75 | + "line_type": "feature", |
| 76 | + "attributes": {"ID": "LOC0002", "modified_track": "removed"}, |
| 77 | + "children": [], |
| 78 | + } |
| 79 | + gff = DummyGFF() |
| 80 | + gff.lines = [root, removed_model] |
| 81 | + |
| 82 | + id_processor.IDprocessing(gff) |
| 83 | + |
| 84 | + self.assertEqual(gff.removed, [removed_model]) |
| 85 | + self.assertNotIn("modified_track", removed_model["attributes"]) |
| 86 | + |
| 87 | + def test_idprocessing_calls_newnreplace_for_merge_track(self): |
| 88 | + child = {"attributes": {"ID": "LOC0003-RA"}, "children": []} |
| 89 | + model = { |
| 90 | + "line_type": "feature", |
| 91 | + "attributes": {"ID": "LOC0003", "modified_track": "geneA_s1_geneB_s2"}, |
| 92 | + "children": [child], |
| 93 | + } |
| 94 | + root = { |
| 95 | + "line_type": "feature", |
| 96 | + "attributes": {"ID": "LOC0001"}, |
| 97 | + "children": [child], |
| 98 | + } |
| 99 | + gff = DummyGFF() |
| 100 | + gff.lines = [root, model] |
| 101 | + |
| 102 | + with mock.patch.object(id_processor, "idgenerator", return_value={"ID": "LOC0004", "maxnum": 4}) as gen_mock, \ |
| 103 | + mock.patch.object(id_processor, "newNreplaceModel", autospec=True) as replace_mock: |
| 104 | + id_processor.IDprocessing(gff) |
| 105 | + |
| 106 | + gen_mock.assert_called_once_with("LOC", 3, 4) |
| 107 | + replace_mock.assert_called_once_with(model, "LOC0004", gff) |
| 108 | + |
| 109 | + def test_idprocessing_calls_newnreplace_for_split_track(self): |
| 110 | + child = {"attributes": {"ID": "LOC0005-RA"}, "children": []} |
| 111 | + model = { |
| 112 | + "line_type": "feature", |
| 113 | + "attributes": {"ID": "LOC0005", "modified_track": "geneX.s1"}, |
| 114 | + "children": [child], |
| 115 | + } |
| 116 | + root = { |
| 117 | + "line_type": "feature", |
| 118 | + "attributes": {"ID": "LOC0001"}, |
| 119 | + "children": [child], |
| 120 | + } |
| 121 | + gff = DummyGFF() |
| 122 | + gff.lines = [root, model] |
| 123 | + |
| 124 | + with mock.patch.object(id_processor, "idgenerator", return_value={"ID": "LOC0006", "maxnum": 6}) as gen_mock, \ |
| 125 | + mock.patch.object(id_processor, "newNreplaceModel", autospec=True) as replace_mock: |
| 126 | + id_processor.IDprocessing(gff) |
| 127 | + |
| 128 | + gen_mock.assert_called_once_with("LOC", 5, 4) |
| 129 | + replace_mock.assert_called_once_with(model, "LOC0006", gff) |
| 130 | + |
| 131 | + def test_ncbi_naming_system_assigns_root_child_and_cds_attributes(self): |
| 132 | + cds = { |
| 133 | + "line_type": "feature", |
| 134 | + "type": "CDS", |
| 135 | + "attributes": {"ID": "LOC0001-RA-CDS"}, |
| 136 | + "children": [], |
| 137 | + } |
| 138 | + mrna = { |
| 139 | + "line_type": "feature", |
| 140 | + "type": "mRNA", |
| 141 | + "attributes": {"ID": "LOC0001-RA", "Parent": ["LOC0001"], "Name": "product name"}, |
| 142 | + "children": [cds], |
| 143 | + } |
| 144 | + cds["attributes"]["Parent"] = ["LOC0001-RA"] |
| 145 | + root = { |
| 146 | + "line_type": "feature", |
| 147 | + "type": "gene", |
| 148 | + "attributes": {"ID": "LOC0001"}, |
| 149 | + "children": [mrna], |
| 150 | + } |
| 151 | + gff = DummyGFF() |
| 152 | + gff.lines = [root, mrna, cds] |
| 153 | + |
| 154 | + id_processor.ncbiNamingSystem(gff, "TAG") |
| 155 | + |
| 156 | + self.assertEqual(root["attributes"]["locus_tag"], "TAG_LOC0001") |
| 157 | + self.assertEqual(mrna["attributes"]["transcript_id"], "LOC0001-RA") |
| 158 | + self.assertEqual(mrna["attributes"]["protein_id"], "LOC0001-PA") |
| 159 | + self.assertEqual(cds["attributes"]["transcript_id"], "LOC0001-RA") |
| 160 | + self.assertEqual(cds["attributes"]["protein_id"], "LOC0001-PA") |
| 161 | + self.assertEqual(cds["attributes"]["product"], "product name") |
| 162 | + |
61 | 163 |
|
62 | 164 | if __name__ == "__main__": |
63 | 165 | unittest.main() |
0 commit comments