def cvterms(cls, sbase: libsbml.SBase) -> Optional[List]: """Parse CVTerms information. :param sbase: SBase instance """ if not sbase.isSetAnnotation(): return None cvterms = [] for kcv in range(sbase.getNumCVTerms()): cv: libsbml.CVTerm = sbase.getCVTerm(kcv) # qualifier q_type = cv.getQualifierType() if q_type == libsbml.MODEL_QUALIFIER: qualifier = ModelQualifierType[cv.getModelQualifierType()] elif q_type == libsbml.BIOLOGICAL_QUALIFIER: qualifier = BiologicalQualifierType[ cv.getBiologicalQualifierType()] else: raise ValueError(f"Unsupported qualifier type: '{q_type}'") resources = [ cv.getResourceURI(k) for k in range(cv.getNumResources()) ] cvterms.append({ "qualifier": qualifier, "resources": resources, }) # add SBO term as CVTerm if sbase.isSetSBOTerm(): sbo = sbase.getSBOTermID() sbo_in_cvs: bool = False for cv in cvterms: for resource in cv["resources"]: if sbo in resource: sbo_in_cvs = True break if not sbo_in_cvs: cvterms = [{ "qualifier": "BQB_IS", "resources": [f"https://identifiers.org/{sbo}"], }] + cvterms return cvterms
def sbase_dict(cls, sbase: libsbml.SBase) -> Dict[str, Any]: """Info dictionary for SBase. :param sbase: SBase instance for which info dictionary is to be created :return info dictionary for item """ pk = cls._get_pk(sbase) d = { "pk": pk, "sbmlType": cls._sbml_type(sbase), "id": sbase.getId() if sbase.isSetId() else None, "metaId": sbase.getMetaId() if sbase.isSetMetaId() else None, "name": sbase.getName() if sbase.isSetName() else None, "sbo": sbase.getSBOTermID() if sbase.isSetSBOTerm() else None, "cvterms": cls.cvterms(sbase), "history": cls.model_history(sbase), "notes": sbase.getNotesString() if sbase.isSetNotes() else None, } # TODO: add the ports information if sbase.getTypeCode() in {libsbml.SBML_DOCUMENT, libsbml.SBML_MODEL}: d["xml"] = None else: d["xml"] = sbase.toSBML() # comp item_comp = sbase.getPlugin("comp") if item_comp and type(item_comp) == libsbml.CompSBasePlugin: # ReplacedBy if item_comp.isSetReplacedBy(): replaced_by = item_comp.getReplacedBy() submodel_ref = replaced_by.getSubmodelRef() d["replacedBy"] = { "submodelRef": submodel_ref, "replacedBySbaseref": cls._sbaseref(replaced_by), } else: d["replacedBy"] = None # ListOfReplacedElements if item_comp.getNumReplacedElements() > 0: replaced_elements = [] for rep_el in item_comp.getListOfReplacedElements(): submodel_ref = rep_el.getSubmodelRef() replaced_elements.append({ "submodelRef": submodel_ref, "replacedElementSbaseref": cls._sbaseref(rep_el), }) d["replacedElements"] = replaced_elements else: d["replacedElements"] = None # distrib sbml_distrib: libsbml.DistribSBasePlugin = sbase.getPlugin("distrib") if sbml_distrib and isinstance(sbml_distrib, libsbml.DistribSBasePlugin): d["uncertainties"] = [] for uncertainty in sbml_distrib.getListOfUncertainties(): u_dict = SBMLDocumentInfo.sbase_dict(uncertainty) u_dict["uncertaintyParameters"] = [] upar: libsbml.UncertParameter for upar in uncertainty.getListOfUncertParameters(): param_dict = { "var": upar.getVar() if upar.isSetVar() else None, "value": upar.getValue() if upar.isSetValue() else None, "units": upar.getUnits() if upar.isSetUnits() else None, "type": upar.getTypeAsString() if upar.isSetType() else None, "definitionURL": upar.getDefinitionURL() if upar.isSetDefinitionURL() else None, "math": astnode_to_latex(upar.getMath()) if upar.isSetMath() else None, } u_dict["uncertaintyParameters"].append(param_dict) d["uncertainties"].append(u_dict) return d