-
Notifications
You must be signed in to change notification settings - Fork 5
feat: map diagnosis facet term values to display value (#4722) #4723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
frano-m
wants to merge
7
commits into
main
Choose a base branch
from
noopfran/4722-facet-term-mapping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
647ac07
feat: map diagnosis facet term values to display value (#4722)
frano-m 6c30669
feat: add HP and OMIM diagnosis term ID to name mapping #4722
NoopDog 91a5d3f
feat: replace placeholder diagnosis mapping with full lookup data #4722
NoopDog 287c314
fix: rename export to DIAGNOSIS_DISPLAY_VALUE to match existing impor…
NoopDog 9f106e8
feat: mapped diagnosis column values (#4722)
frano-m 3305871
fix: trim value before mapping (#4722)
frano-m 1269389
fix: strip obsolete prefix from HP term display names (#4722)
frano-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| #!/usr/bin/env npx ts-node | ||
| /** | ||
| * Fetch HP and OMIM term names from authoritative sources and generate a JS constant. | ||
| * | ||
| * - HP terms: parsed from the official hp.obo file (obophenotype GitHub) | ||
| * - OMIM terms: parsed from the HPO phenotype.hpoa annotations file | ||
| * - Term IDs: extracted live from the AnVIL Azul API | ||
| * | ||
| * Usage: npx ts-node scripts/lookup-diagnosis-terms.ts > site-config/anvil-cmg/dev/index/common/diagnosis.ts | ||
| */ | ||
|
|
||
| const AZUL_URL = | ||
| "https://service.explore.anvilproject.org/index/datasets?size=1&filters=%7B%7D"; | ||
| const HP_OBO_URL = | ||
| "https://raw.githubusercontent.com/obophenotype/human-phenotype-ontology/master/hp.obo"; | ||
frano-m marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const HPOA_URL = | ||
| "https://github.com/obophenotype/human-phenotype-ontology/releases/latest/download/phenotype.hpoa"; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any -- API response shape is dynamic | ||
| async function fetchJson(url: string): Promise<any> { | ||
| const resp = await fetch(url); | ||
| if (!resp.ok) throw new Error(`Failed to fetch ${url}: ${resp.status}`); | ||
| return resp.json(); | ||
| } | ||
|
|
||
| async function fetchText(url: string): Promise<string> { | ||
| const resp = await fetch(url, { redirect: "follow" }); | ||
| if (!resp.ok) throw new Error(`Failed to fetch ${url}: ${resp.status}`); | ||
| return resp.text(); | ||
| } | ||
|
|
||
| async function getTermIdsFromAzul(): Promise<{ | ||
| hpIds: Set<string>; | ||
| omimIds: Set<string>; | ||
| }> { | ||
| console.error("Fetching term IDs from AnVIL Azul API..."); | ||
| const data = await fetchJson(AZUL_URL); | ||
| const facets = data.termFacets ?? {}; | ||
|
|
||
| const hpIds = new Set<string>(); | ||
| const omimIds = new Set<string>(); | ||
|
|
||
| for (const key of ["diagnoses.disease", "diagnoses.phenotype"]) { | ||
| const terms = facets[key]?.terms ?? []; | ||
| for (const t of terms) { | ||
| const term: string | undefined = t.term; | ||
| if (!term) continue; | ||
| // Some entries have multiple IDs separated by semicolons | ||
| for (const part of term.split(";")) { | ||
| const trimmed = part.trim(); | ||
| if (trimmed.startsWith("HP:")) hpIds.add(trimmed); | ||
| else if (trimmed.startsWith("OMIM:")) omimIds.add(trimmed); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| console.error(` Found ${hpIds.size} HP terms, ${omimIds.size} OMIM terms`); | ||
| return { hpIds, omimIds }; | ||
| } | ||
|
|
||
| async function buildHpMap(hpIds: Set<string>): Promise<Map<string, string>> { | ||
| console.error("Downloading hp.obo..."); | ||
| const obo = await fetchText(HP_OBO_URL); | ||
|
|
||
| const hpNames = new Map<string, string>(); | ||
| let currentId: string | null = null; | ||
| let currentName: string | null = null; | ||
| let altIds: string[] = []; | ||
|
|
||
| const saveCurrent = (): void => { | ||
| if (currentId && currentName) { | ||
| if (hpIds.has(currentId)) hpNames.set(currentId, currentName); | ||
| for (const alt of altIds) { | ||
| if (hpIds.has(alt)) hpNames.set(alt, currentName); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| for (const line of obo.split("\n")) { | ||
| const trimmed = line.trim(); | ||
| if (trimmed === "[Term]") { | ||
| saveCurrent(); | ||
| currentId = null; | ||
| currentName = null; | ||
| altIds = []; | ||
| } else if (trimmed.startsWith("id: HP:")) { | ||
| currentId = trimmed.slice(4); | ||
| } else if (trimmed.startsWith("name: ") && currentId) { | ||
| currentName = trimmed.slice(6).replace(/^obsolete\s+/, ""); | ||
| } else if (trimmed.startsWith("alt_id: HP:")) { | ||
| altIds.push(trimmed.slice(8)); | ||
| } | ||
| } | ||
| saveCurrent(); | ||
|
|
||
| console.error(` Resolved ${hpNames.size}/${hpIds.size} HP terms`); | ||
| const missing = [...hpIds].filter((id) => !hpNames.has(id)); | ||
| if (missing.length) | ||
| console.error(` Missing HP terms: ${missing.sort().join(", ")}`); | ||
| return hpNames; | ||
| } | ||
|
|
||
| async function buildOmimMap( | ||
| omimIds: Set<string> | ||
| ): Promise<Map<string, string>> { | ||
| console.error("Downloading phenotype.hpoa..."); | ||
| const hpoa = await fetchText(HPOA_URL); | ||
|
|
||
| const omimNames = new Map<string, string>(); | ||
| for (const line of hpoa.split("\n")) { | ||
| if (line.startsWith("#") || line.startsWith("database_id")) continue; | ||
| const parts = line.split("\t"); | ||
| if (parts.length < 2) continue; | ||
| const dbId = parts[0].trim(); | ||
| const diseaseName = parts[1].trim(); | ||
| if (omimIds.has(dbId) && !omimNames.has(dbId)) { | ||
| omimNames.set(dbId, diseaseName); | ||
| } | ||
| } | ||
|
|
||
| console.error(` Resolved ${omimNames.size}/${omimIds.size} OMIM terms`); | ||
| const missing = [...omimIds].filter((id) => !omimNames.has(id)); | ||
| if (missing.length) | ||
| console.error(` Missing OMIM terms: ${missing.sort().join(", ")}`); | ||
| return omimNames; | ||
| } | ||
|
|
||
| function generateJs(mapping: Map<string, string>): string { | ||
| const lines: string[] = []; | ||
| lines.push("/**"); | ||
| lines.push( | ||
| " * Mapping of HP (Human Phenotype Ontology) and OMIM term IDs to their names." | ||
| ); | ||
| lines.push( | ||
| " * Auto-generated by scripts/lookup-diagnosis-terms.ts from authoritative sources:" | ||
| ); | ||
| lines.push( | ||
| " * - HP terms: hp.obo from obophenotype/human-phenotype-ontology" | ||
| ); | ||
| lines.push(" * - OMIM terms: phenotype.hpoa from HPO project"); | ||
| lines.push(" * - Term IDs: AnVIL Azul API (explore.anvilproject.org)"); | ||
| lines.push(" */"); | ||
| lines.push( | ||
| "export const DIAGNOSIS_DISPLAY_VALUE: Record<string, string> = {" | ||
| ); | ||
|
|
||
| const sortedKeys = [...mapping.keys()].sort(); | ||
| for (const termId of sortedKeys) { | ||
| const name = mapping.get(termId)!; | ||
| const escaped = name.replace(/\\/g, "\\\\").replace(/"/g, '\\"'); | ||
| lines.push(` "${termId}": "${escaped}",`); | ||
| } | ||
|
|
||
| lines.push("};"); | ||
| lines.push(""); | ||
| return lines.join("\n"); | ||
| } | ||
|
|
||
| async function main(): Promise<void> { | ||
| const { hpIds, omimIds } = await getTermIdsFromAzul(); | ||
| const [hpMap, omimMap] = await Promise.all([ | ||
| buildHpMap(hpIds), | ||
| buildOmimMap(omimIds), | ||
| ]); | ||
|
|
||
| const combined = new Map<string, string>([...hpMap, ...omimMap]); | ||
| const js = generateJs(combined); | ||
| process.stdout.write(js); | ||
|
|
||
| console.error( | ||
| `\nGenerated mapping for ${combined.size} terms (${hpMap.size} HP + ${omimMap.size} OMIM)` | ||
| ); | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| console.error(err); | ||
| process.exit(1); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.