Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sequence adapter for viewing and retrieving sequences #133

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: pull_request

jobs:
build:
name: Strict lint
name: Strict lint and test
runs-on: ubuntu-latest
steps:
- name: Check out
Expand All @@ -19,3 +19,5 @@ jobs:
install-command: yarn --immutable
- name: Lint codebase
run: yarn eslint --report-unused-disable-directives --max-warnings 0 --ext .js,.ts,.jsx,.tsx .
- name: Test codebase
run: yarn test
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"start:shared": "yarn workspace apollo-shared run start",
"start:server": "yarn workspace apollo-collaboration-server run start",
"start:plugin": "yarn workspace jbrowse-plugin-apollo run start",
"start": "npm-run-all --print-label --parallel start:shared start:server start:plugin"
"start": "npm-run-all --print-label --parallel start:shared start:server start:plugin",
"test": "yarn workspace jbrowse-plugin-apollo run test"
},
"devDependencies": {
"@babel/eslint-parser": "^7.14.7",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-mst/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"build": "tsc"
},
"dependencies": {
"@jbrowse/core": "^2.0.1",
"@jbrowse/core": "^2.1.4",
"mobx": "^6.6.1",
"mobx-state-tree": "^5.1.5",
"rxjs": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"@gmod/gff": "^1.2.0",
"@jbrowse/core": "^2.0.1",
"@jbrowse/core": "^2.1.4",
"apollo-mst": "workspace:^",
"apollo-schemas": "workspace:^",
"bson-objectid": "^2.0.3",
Expand Down
8 changes: 8 additions & 0 deletions packages/jbrowse-plugin-apollo/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/cypress/'],
automock: false,
setupFiles: ['./jestSetup.js'],
}
2 changes: 2 additions & 0 deletions packages/jbrowse-plugin-apollo/jestSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable @typescript-eslint/no-var-requires */
require('jest-fetch-mock').enableMocks()
26 changes: 16 additions & 10 deletions packages/jbrowse-plugin-apollo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
"jbrowse-plugin": {
"name": "Apollo"
},
"jest": {
"testPathIgnorePatterns": [
"<rootDir>/cypress/"
]
},
"dependencies": {
"@emotion/react": "^11.9.3",
"@emotion/styled": "^11.9.3",
Expand All @@ -60,35 +55,46 @@
"tslib": "^2.0.3"
},
"peerDependencies": {
"@jbrowse/core": "^2.0.1",
"@jbrowse/core": "^2.1.4",
"@mui/material": "^5.9.1",
"mobx": "^6.6.1",
"mobx-react": "^7.2.1",
"mobx-state-tree": "^5.1.5",
"react": "^17.0.2",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rxjs": "^6.5.2",
"tss-react": "^3.7.1"
},
"devDependencies": {
"@jbrowse/cli": "^2.0.1",
"@jbrowse/core": "^2.0.1",
"@jbrowse/core": "^2.1.4",
"@jbrowse/development-tools": "^2.1.1",
"@jest/globals": "^29.0.3",
"@mui/material": "^5.9.1",
"@mui/x-data-grid": "^5.12.3",
"@types/cypress": "^1.1.3",
"@types/node": "^16.11.6",
"@types/prop-types": "^15",
"@types/react": "^17.0.34",
"@types/react-dom": "^18",
"cypress": "^8.7.0",
"jest": "^27.3.1",
"jest": "^29.0.3",
"jest-fetch-mock": "^3.0.3",
"mobx": "^6.6.1",
"mobx-react": "^7.2.1",
"mobx-state-tree": "^5.1.5",
"npm-run-all": "4.1.5",
"react": "^17.0.2",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^3.0.2",
"rollup": "^2.59.0",
"rxjs": "^6.5.2",
"serve": "^14.0.1",
"shx": "^0.3.3",
"start-server-and-test": "^1.11.7",
"ts-jest": "^29.0.0",
"ts-node": "^10.3.0",
"tss-react": "^3.7.1",
"typescript": "^4.7.4"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { beforeAll, beforeEach, describe, expect, it } from '@jest/globals'
import fetchMock from 'jest-fetch-mock'
import { toArray } from 'rxjs/operators'

import { ApolloSequenceAdapter, RefSeq } from './ApolloSequenceAdapter'
import configSchema from './configSchema'

const mockRefSeqs: RefSeq[] = [
{
_id: '6317dac51a89f156b2e21a70',
name: 'ctgA',
description: 'the first contig',
length: 10000,
},
{
_id: '6317dac51a89f156b2e21b7c',
name: 'ctgB',
description: 'the second contig',
length: 10000,
},
]

describe('ApolloSequenceAdapter', () => {
let adapter: ApolloSequenceAdapter
beforeAll(async () => {
adapter = new ApolloSequenceAdapter(
configSchema.create({
assemblyId: '6317d5436061de774b43e9d6',
baseURL: 'http://fake.url',
}),
)
// populate refSeqs cache before the tests are run
fetchMock.mockResponseOnce(JSON.stringify(mockRefSeqs))
await adapter.getRefNames({})
})
beforeEach(() => {
fetchMock.resetMocks()
})
it('can get refNames and regions', async () => {
const refNames = await adapter.getRefNames({})
expect(refNames).toEqual(mockRefSeqs.map((r) => r.name))
const regions = await adapter.getRegions({})
expect(regions).toEqual(
mockRefSeqs.map((r) => ({ refName: r.name, start: 0, end: r.length })),
)
})
it('can get features', async () => {
fetchMock.mockResponseOnce('GCGTGCAACAGACTTTCCATGATGCGAGCT')
const features = adapter.getFeatures(
{ refName: 'ctgA', start: 0, end: 30 },
{},
)
const featuresArray = await features.pipe(toArray()).toPromise()
expect(featuresArray).toMatchInlineSnapshot(`
[
{
"end": 30,
"refName": "ctgA",
"seq": "GCGTGCAACAGACTTTCCATGATGCGAGCT",
"start": 0,
"uniqueId": "ctgA 0-30",
},
]
`)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { readConfObject } from '@jbrowse/core/configuration'
import {
BaseOptions,
BaseSequenceAdapter,
} from '@jbrowse/core/data_adapters/BaseAdapter'
import { getFetcher } from '@jbrowse/core/util/io'
import { ObservableCreate } from '@jbrowse/core/util/rxjs'
import SimpleFeature, { Feature } from '@jbrowse/core/util/simpleFeature'
import { NoAssemblyRegion } from '@jbrowse/core/util/types'

export interface RefSeq {
_id: string
name: string
description: string
length: number
}

export class ApolloSequenceAdapter extends BaseSequenceAdapter {
private refSeqs: Promise<RefSeq[]> | undefined

get baseURL() {
return readConfObject(this.config, 'baseURL')
}

protected async getRefSeqs({ signal }: BaseOptions) {
if (this.refSeqs) {
return this.refSeqs
}
const assemblyId = readConfObject(this.config, 'assemblyId')
const searchParams = new URLSearchParams({ assembly: assemblyId })
const uri = new URL(`refSeqs?${searchParams.toString()}`, this.baseURL).href
const fetch = getFetcher(
{ locationType: 'UriLocation', uri },
this.pluginManager,
)
const response = await fetch(uri, { signal })
if (!response.ok) {
let errorMessage
try {
errorMessage = await response.text()
} catch (e) {
errorMessage = ''
}
throw new Error(
`Failed to fetch refSeqs — ${response.status} (${response.statusText})${
errorMessage ? ` (${errorMessage})` : ''
}`,
)
}
const refSeqs = (await response.json()) as RefSeq[]
this.refSeqs = Promise.resolve(refSeqs)
return refSeqs
}

public async getRefNames(opts: BaseOptions) {
const refSeqs = await this.getRefSeqs(opts)
return refSeqs.map((refSeq) => refSeq.name)
}

public async getRegions(opts: BaseOptions): Promise<NoAssemblyRegion[]> {
const refSeqs = await this.getRefSeqs(opts)
return refSeqs.map((refSeq) => ({
refName: refSeq.name,
start: 0,
end: refSeq.length,
}))
}

/**
* Fetch features for a certain region
* @param param -
* @returns Observable of Feature objects in the region
*/
public getFeatures(
{ refName, start, end }: NoAssemblyRegion,
opts: BaseOptions,
) {
return ObservableCreate<Feature>(async (observer) => {
const refSeqs = await this.getRefSeqs(opts)
const refSeq = refSeqs.find((rs) => rs.name === refName)
if (!refSeq) {
return observer.error(
`Could not find refSeq that matched refName "${refName}"`,
)
}
const searchParams = new URLSearchParams({
refSeq: refSeq._id,
start: String(start),
end: String(end),
})
const uri = new URL(
`refSeqs/getSequence?${searchParams.toString()}`,
this.baseURL,
).href
const fetch = getFetcher(
{ locationType: 'UriLocation', uri },
this.pluginManager,
)
const response = await fetch(uri, { signal: opts.signal })
if (!response.ok) {
let errorMessage
try {
errorMessage = await response.text()
} catch (e) {
errorMessage = ''
}
throw new Error(
`Failed to fetch refSeqs — ${response.status} (${
response.statusText
})${errorMessage ? ` (${errorMessage})` : ''}`,
)
}
const seq = (await response.text()) as string
if (seq) {
observer.next(
new SimpleFeature({
id: `${refName} ${start}-${end}`,
data: { refName, start, end, seq },
}),
)
}
observer.complete()
})
}

/**
* called to provide a hint that data tied to a certain region
* will not be needed for the forseeable future and can be purged
* from caches, etc
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
public freeResources(/* { region } */): void {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ConfigurationSchema } from '@jbrowse/core/configuration'

export default ConfigurationSchema(
'ApolloSequenceAdapter',
{
assemblyId: {
type: 'string',
defaultValue: '',
},
baseURL: {
type: 'string',
defaultValue: '',
},
},
{ explicitlyTyped: true },
)
22 changes: 22 additions & 0 deletions packages/jbrowse-plugin-apollo/src/ApolloSequenceAdapter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import AdapterType from '@jbrowse/core/pluggableElementTypes/AdapterType'
import PluginManager from '@jbrowse/core/PluginManager'

import { ApolloSequenceAdapter } from './ApolloSequenceAdapter'
import configSchema from './configSchema'

export function installApolloSequenceAdapter(pluginManager: PluginManager) {
pluginManager.addAdapterType(
() =>
new AdapterType({
name: 'ApolloSequenceAdapter',
configSchema,
adapterMetadata: {
category: null,
hiddenFromGUI: true,
displayName: null,
description: null,
},
AdapterClass: ApolloSequenceAdapter,
}),
)
}
2 changes: 2 additions & 0 deletions packages/jbrowse-plugin-apollo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ReactComponent as ApolloRendererReactComponent,
configSchema as apolloRendererConfigSchema,
} from './ApolloRenderer'
import { installApolloSequenceAdapter } from './ApolloSequenceAdapter'
import { AddAssembly, ImportFeatures, ViewChangeLog } from './components'
import { DownloadGFF3 } from './components/DownloadGFF3'
import {
Expand All @@ -39,6 +40,7 @@ export default class ApolloPlugin extends Plugin {
version = version

install(pluginManager: PluginManager) {
installApolloSequenceAdapter(pluginManager)
pluginManager.addTrackType(() => {
const configSchema = ConfigurationSchema(
'ApolloTrack',
Expand Down
Loading