def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() tree.add_branch_lengths() if tree.has_negative_branch_lengths(): msg_a = 'calculating weights for a tree ' msg_b = 'with negative branch lengths is not implemented' raise HandlingError(msg_a + msg_b) # get the selected names selection = Util.get_stripped_lines(fs.selection.splitlines()) selected_name_set = set(selection) possible_name_set = set(node.get_name() for node in tree.gen_tips()) extra_names = selected_name_set - possible_name_set if extra_names: msg_a = 'the following selected names are not valid tips: ' msg_b = str(tuple(extra_names)) raise HandlingError(msg_a + msg_b) # prune the tree for name in set(node.name for node in tree.gen_tips()) - set(selection): try: node = tree.get_unique_node(name) except NewickSearchError as e: raise HandlingError(e) tree.prune(node) # get the weights if fs.stone: name_weight_pairs = LeafWeights.get_stone_weights(tree) elif fs.thompson: name_weight_pairs = LeafWeights.get_thompson_weights(tree) # report the weights lines = ['%s: %f' % pair for pair in name_weight_pairs] return '\n'.join(lines) + '\n'
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() tree.add_branch_lengths() if tree.has_negative_branch_lengths(): msg_a = 'calculating weights for a tree ' msg_b = 'with negative branch lengths is not implemented' raise HandlingError(msg_a + msg_b) # get the selected names selection = Util.get_stripped_lines(fs.selection.splitlines()) selected_name_set = set(selection) possible_name_set = set(node.get_name() for node in tree.gen_tips()) extra_names = selected_name_set - possible_name_set if extra_names: msg_a = 'the following selected names are not valid tips: ' msg_b = str(tuple(extra_names)) raise HandlingError(msg_a + msg_b) # prune the tree for name in set(node.name for node in tree.gen_tips()) - set(selection): try: node = tree.get_unique_node(name) except NewickSearchError as e: raise HandlingError(e) tree.prune(node) # get the weights if fs.stone: name_weight_pairs = LeafWeights.get_stone_weights(tree) elif fs.thompson: name_weight_pairs = LeafWeights.get_thompson_weights(tree) # report the weights lines = ['%s: %f' % pair for pair in name_weight_pairs] return '\n'.join(lines) + '\n'
def get_response_content(fs): tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() tree.add_branch_lengths() if tree.has_negative_branch_lengths(): msg_a = 'calculating weights for a tree ' msg_b = 'with negative branch lengths is not implemented' raise HandlingError(msg_a + msg_b) if fs.stone: name_weight_pairs = LeafWeights.get_stone_weights(tree) elif fs.thompson: name_weight_pairs = LeafWeights.get_thompson_weights(tree) lines = ['%s: %f' % pair for pair in name_weight_pairs] return '\n'.join(lines) + '\n'