|
| 1 | +const https = require('https'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const VALUESET_URL = 'https://fhir.ehdsi.eu/laboratory/ValueSet-eHDSILabTechniqueWithExceptions.html'; |
| 6 | +const OUTPUT_FILE = path.join(__dirname, '../src/assets/data/ehdsi-lab-technique-with-exceptions.json'); |
| 7 | + |
| 8 | +/** |
| 9 | + * Download HTML content from URL |
| 10 | + */ |
| 11 | +function downloadHTML(url) { |
| 12 | + return new Promise((resolve, reject) => { |
| 13 | + https.get(url, (res) => { |
| 14 | + let data = ''; |
| 15 | + |
| 16 | + res.on('data', (chunk) => { |
| 17 | + data += chunk; |
| 18 | + }); |
| 19 | + |
| 20 | + res.on('end', () => { |
| 21 | + resolve(data); |
| 22 | + }); |
| 23 | + }).on('error', (err) => { |
| 24 | + reject(err); |
| 25 | + }); |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Parse HTML and extract codes from the expansion table |
| 31 | + */ |
| 32 | +function parseValueSetCodes(html) { |
| 33 | + const codes = []; |
| 34 | + |
| 35 | + // Extract table rows - look for the expansion table |
| 36 | + const tableRegex = /<table[^>]*>([\s\S]*?)<\/table>/gi; |
| 37 | + const tables = html.match(tableRegex) || []; |
| 38 | + |
| 39 | + for (const table of tables) { |
| 40 | + // Check if this table contains codes (has "snomed.info" or "terminology.hl7.org" in it) |
| 41 | + if (!table.includes('snomed.info') && !table.includes('terminology.hl7.org')) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + // Extract rows |
| 46 | + const rowRegex = /<tr[^>]*>([\s\S]*?)<\/tr>/gi; |
| 47 | + let match; |
| 48 | + let isHeader = true; |
| 49 | + |
| 50 | + while ((match = rowRegex.exec(table)) !== null) { |
| 51 | + const row = match[1]; |
| 52 | + |
| 53 | + // Skip header row |
| 54 | + if (isHeader) { |
| 55 | + isHeader = false; |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + // Extract cells |
| 60 | + const cellRegex = /<td[^>]*>([\s\S]*?)<\/td>/gi; |
| 61 | + const cells = []; |
| 62 | + let cellMatch; |
| 63 | + |
| 64 | + while ((cellMatch = cellRegex.exec(row)) !== null) { |
| 65 | + // Remove HTML tags and decode entities |
| 66 | + let cellContent = cellMatch[1] |
| 67 | + .replace(/<[^>]+>/g, '') |
| 68 | + .replace(/ /g, ' ') |
| 69 | + .replace(/&/g, '&') |
| 70 | + .replace(/</g, '<') |
| 71 | + .replace(/>/g, '>') |
| 72 | + .replace(/"/g, '"') |
| 73 | + .replace(/'/g, "'") |
| 74 | + .replace(/\s+/g, ' ') |
| 75 | + .trim(); |
| 76 | + |
| 77 | + cells.push(cellContent); |
| 78 | + } |
| 79 | + |
| 80 | + // Expected format: [Code, System, Display, Definition?] |
| 81 | + if (cells.length >= 3) { |
| 82 | + const code = cells[0].trim(); |
| 83 | + const system = cells[1].trim(); |
| 84 | + let display = cells[2].trim(); |
| 85 | + |
| 86 | + // Simplify NullFlavor displays |
| 87 | + if (system === 'http://terminology.hl7.org/CodeSystem/v3-NullFlavor') { |
| 88 | + if (code === 'OTH') { |
| 89 | + display = 'Other'; |
| 90 | + } else if (code === 'UNC') { |
| 91 | + display = 'Unencoded'; |
| 92 | + } else if (code === 'UNK') { |
| 93 | + display = 'Unknown'; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Only process if we have valid code, system, and display |
| 98 | + if (code && system && display) { |
| 99 | + codes.push({ |
| 100 | + code: code, |
| 101 | + display: display, |
| 102 | + system: system |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // If we found codes in this table, break (we only need the expansion table) |
| 109 | + if (codes.length > 0) { |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return codes; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Main function |
| 119 | + */ |
| 120 | +async function main() { |
| 121 | + try { |
| 122 | + console.log('Downloading eHDSI Laboratory Technique with exceptions from:', VALUESET_URL); |
| 123 | + const html = await downloadHTML(VALUESET_URL); |
| 124 | + |
| 125 | + console.log('Parsing HTML...'); |
| 126 | + const codes = parseValueSetCodes(html); |
| 127 | + |
| 128 | + if (codes.length === 0) { |
| 129 | + throw new Error('No codes found in HTML. The page structure may have changed.'); |
| 130 | + } |
| 131 | + |
| 132 | + console.log(`Found ${codes.length} codes`); |
| 133 | + |
| 134 | + // Sort by display name for better UX |
| 135 | + codes.sort((a, b) => a.display.localeCompare(b.display)); |
| 136 | + |
| 137 | + // Create output structure similar to FHIR ValueSet expansion |
| 138 | + const output = { |
| 139 | + resourceType: 'ValueSet', |
| 140 | + url: 'http://fhir.ehdsi.eu/laboratory/ValueSet/eHDSILabTechniqueWithExceptions', |
| 141 | + name: 'EHDSILabTechniqueWithExceptions', |
| 142 | + title: 'eHDSI Laboratory Technique with exceptions', |
| 143 | + status: 'draft', |
| 144 | + experimental: true, |
| 145 | + date: new Date().toISOString(), |
| 146 | + description: 'The Value Set is used to code laboratory techniques for result measurements and includes exceptional values. It is defined as the union of: (a) eHDSI Laboratory Technique (b) eHDSI Exceptional Value', |
| 147 | + expansion: { |
| 148 | + timestamp: new Date().toISOString(), |
| 149 | + contains: codes.map(code => ({ |
| 150 | + system: code.system, |
| 151 | + code: code.code, |
| 152 | + display: code.display |
| 153 | + })) |
| 154 | + } |
| 155 | + }; |
| 156 | + |
| 157 | + // Ensure output directory exists |
| 158 | + const outputDir = path.dirname(OUTPUT_FILE); |
| 159 | + if (!fs.existsSync(outputDir)) { |
| 160 | + fs.mkdirSync(outputDir, { recursive: true }); |
| 161 | + } |
| 162 | + |
| 163 | + // Write to file |
| 164 | + fs.writeFileSync(OUTPUT_FILE, JSON.stringify(output, null, 2), 'utf8'); |
| 165 | + |
| 166 | + console.log(`✅ Successfully saved ${codes.length} codes to: ${OUTPUT_FILE}`); |
| 167 | + console.log(` Codes:`, codes.map(c => `${c.code} - ${c.display}`).join(', ')); |
| 168 | + |
| 169 | + } catch (error) { |
| 170 | + console.error('❌ Error:', error.message); |
| 171 | + process.exit(1); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +// Run the script |
| 176 | +main(); |
0 commit comments