From dcf17362ccf67aa8d09c722b64d00305cc07236e Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Mon, 5 Jun 2023 12:03:58 -0400 Subject: [PATCH 01/12] move json http request functions to utilities --- xgi/utils/utilities.py | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/xgi/utils/utilities.py b/xgi/utils/utilities.py index 3d972d3cf..47c007945 100644 --- a/xgi/utils/utilities.py +++ b/xgi/utils/utilities.py @@ -1,8 +1,10 @@ """General utilities.""" from collections import defaultdict +from functools import lru_cache from itertools import chain, combinations, count +import requests from numpy import infty from xgi.exception import IDNotFound, XGIError @@ -13,6 +15,8 @@ "powerset", "update_uid_counter", "find_triangles", + "request_json_from_url", + "request_json_from_url_cached", ] @@ -210,3 +214,64 @@ def min_where(dicty, where): else: pass return min_val + + +def request_json_from_url(url): + """HTTP request json file and return as dict. + + Parameters + ---------- + url : str + The url where the json file is located. + + Returns + ------- + dict + A dictionary of the JSON requested. + + Raises + ------ + XGIError + If the connection fails or if there is a bad HTTP request. + """ + + try: + r = requests.get(url) + except requests.ConnectionError: + raise XGIError("Connection Error!") + + if r.ok: + return r.json() + else: + raise XGIError(f"Error: HTTP response {r.status_code}") + + +@lru_cache(maxsize=None) +def request_json_from_url_cached(url): + """HTTP request json file and return as dict. + + Parameters + ---------- + url : str + The url where the json file is located. + + Returns + ------- + dict + A dictionary of the JSON requested. + + Raises + ------ + XGIError + If the connection fails or if there is a bad HTTP request. + """ + + try: + r = requests.get(url) + except requests.ConnectionError: + raise XGIError("Connection Error!") + + if r.ok: + return r.json() + else: + raise XGIError(f"Error: HTTP response {r.status_code}") From d1259154ef63ada8809dc189d16ecf77c9e919ac Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Mon, 5 Jun 2023 12:07:49 -0400 Subject: [PATCH 02/12] updated xgi_data module to reflect moving the json functions to utilities. --- xgi/readwrite/xgi_data.py | 73 +++++---------------------------------- 1 file changed, 8 insertions(+), 65 deletions(-) diff --git a/xgi/readwrite/xgi_data.py b/xgi/readwrite/xgi_data.py index e965df3ac..63a440e05 100644 --- a/xgi/readwrite/xgi_data.py +++ b/xgi/readwrite/xgi_data.py @@ -1,13 +1,11 @@ """Load a data set from the xgi-data repository or a local file.""" import json import os -from functools import lru_cache from warnings import warn -import requests - from .. import convert from ..exception import XGIError +from ..utils import request_json_from_url, request_json_from_url_cached __all__ = ["load_xgi_data", "download_xgi_data"] @@ -57,7 +55,7 @@ def load_xgi_data( # If no dataset is specified, print a list of the available datasets. if dataset is None: index_url = "https://gitlab.com/complexgroupinteractions/xgi-data/-/raw/main/index.json?inline=false" - index_data = _request_json_from_url(index_url) + index_data = request_json_from_url(index_url) print("Available datasets are the following:") print(*index_data, sep="\n") return @@ -76,10 +74,7 @@ def load_xgi_data( "from the xgi-data repository instead. To download a local " "copy, use `download_xgi_data`." ) - if cache: - data = _request_from_xgi_data_cached(dataset) - else: - data = _request_from_xgi_data(dataset) + data = _request_from_xgi_data(dataset, cache=cache) return convert.dict_to_hypergraph( data, nodetype=nodetype, edgetype=edgetype, max_order=max_order @@ -106,7 +101,7 @@ def download_xgi_data(dataset, path=""): jsonfile.close() -def _request_from_xgi_data(dataset=None): +def _request_from_xgi_data(dataset=None, cache=True): """Request a dataset from xgi-data. Parameters @@ -132,7 +127,7 @@ def _request_from_xgi_data(dataset=None): """ index_url = "https://gitlab.com/complexgroupinteractions/xgi-data/-/raw/main/index.json?inline=false" - index_data = _request_json_from_url(index_url) + index_data = request_json_from_url(index_url) key = dataset.lower() if key not in index_data: @@ -140,59 +135,7 @@ def _request_from_xgi_data(dataset=None): print(*index_data, sep="\n") raise XGIError("Must choose a valid dataset name!") - return _request_json_from_url(index_data[key]["url"]) - - -@lru_cache(maxsize=None) -def _request_from_xgi_data_cached(dataset): - """Request a dataset from xgi-data and cache the result. - - Wraps `_request_from_xgi_data` in an lru_cache decorator. - - Parameters - ---------- - dataset : str - Dataset name. Valid options are the top-level tags of the - index.json file in the xgi-data repository. - - Returns - ------- - Data - The requested data loaded from a json file. - - See also - --------- - load_xgi_data - """ - - return _request_from_xgi_data(dataset) - - -def _request_json_from_url(url): - """HTTP request json file and return as dict. - - Parameters - ---------- - url : str - The url where the json file is located. - - Returns - ------- - dict - A dictionary of the JSON requested. - - Raises - ------ - XGIError - If the connection fails or if there is a bad HTTP request. - """ - - try: - r = requests.get(url) - except requests.ConnectionError: - raise XGIError("Connection Error!") - - if r.ok: - return r.json() + if cache: + return request_json_from_url_cached(index_data[key]["url"]) else: - raise XGIError(f"Error: HTTP response {r.status_code}") + return request_json_from_url(index_data[key]["url"]) From 7db7f6c8d699f99347709c71a1be9704b5ef807a Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Mon, 5 Jun 2023 12:08:08 -0400 Subject: [PATCH 03/12] added bigg_data module. --- xgi/readwrite/__init__.py | 3 +- xgi/readwrite/bigg_data.py | 97 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 xgi/readwrite/bigg_data.py diff --git a/xgi/readwrite/__init__.py b/xgi/readwrite/__init__.py index a6a07f1e7..c334dff5b 100644 --- a/xgi/readwrite/__init__.py +++ b/xgi/readwrite/__init__.py @@ -1,6 +1,7 @@ -from . import bipartite, edgelist, incidence, json, xgi_data +from . import bigg_data, bipartite, edgelist, incidence, json, xgi_data from .bipartite import * from .edgelist import * from .incidence import * from .json import * from .xgi_data import * +from .bigg_data import * diff --git a/xgi/readwrite/bigg_data.py b/xgi/readwrite/bigg_data.py new file mode 100644 index 000000000..53cedc419 --- /dev/null +++ b/xgi/readwrite/bigg_data.py @@ -0,0 +1,97 @@ +"""Load a data set from the xgi-data repository or a local file.""" + +from functools import lru_cache + +import requests + +from ..exception import XGIError +from ..utils import request_json_from_url, request_json_from_url_cached + +__all__ = ["load_bigg_data"] + + +def load_bigg_data( + dataset=None, + cache=True, +): + """Load a data set from the xgi-data repository or a local file. + + Parameters + ---------- + dataset : str, default: None + Dataset name. Valid options are the top-level tags of the + index.json file in the xgi-data repository. If None, prints + the list of available datasets. + cache : bool, optional + Whether to cache the input data + nodetype : type, optional + Type to cast the node ID to + edgetype : type, optional + Type to cast the edge ID to + max_order: int, optional + Maximum order of edges to add to the hypergraph + + Returns + ------- + DiHypergraph + The loaded dihypergraph. + + Raises + ------ + XGIError + The specified dataset does not exist. + """ + + indexurl = "http://bigg.ucsd.edu/api/v2/models" + baseurl = "http://bigg.ucsd.edu/static/models/" + + # If no dataset is specified, print a list of the available datasets. + if dataset is None: + index_data = request_json_from_url(indexurl) + ids = [] + for entry in index_data["results"]: + ids.append(entry["bigg_id"]) + print("Available datasets are the following:") + print(*ids, sep="\n") + return + + if cache: + data = request_json_from_url_cached(baseurl + dataset + ".json") + else: + data = request_json_from_url(baseurl + dataset + ".json") + + return _bigg_to_dihypergraph(data) + + +def _bigg_to_dihypergraph(d): + """Convert a BIGG-formatted dict to dihypergraph. + + Parameters + ---------- + d : dict + A BIGG-formatted dict + + Returns + ------- + DiHypergraph + The dihypergraph from the selected BIGG model. + """ + from .. import DiHypergraph + + DH = DiHypergraph() + + for m in d["metabolites"]: + DH.add_node(m["id"], name=m["name"]) + + for r in d["reactions"]: + head = set() + tail = set() + for m, val in r["metabolites"].items(): + if val > 0: + head.add(m) + else: + tail.add(m) + + DH.add_edge((tail, head), id=r["id"]) + + return DH From cb1ff061ac13e0c08eaf551f12c76914d1f56a17 Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Mon, 5 Jun 2023 12:08:16 -0400 Subject: [PATCH 04/12] style: format with black --- xgi/algorithms/centrality.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xgi/algorithms/centrality.py b/xgi/algorithms/centrality.py index ccc18e287..572f425c9 100644 --- a/xgi/algorithms/centrality.py +++ b/xgi/algorithms/centrality.py @@ -353,11 +353,11 @@ def katz_centrality(H, index=False, cutoff=100): .. math:: c = [(I - \alpha A^{t})^{-1} - I]{\bf 1}, - + where :math:`A` is the adjency matrix of the the (hyper)graph. Since :math:`A^{t} = A` for undirected graphs (our case), we have: - + .. math:: &[I + A + \alpha A^2 + \alpha^2 A^3 + \dots](I - \alpha A^{t}) @@ -368,7 +368,7 @@ def katz_centrality(H, index=False, cutoff=100): & - \alpha^2 A^3 - \alpha^3 A^4 - \dots & = I - + And :math:`(I - \alpha A^{t})^{-1} = I + A + \alpha A^2 + \alpha^2 A^3 + \dots` Thus we can use the power serie to compute the Katz-centrality. [2] The Katz-centrality of isolated nodes (no hyperedges contains them) is From 610e608ae98ea951d5ae469126c8b71ad4e430ac Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Mon, 5 Jun 2023 13:52:02 -0400 Subject: [PATCH 05/12] update docstrings --- xgi/readwrite/bigg_data.py | 42 ++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/xgi/readwrite/bigg_data.py b/xgi/readwrite/bigg_data.py index 53cedc419..b43e21970 100644 --- a/xgi/readwrite/bigg_data.py +++ b/xgi/readwrite/bigg_data.py @@ -1,10 +1,5 @@ -"""Load a data set from the xgi-data repository or a local file.""" +"""Load a metabolic network from the BiGG models database.""" -from functools import lru_cache - -import requests - -from ..exception import XGIError from ..utils import request_json_from_url, request_json_from_url_cached __all__ = ["load_bigg_data"] @@ -14,22 +9,20 @@ def load_bigg_data( dataset=None, cache=True, ): - """Load a data set from the xgi-data repository or a local file. + """Load a metabolic network from the BiGG models database. + + The Biochemical, Genetic and Genomic (BiGG) knowledge base + is hosted at http://bigg.ucsd.edu/. It contains metabolic + reaction networks at the genome scale. Parameters ---------- dataset : str, default: None - Dataset name. Valid options are the top-level tags of the - index.json file in the xgi-data repository. If None, prints + Dataset name. Valid options are the "bigg_id" tags in + http://bigg.ucsd.edu/api/v2/models. If None, prints the list of available datasets. cache : bool, optional Whether to cache the input data - nodetype : type, optional - Type to cast the node ID to - edgetype : type, optional - Type to cast the edge ID to - max_order: int, optional - Maximum order of edges to add to the hypergraph Returns ------- @@ -40,14 +33,23 @@ def load_bigg_data( ------ XGIError The specified dataset does not exist. + + References + ---------- + Zachary A. King, Justin Lu, Andreas Dräger, + Philip Miller, Stephen Federowicz, Joshua A. Lerman, + Ali Ebrahim, Bernhard O. Palsson, Nathan E. Lewis + Nucleic Acids Research, Volume 44, Issue D1, + 4 January 2016, Pages D515–D522, + https://doi.org/10.1093/nar/gkv1049 """ - indexurl = "http://bigg.ucsd.edu/api/v2/models" - baseurl = "http://bigg.ucsd.edu/static/models/" + index_url = "http://bigg.ucsd.edu/api/v2/models" + base_url = "http://bigg.ucsd.edu/static/models/" # If no dataset is specified, print a list of the available datasets. if dataset is None: - index_data = request_json_from_url(indexurl) + index_data = request_json_from_url(index_url) ids = [] for entry in index_data["results"]: ids.append(entry["bigg_id"]) @@ -56,9 +58,9 @@ def load_bigg_data( return if cache: - data = request_json_from_url_cached(baseurl + dataset + ".json") + data = request_json_from_url_cached(base_url + dataset + ".json") else: - data = request_json_from_url(baseurl + dataset + ".json") + data = request_json_from_url(base_url + dataset + ".json") return _bigg_to_dihypergraph(data) From 34977097b59d00272c04e8f543b239533060c6cc Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Mon, 5 Jun 2023 13:52:12 -0400 Subject: [PATCH 06/12] update xgi_data module --- xgi/readwrite/xgi_data.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/xgi/readwrite/xgi_data.py b/xgi/readwrite/xgi_data.py index 63a440e05..86eceeb16 100644 --- a/xgi/readwrite/xgi_data.py +++ b/xgi/readwrite/xgi_data.py @@ -23,23 +23,24 @@ def load_xgi_data( Parameters ---------- - dataset : str, default: None + dataset : str, optional Dataset name. Valid options are the top-level tags of the - index.json file in the xgi-data repository. If None, prints + index.json file in the xgi-data repository. If None (default), prints the list of available datasets. cache : bool, optional - Whether to cache the input data + Whether to cache the input data, by default True. read : bool, optional If read==True, search for a local copy of the data set. Use the local - copy if it exists, otherwise use the xgi-data repository. + copy if it exists, otherwise use the xgi-data repository. + By default, False. path : str, optional Path to a local copy of the data set nodetype : type, optional - Type to cast the node ID to + Type to cast the node ID to, by default None. edgetype : type, optional - Type to cast the edge ID to + Type to cast the edge ID to, by default None. max_order: int, optional - Maximum order of edges to add to the hypergraph + Maximum order of edges to add to the hypergraph, by default None. Returns ------- @@ -51,10 +52,14 @@ def load_xgi_data( XGIError The specified dataset does not exist. """ + index_url = ( + "https://gitlab.com/complexgroupinteractions/" + "xgi-data/-/raw/main/index.json?inline=false" + ) # If no dataset is specified, print a list of the available datasets. if dataset is None: - index_url = "https://gitlab.com/complexgroupinteractions/xgi-data/-/raw/main/index.json?inline=false" + index_data = request_json_from_url(index_url) print("Available datasets are the following:") print(*index_data, sep="\n") @@ -106,10 +111,12 @@ def _request_from_xgi_data(dataset=None, cache=True): Parameters ---------- - dataset : str, default: None + dataset : str, optional Dataset name. Valid options are the top-level tags of the index.json file in the xgi-data repository. If None, prints the list of available datasets. + cache : bool, optional + Whether or not to cache the output Returns ------- @@ -125,8 +132,11 @@ def _request_from_xgi_data(dataset=None, cache=True): --------- load_xgi_data """ + index_url = ( + "https://gitlab.com/complexgroupinteractions/" + "xgi-data/-/raw/main/index.json?inline=false" + ) - index_url = "https://gitlab.com/complexgroupinteractions/xgi-data/-/raw/main/index.json?inline=false" index_data = request_json_from_url(index_url) key = dataset.lower() From e045ecf0e478b331b841a9547f58e98aa8899f20 Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Mon, 5 Jun 2023 13:52:22 -0400 Subject: [PATCH 07/12] remove unnecessary imports --- xgi/stats/dinodestats.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/xgi/stats/dinodestats.py b/xgi/stats/dinodestats.py index 9e8adb566..d021ffe53 100644 --- a/xgi/stats/dinodestats.py +++ b/xgi/stats/dinodestats.py @@ -18,10 +18,6 @@ """ -import numpy as np - -import xgi - __all__ = [ "attrs", "degree", From cd7203a40a5bd73d26563bee357d529cbec90b2d Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Mon, 5 Jun 2023 16:50:15 -0400 Subject: [PATCH 08/12] added tests --- tests/readwrite/test_bigg_data.py | 27 +++++++++++++++++++++++++++ tests/readwrite/test_xgi_data.py | 8 +++++++- xgi/readwrite/bigg_data.py | 4 +++- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/readwrite/test_bigg_data.py diff --git a/tests/readwrite/test_bigg_data.py b/tests/readwrite/test_bigg_data.py new file mode 100644 index 000000000..beedf0e51 --- /dev/null +++ b/tests/readwrite/test_bigg_data.py @@ -0,0 +1,27 @@ +import pytest + +from xgi import load_bigg_data +from xgi.exception import XGIError + + +@pytest.mark.webtest +@pytest.mark.slow +def test_load_bigg_data(capfd): + # test loading the online data + H1 = load_bigg_data("iAF1260", cache=False) + assert H1.num_nodes == 1668 + assert H1.num_edges == 2382 + assert H1["name"] == "iAF1260" + assert H1.nodes["2agpg161_c"] == {'name': '2-Acyl-sn-glycero-3-phosphoglycerol (n-C16:1)'} + + H2 = load_bigg_data("iAF1260", cache=True) + assert H1.nodes == H2.nodes + assert H1.edges == H2.edges + + load_bigg_data() + out, _ = capfd.readouterr() + assert "Available datasets are the following:" in out + assert "iAF1260" in out + + with pytest.raises(XGIError): + load_bigg_data("test") \ No newline at end of file diff --git a/tests/readwrite/test_xgi_data.py b/tests/readwrite/test_xgi_data.py index d8a2746bd..6e91c719d 100644 --- a/tests/readwrite/test_xgi_data.py +++ b/tests/readwrite/test_xgi_data.py @@ -10,7 +10,7 @@ @pytest.mark.webtest @pytest.mark.slow -def test_load_xgi_data(): +def test_load_xgi_data(capfd): # test loading the online data H1 = load_xgi_data("email-enron", cache=False) assert H1.num_nodes == 148 @@ -42,6 +42,12 @@ def test_load_xgi_data(): H4 = load_xgi_data("email-enron", read=True, path=dir) assert H1.edges.members() == H4.edges.members() + load_xgi_data() + out, _ = capfd.readouterr() + assert "Available datasets are the following:" in out + assert "email-enron" in out + assert "congress-bills" in out + def test_download_xgi_data(): dir = tempfile.mkdtemp() diff --git a/xgi/readwrite/bigg_data.py b/xgi/readwrite/bigg_data.py index b43e21970..63ad8afdf 100644 --- a/xgi/readwrite/bigg_data.py +++ b/xgi/readwrite/bigg_data.py @@ -82,6 +82,8 @@ def _bigg_to_dihypergraph(d): DH = DiHypergraph() + DH["name"] = d["id"] + for m in d["metabolites"]: DH.add_node(m["id"], name=m["name"]) @@ -94,6 +96,6 @@ def _bigg_to_dihypergraph(d): else: tail.add(m) - DH.add_edge((tail, head), id=r["id"]) + DH.add_edge((tail, head), id=r["id"], name=r["name"]) return DH From 7e064da66b990c3a47e1a7dddc75fbb187b9e1f5 Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Mon, 5 Jun 2023 17:34:03 -0400 Subject: [PATCH 09/12] added docs --- xgi/readwrite/bigg_data.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/xgi/readwrite/bigg_data.py b/xgi/readwrite/bigg_data.py index 63ad8afdf..0a4deb5da 100644 --- a/xgi/readwrite/bigg_data.py +++ b/xgi/readwrite/bigg_data.py @@ -15,6 +15,10 @@ def load_bigg_data( is hosted at http://bigg.ucsd.edu/. It contains metabolic reaction networks at the genome scale. + We represent metabolites as nodes and metabolic reactions + as directed edges where reactants are the tail of the directed + edge and the products are the head of the directed edge. + Parameters ---------- dataset : str, default: None From 771cc7abdf532ff7864054dc0732f7d1a7665cbe Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Mon, 5 Jun 2023 18:09:17 -0400 Subject: [PATCH 10/12] Update bigg_data.py --- xgi/readwrite/bigg_data.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/xgi/readwrite/bigg_data.py b/xgi/readwrite/bigg_data.py index 0a4deb5da..2d26ce477 100644 --- a/xgi/readwrite/bigg_data.py +++ b/xgi/readwrite/bigg_data.py @@ -51,9 +51,10 @@ def load_bigg_data( index_url = "http://bigg.ucsd.edu/api/v2/models" base_url = "http://bigg.ucsd.edu/static/models/" + index_data = request_json_from_url(index_url) + # If no dataset is specified, print a list of the available datasets. if dataset is None: - index_data = request_json_from_url(index_url) ids = [] for entry in index_data["results"]: ids.append(entry["bigg_id"]) @@ -62,14 +63,14 @@ def load_bigg_data( return if cache: - data = request_json_from_url_cached(base_url + dataset + ".json") + model_data = request_json_from_url_cached(base_url + dataset + ".json") else: - data = request_json_from_url(base_url + dataset + ".json") + model_data = request_json_from_url(base_url + dataset + ".json") - return _bigg_to_dihypergraph(data) + return _bigg_to_dihypergraph(index_data, model_data) -def _bigg_to_dihypergraph(d): +def _bigg_to_dihypergraph(d_index, d_model): """Convert a BIGG-formatted dict to dihypergraph. Parameters @@ -86,12 +87,19 @@ def _bigg_to_dihypergraph(d): DH = DiHypergraph() - DH["name"] = d["id"] + id = d_model["id"] + + DH["name"] = id + + for d in d_index["results"]: + if d["bigg_id"] == id: + DH["organism"] = d["organism"] + break - for m in d["metabolites"]: + for m in d_model["metabolites"]: DH.add_node(m["id"], name=m["name"]) - for r in d["reactions"]: + for r in d_model["reactions"]: head = set() tail = set() for m, val in r["metabolites"].items(): From 004c1998875c2e1667821b12172775a6b1289e2e Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Thu, 8 Jun 2023 07:58:09 -0400 Subject: [PATCH 11/12] response to review --- tests/readwrite/test_bigg_data.py | 7 +++++-- xgi/readwrite/bigg_data.py | 8 ++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/readwrite/test_bigg_data.py b/tests/readwrite/test_bigg_data.py index beedf0e51..a64b5559c 100644 --- a/tests/readwrite/test_bigg_data.py +++ b/tests/readwrite/test_bigg_data.py @@ -12,7 +12,10 @@ def test_load_bigg_data(capfd): assert H1.num_nodes == 1668 assert H1.num_edges == 2382 assert H1["name"] == "iAF1260" - assert H1.nodes["2agpg161_c"] == {'name': '2-Acyl-sn-glycero-3-phosphoglycerol (n-C16:1)'} + assert H1["organism"] == "Escherichia coli str. K-12 substr. MG1655" + assert H1.nodes["2agpg161_c"] == { + "name": "2-Acyl-sn-glycero-3-phosphoglycerol (n-C16:1)" + } H2 = load_bigg_data("iAF1260", cache=True) assert H1.nodes == H2.nodes @@ -24,4 +27,4 @@ def test_load_bigg_data(capfd): assert "iAF1260" in out with pytest.raises(XGIError): - load_bigg_data("test") \ No newline at end of file + load_bigg_data("test") diff --git a/xgi/readwrite/bigg_data.py b/xgi/readwrite/bigg_data.py index 2d26ce477..189c2df71 100644 --- a/xgi/readwrite/bigg_data.py +++ b/xgi/readwrite/bigg_data.py @@ -90,12 +90,8 @@ def _bigg_to_dihypergraph(d_index, d_model): id = d_model["id"] DH["name"] = id - - for d in d_index["results"]: - if d["bigg_id"] == id: - DH["organism"] = d["organism"] - break - + info = next((item for item in d_index["results"] if item["bigg_id"] == id), None) + DH["organism"] = info["organism"] for m in d_model["metabolites"]: DH.add_node(m["id"], name=m["name"]) From bd3c1dacd2fe05436cd31502f1d2719d8f12f6d3 Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Thu, 8 Jun 2023 08:00:49 -0400 Subject: [PATCH 12/12] added spaces --- xgi/readwrite/bigg_data.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xgi/readwrite/bigg_data.py b/xgi/readwrite/bigg_data.py index 189c2df71..b613cf547 100644 --- a/xgi/readwrite/bigg_data.py +++ b/xgi/readwrite/bigg_data.py @@ -90,8 +90,10 @@ def _bigg_to_dihypergraph(d_index, d_model): id = d_model["id"] DH["name"] = id + info = next((item for item in d_index["results"] if item["bigg_id"] == id), None) DH["organism"] = info["organism"] + for m in d_model["metabolites"]: DH.add_node(m["id"], name=m["name"])