def generate_factor(treeFile, columnFile, prior, cpd, domain):

    # Build the binary tree and find the interior nodes
	tree = BinaryTree()
	tree.parse_from_string(read_nh_file_into_single_line(treeFile))
	interior_nodes = []
	tree.assign_idx(interior_nodes, [0])


	animal_to_idx={}
	idx_to_animal={}
	factors=[]


	def build_prior_factor(idx):
		factor=Factor()
		factor.scope = [idx]
		factor.card = [len(domain)]
		factor.val = prior
		return factor
	def build_evolution_factor(i2,i1):
		factor=Factor()
		factor.scope = [i2,i1]
		factor.card = [len(domain),len(domain)]
		factor.val = cpd
		return factor


	def assign_index(TreeNode):
		animal_to_idx[TreeNode.data]=TreeNode.idx+1
		idx_to_animal[TreeNode.idx+1]=TreeNode.data
		if TreeNode.left!=None:
			f_e=build_evolution_factor(TreeNode.left.idx+1,TreeNode.idx+1)
			f_e.name = TreeNode.left.data+'<--'+TreeNode.data
			factors.append(f_e)
			assign_index(TreeNode.left)
		if TreeNode.right!=None:
			f_e=build_evolution_factor(TreeNode.right.idx+1,TreeNode.idx+1)
			f_e.name = TreeNode.right.data+'<--'+TreeNode.data
			factors.append(f_e)
			assign_index(TreeNode.right)

	f_p=build_prior_factor(tree.idx+1)
	f_p.name=tree.data
	factors.append(f_p)
	assign_index(tree)

	sequences = read_sequences(columnFile,animal_to_idx)
	domain_to_idx = {domain[i]: i for i in xrange(len(domain))}

	return sequences,tree,factors
Пример #2
0
def generate_factor(treeFile, columnFile, prior, cpd, domain):

    # Build the binary tree and find the interior nodes
    tree = BinaryTree()
    tree.parse_from_string(read_nh_file_into_single_line(treeFile))
    interior_nodes = []
    tree.assign_idx(interior_nodes, [0])

    animal_to_idx = {}
    idx_to_animal = {}
    factors = []

    def build_prior_factor(idx):
        factor = Factor()
        factor.scope = [idx]
        factor.card = [len(domain)]
        factor.val = prior
        return factor

    def build_evolution_factor(i2, i1):
        factor = Factor()
        factor.scope = [i2, i1]
        factor.card = [len(domain), len(domain)]
        factor.val = cpd
        return factor

    def assign_index(TreeNode):
        animal_to_idx[TreeNode.data] = TreeNode.idx + 1
        idx_to_animal[TreeNode.idx + 1] = TreeNode.data
        if TreeNode.left != None:
            f_e = build_evolution_factor(TreeNode.left.idx + 1,
                                         TreeNode.idx + 1)
            f_e.name = TreeNode.left.data + '<--' + TreeNode.data
            factors.append(f_e)
            assign_index(TreeNode.left)
        if TreeNode.right != None:
            f_e = build_evolution_factor(TreeNode.right.idx + 1,
                                         TreeNode.idx + 1)
            f_e.name = TreeNode.right.data + '<--' + TreeNode.data
            factors.append(f_e)
            assign_index(TreeNode.right)

    f_p = build_prior_factor(tree.idx + 1)
    f_p.name = tree.data
    factors.append(f_p)
    assign_index(tree)

    sequences = read_sequences(columnFile, animal_to_idx)
    domain_to_idx = {domain[i]: i for i in xrange(len(domain))}

    return sequences, tree, factors