Skip to content

Commit ddcfe08

Browse files
hextrazaSebastian Benjamin
andauthored
Add subadapter + reflection pattern to ExtendedVariantAdapter in order to delete VCFTabixAdapter (#353)
* Add subadapter + reflection pattern to ExtendedVariantAdapter in order to delete VCFTabixAdapter * Add API wrapper for getMetadata and getHeader --------- Co-authored-by: Sebastian Benjamin <sebastiancbenjamin@gmail.com>
1 parent d0053f8 commit ddcfe08

File tree

2 files changed

+71
-162
lines changed

2 files changed

+71
-162
lines changed

jbrowse/src/client/JBrowse/Browser/plugins/ExtendedVariantPlugin/ExtendedVariantAdapter/ExtendedVariantAdapter.ts

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,57 @@
11
import QuickLRU from '@jbrowse/core/util/QuickLRU';
2-
import { BaseOptions } from '@jbrowse/core/data_adapters/BaseAdapter';
2+
import { BaseOptions, BaseFeatureDataAdapter } from '@jbrowse/core/data_adapters/BaseAdapter';
33
import { NoAssemblyRegion } from '@jbrowse/core/util/types';
44
import { ObservableCreate } from '@jbrowse/core/util/rxjs';
55
import { Feature } from '@jbrowse/core/util/simpleFeature';
66
import ExtendedVcfFeature from './ExtendedVcfFeature';
77
import { VcfFeature } from '@jbrowse/plugin-variants';
8-
import { default as VcfTabixAdapter } from './VcfTabixAdapter';
98

10-
export default class extends VcfTabixAdapter {
9+
export default class extends BaseFeatureDataAdapter {
1110
protected featureCache = new QuickLRU({ maxSize: 20 })
11+
private subAdapterP?: Promise<any>
12+
13+
constructor(...args: any[]) {
14+
super(...args)
15+
16+
// Return a Proxy that forwards any unknown member access to the sub-adapter.
17+
// This avoids re-implementing methods like getHeader/getRefNames/getMetadata/etc.
18+
const self = this
19+
return new Proxy(this, {
20+
get(target, prop, receiver) {
21+
// If we have it already (e.g., getFeatures, getFeaturesAsArray, BaseFeatureDataAdapter-derived properties), use it directly
22+
if (prop in target || typeof prop === 'symbol') {
23+
return Reflect.get(target, prop, receiver)
24+
}
25+
26+
// Otherwise, forward to the VcfTabixAdapter sub-adapter
27+
return async (...callArgs: any[]) => {
28+
const sub = await self.getVcfSubAdapter()
29+
const value = (sub as any)[prop]
30+
31+
// If it’s a method, call it; otherwise return the property value
32+
if (typeof value === 'function') {
33+
return value.apply(sub, callArgs)
34+
}
35+
return value
36+
}
37+
},
38+
})
39+
}
40+
41+
private async getVcfSubAdapter(): Promise<any> {
42+
if (!this.subAdapterP) {
43+
const vcfGzLocation = this.getConf('vcfGzLocation')
44+
const index = this.getConf(['index'])
45+
const vcfAdapterConf = { type: 'VcfTabixAdapter', vcfGzLocation, index }
46+
this.subAdapterP = this.getSubAdapter!(vcfAdapterConf)
47+
.then(({ dataAdapter }) => dataAdapter)
48+
.catch(e => {
49+
this.subAdapterP = undefined
50+
throw e
51+
})
52+
}
53+
return this.subAdapterP
54+
}
1255

1356
public getFeatures(query: NoAssemblyRegion, opts: BaseOptions = {}) {
1457
return ObservableCreate<Feature>(async observer => {
@@ -50,4 +93,29 @@ export default class extends VcfTabixAdapter {
5093

5194
return features
5295
}
96+
97+
// Typescript errors at compile time without these stubs
98+
async configure(opts?: BaseOptions) {
99+
const sub = await this.getVcfSubAdapter()
100+
return sub.configure(opts)
101+
}
102+
103+
async getRefNames(opts: BaseOptions = {}) {
104+
const sub = await this.getVcfSubAdapter()
105+
return sub.getRefNames(opts)
106+
}
107+
108+
async getHeader(opts?: BaseOptions) {
109+
const sub = await this.getVcfSubAdapter()
110+
return sub.getHeader(opts)
111+
}
112+
113+
async getMetadata(opts?: BaseOptions) {
114+
const sub = await this.getVcfSubAdapter()
115+
return sub.getMetadata(opts)
116+
}
117+
118+
freeResources(): void {
119+
void this.getVcfSubAdapter().then(sub => sub.freeResources?.())
120+
}
53121
}

jbrowse/src/client/JBrowse/Browser/plugins/ExtendedVariantPlugin/ExtendedVariantAdapter/VcfTabixAdapter.ts

Lines changed: 0 additions & 159 deletions
This file was deleted.

0 commit comments

Comments
 (0)