Cut the graph first, then summarise it
GraphRAG answers questions by reading summaries of a knowledge graph. We make it read only the part of the graph that the question is about.
The idea
Microsoft's GraphRAG summarises the whole graph once, before it knows the question, and uses the same summaries for every question. Query-first changes the order:
What happens when you ask
“What is Creutzfeldt-Jakob Disease (CJD)?”A question from BioASQ, followed through the five steps.
-
Read the question
Qwen 2.5 7B lists the genes, diseases and drugs the question names, with their type and standard English name. That is all the model decides.
“Creutzfeldt-Jakob Disease (CJD)”, a disease -
Find it in the graph
Each mention is looked up in a dictionary of node names and synonyms from DisGeNET, MeSH and HGNC. Names are lowercased, stripped of punctuation and their words sorted, so "Anemia, Sickle Cell" and "sickle cell anemia" become the same. Only exact matches count, so every link can be traced back to the name that produced it.
The node Creutzfeldt-Jakob disease (UMLS C0022336), by its name -
Grow a subgraph
From each entity we take its direct neighbours. Proteins linked to the same disease tend to cluster in one connected zone of the protein network, called the disease module, and DIAMOnD and C3 grow that zone from the neighbours. A random walk from the whole selection then adds the closest few nodes of each type. No model is involved, and with a fixed seed the same question always gives the same subgraph.
Each node remembers which stage added it, so one selection gives four nested subgraphs: neighbours only, plus DIAMOnD, plus C3, and the full one. The demo answers with all four.
-
Communities and reports
GraphRAG splits the subgraph into communities with Leiden, and Qwen3 30B writes a short report for each one, from the nodes, their edges and the definitions PrimeKG gives for them.
A handful of communities, each with its own report -
Answer
The model reads the reports in batches and takes a list of scored points from each batch. A last call merges the best points into the answer, shaped for the kind of question, citing the reports it used.
Only the reports of this subgraph are read. GraphRAG on the whole graph reads the 95 reports of level 0.
On the BioASQ questions of the demo, keeping only the neighbours of the entities spends a median of about 7 times fewer tokens than GraphRAG on the whole graph, counting every step. The full subgraph spends about as much, and more in a little under half of the questions. What the pipeline saves in tokens it pays in time, probably because it spends its tokens writing reports, and writing text is slower than reading it.
What we added to GraphRAG
The graph and the recorded runs
The graph joins two sources. Genes and diseases come from a version of PrimeKG built by Ingrid Heuer at the lab, with gene and disease associations from DisGeNET. Drugs, their targets and their chemical similarity come from the TDR Targets compound network. The demo replays the questions of the BioASQ summary evaluation from the pipeline's own outputs, packed so a browser can play them back without a GPU.
Technical summary
A summary of the thesis: motivation, the graph, the method and its implementation. 10 minutes.
Technical summary
Motivation and problem
Retrieval-Augmented Generation (RAG) adds a search step before the answer: it retrieves text chunks similar to the query and passes them to the model as context. Retrieving isolated chunks falls short when a question cannot be answered with a single passage. Edge et al. showed that a traditional RAG does worse on global sensemaking questions about a corpus, and proposed GraphRAG. It organises the corpus into a graph of entities and relationships, partitions it into communities and writes a summary for each one, and those summaries become the context of the answer.
In biomedicine the graph does not have to be extracted from text. Curated knowledge graphs such as PrimeKG already integrate genes, diseases and drugs, and some of their relations form the networks studied by network medicine: the interactome, the gene-disease associations and the drug-target networks. This project builds the communities and their summaries for each question instead, over a part of the graph chosen with criteria from network medicine.
- Protein-protein interaction (PPI)gene and geneHIPPIE
- Protein complex formationgene and complexSIGNOR
- Ontology hierarchydisease and diseaseMONDO, via PrimeKG
- Membership in a disease groupdisease and BERT groupPrimeKG
- Chemical similaritydrug and drugTDR Targets
- Gene-disease association (GDA)gene and diseaseDisGeNET
- Drug target, positive bioactivitydrug and geneChEMBL and BioLiP
- Indicationdrug and diseasePrimeKG
Background: global GraphRAG
In the reference implementation, Leiden partitions the graph recursively into a hierarchy of community levels. Level 0 holds the largest communities, and each following level splits again the ones that reach a maximum size. Each community gets a report written from its entities, their descriptions and its relationships, both inside the community and to nodes outside it. Questions are answered with global search, a map and reduce scheme. The reports of a chosen level are read in batches, and the model answers the query with each batch and scores each partial answer. A final call merges the highest scored ones into the answer.
Since all indexing precedes the query, the partition is unique and does not depend on the question. Over a biomedical graph, each report then describes a community of the whole graph even when the query is about a few entities. On the graph used here, 15 of the 95 communities of level 0 hold nine in ten nodes. Report length is fixed, so those few reports have to summarise most of the graph in the same space as the rest.
The knowledge graph: PrimeKG-enriched
The graph consolidates two sources so that between each pair of layers there is a single relation type, with a single meaning and a single origin.
- PrimeKG-curated, built by Ingrid Heuer, starts from the expert-curated gene-disease associations of DisGeNET and adds a network within each layer: protein interactions from HIPPIE and complexes from SIGNOR among genes, and the MONDO hierarchy among diseases. The clinical descriptions come from PrimeKG. It also keeps the PrimeKG BERT groups, nodes that group diseases with similar names, linked in a star to the diseases they group.
- The TDR Targets compound network (version 7) contributes compounds with bioactivity measured in ChEMBL or BioLiP, linked to their protein targets, and a chemical similarity measured with the Braun-Blanquet index. For the sets of structural features $A$ and $B$ of two compounds,
$$\mathrm{BB}(A,B) = \frac{|A \cap B|}{\max(|A|,\,|B|)}.$$
Compounds with $\mathrm{BB} = 1$ are grouped into an identity cluster, and the chemical edges join clusters, with weights from 0.81 to 0.99.
A compound enters the graph if it has positive bioactivity toward a gene of the graph or if it is a PrimeKG drug indicated for one of its diseases. The result has about 118,000 nodes, most of them drugs, and 425,000 edges. Three relations join the layers: GDA between genes and diseases, DRUG_TARGET between drugs and the genes they act on, and INDICATION between drugs and diseases.
Entity detection and linking
Mention extraction. qwen2.5:7b, run deterministically and constrained to valid JSON, receives the query and returns one entry per named entity: the text as written, its type and a standard English name. That name is the HGNC symbol for a gene, the generic name for a drug and the medical name for a disease.
Linking. Each mention is searched only among the nodes of its type. A node can be found by its name or by a synonym from DisGeNET and MeSH for diseases, MeSH for drugs and HGNC for genes. Synonyms are consulted only when no name matches, since several nodes can share one. Each mention then goes through three steps and keeps the first node it finds:
- Normalisation. Names and mentions are lowercased, stripped of accents and punctuation, and their words are sorted, so word order and punctuation stop mattering ("Anemia, Sickle Cell" becomes "anemia cell sickle").
- Dictionary. An exact match of that form, first against node names and then against synonyms.
- Segments. If nothing matched, the longest run of consecutive words of the English name that matches a name or synonym exactly.
When a form belongs to several nodes, the one with the most names or synonyms producing it wins, and ties go to the shortest name. Linking uses no model. A mention that is not linked is left out of the selection.
Subgraph selection
The figure follows one query through every stage of the selection. Each button shows the graph after that stage.
“What are the shared molecular mechanisms between celiac disease and asthma?”
The entity mentions are extracted from the query and each one is linked to a PrimeKG-enriched node of the same type, in this case two diseases.
Each entity finds its neighbours through the bipartite relations: five genes each through GDA, the seeds in the gene network, and two drugs for asthma through INDICATION, too few to expand.
In the gene network, DIAMOnD adds two nodes for each entity (k = 2). The number next to each addition is the iteration at which it entered.
In the asthma module C3 joins a seed that had been left isolated. The celiac disease module is already connected, and C3 adds nothing.
The random walk crosses the three layers together and adds the highest scoring nodes of each type: a gene that interacts with both modules, a disease related to both entities and a drug that targets a gene added by DIAMOnD.
The query subgraph is built with the chosen nodes and all the edges of PrimeKG-enriched among them.
Seeds and steps
From each linked entity, the selection traverses the bipartite relations toward the other two layers. Each traversal is a step, and its seeds are the neighbours it finds inside the intra-layer network of their layer, where the module is grown. Each entity runs the two steps of its type, six in total:
| Origin | Relation | Layer reached | Intra-layer network |
|---|---|---|---|
| gene | GDA |
diseases | MONDO hierarchy |
| gene | DRUG_TARGET |
drugs | chemical similarity $\geq 0.8$ |
| disease | GDA |
genes | protein interaction |
| disease | INDICATION |
drugs | chemical similarity $\geq 0.8$ |
| drug | DRUG_TARGET |
genes | protein interaction |
| drug | INDICATION |
diseases | MONDO hierarchy |
The expansion never changes node type, because the hypergeometric test of DIAMOnD and C3 assumes that all nodes are of one type. A step expands only when the entity has at least five seeds, and otherwise contributes just the neighbours it found. A disease entity also collects the neighbours of its BERT group, because in PrimeKG many indications hang from the group.
DIAMOnD
DIAMOnD grows a module from a set of seeds by adding, at each iteration, the node whose number of links to the module is the most improbable given its degree (its connectivity significance). In a network of $N$ nodes with $s_0$ seeds scattered at random, the probability that a node of degree $g$ has exactly $g_s$ links to seeds is hypergeometric,
$$p(g, g_s) = \frac{\binom{s_0}{g_s}\binom{N - s_0}{g - g_s}}{\binom{N}{g}},$$
and significance is the p-value of observing $g_s$ links or more. At each iteration the node with the lowest p-value joins the module, and $s_0$ grows with it. The selection uses the unweighted version and runs it for a number of iterations $k$ fixed per step.
C3
C3 (connect separate connected components) runs on the output of DIAMOnD and uses the same hypergeometric test, but counts each component of the module as a single node, with the goal of joining them. At each iteration it adds the candidate with the lowest p-value, a single node or a pair of adjacent nodes, only if that p-value is below 0.05 and the addition reduces the number of components. Otherwise it stops. DIAMOnD brings in the nodes most significantly connected to the seeds, and C3 joins the pieces DIAMOnD left apart.
Heterogeneous random walk with restart
DIAMOnD and C3 see only one layer at a time, so they miss nodes reachable only through paths that mix types, such as a disease that shares several of the added genes with the entity. The last stage is a random walk with restart (RWR) over the whole graph, starting from the entire selection so far:
$$p_{n+1} = (1-r)\, S^{\top} p_n + r\, p_0,$$
with $p_0$ uniform over the selection and $r = 0.5$. The transition matrix $S$ follows MultiXrank. The walk first chooses the type to move to, with equal probability for the three, and only then a neighbour of that type. That way the dense gene network does not pull the walk toward genes. The highest scoring nodes not yet selected are added up to a fixed quota of 9 genes, 8 diseases and 8 drugs, at most 25 nodes per query.
From the subgraph to the answer
The GraphRAG adaptation has two parts.
- The graph already exists. The text-to-graph extraction steps disappear. The library's Bring Your Own Graph option accepts the tables the extraction would have produced. Each node becomes an entity described by its type, its name and, for diseases and drugs, its PrimeKG definition. Each edge becomes a fixed sentence by type, such as "The proteins KRT5 and KRT14 interact".
- The model. Reports and answers are written by Qwen3 30B-A3B Instruct, at temperature 0 with a fixed seed, on the lab's GPU. Its context window of 8,192 tokens is close to the one the original work used.
Leiden partitions only the largest connected component of the subgraph, and global search always uses level 0. Reports, scored points and the answer are kept short, about 250, 200 and 400 words, so everything fits in the context window.
Code
The code of the pipeline and of this page is on GitHub: github.com/ezegrenat/query-first-graphrag.
Ezequiel González Grenat, Bioinformatics Lab, Leloir Institute. Thesis work on query-first subgraph selection for biomedical GraphRAG.
