Skip to content

Commit ec0e07c

Browse files
committed
feat: Load columns from .tsv.gz files
1 parent 88abfb8 commit ec0e07c

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

src/schema/context.ts

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { readEntities } from './entities.ts'
1818
import { DatasetIssues } from '../issues/datasetIssues.ts'
1919
import { walkBack } from '../files/inheritance.ts'
2020
import { parseGzip } from '../files/gzip.ts'
21-
import { loadTSV } from '../files/tsv.ts'
21+
import { loadTSV, loadTSVGZ } from '../files/tsv.ts'
2222
import { parseTIFF } from '../files/tiff.ts'
2323
import { loadJSON } from '../files/json.ts'
2424
import { loadHeader } from '../files/nifti.ts'
@@ -254,21 +254,40 @@ export class BIDSContext implements Context {
254254
}
255255

256256
async loadColumns(): Promise<void> {
257-
if (this.extension !== '.tsv') {
258-
return
257+
if (this.extension == '.tsv') {
258+
this.columns = await loadTSV(this.file, this.dataset.options?.maxRows)
259+
.catch((error) => {
260+
if (error.code) {
261+
this.dataset.issues.add({ ...error, location: this.file.path })
262+
}
263+
logger.warn(
264+
`tsv file could not be opened by loadColumns '${this.file.path}'`,
265+
)
266+
logger.debug(error)
267+
return new Map<string, string[]>() as ColumnsMap
268+
}) as Record<string, string[]>
269+
} else if (this.extension == '.tsv.gz') {
270+
const headers = this.sidecar.Columns as string[];
271+
if (!headers || this.size === 0) {
272+
// Missing Columns will be caught by sidecar rules
273+
// Note that these rules currently select for suffix, and will need to be generalized
274+
// or duplicated for new .tsv.gz files
275+
// `this.size === 0` will show as `EMPTY_FILE`, so do not add INVALID_GZIP
276+
return
277+
}
278+
this.columns = await loadTSVGZ(this.file, headers, this.dataset.options?.maxRows)
279+
.catch((error) => {
280+
if (error.code) {
281+
this.dataset.issues.add({ ...error, location: this.file.path })
282+
}
283+
logger.warn(
284+
`tsv.gz file could not be opened by loadColumns '${this.file.path}'`,
285+
)
286+
logger.debug(error)
287+
return new Map<string, string[]>() as ColumnsMap
288+
}) as Record<string, string[]>
259289
}
260290

261-
this.columns = await loadTSV(this.file, this.dataset.options?.maxRows)
262-
.catch((error) => {
263-
if (error.code) {
264-
this.dataset.issues.add({ ...error, location: this.file.path })
265-
}
266-
logger.warn(
267-
`tsv file could not be opened by loadColumns '${this.file.path}'`,
268-
)
269-
logger.debug(error)
270-
return new Map<string, string[]>() as ColumnsMap
271-
}) as Record<string, string[]>
272291
return
273292
}
274293

@@ -340,15 +359,24 @@ export class BIDSContext implements Context {
340359
}
341360

342361
async asyncLoads() {
343-
await Promise.allSettled([
344-
this.loadSubjects(),
362+
// loaders that may be depended on by other loaders
363+
const initial = [
345364
this.loadSidecar(),
346-
this.loadColumns(),
347365
this.loadAssociations(),
366+
]
367+
// loaders that do not depend on other loaders
368+
const independent = [
369+
this.loadSubjects(),
348370
this.loadNiftiHeader(),
349371
this.loadJSON(),
350372
this.loadGzip(),
351373
this.loadTIFF(),
352-
])
374+
]
375+
376+
// Loaders with dependencies
377+
await Promise.allSettled(initial)
378+
await this.loadColumns()
379+
380+
await Promise.allSettled(independent)
353381
}
354382
}

0 commit comments

Comments
 (0)