diff --git a/release-notes/next-release.md b/release-notes/next-release.md index a38777b5a..8bb9bbe10 100644 --- a/release-notes/next-release.md +++ b/release-notes/next-release.md @@ -2,6 +2,8 @@ ## New features +* View number of genes in model notebook representation. + ## Fixes * serializes GPRs to strings to avoid massive storage usage diff --git a/src/cobra/core/dictlist.py b/src/cobra/core/dictlist.py index dad2c71b4..32d45ea85 100644 --- a/src/cobra/core/dictlist.py +++ b/src/cobra/core/dictlist.py @@ -16,6 +16,7 @@ ) import numpy as np +import pandas as pd from .object import Object @@ -542,3 +543,28 @@ def __dir__(self) -> list: attributes.append("_dict") attributes.extend(self._dict.keys()) return attributes + + def to_df(self): + """Convert to a pandas dataframe.""" + item = None + columns = [] + + if self: + item = self[0] + columns = [col for col in item._DF_ATTRS if col != "id"] + + data = [] + ids = [] + + for item in self: + ids.append(item.id) + data.append([getattr(item, attr) for attr in columns]) + + df = pd.DataFrame(columns=columns, data=data, index=ids) + + return df + + def _repr_html_(self): + """Display as HTML.""" + df = self.to_df() + return df._repr_html_() diff --git a/src/cobra/core/formula.py b/src/cobra/core/formula.py index 8aeab23bf..fbbb33d58 100644 --- a/src/cobra/core/formula.py +++ b/src/cobra/core/formula.py @@ -22,6 +22,8 @@ class Formula(Object): A legal formula string contains only letters and numbers. """ + _DF_ATTRS = ["formula"] + def __init__(self, formula: Optional[str] = None, **kwargs) -> None: """Initialize a formula. diff --git a/src/cobra/core/gene.py b/src/cobra/core/gene.py index 4d8a13576..e6e1a1543 100644 --- a/src/cobra/core/gene.py +++ b/src/cobra/core/gene.py @@ -204,6 +204,8 @@ class Gene(Species): used. """ + _DF_ATTRS = Species._DF_ATTRS + # noinspection PyShadowingBuiltins def __init__(self, id: str = None, name: str = "", functional: bool = True) -> None: """Initialize a gene. diff --git a/src/cobra/core/group.py b/src/cobra/core/group.py index ac2e508b3..39651ff49 100644 --- a/src/cobra/core/group.py +++ b/src/cobra/core/group.py @@ -38,6 +38,8 @@ class Group(Object): or member is involved in a disease phenotype). """ + _DF_ATTRS = ["id", "name", "kind"] + KIND_TYPES = ("collection", "classification", "partonomy") def __init__( diff --git a/src/cobra/core/metabolite.py b/src/cobra/core/metabolite.py index 6b3d31dc9..131a94e57 100644 --- a/src/cobra/core/metabolite.py +++ b/src/cobra/core/metabolite.py @@ -44,6 +44,8 @@ class Metabolite(Species): Compartment of the metabolite. """ + _DF_ATTRS = Species._DF_ATTRS + ["formula", "compartment", "charge"] + # noinspection PyShadowingBuiltins def __init__( self, diff --git a/src/cobra/core/model.py b/src/cobra/core/model.py index 597c3802b..156744ead 100644 --- a/src/cobra/core/model.py +++ b/src/cobra/core/model.py @@ -1500,6 +1500,9 @@ def _repr_html_(self) -> str: