/
add-assembly.ts
623 lines (586 loc) · 19.4 KB
/
add-assembly.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import { Args, Flags } from '@oclif/core'
import fs from 'fs'
import path from 'path'
import JBrowseCommand, { Assembly, Sequence, Config } from '../base'
const { rename, copyFile, mkdir, symlink } = fs.promises
function isValidJSON(string: string) {
try {
JSON.parse(string)
return true
} catch (error) {
return false
}
}
export default class AddAssembly extends JBrowseCommand {
// @ts-expect-error
target: string
static description = 'Add an assembly to a JBrowse 2 configuration'
static examples = [
'# add assembly to installation in current directory. assumes .fai file also exists, and copies GRCh38.fa and GRCh38.fa.fai to current directory',
'$ jbrowse add-assembly GRCh38.fa --load copy',
'',
'# add assembly to a specific jb2 installation path using --out, and copies the .fa and .fa.fai file to /path/to/jb2',
'$ jbrowse add-assembly GRCh38.fa --out /path/to/jb2/ --load copy',
'',
'# force indexedFasta for add-assembly without relying on file extension',
'$ jbrowse add-assembly GRCh38.xyz --type indexedFasta --load copy',
'',
'# add displayName for an assembly',
'$ jbrowse add-assembly myFile.fa.gz --name hg38 --displayName "Homo sapiens (hg38)"',
'',
'# use chrom.sizes file for assembly instead of a fasta file',
'$ jbrowse add-assembly GRCh38.chrom.sizes --load inPlace',
'',
'# add assembly from preconfigured json file, expert option',
'$ jbrowse add-assembly GRCh38.config.json --load copy',
'',
'# add assembly from a 2bit file, also note pointing direct to a URL so no --load flag needed',
'$ jbrowse add-assembly https://example.com/data/sample.2bit',
'',
'# add a bgzip indexed fasta inferred by fa.gz extension. assumes .fa.gz.gzi and .fa.gz.fai files also exists',
'$ jbrowse add-assembly myfile.fa.gz --load copy',
]
static args = {
sequence: Args.string({
required: true,
description: `sequence file or URL
If TYPE is indexedFasta or bgzipFasta, the index file defaults to <location>.fai
and can be optionally specified with --faiLocation
If TYPE is bgzipFasta, the gzip index file defaults to <location>.gzi and can be
optionally specified with --gziLocation`,
}),
}
static flags = {
type: Flags.string({
char: 't',
description: `type of sequence, by default inferred from sequence file
indexedFasta An index FASTA (e.g. .fa or .fasta) file;
can optionally specify --faiLocation
bgzipFasta A block-gzipped and indexed FASTA (e.g. .fa.gz or .fasta.gz) file;
can optionally specify --faiLocation and/or --gziLocation
twoBit A twoBit (e.g. .2bit) file
chromSizes A chromosome sizes (e.g. .chrom.sizes) file
custom Either a JSON file location or inline JSON that defines a custom
sequence adapter; must provide --name if using inline JSON`,
options: ['indexedFasta', 'bgzipFasta', 'twoBit', 'chromSizes', 'custom'],
}),
name: Flags.string({
char: 'n',
description:
'Name of the assembly; if not specified, will be guessed using the sequence file name',
}),
alias: Flags.string({
char: 'a',
description:
'An alias for the assembly name (e.g. "hg38" if the name of the assembly is "GRCh38");\ncan be specified multiple times',
multiple: true,
}),
displayName: Flags.string({
description:
'The display name to specify for the assembly, e.g. "Homo sapiens (hg38)" while the name can be a shorter identifier like "hg38"',
}),
faiLocation: Flags.string({
description: '[default: <fastaLocation>.fai] FASTA index file or URL',
}),
gziLocation: Flags.string({
description:
'[default: <fastaLocation>.gzi] FASTA gzip index file or URL',
}),
refNameAliases: Flags.string({
description:
'Reference sequence name aliases file or URL; assumed to be a tab-separated aliases\nfile unless --refNameAliasesType is specified',
}),
refNameAliasesType: Flags.string({
description:
'Type of aliases defined by --refNameAliases; if "custom", --refNameAliases is either\na JSON file location or inline JSON that defines a custom sequence adapter',
options: ['aliases', 'custom'],
dependsOn: ['refNameAliases'],
}),
refNameColors: Flags.string({
description:
'A comma-separated list of color strings for the reference sequence names; will cycle\nthrough colors if there are fewer colors than sequences',
}),
target: Flags.string({
description:
'path to config file in JB2 installation directory to write out to.\nCreates ./config.json if nonexistent',
}),
out: Flags.string({
description: 'synonym for target',
}),
help: Flags.help({ char: 'h' }),
load: Flags.string({
char: 'l',
description:
'Required flag when using a local file. Choose how to manage the data directory. Copy, symlink, or move the data directory to the JBrowse directory. Or use inPlace to modify the config without doing any file operations',
options: ['copy', 'symlink', 'move', 'inPlace'],
}),
skipCheck: Flags.boolean({
description:
"Don't check whether or not the sequence file or URL exists or if you are in a JBrowse directory",
}),
overwrite: Flags.boolean({
description:
'Overwrite existing assembly if one with the same name exists',
}),
force: Flags.boolean({
char: 'f',
description: 'Equivalent to `--skipCheck --overwrite`',
}),
}
async getAssembly(): Promise<Assembly> {
let sequence: Sequence
const { args: runArgs, flags: runFlags } = await this.parse(AddAssembly)
const { sequence: argsSequence } = runArgs as { sequence: string }
if (this.needLoadData(argsSequence) && !runFlags.load) {
this.error(
`Please specify the loading operation for this file with --load copy|symlink|move|inPlace`,
{ exit: 110 },
)
} else if (!this.needLoadData(argsSequence) && runFlags.load) {
this.error(
`URL detected with --load flag. Please rerun the function without the --load flag`,
{ exit: 120 },
)
}
let { name } = runFlags
let { type } = runFlags as {
type:
| 'indexedFasta'
| 'bgzipFasta'
| 'twoBit'
| 'chromSizes'
| 'custom'
| undefined
}
if (type) {
this.debug(`Type is: ${type}`)
} else {
type = this.guessSequenceType(argsSequence)
this.debug(`No type specified, guessing type: ${type}`)
}
if (name) {
this.debug(`Name is: ${name}`)
}
switch (type) {
case 'indexedFasta': {
const { skipCheck, force, load, faiLocation } = runFlags
let sequenceLocation = await this.resolveFileLocation(
argsSequence,
!(skipCheck || force),
load === 'inPlace',
)
this.debug(`FASTA location resolved to: ${sequenceLocation}`)
let indexLocation = await this.resolveFileLocation(
faiLocation || `${argsSequence}.fai`,
!(skipCheck || force),
load === 'inPlace',
)
this.debug(`FASTA index location resolved to: ${indexLocation}`)
if (!name) {
name = path.basename(
sequenceLocation,
sequenceLocation.endsWith('.fasta') ? '.fasta' : '.fa',
)
this.debug(`Guessing name: ${name}`)
}
const loaded = load
? await this.loadData(load, [sequenceLocation, indexLocation])
: false
if (loaded) {
sequenceLocation = path.basename(sequenceLocation)
indexLocation = path.basename(indexLocation)
}
sequence = {
type: 'ReferenceSequenceTrack',
trackId: `${name}-ReferenceSequenceTrack`,
adapter: {
type: 'IndexedFastaAdapter',
fastaLocation: {
uri: sequenceLocation,
locationType: 'UriLocation',
},
faiLocation: { uri: indexLocation, locationType: 'UriLocation' },
},
}
break
}
case 'bgzipFasta': {
let sequenceLocation = await this.resolveFileLocation(
argsSequence,
!(runFlags.skipCheck || runFlags.force),
runFlags.load === 'inPlace',
)
this.debug(`compressed FASTA location resolved to: ${sequenceLocation}`)
let indexLocation = await this.resolveFileLocation(
runFlags.faiLocation || `${sequenceLocation}.fai`,
!(runFlags.skipCheck || runFlags.force),
runFlags.load === 'inPlace',
)
this.debug(
`compressed FASTA index location resolved to: ${indexLocation}`,
)
let bgzipIndexLocation = await this.resolveFileLocation(
runFlags.gziLocation || `${sequenceLocation}.gzi`,
!(runFlags.skipCheck || runFlags.force),
runFlags.load === 'inPlace',
)
this.debug(`bgzip index location resolved to: ${bgzipIndexLocation}`)
if (!name) {
name = path.basename(
sequenceLocation,
sequenceLocation.endsWith('.fasta.gz') ? '.fasta.gz' : '.fa.gz',
)
this.debug(`Guessing name: ${name}`)
}
const loaded = runFlags.load
? await this.loadData(runFlags.load, [
sequenceLocation,
indexLocation,
bgzipIndexLocation,
])
: false
if (loaded) {
sequenceLocation = path.basename(sequenceLocation)
indexLocation = path.basename(indexLocation)
bgzipIndexLocation = path.basename(bgzipIndexLocation)
}
sequence = {
type: 'ReferenceSequenceTrack',
trackId: `${name}-ReferenceSequenceTrack`,
adapter: {
type: 'BgzipFastaAdapter',
fastaLocation: {
uri: sequenceLocation,
locationType: 'UriLocation',
},
faiLocation: { uri: indexLocation, locationType: 'UriLocation' },
gziLocation: {
uri: bgzipIndexLocation,
locationType: 'UriLocation',
},
},
}
break
}
case 'twoBit': {
let sequenceLocation = await this.resolveFileLocation(
argsSequence,
!(runFlags.skipCheck || runFlags.force),
runFlags.load === 'inPlace',
)
this.debug(`2bit location resolved to: ${sequenceLocation}`)
if (!name) {
name = path.basename(sequenceLocation, '.2bit')
this.debug(`Guessing name: ${name}`)
}
const loaded = runFlags.load
? await this.loadData(runFlags.load, [sequenceLocation])
: false
if (loaded) {
sequenceLocation = path.basename(sequenceLocation)
}
sequence = {
type: 'ReferenceSequenceTrack',
trackId: `${name}-ReferenceSequenceTrack`,
adapter: {
type: 'TwoBitAdapter',
twoBitLocation: {
uri: sequenceLocation,
locationType: 'UriLocation',
},
},
}
break
}
case 'chromSizes': {
let sequenceLocation = await this.resolveFileLocation(
argsSequence,
!(runFlags.skipCheck || runFlags.force),
runFlags.load === 'inPlace',
)
this.debug(`chrom.sizes location resolved to: ${sequenceLocation}`)
if (!name) {
name = path.basename(sequenceLocation, '.chrom.sizes')
this.debug(`Guessing name: ${name}`)
}
const loaded = runFlags.load
? await this.loadData(runFlags.load, [sequenceLocation])
: false
if (loaded) {
sequenceLocation = path.basename(sequenceLocation)
}
sequence = {
type: 'ReferenceSequenceTrack',
trackId: `${name}-ReferenceSequenceTrack`,
adapter: {
type: 'ChromSizesAdapter',
chromSizesLocation: {
uri: sequenceLocation,
locationType: 'UriLocation',
},
},
}
break
}
case 'custom': {
const adapter = await this.readInlineOrFileJson(argsSequence)
this.debug(`Custom adapter: ${JSON.stringify(adapter)}`)
if (!name) {
if (isValidJSON(argsSequence)) {
this.error(
'Must provide --name when using custom inline JSON sequence',
{ exit: 130 },
)
} else {
name = path.basename(argsSequence, '.json')
}
this.debug(`Guessing name: ${name}`)
}
if (!('type' in adapter)) {
this.error(
`No "type" specified in sequence adapter "${JSON.stringify(
adapter,
)}"`,
{ exit: 140 },
)
}
sequence = {
type: 'ReferenceSequenceTrack',
trackId: `${name}-ReferenceSequenceTrack`,
adapter,
}
break
}
}
return { name, sequence }
}
async run() {
// https://stackoverflow.com/a/35008327/2129219
const exists = (s: string) =>
new Promise(r => fs.access(s, fs.constants.F_OK, e => r(!e)))
const { args: runArgs, flags: runFlags } = await this.parse(AddAssembly)
const output = runFlags.target || runFlags.out || '.'
if (!(await exists(output))) {
const dir = output.endsWith('.json') ? path.dirname(output) : output
await mkdir(dir, { recursive: true })
}
let isDir = false
try {
isDir = fs.statSync(output).isDirectory()
} catch (e) {}
this.target = isDir ? path.join(output, 'config.json') : output
const { sequence: argsSequence } = runArgs as { sequence: string }
this.debug(`Sequence location is: ${argsSequence}`)
const { name } = runFlags
const assembly = await this.getAssembly()
if (runFlags.alias?.length) {
this.debug(`Adding assembly aliases: ${runFlags.alias}`)
assembly.aliases = runFlags.alias
}
if (runFlags.refNameColors) {
const colors = runFlags.refNameColors
.split(',')
.map(color => color.trim())
this.debug(`Adding refName colors: ${colors}`)
assembly.refNameColors = colors
}
if (runFlags.refNameAliases) {
if (
runFlags.refNameAliasesType &&
runFlags.refNameAliasesType === 'custom'
) {
const refNameAliasesConfig = await this.readInlineOrFileJson(
runFlags.refNameAliases,
)
if (!refNameAliasesConfig.type) {
this.error(
`No "type" specified in refNameAliases adapter "${JSON.stringify(
refNameAliasesConfig,
)}"`,
{ exit: 150 },
)
}
this.debug(
`Adding custom refNameAliases config: ${JSON.stringify(
refNameAliasesConfig,
)}`,
)
assembly.refNameAliases = {
adapter: refNameAliasesConfig,
}
} else {
const refNameAliasesLocation = await this.resolveFileLocation(
runFlags.refNameAliases,
!(runFlags.skipCheck || runFlags.force),
runFlags.load === 'inPlace',
)
this.debug(
`refName aliases file location resolved to: ${refNameAliasesLocation}`,
)
assembly.refNameAliases = {
adapter: {
type: 'RefNameAliasAdapter',
location: {
uri: refNameAliasesLocation,
locationType: 'UriLocation',
},
},
}
}
}
if (runFlags.displayName) {
assembly.displayName = runFlags.displayName
}
const defaultConfig: Config = {
assemblies: [],
configuration: {},
connections: [],
defaultSession: {
name: 'New Session',
},
tracks: [],
}
let configContents: Config
if (fs.existsSync(this.target)) {
this.debug(`Found existing config file ${this.target}`)
configContents = {
...defaultConfig,
...(await this.readJsonFile(this.target)),
}
} else {
this.debug(`Creating config file ${this.target}`)
configContents = { ...defaultConfig }
}
if (!configContents.assemblies) {
configContents.assemblies = []
}
const idx = configContents.assemblies.findIndex(
configAssembly => configAssembly.name === assembly.name,
)
if (idx !== -1) {
this.debug(`Found existing assembly ${name} in configuration`)
if (runFlags.overwrite || runFlags.force) {
this.debug(`Overwriting assembly ${name} in configuration`)
configContents.assemblies[idx] = assembly
} else {
this.error(
`Cannot add assembly with name ${assembly.name}, an assembly with that name already exists`,
{ exit: 160 },
)
}
} else {
configContents.assemblies.push(assembly)
}
this.debug(`Writing configuration to file ${this.target}`)
await this.writeJsonFile(this.target, configContents)
this.log(
`${idx !== -1 ? 'Overwrote' : 'Added'} assembly "${assembly.name}" ${
idx !== -1 ? 'in' : 'to'
} ${this.target}`,
)
}
guessSequenceType(sequence: string) {
if (
sequence.endsWith('.fa') ||
sequence.endsWith('.fna') ||
sequence.endsWith('.fasta')
) {
return 'indexedFasta'
}
if (
sequence.endsWith('.fa.gz') ||
sequence.endsWith('.fna.gz') ||
sequence.endsWith('.fasta.gz')
) {
return 'bgzipFasta'
}
if (sequence.endsWith('.2bit')) {
return 'twoBit'
}
if (sequence.endsWith('.chrom.sizes')) {
return 'chromSizes'
}
if (sequence.endsWith('.json')) {
return 'custom'
}
if (isValidJSON(sequence)) {
return 'custom'
}
return this.error(
'Could not determine sequence type automatically, add --type to specify it',
{ exit: 170 },
)
}
needLoadData(location: string) {
let locationUrl: URL | undefined
try {
locationUrl = new URL(location)
} catch (error) {
// ignore
}
if (locationUrl) {
return false
}
return true
}
async loadData(load: string, filePaths: string[]) {
let locationUrl: URL | undefined
const destination = this.target
try {
locationUrl = new URL(filePaths[0])
} catch (error) {
// ignore
}
if (locationUrl) {
return false
}
switch (load) {
case 'copy': {
await Promise.all(
filePaths.map(async filePath => {
if (!filePath) {
return undefined
}
return copyFile(
filePath,
path.join(path.dirname(destination), path.basename(filePath)),
)
}),
)
return true
}
case 'symlink': {
await Promise.all(
filePaths.map(async filePath => {
if (!filePath) {
return undefined
}
return symlink(
path.resolve(filePath),
path.join(path.dirname(destination), path.basename(filePath)),
)
}),
)
return true
}
case 'move': {
await Promise.all(
filePaths.map(async filePath => {
if (!filePath) {
return undefined
}
return rename(
filePath,
path.join(path.dirname(destination), path.basename(filePath)),
)
}),
)
return true
}
case 'inPlace': {
return false
}
}
return false
}
}