Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mat cleanup #1220

Merged
merged 12 commits into from
Jul 5, 2022
6 changes: 6 additions & 0 deletions release-notes/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

## Fixes

* Reformatted example files (e_coli_core.xml, mini_cobra.xml, mini.json, mini.yml, textbook.xml.gz) to be more compliant with identifiers.org. ncbigi is not a valid identifiers.org, so it was replaced with ncbiprotein.
* make sbml.py subsystem reading add partonomy, which matches the definition
of SBO:0000633 (see https://sourceforge.net/p/sbo/term-request/113/)
* Correct reading and writing of subysstem in mat.
akaviaLab marked this conversation as resolved.
Show resolved Hide resolved
* General cleanup of code in mat.py

## Other

* Resolve `flake8` issues and add missing type annotations and docstrings in `src/cobra/io` and `tests/test_io` (#1212).
Expand Down
39 changes: 19 additions & 20 deletions src/cobra/io/mat.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,27 +525,30 @@ def annotations_to_mat(
empty_lists = [[""] * len(annotation_list) for _ in annotation_matlab]
annotation_cells_to_be = dict(zip(annotation_matlab.values(), empty_lists))
for i in range(len(annotation_list)):
for provider_key, v in annotation_list[i].items():
if isinstance(v, str):
v = [v]
for provider_key, obj_annotation in annotation_list[i].items():
if isinstance(obj_annotation, str):
obj_annotation = [obj_annotation]
if provider_key == "pubmed":
v = ", ".join(
["PMID:" + annot if "PMID:" not in annot else annot for annot in v]
obj_annotation = ", ".join(
[
"PMID:" + annot if "PMID:" not in annot else annot
for annot in obj_annotation
]
)
elif provider_key == "CHEBI":
v = ", ".join(
obj_annotation = ", ".join(
[
"CHEBI:" + annot if "CHBEI:" not in annot else annot
for annot in v
for annot in obj_annotation
]
)
elif provider_key == "ec-code":
v = " or ".join(v)
obj_annotation = " or ".join(obj_annotation)
else:
v = ", ".join(v)
obj_annotation = ", ".join(obj_annotation)
if provider_key not in providers_used:
continue
annotation_cells_to_be[annotation_matlab[provider_key]][i] = v
annotation_cells_to_be[annotation_matlab[provider_key]][i] = obj_annotation
for annotation_key, item_list in annotation_cells_to_be.items():
mat_dict[annotation_key] = _cell(item_list)

Expand Down Expand Up @@ -582,18 +585,18 @@ def notes_to_mat(
empty_lists = [[""] * len(note_list) for _ in annotation_matlab]
annotation_cells_to_be = dict(zip(annotation_matlab.values(), empty_lists))
for i in range(len(note_list)):
for provider_key, v in note_list[i].items():
for provider_key, object_note in note_list[i].items():
if provider_key not in providers_used:
continue
if provider_key == CONFIDENCE_STR:
v = float(v)
object_note = float(object_note)
if not len(annotation_cells_to_be[annotation_matlab[provider_key]][i]):
annotation_cells_to_be[annotation_matlab[provider_key]][i] = v
annotation_cells_to_be[annotation_matlab[provider_key]][i] = object_note
else:
# References that aren't MIRIAM compliant will go to rxnNotes
annotation_cells_to_be[annotation_matlab[provider_key]][i] = (
annotation_cells_to_be[annotation_matlab[provider_key]][i]
+ f"; {v}"
+ f"; {object_note}"
)
for annotation_key, item_list in annotation_cells_to_be.items():
mat_dict[annotation_key] = _cell(item_list)
Expand Down Expand Up @@ -753,9 +756,7 @@ def from_mat_struct(
new_names[new_names.index(old_field)] = new_field
m.dtype.names = new_names

model = (
Model()
) # TODO - Model() creates models with name=None, while SBML creates them with name=''
model = Model()
if model_id is not None:
model.id = model_id
elif "description" in m.dtype.names:
Expand Down Expand Up @@ -903,9 +904,7 @@ def from_mat_struct(
rxn_group_names = set(rxn_subsystems).difference({None})
new_groups = []
for g_name in sorted(rxn_group_names):
group_members = model.reactions.query(
lambda x, g_n=g_name: x.subsystem == g_n
)
group_members = model.reactions.query(lambda x: x.subsystem == g_name)
new_group = Group(
id=g_name, name=g_name, members=group_members, kind="partonomy"
)
Expand Down