dhd-thesaurus 0.1.0

DHD Diagnosethesaurus and Verrichtingenthesaurus delivery (uitleverformaat 5.0) reader
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! The DHD Diagnosethesaurus and Verrichtingenthesaurus deliveries
//! ("Uitleverformaat 5.0", <https://www.dhd.nl/assets/uploads/Uitleverformaat-Thesauri-5.0-v1.0.pdf>).
//!
//! A delivery is a set of UTF-8 CSV tables. `ThesaurusConcept` gives the
//! concepts (`ConceptID`, `TypeConcept`, the flags, the validity dates, a
//! LOINC code for laboratory procedures), `ThesaurusTerm` their terms by type
//! and language (the preferred term, synonyms, the patient-friendly terms, the
//! fully specified names with their SNOMED CT identifier, search terms),
//! `ThesaurusConceptRelaties` replacements and splits, `ThesaurusConceptRol`
//! the roles per specialism group, `Parapluterm` umbrella terms, and the
//! `Afleiding*` and `CodeMapping` tables the derivations to ICD-10, DBC, ZA,
//! and other code systems. The reader turns a delivery into a flat
//! [`Classification`] (no hierarchy; the relations are properties) and the
//! SNOMED CT and ICD-10 links into pairs the build writes as concept maps.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use ::classification::{Class, Classification, PREFERRED, Rubric, collapse};

/// The system URI of the thesauri: the DHD concept identifier OID
/// (<https://www.dhd.nl/producten-diensten/diagnosethesaurus>).
pub const SYSTEM: &str = "urn:oid:2.16.840.1.113883.2.4.3.120.5.1";
/// The Dutch language tag the terms carry.
pub const DUTCH: &str = "nl-NL";
/// The rubric kinds that are designations, the preferred term first
/// (`TypeTerm` values of the delivery, in the reader's spelling).
pub const DESIGNATION_KINDS: [&str; 6] = [PREFERRED, "synonym", "pvo", "pvt", "fsn", "search"];
/// The property that carries the SNOMED CT identifier of a concept.
pub const SNOMED: &str = "snomed";
/// The property that carries an ICD-10 derivation.
pub const ICD10: &str = "icd10";
/// The property that carries a DBC derivation (`DBC_ID (specialism)`).
pub const DBC: &str = "dbc";
/// The property that carries a ZA derivation.
pub const ZA: &str = "za";
/// The property that carries a role (`name=value [group]`).
pub const ROLE: &str = "role";
/// The property that carries a code mapping (`system: code`).
pub const MAPPING: &str = "mapping";
/// The property naming the concepts that replace an ended one.
pub const REPLACED_BY: &str = "replaced-by";
/// The property naming the concepts an ended one was split into.
pub const SPLIT_INTO: &str = "split-into";
/// The property naming the umbrella term a concept falls under.
pub const UMBRELLA: &str = "umbrella";
/// The end date that means "no end" in a delivery.
const NO_END: &str = "2099";

/// A failure to read a delivery.
#[derive(Debug, thiserror::Error)]
pub enum DhdError {
    /// A directory or file does not read.
    #[error("cannot read {path}")]
    Io {
        /// The path.
        path: PathBuf,
        /// The cause.
        #[source]
        source: std::io::Error,
    },
    /// A table the reader needs is not in the delivery.
    #[error("no `{table}` table under {root}")]
    Missing {
        /// The delivery directory.
        root: PathBuf,
        /// The table name.
        table: &'static str,
    },
    /// A table does not parse.
    #[error("cannot parse {path}")]
    Csv {
        /// The path.
        path: PathBuf,
        /// The cause.
        #[source]
        source: csv::Error,
    },
    /// A table lacks a column.
    #[error("{path} has no column `{column}`")]
    Column {
        /// The path.
        path: PathBuf,
        /// The column.
        column: &'static str,
    },
}

/// A delivery: its tables, found by name under a directory.
#[derive(Debug, Clone)]
pub struct Delivery {
    root: PathBuf,
    files: Vec<PathBuf>,
    /// The date the file names carry (`YYYYMMDD`), when they do.
    date: Option<String>,
    /// The thesaurus version the zip or directory name carries (`2.27`).
    version: Option<String>,
}

/// One row of a table, by column name.
#[derive(Debug, Clone, Default)]
pub struct Row {
    fields: BTreeMap<String, String>,
}

impl Row {
    /// The trimmed value of `column`, `None` when absent or empty.
    #[must_use]
    pub fn get(&self, column: &str) -> Option<&str> {
        self.fields
            .get(&column.to_lowercase())
            .map(String::as_str)
            .map(str::trim)
            .filter(|v| !v.is_empty())
    }

    /// Whether the row is current at `date`: its `Einddatum` is absent, in
    /// 2099, or not before `date`.
    #[must_use]
    pub fn current_at(&self, date: Option<&str>) -> bool {
        match (self.get("Einddatum"), date) {
            (Some(end), Some(date)) if !end.starts_with(NO_END) => end >= date,
            _ => true,
        }
    }
}

/// What a delivery yields: the thesaurus and its outward links.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Thesaurus {
    /// The concepts with their terms and properties.
    pub classification: Classification,
    /// `(concept id, SNOMED CT id)` from the fully specified names.
    pub snomed: Vec<(String, String)>,
    /// `(concept id, ICD-10 codes in order)` from the ICD-10 derivations.
    pub icd10: Vec<(String, Vec<String>)>,
}

fn version_from_name(name: &str) -> Option<String> {
    let mut parts = name.split('_');
    while let Some(part) = parts.next() {
        if part.ends_with("thesaurus") {
            let version = parts.next()?;
            return (version.chars().all(|c| c.is_ascii_digit() || c == '.')
                && !version.is_empty())
            .then(|| version.to_owned());
        }
    }
    None
}

fn date_from_name(name: &str) -> Option<String> {
    let head = name.split('_').next()?;
    (head.len() == 8 && head.bytes().all(|b| b.is_ascii_digit())).then(|| head.to_owned())
}

impl Delivery {
    /// Opens the delivery under `root` (the unpacked zip).
    ///
    /// # Errors
    ///
    /// Returns [`DhdError`] when the directory does not read or holds no
    /// `ThesaurusConcept` table.
    pub fn open(root: &Path) -> Result<Self, DhdError> {
        let mut files = Vec::new();
        walk(root, &mut files)?;
        files.sort();
        let names: Vec<String> = files
            .iter()
            .filter_map(|p| p.file_name().and_then(|n| n.to_str()).map(str::to_owned))
            .collect();
        let date = names.iter().find_map(|n| date_from_name(n));
        let version = root
            .file_name()
            .and_then(|n| n.to_str())
            .and_then(version_from_name);
        let delivery = Self {
            root: root.to_path_buf(),
            files,
            date,
            version,
        };
        delivery.table("ThesaurusConcept")?;
        Ok(delivery)
    }

    /// The delivery date the file names carry.
    #[must_use]
    pub fn date(&self) -> Option<&str> {
        self.date.as_deref()
    }

    /// The thesaurus version the directory name carries.
    #[must_use]
    pub fn version(&self) -> Option<&str> {
        self.version.as_deref()
    }

    /// The file of `table` (`…_ThesaurusConcept.csv`).
    ///
    /// # Errors
    ///
    /// Returns [`DhdError::Missing`] when the delivery has no such table.
    pub fn table(&self, table: &'static str) -> Result<PathBuf, DhdError> {
        self.optional(table).ok_or_else(|| DhdError::Missing {
            root: self.root.clone(),
            table,
        })
    }

    /// The file of `table` when the delivery has it.
    #[must_use]
    pub fn optional(&self, table: &str) -> Option<PathBuf> {
        let suffix = format!("_{}.csv", table.to_lowercase());
        self.files
            .iter()
            .find(|p| {
                p.file_name()
                    .and_then(|n| n.to_str())
                    .is_some_and(|n| n.to_lowercase().ends_with(&suffix))
            })
            .cloned()
    }

    /// The rows of `table`, or none when the delivery lacks it.
    ///
    /// # Errors
    ///
    /// Returns [`DhdError`] when the file does not read or parse.
    pub fn rows(&self, table: &str) -> Result<Vec<Row>, DhdError> {
        let Some(path) = self.optional(table) else {
            return Ok(Vec::new());
        };
        read_rows(&path)
    }
}

fn walk(root: &Path, out: &mut Vec<PathBuf>) -> Result<(), DhdError> {
    let io = |source| DhdError::Io {
        path: root.to_path_buf(),
        source,
    };
    for entry in std::fs::read_dir(root).map_err(io)? {
        let path = entry.map_err(io)?.path();
        if path.is_dir() {
            walk(&path, out)?;
        } else {
            out.push(path);
        }
    }
    Ok(())
}

/// Reads a table: UTF-8, a header row, quoted fields.
///
/// # Errors
///
/// Returns [`DhdError`] when the file does not read or parse.
pub fn read_rows(path: &Path) -> Result<Vec<Row>, DhdError> {
    let text = std::fs::read_to_string(path).map_err(|source| DhdError::Io {
        path: path.to_path_buf(),
        source,
    })?;
    let text = text.strip_prefix('\u{feff}').unwrap_or(&text);
    let delimiter = text
        .lines()
        .next()
        .and_then(|h| b",;\t".iter().copied().find(|d| h.contains(char::from(*d))))
        .unwrap_or(b',');
    let mut reader = csv::ReaderBuilder::new()
        .delimiter(delimiter)
        .flexible(true)
        .from_reader(text.as_bytes());
    let csv_error = |source| DhdError::Csv {
        path: path.to_path_buf(),
        source,
    };
    let headers: Vec<String> = reader
        .headers()
        .map_err(csv_error)?
        .iter()
        .map(|h| h.trim().to_lowercase())
        .collect();
    let mut rows = Vec::new();
    for record in reader.records() {
        let record = record.map_err(csv_error)?;
        let mut fields = BTreeMap::new();
        for (i, header) in headers.iter().enumerate() {
            if let Some(value) = record.get(i) {
                fields.insert(header.clone(), value.to_owned());
            }
        }
        rows.push(Row { fields });
    }
    Ok(rows)
}

/// The rubric kind of a `TypeTerm` value.
fn term_kind(type_term: &str) -> Option<&'static str> {
    match type_term.to_lowercase().as_str() {
        "voorkeursterm" => Some(PREFERRED),
        "synoniem" => Some("synonym"),
        "pvo" => Some("pvo"),
        "pvt" => Some("pvt"),
        "fsn" => Some("fsn"),
        "zoekterm" => Some("search"),
        _ => None,
    }
}

fn required<'a>(row: &'a Row, column: &'static str, path: &Path) -> Result<&'a str, DhdError> {
    row.get(column).ok_or_else(|| DhdError::Column {
        path: path.to_path_buf(),
        column,
    })
}

/// Reads the thesaurus of the delivery under `root`.
///
/// Concepts and terms ended before the delivery date are skipped as terms
/// and served inactive as concepts; the relations and derivations become
/// properties; the SNOMED CT identifiers of the fully specified names and the
/// ICD-10 derivations are also returned as pairs for the concept maps.
///
/// # Errors
///
/// Returns [`DhdError`] when a table does not read or lacks a column.
#[expect(
    clippy::too_many_lines,
    reason = "one delivery table after another, read top to bottom"
)]
pub fn read(root: &Path, version: Option<&str>) -> Result<Thesaurus, DhdError> {
    let delivery = Delivery::open(root)?;
    let date = delivery.date();
    let concept_table = delivery.table("ThesaurusConcept")?;
    let mut classes: BTreeMap<String, Class> = BTreeMap::new();
    let mut kinds: Vec<String> = Vec::new();
    for row in read_rows(&concept_table)? {
        let id = required(&row, "ConceptID", &concept_table)?.to_owned();
        let kind = required(&row, "TypeConcept", &concept_table)?.to_owned();
        if !kinds.contains(&kind) {
            kinds.push(kind.clone());
        }
        let mut rubrics = Vec::new();
        for (column, name) in [
            ("Complicatie", "complication"),
            ("GebruiktImplantaat", "implant"),
            ("Gradatie", "grading"),
            ("Lateraliteit", "laterality"),
            ("LOINCCode", "loinc"),
        ] {
            if let Some(value) = row.get(column) {
                rubrics.push(Rubric {
                    kind: name.to_owned(),
                    language: DUTCH.to_owned(),
                    text: value.to_owned(),
                });
            }
        }
        classes.insert(
            id.clone(),
            Class {
                code: id,
                kind,
                parent: None,
                usage: None,
                valid: None,
                active: row.current_at(date),
                rubrics,
            },
        );
    }
    let mut snomed: BTreeMap<String, String> = BTreeMap::new();
    for row in delivery.rows("ThesaurusTerm")? {
        if !row.current_at(date) {
            continue;
        }
        let (Some(id), Some(text), Some(type_term)) = (
            row.get("ConceptID"),
            row.get("Omschrijving"),
            row.get("TypeTerm"),
        ) else {
            continue;
        };
        let Some(class) = classes.get_mut(id) else {
            continue;
        };
        let Some(kind) = term_kind(type_term) else {
            continue;
        };
        class.rubrics.push(Rubric {
            kind: kind.to_owned(),
            language: row.get("TaalCode").unwrap_or(DUTCH).to_owned(),
            text: collapse(text),
        });
        if kind == "fsn"
            && let Some(sctid) = row.get("SnomedID")
        {
            snomed
                .entry(id.to_owned())
                .or_insert_with(|| sctid.to_owned());
            if !class
                .rubrics
                .iter()
                .any(|r| r.kind == SNOMED && r.text == sctid)
            {
                class.rubrics.push(Rubric {
                    kind: SNOMED.to_owned(),
                    language: DUTCH.to_owned(),
                    text: sctid.to_owned(),
                });
            }
        }
    }
    for row in delivery.rows("ThesaurusConceptRelaties")? {
        let (Some(first), Some(second), Some(kind)) = (
            row.get("ConceptID1"),
            row.get("ConceptID2"),
            row.get("TypeRelatie"),
        ) else {
            continue;
        };
        let name = match kind.to_lowercase().as_str() {
            "vervanging" => REPLACED_BY,
            "splitsing" => SPLIT_INTO,
            _ => continue,
        };
        if let Some(class) = classes.get_mut(first) {
            class.rubrics.push(Rubric {
                kind: name.to_owned(),
                language: DUTCH.to_owned(),
                text: second.to_owned(),
            });
        }
    }
    for row in delivery.rows("Parapluterm")? {
        if !row.current_at(date) {
            continue;
        }
        if let (Some(umbrella), Some(member)) = (row.get("ConceptID1"), row.get("ConceptID2"))
            && let Some(class) = classes.get_mut(member)
        {
            class.rubrics.push(Rubric {
                kind: UMBRELLA.to_owned(),
                language: DUTCH.to_owned(),
                text: umbrella.to_owned(),
            });
        }
    }
    for row in delivery.rows("ThesaurusConceptRol")? {
        if !row.current_at(date) {
            continue;
        }
        if let (Some(id), Some(name), Some(value)) = (
            row.get("ConceptID"),
            row.get("Rolnaam"),
            row.get("Rolwaarde"),
        ) && let Some(class) = classes.get_mut(id)
        {
            let group = row
                .get("SpecialismeGroepCode")
                .map(|g| format!(" [{g}]"))
                .unwrap_or_default();
            class.rubrics.push(Rubric {
                kind: ROLE.to_owned(),
                language: DUTCH.to_owned(),
                text: format!("{name}={value}{group}"),
            });
        }
    }
    let mut icd10: BTreeMap<String, Vec<(u32, String)>> = BTreeMap::new();
    for row in delivery.rows("AfleidingICD10")? {
        if !row.current_at(date) {
            continue;
        }
        if let (Some(id), Some(code)) = (row.get("ConceptID"), row.get("ICD10"))
            && let Some(class) = classes.get_mut(id)
        {
            let order = row
                .get("Volgnummer")
                .and_then(|v| v.parse().ok())
                .unwrap_or(u32::MAX);
            icd10
                .entry(id.to_owned())
                .or_default()
                .push((order, code.to_owned()));
            class.rubrics.push(Rubric {
                kind: ICD10.to_owned(),
                language: DUTCH.to_owned(),
                text: code.to_owned(),
            });
        }
    }
    for (table, kind, code_column) in [
        ("AfleidingDBC", DBC, "DBC_ID"),
        ("AfleidingZA", ZA, "ZA_Code"),
    ] {
        for row in delivery.rows(table)? {
            if !row.current_at(date) {
                continue;
            }
            if let (Some(id), Some(code)) = (row.get("ConceptID"), row.get(code_column))
                && let Some(class) = classes.get_mut(id)
            {
                let specialism = row
                    .get("SpecialismeCode")
                    .map(|s| format!(" ({s})"))
                    .unwrap_or_default();
                class.rubrics.push(Rubric {
                    kind: kind.to_owned(),
                    language: DUTCH.to_owned(),
                    text: format!("{code}{specialism}"),
                });
            }
        }
    }
    for row in delivery.rows("CodeMapping")? {
        if !row.current_at(date) {
            continue;
        }
        if let (Some(id), Some(system), Some(code)) = (
            row.get("ConceptID"),
            row.get("Codestelsel"),
            row.get("Code"),
        ) && let Some(class) = classes.get_mut(id)
        {
            class.rubrics.push(Rubric {
                kind: MAPPING.to_owned(),
                language: DUTCH.to_owned(),
                text: format!("{system}: {code}"),
            });
        }
    }
    let icd10 = icd10
        .into_iter()
        .map(|(id, mut codes)| {
            codes.sort();
            (id, codes.into_iter().map(|(_, c)| c).collect())
        })
        .collect();
    let version = version
        .map(str::to_owned)
        .or_else(|| delivery.version().map(str::to_owned))
        .or_else(|| date.map(str::to_owned));
    let procedures = kinds.iter().any(|k| k.eq_ignore_ascii_case("Verrichting"));
    Ok(Thesaurus {
        classification: Classification {
            name: String::from(if procedures {
                "Verrichtingenthesaurus"
            } else {
                "Diagnosethesaurus"
            }),
            title: String::from(if procedures {
                "DHD Verrichtingenthesaurus"
            } else {
                "DHD Diagnosethesaurus"
            }),
            version,
            language: String::from("nl"),
            kinds,
            classes: classes.into_values().collect(),
            designation_kinds: DESIGNATION_KINDS.iter().map(|k| (*k).to_owned()).collect(),
            hierarchy: None,
        },
        snomed: snomed.into_iter().collect(),
        icd10,
    })
}

#[cfg(test)]
mod tests {
    use super::{Row, date_from_name, term_kind, version_from_name};

    #[test]
    fn names_carry_the_date_and_the_version() {
        assert_eq!(
            version_from_name("20170824_113250_Diagnosethesaurus_2.27_uitleverformaat_5.0")
                .as_deref(),
            Some("2.27")
        );
        assert_eq!(version_from_name("random"), None);
        assert_eq!(
            date_from_name("20221212_140720_uitleverformaat5.0_ThesaurusConcept.csv").as_deref(),
            Some("20221212")
        );
        assert_eq!(term_kind("Voorkeursterm"), Some("preferred"));
        assert_eq!(term_kind("Zoekterm"), Some("search"));
        assert_eq!(term_kind("other"), None);
        let mut row = Row::default();
        row.fields
            .insert(String::from("einddatum"), String::from("20990101"));
        assert!(row.current_at(Some("20250101")));
        row.fields
            .insert(String::from("einddatum"), String::from("20240101"));
        assert!(!row.current_at(Some("20250101")));
        assert!(row.current_at(None));
    }
}