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 splits a whole graph into communities and has a language model write a report about each one. Questions are answered by reading those reports, which have to be written for the whole graph before anyone asks anything. We turn the order around.
The whole-graph reports are written once and shared by every question. The small ones are written again for each question.
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. Matches are exact after normalising case, punctuation and word order, 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 the entity we take its direct neighbours, then let DIAMOnD and C3 add the nodes significantly connected to them, the disease module. A random walk from the whole selection adds a last handful of close nodes. No model is involved.
Each node remembers which stage added it, so one selection gives four nested subgraphs: neighbours only, plus DIAMOnD, plus C3, and the full one (in the thesis: seeds, DIAMOnD, C3 and RWR). 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. This index is built for this question only.
A handful of communities, each with its own report -
Answer
The question is put to every report, the useful points are scored, and the best ones are merged into one paragraph that cites the reports it relies on.
Only those reports are read · GraphRAG on the whole graph reads many more, and spends more tokens, for the same question
Under the hood
The graph and the recorded runs
The graph is PrimeKG, integrated in an earlier thesis of the lab with gene and disease associations from DisGeNET, plus a drug layer of bioactivity and chemical similarity built in the lab. 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. The code and the thesis will be published soon.
(This is a summary drawn from the thesis)
Technical summary
Motivation, the graph, the method and its implementation · 12 min read
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, which organises the corpus into a graph of entities and relationships, partitions it into communities and writes a summary for each one, so that 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 theory: the interactome, the gene-disease associations and the drug-target networks. This project proposes a GraphRAG in which the graph that gives context to the model is induced by the question instead of being the whole graph, and that induced graph is found with criteria from network theory.
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, the model answers the query with each batch and scores each partial answer, and 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 whole graph used here (117,689 nodes and 425,409 edges), Leiden partitions the largest connected component of 116,362 nodes into 16,257 communities across six levels. Level 0 has 95 communities, and 15 of them, each with more than 1,000 nodes, hold 90.1% of the nodes. Report length is fixed, so the reports of those giant communities 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 in a previous licentiate thesis, starts from the expert-curated gene-disease associations of DisGeNET and adds a network within each layer: protein-protein interactions from HIPPIE (and complexes from SIGNOR) among genes, and the MONDO hierarchy among diseases, with the clinical descriptions brought 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.
To consolidate them, proteins were mapped from UniProt to Entrez genes (keeping only human genes present in the graph), and the 1,801 PrimeKG drugs with an indication for a disease in the graph were given their ChEMBL and BioLiP codes. A compound enters the graph if it has positive bioactivity toward a gene of the graph or if it is one of those drugs. The result has 117,689 nodes (83,867 drugs, 17,743 genes and 16,079 diseases) and 425,409 edges, with three bipartite relations between layers: GDA (gene and disease), DRUG_TARGET (drug and gene, positive bioactivity) and INDICATION (drug and disease).
- 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
Entity detection and linking
Mention extraction. qwen2.5:7b (temperature 0, fixed seed, output constrained to valid JSON) receives the query and returns one entry per named entity, with the text as written, the type (gene, disease or drug) and a standard English name: the official HGNC symbol for a gene, the generic name for a drug and the medical name for a disease. The prompt asks it to keep the subtype the query names and to expand abbreviations when the query gives the full name in parentheses, and it includes two solved examples.
Linking. Each mention is searched only among the nodes of its type (17,743 genes, 15,039 diseases and 10,194 drugs can be linked). Of those drugs, only the 1,481 that come from PrimeKG have a generic name, and the rest are named by their ChEMBL code or BioLiP CCD. A node can be found by its name or by a synonym: DisGeNET mappings to eleven vocabularies and MeSH entry terms for diseases, MeSH entry terms for drugs, and the HGNC approved name, aliases and previous symbols 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 that "Anemia, Sickle Cell" and "sickle cell anemia" both become "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, each name or synonym that produces it counts as a vote, the node with the most votes wins, and ties go to the node with the shortest name so the result does not vary between runs. Linking uses only the dictionary, with 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. Through GDA, each one brings five genes that are the seeds in the gene network, and they share one. Through INDICATION, asthma brings two drugs, which enter the selection. That step does not expand because it does not reach the minimum of five seeds. Celiac disease has no indicated drugs.
Within the gene network and separately for each entity, DIAMOnD adds at each iteration the node with the most significant connectivity toward the module according to the hypergeometric test (here k = 2). The number next to each addition is the iteration at which it entered.
C3 starts from the seeds plus what DIAMOnD added and only adds nodes that reduce the number of components of the module. In the asthma module it joins a seed that had been left isolated. The celiac disease module is already connected, and C3 stops without adding anything.
A random walk with uniform restart over the whole selection traverses the three layers and the three bipartite relations together. From each type the highest scoring nodes enter up to the quota: a gene that interacts with both modules, a disease related to both entities in the ontology and a drug that targets one of the genes added by DIAMOnD.
The query subgraph is built with the chosen nodes and all the edges of PrimeKG-enriched among them.
Seeds and steps
The selection borrows the disease module hypothesis: proteins associated with the same disease tend to cluster in a connected zone of the interactome instead of scattering at random. It uses the same mechanics of growing a set from initial nodes, called seeds, inside the intra-layer network of each layer.
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 that belong to the intra-layer network of their layer. Each entity runs the two steps of its type, so there are six steps 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, and the jump between types happens only through the bipartite relation. This preserves the homogeneity that the hypergeometric test of DIAMOnD and C3 assumes, since it compares each candidate with nodes of the same network. A step expands only when the entity has at least five seeds. Otherwise it still contributes all the neighbours it found. Neighbours outside the intra-layer network enter the selection but not the expansion. A disease entity also collects the neighbours of its BERT group, and the group itself enters the selection, because in PrimeKG many indications hang from the group and not from the disease.
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,
$$p\text{-value}(g, g_s) = \sum_{g_i = g_s}^{g} p(g, g_i).$$
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, in which seeds and added nodes count the same, and runs it for a number of iterations $k$ fixed per step.
C3
C3 (connect separate connected components) uses the same hypergeometric criterion, but counts each component of the module as a single node, with the goal of joining them. With $s_0$ the number of components of the module, $n_0$ the number of nodes outside it, and a candidate that touches $g_s$ distinct components and has $g_n$ neighbours outside the module (so $g = g_s + g_n$),
$$p(g, g_s) = \frac{\binom{s_0}{g_s}\binom{n_0}{g - g_s}}{\binom{s_0 + n_0}{g}},$$
$$p\text{-value}(g, g_s) = \sum_{g_i = g_s}^{g} p(g, g_i).$$
At each iteration it adds the candidate with the lowest p-value if that p-value is below 0.05 and the addition reduces the number of components. If the lowest p-value node does not meet both conditions, it evaluates pairs of adjacent nodes outside the module (at least one of them an immediate neighbour) as single candidates, and adds the lowest p-value pair whole if it meets the same two conditions. Otherwise the algorithm stops.
Combination. In each step DIAMOnD runs first for $k$ iterations, and its output (the seeds and the nodes it added) becomes the seed set of C3. DIAMOnD brings in the nodes with the most significant connectivity to the seeds, and C3 joins the components 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 by degree, so 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 three 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 and name, followed by its PrimeKG definition (for diseases and drugs) truncated to 150 words at a full sentence, and each edge becomes a relation verbalised with a fixed sentence by type, such as "The gene KRT5 is associated with EPIDERMOLYSIS BULLOSA SIMPLEX" or "The proteins KRT5 and KRT14 interact". Each node also gets a text unit with its definition and the sentences of its edges, which the library requires even though global search does not read it.
- The node text is the definition PrimeKG gives, not a description written by the model from mentions in a corpus.
- The model. Reports and answers are written by Qwen3 30B-A3B Instruct 2507 without reasoning mode, at temperature 0 with a fixed seed, on the lab's GPU with an 8,192-token context window, close to the 8,000 tokens the original work used.
Leiden partitions only the largest connected component of the subgraph, communities of more than 20 nodes are split again, and each report is asked for 250 words (a request in the prompt, not a cap) from at most 2,500 input tokens. Global search always uses level 0. The map step reads the reports in batches of 5,000 tokens and returns scored points of about 200 words, and the reduce step writes an answer of about 400 words in the format of the question type.
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.
