|
| 1 | +var __create = Object.create; |
| 2 | +var __defProp = Object.defineProperty; |
| 3 | +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
| 4 | +var __getOwnPropNames = Object.getOwnPropertyNames; |
| 5 | +var __getProtoOf = Object.getPrototypeOf; |
| 6 | +var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 7 | +var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { |
| 8 | + get: (a, b) => (typeof require !== "undefined" ? require : a)[b] |
| 9 | +}) : x)(function(x) { |
| 10 | + if (typeof require !== "undefined") return require.apply(this, arguments); |
| 11 | + throw Error('Dynamic require of "' + x + '" is not supported'); |
| 12 | +}); |
| 13 | +var __commonJS = (cb, mod) => function __require2() { |
| 14 | + return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; |
| 15 | +}; |
| 16 | +var __copyProps = (to, from, except, desc) => { |
| 17 | + if (from && typeof from === "object" || typeof from === "function") { |
| 18 | + for (let key of __getOwnPropNames(from)) |
| 19 | + if (!__hasOwnProp.call(to, key) && key !== except) |
| 20 | + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
| 21 | + } |
| 22 | + return to; |
| 23 | +}; |
| 24 | +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( |
| 25 | + // If the importer is in node compatibility mode or this is not an ESM |
| 26 | + // file that has been converted to a CommonJS file using a Babel- |
| 27 | + // compatible transform (i.e. "__esModule" has not been set), then set |
| 28 | + // "default" to the CommonJS "module.exports" for node compatibility. |
| 29 | + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, |
| 30 | + mod |
| 31 | +)); |
| 32 | + |
| 33 | +// src/data/index.ts |
| 34 | +import { readFile } from "fs/promises"; |
| 35 | +import { join } from "path"; |
| 36 | +import { parse } from "csv-parse/sync"; |
| 37 | +var CSV = (txt) => parse(txt, { |
| 38 | + columns: true, |
| 39 | + skip_empty_lines: true, |
| 40 | + trim: true, |
| 41 | + bom: true, |
| 42 | + cast: (value, context) => { |
| 43 | + if (context.header) return value; |
| 44 | + if (["resistance_pct", "n_isolates"].includes(context.column)) { |
| 45 | + const num = parseFloat(value); |
| 46 | + return isNaN(num) ? null : num; |
| 47 | + } |
| 48 | + return value; |
| 49 | + } |
| 50 | +}); |
| 51 | +var csvCache = /* @__PURE__ */ new Map(); |
| 52 | +var loadCsv = (dir, file) => { |
| 53 | + const path = join(dir, file); |
| 54 | + if (!csvCache.has(path)) { |
| 55 | + const promise = readFile(path, "utf8").then(CSV).catch((err) => { |
| 56 | + console.error(`Error loading CSV file: ${file} in ${dir}`, err); |
| 57 | + return []; |
| 58 | + }); |
| 59 | + csvCache.set(path, promise); |
| 60 | + } |
| 61 | + return csvCache.get(path); |
| 62 | +}; |
| 63 | +var sharedDataPromise = null; |
| 64 | +var collectSynonyms = (row) => { |
| 65 | + const synonyms = /* @__PURE__ */ new Set(); |
| 66 | + const add = (s) => { |
| 67 | + if (!s) return; |
| 68 | + s.split(/[;,]/).forEach((part) => { |
| 69 | + const trimmed = part.trim(); |
| 70 | + if (trimmed) { |
| 71 | + synonyms.add(trimmed); |
| 72 | + const noDots = trimmed.replace(/\./g, ""); |
| 73 | + if (noDots !== trimmed) synonyms.add(noDots); |
| 74 | + } |
| 75 | + }); |
| 76 | + }; |
| 77 | + add(row.amr_code || row.id); |
| 78 | + for (const key in row) { |
| 79 | + if (key.startsWith("synonyms_") || key.startsWith("full_name_") || key.startsWith("short_name_") || key.startsWith("name_")) { |
| 80 | + add(row[key]); |
| 81 | + } |
| 82 | + } |
| 83 | + return Array.from(synonyms); |
| 84 | +}; |
| 85 | +function getSharedData(dir, files) { |
| 86 | + if (!sharedDataPromise) { |
| 87 | + sharedDataPromise = Promise.all([ |
| 88 | + loadCsv(dir, files.antibiotics), |
| 89 | + loadCsv(dir, files.organisms), |
| 90 | + loadCsv(dir, files.sources), |
| 91 | + loadCsv(dir, files.abxClasses), |
| 92 | + loadCsv(dir, files.orgClasses), |
| 93 | + loadCsv(dir, files.orgGroups) |
| 94 | + ]).then(([abx, org, rawSources, abxClasses, orgClasses, orgGroups]) => { |
| 95 | + const sources = rawSources.map((s) => ({ |
| 96 | + ...s, |
| 97 | + url: s.source_url |
| 98 | + })); |
| 99 | + const abxSyn2Id = mkSynMap(abx); |
| 100 | + const orgSyn2Id = mkSynMap(org); |
| 101 | + const orgClassesById = new Map(orgClasses.map((c) => [c.id, c])); |
| 102 | + const orgClassChildren = /* @__PURE__ */ new Map(); |
| 103 | + for (const c of orgClasses) { |
| 104 | + const parentId = c.parent_id || ""; |
| 105 | + if (!orgClassChildren.has(parentId)) { |
| 106 | + orgClassChildren.set(parentId, []); |
| 107 | + } |
| 108 | + orgClassChildren.get(parentId).push(c); |
| 109 | + } |
| 110 | + const classIdToRank = /* @__PURE__ */ new Map(); |
| 111 | + const traverse = (parentId, prefix) => { |
| 112 | + const children = orgClassChildren.get(parentId) ?? []; |
| 113 | + children.sort((a, b) => orgClasses.indexOf(a) - orgClasses.indexOf(b)); |
| 114 | + children.forEach((child, index) => { |
| 115 | + const rank = prefix ? `${prefix}.${(index + 1).toString().padStart(2, "0")}` : (index + 1).toString().padStart(2, "0"); |
| 116 | + classIdToRank.set(child.id, rank); |
| 117 | + traverse(child.id, rank); |
| 118 | + }); |
| 119 | + }; |
| 120 | + traverse("", ""); |
| 121 | + const orgIdToRank = /* @__PURE__ */ new Map(); |
| 122 | + for (const organism of org) { |
| 123 | + const rank = classIdToRank.get(organism.class_id) || "99"; |
| 124 | + orgIdToRank.set(organism.amr_code, rank); |
| 125 | + } |
| 126 | + const synonymToAllMembers = /* @__PURE__ */ new Map(); |
| 127 | + for (const abxClass of abxClasses) { |
| 128 | + const classId = abxClass.id; |
| 129 | + const members = abx.filter((a) => a.class === classId).map((a) => a.amr_code); |
| 130 | + if (members.length > 0) { |
| 131 | + const synonyms = collectSynonyms(abxClass); |
| 132 | + for (const syn of synonyms) { |
| 133 | + if (!synonymToAllMembers.has(syn)) { |
| 134 | + synonymToAllMembers.set(syn, []); |
| 135 | + } |
| 136 | + synonymToAllMembers.get(syn).push(...members); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + for (const [syn, members] of synonymToAllMembers.entries()) { |
| 141 | + const existingIds = abxSyn2Id.has(syn) ? abxSyn2Id.get(syn).split(",") : []; |
| 142 | + const allIds = [.../* @__PURE__ */ new Set([...existingIds, ...members])]; |
| 143 | + abxSyn2Id.set(syn, allIds.join(",")); |
| 144 | + } |
| 145 | + const classToAllDescendantOrgs = /* @__PURE__ */ new Map(); |
| 146 | + const getDescendantOrgs = (classId) => { |
| 147 | + if (classToAllDescendantOrgs.has(classId)) return classToAllDescendantOrgs.get(classId); |
| 148 | + const directOrgs = org.filter((o) => o.class_id === classId).map((o) => o.amr_code); |
| 149 | + const childClasses = orgClassChildren.get(classId) ?? []; |
| 150 | + const descendantOrgs = childClasses.flatMap((child) => getDescendantOrgs(child.id)); |
| 151 | + const allOrgs = [.../* @__PURE__ */ new Set([...directOrgs, ...descendantOrgs])]; |
| 152 | + classToAllDescendantOrgs.set(classId, allOrgs); |
| 153 | + return allOrgs; |
| 154 | + }; |
| 155 | + for (const classId of orgClassesById.keys()) { |
| 156 | + getDescendantOrgs(classId); |
| 157 | + } |
| 158 | + for (const orgClass of orgClasses) { |
| 159 | + const members = classToAllDescendantOrgs.get(orgClass.id); |
| 160 | + if (members && members.length > 0) { |
| 161 | + const synonyms = collectSynonyms(orgClass); |
| 162 | + for (const syn of synonyms) { |
| 163 | + orgSyn2Id.set(syn, members.join(",")); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + const groupIdToOrgIds = /* @__PURE__ */ new Map(); |
| 168 | + for (const organism of org) { |
| 169 | + const groupIdsStr = organism.groups; |
| 170 | + if (groupIdsStr) { |
| 171 | + const groupIds = groupIdsStr.split(";").map((id) => id.trim()).filter(Boolean); |
| 172 | + for (const groupId of groupIds) { |
| 173 | + if (!groupIdToOrgIds.has(groupId)) { |
| 174 | + groupIdToOrgIds.set(groupId, []); |
| 175 | + } |
| 176 | + groupIdToOrgIds.get(groupId).push(organism.amr_code); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + for (const group of orgGroups) { |
| 181 | + const members = groupIdToOrgIds.get(group.id); |
| 182 | + if (members && members.length > 0) { |
| 183 | + const synonyms = collectSynonyms(group); |
| 184 | + for (const syn of synonyms) { |
| 185 | + const existingIds = orgSyn2Id.has(syn) ? orgSyn2Id.get(syn).split(",") : []; |
| 186 | + const allIds = [.../* @__PURE__ */ new Set([...existingIds, ...members])]; |
| 187 | + orgSyn2Id.set(syn, allIds.join(",")); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + const allAbxIds = abx.filter((r) => r.class).map((r) => r.amr_code); |
| 192 | + const allOrgIds = org.filter((r) => r.class_id).map((r) => r.amr_code); |
| 193 | + const sourcesById = new Map( |
| 194 | + sources.map((s) => [s.id, { ...s, children: [] }]) |
| 195 | + ); |
| 196 | + const hierarchicalSources = []; |
| 197 | + for (const source of sourcesById.values()) { |
| 198 | + if (source.parent_id && sourcesById.has(source.parent_id)) { |
| 199 | + const parent = sourcesById.get(source.parent_id); |
| 200 | + parent.children.push(source); |
| 201 | + } else { |
| 202 | + hierarchicalSources.push(source); |
| 203 | + } |
| 204 | + } |
| 205 | + return { |
| 206 | + abx, |
| 207 | + org, |
| 208 | + sources, |
| 209 | + hierarchicalSources, |
| 210 | + abxSyn2Id, |
| 211 | + orgSyn2Id, |
| 212 | + allAbxIds, |
| 213 | + allOrgIds, |
| 214 | + orgClasses, |
| 215 | + orgIdToRank: Object.fromEntries(orgIdToRank) |
| 216 | + }; |
| 217 | + }); |
| 218 | + } |
| 219 | + return sharedDataPromise; |
| 220 | +} |
| 221 | +var getPathToSource = (sources, targetId) => { |
| 222 | + const sourcesById = new Map(sources.map((s) => [s.id, s])); |
| 223 | + const path = []; |
| 224 | + let currentId = targetId; |
| 225 | + while (currentId && sourcesById.has(currentId)) { |
| 226 | + const source = sourcesById.get(currentId); |
| 227 | + path.unshift(source); |
| 228 | + currentId = source.parent_id; |
| 229 | + } |
| 230 | + return path; |
| 231 | +}; |
| 232 | +var loadResistanceDataForSource = async (source, allSources, dataDir) => { |
| 233 | + const path = getPathToSource(allSources, source.id); |
| 234 | + if (path.length === 0) return []; |
| 235 | + const csvDataFrames = await Promise.all( |
| 236 | + path.map((s) => loadCsv(dataDir, s.source_file)) |
| 237 | + ); |
| 238 | + const allDataFrames = csvDataFrames.map( |
| 239 | + (rows, idx) => rows.map((row) => ({ ...row, source_id: path[idx].id })) |
| 240 | + ); |
| 241 | + const mergedData = /* @__PURE__ */ new Map(); |
| 242 | + for (const df of allDataFrames) { |
| 243 | + for (const row of df) { |
| 244 | + const key = `${row.antibiotic_id}-${row.organism_id}`; |
| 245 | + mergedData.set(key, row); |
| 246 | + } |
| 247 | + } |
| 248 | + return Array.from(mergedData.values()); |
| 249 | +}; |
| 250 | +var stripMarkdownLight = (s) => s.replace(/`{1,3}[\s\S]*?`{1,3}/g, " ").replace(/!\\\[[^\\]*\]\([^)]*\)/g, " ").replace(/\\[^\\]+\]\([^)]*\)/g, "$1").replace(/[*_~#>\/.,]+/g, " ").replace(/\s+/g, " ").trim(); |
| 251 | +var makeTokenRegex = (synRaw) => { |
| 252 | + const syn = synRaw.trim(); |
| 253 | + if (!syn) return null; |
| 254 | + let core = syn.replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replace(/\\\./g, "\\.?").replace(/\s+/g, "\\s+"); |
| 255 | + const W = "\\p{L}\\p{N}"; |
| 256 | + const pattern = `(?<![${W}])${core}(?![${W}])`; |
| 257 | + try { |
| 258 | + return new RegExp(pattern, "iu"); |
| 259 | + } catch { |
| 260 | + return new RegExp(`(^|[^${W}])(${core})(?=$|[^${W}])`, "iu"); |
| 261 | + } |
| 262 | +}; |
| 263 | +var selectDataSource = (src, sources) => { |
| 264 | + const getParentCount = (source, allSources) => { |
| 265 | + let count = 0; |
| 266 | + let current = source; |
| 267 | + const sourceMap = new Map(allSources.map((s) => [s.id, s])); |
| 268 | + while (current.parent_id && sourceMap.has(current.parent_id)) { |
| 269 | + count++; |
| 270 | + current = sourceMap.get(current.parent_id); |
| 271 | + } |
| 272 | + return count; |
| 273 | + }; |
| 274 | + const sort = (sourcesToSort, priority) => { |
| 275 | + return sourcesToSort.sort((a, b) => { |
| 276 | + const parentCountA = getParentCount(a, sources); |
| 277 | + const parentCountB = getParentCount(b, sources); |
| 278 | + const yearA = a.year; |
| 279 | + const yearB = b.year; |
| 280 | + if (priority === "parents") { |
| 281 | + if (parentCountA !== parentCountB) return parentCountB - parentCountA; |
| 282 | + if (yearA !== yearB) return yearB - yearA; |
| 283 | + } else { |
| 284 | + if (yearA !== yearB) return yearB - yearA; |
| 285 | + if (parentCountA !== parentCountB) return parentCountB - parentCountA; |
| 286 | + } |
| 287 | + return sources.indexOf(a) - sources.indexOf(b); |
| 288 | + }); |
| 289 | + }; |
| 290 | + if (!src) { |
| 291 | + return sort([...sources], "year")[0]; |
| 292 | + } |
| 293 | + const filteredSources = sources.filter( |
| 294 | + (s) => s.id.toLowerCase().includes(src.toLowerCase()) || s.name_de.toLowerCase().includes(src.toLowerCase()) || s.year.toString().includes(src) |
| 295 | + ); |
| 296 | + if (filteredSources.length === 0) { |
| 297 | + return sort([...sources], "year")[0]; |
| 298 | + } |
| 299 | + return sort(filteredSources, "parents")[0]; |
| 300 | +}; |
| 301 | +var mkSynMap = (rows) => rows.reduce((m, r) => { |
| 302 | + const id = r.amr_code || r.id; |
| 303 | + if (!id) return m; |
| 304 | + const synonyms = collectSynonyms(r); |
| 305 | + for (const syn of synonyms) { |
| 306 | + m.set(syn, id); |
| 307 | + } |
| 308 | + return m; |
| 309 | +}, /* @__PURE__ */ new Map()); |
| 310 | +var getLowerCaseSynMap = /* @__PURE__ */ (() => { |
| 311 | + let cache = null; |
| 312 | + let originalMap = null; |
| 313 | + return (synMap) => { |
| 314 | + if (cache && originalMap === synMap) { |
| 315 | + return cache; |
| 316 | + } |
| 317 | + const lowerCaseMap = /* @__PURE__ */ new Map(); |
| 318 | + synMap.forEach((value, key) => { |
| 319 | + lowerCaseMap.set(key.toLowerCase(), value); |
| 320 | + }); |
| 321 | + cache = lowerCaseMap; |
| 322 | + originalMap = synMap; |
| 323 | + return lowerCaseMap; |
| 324 | + }; |
| 325 | +})(); |
| 326 | +var resolveIds = (param, allIds, synMap, pageText) => { |
| 327 | + if (!param) return { resolved: [], unresolved: [] }; |
| 328 | + const resolved = /* @__PURE__ */ new Set(); |
| 329 | + const requestedTokens = param.split(",").map((t) => t.trim()).filter(Boolean); |
| 330 | + const lowerCaseSynMap = getLowerCaseSynMap(synMap); |
| 331 | + for (const token of requestedTokens) { |
| 332 | + if (token === "auto") { |
| 333 | + const text = stripMarkdownLight(pageText); |
| 334 | + for (const [syn, idOrIds] of synMap.entries()) { |
| 335 | + const strippedSyn = stripMarkdownLight(syn); |
| 336 | + const rx = makeTokenRegex(strippedSyn); |
| 337 | + if (!rx) continue; |
| 338 | + if (rx.test(text)) { |
| 339 | + idOrIds.split(",").forEach((id) => resolved.add(id)); |
| 340 | + } else { |
| 341 | + const synNoDots = syn.replace(/\./g, ""); |
| 342 | + if (synNoDots !== syn) { |
| 343 | + const strippedSynNoDots = stripMarkdownLight(synNoDots); |
| 344 | + const rx2 = makeTokenRegex(strippedSynNoDots); |
| 345 | + if (rx2 && rx2.test(text)) { |
| 346 | + idOrIds.split(",").forEach((id) => resolved.add(id)); |
| 347 | + } |
| 348 | + } |
| 349 | + } |
| 350 | + } |
| 351 | + } else if (token === "all") { |
| 352 | + allIds.forEach((id) => resolved.add(id)); |
| 353 | + } else { |
| 354 | + const lowerToken = token.toLowerCase(); |
| 355 | + const idOrIds = lowerCaseSynMap.get(lowerToken) ?? token.toUpperCase(); |
| 356 | + idOrIds.split(",").forEach((id) => { |
| 357 | + if (allIds.includes(id)) { |
| 358 | + resolved.add(id); |
| 359 | + } |
| 360 | + }); |
| 361 | + } |
| 362 | + } |
| 363 | + if (resolved.size === 0) { |
| 364 | + const unresolved2 = requestedTokens.filter((t) => t !== "all" && t !== "auto"); |
| 365 | + if (unresolved2.length === 0 && requestedTokens.includes("auto")) { |
| 366 | + unresolved2.push("auto"); |
| 367 | + } |
| 368 | + return { resolved: [], unresolved: unresolved2 }; |
| 369 | + } |
| 370 | + const finalResolved = Array.from(resolved); |
| 371 | + const unresolved = requestedTokens.filter((token) => { |
| 372 | + if (token === "auto" || token === "all") return false; |
| 373 | + const lowerToken = token.toLowerCase(); |
| 374 | + const idOrIds = lowerCaseSynMap.get(lowerToken) ?? token.toUpperCase(); |
| 375 | + return !idOrIds.split(",").some((id) => finalResolved.includes(id)); |
| 376 | + }); |
| 377 | + return { resolved: finalResolved, unresolved }; |
| 378 | +}; |
| 379 | + |
| 380 | +export { |
| 381 | + __require, |
| 382 | + __commonJS, |
| 383 | + __toESM, |
| 384 | + getSharedData, |
| 385 | + loadResistanceDataForSource, |
| 386 | + selectDataSource, |
| 387 | + resolveIds |
| 388 | +}; |
0 commit comments