def remove_reaction_duplicates(self, ignored_m_ids=None): r_id2i = self.get_main_r_id2i() m_id2i = self.get_main_m_id2i(ignored_m_ids=ignored_m_ids) N = self.get_main_N(r_id2i=r_id2i, m_id2i=m_id2i) r_id_groups = get_reaction_duplicates(N, r_id2i) gr_i = 0 for r_ids in r_id_groups: grouped_r_id, gr_i = get_unique_id(self.r_id2i, 'r_type', gr_i) self.r_types.add(grouped_r_id) self.r_id2gr_id.update({r_id: grouped_r_id for r_id in r_ids}) self.r_id2i[grouped_r_id] = self.V.shape[0] sample_r_index = self.r_id2i[r_ids[0]] sample_m_index = np.nonzero(self.N[:, sample_r_index])[0][0] sample_m = self.N[sample_m_index, :] st = sample_m[sample_r_index] * 1.0 indices = tuple((self.r_id2i[r_id] for r_id in r_ids)) self.gr_id2r_id2c[grouped_r_id] = {r_id: sample_m[self.r_id2i[r_id]] / st for r_id in r_ids} self.N = np.concatenate((self.N, self.N[:, (sample_r_index, )]), axis=1) v_new = np.dot(np.array([self.gr_id2r_id2c[grouped_r_id][r_id] for r_id in r_ids]), self.V[indices, :]) replace_zeros(v_new) self.V = np.vstack((self.V, v_new)) self.r_ids -= set(r_ids) self.r_ids.add(grouped_r_id)
def merge_efm_groups(self): r_id2i = self.get_main_r_id2i() m_id2i = self.get_main_m_id2i(boundary=True) efm_id2i = self.get_main_efm_id2i() V = self.get_main_V(r_id2i, efm_id2i) N = self.get_main_N(r_id2i, m_id2i) efm_id_groups = \ get_efm_groups_based_on_boundary_metabolites(N, V, efm_id2i) gr_i = 0 for efm_ids in efm_id_groups: grouped_efm_id, gr_i = get_unique_id(self.efm_id2i, 'pathway', gr_i) self.pathways.add(grouped_efm_id) self.efm_id2gr_id.update({efm_id: grouped_efm_id for efm_id in efm_ids}) self.gr_id2efm_ids[grouped_efm_id] = efm_ids self.efm_id2i[grouped_efm_id] = self.V.shape[1] indices = tuple((self.efm_id2i[efm_id] for efm_id in efm_ids)) lambdas = np.array([[1] for _ in efm_ids]) v_new = np.dot(self.V[:, indices], lambdas) replace_zeros(v_new) self.V = np.concatenate((self.V, v_new), axis=1) self.efm_ids -= set(efm_ids) self.efm_ids.add(grouped_efm_id)
def remove_efm_duplicates(self): efm_id2i = self.get_main_efm_id2i() V = self.get_main_V(efm_id2i=efm_id2i) efm_id_groups = get_efm_duplicates(V, efm_id2i) gr_i = 0 for efm_ids in efm_id_groups: grouped_efm_id, gr_i = get_unique_id(self.efm_id2i, 'efm_type', gr_i) self.folded_efms.add(grouped_efm_id) self.efm_id2gr_id.update({efm_id: grouped_efm_id for efm_id in efm_ids}) self.gr_id2efm_ids[grouped_efm_id] = efm_ids self.efm_id2i[grouped_efm_id] = self.V.shape[1] sample_efm_index = self.efm_id2i[efm_ids[0]] self.V = np.concatenate((self.V, self.V[:, (sample_efm_index, )]), axis=1) self.efm_ids -= set(efm_ids) self.efm_ids.add(grouped_efm_id)
def lump_coupled_reactions(self): coupled_r_id_groups = get_coupled_reactions(self.V, self.r_id2i) lr_i = 0 for r_ids in coupled_r_id_groups: lumped_r_id, lr_i = get_unique_id(self.r_id2i, 'coupled_r', lr_i) self.coupled_rs.add(lumped_r_id) self.r_id2gr_id.update({r_id: lumped_r_id for r_id in r_ids}) self.r_id2i[lumped_r_id] = self.V.shape[0] sample_r_index = self.r_id2i[r_ids[0]] sample_efm_index = np.nonzero(self.V[sample_r_index, :])[0][0] sample_efm = self.V[:, sample_efm_index] c = abs(sample_efm[sample_r_index]) * 1.0 sign = coefficient_to_binary(sample_efm[sample_r_index]) self.gr_id2r_id2c[lumped_r_id] = {r_id: sample_efm[self.r_id2i[r_id]] / c for r_id in r_ids} lambdas = np.array([[self.gr_id2r_id2c[lumped_r_id][r_id]] for r_id in r_ids]) r_new = np.dot(self.N[:, tuple((self.r_id2i[r_id] for r_id in r_ids))], lambdas) replace_zeros(r_new) self.N = np.concatenate((self.N, r_new), axis=1) self.V = np.concatenate((self.V, [self.V[sample_r_index, :] * sign]), axis=0) self.r_ids -= set(r_ids) self.r_ids.add(lumped_r_id)