forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVcfTabixAdapter.ts
More file actions
159 lines (145 loc) · 5.14 KB
/
VcfTabixAdapter.ts
File metadata and controls
159 lines (145 loc) · 5.14 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
import { TabixIndexedFile } from '@gmod/tabix'
import VcfParser from '@gmod/vcf'
import { BaseFeatureDataAdapter } from '@jbrowse/core/data_adapters/BaseAdapter'
import {
fetchAndMaybeUnzipText,
updateStatus,
} from '@jbrowse/core/util'
import { openLocation } from '@jbrowse/core/util/io'
import { ObservableCreate } from '@jbrowse/core/util/rxjs'
import type { BaseOptions } from '@jbrowse/core/data_adapters/BaseAdapter'
import type { Feature } from '@jbrowse/core/util'
import type { NoAssemblyRegion } from '@jbrowse/core/util/types'
import { VcfFeature } from '@jbrowse/plugin-variants';
function shorten2(name: string, max = 70) {
return name.length > max ? `${name.slice(0, max)}...` : name
}
export default class VcfTabixAdapter extends BaseFeatureDataAdapter {
private configured?: Promise<{
vcf: TabixIndexedFile
parser: VcfParser
}>
private async configurePre(_opts?: BaseOptions) {
const vcfGzLocation = this.getConf('vcfGzLocation')
const location = this.getConf(['index', 'location'])
const indexType = this.getConf(['index', 'indexType'])
const filehandle = openLocation(vcfGzLocation, this.pluginManager)
const isCSI = indexType === 'CSI'
const vcf = new TabixIndexedFile({
filehandle,
csiFilehandle: isCSI
? openLocation(location, this.pluginManager)
: undefined,
tbiFilehandle: !isCSI
? openLocation(location, this.pluginManager)
: undefined,
chunkCacheSize: 50 * 2 ** 20,
})
return {
vcf,
parser: new VcfParser({
header: await vcf.getHeader(),
}),
}
}
protected async configurePre2() {
if (!this.configured) {
this.configured = this.configurePre().catch((e: unknown) => {
this.configured = undefined
throw e
})
}
return this.configured
}
async configure(opts?: BaseOptions) {
const { statusCallback = () => {} } = opts || {}
return updateStatus('Downloading index', statusCallback, () =>
this.configurePre2(),
)
}
public async getRefNames(opts: BaseOptions = {}) {
const { vcf } = await this.configure(opts)
return vcf.getReferenceSequenceNames(opts)
}
async getHeader(opts?: BaseOptions) {
const { vcf } = await this.configure(opts)
return vcf.getHeader()
}
async getMetadata(opts?: BaseOptions) {
const { parser } = await this.configure(opts)
return parser.getMetadata()
}
public getFeatures(query: NoAssemblyRegion, opts: BaseOptions = {}) {
return ObservableCreate<Feature>(async observer => {
const { refName, start, end } = query
const { statusCallback = () => {} } = opts
const { vcf, parser } = await this.configure(opts)
await updateStatus('Downloading variants', statusCallback, () =>
vcf.getLines(refName, start, end, {
lineCallback: (line, fileOffset) => {
observer.next(
new VcfFeature({
variant: parser.parseLine(line),
parser,
id: `${this.id}-vcf-${fileOffset}`,
}),
)
},
...opts,
}),
)
observer.complete()
}, opts.stopToken)
}
async getSources() {
const conf = this.getConf('samplesTsvLocation')
if (conf.uri === '' || conf.uri === '/path/to/samples.tsv') {
const { parser } = await this.configure()
return parser.samples.map(name => ({
name,
}))
} else {
const txt = await fetchAndMaybeUnzipText(
openLocation(conf, this.pluginManager),
)
const lines = txt.split(/\n|\r\n|\r/)
const header = lines[0]!.split('\t')
const { parser } = await this.configure()
const metadataLines = lines
.slice(1)
.filter(f => !!f)
.map(line => {
const [name, ...rest] = line.split('\t')
return {
...Object.fromEntries(
// force col 0 to be called name
rest.map((c, idx) => [header[idx + 1]!, c] as const),
),
name: name!,
}
})
const vcfSampleSet = new Set(parser.samples)
const metadataSet = new Set(metadataLines.map(r => r.name))
const metadataNotInVcfSamples = [...metadataSet].filter(
f => !vcfSampleSet.has(f),
)
const vcfSamplesNotInMetadata = [...vcfSampleSet].filter(
f => !metadataSet.has(f),
)
if (metadataNotInVcfSamples.length) {
console.warn(
`There are ${metadataNotInVcfSamples.length} samples in metadata file (${metadataLines.length} lines) not in VCF (${parser.samples.length} samples):`,
shorten2(metadataNotInVcfSamples.join(',')),
)
}
if (vcfSamplesNotInMetadata.length) {
console.warn(
`There are ${vcfSamplesNotInMetadata.length} samples in VCF file (${parser.samples.length} samples) not in metadata file (${metadataLines.length} lines):`,
shorten2(vcfSamplesNotInMetadata.map(m => m).join(',')),
)
}
return metadataLines.filter(f => vcfSampleSet.has(f.name))
}
}
public freeResources(/* { region } */): void {}
}