|
| 1 | +/** |
| 2 | + * Index Nextclade dataset reference trees, including past versions. |
| 3 | + * |
| 4 | + * Transforms Nextclade's own index for use with our resourceIndexer/… and |
| 5 | + * src/resourceIndex.js framework. |
| 6 | + */ |
| 7 | +import { strict as assert } from "assert"; |
| 8 | +import { DateTime } from "luxon"; |
| 9 | +import { readFile, writeFile, mkdir } from "node:fs/promises"; |
| 10 | +import path from "node:path"; |
| 11 | + |
| 12 | +import { fetch } from "../src/fetch.js"; |
| 13 | +import { NextcladeSource } from "../src/sources/nextclade.js"; |
| 14 | +import { rootDirFullPath } from "../src/utils/index.js"; |
| 15 | + |
| 16 | + |
| 17 | +const LOCAL_DATA = path.relative(".", path.join(rootDirFullPath, "devData", "nextclade")); |
| 18 | +const LOCAL_INDEX = path.join(LOCAL_DATA, "index.json"); |
| 19 | + |
| 20 | + |
| 21 | +/* All class members are part of the "collection" interface expected by |
| 22 | + * resourceIndexer/main.js and use its terminology for arguments and return |
| 23 | + * values. This interface is kind of a weird fit for things that aren't S3 |
| 24 | + * inventories, so the chain of methods and way they pass values are a bit |
| 25 | + * contrived. |
| 26 | + */ |
| 27 | +export class NextcladeData { |
| 28 | + #source; |
| 29 | + |
| 30 | + name = "nextclade"; |
| 31 | + |
| 32 | + async collect({local, save}) { |
| 33 | + if (local) { |
| 34 | + console.log(`Reading ${LOCAL_INDEX}`); |
| 35 | + this.#source = new NextcladeSource(JSON.parse(await readFile(LOCAL_INDEX))); |
| 36 | + } |
| 37 | + else { |
| 38 | + this.#source = new NextcladeSource(); |
| 39 | + |
| 40 | + if (save) { |
| 41 | + console.log(`Saving ${LOCAL_INDEX}`); |
| 42 | + await mkdir(path.dirname(LOCAL_INDEX), {recursive: true}); |
| 43 | + await writeFile(LOCAL_INDEX, JSON.stringify(await this.#source._index(), null, 2)); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + const datasetPaths = await this.#source.availableDatasets(); |
| 48 | + |
| 49 | + return (await Promise.all( |
| 50 | + datasetPaths.map(async (datasetPath) => { |
| 51 | + const dataset = this.#source.dataset(datasetPath.split("/")); |
| 52 | + const indexDataset = await dataset._indexDataset(); |
| 53 | + |
| 54 | + /* Sort and collapse versions per our documented behaviour: |
| 55 | + * |
| 56 | + * > All times are UTC. A datestamp refers to datasets uploaded |
| 57 | + * > between 00h00 and 23h59 UTC on that day. |
| 58 | + * |
| 59 | + * > If multiple datasets are uploaded on the same day we take the most |
| 60 | + * > recent. |
| 61 | + * |
| 62 | + * See <https://docs.nextstrain.org/page/guides/snapshots.html#details-for-dataset-maintainers>. |
| 63 | + */ |
| 64 | + const datesSeen = new Set(); |
| 65 | + const indexVersions = |
| 66 | + indexDataset.versions |
| 67 | + .map(v => ({...v, _timestamp: DateTime.fromISO(v.updatedAt, {zone:"UTC"})})) |
| 68 | + .toSorted((a, b) => b._timestamp - a._timestamp) |
| 69 | + .map(v => ({...v, _date: v._timestamp.toISODate()})) |
| 70 | + .filter(v => !datesSeen.has(v._date) && datesSeen.add(v._date)) |
| 71 | + |
| 72 | + // Produce one resourceIndexer/main.js "item" per dataset version |
| 73 | + return (await Promise.all( |
| 74 | + indexVersions.map(async (indexVersion) => { |
| 75 | + const versionMetaPath = `${indexDataset.path}/${indexVersion.tag}/pathogen.json`; |
| 76 | + |
| 77 | + const localFile = path.join(LOCAL_DATA, versionMetaPath); |
| 78 | + |
| 79 | + let versionMeta; |
| 80 | + |
| 81 | + if (local) { |
| 82 | + console.log(`Reading ${localFile}`); |
| 83 | + versionMeta = JSON.parse(await readFile(localFile)); |
| 84 | + } |
| 85 | + else { |
| 86 | + const remoteUrl = await this.#source.urlFor(versionMetaPath); |
| 87 | + |
| 88 | + console.log(`Fetching ${remoteUrl}`); |
| 89 | + const response = await fetch(remoteUrl, {cache: "no-cache"}); |
| 90 | + assert(response.status === 200); |
| 91 | + |
| 92 | + versionMeta = await response.json(); |
| 93 | + |
| 94 | + if (save) { |
| 95 | + console.log(`Saving ${localFile}`); |
| 96 | + await mkdir(path.dirname(localFile), {recursive: true}); |
| 97 | + await writeFile(localFile, JSON.stringify(versionMeta, null, 2)); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /* This filter must be *after* we fetch the version's own |
| 102 | + * pathogen.json. Because versions are filtered to one-per-day |
| 103 | + * *before* we fetch, it's possible there's an older version from |
| 104 | + * the same day that *does* include a treeJson, and we'd miss it. |
| 105 | + * The fix would be fetching *all* versions and only then filtering |
| 106 | + * to one-per-day (i.e. in createResource() below). |
| 107 | + * |
| 108 | + * Doing so, however, seems unnecessary. The scenario seems |
| 109 | + * unlikely and it's not entirely clear how we'd want to interpret |
| 110 | + * such a dataset update anyway (e.g. was the earlier version on |
| 111 | + * the same day in error?). |
| 112 | + * |
| 113 | + * Also note that this filters out some datasets entirely: those |
| 114 | + * that don't have a reference tree at all. |
| 115 | + * -trs, 27 Oct 2025 |
| 116 | + */ |
| 117 | + if (!versionMeta.files.treeJson) |
| 118 | + return; |
| 119 | + |
| 120 | + // One "item" produced by collect() |
| 121 | + return { |
| 122 | + // Used by resourceIndexer/main.js |
| 123 | + source: this.#source.name, |
| 124 | + resourceType: "dataset", |
| 125 | + resourcePath: datasetPath, |
| 126 | + |
| 127 | + // Used in createResource() below |
| 128 | + version: { |
| 129 | + date: indexVersion._date, |
| 130 | + fileUrls: { |
| 131 | + main: await this.#source.urlFor(`${indexDataset.path}/${indexVersion.tag}/${versionMeta.files.treeJson}`) |
| 132 | + } |
| 133 | + }, |
| 134 | + }; |
| 135 | + }) |
| 136 | + )).flat(); |
| 137 | + }) |
| 138 | + )).flat(); |
| 139 | + } |
| 140 | + |
| 141 | + categorise(item) { |
| 142 | + return item; |
| 143 | + } |
| 144 | + |
| 145 | + createResource(resourceType, resourcePath, items) { |
| 146 | + return { |
| 147 | + versions: items.map(i => i.version), |
| 148 | + }; |
| 149 | + } |
| 150 | +} |
0 commit comments