def fates(self, populations): """ Computes fates for each population Parameters ---------- self : wot.TransportMapModel The TransportMapModel used to find fates populations : list of wot.Population The target populations such as ones from self.population_from_cell_sets. The populations must be from the same time. Returns ------- fates : anndata.AnnData Rows : all cells, Columns : populations index. At point (i, j) : the probability that cell i belongs to population j """ start_day = wot.tmap.unique_timepoint( *populations) # check for unique timepoint populations = Population.copy(*populations, normalize=False, add_missing=True) pop_names = [pop.name for pop in populations] results = [] results.insert(0, np.array([pop.p for pop in populations]).T) while self.can_pull_back(*populations): populations = self.pull_back(*populations, as_list=True, normalize=False) results.insert(0, np.array([pop.p for pop in populations]).T) X = np.concatenate(results) X /= X.sum(axis=1, keepdims=1) obs = self.meta.copy() obs = obs[obs['day'] <= start_day] return anndata.AnnData(X=X, obs=obs, var=pd.DataFrame(index=pop_names))
def transition_table(self, start_populations, end_populations): """ Computes a transition table from the starting populations to the ending populations Parameters ---------- self : wot.TransportMapModel The TransportMapModel start_populations : list of wot.Population The target populations such as ones from self.population_from_cell_sets. THe populations must be from the same time. Returns ------- transition table : anndata.AnnData Rows : starting populations, Columns : ending populations. """ # add "other" population if any cells are missing across all populations start_time = wot.tmap.unique_timepoint(*start_populations) start_populations = Population.copy(*start_populations, normalize=False, add_missing=True) end_populations = Population.copy(*end_populations, normalize=False, add_missing=True) wot.tmap.unique_timepoint( *end_populations) # check for unique timepoint populations = end_populations results = [] results.insert(0, np.array([pop.p for pop in populations]).T) while self.can_pull_back(*populations) and wot.tmap.unique_timepoint( *populations) > start_time: populations = self.pull_back(*populations, as_list=True, normalize=False) end_p = np.vstack([pop.p for pop in populations]) start_p = np.vstack([pop.p for pop in start_populations]) p = (start_p @ end_p.T) p = p / p.sum() return anndata.AnnData( X=p, obs=pd.DataFrame(index=[p.name for p in start_populations]), var=pd.DataFrame(index=[p.name for p in end_populations]))
def trajectories(self, populations): """ Computes a trajectory for each population Parameters ---------- self : wot.TransportMapModel The TransportMapModel used to find ancestors and descendants of the population populations : list of wot.Population The target populations such as ones from self.population_from_cell_sets. THe populations must be from the same time. Returns ------- trajectories : anndata.AnnData Rows : all cells, Columns : populations index. At point (i, j) : the probability that cell i is an ancestor/descendant of population j """ wot.tmap.unique_timepoint(*populations) # check for unique timepoint trajectories = [] populations = Population.copy(*populations, normalize=True, add_missing=False) population_names = [p.name for p in populations] initial_populations = populations def update(head, populations_to_update): idx = 0 if head else len(trajectories) trajectories.insert( idx, np.array([pop.p for pop in populations_to_update]).T) update(True, populations) while self.can_pull_back(*populations): populations = self.pull_back(*populations, as_list=True) update(True, populations) populations = initial_populations while self.can_push_forward(*populations): populations = self.push_forward(*populations, as_list=True) update(False, populations) return anndata.AnnData(X=np.concatenate(trajectories), obs=self.meta.copy(), var=pd.DataFrame(index=population_names))