Beispiel #1
0
def rearr_Worker(t, veal, target_label, dest_label, target_parent_label, result_queue, sema):
	t_prime = dendropy.Tree(t)
	try:
		t_prime, affected = Subtree.move_subtree(t_prime, target_label, dest_label)
		assert len(t_prime.nodes()) == len(t.nodes())
	except Exception:
		print("EXCEPTION ENCOUNTERED!!!!")
		result_queue.put((None, "Subtree.move_subtree Failed"))
		#result_queue.close() # do not use if using Manager.Queue()
		sema.release()
		return

# the three local branche we need to optimize are edges of
	# target, dest, and parent of target
	lst = [(target_parent_label, target_label),\
		   (target_parent_label, dest_label),\
		   (t_prime.find_node_with_label(target_parent_label).parent_node.label, \
		   target_parent_label)]
	log_l_prime, t_prime = veal(t_prime, lst)
#	print log_l_prime, t_prime
	if log_l_prime is not None: # if is None, means optimization failed
		result_queue.put((log_l_prime, t_prime))
#		result_queue.close() # do not use if using Manager.Queue()
	else:
		#print("OPTIMIZATION FAILED!!!")
		result_queue.put((None, "Subtree.move_subtree Failed"))
#		result_queue.close() # do not use if using Manager.Queue()
	sema.release()
Beispiel #2
0
	def rearr(t, log_l, rL, rU):
		for target in t.postorder_node_iter():
			if target.parent_node is None:
				break
			# first make a deep copy of the tree
			t_prime = dendropy.Tree(t)
			print "finding subtree rearrangement for {0}".format(target.label)
			dest = cello.subtree_rearrangement(t_prime, target, rL, rU)
			print "inserting subtree {0} into branch above {1}".format(target.label, dest.label)
			try:
				# attach the subtree of <target> to branch above <dest>
				t_prime, affected = Subtree.move_subtree(t_prime, target.label, dest.label)
			except Exception:
				print "could not succesfully do the move, forget it! move on!"
				continue
			cheat_order = Tree.postorder_traverse_with_cheat(t_prime, affected)
			# the three local branche we need to optimize are edges of
			# target, dest, and parent of target
			lst = []
			for i, (k, bunch) in enumerate(cheat_order):
				for j, (a, t_a) in enumerate(bunch):
					if a in (target.label, dest.label, target.parent_node.label):
						lst.append((i, j))
			print 'list is', lst
			log_l_prime = MyMat.optimize_branch_fast(lst, cheat_order, msa, single_model, paired_model, S, P)
			if log_l_prime < log_l:
				print "subtree rearrangement improved log like from {0}-->{1}, keep!".format(log_l, log_l_prime)
				t = t_prime
				log_l = log_l_prime
				# put in the fastly optimized branch lengths
				for i, j in lst:
					t.find_node_with_label(cheat_order[i][1][j][0]).edge_length = cheat_order[i][1][j][1]
				raw_input("continue?")
				return
			else:
				print "subtree rearrangment made log likelihood worse. Move on"
				raw_input("continue")
				return
		return t, log_l