def test_convert(): model = cobra.io.load_json_model(join(root_directory, 'escher', 'example_data', 'iJO1366.json')) # reverse the reaction model.reactions.get_by_id('GAPD').upper_bound = 0 model.reactions.get_by_id('GAPD').lower_bound = -1000 # map old_map = {'reactions': {'1': {'bigg_id': 'GAPD', 'label_x': '0', 'label_y': 0, 'segments': {'2': {'from_node_id': '0', 'to_node_id': '1', 'b1': {'x': None, 'y': None}, 'b2': None}}}, '2': {'bigg_id': 'PDH'}}, 'nodes': {'0': {'node_type': 'multimarker', 'x': 1, 'y': 2}, '1': {'node_type': 'metabolite', 'label_x': 10, 'label_y': 12.2, 'x': 1, 'y': 2, 'bigg_id': 'g3p_c', 'name': 'glycerol-3-phosphate'}, '3': {'node_type': 'metabolite', 'x': 1, 'y': 2, 'bigg_id': 'glc__D_c', 'name': 'D-Glucose'}}} new_map = convert(old_map, model) print(new_map) # no segments: delete reaction assert '2' not in new_map[1]['reactions'] # reversed the map for m in new_map[1]['reactions']['1']['metabolites']: if m['bigg_id'] == 'g3p_c': assert m['coefficient'] == 1 elif m['bigg_id'] == 'nadh_c': assert m['coefficient'] == -1 assert new_map[1]['reactions']['1']['reversibility'] == False # Remove unconnected nodes. These do not make the map invalid, but they are # annoying. assert '3' not in new_map[1]['nodes'] # casting assert type(new_map[1]['reactions']['1']['label_x']) is float assert new_map[1]['reactions']['1']['segments']['2']['b1'] is None
def main(): # go though the maps for filepath, output_path, save_model_dir in get_map_dirs(map_paths): filename = basename(filepath) logging.info("Converting %s" % filename) # # debug # if 'iAF692' not in filename: # continue # get the model id model_id = parse_model(filename)[0] # check id mapping try: model_id = model_id_mapping[model_id] except KeyError: pass # load the model try: model = load_bigg_model(model_id) except NotFoundError as e: print(filepath, e) continue # save the bigg model if save_model_dir: save_model(save_model_dir, model, model_id) # get the id mapping dictionaries. Eventually, this should be available # in the API. reaction_id_mapping = get_reaction_mapping(model.id) metabolite_id_mapping = get_metabolite_mapping(model.id) gene_id_mapping = None # get_gene_mapping(model_id) # convert the map with open(filepath, "r") as f: map_in = json.load(f) map_out = convert( map_in, model, map_name=parse_map_name(fix_filename(filename, model_id)), reaction_id_mapping=reaction_id_mapping, metabolite_id_mapping=metabolite_id_mapping, gene_id_mapping=gene_id_mapping, ) # write with open(join(output_path, fix_filename(filename, model_id)), "w") as f: json.dump(map_out, f)
def main(): # go though the maps for filepath, output_path, save_model_dir in get_map_dirs(map_paths): filename = basename(filepath) logging.info('Converting %s' % filename) # # debug # if 'iAF692' not in filename: # continue # get the model id model_id = parse_model(filename)[0] # check id mapping try: model_id = model_id_mapping[model_id] except KeyError: pass # load the model try: model = load_bigg_model(model_id) except NotFoundError as e: print(filepath, e) continue # save the bigg model if save_model_dir: save_model(save_model_dir, model, model_id) # get the id mapping dictionaries. Eventually, this should be available # in the API. reaction_id_mapping = get_reaction_mapping(model.id) metabolite_id_mapping = get_metabolite_mapping(model.id) gene_id_mapping = None # get_gene_mapping(model_id) # convert the map with open(filepath, 'r') as f: map_in = json.load(f) map_out = convert(map_in, model, map_name=parse_map_name( fix_filename(filename, model_id)), reaction_id_mapping=reaction_id_mapping, metabolite_id_mapping=metabolite_id_mapping, gene_id_mapping=gene_id_mapping) # write with open(join(output_path, fix_filename(filename, model_id)), 'w') as f: json.dump(map_out, f)
def save_map(filename, out_directory, model_name): if filename.endswith('.json.gz'): with gzip.open(filename, "r") as f: data = json.load(f) out_file = join(out_directory, basename(filename).replace('.json.gz', '_map.json')) elif filename.endswith('.json'): with open(filename, "r") as f: data = json.load(f) out_file = join(out_directory, basename(filename).replace('.json', '_map.json')) else: logging.warn('Not loading file %s' % filename) # get the cobra model model = load_model(model_name) # get the compartment dictionary df = pd.DataFrame.from_csv("compartment_id_key.csv") compartment_id_dictionary = {} for row in df.itertuples(index=True): compartment_id_dictionary[row[0]] = row[1:3] # major categories reactions = [] line_segments = [] text_labels = [] nodes = [] for k, v in data.iteritems(): if k == "MAPREACTION": reactions = v elif k == "MAPLINESEGMENT": segments = v elif k == "MAPTEXT": text_labels = v elif k == "MAPNODE": nodes = v else: raise Exception('Unrecognized category: %s' % k) # do the nodes nodes = parse_nodes(nodes, compartment_id_dictionary) # do the segments parse_segments(segments, reactions, nodes) # do the reactions reactions = parse_reactions(reactions, model, nodes) # do the text labels text_labels = parse_labels(text_labels) # compile the data out = {} out['nodes'] = nodes out['reactions'] = reactions out['text_labels'] = text_labels # translate everything so x > 0 and y > 0 # out = translate_everything(out) # for export, only keep the necessary stuff node_keys_to_keep = [ 'node_type', 'x', 'y', 'name', 'bigg_id', 'label_x', 'label_y', 'node_is_primary', 'connected_segments' ] segment_keys_to_keep = ['from_node_id', 'to_node_id', 'b1', 'b2'] reaction_keys_to_keep = [ 'segments', 'name', 'reversibility', 'bigg_id', 'label_x', 'label_y', 'metabolites', 'gene_reaction_rule' ] text_label_keys_to_keep = ['x', 'y', 'text'] for k, node in out['nodes'].iteritems(): only_keep_keys(node, node_keys_to_keep) for k, reaction in out['reactions'].iteritems(): if 'segments' not in reaction: continue for k, segment in reaction['segments'].iteritems(): only_keep_keys(segment, segment_keys_to_keep) only_keep_keys(reaction, reaction_keys_to_keep) for k, text_label in out['text_labels'].iteritems(): only_keep_keys(text_label, text_label_keys_to_keep) # get max width and height min_max = {'x': [inf, -inf], 'y': [inf, -inf]} for node in nodes.itervalues(): if node['x'] < min_max['x'][0]: min_max['x'][0] = node['x'] if node['x'] > min_max['x'][1]: min_max['x'][1] = node['x'] if node['y'] < min_max['y'][0]: min_max['y'][0] = node['y'] if node['y'] > min_max['y'][1]: min_max['y'][1] = node['y'] width = min_max['x'][1] - min_max['x'][0] height = min_max['y'][1] - min_max['y'][0] out['canvas'] = { 'x': min_max['x'][0] - 0.05 * width, 'y': min_max['y'][0] - 0.05 * height, 'width': width + 0.10 * width, 'height': height + 0.10 * height } header = { "schema": "https://zakandrewking.github.io/escher/escher/jsonschema/1-0-0#", "homepage": "https://zakandrewking.github.io/escher", "map_id": basename(filename).replace('.json', '').replace('.gz', ''), "map_name": "", "map_description": "" } the_map = [header, out] from escher.convert_map import convert the_map = convert(the_map, model) with open(out_file, 'w') as f: json.dump(the_map, f, allow_nan=False)
model.genes.get_by_id(old_id).remove_from_model() # update the gene names with the old names for g in model.genes: g.name = g.id model.id = model_id cobra.io.save_json_model(model, model_path) print 'Found model %s %s' % (the_organism, model_path) break if the_organism is None: print 'Could not find model' exit() # apply model to maps map_org = join('maps', the_organism) for map_file in listdir(map_org): if map_file.startswith('.'): continue map_path = join(map_org, map_file) print 'Writing map %s' % map_path with open(map_path, 'r') as f: map = json.load(f) for r in map[1]['reactions'].itervalues(): r['gene_reaction_rule'] = re.sub('\.[0-9]+', '', r['gene_reaction_rule']) for g in r['genes']: g['bigg_id'] = g['bigg_id'].split('.')[0] new_map = convert(map, model) # TODO the gene names are not being fixed here # import ipdb; ipdb.set_trace() with open(map_path, 'w') as f: json.dump(new_map, f)
def save_map(filename, out_directory, model_name): if filename.endswith('.json.gz'): with gzip.open(filename, "r") as f: data = json.load(f) out_file = join(out_directory, basename(filename).replace('.json.gz', '_map.json')) elif filename.endswith('.json'): with open(filename, "r") as f: data = json.load(f) out_file = join(out_directory, basename(filename).replace('.json', '_map.json')) else: logging.warn('Not loading file %s' % filename) # get the cobra model model = load_model(model_name) # get the compartment dictionary df = pd.DataFrame.from_csv("compartment_id_key.csv") compartment_id_dictionary = {} for row in df.itertuples(index=True): compartment_id_dictionary[row[0]] = row[1:3] # major categories reactions = []; line_segments = []; text_labels = []; nodes = [] for k, v in data.iteritems(): if k=="MAPREACTION": reactions = v elif k=="MAPLINESEGMENT": segments = v elif k=="MAPTEXT": text_labels = v elif k=="MAPNODE": nodes = v else: raise Exception('Unrecognized category: %s' % k) # do the nodes nodes = parse_nodes(nodes, compartment_id_dictionary) # do the segments parse_segments(segments, reactions, nodes) # do the reactions reactions = parse_reactions(reactions, model, nodes) # do the text labels text_labels = parse_labels(text_labels) # compile the data out = {} out['nodes'] = nodes out['reactions'] = reactions out['text_labels'] = text_labels # translate everything so x > 0 and y > 0 # out = translate_everything(out) # for export, only keep the necessary stuff node_keys_to_keep = ['node_type', 'x', 'y', 'name', 'bigg_id', 'label_x', 'label_y', 'node_is_primary', 'connected_segments'] segment_keys_to_keep = ['from_node_id', 'to_node_id', 'b1', 'b2'] reaction_keys_to_keep = ['segments', 'name', 'reversibility', 'bigg_id', 'label_x', 'label_y', 'metabolites', 'gene_reaction_rule'] text_label_keys_to_keep = ['x', 'y', 'text'] for k, node in out['nodes'].iteritems(): only_keep_keys(node, node_keys_to_keep) for k, reaction in out['reactions'].iteritems(): if 'segments' not in reaction: continue for k, segment in reaction['segments'].iteritems(): only_keep_keys(segment, segment_keys_to_keep) only_keep_keys(reaction, reaction_keys_to_keep) for k, text_label in out['text_labels'].iteritems(): only_keep_keys(text_label, text_label_keys_to_keep) # get max width and height min_max = {'x': [inf, -inf], 'y': [inf, -inf]} for node in nodes.itervalues(): if node['x'] < min_max['x'][0]: min_max['x'][0] = node['x'] if node['x'] > min_max['x'][1]: min_max['x'][1] = node['x'] if node['y'] < min_max['y'][0]: min_max['y'][0] = node['y'] if node['y'] > min_max['y'][1]: min_max['y'][1] = node['y'] width = min_max['x'][1] - min_max['x'][0] height = min_max['y'][1] - min_max['y'][0] out['canvas'] = { 'x': min_max['x'][0] - 0.05 * width, 'y': min_max['y'][0] - 0.05 * height, 'width': width + 0.10 * width, 'height': height + 0.10 * height} header = { "schema": "https://zakandrewking.github.io/escher/escher/jsonschema/1-0-0#", "homepage": "https://zakandrewking.github.io/escher", "map_id": basename(filename).replace('.json', '').replace('.gz', ''), "map_name": "", "map_description": "" } the_map = [header, out] from escher.convert_map import convert the_map = convert(the_map, model) with open(out_file, 'w') as f: json.dump(the_map, f, allow_nan=False)