Skip to content

Commit

Permalink
pre-david commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bielcardona committed Mar 17, 2012
1 parent ce8b081 commit e7e605f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/PhyloNetwork/classes.py
Expand Up @@ -62,6 +62,13 @@ def node_by_taxa(self,taxa):
return node
return None

@memoize_method
def all_nodes_by_taxa(self,taxa):
"""
Returns all nodes labelled by taxa.
"""
return [node for node in self.labelled_nodes() if self.label(node)==taxa]

def is_tree_node(self,u):
"""
Returns True if u is a tree node, False otherwise.
Expand Down Expand Up @@ -194,10 +201,11 @@ def walk(self,parsed,ignore_prefix=None):
return internal_label

def from_eNewick(self,string,ignore_prefix=None):
#parsed=eNewickParser(string)[0]
try:
parsed=eNewickParser(string)[0]
except pyparsing.ParseException:
print 'Malformed eNewick'
raise 'Malformed eNewick'
#raise JoQueSe
return False
self.walk(parsed,ignore_prefix=ignore_prefix)
Expand Down Expand Up @@ -328,8 +336,8 @@ def topological_restriction(self,subtaxa):
restricted=copy.deepcopy(self)
for taxon in restricted.taxa():
if not taxon in subtaxa:
u=restricted.node_by_taxa(taxon)
del restricted._labels[u]
for u in restricted.all_nodes_by_taxa(taxon):
del restricted._labels[u]
restricted.cache = {}

while True:
Expand Down
10 changes: 6 additions & 4 deletions src/PhyloNetwork/distances.py
Expand Up @@ -55,13 +55,15 @@ def nodal_distance_unsplitted(net1,net2,p=1,take_root=False):
mat=net1.nodal_matrix()-net2.nodal_matrix()
except:
raise Exception("Networks over different set of taxa")
n = len(net1.taxa())
result = sum([ (mat1[i,j]+mat1[j,i]-mat2[i,j]-mat2[j,i])**p for i in range(n) for j in range(i+1,n)])
if p==1:
return sum(abs(mat.flatten()))/2
return result
else:
if take_root:
return (sum(abs(mat.flatten())**p)/2)**(1.0/p)
return (result)**(1.0/p)
else:
return (sum(abs(mat.flatten())**p)/2)
return result

def cophenetic_distance(net1,net2,p=1,take_root=False):
mat1=net1.cophenetic_matrix()
Expand All @@ -71,7 +73,7 @@ def cophenetic_distance(net1,net2,p=1,take_root=False):
except:
raise Exception("Networks over different set of taxa")
if p==1:
return sum(abs(mat.flatten()))/2
return sum(abs(mat.flatten()))
else:
if take_root:
return (sum(abs(mat.flatten())**p))**(1.0/p)
Expand Down
2 changes: 1 addition & 1 deletion src/PhyloNetwork/eNewick.py
Expand Up @@ -18,7 +18,7 @@ def makeeNewickParser():
sharp = Literal("#").suppress()

# terminal
name = Word(alphanums + alphas8bit + "_" + "-" + "." + "+" + "~" + "{" + "}" + "*" )
name = Word(alphanums + alphas8bit + "_" + "-" + "." + "+" + "&" + "/" + "~" + "{" + "}" + "*" + "'" + '"' + '\\' + '?')
string = Word(alphas)
fnumber = Combine(
Word("+-"+nums, nums) +
Expand Down
16 changes: 16 additions & 0 deletions src/PhyloNetwork/generators.py
Expand Up @@ -288,6 +288,22 @@ def Tree_generator_random(taxa,binary=False,nested_taxa=True,id_offset=0):
while True:
yield f(taxa,id_offset)

def tree_generator_random_yule(taxa):
n = len(taxa)
if n == 1:
return PhyloNetwork(eNewick=('%s;' % taxa[0]))
tree = PhyloNetwork(eNewick=('(%s,%s);' % (taxa[0],taxa[1])))
for i in range(2,n):
leaf = random.choice(tree.leaves())
tree = push_and_hang(tree,leaf,taxa[i])
labels = tree._labels
keys = labels.keys()
values = labels.values()
random.shuffle(values)
tree._labels = dict(zip(keys,values))
tree.cache = {}
return tree

if __name__ == "__main__":
tg = Tree_generator(['1','2','3'])
while tg:
Expand Down

0 comments on commit e7e605f

Please sign in to comment.