def go(species): method_dir = r'%s/method/Ding' % my_constants.basePath out_dir = r'%s/Ding' % my_constants.basePath my_util.mkdir_p(out_dir) # source_file = '%s/dataset/networks/%s' % (my_constants.basePath, my_constants.species_sbml[species]) source_file = '%s/method/Ding/Y2H-union.txt' # S, mets, rxns, revs, met_names, rxn_names, biomass, met_comparts = importer.sbmlStoichiometricMatrix(source_file, True, read_species_compart=True, remove_biomass=False, normalize_stoich=False) f = open('Ding.m', 'w') write_line(f, 'addpath %s/src' % method_dir) write_line(f, "model = readCbModel('%s')" % source_file) # write_line(f, "model.c(%d) = 1;" % (rxns.index(biomass[0]) + 1)) # write_line(f, "changeCobraSolver('glpk');") write_line(f, '[modules] = main( model );') write_line(f, 'save Dingout.mat ') f.close() my_util.prepare_matlab_file_and_exec_and_wait_finish( 'Ding', 'Dingout.mat', False) res_vars = my_util.try_load_matlab_result('Dingout.mat') raw_modules = res_vars[ 'modules'] # if matlab has failed, this will throw exception!
def read_module_hierarchy(file_path, mets, rxns): res_vars = my_util.try_load_matlab_result(file_path) raw_modules = res_vars['mods'] raw_modules_hierarchy = res_vars['mods_hier'] mods_rxns = [] mods_mets = [] for x, raw_mod in enumerate(raw_modules): raw_mod = raw_mod[0] mod_rxns_idxs = raw_mod[0][:, 0] mod_mets_idxs = raw_mod[1][:, 0] # mod_metname[0] = str(raw_mod[2][:, 0][1][0]) mod_s = raw_mod[3].toarray().tolist() # mod_rxnname[0] = str(raw_mod[4][:, 0][1][0]) mod_bmatrix = raw_mod[5].tolist() if len(raw_mod[5][0]) > 0 else None mod_q = raw_mod[6][0][0] if len(raw_mod[6][0]) > 0 else None mod_s_eig = raw_mod[7][:, 0] if len(raw_mod[7][0]) > 0 else None mod_rxns = [rxns[i - 1] for i in mod_rxns_idxs] mod_mets = [mets[i - 1] for i in mod_mets_idxs] mods_rxns.append(mod_rxns) mods_mets.append(mod_mets) raw_modules_hierarchy = convert_to_int_recursive( raw_modules_hierarchy.toarray().tolist()) modules_hierarchy = [ [None, [], -1] for i in range(len(raw_modules)) ] # map of module index to (super_index, [sub_indexes], depth). note that depth of root is 1 for ri, rm in enumerate(raw_modules_hierarchy): for di, d in enumerate(rm): if d != 0: modules_hierarchy[ri][1].append(di) modules_hierarchy[di][0] = ri # note that tree_height - depth + 1 will give the height of each module. height of deepest modules will be 1! root will be the highest. tree_height = -1 for mi, mh in enumerate(modules_hierarchy): depth = len(get_path_to_root_recursive(modules_hierarchy, mi)) mh[2] = depth if depth > tree_height: tree_height = depth # NOTE: this important hack causes leaf modules (which are real outputs of sridharan be placed on the bottom of the # dendrogram, nearest place to their members (i.e. observations) leaf_modules_idxs = [] for mi, mh in enumerate(modules_hierarchy): if len(mh[1]) == 0: leaf_modules_idxs.append(mi) for mi in leaf_modules_idxs: modules_hierarchy[mi][2] = tree_height return modules_hierarchy, mods_mets, mods_rxns, tree_height
def compute_couplings(S, mets, rxns, revs, REACT_MAX_LEN, METAB_MAX_LEN): global couplings_cache for cc in couplings_cache: if S == cc[0] and mets == cc[1] and rxns == cc[2] and revs == cc[ 3] and REACT_MAX_LEN == cc[4] and METAB_MAX_LEN == cc[5]: return cc[6], cc[7] couplings_cache.append([S, mets, rxns, revs, REACT_MAX_LEN, METAB_MAX_LEN]) f = open('cohesion_coupling.m', 'w') s_str = 'thenet.stoichiometricMatrix = zeros(%d,%d);' % (len(mets), len(rxns)) write_line(f, s_str) for i in xrange(len(S)): s_str = 'thenet.stoichiometricMatrix(%d,:) = %s;' % (i + 1, str(S[i])) write_line(f, s_str) rev_str = 'thenet.reversibilityVector = [' + mat_rows(revs) + '];' write_line(f, rev_str) react_str = 'thenet.Reactions = [' + mat_rows(rxns, REACT_MAX_LEN) + '];' write_line(f, react_str) met_str = 'thenet.Metabolites = [' + mat_rows(mets, METAB_MAX_LEN) + '];' write_line(f, met_str) # mod_path = r'addpath %s/evaluation/fcf/ffca' # func = 'FFCA' mod_path = r'addpath %s/evaluation/fcf/f2c2' func = 'F2C2' fcf_tool_path = mod_path % my_constants.basePath write_line(f, fcf_tool_path) compute_str = "[cpls, blk] = %s('clp', thenet);" % func write_line(f, compute_str) save_str = 'save cohesion_coupling.mat cpls blk' write_line(f, save_str) f.close() my_util.prepare_matlab_file_and_exec_and_wait_finish( 'cohesion_coupling', 'cohesion_coupling.mat', False) res_vars = my_util.try_load_matlab_result('cohesion_coupling.mat') # if matlab has failed, this will throw exception! couplings = res_vars['cpls'].tolist() blocks = [b[0] for b in res_vars['blk'].tolist()] couplings_cache[-1].append(couplings) couplings_cache[-1].append(blocks) return couplings, blocks
def go(species): method_dir = r'%s/method/muller2_new' % my_constants.basePath out_dir = r'%s/%s/muller2_new' % (my_constants.resultPath, species) my_util.mkdir_p(out_dir) source_file = '%s/dataset/networks/%s' % ( my_constants.basePath, my_constants.species_sbml[species]) S, mets, rxns, revs, met_names, rxn_names, biomass, met_comparts = importer.sbmlStoichiometricMatrix( source_file, True, read_species_compart=True, remove_biomass=False, normalize_stoich=False) f = open('muller2_new.m', 'w') write_line(f, 'addpath %s/code' % method_dir) write_line(f, "model = readCbModel('%s')" % source_file) write_line(f, "model.c(%d) = 1;" % (rxns.index(biomass[0]) + 1)) write_line(f, "changeCobraSolver('glpk');") write_line(f, '[modules, var, flux] = computeModulesOpt( model );') write_line(f, 'save muller2_newout.mat modules var flux') f.close() my_util.prepare_matlab_file_and_exec_and_wait_finish( 'muller2_new', 'muller2_newout.mat', False) res_vars = my_util.try_load_matlab_result('muller2_newout.mat') raw_modules = res_vars[ 'modules'] # if matlab has failed, this will throw exception! shutil.copy('muller2_newout.mat', out_dir) raw_modules = raw_modules.T.tolist( ) # eacho row will be a module where reactions are marked modules = [] for raw_module in raw_modules: modules.append([]) for rIdx, in_module in enumerate(raw_module): if in_module == 1: modules[-1].append(rxns[rIdx]) out = open('%s/final_modules.txt' % out_dir, 'w') out.write( "#each row is a module of reactions. not all reactions are specified (nature of this method only select some reaction to be modules)\n" ) for m in modules: out.write(' '.join(m)) out.write('\n') out.close()
def read_module_hierarchy_incorrect(file_path, mets, rxns): res_vars = my_util.try_load_matlab_result(file_path) raw_modules = res_vars['mods'] raw_modules_hierarchy = res_vars['mods_hier'] mods_rxns = [] mods_mets = [] for x, raw_mod in enumerate(raw_modules): raw_mod = raw_mod[0] mod_rxns_idxs = raw_mod[0][:, 0] mod_mets_idxs = raw_mod[1][:, 0] # mod_metname[0] = str(raw_mod[2][:, 0][1][0]) mod_s = raw_mod[3].toarray().tolist() # mod_rxnname[0] = str(raw_mod[4][:, 0][1][0]) mod_bmatrix = raw_mod[5].tolist() if len(raw_mod[5][0]) > 0 else None mod_q = raw_mod[6][0][0] if len(raw_mod[6][0]) > 0 else None mod_s_eig = raw_mod[7][:, 0] if len(raw_mod[7][0]) > 0 else None mod_rxns = [rxns[i - 1] for i in mod_rxns_idxs] mod_mets = [mets[i - 1] for i in mod_mets_idxs] mods_rxns.append(mod_rxns) mods_mets.append(mod_mets) raw_modules_hierarchy = convert_to_int_recursive( raw_modules_hierarchy.toarray().tolist()) modules_hierarchy = [ [None, [], -1] for i in range(len(raw_modules)) ] # map of module index to (super_index, [sub_indexes], depth). note that depth of root is 1 for ri, rm in enumerate(raw_modules_hierarchy): for di, d in enumerate(rm): if d != 0: modules_hierarchy[ri][1].append(di) modules_hierarchy[di][0] = ri # note that tree_height - depth + 1 will give the height of each module. height of deepest modules will be 1! root will be the highest. tree_height = -1 for mi, mh in enumerate(modules_hierarchy): depth = len(get_path_to_root_recursive(modules_hierarchy, mi)) mh[2] = depth if depth > tree_height: tree_height = depth return modules_hierarchy, mods_mets, mods_rxns, tree_height