def NJ_Algorithm(): """ Neighbor-Joining algorithm to join species and create phylogenetic tree """ phylo_tree = None tree_dict = dict() # Loop through species until down to one pair for k in range(len(species)-1): # Get average distance for every node node_avg = ComputeNodeAvgDist() # Find lowest pair of species to join from average distance species list min_val, min_a, min_b = FindLowestPair(node_avg) if species[min_a] in tree_dict: # If the first joined species is already in the tree dictionary, retrieve it child1 = tree_dict[species[min_a]] else: # Else create new species tree child1 = Tree(species[min_a]) # v_i = D_ij/2 + (u_i - u_j)/2 child1.value = min_val/2.0 + (node_avg[min_a] - node_avg[min_b])/2.0 if species[min_b] in tree_dict: # If the second joined species is already in the tree dictionary, retrieve it child2 = tree_dict[species[min_b]] else: # Else create new species tree child2 = Tree(species[min_b]) # v_j = D_ij/2 + (u_j - u_i)/2 child2.value = min_val/2.0 + (node_avg[min_b] - node_avg[min_a])/2.0 # Create new tree with both A and B species joined key = "(" + species[min_a] + "),(" + species[min_b] + ")" phylo_tree = Tree(key) phylo_tree.value = min_val/2.0 # Put lower value to left of tree if child1.value <= child2.value: phylo_tree.AddLeftChild(child1, child1.value) phylo_tree.AddRightChild(child2, child2.value) else: phylo_tree.AddLeftChild(child2, child2.value) phylo_tree.AddRightChild(child1, child1.value) #phylo_tree.printNodeInfo() tree_dict[key] = phylo_tree # Create joined values to put back into 2d matrix joinedDist = CreateJoinedDistances(min_a, min_b, min_val) # Remove A and B individually and insert AB joined CombineSpecies(joinedDist, min_a, min_b) return phylo_tree
def UPGMA_Algorithm(): """ UPGMA algorithm to join species and create phylogenetic tree """ phylo_tree = None tree_dict = dict() # Loop through species until down to one pair for k in range(len(species)-1): min_val, min_i, min_j = FindLowestPair() if species[min_i] in tree_dict: # If the first joined species is already in the tree dictionary, retrieve it child1 = tree_dict[species[min_i]] else: # Else create new species tree child1 = Tree(species[min_i]) child1.value = min_val/2.0 if species[min_j] in tree_dict: # If the second joined species is already in the tree dictionary, retrieve it child2 = tree_dict[species[min_j]] else: # Else create new species tree child2 = Tree(species[min_j]) child2.value = min_val/2.0 # Create new tree with both A and B species joined key = "(" + species[min_i] + "),(" + species[min_j] + ")" phylo_tree = Tree(key) phylo_tree.value = min_val/2.0 # Put lower value to left of tree if child1.value <= child2.value: phylo_tree.AddLeftChild( child1, (phylo_tree.value - child1.value) ) phylo_tree.AddRightChild( child2, (phylo_tree.value - child2.value) ) else: phylo_tree.AddLeftChild( child2, (phylo_tree.value - child2.value) ) phylo_tree.AddRightChild( child1, (phylo_tree.value - child1.value) ) #phylo_tree.printNodeInfo() tree_dict[key] = phylo_tree # Create joined values to put back into 2d matrix joined_values = CreateJoinedDistances(min_i, min_j) # Remove A and B individually and insert AB joined CombineSpecies(joined_values, min_i, min_j) return phylo_tree
def UPGMA_Algorithm(): """ UPGMA algorithm to join species and create phylogenetic tree """ phylo_tree = None tree_dict = dict() # Loop through species until down to one pair for k in range(len(species) - 1): min_val, min_i, min_j = FindLowestPair() if species[min_i] in tree_dict: # If the first joined species is already in the tree dictionary, retrieve it child1 = tree_dict[species[min_i]] else: # Else create new species tree child1 = Tree(species[min_i]) child1.value = min_val / 2.0 if species[min_j] in tree_dict: # If the second joined species is already in the tree dictionary, retrieve it child2 = tree_dict[species[min_j]] else: # Else create new species tree child2 = Tree(species[min_j]) child2.value = min_val / 2.0 # Create new tree with both A and B species joined key = "(" + species[min_i] + "),(" + species[min_j] + ")" phylo_tree = Tree(key) phylo_tree.value = min_val / 2.0 # Put lower value to left of tree if child1.value <= child2.value: phylo_tree.AddLeftChild(child1, (phylo_tree.value - child1.value)) phylo_tree.AddRightChild(child2, (phylo_tree.value - child2.value)) else: phylo_tree.AddLeftChild(child2, (phylo_tree.value - child2.value)) phylo_tree.AddRightChild(child1, (phylo_tree.value - child1.value)) #phylo_tree.printNodeInfo() tree_dict[key] = phylo_tree # Create joined values to put back into 2d matrix joined_values = CreateJoinedDistances(min_i, min_j) # Remove A and B individually and insert AB joined CombineSpecies(joined_values, min_i, min_j) return phylo_tree
def testProperty(self): x = Tree(value=None) self.assertTrue(x.value == None) x.value = 5 self.assertTrue(x.value == 5)
def predictSubTree(image_caption_model, img, now_node, root, depth): if depth > 3: return device = torch.device("cuda:0") count = 0 while (1): count = count + 1 if count == 10: end_node = Tree("None") end_node = word_embedding(end_node) end_node.value = torch.tensor(end_node.value).to(device).float() now_node.add_child(end_node) return sub_tree = Tree(image_caption_model(img, root).flatten()) max = 0 max_index = 0 for i in range(14): if sub_tree.value[i] > max: max = sub_tree.value[i] max_index = i for i in range(14): if i != max_index: sub_tree.value[i] = 0 else: sub_tree.value[i] = 1 now_node.add_child(sub_tree) if sub_tree.value[2] == 1: break for child in now_node.children: if child.value[3] == 1 or child.value[7] == 1 or child.value[ 8] == 1 or child.value[9] == 1 or child.value[12] == 1: predictSubTree(image_caption_model, img, child, root, depth + 1)
def predictTree(image_caption_model, img): device = torch.device("cuda:0") root = Tree("root") root = word_embedding(root) root.value = torch.tensor(root.value).to(device).float() while (1): sub_tree = Tree(image_caption_model(img, root).flatten()) max = 0 max_index = 0 for i in range(14): if sub_tree.value[i] > max: max = sub_tree.value[i] max_index = i for i in range(14): if i != max_index: sub_tree.value[i] = 0 else: sub_tree.value[i] = 1 root.add_child(sub_tree) if sub_tree.value[2] == 1: break for child in root.children: if child.value[3] == 1 or child.value[7] == 1 or child.value[ 8] == 1 or child.value[9] == 1 or child.value[12] == 1: predictSubTree(image_caption_model, img, child, root, 1) return root
def testEquals(self): t1 = Tree(value="Root Node", children=TreeTest.tree_layout) t2 = Tree(value="Root Node", children=TreeTest.tree_layout) self.assertTrue(t1 == t2) t1.value = "x" t2.value = "Y" self.assertFalse(t1 == t2) t1.value = 15 t2.value = 15 self.assertTrue(t1 == t2) t1 = Tree(value="Root Node", children=TreeTest.tree_layout) t2 = Tree(value="Root Node", children=TreeTest.tree_layout_2) self.assertFalse(t1 == t2) t3_1 = Tree(value=None, children=TreeTest.tree_layout_3) t3_2 = Tree(value=None, children=TreeTest.tree_layout_3) self.assertTrue(t3_1 == t3_2)
def convert_json_tree(json): for key, val in json.items(): node = Tree(pos_tag=key) if(type(val) == type("")): node.value = val node.children = None elif(type(val) == type([])): for child in val: node.add_child(convert_json_tree(child)) else: continue return node
def generateInitialPopulation(self): """ Generates the initial population of the evolution """ # iterate over populationSize creating a new tree # for each element for x in range(self.populationSize): new = Tree() new.left = Tree() new.right = Tree() new.value = self.functions[randint(0, 2)] self.generatePopulation(new.left, 1) self.generatePopulation(new.right, 0) self.population.append(new)
def set_(tree_ref, key, value, f): node = ref_to_node(tree_ref, f) if node == None: left = None right = None address = None node = Tree(key, value, left, right, address) tree_ref = TreeRef() elif key < node.key: new_node = set_(node.left_ref, key, value, f) node.left_ref = new_node elif key > node.key: new_node = set_(node.right_ref, key, value, f) node.right_ref = new_node else: node.value = value tree_ref.is_change = True tree_ref.node = node return tree_ref