forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtendedVariantWidget.tsx
More file actions
429 lines (380 loc) · 15.7 KB
/
ExtendedVariantWidget.tsx
File metadata and controls
429 lines (380 loc) · 15.7 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import { FIELD_NAME_MAP } from './fields';
import { Chart } from 'react-google-charts';
import { FieldModel, getGenotypeURL } from '../../../../utils';
import { ActionURL, Ajax } from '@labkey/api';
import React, { useEffect, useState } from 'react';
import { BaseCard, FeatureDetails } from '@jbrowse/core/BaseFeatureWidget/BaseFeatureDetail';
import { Paper, Table, TableBody, TableCell, TableHead, TableRow, Tooltip } from '@mui/material';
import { observer, PropTypes as MobxPropTypes } from 'mobx-react';
import { styled } from '@mui/material/styles';
export default jbrowse => {
const TableNoPaddingBlock = styled(Table)(({ theme }) => ({
padding: 0,
display: 'block'
}))
const TableRowBgGrey = styled(TableRow)(({ theme }) => ({
background: theme.palette.grey[100]
}))
const Link = styled('div')(({ theme }) => ({
padding: theme.spacing(5)
}))
const TableRowFlexWrap = styled(TableRow)(({ theme }) => ({
display: 'flex',
flexWrap: 'wrap'
}))
const TableCellFieldName = styled(TableCell)(({ theme }) => ({
wordBreak: 'break-all',
minWidth: 120,
borderBottom: '1px solid #0003',
background: theme.palette.grey[200],
marginRight: theme.spacing(1),
padding: theme.spacing(0.5)
}))
const TableCellFieldValue = styled(TableCell)(({ theme }) => ({
wordBreak: 'break-word',
maxWidth: 500,
padding: theme.spacing(0.5),
overflow: 'auto'
}))
const Message = styled('div')(({ theme }) => ({
paddingTop: theme.spacing(5),
paddingLeft: theme.spacing(5),
paddingRight: theme.spacing(5),
maxWidth: 500
}))
function round(value, decimals) {
return Number(Math.round(Number(value+'e'+decimals)) + 'e-'+decimals);
}
function makeAnnTable(data){
const geneNames = []
const tableBodyRows = []
for (let lineStr of data){
let line = lineStr.split('|')
if (line[10]){
line[10] = <div>{line[10]}</div>
}
const geneName = line[3] + (line[4] ? " (" + line[4] + ")" : "");
if (!geneNames.includes(geneName)){
tableBodyRows.push(
<TableRow key={geneName}>
<TableCell>{line[1]}</TableCell>
<TableCell>{line[2]}</TableCell>
<TableCell>{geneName}</TableCell>
<TableCell>{line[9]} {line[10]}</TableCell>
</TableRow>
)
geneNames.push(geneName)
}
}
return(
<BaseCard title="Predicted Function">
<TableNoPaddingBlock>
<TableHead>
<TableRowBgGrey>
<TableCell>Effect</TableCell>
<TableCell>Impact</TableCell>
<TableCell>Gene Name</TableCell>
<TableCell>Position/Consequence</TableCell>
</TableRowBgGrey>
</TableHead>
<TableBody>
{tableBodyRows}
</TableBody>
</TableNoPaddingBlock>
</BaseCard>
)
}
function makeDisplays(feat, displays, featureInfoFields, infoFields){
if (!infoFields) {
return null
}
const propertyJSX = []
for (let display of displays){
const tempProp = []
for (let propertyName of display.properties){
// This value is not in the header, and is probably injected programmatically, so skip:
if (!featureInfoFields[propertyName]) {
continue
}
const value = feat["INFO"][propertyName]
const fieldTitle = infoFields[propertyName]?.label || FIELD_NAME_MAP[propertyName]?.title || propertyName
const tooltip = infoFields[propertyName]?.description || featureInfoFields[propertyName]?.Description
if (value){
tempProp.push(
<TableRowFlexWrap key={propertyName + "-field"}>
<Tooltip title={tooltip}>
<TableCellFieldName>
{fieldTitle}
</TableCellFieldName>
</Tooltip>
<TableCellFieldValue key={propertyName + "-val"}>
{/* TODO: use JEXL and formatString */}
{Array.isArray(value) ? value.join(', ') : value}
</TableCellFieldValue>
</TableRowFlexWrap>
)
}
}
if (tempProp.length !== 0){
propertyJSX.push(tempProp)
}
}
const displayJSX = []
for (let i = 0; i < propertyJSX.length; i++){
displayJSX.push(
<BaseCard key={displays[i].title} title={displays[i].title}>
<TableNoPaddingBlock>
<TableBody>
{propertyJSX[i]}
</TableBody>
</TableNoPaddingBlock>
</BaseCard>
)
}
return displayJSX
}
function inferSections(feat, infoFields: Map<string, FieldModel>) {
if (!infoFields) {
return []
}
const sectionMap = {}
for (const [key, fieldDescriptor] of Object.entries(infoFields)) {
if (!fieldDescriptor.category) {
continue
}
if (!sectionMap[fieldDescriptor.category]) {
sectionMap[fieldDescriptor.category] = {
title: fieldDescriptor.category,
properties: []
}
}
sectionMap[fieldDescriptor.category].properties.push(fieldDescriptor.name)
}
const keys = Object.keys(sectionMap).sort()
return keys.map((x) => sectionMap[x])
}
function makeChart(samples, feat, trackId){
// Abort if there are no samples
if (!samples || Object.keys(samples).length === 0) {
return null;
}
const ref = feat["REF"];
const alt = feat["ALT"] || []
const [state, setState] = useState(null)
useEffect(() => {
setState(
<BaseCard title="Genotypes">
<div>
Loading genotypes...
</div>
</BaseCard>
)
const alleleCounts = {}
let alleleTotal = 0
alleleCounts[ref] = 0
for (let allele of alt){
alleleCounts[allele] = 0
}
const gtCounts = {}
let gtTotal = 0
// unphased gts split on /, phased on |
const regex = /\/|\|/
for (let sample in samples){
const gt = samples[sample]["GT"]
for (let genotype of gt){
const nc = "No Call"
if (genotype === "./." || genotype === ".|." || genotype === '.'){
gtCounts[nc] = gtCounts[nc] ? gtCounts[nc] + 1 : 1
gtTotal = gtTotal + 1
}
else {
const genotypes = genotype.split(regex)
const alleles = [ref].concat(alt)
// Calculate per-base values:
for (let gtIdx = 0; gtIdx < genotypes.length; gtIdx++){
if (!alleles[genotypes[gtIdx]]) {
console.error('Unable to parse genotype: ' + genotype)
continue
}
genotypes[gtIdx] = alleles[genotypes[gtIdx]]
alleleCounts[genotypes[gtIdx]] = alleleCounts[genotypes[gtIdx]] + 1 // tick up allele count
alleleTotal = alleleTotal + 1
}
// Then by genotype:
const genotypeString = genotypes.join("/")
gtCounts[genotypeString] = gtCounts[genotypeString] ? gtCounts[genotypeString] + 1 : 1
gtTotal = gtTotal + 1
}
}
}
const gtBarData = [[
'Genotype',
'Total Count',
{ role: 'style' },
{
sourceColumn: 0,
role: 'annotation',
type: 'string',
calc: 'stringify',
},
]]
let rounds = 0
let decimal = 1
while (decimal !== 2){
for (let entry in gtCounts){
rounds = rounds + round(gtCounts[entry]/gtTotal*100, decimal)
}
if (rounds === 100){
break
}
rounds = 0
decimal = decimal + 1
}
for (let entry in gtCounts){
let rounded:any = round(gtCounts[entry]/gtTotal*100, decimal)
if (rounds !== 100){
rounded = "~" + rounded
}
gtBarData.push([entry, gtCounts[entry], "#0088FF", rounded+"%"])
}
const alleleTableRows = []
for (let allele in alleleCounts){
alleleTableRows.push(
<TableRow key={allele}>
<TableCell>{allele}</TableCell>
<TableCell>{round(alleleCounts[allele]/alleleTotal, 4)}</TableCell>
<TableCell>{alleleCounts[allele]}</TableCell>
</TableRow>
)
}
const gtTitle = "Genotype Frequency (" + gtTotal.toString() + ")"
const contig = feat["CHROM"];
const start = feat["POS"];
const end = feat["end"];
const href = <a href={getGenotypeURL(trackId, contig, start, end)} target="_blank">Click here to view sample-level genotypes</a>
setState(
<div>
<BaseCard title="Allele Frequencies">
<TableNoPaddingBlock>
<TableHead>
<TableRowBgGrey>
<TableCell>Sequence</TableCell>
<TableCell>Fraction</TableCell>
<TableCell>Count</TableCell>
</TableRowBgGrey>
</TableHead>
<TableBody>
{alleleTableRows}
</TableBody>
</TableNoPaddingBlock>
</BaseCard>
<BaseCard title={gtTitle}>
<Chart
width={'250px'}
height={'200px'}
chartType="BarChart"
loader={<div>Loading Chart</div>}
data={gtBarData}
options={{
title: "Genotypes",
width: 400,
height: 200,
bar: { groupWidth: '60%' },
legend: { position: 'none' },
fontSize: 14
}}
// For tests
rootProps={{ 'data-testid': '6' }}
/>
<Link>
{href}
</Link>
</BaseCard>
</div>)
}, []);
return state
}
function CreatePanel(props) {
const { model } = props
const detailsConfig = JSON.parse(JSON.stringify(model.detailsConfig || {}))
const feature = model.featureData
const featureInfoFields = feature.parser.metadata.INFO
const [infoFields, setInfoFields] = useState<Map<string, FieldModel>>(null)
const feat = JSON.parse(JSON.stringify(feature))
const { samples } = feat
feat["samples"] = null
const trackId = model.trackId
if (!trackId) {
console.error('Error! No trackId')
}
const infoKeys = Object.keys(feat.INFO)
let isApiSubscribed = true
useEffect(() => {
Ajax.request({
url: ActionURL.buildURL('jbrowse', 'resolveVcfFields.api'),
method: 'POST',
success: async function(res){
if (isApiSubscribed) {
const fields: Map<string, FieldModel> = JSON.parse(res.response);
setInfoFields(fields)
}
},
failure: function(res){
console.error("There was an error while fetching field types: " + res.status + "\n Status Body: " + res.statusText)
},
params: {infoKeys: infoKeys},
});
return () => {
isApiSubscribed = false;
};
}, [infoKeys])
let annTable;
if (feat["INFO"]["ANN"]){
annTable = makeAnnTable(feat["INFO"]["ANN"])
delete feat["INFO"]["ANN"]
}
const sections = detailsConfig.sections || inferSections(feat, infoFields)
const displays = makeDisplays(feat, sections, featureInfoFields, infoFields)
// If a given INFO field is used in a specific section, dont include in the catch-all INFO section:
for (let i in sections){
for (let j in sections[i].properties){
delete feat["INFO"][sections[i].properties[j]]
}
}
if (infoFields) {
const ignoredFields = Object.values(infoFields).filter((x: FieldModel) => !!x.isHidden).map((x: FieldModel) => x.name)
for (const fieldName of ignoredFields) {
if (feat["INFO"][fieldName]) {
delete feat["INFO"][fieldName]
}
}
}
const message = detailsConfig.message ? <Message >{detailsConfig.message}</Message> : null
const infoConfig = [{
title: "Other Fields",
properties: []
}]
for (let infoEntry in feat["INFO"]){
infoConfig[0].properties.push(infoEntry)
}
const infoDisplays = makeDisplays(feat, infoConfig, featureInfoFields, infoFields)
feat["INFO"] = null
return (
<Paper data-testid="extended-variant-widget">
{message}
<FeatureDetails
feature={feat}
{...props}
/>
{annTable}
{displays}
{infoDisplays}
{makeChart(samples, feat, trackId)}
</Paper>
)
}
CreatePanel.propTypes = {
model: MobxPropTypes.observableObject.isRequired,
}
return observer(CreatePanel)
}