def compute_through(epoch_2, epoch_3, break_points_12, break_points_123): """Computes the matrices for moving through an interval. :rtype: list[matrix] """ through_12 = [None] * len(break_points_12) through_123 = [None] * (len(break_points_123) - 1) def state_map_21(state): return frozenset([(123, nucleotides) for (_, nucleotides) in state]) projection_21 = projection_matrix(epoch_2.state_space, epoch_3.state_space, state_map_21) # Through epoch 2 for i in range(len(break_points_12) - 1): through_12[i] = epoch_2.probability_matrix(break_points_12[i + 1] - break_points_12[i]) through_12[len(break_points_12)-1] = \ epoch_2.probability_matrix(break_points_123[0] - break_points_12[-1]) * projection_21 # Through epoch 3 for i in range(len(break_points_123) - 1): through_123[i] = epoch_3.probability_matrix(break_points_123[i + 1] - break_points_123[i]) # As a hack we set up a pseudo through matrix for the last interval that # just puts all probability on ending in one of the end states. This # simplifies the HMM transition probability code as it avoids a special case # for the last interval. # noinspection PyCallingNonCallable pseudo_through = matrix(zeros((len(epoch_3.state_space.states), len(epoch_3.state_space.states)))) pseudo_through[:, epoch_3.state_space.state_type[(STATE_E, STATE_E)][0]] = 1.0 through_123.append(pseudo_through) return through_12 + through_123
def _compute_upto0(isolation, migration, break_points): """Computes the probability matrices for moving to time zero.""" # the states in the isolation state space are the same in the migration state_map = lambda x: x projection = projection_matrix(isolation.state_space, migration.state_space, state_map) return isolation.probability_matrix(break_points[0]) * projection
def _compute_upto0(isolation, ancestral, break_points): """Computes the probability matrices for moving from time zero up to, but not through, interval i.""" def state_map(state): return frozenset([(0, nucs) for (_, nucs) in state]) projection = projection_matrix(isolation.state_space, ancestral[0].state_space, state_map) return isolation.probability_matrix(break_points[0]) * projection
def compute_up_to0(epoch_1, epoch_2, tau1): """Computes the probability matrices for moving to time zero.""" def state_map_32(state): def lineage_map(lineage): population, nucleotides = lineage if population == 3: return 3, nucleotides else: return 12, nucleotides return frozenset(lineage_map(lineage) for lineage in state) projection_32 = projection_matrix(epoch_1.state_space, epoch_2.state_space, state_map_32) return epoch_1.probability_matrix(tau1) * projection_32
def __init__(self, isolation_ctmc, middle_ctmc, ancestral_ctmc, p, q, middle_break_points, ancestral_break_points): """Construct all the matrices and cache them for the method calls. """ super(AdmixtureCTMCSystem12, self).__init__( no_hmm_states=len(middle_break_points) + len(ancestral_break_points), initial_ctmc_state=isolation_ctmc.state_space.i12_index) self.no_middle_states = len(middle_break_points) self.middle = middle_ctmc self.no_ancestral_states = len(ancestral_break_points) self.ancestral = ancestral_ctmc self.through_ = [None] * (self.no_middle_states + self.no_ancestral_states - 1) for i in xrange(self.no_middle_states - 1): self.through_[i] = middle_ctmc.probability_matrix( middle_break_points[i + 1] - middle_break_points[i]) xx = middle_ctmc.probability_matrix(ancestral_break_points[0] - middle_break_points[-1]) projection = projection_matrix( middle_ctmc.state_space, ancestral_ctmc.state_space, lambda state: frozenset([(0, nucs) for (_, nucs) in state])) self.through_[self.no_middle_states - 1] = xx * projection for i in xrange(self.no_middle_states, self.no_middle_states + self.no_ancestral_states - 1): ii = i - self.no_middle_states self.through_[i] = ancestral_ctmc.probability_matrix( ancestral_break_points[ii + 1] - ancestral_break_points[ii]) pseudo_through = matrix( zeros((len(ancestral_ctmc.state_space.states), len(ancestral_ctmc.state_space.states)))) pseudo_through[:, ancestral_ctmc.state_space.end_states[0]] = 1.0 self.through_.append(pseudo_through) projection = admixture_state_space_map(isolation_ctmc.state_space, middle_ctmc.state_space, p, q) self.upto_ = compute_upto( isolation_ctmc.probability_matrix(middle_break_points[0]) * projection, self.through_) self.between_ = compute_between(self.through_)
def _compute_through(migration_ctmcs, migration_break_points, ancestral_ctmcs, ancestral_break_points): """Computes the matrices for moving through an interval. :param migration_ctmcs: CTMCs for the migration phase. :type migration_ctmcs: list[IMCoalHMM.CTMC.CTMC] :param ancestral_ctmcs: CTMCs for the ancestral population. :type ancestral_ctmcs: list[IMCoalHMM.CTMC.CTMC] :param migration_break_points: List of break points in the migration phase. :type migration_break_points: list[float] :param ancestral_break_points: List of break points in the ancestral population. :type ancestral_break_points: list[float] """ def state_map(state): return frozenset([(0, nucs) for (_, nucs) in state]) projection = projection_matrix(migration_ctmcs[0].state_space, ancestral_ctmcs[0].state_space, state_map) no_migration_states = len(migration_break_points) no_ancestral_states = len(ancestral_break_points) # Construct the transition matrices for going through each interval in # the migration phase migration_through = map( ComputeThroughInterval(migration_ctmcs, migration_break_points), range(no_migration_states - 1)) last_migration = migration_ctmcs[-1].probability_matrix( ancestral_break_points[0] - migration_break_points[-1]) * projection migration_through.append(last_migration) ancestral_through = map( ComputeThroughInterval(ancestral_ctmcs, ancestral_break_points), range(no_ancestral_states - 1)) # As a hack we set up a pseudo through matrix for the last interval that # just puts all probability on ending in one of the end states. This # simplifies the HMM transition probability code as it avoids a special case # for the last interval. # noinspection PyCallingNonCallable pseudo_through = matrix( zeros((len(ancestral_ctmcs[0].state_space.states), len(ancestral_ctmcs[0].state_space.states)))) pseudo_through[:, ancestral_ctmcs[0].state_space.end_states[0]] = 1.0 ancestral_through.append(pseudo_through) return migration_through + ancestral_through