def annotate_tree_all(tree, seqs, VERBOSE=0): '''Annotate tree with metadata''' from hivwholeseq.utils.tree import add_mutations_tree from operator import attrgetter add_mutations_tree(tree, translate=False, mutation_attrname='muts') seqnames = [seq.name for seq in seqs] for leaf in tree.get_terminals(): if 'Isolate' not in leaf.name: leaf.patient = leaf.name.split('_')[0] else: leaf.patient = leaf.name[len('Isolate_'):] sample = seqs[seqnames.index(leaf.name)] leaf.subtype = sample.subtype if (leaf.patient[0] == 'p') and (len(leaf.patient) <= 3): leaf.DSI = float(leaf.name.split('_')[1]) leaf.CD4 = sample.cell_count leaf.VL = sample.viral_load leaf.name = None # Reroot tree.root_at_midpoint() # Propagate the properties up the tree for prop in ['patient', 'subtype']: groups = defaultdict(list) for node in tree.get_nonterminals(order='postorder'): key = set([getattr(child, prop, None) for child in node.clades]) if (None not in key) and (len(key) == 1): setattr(node, prop, key.pop())
def annotate_tree(patient, tree, ali, VERBOSE=0): '''Annotate tree with metadata''' from hivwholeseq.utils.tree import add_mutations_tree from operator import attrgetter add_mutations_tree(tree, translate=False, mutation_attrname='muts') for leaf in tree.get_terminals(): leaf.patient = patient.code leaf.DSI = float(leaf.name.split('_')[1]) leaf.name = None sample = patient.samples.loc[patient.times == leaf.DSI].iloc[0] leaf.CD4 = sample['CD4+ count'] leaf.VL = sample['viral load'] leaf.subtype = patient.Subtype # Reroot tmin = min(leaf.DSI for leaf in tree.get_terminals()) newroot = min((leaf for leaf in tree.get_terminals()), key=attrgetter('DSI')) tree.root_with_outgroup(newroot) # Propagate the properties up the tree for prop in ['patient', 'subtype']: groups = defaultdict(list) for node in tree.get_nonterminals(order='postorder'): key = set([getattr(child, prop, None) for child in node.clades]) if (None not in key) and (len(key) == 1): setattr(node, prop, key.pop())
def annotate_tree(patient, tree, ali, VERBOSE=0): '''Annotate tree with metadata''' from hivwholeseq.utils.tree import add_mutations_tree from operator import attrgetter add_mutations_tree(tree, translate=False, mutation_attrname='muts') for leaf in tree.get_terminals(): leaf.DSI = float(leaf.name.split('_')[0]) leaf.frequency = float(leaf.name.split('_')[2].split('%')[0]) / 100.0 leaf.count = int(leaf.name.split('_')[3]) leaf.name = None sample = patient.samples.loc[patient.times == leaf.DSI].iloc[0] leaf.CD4 = sample['CD4+ count'] leaf.VL = sample['viral load'] for node in tree.get_terminals() + tree.get_nonterminals(): node.subtype = patient.Subtype # Reroot tmin = min(leaf.DSI for leaf in tree.get_terminals()) newroot = max((leaf for leaf in tree.get_terminals() if leaf.DSI == tmin), key=attrgetter('frequency')) tree.root_with_outgroup(newroot)
def annotate_tree(patient, tree, VERBOSE=0, fields=('DSI', 'muts', 'VL', 'ntemplates', 'CD4', 'patient')): '''Annotate a tree with info on the nodes''' from hivwholeseq.utils.tree import add_mutations_tree for node in tree.get_terminals(): label = node.name entries = label.split('_') node.name = entries[0] if node.name == 'reference': continue # Days Since Infection if 'DSI' in fields: time = float(entries[0]) node.DSI = time sample = patient.samples.loc[patient.times == time].iloc[0] if 'CD4' in fields: node.CD4 = sample['CD4+ count'] if 'VL' in fields: node.VL = sample['viral load'] # FIXME: shall we check the patient method for this? # Well, we are going to quantify fragment-by-fragment, so... if 'ntemplates' in fields: node.ntemplates = sample['n templates'] if 'subtype' in fields: for node in tree.get_terminals() + tree.get_nonterminals(): node.subtype = patient.Subtype if 'patient' in fields: for node in tree.get_terminals() + tree.get_nonterminals(): node.patient = patient.code if 'muts' in fields: add_mutations_tree(tree, translate=False, mutation_attrname='muts') if 'mutsprot' in fields: add_mutations_tree(tree, translate=True, mutation_attrname='mutsprot')