# Path: scripts_of/newick.py
# def read_newick(newick, root_node=None, format=0):
#     """ Reads a newick tree from either a string or a file, and returns
#     an ETE tree structure.
# 
#     A previously existent node object can be passed as the root of the
#     tree, which means that all its new children will belong to the same
#     class as the root(This allows to work with custom TreeNode
#     objects).
# 
#     You can also take advantage from this behaviour to concatenate
#     several tree structures.
#     """
# 
#     if root_node is None:
#         from .tree import TreeNode   
#         root_node = TreeNode()
# 
#     if isinstance(newick, basestring):
#         if os.path.exists(newick):
#             nw = open(newick, 'rU').read()
#         else:
#             nw = newick
#         nw = nw.strip()
#         if not nw.startswith('(') and nw.endswith(';'):
#             return _read_node_data(nw, root_node, "single", format)
#             
#         elif not nw.startswith('(') or not nw.endswith(';'):
#             raise NewickError('Unexisting tree file or Malformed newick tree structure.')
#         else:
#             return _read_newick_from_string(nw, root_node, format)
# 
#     else:
#         raise NewickError("'newick' argument must be either a filename or a newick string.")
# 
# def write_newick(rootnode, features=None, format=1, format_root_node=True,
#                  is_leaf_fn=None):
#     """ Iteratively export a tree structure and returns its NHX
#     representation. """
#     newick = []
#     leaf = is_leaf_fn if is_leaf_fn else lambda n: not bool(n.children)
#     for postorder, node in rootnode.iter_prepostorder(is_leaf_fn=is_leaf_fn):
#         if postorder:
#             newick.append(")")
#             if node.up is not None or format_root_node:
#                 newick.append(format_node(node, "internal", format))
#                 newick.append(_get_features_string(node, features))
#         else:
#             if node is not rootnode and node != node.up.children[0]:
#                 newick.append(",")
#                 
#             if leaf(node):
#                 safe_name = re.sub("["+_ILEGAL_NEWICK_CHARS+"]", "_", \
#                                str(getattr(node, "name")))
#                 newick.append(format_node(node, "leaf", format))
#                 newick.append(_get_features_string(node, features))
#             else:
#                 newick.append("(")
# 
#     newick.append(";")
#     return ''.join(newick)


import os
    import cPickle as pickle
    import pickle
import random
import copy
import itertools
import functools
from collections import deque 
from .newick import read_newick, write_newick
        else:
            raise ValueError("[%s] is not a valid FaceAreas instance" %type(value))
        
    def _get_face_areas(self):
        if not hasattr(self, "_faces"):
            self._faces = _FaceAreas()
        return self._faces

    faces = property(fget=_get_face_areas, \
                         fset=_set_face_areas)

    def __init__(self, newick=None, format=0, dist=None, support=None,
                 name=None):
        self._children = []
        self._up = None
        self._dist = DEFAULT_DIST
        self._support = DEFAULT_SUPPORT
        self._img_style = None
        self.features = set([])
        # Add basic features
        self.features.update(["dist", "support", "name"])
        if dist is not None:
            self.dist = dist
        if support is not None:
            self.support = support

        self.name = name if name is not None else DEFAULT_NAME
        
        # Initialize tree
        if newick is not None:

# Path: scripts_of/newick.py
# def read_newick(newick, root_node=None, format=0):
#     """ Reads a newick tree from either a string or a file, and returns
#     an ETE tree structure.
# 
#     A previously existent node object can be passed as the root of the
#     tree, which means that all its new children will belong to the same
#     class as the root(This allows to work with custom TreeNode
#     objects).
# 
#     You can also take advantage from this behaviour to concatenate
#     several tree structures.
#     """
# 
#     if root_node is None:
#         from .tree import TreeNode   
#         root_node = TreeNode()
# 
#     if isinstance(newick, basestring):
#         if os.path.exists(newick):
#             nw = open(newick, 'rU').read()
#         else:
#             nw = newick
#         nw = nw.strip()
#         if not nw.startswith('(') and nw.endswith(';'):
#             return _read_node_data(nw, root_node, "single", format)
#             
#         elif not nw.startswith('(') or not nw.endswith(';'):
#             raise NewickError('Unexisting tree file or Malformed newick tree structure.')
#         else:
#             return _read_newick_from_string(nw, root_node, format)
# 
#     else:
#         raise NewickError("'newick' argument must be either a filename or a newick string.")
# 
# def write_newick(rootnode, features=None, format=1, format_root_node=True,
#                  is_leaf_fn=None):
#     """ Iteratively export a tree structure and returns its NHX
#     representation. """
#     newick = []
#     leaf = is_leaf_fn if is_leaf_fn else lambda n: not bool(n.children)
#     for postorder, node in rootnode.iter_prepostorder(is_leaf_fn=is_leaf_fn):
#         if postorder:
#             newick.append(")")
#             if node.up is not None or format_root_node:
#                 newick.append(format_node(node, "internal", format))
#                 newick.append(_get_features_string(node, features))
#         else:
#             if node is not rootnode and node != node.up.children[0]:
#                 newick.append(",")
#                 
#             if leaf(node):
#                 safe_name = re.sub("["+_ILEGAL_NEWICK_CHARS+"]", "_", \
#                                str(getattr(node, "name")))
#                 newick.append(format_node(node, "leaf", format))
#                 newick.append(_get_features_string(node, features))
#             else:
#                 newick.append("(")
# 
#     newick.append(";")
#     return ''.join(newick)


# Path: scripts_of/tree.py
import os
    import cPickle as pickle
    import pickle
import random
import copy
import itertools
import functools
from collections import deque 
from .newick import read_newick, write_newick

        else:
            raise ValueError("[%s] is not a valid FaceAreas instance" %type(value))
        
    def _get_face_areas(self):
        if not hasattr(self, "_faces"):
            self._faces = _FaceAreas()
        return self._faces

    faces = property(fget=_get_face_areas, \
                         fset=_set_face_areas)

    def __init__(self, newick=None, format=0, dist=None, support=None,
                 name=None):
        self._children = []
        self._up = None
        self._dist = DEFAULT_DIST
        self._support = DEFAULT_SUPPORT
        self._img_style = None
        self.features = set([])
        # Add basic features
        self.features.update(["dist", "support", "name"])
        if dist is not None:
            self.dist = dist
        if support is not None:
            self.support = support

        self.name = name if name is not None else DEFAULT_NAME
        
        # Initialize tree
        if newick is not None:

            read_newick(newick, root_node = self, format=format)