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 test_reaction_duplicates_content(self): N = np.array([[1, 2, 0, -1], [0, 0, 1, 0], [-2, -4, -1, 2]]) r_id2i = {'r1': 0, 'r2': 1, 'r3': 2, 'r4': 3} r_duplicates = get_reaction_duplicates(N, r_id2i) self.assertSetEqual({'r1', 'r2', 'r4'}, set(r_duplicates[0]), 'Was supposed to find [r1, r2, r4] as duplicates, found %s' % r_duplicates[0])
def test_reaction_duplicates_len(self): N = np.array([[1, 2, 0, -1], [0, 0, 1, 0], [-2, -4, -1, 2]]) r_id2i = {'r1': 0, 'r2': 1, 'r3': 2, 'r4': 3} r_duplicates = get_reaction_duplicates(N, r_id2i) self.assertEqual(1, len(r_duplicates), 'Was supposed to find one group of duplicates, found %d' % len(r_duplicates))
def test_remove_reaction_duplicates_V_shape(self): N = np.array([[1, 2, 0, -1], [0, 0, 1, 0], [-2, -4, -1, 2]]) V = np.array([[-1, 0], [1, 1], [0, -1], [2, -1]]) r_id2i = {'r1': 0, 'r2': 1, 'r3': 2, 'r4': 3} r_duplicates = get_reaction_duplicates(N, r_id2i) N_new, V_new, new_r_id2i, r_id2gr_id, _ = remove_reaction_duplicates(N, V, r_duplicates, r_id2i) self.assertTupleEqual((2, 2), V_new.shape, 'Was supposed to get 2x2 V, got %s' % [str(it) for it in V_new.shape])
def test_remove_reaction_duplicates_NV(self): N = np.array([[1, 2, 0, -1], [0, 0, 1, 0], [-2, -4, -1, 2]]) V = np.array([[-1, 0], [1, 1], [0, -1], [2, -1]]) r_id2i = {'r1': 0, 'r2': 1, 'r3': 2, 'r4': 3} r_duplicates = get_reaction_duplicates(N, r_id2i) N_new, V_new, new_r_id2i, r_id2gr_id, _ = remove_reaction_duplicates(N, V, r_duplicates, r_id2i) NV = np.dot(N, V) NV_new = np.dot(N_new, V_new) self.assertListEqual(list(NV.flatten()), list(NV_new.flatten()), 'NV was not supposed to change')
def test_remove_reaction_duplicates_N_content_1(self): N = np.array([[1, 2, 0, -1], [0, 0, 1, 0], [-2, -4, -1, 2]]) V = np.array([[-1, 0], [1, 1], [0, -1], [2, -1]]) r_id2i = {'r1': 0, 'r2': 1, 'r3': 2, 'r4': 3} r_duplicates = get_reaction_duplicates(N, r_id2i) N_new, V_new, new_r_id2i, r_id2gr_id, _ = remove_reaction_duplicates(N, V, r_duplicates, r_id2i) m2_st = N[1, new_r_id2i[r_id2gr_id['r1']]] self.assertEqual(0, m2_st, 'Was supposed to find no metabolite m2 in the grouped reaction, found %g' % m2_st)
def test_remove_reaction_duplicates_N_content_2(self): N = np.array([[1, 2, 0, -1], [0, 0, 1, 0], [-2, -4, -1, 2]]) V = np.array([[-1, 0], [1, 1], [0, -1], [2, -1]]) r_id2i = {'r1': 0, 'r2': 1, 'r3': 2, 'r4': 3} r_duplicates = get_reaction_duplicates(N, r_id2i) N_new, V_new, new_r_id2i, r_id2gr_id, _ = remove_reaction_duplicates(N, V, r_duplicates, r_id2i) new_r_index = new_r_id2i[r_id2gr_id['r1']] m1_st = N[0, new_r_index] m3_st = N[2, new_r_index] self.assertEqual(-2, m3_st / m1_st, 'Was supposed to get -2 as a proportion between m3 and m1 in the new reaction, found %g' % (m3_st / m1_st))
def test_remove_reaction_duplicates_V_content(self): N = np.array([[1, 2, 0, -1], [0, 0, 1, 0], [-2, -4, -1, 2]]) V = np.array([[-1, 0], [1, 1], [0, -1], [2, -1]]) r_id2i = {'r1': 0, 'r2': 1, 'r3': 2, 'r4': 3} r_duplicates = get_reaction_duplicates(N, r_id2i) N_new, V_new, new_r_id2i, r_id2gr_id, _ = remove_reaction_duplicates(N, V, r_duplicates, r_id2i) new_r_index = new_r_id2i[r_id2gr_id['r1']] efm1_c = V_new[new_r_index, 0] efm2_c = V_new[new_r_index, 1] ratio = efm2_c / efm1_c self.assertEqual(-3, ratio, 'Was supposed to get -3 as a ratio between the coefficients of the grouped reaction in EFMs 2 and 1, got %g' % ratio)