Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gregdenay committed Jun 14, 2024
1 parent 5c2adc0 commit 41fc46b
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 81 deletions.
1 change: 0 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
**New features**

* It is now possible to create Newick tree with `Taxonomy.toNewick`
* It is now possible to clip Taxonomies with `Taxonomy.clip()`, which behaves similarly to `Taxonomy.prune()` but removes upstream nodes and reroots tree.

## 2.5.2

Expand Down
71 changes: 1 addition & 70 deletions taxidTools/Taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ def prune(self, taxid: Union[str, int], inplace: Optional[bool] = True) -> None:
tax = self.copy()

# Getting upstream nodes
nodes = tax.getAncestry(taxid)
nodes = self.getAncestry(taxid)

# Unlinking other branches from upstream nodes
# No need to change parents of the other nodes,
Expand Down Expand Up @@ -811,75 +811,6 @@ def write(self, path: str) -> None:
with open(path, 'w') as fi:
fi.write(writer)

def clip(self, taxid: Union[str, int], inplace: Optional[bool] = True) -> None:
"""
Clip the Taxonomy at the given taxid
Nodes not in the lineage (upwards and downwards)
of the given taxid will be discarded.
The Ancestors of the given taxid will NOT be kept and the given
node will become the new root!
Parameters
----------
taxid: str or int
taxid whose Lineage to keep
inplace: bool, optional
perfrom the operation inplace and mutate the underlying objects
or return a mutated copy of the instance, keep the original unchanged
Returns
-------
None
See Also
--------
Taxonomy.prune
Examples
--------
>>> node0 = Node(taxid = 0, name = "root",
rank = "root", parent = None)
>>> node1 = Node(taxid = 1, name = "node1",
rank = "rank1", parent = node0)
>>> node2 = Node(taxid = 2, name = "node2",
rank = "rank1", parent = node0)
>>> node11 = Node(taxid = 11, name = "node11",
rank = "rank2", parent = node1)
>>> node12 = Node(taxid = 12, name = "node12",
rank = "rank2", parent = node1)
>>> tax = Taxonomy.from_list([node0, node1, node2, node11, node12])
>>> tax.clip(1)
Ancestry not kept
>>> tax.getAncestry(11)
Lineage([Node(11)])
Other branches are gone
>>> tax.get('2')
None
"""
if inplace:
tax = self
else:
tax = self.copy()

# Getting upstream nodes
nodes = tax.getAncestry(taxid)

# Removing upstream nodes
for i in range(1, len(nodes)):
tax.pop(nodes[i])

# Rerooting taxonomy
taxid.parent = None
tax.root = taxid

if not inplace:
return tax

def toNewick(self, names: str = 'name') -> str:
"""
Generate a Newock string fro the current taxonomy
Expand Down
2 changes: 1 addition & 1 deletion tests/test_Taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_ancestry_tests(self):
def test_copy(self):
self.new = self.txd.copy()
self.txd.data = {}
self.assertRaises(taxidTools.InvalidNodeError, self.txd[0])
self.assertIsNone(self.txd.get('0'))
self.assertIsNotNone(self.new.get('0', None))

def test_InvalidNodeError(self):
Expand Down
11 changes: 2 additions & 9 deletions tests/test_complextree.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,5 @@ def test_insert_nodes_recc(self):
self.txd.addNode(node3)
new_nodes = _insert_nodes_recc(self.node0, ['rank3', 'rank2', 'rank1.5', 'rank1'])
self.assertEqual(len(new_nodes), 12)

self.assertRaises(ValueError, _insert_nodes_recc, self.node0, ['root'])

def test_clip(self):
subtree = self.txd.clip(1)
self.assertEqual(self.txd.root, self.txd['1'])
self.assertIsNone(self.txd['1'].parent)
self.assertRaises(taxidTools.InvalidNodeError, self.txd.__getitem__, '0')
self.assertRaises(taxidTools.InvalidNodeError, self.txd.__getitem__, '2')
with self.assertRaises(ValueError):
_insert_nodes_recc(self.node0, ['root'])

0 comments on commit 41fc46b

Please sign in to comment.