def exclude_tip(tip, tree, verbose=False): """Return pruned tree with tip excluded tip -- a PyCogent PhyloNode object from the tree tree -- a PyCogent PhyloNode object (the tree) NOTE: This function exists to support distance-based exclusion of neightbors. For holdout analysis we really want to exclude the organism in the trait table rather than the tree. """ # Make sure the tip is really a tip if not tip.isTip(): raise ValueError( "The 'holdout' fn got a non-tip node for it's 'tip' parameter.") #Old exclusion based method...but for large trees this is quite slow # Assuming that's OK, remove the tip #if tip.Parent is not None: # removal_ok = tip.Parent.remove(tip) #else: # removal_ok = False #if not removal_ok: # raise ValueError("holdout could not remove node '%s' from tree." %(tip.Name)) #tree.prune() #Newer subtree based method tips_to_keep = [t.Name for t in tree.tips() if t.Name != tip.Name] if verbose: print "Generating subtree with %i tips" % len(tips_to_keep) subtree = get_sub_tree(tree, tips_to_keep) return subtree
def exclude_tip(tip, tree,verbose=False): """Return pruned tree with tip excluded tip -- a PyCogent PhyloNode object from the tree tree -- a PyCogent PhyloNode object (the tree) NOTE: This function exists to support distance-based exclusion of neightbors. For holdout analysis we really want to exclude the organism in the trait table rather than the tree. """ # Make sure the tip is really a tip if not tip.isTip(): raise ValueError("The 'holdout' fn got a non-tip node for it's 'tip' parameter.") #Old exclusion based method...but for large trees this is quite slow # Assuming that's OK, remove the tip #if tip.Parent is not None: # removal_ok = tip.Parent.remove(tip) #else: # removal_ok = False #if not removal_ok: # raise ValueError("holdout could not remove node '%s' from tree." %(tip.Name)) #tree.prune() #Newer subtree based method tips_to_keep = [t.Name for t in tree.tips() if t.Name != tip.Name] if verbose: print "Generating subtree with %i tips" %len(tips_to_keep) subtree = get_sub_tree(tree,tips_to_keep) return subtree
def exclude_tip_neighbors(tip,tree, verbose=False): # check that the exclusion distance won't exclude the entire tree max_dist = tree.maxTipTipDistance()[0] tips_to_delete = tip.tipsWithinDistance(exclusion_distance) tips_to_delete = [t.Name for t in tips_to_delete] if verbose: print "Tips to delete:",len(tips_to_delete) tips_to_keep = [t.Name for t in tree.tips() if t.Name not in tips_to_delete] if float(exclusion_distance) > float(max_dist) or \ len(tips_to_delete) == len(tree.tips()): raise ValueError("specified tree would be entirely excluded because branch lengths are too short") #holdout_tip = tip holdout_tip = tip.Name #We're working with the *pruned* tree so we want the holdout tip #removed if holdout_tip in tips_to_keep: tips_to_keep.remove(holdout_tip) if verbose: print "Will remove our tip of interest from the tree: {0}".format(holdout_tip) if verbose: print "Tips to keep:", len(tips_to_keep) subtree = get_sub_tree(tree,tips_to_keep) del tree # Then it's neighbors #for tip in tips_to_delete: # if tip == holdout_tip: # #Don't exclude the tip you're testing # continue # tree = exclude_tip(tip,tree) return subtree