Skip to content

Commit

Permalink
Misc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Jun 1, 2023
1 parent c93c7de commit 71e7c68
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 103 deletions.
5 changes: 1 addition & 4 deletions plugins/legacy-jbrowse/src/JBrowse1Connection/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ export default function (pluginManager: PluginManager) {
const { fetchJb1 } = await import('./jb1ConfigLoad')
const { convertTrackConfig } = await import('./jb1ToJb2')
const config = await fetchJb1(dataDirLocation)
const assemblyName = readConfObject(
self.configuration,
'assemblyNames',
)[0]
const assemblyName = getConf(self, 'assemblyNames')[0]
if (!assemblyName) {
throw new Error('assembly name required for JBrowse 1 connection')
}
Expand Down
15 changes: 7 additions & 8 deletions products/jbrowse-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ ARGUMENTS
For JBrowse 1, location of JB1 data directory similar to http://mysite.com/jbrowse/data/
OPTIONS
-a, --assemblyNames=assemblyNames Comma separated list of assembly name(s) to filter from this connection. Optional,
will show all assemblies from connection if unspecified
-a, --assemblyNames=assemblyNames For UCSC, optional: Comma separated list of assembly name(s) to filter from this
connection. For JBrowse: a single assembly name
-c, --config=config Any extra config settings to add to connection in JSON object format, such as
'{"uri":"url":"https://sample.com"}, "locationType": "UriLocation"}'
Expand All @@ -216,13 +216,12 @@ OPTIONS
--target=target path to config file in JB2 installation directory to write out to.
EXAMPLES
$ jbrowse add-connection http://mysite.com/jbrowse/data/
$ jbrowse add-connection http://mysite.com/jbrowse/custom_data_folder/ --type JBrowse1Connection
$ jbrowse add-connection http://mysite.com/path/to/hub.txt --assemblyName hg19
$ jbrowse add-connection http://mysite.com/path/to/custom_hub_name.txt --type UCSCTrackHubConnection --assemblyName
hg19
$ jbrowse add-connection http://mysite.com/jbrowse/data/ -a hg19
$ jbrowse add-connection http://mysite.com/jbrowse/custom_data_folder/ --type JBrowse1Connection -a hg38
$ jbrowse add-connection http://mysite.com/path/to/hub.txt
$ jbrowse add-connection http://mysite.com/path/to/custom_hub_name.txt --type UCSCTrackHubConnection
$ jbrowse add-connection http://mysite.com/path/to/custom --type custom --config
'{"uri":{"url":"https://mysite.com/path/to/custom"}, "locationType": "UriLocation"}' --assemblyName hg19
'{"uri":{"url":"https://mysite.com/path/to/custom"}, "locationType": "UriLocation"}' -a hg19
$ jbrowse add-connection https://mysite.com/path/to/hub.txt --connectionId newId --name newName --target
/path/to/jb2/installation/config.json
```
Expand Down
2 changes: 1 addition & 1 deletion products/jbrowse-cli/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default abstract class JBrowseCommand extends Command {
return fsPromises.readFile(location, { encoding: 'utf8' })
}

async readJsonFile(location: string): Promise<Record<string, unknown>> {
async readJsonFile<T>(location: string): Promise<T> {
let contents
try {
contents = await fsPromises.readFile(location, { encoding: 'utf8' })
Expand Down
23 changes: 1 addition & 22 deletions products/jbrowse-cli/src/commands/add-connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('add-connection', () => {
)
})
.command(['add-connection', 'https://mysite.com/jbrowse/data'])
.it('adds an JBrowse1 connection from a url', async ctx => {
.it('adds JBrowse1 connection from a url', async ctx => {
const contents = readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
Expand All @@ -125,28 +125,7 @@ describe('add-connection', () => {
],
})
})
setup
.nock('https://mysite.com', site => site.head('/custom').reply(200))
.do(async ctx => {
await copyFile(testConfig, path.join(ctx.dir, path.basename(testConfig)))

await rename(
path.join(ctx.dir, path.basename(testConfig)),
path.join(ctx.dir, 'config.json'),
)
})
.command([
'add-connection',
'https://mysite.com/custom',
'--type',
'newType',
'--connectionId',
'newConnectionId',
'--name',
'newName',
])
.exit(140)
.it('fails if custom without a config object')
setup
.nock('https://mysite.com', site => site.head('/custom').reply(200))
.do(async ctx => {
Expand Down
121 changes: 53 additions & 68 deletions products/jbrowse-cli/src/commands/add-connection.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { flags } from '@oclif/command'
import fetch from '../fetchWithProxy'
import fs from 'fs'
import path from 'path'
import parseJSON from 'json-parse-better-errors'

// locals
import fetch from '../fetchWithProxy'
import JBrowseCommand from '../base'

interface Connection {
[key: string]: unknown
}

interface Assembly {
name: string
sequence: { [key: string]: unknown }
}

interface Config {
assemblies?: { name: string; sequence: { [key: string]: unknown } }[]
assemblies?: Assembly[]
configuration?: {}
connections?: Connection[]
defaultSession?: {}
Expand All @@ -24,11 +31,11 @@ export default class AddConnection extends JBrowseCommand {
static description = 'Add a connection to a JBrowse 2 configuration'

static examples = [
'$ jbrowse add-connection http://mysite.com/jbrowse/data/',
'$ jbrowse add-connection http://mysite.com/jbrowse/custom_data_folder/ --type JBrowse1Connection',
'$ jbrowse add-connection http://mysite.com/path/to/hub.txt --assemblyName hg19',
'$ jbrowse add-connection http://mysite.com/path/to/custom_hub_name.txt --type UCSCTrackHubConnection --assemblyName hg19',
`$ jbrowse add-connection http://mysite.com/path/to/custom --type custom --config '{"uri":{"url":"https://mysite.com/path/to/custom"}, "locationType": "UriLocation"}' --assemblyName hg19`,
'$ jbrowse add-connection http://mysite.com/jbrowse/data/ -a hg19',
'$ jbrowse add-connection http://mysite.com/jbrowse/custom_data_folder/ --type JBrowse1Connection -a hg38',
'$ jbrowse add-connection http://mysite.com/path/to/hub.txt',
'$ jbrowse add-connection http://mysite.com/path/to/custom_hub_name.txt --type UCSCTrackHubConnection',
`$ jbrowse add-connection http://mysite.com/path/to/custom --type custom --config '{"uri":{"url":"https://mysite.com/path/to/custom"}, "locationType": "UriLocation"}' -a hg19`,
'$ jbrowse add-connection https://mysite.com/path/to/hub.txt --connectionId newId --name newName --target /path/to/jb2/installation/config.json',
]

Expand All @@ -49,7 +56,7 @@ export default class AddConnection extends JBrowseCommand {
assemblyNames: flags.string({
char: 'a',
description:
'Comma separated list of assembly name(s) to filter from this connection. Optional, will show all assemblies from connection if unspecified',
'For UCSC, optional: Comma separated list of assembly name(s) to filter from this connection. For JBrowse: a single assembly name',
}),
config: flags.string({
char: 'c',
Expand Down Expand Up @@ -91,15 +98,14 @@ export default class AddConnection extends JBrowseCommand {
const isDir = fs.lstatSync(output).isDirectory()
this.target = isDir ? `${output}/config.json` : output

const { connectionUrlOrPath: argsPath } = runArgs as {
connectionUrlOrPath: string
}
const { config, assemblyNames } = runFlags
let { type, name, connectionId } = runFlags
const { connectionUrlOrPath } = runArgs
const { assemblyNames, type, name, config, connectionId } = runFlags
const { skipCheck, force } = runFlags
const url = await this.resolveURL(argsPath, !(skipCheck || force))

const configContents: Config = await this.readJsonFile(this.target)
const url = await this.resolveURL(
connectionUrlOrPath,
!(skipCheck || force),
)
const configContents = await this.readJsonFile<Config>(this.target)
this.debug(`Using config file ${this.target}`)

if (!configContents.assemblies?.length) {
Expand All @@ -109,62 +115,46 @@ export default class AddConnection extends JBrowseCommand {
)
}

type = type || this.determineConnectionType(url, config)
connectionId =
const configType = type || this.determineConnectionType(url)
const id =
connectionId ||
[type, assemblyNames, +Date.now()].filter(f => !!f).join('-')
name = name || connectionId

let configObj = {}
if (config) {
configObj = parseJSON(config)
}
const connectionConfig: Connection = {
type,
connectionId,
name: name || connectionId,
assemblyNames: assemblyNames?.split(','),
...configObj,
}

switch (type) {
case 'UCSCTrackHubConnection': {
connectionConfig.hubTxtLocation = {
uri: url,
locationType: 'UriLocation',
}
break
}
case 'JBrowse1Connection': {
connectionConfig.dataDirLocation = {
uri: url,
locationType: 'UriLocation',
}
break
}
default: {
if (!config || !this.isValidJSON(config)) {
this.error(
'When type is not UCSCTrackHubConnection or JBrowse1Connection, config object must be provided.\nPlease enter a config object using --config',
{ exit: 140 },
)
}

break
}
[configType, assemblyNames, +Date.now()].filter(f => !!f).join('-')
const connectionConfig = {
type: configType,
name: name || id,
...(configType === 'UCSCTrackHubConnection'
? {
hubTxtLocation: {
uri: url,
locationType: 'UriLocation',
},
}
: {}),
...(configType === 'JBrowse1Connection'
? {
dataDirLocation: {
uri: url,
locationType: 'UriLocation',
},
}
: {}),
connectionId: id,
assemblyNames:
assemblyNames || type === 'JBrowse1Connection'
? [configContents.assemblies[0]?.name]
: undefined,
...(config ? parseJSON(config) : {}),
}

if (!configContents.connections) {
configContents.connections = []
}
const idx = configContents.connections.findIndex(
configConnection => configConnection.connectionId === connectionId,
c => c.connectionId === connectionId,
)

if (idx !== -1) {
this.debug(`Found existing connectionId ${connectionId} in configuration`)
if (runFlags.force || runFlags.overwrite) {
this.debug(`Overwriting connection ${connectionId} in configuration`)
configContents.connections[idx] = connectionConfig
} else {
this.error(
Expand Down Expand Up @@ -213,19 +203,14 @@ export default class AddConnection extends JBrowseCommand {
})
}

determineConnectionType(url: string, config: string | undefined) {
determineConnectionType(url: string) {
if (path.basename(url) === 'hub.txt') {
return 'UCSCTrackHubConnection'
}
if (url.includes('jbrowse/data')) {
return 'JBrowse1Connection'
}
if (config && this.isValidJSON(config)) {
return 'custom'
}
return this.error(
`Unable to determine a specific connection from URL given.\nPlease specify a type with --type.\nIf you want a custom type, please provide the config object with --config`,
)
return 'custom'
}

isValidJSON(str: string) {
Expand Down

0 comments on commit 71e7c68

Please sign in to comment.