|
| 1 | +from pathlib import Path |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +import libsbml |
| 5 | +import pandas as pd |
| 6 | +from pymetadata.identifiers.miriam import BQB |
| 7 | + |
| 8 | +from sbmlutils.console import console |
| 9 | +from sbmlutils.io.sbml import read_sbml |
| 10 | +from sbmlutils.log import get_logger |
| 11 | +from pymetadata.core.annotation import RDFAnnotation |
| 12 | + |
| 13 | + |
| 14 | +logger = get_logger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +def validate_sbml_annotations(source: Union[Path, str]) -> pd.DataFrame: |
| 18 | + """Validate annotations in a given SBML file. |
| 19 | +
|
| 20 | + :param source: SBML to check |
| 21 | + :return: DataFrame of invalid annotations |
| 22 | + """ |
| 23 | + doc: libsbml.SBMLDocument = read_sbml(source=source) |
| 24 | + console.rule(style="white") |
| 25 | + console.print(f"Validate annotations: {source}", style="white bold") |
| 26 | + console.rule(style="white") |
| 27 | + |
| 28 | + elements = doc.getListOfAllElements() |
| 29 | + element: libsbml.SBase |
| 30 | + invalid_annotations: list = [] |
| 31 | + for element in elements: |
| 32 | + if element.isSetAnnotation(): |
| 33 | + cvterm: libsbml.CVTerm |
| 34 | + cvterms = element.getCVTerms() |
| 35 | + |
| 36 | + # console.rule(f"id='{element.id}' | {type(element)} | '{element.name}'", align="left", style="bold white") |
| 37 | + for cvterm in cvterms: |
| 38 | + qualifier_type = cvterm.getQualifierType() |
| 39 | + for k in range(cvterm.getNumResources()): |
| 40 | + |
| 41 | + resource_uri = cvterm.getResourceURI(k) |
| 42 | + # console.print(f"{qualifier_type} | {resource_uri}") |
| 43 | + annotation = RDFAnnotation(qualifier=BQB.IS, resource=resource_uri, validate=False) |
| 44 | + valid: bool = annotation.validate() |
| 45 | + if not valid: |
| 46 | + console.print( |
| 47 | + f"id='{element.id}' | {type(element).__name__} | '{element.name}' | {resource_uri}", |
| 48 | + style="warning" |
| 49 | + ) |
| 50 | + invalid_annotations.append( |
| 51 | + { |
| 52 | + "id": element.id, |
| 53 | + "object": type(element).__name__, |
| 54 | + "resource": resource_uri, |
| 55 | + } |
| 56 | + ) |
| 57 | + df = pd.DataFrame(invalid_annotations) |
| 58 | + if len(invalid_annotations) == 0: |
| 59 | + console.print("All annotations valid", style="success") |
| 60 | + else: |
| 61 | + console.print(df.to_string()) |
| 62 | + console.print("Invalid annotations", style="error") |
| 63 | + console.rule(style="white") |
| 64 | + return df |
| 65 | + |
0 commit comments