Beispiel #1
0
def tree_from_order(order):
  nodes = script_tree.getNodes(tree)
  for n in nodes:
    if not script_tree.isRoot(tree,n):
      if script_tree.isLeaf(tree,n):
	index = len(order)
      else:
	index = order.index(script_tree.getBootstrap(tree,n))
      index_parent = order.index(script_tree.getBootstrap(tree,script_tree.getParent(tree,n)))
      script_tree.setLength(tree,n,index-index_parent)

  return script_tree.writeTree(tree,root,False)
Beispiel #2
0
def tree_from_order(order):
    nodes = script_tree.getNodes(tree)
    for n in nodes:
        if not script_tree.isRoot(tree, n):
            if script_tree.isLeaf(tree, n):
                index = len(order)
            else:
                index = order.index(script_tree.getBootstrap(tree, n))
            index_parent = order.index(script_tree.getBootstrap(tree, script_tree.getParent(tree, n)))
            script_tree.setLength(tree, n, index - index_parent)

    return script_tree.writeTree(tree, root, False)
Beispiel #3
0
def opt(tree, root):
    if (script_tree.isLeaf(tree, script_tree.getChildren(tree, root)[0]) and
            script_tree.isLeaf(tree, script_tree.getChildren(tree, root)[1])):
        return [script_tree.getBootstrap(tree, root)]
    elif script_tree.isLeaf(tree, script_tree.getChildren(tree, root)[0]):
        return opt(tree, script_tree.getChildren(tree, root)[1]) + [script_tree.getBootstrap(tree, root)]
    elif script_tree.isLeaf(tree, script_tree.getChildren(tree, root)[1]):
        return opt(tree, script_tree.getChildren(tree, root)[0]) + [script_tree.getBootstrap(tree, root)]
    else:
        order1 = opt(tree, script_tree.getChildren(tree, root)[0])
        order2 = opt(tree, script_tree.getChildren(tree, root)[1])
        # print "level",len(order1)+len(order2),order1,order2
        return mix(order1, order2) + [script_tree.getBootstrap(tree, root)]
def opt(tree,root):
  if (script_tree.isLeaf(tree,script_tree.getChildren(tree,root)[0]) and
      script_tree.isLeaf(tree,script_tree.getChildren(tree,root)[1])):
    return [script_tree.getBootstrap(tree,root)]
  elif script_tree.isLeaf(tree,script_tree.getChildren(tree,root)[0]):
    return opt(tree,script_tree.getChildren(tree,root)[1])+[script_tree.getBootstrap(tree,root)]
  elif script_tree.isLeaf(tree,script_tree.getChildren(tree,root)[1]):
    return opt(tree,script_tree.getChildren(tree,root)[0])+[script_tree.getBootstrap(tree,root)]
  else:
    order1 = opt(tree,script_tree.getChildren(tree,root)[0])
    order2 = opt(tree,script_tree.getChildren(tree,root)[1])
    #print "level",len(order1)+len(order2),order1,order2
    return mix(order1,order2) + [script_tree.getBootstrap(tree,root)]
def parent(x):
  #print x
  nodes = script_tree.getNodes(species_tree)
  for n in nodes:
    if script_tree.isLeaf(species_tree,n):
      name = script_tree.getName(species_tree,n)
    else:
      name = script_tree.getBootstrap(species_tree,n)
    if name == x:
      if script_tree.isRoot(species_tree,n):
	result = -1
      else:
	result = script_tree.getBootstrap(species_tree,script_tree.getParent(species_tree,n))
  return result
Beispiel #6
0
def parent(x):
    #print x
    nodes = script_tree.getNodes(species_tree)
    for n in nodes:
        if script_tree.isLeaf(species_tree, n):
            name = script_tree.getName(species_tree, n)
        else:
            name = script_tree.getBootstrap(species_tree, n)
        if name == x:
            if script_tree.isRoot(species_tree, n):
                result = -1
            else:
                result = script_tree.getBootstrap(
                    species_tree, script_tree.getParent(species_tree, n))
    return result
Beispiel #7
0
def distance_from(x, y):
    nodes = script_tree.getNodes(species_tree)
    for n in nodes:
        if script_tree.isLeaf(species_tree, n):
            name = script_tree.getName(species_tree, n)
        else:
            name = script_tree.getBootstrap(species_tree, n)
        if name == x:
            A = n
        if name == y:
            B = n
    return script_tree.distanceFrom(species_tree, A, B)
def distance_from(x,y):
  nodes = script_tree.getNodes(species_tree)
  for n in nodes:
    if script_tree.isLeaf(species_tree,n):
      name = script_tree.getName(species_tree,n)
    else:
      name = script_tree.getBootstrap(species_tree,n)
    if name == x:
      A = n
    if name == y:
      B = n
  return script_tree.distanceFrom(species_tree,A,B)
def order_from_tree(tree):
  order = []
  nodes = script_tree.getNodes(tree)
  root = script_tree.getRoot(tree)
  for n in nodes:
    if not script_tree.isLeaf(tree,n):
      order.append(n)
  #print order
  order.sort(lambda x,y: cmp(script_tree.distanceFrom(tree,x,root),script_tree.distanceFrom(tree,y,root)))
  for i in range(len(order)):
    order[i] = script_tree.getBootstrap(tree,order[i])
  return order
Beispiel #10
0
def order_from_tree(tree):
    order = []
    nodes = script_tree.getNodes(tree)
    root = script_tree.getRoot(tree)
    for n in nodes:
        if not script_tree.isLeaf(tree, n):
            order.append(n)
    # print order
    order.sort(lambda x, y: cmp(script_tree.distanceFrom(tree, x, root), script_tree.distanceFrom(tree, y, root)))
    for i in range(len(order)):
        order[i] = script_tree.getBootstrap(tree, order[i])
    return order
def donnor_search(t,n):
  current_node = n
  result = -1
  while result == -1 and not script_tree.isRoot(t,current_node):
    current_node = script_tree.getParent(t,current_node)
    annot = script_tree.getBootstrap(t,current_node)
    event = annot[1:].split(".")[0]
    if event[:2] != "T@" and event[:2] != "D@":
      result = event
    if annot.find("@T") >= 0:
      current_node = script_tree.getRoot(t)
      result = -1
  return result
Beispiel #12
0
def donnor_search(t, n):
    current_node = n
    result = -1
    while result == -1 and not script_tree.isRoot(t, current_node):
        current_node = script_tree.getParent(t, current_node)
        annot = script_tree.getBootstrap(t, current_node)
        event = annot[1:].split(".")[0]
        if event[:2] != "T@" and event[:2] != "D@":
            result = event
        if annot.find("@T") >= 0:
            current_node = script_tree.getRoot(t)
            result = -1
    return result
def receptor_search(t,n):
  if script_tree.isLeaf(t,n):
    result = []
  else:
    annot = script_tree.getBootstrap(t,n)
    if annot.find("T@") >= 0:
      result = []
    elif annot.split(".")[1][:2] == "D@":
      children = script_tree.getChildren(t,n)
      result = receptor_search(t,children[0]) + receptor_search(t,children[1])
    else:
      result = [annot.split(".")[1]]
  #print result
  return result
Beispiel #14
0
def receptor_search(t, n):
    if script_tree.isLeaf(t, n):
        result = []
    else:
        annot = script_tree.getBootstrap(t, n)
        if annot.find("T@") >= 0:
            result = []
        elif annot.split(".")[1][:2] == "D@":
            children = script_tree.getChildren(t, n)
            result = receptor_search(t, children[0]) + receptor_search(
                t, children[1])
        else:
            result = [annot.split(".")[1]]
    #print result
    return result
Beispiel #15
0
def maximum_distance(tree,root):
  c1 = script_tree.getChildren(tree,root)[0]
  c2 = script_tree.getChildren(tree,root)[1]
  name = script_tree.getBootstrap(tree,root)
  if script_tree.isLeaf(tree,c1) and script_tree.isLeaf(tree,c2):
    return [[name],[name]]
  elif script_tree.isLeaf(tree,c1):
    oo = maximum_distance(tree,c2)
    return [[name] + oo[0],[name] + oo[1]]
  elif script_tree.isLeaf(tree,c2):
    oo = maximum_distance(tree,c1)
    return [[name] + oo[0],[name] + oo[1]]
  else:
    orders1 = maximum_distance(tree,c1)
    orders2 = maximum_distance(tree,c2)
    return [[name] + orders1[0] + orders2[0],[name] + orders2[1] + orders1[1]]
Beispiel #16
0
def maximum_distance(tree, root):
    c1 = script_tree.getChildren(tree, root)[0]
    c2 = script_tree.getChildren(tree, root)[1]
    name = script_tree.getBootstrap(tree, root)
    if script_tree.isLeaf(tree, c1) and script_tree.isLeaf(tree, c2):
        return [[name], [name]]
    elif script_tree.isLeaf(tree, c1):
        oo = maximum_distance(tree, c2)
        return [[name] + oo[0], [name] + oo[1]]
    elif script_tree.isLeaf(tree, c2):
        oo = maximum_distance(tree, c1)
        return [[name] + oo[0], [name] + oo[1]]
    else:
        orders1 = maximum_distance(tree, c1)
        orders2 = maximum_distance(tree, c2)
        return [[name] + orders1[0] + orders2[0], [name] + orders2[1] + orders1[1]]
Beispiel #17
0
def random_order(tree, root):
    c1 = script_tree.getChildren(tree, root)[0]
    c2 = script_tree.getChildren(tree, root)[1]
    name = script_tree.getBootstrap(tree, root)
    if script_tree.isLeaf(tree, c1) and script_tree.isLeaf(tree, c2):
        return [name]
    elif script_tree.isLeaf(tree, c1):
        return [name] + random_order(tree, c2)
    elif script_tree.isLeaf(tree, c2):
        return [name] + random_order(tree, c1)
    else:
        order1 = random_order(tree, c1)
        order2 = random_order(tree, c2)
        # if script_tree.isRoot(tree,root):
        # print order1,order2
        pos = range(len(order1) + len(order2))
        for i in range(len(order2)):
            index = int(random.random() * (len(pos)))
            del pos[index]
        # if script_tree.isRoot(tree,root):
        # print pos
        order = [name]
        previous = 0
        for i in pos:
            for j in range(i - previous):
                # if script_tree.isRoot(tree,root):
                # print order2,i,previous
                order.append(order2[0])
                del order2[0]
            order.append(order1[0])
            del order1[0]
            previous = i + 1

        # if script_tree.isRoot(tree,root):
        # print order1,order2,order
        return order + order2
Beispiel #18
0
def random_order(tree,root):
  c1 = script_tree.getChildren(tree,root)[0]
  c2 = script_tree.getChildren(tree,root)[1]
  name = script_tree.getBootstrap(tree,root)
  if script_tree.isLeaf(tree,c1) and script_tree.isLeaf(tree,c2):
    return [name]
  elif script_tree.isLeaf(tree,c1):
    return [name] + random_order(tree,c2)
  elif script_tree.isLeaf(tree,c2):
    return [name] + random_order(tree,c1)
  else:
    order1 = random_order(tree,c1)
    order2 = random_order(tree,c2)
    #if script_tree.isRoot(tree,root):
      #print order1,order2
    pos = range(len(order1)+len(order2))
    for i in range(len(order2)):
      index = int(random.random()*(len(pos)))
      del pos[index]
    #if script_tree.isRoot(tree,root):
      #print pos
    order = [name]
    previous = 0
    for i in pos:
      for j in range(i - previous):
	#if script_tree.isRoot(tree,root):
	  #print order2,i,previous
	order.append(order2[0])
	del order2[0]
      order.append(order1[0])
      del order1[0]
      previous = i+1
	
    #if script_tree.isRoot(tree,root):
      #print order1,order2,order
    return order + order2
Beispiel #19
0
    order[i] = script_tree.getBootstrap(tree,order[i])
  return order

# useful variables
degre_entrant = {}
edge = {}
graph = {}
nodes = script_tree.getNodes(tree)
leaves = script_tree.getLeavesNames(tree)
root = script_tree.getRoot(tree)
internal_nodes = []

# initialise from nodes
for n in nodes:
  if (not script_tree.isLeaf(tree,n)):
    node = script_tree.getBootstrap(tree,n)
    graph[node] = []
    degre_entrant[node] = []
    internal_nodes.append(node)
  else:
    node = script_tree.getName(tree,n)
    graph[node] = []


print "tree with ",len(internal_nodes),"internal nodes"


# parse transfers
#by_family = {}
for line in constraints:
  #print line
Beispiel #20
0
    order[i] = script_tree.getBootstrap(tree,order[i])
  return order

# useful variables
degre_entrant = {}
edge = {}
graph = {}
nodes = script_tree.getNodes(tree)
leaves = script_tree.getLeavesNames(tree)
root = script_tree.getRoot(tree)
internal_nodes = []

# initialise from nodes
for n in nodes:
  if (not script_tree.isLeaf(tree,n)):
    node = script_tree.getBootstrap(tree,n)
    graph[node] = []
    degre_entrant[node] = []
    internal_nodes.append(node)
  else:
    node = script_tree.getName(tree,n)
    graph[node] = []

# initialise from branches
for n in nodes:
  if (not script_tree.isLeaf(tree,n)):
    if (not script_tree.isRoot(tree,n)):
      parent = script_tree.getBootstrap(tree,script_tree.getParent(tree,n))
      child = script_tree.getBootstrap(tree,n)
      graph[parent].append(child)
      edge[parent+","+child] = MAX_NUMBER
		#species_under_origin.append(script_tree.getName(species_tree,l))
	leaves = script_tree.getLeaves(tree,root)
	#species_present = []
	#for l in leaves:
	  #if script_tree.getName(tree,l).split("_")[0] not in species_present:
	    #species_present.append(script_tree.getName(tree,l).split("_")[0])
	if len(leaves) > MINIMUM_FAMILY_SIZE:# and difference_symetrique(species_present,species_under_origin) < MAXIMUM_DIFFERENCE_WITH_PROFILE:
	  #print script_tree.writeTree(tree,root,False)
	  #print origination,len(leaves),len(species_present),len(species_under_origin),
	  #output_distr.write(str(difference_symetrique(species_present,species_under_origin))+"\n")
	  nodes = script_tree.getNodes(tree)
	  for n in nodes:
	    if script_tree.isLeaf(tree,n):
	      annot = script_tree.getName(tree,n)
	    else:
	      annot = script_tree.getBootstrap(tree,n)
	    #if script_tree.isLeaf(tree,n) and annot.find("T")>=0:
	      #print annot
	    # annot: evenement
	    events = annot.split(".")
	    if events[0] == "":
	      del events[0]
	    for e in events:
	      if e[:2] == "T@":
		# on a touve un transfert
		#print e
		donnor = e[2:].split("->")[0]
		receptor = e[2:].split("->")[1]
		#print annot,donnor,receptor
		# 1/ calculer la contrainte selon les transferts
		if parent(donnor) != -1 and not (receptor in script_tree.getLeavesNames(species_tree)):
Beispiel #22
0

# useful variables
degre_entrant = {}
edge = {}
graph = {}
nodes = script_tree.getNodes(tree)
leaves = script_tree.getLeavesNames(tree)
root = script_tree.getRoot(tree)
# rootname = script_tree.getBootstrap(tree,root)
internal_nodes = []

# initialise from nodes
for n in nodes:
    if (not script_tree.isLeaf(tree, n)):
        node = script_tree.getBootstrap(tree, n)
        graph[node] = []
        degre_entrant[node] = []
        internal_nodes.append(node)
    else:
        node = script_tree.getName(tree, n)
        graph[node] = []

print
"tree with ", len(internal_nodes), "internal nodes"

# parse transfers
# by_family = {}
for line in constraints:
    # print line
    if (len(line.split()) >= 2 or len(line.split(",")) >= 2) and line.find("FRQ") < 0 and line[0] != "#":
Beispiel #23
0
 #species_present = []
 #for l in leaves:
 #if script_tree.getName(tree,l).split("_")[0] not in species_present:
 #species_present.append(script_tree.getName(tree,l).split("_")[0])
 if len(
         leaves
 ) > MINIMUM_FAMILY_SIZE:  # and difference_symetrique(species_present,species_under_origin) < MAXIMUM_DIFFERENCE_WITH_PROFILE:
     #print script_tree.writeTree(tree,root,False)
     #print origination,len(leaves),len(species_present),len(species_under_origin),
     #output_distr.write(str(difference_symetrique(species_present,species_under_origin))+"\n")
     nodes = script_tree.getNodes(tree)
     for n in nodes:
         if script_tree.isLeaf(tree, n):
             annot = script_tree.getName(tree, n)
         else:
             annot = script_tree.getBootstrap(tree, n)
         #if script_tree.isLeaf(tree,n) and annot.find("T")>=0:
         #print annot
         # annot: evenement
         events = annot.split(".")
         if events[0] == "":
             del events[0]
         for e in events:
             if e[:2] == "T@":
                 # on a touve un transfert
                 #print e
                 donnor = e[2:].split("->")[0]
                 receptor = e[2:].split("->")[1]
                 #print annot,donnor,receptor
                 # 1/ calculer la contrainte selon les transferts
                 if parent(donnor) != -1 and not (