-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathClientDataStore.ts
More file actions
332 lines (328 loc) · 11.6 KB
/
ClientDataStore.ts
File metadata and controls
332 lines (328 loc) · 11.6 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
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
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { type ClientDataStore as ClientDataStoreType } from '@apollo-annotation/common'
import {
type AnnotationFeatureModel,
type AnnotationFeatureSnapshot,
ApolloAssembly,
type ApolloAssemblySnapshot,
ApolloRefSeq,
type BackendDriverType,
CheckResult,
type CheckResultSnapshot,
} from '@apollo-annotation/mst'
import { getConf, readConfObject } from '@jbrowse/core/configuration'
import { type ConfigurationModel } from '@jbrowse/core/configuration/types'
import { type Region, getSession, isElectron } from '@jbrowse/core/util'
import {
type LocalPathLocation,
type UriLocation,
} from '@jbrowse/core/util/types/mst'
import { autorun } from 'mobx'
import {
type Instance,
addDisposer,
flow,
getParentOfType,
getRoot,
resolveIdentifier,
types,
} from 'mobx-state-tree'
import {
type ApolloInternetAccount,
CollaborationServerDriver,
DesktopFileDriver,
InMemoryFileDriver,
} from '../BackendDrivers'
import { ChangeManager } from '../ChangeManager'
import {
OntologyManagerType,
type OntologyRecordConfiguration,
type TextIndexFieldDefinition,
} from '../OntologyManager'
import type ApolloPluginConfigurationSchema from '../config'
import { type ApolloRootModel } from '../types'
import { type ApolloSessionModel } from './session'
export function clientDataStoreFactory(
AnnotationFeatureExtended: typeof AnnotationFeatureModel,
) {
return types
.model('ClientDataStore', {
typeName: types.optional(types.literal('Client'), 'Client'),
assemblies: types.map(ApolloAssembly),
checkResults: types.map(CheckResult),
ontologyManager: types.optional(OntologyManagerType, {}),
})
.views((self) => ({
get internetAccounts() {
return getRoot<ApolloRootModel>(self).internetAccounts
},
get pluginConfiguration() {
return getRoot<ApolloRootModel>(self).jbrowse.configuration
.ApolloPlugin as Instance<typeof ApolloPluginConfigurationSchema>
},
getFeature(featureId: string) {
return resolveIdentifier(
AnnotationFeatureExtended,
self.assemblies,
featureId,
)
},
}))
.actions((self) => ({
addAssembly(assemblyId: string, backendDriverType?: BackendDriverType) {
const assemblySnapshot: ApolloAssemblySnapshot = {
_id: assemblyId,
refSeqs: {},
}
if (backendDriverType) {
assemblySnapshot.backendDriverType = backendDriverType
}
return self.assemblies.put(assemblySnapshot)
},
addFeature(assemblyId: string, feature: AnnotationFeatureSnapshot) {
let assembly = self.assemblies.get(assemblyId)
if (!assembly) {
// Auto-create the assembly if it doesn't exist in client state
// This handles the race condition where assembly exists on server but not in client state
assembly = self.assemblies.put({
_id: assemblyId,
refSeqs: {},
})
}
let ref = assembly.refSeqs.get(feature.refSeq)
if (!ref) {
// Auto-create the refSeq if it doesn't exist
ref = assembly.refSeqs.put({
_id: feature.refSeq,
name: feature.refSeq,
features: {},
})
}
ref.features.put(feature)
},
deleteFeature(featureId: string) {
const feature = self.getFeature(featureId)
if (!feature) {
throw new Error(`Could not find feature "${featureId}" to delete`)
}
const { _id, parent } = feature
if (parent) {
parent.deleteChild(featureId)
} else {
const refSeq = getParentOfType(feature, ApolloRefSeq)
refSeq.deleteFeature(_id)
}
},
deleteAssembly(assemblyId: string) {
self.assemblies.delete(assemblyId)
},
addCheckResult(checkResult: CheckResultSnapshot) {
self.checkResults.put(checkResult)
},
addCheckResults(checkResults: CheckResultSnapshot[]) {
for (const checkResult of checkResults) {
if (!self.checkResults.has(checkResult._id)) {
self.checkResults.put(checkResult)
}
}
},
deleteCheckResult(checkResultId: string) {
self.checkResults.delete(checkResultId)
},
clearCheckResults() {
self.checkResults.clear()
},
}))
.volatile((self) => ({
changeManager: new ChangeManager(self as unknown as ClientDataStoreType),
collaborationServerDriver: new CollaborationServerDriver(
self as unknown as ClientDataStoreType,
),
inMemoryFileDriver: new InMemoryFileDriver(
self as unknown as ClientDataStoreType,
),
desktopFileDriver: isElectron
? new DesktopFileDriver(self as unknown as ClientDataStoreType)
: undefined,
}))
.actions((self) => ({
afterCreate() {
addDisposer(
self,
autorun(() => {
// Merge in the ontologies from our plugin configuration.
// Ontologies of a given name that are already in the session
// take precedence over the ontologies in the configuration.
const { ontologyManager, pluginConfiguration } = self
const configuredOntologies =
pluginConfiguration.ontologies as ConfigurationModel<
typeof OntologyRecordConfiguration
>[]
for (const ont of configuredOntologies || []) {
const [name, version, source, indexFields] = [
readConfObject(ont, 'name') as string,
readConfObject(ont, 'version') as string,
readConfObject(ont, 'source') as
| Instance<typeof LocalPathLocation>
| Instance<typeof UriLocation>,
readConfObject(
ont,
'textIndexFields',
) as TextIndexFieldDefinition[],
]
if (!ontologyManager.findOntology(name)) {
const session = getSession(
self,
) as unknown as ApolloSessionModel
const { jobsManager } = session
const controller = new AbortController()
const jobName = `Loading ontology "${name}"`
const job = {
name: jobName,
statusMessage: `Loading ontology "${name}", version "${version}", this may take a while`,
progressPct: 0,
cancelCallback: () => {
controller.abort()
jobsManager.abortJob(job.name)
},
}
const update = (message: string, progress: number): void => {
if (progress === 0) {
jobsManager.runJob(job)
return
}
if (progress === 100) {
jobsManager.done(job)
return
}
jobsManager.update(jobName, message, progress)
return
}
ontologyManager.addOntology(name, version, source, {
textIndexing: { indexFields },
update,
})
}
}
// TODO: add in any configured ontology prefixes that we don't already
// have in the session (or hardcoded in the model)
}),
)
},
}))
.views((self) => ({
getBackendDriver(assemblyId: string) {
if (!assemblyId) {
return self.collaborationServerDriver
}
const session = getSession(self)
const { assemblyManager } = session
const assembly = assemblyManager.get(assemblyId)
if (!assembly) {
return self.collaborationServerDriver
}
const { file, internetAccountConfigId } = getConf(assembly, [
'sequence',
'metadata',
]) as { internetAccountConfigId?: string; file: string }
if (isElectron && file) {
return self.desktopFileDriver
}
if (internetAccountConfigId) {
return self.collaborationServerDriver
}
return self.inMemoryFileDriver
},
getInternetAccount(assemblyName?: string, internetAccountId?: string) {
if (!(assemblyName ?? internetAccountId)) {
throw new Error(
'Must provide either assemblyName or internetAccountId',
)
}
let configId = internetAccountId
if (assemblyName && !configId) {
const { assemblyManager } = getSession(self)
const assembly = assemblyManager.get(assemblyName)
if (!assembly) {
throw new Error(`No assembly found with name ${assemblyName}`)
}
;({ internetAccountConfigId: configId } = getConf(assembly, [
'sequence',
'metadata',
]) as { internetAccountConfigId: string })
}
const { internetAccounts } = self
const internetAccount = internetAccounts.find(
(ia) => ia.internetAccountId === configId,
) as ApolloInternetAccount | undefined
if (!internetAccount) {
throw new Error(
`No InternetAccount found with config id ${internetAccountId}`,
)
}
return internetAccount
},
}))
.actions((self) => ({
loadFeatures: flow(function* loadFeatures(regions: Region[]) {
for (const region of regions) {
const backendDriver = self.getBackendDriver(region.assemblyName)
if (!backendDriver) {
return
}
const [features, checkResults] = (yield backendDriver.getFeatures(
region,
)) as [AnnotationFeatureSnapshot[], CheckResultSnapshot[]]
if (features.length === 0) {
continue
}
const { assemblyName, refName } = region
let assembly = self.assemblies.get(assemblyName)
if (!assembly) {
assembly = self.assemblies.put({ _id: assemblyName, refSeqs: {} })
}
const [firstFeature] = features
let ref = assembly.refSeqs.get(firstFeature.refSeq)
if (!ref) {
ref = assembly.refSeqs.put({
_id: firstFeature.refSeq,
name: refName,
features: {},
})
}
for (const feature of features) {
if (!ref.features.has(feature._id)) {
ref.features.put(feature)
}
}
self.addCheckResults(checkResults)
}
}),
loadRefSeq: flow(function* loadRefSeq(regions: Region[]) {
for (const region of regions) {
const backendDriver = self.getBackendDriver(region.assemblyName)
if (!backendDriver) {
return
}
const { refSeq, seq } = yield backendDriver.getSequence(region)
const { assemblyName, end, refName, start } = region
let assembly = self.assemblies.get(assemblyName)
if (!assembly) {
assembly = self.assemblies.put({ _id: assemblyName, refSeqs: {} })
}
let ref = assembly.refSeqs.get(refSeq)
if (!ref) {
ref = assembly.refSeqs.put({
_id: refSeq,
name: refName,
sequence: [],
})
}
ref.addSequence({ start, stop: end, sequence: seq })
}
}),
}))
}