Overview

3 Create your first Knowledge Graph from ontologies

Building a useful knowledge graph starts with business goals and data understanding, not technology. The chapter frames knowledge graph construction as a semantic integration problem: fusing heterogeneous data sources that differ by format, storage, syntax, and—crucially—meaning. It proposes using ontologies as a reference schema and controlled vocabulary to harmonize synonyms, disambiguate acronyms, and align different levels of granularity. Grounding the discussion in a clinical use case—supporting clinicians who must recognize rare diseases—the chapter adopts the Human Phenotype Ontology and an ontology-annotated dataset to create a unified, queryable representation that preserves provenance and enables exploration of disease–phenotype relationships.

The chapter compares the main knowledge graph technologies through a goal-driven lens. RDF excels at standardized, interoperable representations and rich semantics for reasoning, but requires modeling patterns (n-ary relations, named graphs, or RDF-star) to attach metadata to specific assertions. Labeled Property Graphs attach properties directly to nodes and edges, making them efficient and intuitive for relationship-centric metadata such as evidence, source, frequency, onset, and curation details. Given clinicians’ needs to compare and trace annotation-level details, the chapter favors LPG for the core graph while noting bridges between the two worlds and vendor tooling that brings inference capabilities to property graphs. The takeaway is pragmatic: let the use case dictate the model.

Practically, the chapter walks through a repeatable pipeline aligned with business and data understanding, preparation, model creation, and querying. It imports the HPO ontology, derives clean identifiers and labels, ingests the annotations file to create disease nodes and disease–phenotype links, and enriches relationships with provenance and interpretive fields (evidence categories, frequency, onset, curator, dates). After pruning extraneous ontology scaffolding, the resulting graph supports queries that rank diseases by observed phenotypic features, inspect co-occurrence patterns, and trace sources. Finally, ontology-based reasoning over the HPO hierarchy enables inference from high-level categories down to specific traits, letting clinicians navigate different granularities with a precise vocabulary, transparent provenance, and actionable insights for rare disease diagnosis.

The mental model describing the KG construction process as a specification of the CRIPS-DM model, from understanding the business goal to defining the KG queries that support the clinicians' activities.
The CRIPS-DM model adapted to KGs. A subset of the components of this diagram, including business understanding, data understanding, data preparation, and KG model creation/update, represent key phases described in this chapter.
Understanding the business domain for creating a KG that supports the clinicians' activities. This phase is not strictly related to the technical aspects, but it is fundamental for the next steps.
An annotated Venn diagram shows that Type 1 Diabetes Mellitus can be considered as either a disease or a phenotypic feature. Based on the context, two different IDs can be adopted.
Understanding the data to support the clinicians' activities. This fundamental explorative phase gets the key information needed to construct the KG.
Data transformation from a table row to a KG edge. Information included in this table is adapted to define the properties of nodes and the edges of the KG.
Ontology ingestion and processing.
A portion of the HPO ontology loaded in the graph database using LPG as storage model. We can distinguish between two types of information: ontological information (left side) and domain-specific information related to the phenotypic features (right side).
Annotation data set ingestion and processing to finalize the construction of the KG.
Querying the generated KG to support the clinician's activities.
Result of a query that gets all the phenotype features related to Type 1 diabetes.

Summary

  • Constructing the knowledge graph (KG) is a complex process because it requires extracting and integrating information from heterogeneous sources.
  • Incorporating these sources aims to harmonize their data and leads to a coherent perspective on the general information.
  • At the end of the building process, the resulting KG must be a unified, well-grounded, and meaningful representation of data from different sources, where the individual pieces of information are fused into a unique view.
  • A well-driven KG construction requires a clear idea of the problem you want to solve, an understanding of the reference domain, and a phase involving data scouting, exploration, and comprehension.
  • The Resource Description Framework (RDF) and the labeled property graph (LPG) are two of the most prominent technologies for materializing KGs.
  • The RDF data model focuses on knowledge representation and is particularly suitable for constructing ontologies.
  • The LPG approach intends to provide a fast and query-based traversal mechanism of graph data and path analysis features, emphasizing the efficiency of data storage and access.
  • Understanding the differences between RDF and LPG is crucial to selecting the best technologies for your specific purpose.
  • Building a KG from the Human Phenotype Ontology (HPO) dataset can be particularly helpful to clinicians, which enables them to make better diagnoses, starting with the analysis of the phenotypic features detected in a patient.
  1. Kohler, S., Gargano, M., Matentzoglu, N., Carmody, L. C., Lewis-Smith, D., Vasilevsky, N. A., ... & Robinson, P. N. (2021). The human phenotype ontology in 2021. Nucleic acids research, 49(D1), D1207-D1217. https://academic.oup.com/nar/article/49/D1/D1207/6017351?login=true
  2. Orlandi, F., Graux, D., & O'Sullivan, D. (2021, January). Benchmarking RDF Metadata Representations: Reification, Singleton Property, and RDF. In 2021 IEEE 15th International Conference on Semantic Computing (ICSC) (pp. 233-240). IEEE. https://ieeexplore.ieee.org/abstract/document/9364401/
  3. Stickle, D., & Turk, J. (1997). A kinetic mass balance model for 1, 5-anhydroglucitol: applications to monitoring of glycemic control. American Journal of Physiology-Endocrinology and Metabolism, 273(4), E821-E830. https://journals.physiology.org/doi/full/10.1152/ajpendo.1997.273.4.E821

FAQ

What business problem does this chapter address?The chapter shows how to build a knowledge graph that helps clinicians diagnose rare diseases. It starts from business goals, identifies the clinician as the target persona, and designs a KG that unifies heterogeneous data, preserves clinical context (phenotype hierarchies), and exposes provenance so clinicians can trust and compare evidence.
Which data sources are used to build the KG?Two HPO sources are used: - hp.owl (OWL/RDF): the Human Phenotype Ontology with standardized phenotype terms, hierarchies, synonyms, and cross-references. - phenotype.hpoa (TSV): curated annotations linking diseases (e.g., OMIM, Orphanet) to HPO phenotypic features, with metadata such as evidence, frequency, onset, and provenance.
Why are ontologies essential for semantic integration?Ontologies provide a common vocabulary and structure (concepts, properties, relationships) that: - Reconcile synonyms and acronyms (e.g., “Type 2 diabetes” variants, ambiguous “PE”). - Align different granularities (e.g., “necrosis” vs “lobular necrosis”). - Enable mappings from local schemas to a shared reference model so heterogeneous sources can be fused into one coherent KG.
RDF or LPG: which model fits this use case and why?Both are viable, but LPG is chosen because the use case centers on rich, edge-specific metadata (source, evidence, onset, frequency, curator, date) on the relationship between a disease and a phenotype. LPG stores properties directly on relationships and offers efficient traversal for query workflows clinicians need. RDF excels at ontology modeling, and the HPO ontology itself is in RDF/OWL.
How can I represent relationship metadata in RDF, and what are the trade-offs?Common RDF patterns: - N-ary relations: introduce an Annotation node to hold metadata; clear but adds indirection. - Named graphs: group triples into a graph and attach provenance to the graph; powerful but can add management overhead. - RDF-star: annotate a triple directly; compact and readable, but engine support and performance can vary. LPG attaches properties directly on edges, which is simpler for this metadata-heavy scenario.
What tools and environment are used?Neo4j 5.20.0 (Enterprise), APOC 5.20.0, and Neosemantics (n10s) 5.20.0 for loading RDF and building an LPG-based KG. rdflib (Python) is shown for exploring OWL as triples. Sample code and queries are provided in the chapter’s GitHub repository: https://github.com/alenegro81/knowledge-graphs-and-llms-in-action/tree/main/chapters/ch03
How is the ontology (hp.owl) loaded and prepared in Neo4j?Steps: - Create the database and add constraints/indexes for Resource.uri and Resource.id; index HpoPhenotype.id and HpoDisease.id. - Configure neosemantics (e.g., handleVocabUris: IGNORE, applyNeo4jNaming: true). - Import hp.owl via n10s.rdf.import.fetch. - Enrich Resource nodes whose URIs start with HP_ by adding label HpoPhenotype and computing id (e.g., HP:0000819).
How are HPO annotations ingested and how is edge metadata preserved?Process: - LOAD CSV of phenotype.hpoa (TSV), skip header rows. - MERGE HpoDisease nodes from database_id and set their labels from disease_name. - MERGE HAS_PHENOTYPIC_FEATURE edges from disease to HpoPhenotype using hpo_id. - Add relationship properties conditionally: source, evidence, onset, frequency, sex, modifier, aspect, biocuration. - Derive helpful fields (e.g., createdBy, creationDate from biocuration; evidenceName/Description; aspectName/Description; url to PubMed/OMIM) using APOC string utilities and batch updates.
How does the KG support clinical queries for diagnosis?Clinicians can: - Retrieve phenotypic features of a known disease (e.g., OMIM:222100 for Type 1 diabetes). - Input a set of observed features (e.g., Growth delay, Large knee, Sensorineural hearing impairment, Pruritus, Type I diabetes mellitus) and rank diseases by the number of matching phenotypes. - Inspect provenance on each disease–phenotype link (e.g., evidence type, source PMID, curator, date) to assess trust and relevance.
How is ontology-based reasoning applied in this KG?By leveraging the HPO hierarchy (SUBCLASSOF): - Expand high-level features (e.g., “Abnormality of the endocrine system”, HP:0000818) to their subclasses. - Use Neosemantics’ n10s.inference.nodesInCategory to infer diseases linked via any subclass features. - Combine explicit and inferred links to broaden differential diagnosis while preserving traceability.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Knowledge Graphs and LLMs in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Knowledge Graphs and LLMs in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Knowledge Graphs and LLMs in Action ebook for free