Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/lib/provenance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,58 @@ describe("detectProvenance", () => {
}
});

it("prefers canonical ds when multiple IsDerivedFrom entries exist", () => {
// Re-released datasets carry both the original and the canonical id as
// IsDerivedFrom. on002718 was originally ds000117 on OpenNeuro, then
// re-released as ds002718; the canonical (ds002718) must win the pick.
const p = detectProvenance(
meta({
dataset_id: "on002718",
related_identifiers: [
{
identifier: "10.18112/openneuro.ds000117",
identifier_type: "DOI",
relation_type: "IsDerivedFrom",
},
{
identifier: "10.18112/openneuro.ds002718.v1.1.0",
identifier_type: "DOI",
relation_type: "IsDerivedFrom",
},
],
}),
null,
);
expect(p.kind).toBe("derived");
if (p.kind === "derived") {
expect(p.originalDatasetId).toBe("ds002718");
expect(p.originalDoi).toBe("10.18112/openneuro.ds002718.v1.1.0");
expect(p.originalUrl).toBe("https://openneuro.org/datasets/ds002718");
}
});

it("falls back to first IsDerivedFrom when canonical is absent", () => {
// Some datasets list only the older id (no canonical entry). Use it
// rather than silently dropping the provenance signal.
const p = detectProvenance(
meta({
dataset_id: "on999999",
related_identifiers: [
{
identifier: "10.18112/openneuro.ds000117",
identifier_type: "DOI",
relation_type: "IsDerivedFrom",
},
],
}),
null,
);
expect(p.kind).toBe("derived");
if (p.kind === "derived") {
expect(p.originalDatasetId).toBe("ds000117");
}
});

it("falls back to id prefix swap when no related_identifiers", () => {
const p = detectProvenance(meta({ dataset_id: "on005262", related_identifiers: [] }), null);
expect(p.kind).toBe("derived");
Expand Down
40 changes: 27 additions & 13 deletions src/lib/provenance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,45 @@ export function detectProvenance(
const id = metadata.dataset_id;
if (!id.startsWith("on")) return { kind: "native" };

let originalDatasetId: string | null = null;
let originalDoi: string | null = null;
let originalUrl: string | null = null;
// A dataset re-released on OpenNeuro under a new id keeps BOTH the old and
// new id in related_identifiers as IsDerivedFrom (e.g. on002718 lists both
// ds000117 and ds002718). Prefer the candidate whose extracted ds id
// matches the canonical on->ds swap so the chip and link agree with the
// dataset's actual source_id.
const expectedDsId = id.replace(/^on/, "ds").toLowerCase();
const candidates: Array<{ datasetId: string; doi: string | null; url: string | null }> = [];

for (const ri of metadata.related_identifiers ?? []) {
if (!DERIVED_RELATIONS.has(ri.relation_type)) continue;
const fromUrl = OPENNEURO_HOST_PATTERN.exec(ri.identifier);
if (fromUrl) {
originalDatasetId ??= fromUrl[1].toLowerCase();
originalUrl ??= ri.identifier_type === "URL" ? ri.identifier : null;
candidates.push({
datasetId: fromUrl[1].toLowerCase(),
doi: null,
url: ri.identifier_type === "URL" ? ri.identifier : null,
});
continue;
}
const fromId = OPENNEURO_ID_PATTERN.exec(ri.identifier);
if (fromId && ri.identifier_type === "DOI") {
originalDoi ??= ri.identifier;
originalDatasetId ??= fromId[1].toLowerCase();
} else if (fromId) {
originalDatasetId ??= fromId[1].toLowerCase();
if (fromId) {
candidates.push({
datasetId: fromId[1].toLowerCase(),
doi: ri.identifier_type === "DOI" ? ri.identifier : null,
url: null,
});
}
}

const picked = candidates.find((c) => c.datasetId === expectedDsId) ?? candidates[0] ?? null;

let originalDatasetId: string | null = picked?.datasetId ?? null;
const originalDoi: string | null = picked?.doi ?? null;
let originalUrl: string | null = picked?.url ?? null;

if (!originalDatasetId) {
// Last-ditch: convert on005262 -> ds005262.
const stripped = id.replace(/^on/, "ds");
if (OPENNEURO_ID_PATTERN.test(stripped)) {
originalDatasetId = stripped;
if (OPENNEURO_ID_PATTERN.test(expectedDsId)) {
originalDatasetId = expectedDsId;
}
}

Expand Down