@dcmjs-org/fhir 1.0.0-beta.4 → 1.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1044 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +1023 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +19 -8
- package/src/documentReference.js +0 -169
- package/src/helpers.js +0 -270
- package/src/imagingStudy.js +0 -288
- package/src/index.js +0 -184
- package/src/patient.js +0 -71
- package/src/patientToDataset.js +0 -80
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1044 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@dcmjs-org/core');
|
|
4
|
+
var encapsulated = require('@dcmjs-org/encapsulated');
|
|
5
|
+
|
|
6
|
+
// packages/fhir/src/helpers.js
|
|
7
|
+
//
|
|
8
|
+
// Shared DICOM → FHIR value mappers. Inputs are values from a NATURALIZED
|
|
9
|
+
// dcmjs dataset (DicomMetaDictionary.naturalizeDataset): keyword keys,
|
|
10
|
+
// single-value arrays collapsed to scalars, PN values as [{ Alphabetic }],
|
|
11
|
+
// IS values as numbers. Helpers are permissive about input shape
|
|
12
|
+
// (string / number / array / PN object) and strict about output — absent
|
|
13
|
+
// or unparseable inputs return null, never partial garbage.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
const DICOM_UID_SYSTEM = "urn:dicom:uid";
|
|
17
|
+
const DICOM_MODALITY_SYSTEM =
|
|
18
|
+
"http://dicom.nema.org/resources/ontology/DCM";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Prefix a DICOM UID with urn:oid: for FHIR identifier/uid slots.
|
|
22
|
+
*/
|
|
23
|
+
function uidToUrn(uid) {
|
|
24
|
+
const clean = core.toStringFirst(uid);
|
|
25
|
+
if (!clean) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
return clean.startsWith("urn:oid:") ? clean : `urn:oid:${clean}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* DICOM DA (YYYYMMDD) + optional TM (HHMMSS.ffffff) → a FHIR dateTime.
|
|
33
|
+
*
|
|
34
|
+
* FHIR requires a timezone when a dateTime has a time. The time is written
|
|
35
|
+
* only with a valid `timezoneOffset` (TimezoneOffsetFromUTC (0008,0201),
|
|
36
|
+
* "+HHMM" or "-HHMM"); without it the result is the date only, which is
|
|
37
|
+
* a valid dateTime that asserts no zone.
|
|
38
|
+
*
|
|
39
|
+
* The date must be YYYYMMDD (the ACR-NEMA form YYYY.MM.DD is accepted), else
|
|
40
|
+
* the result is null. The time must start with HHMMSS (the form HH:MM:SS is
|
|
41
|
+
* accepted), else the result is the date only.
|
|
42
|
+
*/
|
|
43
|
+
function dicomDateTimeToIso(dicomDate, dicomTime, timezoneOffset) {
|
|
44
|
+
const rawDate = core.toStringFirst(dicomDate);
|
|
45
|
+
const date = /^\d{4}\.\d{2}\.\d{2}$/.test(rawDate || "")
|
|
46
|
+
? rawDate.replace(/\./g, "")
|
|
47
|
+
: rawDate;
|
|
48
|
+
if (!date || !/^\d{8}$/.test(date)) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let iso = `${date.substring(0, 4)}-${date.substring(4, 6)}-${date.substring(
|
|
53
|
+
6,
|
|
54
|
+
8
|
|
55
|
+
)}`;
|
|
56
|
+
|
|
57
|
+
const rawTime = core.toStringFirst(dicomTime);
|
|
58
|
+
const time = /^\d{2}:\d{2}:\d{2}/.test(rawTime || "")
|
|
59
|
+
? rawTime.replace(/:/g, "")
|
|
60
|
+
: rawTime;
|
|
61
|
+
const offset = core.toStringFirst(timezoneOffset);
|
|
62
|
+
if (time && /^\d{6}/.test(time) && offset && /^[+-]\d{4}$/.test(offset)) {
|
|
63
|
+
iso +=
|
|
64
|
+
`T${time.substring(0, 2)}:${time.substring(2, 4)}:` +
|
|
65
|
+
`${time.substring(4, 6)}${offset.substring(0, 3)}:` +
|
|
66
|
+
offset.substring(3);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return iso;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* DICOM DA + TM + TimezoneOffsetFromUTC → a FHIR instant, or null when a
|
|
74
|
+
* part is missing (an instant needs the full time and the zone).
|
|
75
|
+
*/
|
|
76
|
+
function dicomDateTimeToInstant(dicomDate, dicomTime, timezoneOffset) {
|
|
77
|
+
const iso = dicomDateTimeToIso(dicomDate, dicomTime, timezoneOffset);
|
|
78
|
+
return iso && iso.includes("T") ? iso : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Parse a DICOM person name into FHIR HumanName parts.
|
|
83
|
+
* Accepts the naturalized PN shape ([{ Alphabetic: "Family^Given" }] or
|
|
84
|
+
* { Alphabetic }) as well as a raw "Family^Given^Middle^Prefix^Suffix"
|
|
85
|
+
* string.
|
|
86
|
+
* @returns {Object|null} { family, given, middle, prefix, suffix, text }
|
|
87
|
+
*/
|
|
88
|
+
function parsePersonName(pnValue) {
|
|
89
|
+
let raw = pnValue;
|
|
90
|
+
if (Array.isArray(raw)) {
|
|
91
|
+
raw = raw.length > 0 ? raw[0] : null;
|
|
92
|
+
}
|
|
93
|
+
if (raw && typeof raw === "object" && "Alphabetic" in raw) {
|
|
94
|
+
raw = raw.Alphabetic;
|
|
95
|
+
}
|
|
96
|
+
if (typeof raw !== "string" || raw.trim().length === 0) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const parts = raw.split("^");
|
|
101
|
+
return {
|
|
102
|
+
family: parts[0] || "",
|
|
103
|
+
given: parts[1] || "",
|
|
104
|
+
middle: parts[2] || "",
|
|
105
|
+
prefix: parts[3] || "",
|
|
106
|
+
suffix: parts[4] || "",
|
|
107
|
+
text: parts.filter(part => part).join(" ")
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Parsed person name → FHIR HumanName element (or null when empty).
|
|
113
|
+
*/
|
|
114
|
+
function personNameToHumanName(parsed) {
|
|
115
|
+
if (!parsed) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
const humanName = {};
|
|
119
|
+
if (parsed.family) {
|
|
120
|
+
humanName.family = parsed.family;
|
|
121
|
+
}
|
|
122
|
+
const given = [parsed.given, parsed.middle].filter(Boolean);
|
|
123
|
+
if (given.length > 0) {
|
|
124
|
+
humanName.given = given;
|
|
125
|
+
}
|
|
126
|
+
if (parsed.prefix) {
|
|
127
|
+
humanName.prefix = [parsed.prefix];
|
|
128
|
+
}
|
|
129
|
+
if (parsed.suffix) {
|
|
130
|
+
humanName.suffix = [parsed.suffix];
|
|
131
|
+
}
|
|
132
|
+
if (parsed.text) {
|
|
133
|
+
humanName.text = parsed.text;
|
|
134
|
+
}
|
|
135
|
+
return Object.keys(humanName).length > 0 ? humanName : null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* DICOM PatientSex (M/F/O) → FHIR administrative gender. PatientSex is an
|
|
140
|
+
* administrative value, so the Patient gets no birth-sex or clinical-sex
|
|
141
|
+
* extension from it (FHIR-USR-3).
|
|
142
|
+
*/
|
|
143
|
+
function sexToGender(dicomSex) {
|
|
144
|
+
const mapping = { M: "male", F: "female", O: "other" };
|
|
145
|
+
return mapping[core.toStringFirst(dicomSex)] || "unknown";
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* FHIR administrative gender → DICOM PatientSex (0010,0040).
|
|
150
|
+
*
|
|
151
|
+
* FHIR `Patient.gender` is *administrative
|
|
152
|
+
* gender*; DICOM PatientSex is a three-letter administrative repertoire
|
|
153
|
+
* (M / F / O, Type 2 so empty is legal). These are not the same concept as
|
|
154
|
+
* clinical sex or gender identity, and this mapping deliberately consults
|
|
155
|
+
* ONLY `Patient.gender` — never US Core birthsex / sex-for-clinical-use
|
|
156
|
+
* extensions, which carry different semantics and belong to profiles, not
|
|
157
|
+
* the base resource. The mapping is lossy by design:
|
|
158
|
+
*
|
|
159
|
+
* male → M
|
|
160
|
+
* female → F
|
|
161
|
+
* other → O (as does any unrecognized non-empty value — DICOM has
|
|
162
|
+
* no better bucket, and inventing codes would be worse)
|
|
163
|
+
* unknown → "" (DICOM PatientSex is Type 2: present-but-empty is the
|
|
164
|
+
* standard way to say "not known")
|
|
165
|
+
* absent/blank → ""
|
|
166
|
+
*
|
|
167
|
+
* Mirrors sexToGender above, so M/F/O round-trip exactly and empty ⇄
|
|
168
|
+
* unknown round-trips to empty.
|
|
169
|
+
*/
|
|
170
|
+
function genderToSex(gender) {
|
|
171
|
+
const value = core.toStringFirst(gender);
|
|
172
|
+
if (!value) {
|
|
173
|
+
return "";
|
|
174
|
+
}
|
|
175
|
+
const normalized = value.toLowerCase();
|
|
176
|
+
if (normalized === "male") {
|
|
177
|
+
return "M";
|
|
178
|
+
}
|
|
179
|
+
if (normalized === "female") {
|
|
180
|
+
return "F";
|
|
181
|
+
}
|
|
182
|
+
if (normalized === "unknown") {
|
|
183
|
+
return "";
|
|
184
|
+
}
|
|
185
|
+
return "O";
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* FHIR HumanName → DICOM PN string ("Family^Given^Middle^Prefix^Suffix").
|
|
190
|
+
* Inverse of parsePersonName/personNameToHumanName: given[0] is the given
|
|
191
|
+
* name, given[1] the middle; only the first prefix/suffix survive (PN has
|
|
192
|
+
* one slot each). Trailing empty components are trimmed per PN convention.
|
|
193
|
+
*
|
|
194
|
+
* When the name carries no structured parts at all, `HumanName.text`
|
|
195
|
+
* (when present) survives as a single-component PN rather than returning
|
|
196
|
+
* null — a text-only name must not erase an existing PatientName under
|
|
197
|
+
* patientToDataset's deterministic overwrite (review finding 19). The
|
|
198
|
+
* text is used whole; guessing a "Family, Given" split from free text
|
|
199
|
+
* would fabricate structure the resource never asserted.
|
|
200
|
+
* @returns {string|null} null when the name carries no usable parts
|
|
201
|
+
*/
|
|
202
|
+
function humanNameToPersonName(humanName) {
|
|
203
|
+
if (!humanName || typeof humanName !== "object") {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
const given = Array.isArray(humanName.given) ? humanName.given : [];
|
|
207
|
+
const components = [
|
|
208
|
+
core.toStringFirst(humanName.family) || "",
|
|
209
|
+
core.toStringFirst(given[0]) || "",
|
|
210
|
+
core.toStringFirst(given[1]) || "",
|
|
211
|
+
core.toStringFirst(
|
|
212
|
+
Array.isArray(humanName.prefix) ? humanName.prefix[0] : null
|
|
213
|
+
) || "",
|
|
214
|
+
core.toStringFirst(
|
|
215
|
+
Array.isArray(humanName.suffix) ? humanName.suffix[0] : null
|
|
216
|
+
) || ""
|
|
217
|
+
];
|
|
218
|
+
while (components.length && components[components.length - 1] === "") {
|
|
219
|
+
components.pop();
|
|
220
|
+
}
|
|
221
|
+
const pn = components.join("^");
|
|
222
|
+
if (pn.length > 0) {
|
|
223
|
+
return pn;
|
|
224
|
+
}
|
|
225
|
+
// No structured parts: fall back to the free-text form, if any.
|
|
226
|
+
return core.toStringFirst(humanName.text);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* FHIR date (ISO 8601) → DICOM DA (YYYYMMDD). Inverse of
|
|
231
|
+
* dicomDateTimeToIso for the date part. FHIR allows partial dates (YYYY,
|
|
232
|
+
* YYYY-MM); DICOM DA does not — partial dates return null rather than a
|
|
233
|
+
* fabricated day.
|
|
234
|
+
*/
|
|
235
|
+
function isoDateToDicom(isoDate) {
|
|
236
|
+
const value = core.toStringFirst(isoDate);
|
|
237
|
+
if (!value) {
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
if (/^\d{8}$/.test(value)) {
|
|
241
|
+
return value;
|
|
242
|
+
}
|
|
243
|
+
const match = value.match(/^(\d{4})-(\d{2})-(\d{2})/);
|
|
244
|
+
return match ? `${match[1]}${match[2]}${match[3]}` : null;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* FHIR Coding for a DICOM modality code.
|
|
249
|
+
*/
|
|
250
|
+
function modalityCoding(modalityCode) {
|
|
251
|
+
const code = core.toStringFirst(modalityCode);
|
|
252
|
+
if (!code) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
system: DICOM_MODALITY_SYSTEM,
|
|
257
|
+
code,
|
|
258
|
+
display: core.MODALITY_DISPLAY[code] || code
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Guard: only R4B is implemented. Throws on anything else so callers
|
|
264
|
+
* never silently receive the wrong flavor of JSON.
|
|
265
|
+
*/
|
|
266
|
+
function assertSupportedFhirVersion(options = {}) {
|
|
267
|
+
const version = options.fhirVersion || "R4B";
|
|
268
|
+
if (version !== "R4B" && version !== "R4") {
|
|
269
|
+
throw new Error(
|
|
270
|
+
`@dcmjs/fhir: unsupported fhirVersion "${version}" (supported: R4, R4B)`
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
return version;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// packages/fhir/src/patient.js
|
|
277
|
+
//
|
|
278
|
+
// FHIR R4B Patient from the DICOM patient module (group 0010) of a
|
|
279
|
+
// naturalized dataset. Emits standard FHIR only — no ids, no meta.tags,
|
|
280
|
+
// no storage references. Id assignment and any deployment-specific
|
|
281
|
+
// decoration are the consumer's job.
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Build a FHIR Patient resource from a naturalized DICOM dataset.
|
|
286
|
+
* @param {Object} dataset - DicomMetaDictionary.naturalizeDataset output
|
|
287
|
+
* @returns {Object|null} FHIR Patient, or null when the dataset carries
|
|
288
|
+
* no usable patient module (no PatientName and no PatientID)
|
|
289
|
+
*/
|
|
290
|
+
function patientFromDataset(dataset) {
|
|
291
|
+
if (!dataset) {
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const patientId = core.toStringFirst(dataset.PatientID);
|
|
296
|
+
const humanName = personNameToHumanName(
|
|
297
|
+
parsePersonName(dataset.PatientName)
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
if (!patientId && !humanName) {
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const patient = {
|
|
305
|
+
resourceType: "Patient"
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
if (patientId) {
|
|
309
|
+
patient.identifier = [
|
|
310
|
+
{
|
|
311
|
+
use: "usual",
|
|
312
|
+
type: {
|
|
313
|
+
coding: [
|
|
314
|
+
{
|
|
315
|
+
system: "http://terminology.hl7.org/CodeSystem/v2-0203",
|
|
316
|
+
code: "MR",
|
|
317
|
+
display: "Medical Record Number"
|
|
318
|
+
}
|
|
319
|
+
],
|
|
320
|
+
text: "Medical Record Number"
|
|
321
|
+
},
|
|
322
|
+
value: patientId
|
|
323
|
+
}
|
|
324
|
+
];
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (humanName) {
|
|
328
|
+
patient.name = [humanName];
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const birthDate = dicomDateTimeToIso(dataset.PatientBirthDate);
|
|
332
|
+
if (birthDate) {
|
|
333
|
+
patient.birthDate = birthDate;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
patient.gender = sexToGender(dataset.PatientSex);
|
|
337
|
+
|
|
338
|
+
return patient;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// packages/fhir/src/imagingStudy.js
|
|
342
|
+
//
|
|
343
|
+
// FHIR R4B ImagingStudy from naturalized DICOM dataset(s). The DICOM
|
|
344
|
+
// study/series/instance hierarchy maps directly onto
|
|
345
|
+
// ImagingStudy.series[].instance[] — this is deliberately a thin
|
|
346
|
+
// restatement of the Part 10 elements as FHIR-flavored JSON.
|
|
347
|
+
// Based on the IHE Radiology Technical Framework (MADO) mapping.
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
function instanceFromDataset(dataset) {
|
|
351
|
+
const sopInstanceUid = core.toStringFirst(dataset.SOPInstanceUID);
|
|
352
|
+
if (!sopInstanceUid) {
|
|
353
|
+
return null;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const sopClassUrn = uidToUrn(dataset.SOPClassUID);
|
|
357
|
+
if (!sopClassUrn) {
|
|
358
|
+
core.validationLog.warn(
|
|
359
|
+
`@dcmjs-org/fhir: skipping instance ${sopInstanceUid} — ` +
|
|
360
|
+
"no SOPClassUID (instance.sopClass is required 1..1)"
|
|
361
|
+
);
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const instance = {
|
|
366
|
+
uid: sopInstanceUid,
|
|
367
|
+
sopClass: {
|
|
368
|
+
system: "urn:ietf:rfc:3986",
|
|
369
|
+
code: sopClassUrn
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
const number = unsignedIntFirst(dataset.InstanceNumber);
|
|
374
|
+
if (number !== null) {
|
|
375
|
+
instance.number = number;
|
|
376
|
+
}
|
|
377
|
+
const numberOfFrames = unsignedIntFirst(dataset.NumberOfFrames);
|
|
378
|
+
if (numberOfFrames !== null) {
|
|
379
|
+
instance.numberOfFrames = numberOfFrames;
|
|
380
|
+
}
|
|
381
|
+
const title =
|
|
382
|
+
core.toStringFirst(dataset.SeriesDescription) ||
|
|
383
|
+
core.toStringFirst(dataset.StudyDescription);
|
|
384
|
+
if (title) {
|
|
385
|
+
instance.title = title;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return instance;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function seriesShellFromDataset(dataset) {
|
|
392
|
+
const series = {
|
|
393
|
+
uid: core.toStringFirst(dataset.SeriesInstanceUID),
|
|
394
|
+
instance: []
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
const number = unsignedIntFirst(dataset.SeriesNumber);
|
|
398
|
+
if (number !== null) {
|
|
399
|
+
series.number = number;
|
|
400
|
+
}
|
|
401
|
+
// The caller skips a dataset with no Modality (series.modality is 1..1).
|
|
402
|
+
series.modality = modalityCoding(dataset.Modality);
|
|
403
|
+
const description = core.toStringFirst(dataset.SeriesDescription);
|
|
404
|
+
if (description) {
|
|
405
|
+
series.description = description;
|
|
406
|
+
}
|
|
407
|
+
const started = dicomDateTimeToIso(
|
|
408
|
+
dataset.SeriesDate,
|
|
409
|
+
dataset.SeriesTime,
|
|
410
|
+
dataset.TimezoneOffsetFromUTC
|
|
411
|
+
);
|
|
412
|
+
if (started) {
|
|
413
|
+
series.started = started;
|
|
414
|
+
}
|
|
415
|
+
// A DICOM defined term is not a SNOMED display, so it has no system.
|
|
416
|
+
const bodyPart = core.toStringFirst(dataset.BodyPartExamined);
|
|
417
|
+
if (bodyPart) {
|
|
418
|
+
series.bodySite = { display: bodyPart };
|
|
419
|
+
}
|
|
420
|
+
const laterality = core.toStringFirst(dataset.Laterality);
|
|
421
|
+
if (laterality) {
|
|
422
|
+
series.laterality = LATERALITY_CODING[laterality] || {
|
|
423
|
+
display: laterality
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
return series;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** DICOM Laterality (0020,0060) → SNOMED CT (PS3.16 CID 244). */
|
|
431
|
+
const LATERALITY_CODING = {
|
|
432
|
+
L: { system: "http://snomed.info/sct", code: "7771000", display: "Left" },
|
|
433
|
+
R: { system: "http://snomed.info/sct", code: "24028007", display: "Right" },
|
|
434
|
+
B: {
|
|
435
|
+
system: "http://snomed.info/sct",
|
|
436
|
+
code: "51440002",
|
|
437
|
+
display: "Right and left"
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
function warnSkip(dataset, reason) {
|
|
442
|
+
core.validationLog.warn(
|
|
443
|
+
"@dcmjs-org/fhir: skipping dataset " +
|
|
444
|
+
`${
|
|
445
|
+
core.toStringFirst(dataset.SOPInstanceUID) || "<no SOPInstanceUID>"
|
|
446
|
+
} — ${reason}`
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/** The first value as a FHIR unsignedInt, or null. */
|
|
451
|
+
function unsignedIntFirst(value) {
|
|
452
|
+
const number = core.toFloatFirst(value);
|
|
453
|
+
return Number.isInteger(number) && number >= 0 ? number : null;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Build one FHIR ImagingStudy from one or more naturalized datasets
|
|
458
|
+
* belonging to the same study. Instances are grouped by
|
|
459
|
+
* SeriesInstanceUID. The first dataset with a StudyInstanceUID anchors the
|
|
460
|
+
* study-level identity.
|
|
461
|
+
*
|
|
462
|
+
* The mapper never fabricates a value that FHIR requires (FHIR-USR-3). It
|
|
463
|
+
* skips, with a validationLog warning: a dataset of another study, a
|
|
464
|
+
* dataset with no SeriesInstanceUID or no Modality (series.uid and
|
|
465
|
+
* series.modality are 1..1), and an instance with no SOPClassUID
|
|
466
|
+
* (instance.sopClass is 1..1).
|
|
467
|
+
*
|
|
468
|
+
* @param {Object[]} datasets - naturalized datasets (same StudyInstanceUID)
|
|
469
|
+
* @param {Object} [options]
|
|
470
|
+
* @param {Object} [options.subject] - FHIR Reference ({ reference, display })
|
|
471
|
+
* to attach as ImagingStudy.subject — generic FHIR, supplied by the caller
|
|
472
|
+
* @returns {Object|null} FHIR ImagingStudy, or null for empty input
|
|
473
|
+
*/
|
|
474
|
+
function imagingStudyFromDatasets(datasets, options = {}) {
|
|
475
|
+
const all = (datasets || []).filter(Boolean);
|
|
476
|
+
if (all.length === 0) {
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
const first =
|
|
481
|
+
all.find(dataset => core.toStringFirst(dataset.StudyInstanceUID)) || all[0];
|
|
482
|
+
const anchorStudyUid = core.toStringFirst(first.StudyInstanceUID);
|
|
483
|
+
const list = all.filter(dataset => {
|
|
484
|
+
const studyUid = core.toStringFirst(dataset.StudyInstanceUID);
|
|
485
|
+
if (studyUid && studyUid !== anchorStudyUid) {
|
|
486
|
+
warnSkip(
|
|
487
|
+
dataset,
|
|
488
|
+
`StudyInstanceUID ${studyUid} is not the study ` +
|
|
489
|
+
`${anchorStudyUid} of this ImagingStudy`
|
|
490
|
+
);
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
return true;
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
// No imaging identity at all → not a study
|
|
497
|
+
const hasIdentity = list.some(
|
|
498
|
+
dataset =>
|
|
499
|
+
core.toStringFirst(dataset.StudyInstanceUID) ||
|
|
500
|
+
core.toStringFirst(dataset.SeriesInstanceUID) ||
|
|
501
|
+
core.toStringFirst(dataset.SOPInstanceUID)
|
|
502
|
+
);
|
|
503
|
+
if (!hasIdentity) {
|
|
504
|
+
return null;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const seriesMap = new Map();
|
|
508
|
+
list.forEach(dataset => {
|
|
509
|
+
const seriesUid = core.toStringFirst(dataset.SeriesInstanceUID);
|
|
510
|
+
if (!seriesUid) {
|
|
511
|
+
warnSkip(dataset, "no SeriesInstanceUID (series.uid is 1..1)");
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if (!core.toStringFirst(dataset.Modality)) {
|
|
515
|
+
warnSkip(dataset, "no Modality (series.modality is 1..1)");
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
if (!seriesMap.has(seriesUid)) {
|
|
519
|
+
seriesMap.set(seriesUid, seriesShellFromDataset(dataset));
|
|
520
|
+
}
|
|
521
|
+
const instance = instanceFromDataset(dataset);
|
|
522
|
+
if (instance) {
|
|
523
|
+
seriesMap.get(seriesUid).instance.push(instance);
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
const series = Array.from(seriesMap.values()).map(shell => {
|
|
528
|
+
const entry = { ...shell };
|
|
529
|
+
entry.numberOfInstances = entry.instance.length;
|
|
530
|
+
if (entry.instance.length === 0) {
|
|
531
|
+
delete entry.instance;
|
|
532
|
+
}
|
|
533
|
+
return entry;
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
const numberOfInstances = series.reduce(
|
|
537
|
+
(sum, entry) => sum + (entry.numberOfInstances || 0),
|
|
538
|
+
0
|
|
539
|
+
);
|
|
540
|
+
|
|
541
|
+
const study = {
|
|
542
|
+
resourceType: "ImagingStudy",
|
|
543
|
+
status: "available",
|
|
544
|
+
numberOfSeries: series.length,
|
|
545
|
+
numberOfInstances,
|
|
546
|
+
series
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
const identifiers = [];
|
|
550
|
+
const studyUid = core.toStringFirst(first.StudyInstanceUID);
|
|
551
|
+
if (studyUid) {
|
|
552
|
+
identifiers.push({
|
|
553
|
+
use: "official",
|
|
554
|
+
system: DICOM_UID_SYSTEM,
|
|
555
|
+
value: uidToUrn(studyUid)
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
const accessionNumber = core.toStringFirst(first.AccessionNumber);
|
|
559
|
+
if (accessionNumber) {
|
|
560
|
+
identifiers.push({
|
|
561
|
+
use: "usual",
|
|
562
|
+
type: {
|
|
563
|
+
coding: [
|
|
564
|
+
{
|
|
565
|
+
system: "http://terminology.hl7.org/CodeSystem/v2-0203",
|
|
566
|
+
code: "ACSN"
|
|
567
|
+
}
|
|
568
|
+
]
|
|
569
|
+
},
|
|
570
|
+
value: accessionNumber
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
if (identifiers.length > 0) {
|
|
574
|
+
study.identifier = identifiers;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
const started = dicomDateTimeToIso(
|
|
578
|
+
first.StudyDate,
|
|
579
|
+
first.StudyTime,
|
|
580
|
+
first.TimezoneOffsetFromUTC
|
|
581
|
+
);
|
|
582
|
+
if (started) {
|
|
583
|
+
study.started = started;
|
|
584
|
+
}
|
|
585
|
+
const description = core.toStringFirst(first.StudyDescription);
|
|
586
|
+
if (description) {
|
|
587
|
+
study.description = description;
|
|
588
|
+
}
|
|
589
|
+
const referrer = parsePersonName(first.ReferringPhysicianName);
|
|
590
|
+
if (referrer && referrer.text) {
|
|
591
|
+
study.referrer = { display: referrer.text };
|
|
592
|
+
}
|
|
593
|
+
if (options.subject && options.subject.reference) {
|
|
594
|
+
study.subject = options.subject;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// Study-level modality union (distinct series modalities)
|
|
598
|
+
const modalities = [];
|
|
599
|
+
series.forEach(entry => {
|
|
600
|
+
if (
|
|
601
|
+
entry.modality &&
|
|
602
|
+
!modalities.some(m => m.code === entry.modality.code)
|
|
603
|
+
) {
|
|
604
|
+
modalities.push(entry.modality);
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
if (modalities.length > 0) {
|
|
608
|
+
study.modality = modalities;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
return study;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/**
|
|
615
|
+
* Single-dataset convenience wrapper.
|
|
616
|
+
*/
|
|
617
|
+
function imagingStudyFromDataset(dataset, options = {}) {
|
|
618
|
+
return imagingStudyFromDatasets(dataset ? [dataset] : [], options);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// packages/fhir/src/documentReference.js
|
|
622
|
+
//
|
|
623
|
+
// FHIR R4B DocumentReference from a DICOM Encapsulated Document instance
|
|
624
|
+
// (Encapsulated PDF, SOP Class 1.2.840.10008.5.1.4.1.1.104.1) — the "PDF
|
|
625
|
+
// arrived via DICOM" mapping. Emits standard FHIR only — no ids, no
|
|
626
|
+
// meta.tags; id assignment is the consumer's job (same contract as
|
|
627
|
+
// patient.js / imagingStudy.js).
|
|
628
|
+
//
|
|
629
|
+
// DocumentReference.date is a FHIR instant, so it is written only when the
|
|
630
|
+
// dataset has ContentDate, ContentTime, and TimezoneOffsetFromUTC.
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
const CODING_SCHEME_SYSTEMS = {
|
|
634
|
+
LN: "http://loinc.org",
|
|
635
|
+
DCM: "http://dicom.nema.org/resources/ontology/DCM",
|
|
636
|
+
SCT: "http://snomed.info/sct",
|
|
637
|
+
SRT: "http://snomed.info/sct"
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* Naturalized ConceptNameCodeSequence (item object, array of items, or
|
|
642
|
+
* addAccessors proxy) → FHIR CodeableConcept, or null.
|
|
643
|
+
*/
|
|
644
|
+
function conceptNameToCodeableConcept(conceptName) {
|
|
645
|
+
let item = conceptName;
|
|
646
|
+
if (Array.isArray(item)) {
|
|
647
|
+
item = item.length > 0 ? item[0] : null;
|
|
648
|
+
}
|
|
649
|
+
if (!item || typeof item !== "object") {
|
|
650
|
+
return null;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
const code = core.toStringFirst(item.CodeValue);
|
|
654
|
+
const meaning = core.toStringFirst(item.CodeMeaning);
|
|
655
|
+
if (!code && !meaning) {
|
|
656
|
+
return null;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
const codeableConcept = {};
|
|
660
|
+
if (code) {
|
|
661
|
+
const designator = core.toStringFirst(item.CodingSchemeDesignator);
|
|
662
|
+
const coding = { code };
|
|
663
|
+
if (designator) {
|
|
664
|
+
coding.system = CODING_SCHEME_SYSTEMS[designator] || designator;
|
|
665
|
+
}
|
|
666
|
+
if (meaning) {
|
|
667
|
+
coding.display = meaning;
|
|
668
|
+
}
|
|
669
|
+
codeableConcept.coding = [coding];
|
|
670
|
+
}
|
|
671
|
+
if (meaning) {
|
|
672
|
+
codeableConcept.text = meaning;
|
|
673
|
+
}
|
|
674
|
+
return codeableConcept;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/** The BulkDataURI of a naturalized `{ BulkDataURI }` payload, or null. */
|
|
678
|
+
function bulkDataUriOf(payload) {
|
|
679
|
+
const value = Array.isArray(payload) ? payload[0] : payload;
|
|
680
|
+
return core.toStringFirst(value?.BulkDataURI);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Build a FHIR DocumentReference from a naturalized Encapsulated Document
|
|
685
|
+
* dataset.
|
|
686
|
+
* @param {Object} dataset - DicomMetaDictionary.naturalizeDataset output
|
|
687
|
+
* @param {Object} [options]
|
|
688
|
+
* @param {Object} [options.subject] - FHIR Reference for .subject
|
|
689
|
+
* @param {boolean} [options.includeData=true] - inline the document as
|
|
690
|
+
* base64 in content[0].attachment.data (size is always emitted)
|
|
691
|
+
* @returns {Object|null} FHIR DocumentReference, or null when the dataset
|
|
692
|
+
* is not an encapsulated document instance
|
|
693
|
+
*/
|
|
694
|
+
function documentReferenceFromDataset(dataset, options = {}) {
|
|
695
|
+
if (!dataset) {
|
|
696
|
+
return null;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
const mimeType = core.toStringFirst(dataset.MIMETypeOfEncapsulatedDocument);
|
|
700
|
+
const sopClassUid = core.toStringFirst(dataset.SOPClassUID);
|
|
701
|
+
const isEncapsulated =
|
|
702
|
+
mimeType !== null || sopClassUid === encapsulated.ENCAPSULATED_PDF_SOP_CLASS_UID;
|
|
703
|
+
if (!isEncapsulated || dataset.EncapsulatedDocument === undefined) {
|
|
704
|
+
return null;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
const includeData = options.includeData !== false;
|
|
708
|
+
const documentReference = {
|
|
709
|
+
resourceType: "DocumentReference",
|
|
710
|
+
status: "current"
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
const sopInstanceUrn = uidToUrn(dataset.SOPInstanceUID);
|
|
714
|
+
if (sopInstanceUrn) {
|
|
715
|
+
documentReference.masterIdentifier = {
|
|
716
|
+
system: DICOM_UID_SYSTEM,
|
|
717
|
+
value: sopInstanceUrn
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
const type = conceptNameToCodeableConcept(dataset.ConceptNameCodeSequence);
|
|
722
|
+
if (type) {
|
|
723
|
+
documentReference.type = type;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
if (options.subject) {
|
|
727
|
+
documentReference.subject = options.subject;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
const date = dicomDateTimeToInstant(
|
|
731
|
+
dataset.ContentDate,
|
|
732
|
+
dataset.ContentTime,
|
|
733
|
+
dataset.TimezoneOffsetFromUTC
|
|
734
|
+
);
|
|
735
|
+
if (date) {
|
|
736
|
+
documentReference.date = date;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
const title = core.toStringFirst(dataset.DocumentTitle);
|
|
740
|
+
if (title) {
|
|
741
|
+
documentReference.description = title;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const attachment = {
|
|
745
|
+
contentType: mimeType || encapsulated.PDF_MIME_TYPE
|
|
746
|
+
};
|
|
747
|
+
if (title) {
|
|
748
|
+
attachment.title = title;
|
|
749
|
+
}
|
|
750
|
+
const bytes = encapsulated.extractEncapsulated(dataset);
|
|
751
|
+
const bulkDataUri = bulkDataUriOf(dataset.EncapsulatedDocument);
|
|
752
|
+
if (bytes) {
|
|
753
|
+
attachment.size = bytes.byteLength;
|
|
754
|
+
// FHIR does not permit an empty string value.
|
|
755
|
+
if (includeData && bytes.byteLength > 0) {
|
|
756
|
+
attachment.data = core.encodeBase64(bytes);
|
|
757
|
+
}
|
|
758
|
+
} else if (bulkDataUri) {
|
|
759
|
+
attachment.url = bulkDataUri;
|
|
760
|
+
}
|
|
761
|
+
documentReference.content = [{ attachment }];
|
|
762
|
+
|
|
763
|
+
const studyUrn = uidToUrn(dataset.StudyInstanceUID);
|
|
764
|
+
if (studyUrn) {
|
|
765
|
+
documentReference.context = {
|
|
766
|
+
related: [
|
|
767
|
+
{
|
|
768
|
+
identifier: {
|
|
769
|
+
system: DICOM_UID_SYSTEM,
|
|
770
|
+
value: studyUrn
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
]
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
return documentReference;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
// packages/fhir/src/patientToDataset.js
|
|
781
|
+
//
|
|
782
|
+
// The FHIR source direction: a FHIR Patient resource → DICOM patient-module
|
|
783
|
+
// attributes (group 0010) in naturalized keyword form. Inverse of
|
|
784
|
+
// patient.js's patientFromDataset, and the primitive behind demographics
|
|
785
|
+
// injection ("apply this Patient to these instances").
|
|
786
|
+
//
|
|
787
|
+
// The result rules are FHIR-USR-6 in packages/fhir/README.md.
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
const MR_IDENTIFIER_CODE = "MR";
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* Pick the name to use: `official` wins, then `usual`, then the first
|
|
794
|
+
* entry with no `use` at all, then the first entry — so a resource
|
|
795
|
+
* carrying both a married (official) and maiden name maps to the married
|
|
796
|
+
* one, never the maiden.
|
|
797
|
+
*/
|
|
798
|
+
function selectName(names) {
|
|
799
|
+
if (!Array.isArray(names) || names.length === 0) {
|
|
800
|
+
return null;
|
|
801
|
+
}
|
|
802
|
+
for (const use of ["official", "usual"]) {
|
|
803
|
+
const match = names.find(name => name && name.use === use);
|
|
804
|
+
if (match) {
|
|
805
|
+
return match;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
return names.find(name => name && !name.use) || names[0];
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* Pick the identifier for PatientID: an MR-typed identifier wins
|
|
813
|
+
* (type.coding code "MR", the shape patientFromDataset emits), else the
|
|
814
|
+
* first identifier carrying a value.
|
|
815
|
+
*/
|
|
816
|
+
function selectIdentifier(identifiers) {
|
|
817
|
+
if (!Array.isArray(identifiers) || identifiers.length === 0) {
|
|
818
|
+
return null;
|
|
819
|
+
}
|
|
820
|
+
const mrTyped = identifiers.find(identifier =>
|
|
821
|
+
identifier?.type?.coding?.some(
|
|
822
|
+
coding => coding && coding.code === MR_IDENTIFIER_CODE
|
|
823
|
+
)
|
|
824
|
+
);
|
|
825
|
+
return mrTyped || identifiers.find(identifier => identifier?.value) || null;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/**
|
|
829
|
+
* Map a FHIR Patient resource to DICOM patient-module attributes.
|
|
830
|
+
*
|
|
831
|
+
* @param {Object} patient - FHIR Patient (R4/R4B)
|
|
832
|
+
* @returns {{ PatientName: string, PatientID: string,
|
|
833
|
+
* PatientBirthDate: string, PatientSex: string }} naturalized keyword
|
|
834
|
+
* attributes; absent resource fields are "" (deterministic overwrite)
|
|
835
|
+
* @throws when the input is not a Patient resource
|
|
836
|
+
*/
|
|
837
|
+
function patientToDataset(patient) {
|
|
838
|
+
if (!patient || patient.resourceType !== "Patient") {
|
|
839
|
+
throw new Error(
|
|
840
|
+
"patientToDataset expects a FHIR Patient resource " +
|
|
841
|
+
`(got resourceType "${patient?.resourceType}")`
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
const name = selectName(patient.name);
|
|
846
|
+
const identifier = selectIdentifier(patient.identifier);
|
|
847
|
+
|
|
848
|
+
return {
|
|
849
|
+
PatientName: (name && humanNameToPersonName(name)) || "",
|
|
850
|
+
PatientID: identifier?.value || "",
|
|
851
|
+
PatientBirthDate: isoDateToDicom(patient.birthDate) || "",
|
|
852
|
+
PatientSex: genderToSex(patient.gender)
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// packages/fhir/src/index.js
|
|
857
|
+
//
|
|
858
|
+
// @dcmjs-org/fhir — FHIR both ways.
|
|
859
|
+
//
|
|
860
|
+
// PRELIMINARY API: the exports can change in a minor release while
|
|
861
|
+
// packages/fhir/README.md has the preliminary notice.
|
|
862
|
+
//
|
|
863
|
+
// The sink: naturalized DICOM Part 10 datasets
|
|
864
|
+
// (DicomMetaDictionary.naturalizeDataset output) → FHIR R4B resources.
|
|
865
|
+
// The source: a FHIR Patient → DICOM patient-module attributes
|
|
866
|
+
// (patientToDataset), for demographics injection.
|
|
867
|
+
//
|
|
868
|
+
// Deliberately simple: standard FHIR, nothing deployment-specific.
|
|
869
|
+
// Consumers assign resource ids, storage references, and meta.tags.
|
|
870
|
+
//
|
|
871
|
+
// const { patient, imagingStudy, documentReference } = toFhir(dataset);
|
|
872
|
+
// const bundle = toBundle([dataset1, dataset2]);
|
|
873
|
+
// const attrs = patientToDataset(patientResource); // → { PatientName, ... }
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* Map one naturalized dataset to its FHIR resources.
|
|
878
|
+
*
|
|
879
|
+
* Encapsulated document instances (e.g. Encapsulated PDF from a PACS) map
|
|
880
|
+
* to a DocumentReference and no ImagingStudy; image instances map to an
|
|
881
|
+
* ImagingStudy and no DocumentReference. The 3-key shape is stable either
|
|
882
|
+
* way.
|
|
883
|
+
*
|
|
884
|
+
* Without `options.subject`, the ImagingStudy or DocumentReference
|
|
885
|
+
* references the Patient of the result as `{ reference: "./patient" }`, a
|
|
886
|
+
* path relative to the returned object (FHIR-USR-10). The consumer replaces
|
|
887
|
+
* it when it stores the resources.
|
|
888
|
+
*
|
|
889
|
+
* @param {Object} dataset - DicomMetaDictionary.naturalizeDataset output
|
|
890
|
+
* @param {Object} [options]
|
|
891
|
+
* @param {string} [options.fhirVersion='R4B'] - R4/R4B only; throws otherwise
|
|
892
|
+
* @param {Object} [options.subject] - FHIR Reference for
|
|
893
|
+
* ImagingStudy.subject / DocumentReference.subject
|
|
894
|
+
* @param {boolean} [options.includeData=true] - inline document bytes as
|
|
895
|
+
* base64 in DocumentReference attachments
|
|
896
|
+
* @returns {{ patient: Object|null, imagingStudy: Object|null,
|
|
897
|
+
* documentReference: Object|null }}
|
|
898
|
+
*/
|
|
899
|
+
function toFhir(dataset, options = {}) {
|
|
900
|
+
assertSupportedFhirVersion(options);
|
|
901
|
+
const patient = patientFromDataset(dataset);
|
|
902
|
+
const resourceOptions = withSubject(options, patient && "./patient");
|
|
903
|
+
const documentReference = documentReferenceFromDataset(
|
|
904
|
+
dataset,
|
|
905
|
+
resourceOptions
|
|
906
|
+
);
|
|
907
|
+
return {
|
|
908
|
+
patient,
|
|
909
|
+
imagingStudy: documentReference
|
|
910
|
+
? null
|
|
911
|
+
: imagingStudyFromDataset(dataset, resourceOptions),
|
|
912
|
+
documentReference
|
|
913
|
+
};
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
/** The options with a relative `subject`, unless the caller gave one. */
|
|
917
|
+
function withSubject(options, reference) {
|
|
918
|
+
if (options.subject || !reference) {
|
|
919
|
+
return options;
|
|
920
|
+
}
|
|
921
|
+
return { ...options, subject: { reference } };
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* Map one or more datasets (one study's worth of instances) to a FHIR
|
|
926
|
+
* collection Bundle: at most one Patient, a DocumentReference for each
|
|
927
|
+
* encapsulated document, and one aggregated ImagingStudy.
|
|
928
|
+
*
|
|
929
|
+
* The first dataset with a patient module gives the Patient, and the first
|
|
930
|
+
* dataset with a StudyInstanceUID gives the study. A dataset with a
|
|
931
|
+
* different PatientID or StudyInstanceUID is skipped with a validationLog
|
|
932
|
+
* warning (FHIR-USR-3). Each entry has `fullUrl: "./entry/<index>"`, a path
|
|
933
|
+
* relative to the returned Bundle, and without `options.subject` the
|
|
934
|
+
* ImagingStudy and the DocumentReferences reference the Patient by its
|
|
935
|
+
* `fullUrl` (FHIR-USR-10).
|
|
936
|
+
*
|
|
937
|
+
* @param {Object[]} datasets - naturalized datasets
|
|
938
|
+
* @param {Object} [options] - as toFhir
|
|
939
|
+
* @returns {Object} FHIR Bundle (type: collection)
|
|
940
|
+
*/
|
|
941
|
+
function toBundle(datasets, options = {}) {
|
|
942
|
+
assertSupportedFhirVersion(options);
|
|
943
|
+
|
|
944
|
+
const all = (datasets || []).filter(Boolean);
|
|
945
|
+
const resources = [];
|
|
946
|
+
|
|
947
|
+
let patient = null;
|
|
948
|
+
let patientId = null;
|
|
949
|
+
for (const dataset of all) {
|
|
950
|
+
patient = patientFromDataset(dataset);
|
|
951
|
+
if (patient) {
|
|
952
|
+
patientId = core.toStringFirst(dataset.PatientID);
|
|
953
|
+
break;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
const studyUid = all
|
|
957
|
+
.map(dataset => core.toStringFirst(dataset.StudyInstanceUID))
|
|
958
|
+
.find(Boolean);
|
|
959
|
+
const list = all.filter(dataset =>
|
|
960
|
+
sameContext(dataset, patientId, studyUid)
|
|
961
|
+
);
|
|
962
|
+
if (patient) {
|
|
963
|
+
resources.push(patient);
|
|
964
|
+
}
|
|
965
|
+
const resourceOptions = withSubject(options, patient && "./entry/0");
|
|
966
|
+
|
|
967
|
+
// Encapsulated documents each become a DocumentReference; the
|
|
968
|
+
// remaining (image) datasets aggregate into one ImagingStudy.
|
|
969
|
+
const imageDatasets = [];
|
|
970
|
+
for (const dataset of list) {
|
|
971
|
+
const documentReference = documentReferenceFromDataset(
|
|
972
|
+
dataset,
|
|
973
|
+
resourceOptions
|
|
974
|
+
);
|
|
975
|
+
if (documentReference) {
|
|
976
|
+
resources.push(documentReference);
|
|
977
|
+
} else {
|
|
978
|
+
imageDatasets.push(dataset);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
const imagingStudy = imagingStudyFromDatasets(
|
|
983
|
+
imageDatasets,
|
|
984
|
+
resourceOptions
|
|
985
|
+
);
|
|
986
|
+
if (imagingStudy) {
|
|
987
|
+
resources.push(imagingStudy);
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
return {
|
|
991
|
+
resourceType: "Bundle",
|
|
992
|
+
type: "collection",
|
|
993
|
+
total: resources.length,
|
|
994
|
+
entry: resources.map((resource, index) => ({
|
|
995
|
+
fullUrl: `./entry/${index}`,
|
|
996
|
+
resource
|
|
997
|
+
}))
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
/** True when the dataset belongs to the patient and study of the Bundle. */
|
|
1002
|
+
function sameContext(dataset, patientId, studyUid) {
|
|
1003
|
+
const datasetPatientId = core.toStringFirst(dataset.PatientID);
|
|
1004
|
+
const datasetStudyUid = core.toStringFirst(dataset.StudyInstanceUID);
|
|
1005
|
+
const reason =
|
|
1006
|
+
patientId && datasetPatientId && datasetPatientId !== patientId
|
|
1007
|
+
? `PatientID ${datasetPatientId} is not ${patientId}`
|
|
1008
|
+
: studyUid && datasetStudyUid && datasetStudyUid !== studyUid
|
|
1009
|
+
? `StudyInstanceUID ${datasetStudyUid} is not ${studyUid}`
|
|
1010
|
+
: null;
|
|
1011
|
+
if (reason) {
|
|
1012
|
+
core.validationLog.warn(
|
|
1013
|
+
"@dcmjs-org/fhir: toBundle skips dataset " +
|
|
1014
|
+
`${
|
|
1015
|
+
core.toStringFirst(dataset.SOPInstanceUID) ||
|
|
1016
|
+
"<no SOPInstanceUID>"
|
|
1017
|
+
} — ${reason}`
|
|
1018
|
+
);
|
|
1019
|
+
return false;
|
|
1020
|
+
}
|
|
1021
|
+
return true;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
exports.DICOM_MODALITY_SYSTEM = DICOM_MODALITY_SYSTEM;
|
|
1025
|
+
exports.DICOM_UID_SYSTEM = DICOM_UID_SYSTEM;
|
|
1026
|
+
exports.assertSupportedFhirVersion = assertSupportedFhirVersion;
|
|
1027
|
+
exports.dicomDateTimeToInstant = dicomDateTimeToInstant;
|
|
1028
|
+
exports.dicomDateTimeToIso = dicomDateTimeToIso;
|
|
1029
|
+
exports.documentReferenceFromDataset = documentReferenceFromDataset;
|
|
1030
|
+
exports.genderToSex = genderToSex;
|
|
1031
|
+
exports.humanNameToPersonName = humanNameToPersonName;
|
|
1032
|
+
exports.imagingStudyFromDataset = imagingStudyFromDataset;
|
|
1033
|
+
exports.imagingStudyFromDatasets = imagingStudyFromDatasets;
|
|
1034
|
+
exports.isoDateToDicom = isoDateToDicom;
|
|
1035
|
+
exports.modalityCoding = modalityCoding;
|
|
1036
|
+
exports.parsePersonName = parsePersonName;
|
|
1037
|
+
exports.patientFromDataset = patientFromDataset;
|
|
1038
|
+
exports.patientToDataset = patientToDataset;
|
|
1039
|
+
exports.personNameToHumanName = personNameToHumanName;
|
|
1040
|
+
exports.sexToGender = sexToGender;
|
|
1041
|
+
exports.toBundle = toBundle;
|
|
1042
|
+
exports.toFhir = toFhir;
|
|
1043
|
+
exports.uidToUrn = uidToUrn;
|
|
1044
|
+
//# sourceMappingURL=index.cjs.map
|