@@ -18,7 +18,7 @@ import { readEntities } from './entities.ts'
1818import { DatasetIssues } from '../issues/datasetIssues.ts'
1919import { walkBack } from '../files/inheritance.ts'
2020import { parseGzip } from '../files/gzip.ts'
21- import { loadTSV } from '../files/tsv.ts'
21+ import { loadTSV , loadTSVGZ } from '../files/tsv.ts'
2222import { parseTIFF } from '../files/tiff.ts'
2323import { loadJSON } from '../files/json.ts'
2424import { 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