@jbrowse/plugin-data-management 2.3.1 → 2.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,326 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { RaStanza, TrackDbFile } from '@gmod/ucsc-hub'
3
+ import { FileLocation, isUriLocation, objectHash } from '@jbrowse/core/util'
4
+ import { openLocation } from '@jbrowse/core/util/io'
5
+ import {
6
+ generateUnsupportedTrackConf,
7
+ generateUnknownTrackConf,
8
+ } from '@jbrowse/core/util/tracks'
9
+ import ucscAssemblies from './ucscAssemblies'
10
+
11
+ export { ucscAssemblies }
12
+
13
+ export async function fetchHubFile(hubLocation: FileLocation) {
14
+ try {
15
+ const hubFileText = await openLocation(hubLocation).readFile('utf8')
16
+ const { HubFile } = await import('@gmod/ucsc-hub')
17
+ return new HubFile(hubFileText)
18
+ } catch (error) {
19
+ throw new Error(`Not a valid hub.txt file, got error: '${error}'`)
20
+ }
21
+ }
22
+
23
+ export async function fetchGenomesFile(genomesLoc: FileLocation) {
24
+ const genomesFileText = await openLocation(genomesLoc).readFile('utf8')
25
+ const { GenomesFile } = await import('@gmod/ucsc-hub')
26
+ return new GenomesFile(genomesFileText)
27
+ }
28
+
29
+ export async function fetchTrackDbFile(trackDbLoc: FileLocation) {
30
+ const text = await openLocation(trackDbLoc).readFile('utf8')
31
+ const { TrackDbFile } = await import('@gmod/ucsc-hub')
32
+ return new TrackDbFile(text)
33
+ }
34
+
35
+ export function makeLoc(first: string, base: { uri: string }) {
36
+ return {
37
+ uri: new URL(first, base.uri).href,
38
+ locationType: 'UriLocation',
39
+ }
40
+ }
41
+
42
+ export function makeLocAlt(first: string, alt: string, base: { uri: string }) {
43
+ return first ? makeLoc(first, base) : makeLoc(alt, base)
44
+ }
45
+
46
+ export function makeLoc2(first: string, alt?: string) {
47
+ return first
48
+ ? {
49
+ uri: first,
50
+ locationType: 'LocalPath',
51
+ }
52
+ : {
53
+ uri: alt,
54
+ locationType: 'UriLocation',
55
+ }
56
+ }
57
+
58
+ export function generateTracks(
59
+ trackDb: TrackDbFile,
60
+ trackDbLoc: FileLocation,
61
+ assemblyName: string,
62
+ sequenceAdapter: any,
63
+ ) {
64
+ const tracks: any = []
65
+
66
+ trackDb.forEach((track, trackName) => {
67
+ const trackKeys = Array.from(track.keys()) as string[]
68
+ const parentTrackKeys = [
69
+ 'superTrack',
70
+ 'compositeTrack',
71
+ 'container',
72
+ 'view',
73
+ ]
74
+ if (trackKeys.some(key => parentTrackKeys.includes(key))) {
75
+ return
76
+ }
77
+ const parentTracks = []
78
+ let currentTrackName = trackName
79
+ do {
80
+ currentTrackName = trackDb.get(currentTrackName)?.get('parent') || ''
81
+ if (currentTrackName) {
82
+ ;[currentTrackName] = currentTrackName.split(' ')
83
+ parentTracks.push(trackDb.get(currentTrackName))
84
+ }
85
+ } while (currentTrackName)
86
+ parentTracks.reverse()
87
+ const categories = parentTracks
88
+ .map(p => p?.get('shortLabel'))
89
+ .filter((f): f is string => !!f)
90
+ const res = makeTrackConfig(
91
+ track,
92
+ categories,
93
+ trackDbLoc,
94
+ trackDb,
95
+ sequenceAdapter,
96
+ )
97
+ tracks.push({
98
+ ...res,
99
+ trackId: `ucsc-trackhub-${objectHash(res)}`,
100
+ assemblyNames: [assemblyName],
101
+ })
102
+ })
103
+
104
+ return tracks
105
+ }
106
+
107
+ function makeTrackConfig(
108
+ track: RaStanza,
109
+ categories: string[],
110
+ trackDbLoc: FileLocation,
111
+ trackDb: TrackDbFile,
112
+ sequenceAdapter: any,
113
+ ) {
114
+ let trackType = track.get('type')
115
+ const name = track.get('shortLabel') || ''
116
+ const bigDataUrl = track.get('bigDataUrl') || ''
117
+ const bigDataIdx = track.get('bigDataIndex') || ''
118
+ const isUri = isUriLocation(trackDbLoc)
119
+ if (!trackType) {
120
+ trackType = trackDb.get(track.get('parent') || '')?.get('type')
121
+ }
122
+ let baseTrackType = trackType?.split(' ')[0] || ''
123
+ if (baseTrackType === 'bam' && bigDataUrl.toLowerCase().endsWith('cram')) {
124
+ baseTrackType = 'cram'
125
+ }
126
+ const bigDataLocation = isUri
127
+ ? makeLoc(bigDataUrl, trackDbLoc)
128
+ : makeLoc2(bigDataUrl)
129
+
130
+ switch (baseTrackType) {
131
+ case 'bam':
132
+ return {
133
+ type: 'AlignmentsTrack',
134
+ name: track.get('shortLabel'),
135
+ description: track.get('longLabel'),
136
+ category: categories,
137
+ adapter: {
138
+ type: 'BamAdapter',
139
+ bamLocation: bigDataLocation,
140
+ index: {
141
+ location: isUri
142
+ ? makeLocAlt(bigDataIdx, bigDataUrl + '.bai', trackDbLoc)
143
+ : makeLoc2(bigDataIdx, bigDataUrl + '.bai'),
144
+ },
145
+ },
146
+ }
147
+
148
+ case 'bigBarChart':
149
+ return {
150
+ type: 'FeatureTrack',
151
+ name,
152
+ description: track.get('longLabel'),
153
+ category: categories,
154
+ adapter: {
155
+ type: 'BigBedAdapter',
156
+ bigBedLocation: bigDataLocation,
157
+ },
158
+ renderer: {
159
+ type: 'SvgFeatureRenderer',
160
+ },
161
+ }
162
+ case 'bigBed':
163
+ return {
164
+ type: 'FeatureTrack',
165
+ name,
166
+ description: track.get('longLabel'),
167
+ category: categories,
168
+ adapter: {
169
+ type: 'BigBedAdapter',
170
+ bigBedLocation: bigDataLocation,
171
+ },
172
+ }
173
+ case 'bigGenePred':
174
+ return {
175
+ type: 'FeatureTrack',
176
+ name,
177
+ description: track.get('longLabel'),
178
+ category: categories,
179
+ adapter: {
180
+ type: 'BigBedAdapter',
181
+ bigBedLocation: bigDataLocation,
182
+ },
183
+ }
184
+ case 'bigChain':
185
+ return {
186
+ type: 'FeatureTrack',
187
+ name,
188
+ description: track.get('longLabel'),
189
+ category: categories,
190
+ adapter: {
191
+ type: 'BigBedAdapter',
192
+ bigBedLocation: bigDataLocation,
193
+ },
194
+ renderer: {
195
+ type: 'SvgFeatureRenderer',
196
+ },
197
+ }
198
+ case 'bigInteract':
199
+ return {
200
+ type: 'FeatureTrack',
201
+ name,
202
+ description: track.get('longLabel'),
203
+ category: categories,
204
+ adapter: {
205
+ type: 'BigBedAdapter',
206
+ bigBedLocation: bigDataLocation,
207
+ },
208
+ renderer: {
209
+ type: 'SvgFeatureRenderer',
210
+ },
211
+ }
212
+ case 'bigMaf':
213
+ return {
214
+ type: 'FeatureTrack',
215
+ name,
216
+ description: track.get('longLabel'),
217
+ category: categories,
218
+ adapter: {
219
+ type: 'BigBedAdapter',
220
+ bigBedLocation: bigDataLocation,
221
+ },
222
+ renderer: {
223
+ type: 'SvgFeatureRenderer',
224
+ },
225
+ }
226
+ case 'bigPsl':
227
+ return {
228
+ type: 'FeatureTrack',
229
+ name,
230
+ description: track.get('longLabel'),
231
+ category: categories,
232
+ adapter: {
233
+ type: 'BigBedAdapter',
234
+ bigBedLocation: bigDataLocation,
235
+ },
236
+ renderer: {
237
+ type: 'SvgFeatureRenderer',
238
+ },
239
+ }
240
+ case 'bigWig':
241
+ return {
242
+ type: 'QuantitativeTrack',
243
+ name,
244
+ description: track.get('longLabel'),
245
+ category: categories,
246
+ adapter: {
247
+ type: 'BigWigAdapter',
248
+ bigWigLocation: bigDataLocation,
249
+ },
250
+ }
251
+
252
+ case 'cram':
253
+ return {
254
+ type: 'AlignmentsTrack',
255
+ name,
256
+ description: track.get('longLabel'),
257
+ category: categories,
258
+ adapter: {
259
+ type: 'CramAdapter',
260
+ cramLocation: bigDataLocation,
261
+ craiLocation: isUri
262
+ ? makeLocAlt(bigDataIdx, bigDataUrl + '.crai', trackDbLoc)
263
+ : makeLoc2(bigDataIdx, bigDataUrl + '.crai'),
264
+ sequenceAdapter,
265
+ },
266
+ }
267
+
268
+ case 'bigNarrowPeak':
269
+ return {
270
+ type: 'FeatureTrack',
271
+ name,
272
+ description: track.get('longLabel'),
273
+ category: categories,
274
+ adapter: {
275
+ type: 'BigBedAdapter',
276
+ bigBedLocation: bigDataLocation,
277
+ },
278
+ }
279
+ case 'peptideMapping':
280
+ return generateUnsupportedTrackConf(name, baseTrackType, categories)
281
+ case 'vcfTabix':
282
+ return {
283
+ type: 'VariantTrack',
284
+ name,
285
+ description: track.get('longLabel'),
286
+ category: categories,
287
+ adapter: {
288
+ type: 'VcfTabixAdapter',
289
+ vcfGzLocation: bigDataLocation,
290
+ index: {
291
+ location: isUri
292
+ ? makeLocAlt(bigDataIdx, bigDataUrl + '.tbi', trackDbLoc)
293
+ : makeLoc2(bigDataIdx, bigDataUrl + '.tbi'),
294
+ },
295
+ },
296
+ }
297
+
298
+ case 'hic':
299
+ return {
300
+ type: 'HicTrack',
301
+ name,
302
+ description: track.get('longLabel'),
303
+ category: categories,
304
+ adapter: {
305
+ type: 'HicAdapter',
306
+ hicLocation: bigDataLocation,
307
+ },
308
+ }
309
+
310
+ // unsupported types
311
+ // case 'gvf':
312
+ // case 'ld2':
313
+ // case 'narrowPeak':
314
+ // case 'wig':
315
+ // case 'wigMaf':
316
+ // case 'halSnake':
317
+ // case 'bed':
318
+ // case 'bed5FloatScore':
319
+ // case 'bedGraph':
320
+ // case 'bedRnaElements':
321
+ // case 'broadPeak':
322
+ // case 'coloredExon':
323
+ default:
324
+ return generateUnknownTrackConf(name, baseTrackType, categories)
325
+ }
326
+ }