def calc_nonepitope_star_distance(tree, attr='ne_star', seasons = []): ''' calculates the distance at nonepitope sites of any tree node to ref tree -- dendropy tree attr -- the attribute name used to save the result ''' if not hasattr(tree, "nonepitope_star_distance_assigned") or tree.nonepitope_star_distance_assigned==False: setup_epitope_mask() for node in tree.postorder_node_iter(): if len(node.season_tips) and node!=tree.seed_node: if not hasattr(node, 'aa'): node.aa = translate(node.seq) tmp_node = node.parent_node cur_season = min(node.season_tips.keys()) prev_season = seasons[max(0,seasons.index(cur_season)-1)] while True: if tmp_node!=tree.seed_node: if prev_season in tmp_node.season_tips and len(tmp_node.season_tips[prev_season])>0: break else: tmp_node=tmp_node.parent_node else: break if not hasattr(tmp_node, 'aa'): tmp_node.aa = translate(tmp_node.seq) node.__setattr__(attr, nonepitope_distance(node.aa, tmp_node.aa)) else: node.__setattr__(attr, np.nan) tree.nonepitope_star_distance_assigned=True
def calc_rbs_distance(self, tree, attr='rb', ref=None): ''' calculates the distance at receptor binding sites of any tree node to ref tree -- dendropy tree attr -- the attribute name used to save the result ''' if ref == None: ref = translate(tree.seed_node.seq) for node in tree.postorder_node_iter(): if not hasattr(node, 'aa'): node.aa = translate(node.seq) node.__setattr__(attr, self.rbs_distance(node.aa, ref))
def calc_nonepitope_distance(self, tree, attr='ne', ref=None): ''' calculates -1 * distance at nonepitope sites of each node in tree to ref tree -- dendropy tree attr -- the attribute name used to save the result ''' if ref == None: ref = translate(tree.seed_node.seq) for node in tree.postorder_node_iter(): if not hasattr(node, 'aa'): node.aa = translate(node.seq) distance = self.nonepitope_distance(node.aa, ref) node.__setattr__(attr, -1 * distance)
def calc_rbs_distance(tree, attr='rb', ref = None): ''' calculates the distance at receptor binding sites of any tree node to ref tree -- dendropy tree attr -- the attribute name used to save the result ''' if not hasattr(tree, "rbs_distance_assigned") or tree.rbs_distance_assigned==False: if ref == None: ref = translate(tree.seed_node.seq) for node in tree.postorder_node_iter(): if not hasattr(node, 'aa'): node.aa = translate(node.seq) node.__setattr__(attr, rbs_distance(node.aa, ref)) tree.rbs_distance_assigned=True
def calc_nonepitope_distance(tree, attr="ne", ref=None): """ calculates the distance at nonepitope sites of any tree node to ref tree -- dendropy tree attr -- the attribute name used to save the result """ if not hasattr(tree, "nonepitope_distance_assigned") or tree.nonepitope_distance_assigned == False: if ref == None: ref = translate(tree.seed_node.seq) for node in tree.postorder_node_iter(): if not hasattr(node, "aa"): node.aa = translate(node.seq) node.__setattr__(attr, nonepitope_distance(node.aa, ref)) tree.nonepitope_distance_assigned = True
def calc_epitope_distance(tree, attr='ep', ref = None): ''' calculates the distance at epitope sites of any tree node to ref tree -- dendropy tree attr -- the attribute name used to save the result ''' if not hasattr(tree, "epitope_distance_assigned") or tree.epitope_distance_assigned==False: setup_epitope_mask() if ref == None: ref = translate(tree.seed_node.seq) for node in tree.postorder_node_iter(): if not hasattr(node, 'aa'): node.aa = translate(node.seq) node.__setattr__(attr, epitope_distance(node.aa, ref)) tree.epitope_distance_assigned=True
def calc_tolerance(tree, attr='tol'): ''' calculates the distance at epitope sites of any tree node to ref tree -- dendropy tree attr -- the attribute name used to save the result ''' from Bio import AlignIO aa, sites, wt_aa, aa_prob = load_mutational_tolerance() aln = AlignIO.read('/Users/yujiazhou/Documents/nextflu/H9_nextflu-master/augur/source-data/H1_H3.fasta', 'fasta') # returns true whenever either of the sequences have a gap aligned = (np.array(aln)!='-').min(axis=0) # map alignment positions to sequence positions, subset to aligned amino acids indices = {} for seq in aln: indices[seq.name] = (np.cumsum(np.fromstring(str(seq.seq), dtype='S1')!='-')-1)[aligned] # make a reduced set of amino-acid probabilities that only contains aligned positions aa_prob=aa_prob[indices['H1'],:] # attach another column for non-canonical amino acids aa_prob = np.hstack((aa_prob, 1e-5*np.ones((aa_prob.shape[0],1)))) if not hasattr(tree, "tolerance_assigned") or tree.tolerance_assigned==False: for node in tree.postorder_node_iter(): if not hasattr(node, 'aa'): node.aa = translate(node.seq) node.__setattr__(attr, calc_fitness_tolerance(node.aa, aa_prob, aa, indices['H3'])) tree.tolerance_assigned=True
def calc_epitope_cross_immunity(self, tree, timepoint, window=2.0, attr='ep_x'): ''' calculates the distance at epitope sites to contemporaneous viruses this should capture cross-immunity of circulating viruses meant to be used in conjunction with epitope_distance that focuses on escape from previous human immunity tree -- dendropy tree attr -- the attribute name used to save the result ''' comparison_nodes = [] for node in tree.postorder_node_iter(): if node.is_leaf(): if node.num_date < timepoint and node.num_date > timepoint - window: comparison_nodes.append(node) if not hasattr(node, 'np_ep'): if not hasattr(node, 'aa'): node.aa = translate(node.seq) node.np_ep = np.array(list(self.epitope_sites(node.aa))) print "calculating cross-immunity to " + str( len(comparison_nodes)) + " comparison nodes" for node in tree.postorder_node_iter(): mean_distance = 0 count = 0 for comp_node in comparison_nodes: mean_distance += self.fast_epitope_distance( node.np_ep, comp_node.np_ep) count += 1 if count > 0: mean_distance /= float(count) node.__setattr__(attr, mean_distance)
def calc_epitope_distance(self, tree, attr='ep', ref=None): ''' calculates the distance at epitope sites of any tree node to ref tree -- dendropy tree attr -- the attribute name used to save the result ''' for node in tree.postorder_node_iter(): if not hasattr(node, 'np_ep'): if not hasattr(node, 'aa'): node.aa = translate(node.seq) node.np_ep = np.array(list(self.epitope_sites(node.aa))) if ref == None: ref = tree.seed_node for node in tree.postorder_node_iter(): node.__setattr__(attr, self.fast_epitope_distance(node.np_ep, ref.np_ep))