<commit_before>#! /usr/bin/env python

##############################################################################
##  pyvolve: Python platform for simulating evolutionary sequences.
##
##  Written by <NAME> (<EMAIL>) 
##############################################################################

'''
Read/parse a newick tree.
'''


from misc import Tree
import re
import os


def read_tree(**kwargs):
    
    '''
    Parses a newick phylogeny, provided either via a file or a string.
    Input tree may contain internal model flags at bifurcations (or trifurcations, polytomies are acceptable!), indicating that the daughter branches should evolve according to a different evolutionary model from parent. 
    Returns a Tree() object along which sequences may be evolved and a list of model flags,  
    
    Example input tree containing model flags, _m1_ and _m2_. Flags *must* be provided in format _flagname_ , i.e. with both a leading a and trailing underscore).
    ((((t1:1.0,t8:1.0):1.0,t7:1.0):1.0_m1_,((t2:1.0,t9:1.0):1.0,t3:1.0):1.0):1.0,(((t6:1.0,t4:1.0):1.0,t5:1.0):1.0_m2_,t10:1.0):1.0);

    ONE OF THESE TWO ARGUMENTS IS REQUIRED:
        1. *file* is the name of the file containing a newick tree for parsing. If this argument is provided in addition to tstring, the tree in the file will be used and tstring will be ignored.
        2. *tstring* is a newick string for a tree. Tree may be rooted or unrooted. If a file is additionally provided, the tstring argument will be ignored.   
    '''    
    
    filename           = kwargs.get('file')
    tstring            = kwargs.get('tree')
        
    if filename:
        assert (os.path.exists(filename)), "File does not exist. Check path?"
        t = open(filename, 'r')
        tstring = t.read()
        t.close()
    else:
        assert (tstring is not None), "You need to either specify a file with a tree or give your own."
        assert (type(tstring) is str), "Trees provided with the flag `tstring` must be in quotes to be considered a string."
        
    tstring = re.sub(r"\s", "", tstring)
    tstring = tstring.rstrip(';')
    
    flags = []
    internal_node_count = 1
    (tree, flags, internal_node_count, index) = _parse_tree(tstring, flags, internal_node_count, 0) 
    assert(flags == list(set(flags)) ), "Unique identifiers required for branch model heterogeneity flags."

    return tree, flags



def print_tree(tree, level=0):<commit_msg>only return flags if they exist<commit_after>#! /usr/bin/env python

##############################################################################
##  pyvolve: Python platform for simulating evolutionary sequences.
##
##  Written by <NAME> (<EMAIL>) 
##############################################################################

'''
Read/parse a newick tree.
'''


from misc import Tree
import re
import os


def read_tree(**kwargs):
    
    '''
    Parses a newick phylogeny, provided either via a file or a string.
    Input tree may contain internal model flags at bifurcations (or trifurcations, polytomies are acceptable!), indicating that the daughter branches should evolve according to a different evolutionary model from parent. 
    Returns a Tree() object along which sequences may be evolved and a list of model flags,  
    
    Example input tree containing model flags, _m1_ and _m2_. Flags *must* be provided in format _flagname_ , i.e. with both a leading a and trailing underscore).
    ((((t1:1.0,t8:1.0):1.0,t7:1.0):1.0_m1_,((t2:1.0,t9:1.0):1.0,t3:1.0):1.0):1.0,(((t6:1.0,t4:1.0):1.0,t5:1.0):1.0_m2_,t10:1.0):1.0);

    ONE OF THESE TWO ARGUMENTS IS REQUIRED:
        1. *file* is the name of the file containing a newick tree for parsing. If this argument is provided in addition to tstring, the tree in the file will be used and tstring will be ignored.
        2. *tstring* is a newick string for a tree. Tree may be rooted or unrooted. If a file is additionally provided, the tstring argument will be ignored.   
    '''    
    
    filename           = kwargs.get('file')
    tstring            = kwargs.get('tree')
        
    if filename:
        assert (os.path.exists(filename)), "File does not exist. Check path?"
        t = open(filename, 'r')
        tstring = t.read()
        t.close()
    else:
        assert (tstring is not None), "You need to either specify a file with a tree or give your own."
        assert (type(tstring) is str), "Trees provided with the flag `tstring` must be in quotes to be considered a string."
        
    tstring = re.sub(r"\s", "", tstring)
    tstring = tstring.rstrip(';')
    
    flags = []
    internal_node_count = 1
    (tree, flags, internal_node_count, index) = _parse_tree(tstring, flags, internal_node_count, 0) 
    assert(flags == list(set(flags)) ), "Unique identifiers required for branch model heterogeneity flags."
    
    if len(flags) == 0:
        return tree
    else:
        return tree, flags


def print_tree(tree, level=0):
{'fraction_non_alphanumeric': 0.16214041440354054, 'fraction_numerical': 0.022731844699255682, 'mean_word_length': 3.242320819112628, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3044298', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1469, 'n_words': 554}