How githubtarohario/mkgr-agent works
Purpose and Entry Points
MKGR-Agent is a CLI medical knowledge-graph research MVP for Disease → Phenotype → Gene exploration. The primary entry point is app/main.py; it builds local RDF assets with --build-rdf or runs a natural-language research query. app/orchestrator.py coordinates the agent pipeline and produces structured answers plus GraphML.
Knowledge Ingestion and RDF Boundary
Ontology/source data from MONDO, HPO, and HGNC is parsed and integrated into RDF by the sole write path, app/graph/build_rdf.py. Source-specific parsing is isolated in app/graph/obo_parser.py, while namespaces and RDF construction conventions reside in app/graph/ns.py. This is the critical offline-build/runtime boundary: generated Turtle/N-Triples and Oxigraph data are consumed read-only during queries.
Graph Storage, Querying, and Projection
app/graph/store.py opens the persisted RDF graph for runtime access. app/tools/sparql.py is the query-facing abstraction used by agents rather than allowing arbitrary graph mutation. RDF can be projected to NetworkX and narrowed to relevant neighborhoods through app/graph/rdf_to_networkx.py and app/graph/subgraph.py; app/graph/graphml_export.py exports the resulting research graph for Gephi.
Query Resolution and Entity Validation
User requests first pass through app/agents/query_parser.py, which extracts disease candidates with rules, Japanese aliases, and fuzzy matching. app/tools/ja_alias.py supplies the limited Japanese disease alias vocabulary, and app/tools/mondo.py resolves candidates against MONDO. This validation boundary prevents ungrounded disease names from reaching graph traversal.
Three-Agent Research Pipeline
The orchestrator invokes:
• Knowledge retrieval in app/agents/knowledge_agent.py, which explores RDF/SPARQL relations for disease, phenotype, and gene entities.
• Context retrieval in app/agents/literature_agent.py, which currently searches RDF-derived local text rather than external literature.
• Relation checking and evidence labeling in app/agents/critic_agent.py, which revalidates claims against RDF and caps evidence at ontology-derived E2.
Shared result structures are represented by app/models/entity.py, app/models/relation.py, and app/models/evidence.py.
Retrieval and External-Service Boundaries
“Literature” retrieval is local LightRAG-lite TF-IDF search via app/tools/lightrag_tool.py and app/tools/search_index.py; PubMed/PMC is not integrated. Optional OpenAI access is isolated in app/tools/llm_client.py: it only helps extract a disease-name string when rule extraction is weak, after which MONDO fuzzy validation remains mandatory. Runtime otherwise operates locally on Python 3.11, RDF/Oxigraph, and graph-export tooling.
Domain Scope and Reliability Constraints
The implemented graph intentionally limits disease–gene links to MONDO germline-mutation relations; it does not infer links by joining disease and gene OMIM cross-references. HPO annotations inherit rare-disease source coverage. Consequently, an empty phenotype or gene result can be a valid source-coverage outcome, and agents should preserve that distinction rather than fabricate relationships.
Main entry points: orchestrator.py Research orchestrator · build_rdf.py RDF builder · query_parser.py Disease query parser · knowledge_agent.py Knowledge agent
Components 20
CLI & Orchestration
- CLI entry point · Builds RDF assets or runs a research query.
app/main.py - Research orchestrator · Coordinates resolution, agents, structured answers, and GraphML.
app/orchestrator.py
RDF Build & Graph Data
Offline-generated RDF is read-only at runtime.
- RDF builder · The sole path that integrates source ontologies into RDF.
app/graph/build_rdf.py - Ontology parser
app/graph/obo_parser.py - RDF namespaces
app/graph/ns.py - Persisted RDF assets · Generated offline and consumed read-only during research.
- Runtime graph store
app/graph/store.py - SPARQL query tool · Agent-facing RDF access boundary.
app/tools/sparql.py
Query Resolution
- Disease query parser · Uses rules, Japanese aliases, and fuzzy matching.
app/agents/query_parser.py - Japanese aliases
app/tools/ja_alias.py - MONDO resolver · Mandatory grounding boundary for disease candidates.
app/tools/mondo.py - Optional OpenAI helper · Only assists disease-name extraction when rules are weak.
app/tools/llm_client.py
Research Agents
- Knowledge agent · Explores disease, phenotype, and gene relations.
app/agents/knowledge_agent.py - Context agent · Retrieves RDF-derived local context, not PubMed or PMC.
app/agents/literature_agent.py - Critic agent · Revalidates RDF claims and caps evidence at ontology-derived E2.
app/agents/critic_agent.py - LightRAG-lite search
app/tools/lightrag_tool.py - Local search index
app/tools/search_index.py
Results & Export
- Research result models
app/models/entity.py - Research subgraph · Projects RDF and narrows relevant neighborhoods.
app/graph/subgraph.py - GraphML export
app/graph/graphml_export.py
Connections 25
- CLI entry point → RDF builder: --build-rdf
- CLI entry point → Research orchestrator: research query
- RDF builder → Ontology parser: parses MONDO, HPO, HGNC
- RDF builder → RDF namespaces: uses RDF conventions
- RDF builder → Persisted RDF assets: writes
- Persisted RDF assets → Runtime graph store: read-only runtime data
- Runtime graph store → SPARQL query tool: serves queries
- Research orchestrator → Disease query parser: natural-language request
- Disease query parser → Japanese aliases: looks up aliases
- Disease query parser → Optional OpenAI helper: fallback extraction
- Disease query parser → MONDO resolver: candidate validation
- MONDO resolver → Knowledge agent: grounded disease
- Research orchestrator → Knowledge agent: invokes
- Research orchestrator → Context agent: invokes
- Research orchestrator → Critic agent: invokes
- Knowledge agent → SPARQL query tool: retrieves relations
- Context agent → LightRAG-lite search: retrieves context
- LightRAG-lite search → Local search index: searches
- Critic agent → SPARQL query tool: revalidates claims
- Knowledge agent → Research result models: produces findings
- Context agent → Research result models: adds context
- Critic agent → Research result models: labels evidence
- SPARQL query tool → Research subgraph: RDF relations
- Research subgraph → GraphML export: exports
- Research orchestrator → GraphML export: produces GraphML