Package: nxontology
NetworkX-based Python library for representing ontologiesSummarynxontology is a Python library for representing ontologies using a NetworkX graph.
Currently, the main area of functionality is computing similarity measures between pairs of nodes.UsageHere, we'll use the examplemetals ontology:Note thatNXOntologyrepresents the ontology as anetworkx.DiGraph, where edge direction goes from superterm to subterm.Given anNXOntologyinstance, here how to compute intrinsic similarity metrics.fromnxontology.examplesimportcreate_metal_nxometals=create_metal_nxo()# Freezing the ontology prevents adding or removing nodes or edges.# Frozen ontologies cache expensive computations.metals.freeze()# Get object for computing similarity, using the Sanchez et al metric for information content.similarity=metals.similarity("gold","silver",ic_metric="intrinsic_ic_sanchez")# Access a single similarity metricsimilarity.lin# Access all similarity metricssimilarity.results()The final line outputs a dictionary like:{'node_0':'gold','node_1':'silver','node_0_subsumes_1':False,'node_1_subsumes_0':False,'n_common_ancestors':3,'n_union_ancestors':5,'batet':0.6,'batet_log':0.5693234419266069,'ic_metric':'intrinsic_ic_sanchez','mica':'coinage','resnik':0.8754687373538999,'resnik_scaled':0.48860840553061435,'lin':0.5581154235118403,'jiang':0.41905978419640516,'jiang_seco':0.6131471927654584,}It's also possible to visualize the similarity between two nodes like:fromnxontology.vizimportcreate_similarity_graphvizgviz=create_similarity_graphviz(# similarity instance from abovesimilarity,# show all nodes (defaults to union of ancestors)nodes=list(metals.graph),)# draw to PNG filegviz.draw("metals-sim-gold-silver-all.png"))Resulting in the following figure:The two query nodes (gold & silver) are outlined with a bold dashed line.
Node fill color corresponds to the Sánchez information content, such that darker nodes have higher IC.
The most informative common ancestor (coinage) is outlined with a bold solid line.
Nodes that are not an ancestor of gold or silver have an invisible outline.Loading ontologiesPronto supports reading ontologies from the following file formats:Open Biomedical Ontologies 1.4:.oboextension, uses thefastoboparser.OBO Graphs JSON:.jsonextension, uses the fastobo parser.Ontology Web Language 2 RDF/XML:.owlextension, uses the prontoRdfXMLParser.The files can be local or at a network location (URL starting with https, http, or ftp).
Pronto detects and handles gzip, bzip2, and xz compression.Here are examples operations on the Gene Ontology,
using pronto to load the ontology:>>>fromnxontology.importsimportfrom_file>>># versioned URL for the Gene Ontology>>>url="http://release.geneontology.org/2021-02-01/ontology/go-basic.json.gz">>>nxo=from_file(url)>>>nxo.n_nodes44085>>># similarity between "myelination" and "neurogenesis">>>sim=nxo.similarity("GO:0042552","GO:0022008")>>>round(sim.lin,2)0.21>>>importnetworkxasnx>>># Gene Ontology domains are disconnected, expect 3 components>>>nx.number_weakly_connected_components(nxo.graph)3>>># Note however that the default from_file reader only uses "is a" relationships.>>># We can preserve all GO relationship types as follows>>>fromcollectionsimportCounter>>>importpronto>>>fromnxontologyimportNXOntology>>>fromnxontology.importsimportpronto_to_multidigraph,multidigraph_to_digraph>>>go_pronto=pronto.Ontology(handle=url)>>>go_multidigraph=pronto_to_multidigraph(go_pronto)>>>Counter(keyfor_,_,keyingo_multidigraph.edges(keys=True))Counter({'is a':71509,'part of':7187,'regulates':3216,'negatively regulates':2768,'positively regulates':2756})>>>go_digraph=multidigraph_to_digraph(go_multidigraph,reduce=True)>>>go_nxo=NXOntology(go_digraph)>>># Notice the similarity increases due to the full set of edges>>>round(go_nxo.similarity("GO:0042552","GO:0022008").lin,3)0.699>>># Note that there is also a dedicated reader for the Gene Ontology>>>fromnxontology.importsimportread_gene_ontology>>>read_gene_ontology(release="2021-02-01")Users can also create their ownnetworkx.DiGraphto use this package.Prebuilt OntologiesThenxontology-datarepository creates NXOntology objects for many popular ontologies / taxonomies.Installationnxontology can be installed withpipfromPyPIlike:# standard installationpipinstallnxontology# installation with viz extraspipinstallnxontology[viz]The extravizdependencies are required for thenxontology.vizmodule.
This includespygraphviz, which requires a pre-existinggraphvizinstallation.DevelopmentSome helpful development commands:# create a virtual environment for developmentpython3-mvenv.venv# activate virtual environmentsource.venv/bin/activate# install package for developmentpipinstall--editable".[dev,viz]"# Set up the git pre-commit hooks.# `git commit` will now trigger automatic checks including linting.pre-commitinstall# Run all pre-commit checks (CI will also run this).pre-commitrun--all# run testspytestReleases are created onGitHub.
Therelease actiondefined byrelease.yamlwill build the distribution and upload toPyPI.
The package version is automatically generated from the git tag bysetuptools_scm.BibliographyHere's a list of alternative projects with code for computing semantic similarity measures on ontologies:Semantic Measures Library & ToolKitatsharispe/slibin Java.DiShInatlasigeBioTM/DiShInin Python.Sematchatgsi-upm/sematchin Python.ontologySimilaritymirrored atcran/ontologySimilarity. Part of theontologyXsuite of R packages.Materials for Machine Learning with Ontologies atbio-ontology-research-group/machine-learning-with-ontologies(compilation)Below are a list of references related to ontology-derived measures of similarity.
Feel free to add any reference that provides useful context and details for algorithms supported by this package.
Metadata for a reference can be generated likemanubot cite --yml doi:10.1016/j.jbi.2011.03.013.
Adding CSL YAML output tomedia/bibliography.yamlwill cache the metadata and allow manual edits in case of errors.Semantic Similarity in Biomedical OntologiesCatia Pesquita, Daniel Faria, André O. Falcão, Phillip Lord, Francisco M. CoutoPLoS Computational Biology(2009-07-31)https://doi.org/cx8h87DOI:10.1371/journal.pcbi.1000443· PMID:19649320· PMCID:PMC2712090An Intrinsic Information Content Metric for Semantic Similarity in WordNet.Nuno Seco, Tony Veale, Jer HayesIn Proceedings of the 16th European Conference on Artificial Intelligence (ECAI-04),(2004)https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.1065.1695Metrics for GO based protein semantic similarity: a systematic evaluationCatia Pesquita, Daniel Faria, Hugo Bastos, António EN Ferreira, André O Falcão, Francisco M CoutoBMC Bioinformatics(2008-04-29)https://doi.org/cmcgw6DOI:10.1186/1471-2105-9-s5-s4· PMID:18460186· PMCID:PMC2367622Semantic similarity and machine learning with ontologiesMaxat Kulmanov, Fatima Zohra Smaili, Xin Gao, Robert HoehndorfBriefings in Bioinformatics(2020-10-13)https://doi.org/ghfqktDOI:10.1093/bib/bbaa199· PMID:33049044Semantic Similarity in a Taxonomy: An Information-Based Measure and its Application to Problems of Ambiguity in Natural LanguageP. ResnikJournal of Artificial Intelligence Research(1999-07-01)https://doi.org/gftcpzDOI:10.1613/jair.514An Information-Theoretic Definition of SimilarityDekang LinICML(1998)https://api.semanticscholar.org/CorpusID:5659557ontologyX: a suite of R packages for working with ontological dataDaniel Greene, Sylvia Richardson, Ernest TurroBioinformatics(2017-01-05)https://doi.org/f9k7sxDOI:10.1093/bioinformatics/btw763· PMID:28062448· PMCID:PMC5386138Metric of intrinsic information content for measuring semantic similarity in an ontologyMd. Hanif Seddiqui, Masaki AonoProceedings of the Seventh Asia-Pacific Conference on Conceptual Modelling - Volume 110(2010-01-01)https://dl.acm.org/doi/10.5555/1862330.1862343ISBN:9781920682927Disjunctive shared information between ontology concepts: application to Gene OntologyFrancisco M Couto, Mário J SilvaJournal of Biomedical Semantics(2011)https://doi.org/fnb73vDOI:10.1186/2041-1480-2-5· PMID:21884591· PMCID:PMC3200982A framework for unifying ontology-based semantic similarity measures: A study in the biomedical domainSébastien Harispe, David Sánchez, Sylvie Ranwez, Stefan Janaqi, Jacky MontmainJournal of Biomedical Informatics(2014-04)https://doi.org/f52557DOI:10.1016/j.jbi.2013.11.006· PMID:24269894Semantic Similarity in CheminformaticsJoão D. Ferreira, Francisco M. CoutoIntechOpen(2020-07-15)https://doi.org/ghh2d4DOI:10.5772/intechopen.89032An ontology-based measure to compute semantic similarity in biomedicineMontserrat Batet, David Sánchez, Aida VallsJournal of Biomedical Informatics(2011-02)https://doi.org/dfhkjvDOI:10.1016/j.jbi.2010.09.002· PMID:20837160Semantic similarity in the biomedical domain: an evaluation across knowledge sourcesVijay N Garla, Cynthia BrandtBMC Bioinformatics(2012-10-10)https://doi.org/gb8vpnDOI:10.1186/1471-2105-13-261· PMID:23046094· PMCID:PMC3533586Semantic similarity estimation in the biomedical domain: An ontology-based information-theoretic perspectiveDavid Sánchez, Montserrat BatetJournal of Biomedical Informatics(2011-10)https://doi.org/d2436qDOI:10.1016/j.jbi.2011.03.013· PMID:21463704Ontology-based information content computationDavid Sánchez, Montserrat Batet, David IsernKnowledge-Based Systems(2011-03)https://doi.org/cwzw4rDOI:10.1016/j.knosys.2010.10.001Leveraging synonymy and polysemy to improve semantic similarity assessments based on intrinsic information contentMontserrat Batet, David SánchezArtificial Intelligence Review(2019-06-03)https://doi.org/ghnfmtDOI:10.1007/s10462-019-09725-4An intrinsic information content-based semantic similarity measure considering the disjoint common subsumers of concepts of an ontologyAbhijit Adhikari, Biswanath Dutta, Animesh Dutta, Deepjyoti Mondal, Shivang SinghJournal of the Association for Information Science and Technology(2018-08)https://doi.org/gd2j5bDOI:10.1002/asi.24021