-
Notifications
You must be signed in to change notification settings - Fork 60
/
text-index.ts
523 lines (488 loc) · 14.3 KB
/
text-index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import fs from 'fs'
import path from 'path'
import { Readable } from 'stream'
import { ixIxxStream } from 'ixixx'
import { Flags } from '@oclif/core'
// locals
import { indexGff3 } from '../types/gff3Adapter'
import { indexVcf } from '../types/vcfAdapter'
import JBrowseCommand, {
Track,
Config,
TrixTextSearchAdapter,
UriLocation,
LocalPathLocation,
} from '../base'
import {
generateMeta,
supported,
guessAdapterFromFileName,
} from '../types/common'
function readConf(path: string) {
return JSON.parse(fs.readFileSync(path, 'utf8')) as Config
}
function writeConf(obj: Config, path: string) {
fs.writeFileSync(path, JSON.stringify(obj, null, 2))
}
function getLoc(elt: UriLocation | LocalPathLocation) {
return elt.locationType === 'LocalPathLocation' ? elt.localPath : elt.uri
}
export default class TextIndex extends JBrowseCommand {
static description = 'Make a text-indexing file for any given track(s).'
static examples = [
"# indexes all tracks that it can find in the current directory's config.json",
'$ jbrowse text-index',
'',
"# indexes specific trackIds that it can find in the current directory's config.json",
'$ jbrowse text-index --tracks=track1,track2,track3',
'',
"# indexes all tracks in a directory's config.json or in a specific config file",
'$ jbrowse text-index --out /path/to/jb2/',
'',
'# indexes only a specific assembly, and overwrite what was previously there using force (which is needed if a previous index already existed)',
'$ jbrowse text-index -a hg19 --force',
'',
'# create index for some files for use in @jbrowse/react-linear-genome-view or similar',
'$ jbrowse text-index --file myfile.gff3.gz --file myfile.vcfgz --out indexes',
]
static flags = {
help: Flags.help({ char: 'h' }),
tracks: Flags.string({
description: `Specific tracks to index, formatted as comma separated trackIds. If unspecified, indexes all available tracks`,
}),
target: Flags.string({
description:
'Path to config file in JB2 installation directory to read from.',
}),
out: Flags.string({
description: 'Synonym for target',
}),
attributes: Flags.string({
description: 'Comma separated list of attributes to index',
default: 'Name,ID',
}),
assemblies: Flags.string({
char: 'a',
description:
'Specify the assembl(ies) to create an index for. If unspecified, creates an index for each assembly in the config',
}),
force: Flags.boolean({
default: false,
description: 'Overwrite previously existing indexes',
}),
quiet: Flags.boolean({
char: 'q',
default: false,
description: 'Hide the progress bars',
}),
perTrack: Flags.boolean({
default: false,
description: 'If set, creates an index per track',
}),
exclude: Flags.string({
description: 'Adds gene type to list of excluded types',
default: 'CDS,exon',
}),
prefixSize: Flags.integer({
description:
'Specify the prefix size for the ixx index. We attempt to automatically calculate this, but you can manually specify this too. If many genes have similar gene IDs e.g. Z000000001, Z000000002 the prefix size should be larger so that they get split into different bins',
}),
file: Flags.string({
description:
'File or files to index (can be used to create trix indexes for embedded component use cases not using a config.json for example)',
multiple: true,
}),
fileId: Flags.string({
description:
'Set the trackId used for the indexes generated with the --file argument',
multiple: true,
}),
dryrun: Flags.boolean({
description:
'Just print out tracks that will be indexed by the process, without doing any indexing',
}),
}
async run() {
const { flags } = await this.parse(TextIndex)
const { perTrack, file } = flags
if (file) {
await this.indexFileList()
} else if (perTrack) {
await this.perTrackIndex()
} else {
await this.aggregateIndex()
}
this.log('Finished!')
}
async aggregateIndex() {
const { flags } = await this.parse(TextIndex)
const {
out,
target,
tracks,
assemblies,
attributes,
quiet,
force,
exclude,
dryrun,
prefixSize,
} = flags
const outFlag = target || out || '.'
const isDir = fs.lstatSync(outFlag).isDirectory()
const confPath = isDir ? path.join(outFlag, 'config.json') : outFlag
const outLocation = path.dirname(confPath)
const config = readConf(confPath)
const trixDir = path.join(outLocation, 'trix')
if (!fs.existsSync(trixDir)) {
fs.mkdirSync(trixDir)
}
const aggregateTextSearchAdapters = config.aggregateTextSearchAdapters || []
const asms =
assemblies?.split(',') ||
config.assemblies?.map(a => a.name) ||
(config.assembly ? [config.assembly.name] : [])
if (!asms?.length) {
throw new Error('No assemblies found')
}
for (const asm of asms) {
const trackConfigs = await this.getTrackConfigs(
confPath,
tracks?.split(','),
asm,
)
if (!trackConfigs.length) {
this.log('Indexing assembly ' + asm + '...(no tracks found)...')
continue
}
this.log('Indexing assembly ' + asm + '...')
if (dryrun) {
this.log(
trackConfigs.map(e => `${e.trackId}\t${e.adapter.type}`).join('\n'),
)
} else {
const id = asm + '-index'
const idx = aggregateTextSearchAdapters.findIndex(
x => x.textSearchAdapterId === id,
)
if (idx !== -1 && !force) {
this.log(
`Note: ${asm} has already been indexed with this configuration, use --force to overwrite this assembly. Skipping for now`,
)
continue
}
await this.indexDriver({
trackConfigs,
outLocation,
quiet,
name: asm,
attributes: attributes.split(','),
typesToExclude: exclude.split(','),
assemblyNames: [asm],
prefixSize,
})
const trixConf = {
type: 'TrixTextSearchAdapter',
textSearchAdapterId: id,
ixFilePath: {
uri: `trix/${asm}.ix`,
locationType: 'UriLocation',
},
ixxFilePath: {
uri: `trix/${asm}.ixx`,
locationType: 'UriLocation',
},
metaFilePath: {
uri: `trix/${asm}_meta.json`,
locationType: 'UriLocation',
},
assemblyNames: [asm],
} as TrixTextSearchAdapter
if (idx === -1) {
aggregateTextSearchAdapters.push(trixConf)
} else {
aggregateTextSearchAdapters[idx] = trixConf
}
}
}
if (!dryrun) {
writeConf(
{
...config,
aggregateTextSearchAdapters,
},
confPath,
)
}
}
async perTrackIndex() {
const { flags } = await this.parse(TextIndex)
const {
out,
target,
tracks,
assemblies,
attributes,
quiet,
force,
exclude,
prefixSize,
} = flags
const outFlag = target || out || '.'
const isDir = fs.lstatSync(outFlag).isDirectory()
const confFilePath = isDir ? path.join(outFlag, 'config.json') : outFlag
const outLocation = path.dirname(confFilePath)
const config = readConf(confFilePath)
const configTracks = config.tracks || []
const trixDir = path.join(outLocation, 'trix')
if (!fs.existsSync(trixDir)) {
fs.mkdirSync(trixDir)
}
if (assemblies) {
throw new Error(
`Can't specify assemblies when indexing per track, remove assemblies flag to continue.`,
)
}
const confs = await this.getTrackConfigs(confFilePath, tracks?.split(','))
if (!confs.length) {
throw new Error(
`Tracks not found in config.json, please add track configurations before indexing.`,
)
}
for (const trackConfig of confs) {
const { textSearching, trackId, assemblyNames } = trackConfig
if (textSearching?.textSearchAdapter && !force) {
this.log(
`Note: ${trackId} has already been indexed with this configuration, use --force to overwrite this track. Skipping for now`,
)
continue
}
this.log('Indexing track ' + trackId + '...')
await this.indexDriver({
trackConfigs: [trackConfig],
attributes: attributes.split(','),
outLocation,
quiet,
name: trackId,
typesToExclude: exclude.split(','),
assemblyNames,
prefixSize,
})
if (!textSearching?.textSearchAdapter) {
// modifies track with new text search adapter
const index = configTracks.findIndex(track => trackId === track.trackId)
if (index !== -1) {
configTracks[index] = {
...trackConfig,
textSearching: {
...textSearching,
textSearchAdapter: {
type: 'TrixTextSearchAdapter',
textSearchAdapterId: trackId + '-index',
ixFilePath: {
uri: `trix/${trackId}.ix`,
locationType: 'UriLocation' as const,
},
ixxFilePath: {
uri: `trix/${trackId}.ixx`,
locationType: 'UriLocation' as const,
},
metaFilePath: {
uri: `trix/${trackId}_meta.json`,
locationType: 'UriLocation' as const,
},
assemblyNames: assemblyNames,
},
},
}
} else {
this.log("Error: can't find trackId")
}
}
writeConf({ ...config, tracks: configTracks }, confFilePath)
}
}
async indexFileList() {
const { flags } = await this.parse(TextIndex)
const {
out,
target,
fileId,
file,
attributes,
quiet,
exclude,
prefixSize,
} = flags
if (!file) {
throw new Error('Cannot index file list without files')
}
const outFlag = target || out || '.'
const trixDir = path.join(outFlag, 'trix')
if (!fs.existsSync(trixDir)) {
fs.mkdirSync(trixDir)
}
const trackConfigs = file
.map(file => guessAdapterFromFileName(file))
.filter(fileConfig => supported(fileConfig.adapter.type))
if (fileId?.length) {
for (let i = 0; i < fileId.length; i++) {
trackConfigs[i].trackId = fileId[i]
}
}
await this.indexDriver({
trackConfigs,
outLocation: outFlag,
name: trackConfigs.length > 1 ? 'aggregate' : path.basename(file[0]),
quiet,
attributes: attributes.split(','),
typesToExclude: exclude.split(','),
assemblyNames: [],
prefixSize,
})
this.log(
'Successfully created index for these files. See https://jbrowse.org/storybook/lgv/main/?path=/story/text-searching--page for info about usage',
)
}
async indexDriver({
trackConfigs,
attributes,
outLocation,
name,
quiet,
typesToExclude,
assemblyNames,
prefixSize,
}: {
trackConfigs: Track[]
attributes: string[]
outLocation: string
name: string
quiet: boolean
typesToExclude: string[]
assemblyNames: string[]
prefixSize?: number
}) {
const readStream = Readable.from(
this.indexFiles({
trackConfigs,
attributes,
outLocation,
quiet,
typesToExclude,
}),
)
const ixIxxStream = await this.runIxIxx({
readStream,
outLocation,
name,
prefixSize,
})
await generateMeta({
trackConfigs,
attributes,
outLocation,
name,
typesToExclude,
assemblyNames,
})
return ixIxxStream
}
async *indexFiles({
trackConfigs,
attributes,
outLocation,
quiet,
typesToExclude,
}: {
trackConfigs: Track[]
attributes: string[]
outLocation: string
quiet: boolean
typesToExclude: string[]
}) {
for (const config of trackConfigs) {
const { adapter, textSearching } = config
const { type } = adapter
const {
indexingFeatureTypesToExclude = typesToExclude,
indexingAttributes = attributes,
} = textSearching || {}
let loc
if (type === 'Gff3TabixAdapter') {
loc = adapter.gffGzLocation
} else if (type === 'Gff3Adapter') {
loc = adapter.gffLocation
} else if (type === 'VcfAdapter') {
loc = adapter.vcfLocation
} else if (type === 'VcfTabixAdapter') {
loc = adapter.vcfGzLocation
}
if (!loc) {
return
}
if (type === 'Gff3TabixAdapter' || type === 'Gff3Adapter') {
yield* indexGff3({
config,
attributesToIndex: indexingAttributes,
inLocation: getLoc(loc),
outLocation,
typesToExclude: indexingFeatureTypesToExclude,
quiet,
})
} else if (type === 'VcfTabixAdapter' || type === 'VcfAdapter') {
yield* indexVcf({
config,
attributesToIndex: indexingAttributes,
inLocation: getLoc(loc),
outLocation,
typesToExclude: indexingFeatureTypesToExclude,
quiet,
})
}
}
}
runIxIxx({
readStream,
outLocation,
name,
prefixSize,
}: {
readStream: Readable
outLocation: string
name: string
prefixSize?: number
}) {
return ixIxxStream(
readStream,
path.join(outLocation, 'trix', `${name}.ix`),
path.join(outLocation, 'trix', `${name}.ixx`),
prefixSize,
)
}
async getTrackConfigs(
configPath: string,
trackIds?: string[],
assemblyName?: string,
) {
const { tracks } = readConf(configPath)
if (!tracks) {
return []
}
const trackIdsToIndex = trackIds || tracks?.map(track => track.trackId)
return trackIdsToIndex
.map(trackId => {
const currentTrack = tracks.find(t => trackId === t.trackId)
if (!currentTrack) {
throw new Error(
`Track not found in config.json for trackId ${trackId}, please add track configuration before indexing.`,
)
}
return currentTrack
})
.filter(track => supported(track.adapter?.type))
.filter(track =>
assemblyName ? track.assemblyNames.includes(assemblyName) : true,
)
}
}