-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathApolloTextSearchAdapter.ts
More file actions
96 lines (87 loc) · 2.92 KB
/
ApolloTextSearchAdapter.ts
File metadata and controls
96 lines (87 loc) · 2.92 KB
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
import type { AnnotationFeatureSnapshot } from '@apollo-annotation/mst'
import BaseResult from '@jbrowse/core/TextSearch/BaseResults'
import type { Assembly } from '@jbrowse/core/assemblyManager/assembly'
import { readConfObject } from '@jbrowse/core/configuration'
import {
BaseAdapter,
type BaseTextSearchAdapter,
type BaseTextSearchArgs,
} from '@jbrowse/core/data_adapters/BaseAdapter'
import type { AbstractSessionModel, UriLocation } from '@jbrowse/core/util'
import type { ApolloSessionModel } from '../session'
function getMatchedFeature(
query: string,
feature: AnnotationFeatureSnapshot,
): AnnotationFeatureSnapshot | undefined {
// @ts-expect-error this actually has a bit more info that a plain snapshot
const { children, indexedIds, allIds, ...featureWithoutChildren } = feature
const featureString = JSON.stringify(featureWithoutChildren)
if (featureString.includes(query)) {
return feature
}
if (!children) {
return undefined
}
for (const subFeature of Object.values(children)) {
const matchedFeature = getMatchedFeature(query, subFeature)
if (matchedFeature) {
return matchedFeature
}
}
}
export class ApolloTextSearchAdapter
extends BaseAdapter
implements BaseTextSearchAdapter
{
get baseURL() {
return (readConfObject(this.config, 'baseURL') as UriLocation).uri
}
get trackId() {
return readConfObject(this.config, 'trackId') as string
}
get assemblyNames() {
return readConfObject(this.config, 'assemblyNames') as string[]
}
mapBaseResult(
features: AnnotationFeatureSnapshot[],
assembly: Assembly,
query: string,
) {
return features.map((feature) => {
const matchedObject = getMatchedFeature(query, feature) ?? feature
const refName = assembly.getCanonicalRefName(feature.refSeq)
return new BaseResult({
label: query,
trackId: this.trackId,
locString: `${refName}:${matchedObject.min + 1}..${matchedObject.max}`,
matchedObject,
})
})
}
async searchIndex(args: BaseTextSearchArgs): Promise<BaseResult[]> {
const query = args.queryString
const results: BaseResult[] = []
const session = this.pluginManager?.rootModel?.session as
| ApolloSessionModel
| undefined
if (!session) {
return results
}
const { apolloDataStore } = session
const { assemblyManager } = session as unknown as AbstractSessionModel
for (const assemblyName of this.assemblyNames) {
const backendDriver = apolloDataStore.getBackendDriver(assemblyName)
const assembly = assemblyManager.get(assemblyName)
if (!(backendDriver && assembly)) {
continue
}
const features = await backendDriver.searchFeatures(args.queryString, [
assemblyName,
])
results.push(...this.mapBaseResult(features, assembly, query))
}
return results
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
freeResources() {}
}