Skip to content

Commit

Permalink
Add aborting to CoreGetFeatures
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Apr 4, 2021
1 parent 1a50f3a commit 7975c87
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 deletions.
13 changes: 7 additions & 6 deletions packages/core/rpc/coreRpcMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,26 @@ export class CoreGetFeatures extends RpcMethodType {
signal: RemoteAbortSignal
region: Region
adapterConfig: {}
opts?: { signal?: AbortSignal }
},
rpcDriverClassName: string,
) {
const deserializedArgs = await this.deserializeArguments(
args,
rpcDriverClassName,
)
const { sessionId, adapterConfig, region } = deserializedArgs
const { sessionId, adapterConfig, region, opts } = deserializedArgs
const { dataAdapter } = getAdapter(
this.pluginManager,
sessionId,
adapterConfig,
)
if (isFeatureAdapter(dataAdapter)) {
const ret = dataAdapter.getFeatures(region)
const r = await ret.pipe(toArray()).toPromise()
return r.map(f => f.toJSON())
if (!isFeatureAdapter(dataAdapter)) {
return []
}
return []
const ret = dataAdapter.getFeatures(region, opts)
const r = await ret.pipe(toArray()).toPromise()
return r.map(f => f.toJSON())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,36 @@ const useStyles = makeStyles(theme => ({

/**
* Fetches and returns a list features for a given list of regions
* @param selectedRegions - Region[]
* @returns Features[]
*/
async function fetchSequence(
self: LinearGenomeViewModel,
model: LinearGenomeViewModel,
selectedRegions: Region[],
opts: {
signal?: AbortSignal
},
) {
const session = getSession(self)
const assemblyName =
self.leftOffset?.assemblyName || self.rightOffset?.assemblyName || ''
const { rpcManager, assemblyManager } = session
const assemblyConfig = assemblyManager.get(assemblyName)?.configuration
const session = getSession(model)
const { leftOffset, rightOffset } = model

// assembly configuration
const adapterConfig = readConfObject(assemblyConfig, ['sequence', 'adapter'])
if (!leftOffset || !rightOffset) {
throw new Error('no offsets on model to use for range')
}

if (leftOffset.assemblyName !== rightOffset.assemblyName) {
throw new Error(
'not able to fetch from multiple assemblies at once currently',
)
}
const { rpcManager, assemblyManager } = session
const assemblyName = leftOffset.assemblyName || rightOffset.assemblyName || ''
const assembly = assemblyManager.get(assemblyName)
if (!assembly) {
throw new Error(`assembly ${assemblyName} not found`)
}
const adapterConfig = readConfObject(assembly.configuration, [
'sequence',
'adapter',
])

const sessionId = 'getSequence'
const chunks = (await Promise.all(
Expand All @@ -69,6 +84,7 @@ async function fetchSequence(
adapterConfig,
region,
sessionId,
opts,
}),
),
)) as Feature[][]
Expand Down Expand Up @@ -101,41 +117,38 @@ function SequenceDialog({

useEffect(() => {
let active = true
const controller = new AbortController()

function formatSequence(seqChunks: Feature[]) {
const sequenceChunks: SeqChunk[] = []
const incompleteSeqErrs: string[] = []
seqChunks.forEach((chunk: Feature) => {
const chunkSeq = chunk.get('seq')
const chunkRefName = chunk.get('refName')
const chunkStart = chunk.get('start') + 1
const chunkEnd = chunk.get('end')
const chunkLocstring = `${chunkRefName}:${chunkStart}-${chunkEnd}`
if (chunkSeq) {
sequenceChunks.push({ header: chunkLocstring, seq: chunkSeq })
if (chunkSeq.length !== chunkEnd - chunkStart + 1) {
incompleteSeqErrs.push(
return formatSeqFasta(
seqChunks.map(chunk => {
const chunkSeq = chunk.get('seq')
const chunkRefName = chunk.get('refName')
const chunkStart = chunk.get('start') + 1
const chunkEnd = chunk.get('end')
const chunkLocstring = `${chunkRefName}:${chunkStart}-${chunkEnd}`
if (chunkSeq?.length !== chunkEnd - chunkStart + 1) {
throw new Error(
`${chunkLocstring} returned ${chunkSeq.length.toLocaleString()} bases, but should have returned ${(
chunkEnd - chunkStart
).toLocaleString()}`,
)
}
}
})
if (incompleteSeqErrs.length > 0) {
session.notify(
`Unable to retrieve complete reference sequence from regions:${incompleteSeqErrs.join()}`,
)
}
setSequence(formatSeqFasta(sequenceChunks))
return { header: chunkLocstring, seq: chunkSeq }
}),
)
}

;(async () => {
try {
if (regionsSelected.length > 0) {
const chunks = await fetchSequence(model, regionsSelected)
const chunks = await fetchSequence(
model,
regionsSelected,
controller.signal,
)
if (active) {
formatSequence(chunks)
setSequence(formatSequence(chunks))
}
} else {
throw new Error('Selected region is out of bounds')
Expand All @@ -149,6 +162,7 @@ function SequenceDialog({
})()

return () => {
controller.abort()
active = false
}
}, [model, session, regionsSelected, setSequence])
Expand All @@ -161,10 +175,8 @@ function SequenceDialog({
maxWidth="xl"
open
onClose={handleClose}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">
<DialogTitle>
Reference sequence
{handleClose ? (
<IconButton
Expand Down

0 comments on commit 7975c87

Please sign in to comment.