Skip to content

Commit

Permalink
clean up code for Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
jhcepas committed Mar 30, 2020
1 parent 465f56f commit 59dea1b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
20 changes: 10 additions & 10 deletions ete3/parser/newick.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,19 +225,19 @@ class as the root(This allows to work with custom TreeNode
You can also take advantage from this behaviour to concatenate
several tree structures.
"""

if root_node is None:
from ..coretype.tree import TreeNode
root_node = TreeNode()

if isinstance(newick, six.string_types):

# try to determine whether the file exists.
# try to determine whether the file exists.
# For very large trees, if newick contains the content of the tree, rather than a file name,
# this may fail, at least on Windows, because the os fails to stat the file content, deeming it
# this may fail, at least on Windows, because the os fails to stat the file content, deeming it
# too long for testing with os.path.exists. This raises a ValueError with description
# "stat: path too long for Windows". This is described in issue #258
try:
try:
file_exists = os.path.exists(newick)
except ValueError: # failed to stat
file_exists = False
Expand All @@ -246,9 +246,11 @@ class as the root(This allows to work with custom TreeNode
if file_exists:
if newick.endswith('.gz'):
import gzip
nw = gzip.open(newick).read()
with gzip.open(newick) as INPUT:
nw = INPUT.read()
else:
nw = open(newick, 'r').read()
with open(newick) as INPUT:
nw = INPUT.read()
else:
nw = newick

Expand All @@ -269,7 +271,7 @@ class as the root(This allows to work with custom TreeNode
def _read_newick_from_string(nw, root_node, matcher, formatcode, quoted_names):
""" Reads a newick string in the New Hampshire format. """

if quoted_names:
if quoted_names:
# Quoted text is mapped to references
quoted_map = {}
unquoted_nw = ''
Expand Down Expand Up @@ -416,7 +418,7 @@ def _read_node_data(subnw, current_node, node_type, matcher, formatcode):
node = current_node

subnw = subnw.strip()

if not subnw and node_type == 'leaf' and formatcode != 100:
raise NewickError('Empty leaf node found')
elif not subnw:
Expand Down Expand Up @@ -507,5 +509,3 @@ def _get_features_string(self, features=None):
string = "[&&NHX:"+string+"]"

return string


33 changes: 18 additions & 15 deletions ete3/test/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def test_tree_read_and_write(self):
""" Tests newick support """
# Read and write newick tree from file (and support for NHX
# format): newick parser
open("/tmp/etetemptree.nw","w").write(nw_full)
with open("/tmp/etetemptree.nw","w") as OUT:
OUT.write(nw_full)

t = Tree("/tmp/etetemptree.nw")
t.write(outfile='/tmp/etewritetest.nw')
self.assertEqual(nw_full, t.write(features=["flag","mood"]))
Expand Down Expand Up @@ -494,10 +496,10 @@ def test_common_ancestors(self):
common = A.get_common_ancestor(C)
self.assertEqual("root", common.get_tree_root().tag)

self.assert_(common.get_tree_root().is_root())
self.assert_(not A.is_root())
self.assert_(A.is_leaf())
self.assert_(not A.get_tree_root().is_leaf())
self.assertTrue(common.get_tree_root().is_root())
self.assertTrue(not A.is_root())
self.assertTrue(A.is_leaf())
self.assertTrue(not A.get_tree_root().is_leaf())
self.assertRaises(TreeError, A.get_common_ancestor, Tree())

# Test multiple target nodes and get_path argument
Expand All @@ -508,13 +510,14 @@ def test_common_ancestors(self):
self.assertEqual(common, N2)
self.assertEqual(path.keys(), expected_path.keys())
for k in path.keys():
self.assertEqual(list(sorted(path[k])), list(sorted(expected_path[k])))
self.assertEqual(list(sorted(path[k], key=lambda x: x.name)),
list(sorted(expected_path[k], key=lambda x: x.name)))

# Test common ancestor function using self as single argument (issue #398)
common = A.get_common_ancestor(A)
self.assert_(common, A)
self.assertEqual(common, A)
common = C.get_common_ancestor("C")
self.assert_(common, C)
self.assertEqual(common, C)
common, path = C.get_common_ancestor("C", get_path=True)
self.assertEqual(common, C)
self.assertDictEqual(path, {})
Expand All @@ -532,10 +535,10 @@ def test_getters_iters(self):
# Tree magic python features
t = Tree(nw_dflt)
self.assertEqual(len(t), 20)
self.assert_("Ddi0002240" in t)
self.assert_(t.children[0] in t)
self.assertTrue("Ddi0002240" in t)
self.assertTrue(t.children[0] in t)
for a in t:
self.assert_(a.name)
self.assertTrue(a.name)

# Populate
t = Tree(nw_full)
Expand All @@ -554,13 +557,13 @@ def test_getters_iters(self):

# Check gettters and itters return the same
t = Tree(nw2_full)
self.assert_(t.get_leaf_names(), [name for name in t.iter_leaf_names()])
self.assert_(t.get_leaves(), [name for name in t.iter_leaves()])
self.assert_(t.get_descendants(), [n for n in t.iter_descendants()])
self.assertEqual(t.get_leaf_names(), [name for name in t.iter_leaf_names()])
self.assertEqual(t.get_leaves(), [name for name in t.iter_leaves()])
self.assertEqual(t.get_descendants(), [n for n in t.iter_descendants()])

self.assertEqual(set([n for n in t.traverse("preorder")]), \
set([n for n in t.traverse("postorder")]))
self.assert_(t in set([n for n in t.traverse("preorder")]))
self.assertTrue(t in set([n for n in t.traverse("preorder")]))

# Check order or visiting nodes

Expand Down

0 comments on commit 59dea1b

Please sign in to comment.