UNPKG

@nightingale-elements/nightingale-structure

Version:
74 lines (63 loc) 2.08 kB
import { CustomElementProperty } from "molstar/lib/mol-model-props/common/custom-element-property"; import { Color } from "molstar/lib/mol-util/color"; import { scaleLinear, color } from "d3"; import parseAMData from "./alpha-missense-parser"; const AM_COLOR_SCALE = { checkpoints: [0, 0.1132, 0.2264, 0.3395, 0.4527, 0.5895, 0.7264, 0.8632, 1], colors: [ "#2166ac", "#4290bf", "#8cbcd4", "#c3d6e0", "#e2e2e2", "#edcdba", "#e99e7c", "#d15e4b", "#b2182b", ], }; export const amColorScale = scaleLinear( AM_COLOR_SCALE.checkpoints, AM_COLOR_SCALE.colors ); // eslint-disable-next-line no-magic-numbers const defaultColor = Color(0x000000); const hexToPDBeMolstarColor = (hexColor: string) => { const rgb = color(hexColor)?.rgb() || { r: 100, g: 100, b: 100 }; return { r: rgb.r, g: rgb.g, b: rgb.b }; }; const getAverage = (scores: number[]) => { return scores.reduce((a: number, b: number) => a + b, 0) / scores.length; }; const AlphaMissenseColorTheme = CustomElementProperty.create({ label: "Colour by Alphamissense pathogenecity", name: "basic-wrapper-am-coloring", getData: async (model) => { const map = new Map(); if (model.entry.startsWith("AF")) { const residueIndex = model.atomicHierarchy.residueAtomSegments.index; const residueRowCount = model.atomicHierarchy.atoms._rowCount; const response = await fetch( `https://alphafold.ebi.ac.uk/files/${model.entry}-aa-substitutions.csv` ); const rawText = await response.text(); const scores = parseAMData(rawText); for (let i = 0; i < residueRowCount; i++) { const averageScore = getAverage(scores[residueIndex[i]]); map.set(i, averageScore); } } return { value: map }; }, coloring: { getColor: (e) => { const { r, g, b } = hexToPDBeMolstarColor(amColorScale(e)); return Color.fromRgb(r, g, b); }, defaultColor: defaultColor, }, getLabel: (e) => { return `Pathogenecity Score: ${e}`; }, }); export default AlphaMissenseColorTheme;