molstar
Version:
A comprehensive macromolecular library.
323 lines (322 loc) • 14.5 kB
JavaScript
/**
* Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author Fred Ludlow <Fred.Ludlow@astx.com>
*
* based in part on NGL (https://github.com/arose/ngl)
*/
import { ParamDefinition as PD } from '../../../mol-util/param-definition';
import { Features } from './features';
import { ProteinBackboneAtoms, PolymerNames, BaseNames } from '../../../mol-model/structure/model/types';
import { typeSymbol, atomId, eachBondedAtom } from '../chemistry/util';
import { ValenceModelProvider } from '../valence-model';
import { degToRad } from '../../../mol-math/misc';
import { FeatureGroup, InteractionType } from './common';
import { Segmentation } from '../../../mol-data/int';
import { isGuanidine, isAcetamidine, isPhosphate, isSulfonicAcid, isSulfate, isCarboxylate } from '../chemistry/functional-group';
import { Vec3 } from '../../../mol-math/linear-algebra';
const IonicParams = {
distanceMax: PD.Numeric(5.0, { min: 0, max: 8, step: 0.1 }),
};
const PiStackingParams = {
distanceMax: PD.Numeric(5.5, { min: 1, max: 8, step: 0.1 }),
offsetMax: PD.Numeric(2.0, { min: 0, max: 4, step: 0.1 }),
angleDevMax: PD.Numeric(30, { min: 0, max: 180, step: 1 }),
};
const CationPiParams = {
distanceMax: PD.Numeric(6.0, { min: 1, max: 8, step: 0.1 }),
offsetMax: PD.Numeric(2.0, { min: 0, max: 4, step: 0.1 }),
};
//
const PositvelyCharged = ['ARG', 'HIS', 'LYS'];
const NegativelyCharged = ['GLU', 'ASP'];
function getUnitValenceModel(structure, unit) {
const valenceModel = ValenceModelProvider.get(structure).value;
if (!valenceModel)
throw Error('expected valence model to be available');
const unitValenceModel = valenceModel.get(unit.id);
if (!unitValenceModel)
throw Error('expected valence model for unit to be available');
return unitValenceModel;
}
function addUnitPositiveCharges(structure, unit, builder) {
const { charge } = getUnitValenceModel(structure, unit);
const { elements } = unit;
const { x, y, z } = unit.model.atomicConformation;
const addedElements = new Set();
const { label_comp_id } = unit.model.atomicHierarchy.atoms;
const residueIt = Segmentation.transientSegments(unit.model.atomicHierarchy.residueAtomSegments, elements);
while (residueIt.hasNext) {
const { index: residueIndex, start, end } = residueIt.move();
const compId = label_comp_id.value(unit.model.atomicHierarchy.residueAtomSegments.offsets[residueIndex]);
if (PositvelyCharged.includes(compId)) {
builder.startState();
for (let j = start; j < end; ++j) {
if (typeSymbol(unit, j) === "N" /* Elements.N */ && !ProteinBackboneAtoms.has(atomId(unit, j))) {
builder.pushMember(x[elements[j]], y[elements[j]], z[elements[j]], j);
}
}
builder.finishState(1 /* FeatureType.PositiveCharge */, FeatureGroup.None);
}
else if (!PolymerNames.has(compId)) {
addedElements.clear();
for (let j = start; j < end; ++j) {
let group = FeatureGroup.None;
if (isGuanidine(structure, unit, j)) {
group = FeatureGroup.Guanidine;
}
else if (isAcetamidine(structure, unit, j)) {
group = FeatureGroup.Acetamidine;
}
if (group) {
builder.startState();
eachBondedAtom(structure, unit, j, (_, k) => {
if (typeSymbol(unit, k) === "N" /* Elements.N */) {
addedElements.add(k);
builder.pushMember(x[elements[k]], y[elements[k]], z[elements[k]], k);
}
});
builder.finishState(1 /* FeatureType.PositiveCharge */, group);
}
}
for (let j = start; j < end; ++j) {
if (charge[j] > 0 && !addedElements.has(j)) {
builder.add(1 /* FeatureType.PositiveCharge */, FeatureGroup.None, x[elements[j]], y[elements[j]], z[elements[j]], j);
}
}
}
}
}
function addUnitNegativeCharges(structure, unit, builder) {
const { charge } = getUnitValenceModel(structure, unit);
const { elements } = unit;
const { x, y, z } = unit.model.atomicConformation;
const addedElements = new Set();
const { label_comp_id } = unit.model.atomicHierarchy.atoms;
const residueIt = Segmentation.transientSegments(unit.model.atomicHierarchy.residueAtomSegments, elements);
while (residueIt.hasNext) {
const { index: residueIndex, start, end } = residueIt.move();
const compId = label_comp_id.value(unit.model.atomicHierarchy.residueAtomSegments.offsets[residueIndex]);
if (NegativelyCharged.includes(compId)) {
builder.startState();
for (let j = start; j < end; ++j) {
if (typeSymbol(unit, j) === "O" /* Elements.O */ && !ProteinBackboneAtoms.has(atomId(unit, j))) {
builder.pushMember(x[elements[j]], y[elements[j]], z[elements[j]], j);
}
}
builder.finishState(2 /* FeatureType.NegativeCharge */, FeatureGroup.None);
}
else if (BaseNames.has(compId)) {
for (let j = start; j < end; ++j) {
if (isPhosphate(structure, unit, j)) {
builder.startState();
eachBondedAtom(structure, unit, j, (_, k) => {
if (typeSymbol(unit, k) === "O" /* Elements.O */) {
builder.pushMember(x[elements[k]], y[elements[k]], z[elements[k]], k);
}
});
builder.finishState(2 /* FeatureType.NegativeCharge */, FeatureGroup.Phosphate);
}
}
}
else if (!PolymerNames.has(compId)) {
for (let j = start; j < end; ++j) {
builder.startState();
if (typeSymbol(unit, j) === "N" /* Elements.N */ && !ProteinBackboneAtoms.has(atomId(unit, j))) {
builder.pushMember(x[elements[j]], y[elements[j]], z[elements[j]], j);
}
builder.finishState(2 /* FeatureType.NegativeCharge */, FeatureGroup.None);
let group = FeatureGroup.None;
if (isSulfonicAcid(structure, unit, j)) {
group = FeatureGroup.SulfonicAcid;
}
else if (isPhosphate(structure, unit, j)) {
group = FeatureGroup.Phosphate;
}
else if (isSulfate(structure, unit, j)) {
group = FeatureGroup.Sulfate;
}
else if (isCarboxylate(structure, unit, j)) {
group = FeatureGroup.Carboxylate;
}
if (group) {
builder.startState();
eachBondedAtom(structure, unit, j, (_, k) => {
if (typeSymbol(unit, k) === "O" /* Elements.O */) {
addedElements.add(k);
builder.pushMember(x[elements[k]], y[elements[k]], z[elements[k]], k);
}
});
builder.finishState(2 /* FeatureType.NegativeCharge */, group);
}
}
for (let j = start; j < end; ++j) {
if (charge[j] < 0 && !addedElements.has(j)) {
builder.add(2 /* FeatureType.NegativeCharge */, FeatureGroup.None, x[elements[j]], y[elements[j]], z[elements[j]], j);
}
}
}
}
}
function addUnitAromaticRings(structure, unit, builder) {
const { elements } = unit;
const { x, y, z } = unit.model.atomicConformation;
for (const ringIndex of unit.rings.aromaticRings) {
const ring = unit.rings.all[ringIndex];
builder.startState();
for (let i = 0, il = ring.length; i < il; ++i) {
const j = ring[i];
builder.pushMember(x[elements[j]], y[elements[j]], z[elements[j]], j);
}
builder.finishState(3 /* FeatureType.AromaticRing */, FeatureGroup.None);
}
}
function isIonic(ti, tj) {
return ((ti === 2 /* FeatureType.NegativeCharge */ && tj === 1 /* FeatureType.PositiveCharge */) ||
(ti === 1 /* FeatureType.PositiveCharge */ && tj === 2 /* FeatureType.NegativeCharge */));
}
function isPiStacking(ti, tj) {
return ti === 3 /* FeatureType.AromaticRing */ && tj === 3 /* FeatureType.AromaticRing */;
}
function isCationPi(ti, tj) {
return ((ti === 3 /* FeatureType.AromaticRing */ && tj === 1 /* FeatureType.PositiveCharge */) ||
(ti === 1 /* FeatureType.PositiveCharge */ && tj === 3 /* FeatureType.AromaticRing */));
}
const tmpPointA = Vec3();
const tmpPointB = Vec3();
function areFeaturesWithinDistanceSq(infoA, infoB, distanceSq) {
const { feature: featureA, offsets: offsetsA, members: membersA } = infoA;
const { feature: featureB, offsets: offsetsB, members: membersB } = infoB;
for (let i = offsetsA[featureA], il = offsetsA[featureA + 1]; i < il; ++i) {
const elementA = membersA[i];
infoA.unit.conformation.position(infoA.unit.elements[elementA], tmpPointA);
for (let j = offsetsB[featureB], jl = offsetsB[featureB + 1]; j < jl; ++j) {
const elementB = membersB[j];
infoB.unit.conformation.position(infoB.unit.elements[elementB], tmpPointB);
if (Vec3.squaredDistance(tmpPointA, tmpPointB) < distanceSq)
return true;
}
}
return false;
}
const tmpVecA = Vec3();
const tmpVecB = Vec3();
const tmpVecC = Vec3();
const tmpVecD = Vec3();
function getNormal(out, info) {
const { unit, feature, offsets, members } = info;
const { elements } = unit;
const i = offsets[feature];
info.unit.conformation.position(elements[members[i]], tmpVecA);
info.unit.conformation.position(elements[members[i + 1]], tmpVecB);
info.unit.conformation.position(elements[members[i + 2]], tmpVecC);
return Vec3.triangleNormal(out, tmpVecA, tmpVecB, tmpVecC);
}
const getOffset = function (infoA, infoB, normal) {
Features.position(tmpVecA, infoA);
Features.position(tmpVecB, infoB);
Vec3.sub(tmpVecC, tmpVecA, tmpVecB);
Vec3.projectOnPlane(tmpVecD, tmpVecC, normal);
Vec3.add(tmpVecD, tmpVecD, tmpVecB);
return Vec3.distance(tmpVecD, tmpVecB);
};
function getIonicOptions(props) {
return {
distanceMaxSq: props.distanceMax * props.distanceMax,
};
}
function getPiStackingOptions(props) {
return {
offsetMax: props.offsetMax,
angleDevMax: degToRad(props.angleDevMax),
};
}
function getCationPiOptions(props) {
return {
offsetMax: props.offsetMax
};
}
const deg180InRad = degToRad(180);
const deg90InRad = degToRad(90);
const tmpNormalA = Vec3();
const tmpNormalB = Vec3();
function testIonic(structure, infoA, infoB, distanceSq, opts) {
const typeA = infoA.types[infoA.feature];
const typeB = infoB.types[infoB.feature];
if (isIonic(typeA, typeB)) {
if (areFeaturesWithinDistanceSq(infoA, infoB, opts.distanceMaxSq)) {
return InteractionType.Ionic;
}
}
}
function testPiStacking(structure, infoA, infoB, distanceSq, opts) {
const typeA = infoA.types[infoA.feature];
const typeB = infoB.types[infoB.feature];
if (isPiStacking(typeA, typeB)) {
getNormal(tmpNormalA, infoA);
getNormal(tmpNormalB, infoB);
const angle = Vec3.angle(tmpNormalA, tmpNormalB);
const offset = Math.min(getOffset(infoA, infoB, tmpNormalB), getOffset(infoB, infoA, tmpNormalA));
if (offset <= opts.offsetMax) {
if (angle <= opts.angleDevMax || angle >= deg180InRad - opts.angleDevMax) {
return InteractionType.PiStacking; // parallel
}
else if (angle <= opts.angleDevMax + deg90InRad && angle >= deg90InRad - opts.angleDevMax) {
return InteractionType.PiStacking; // t-shaped
}
}
}
}
function testCationPi(structure, infoA, infoB, distanceSq, opts) {
const typeA = infoA.types[infoA.feature];
const typeB = infoB.types[infoB.feature];
if (isCationPi(typeA, typeB)) {
const [infoR, infoC] = typeA === 3 /* FeatureType.AromaticRing */ ? [infoA, infoB] : [infoB, infoA];
getNormal(tmpNormalA, infoR);
const offset = getOffset(infoC, infoR, tmpNormalA);
if (offset <= opts.offsetMax) {
return InteractionType.CationPi;
}
}
}
//
export const NegativChargeProvider = Features.Provider([2 /* FeatureType.NegativeCharge */], addUnitNegativeCharges);
export const PositiveChargeProvider = Features.Provider([1 /* FeatureType.PositiveCharge */], addUnitPositiveCharges);
export const AromaticRingProvider = Features.Provider([3 /* FeatureType.AromaticRing */], addUnitAromaticRings);
export const IonicProvider = {
name: 'ionic',
params: IonicParams,
createTester: (props) => {
const opts = getIonicOptions(props);
return {
maxDistance: props.distanceMax,
requiredFeatures: new Set([2 /* FeatureType.NegativeCharge */, 1 /* FeatureType.PositiveCharge */]),
getType: (structure, infoA, infoB, distanceSq) => testIonic(structure, infoA, infoB, distanceSq, opts)
};
}
};
export const PiStackingProvider = {
name: 'pi-stacking',
params: PiStackingParams,
createTester: (props) => {
const opts = getPiStackingOptions(props);
return {
maxDistance: props.distanceMax,
requiredFeatures: new Set([3 /* FeatureType.AromaticRing */]),
getType: (structure, infoA, infoB, distanceSq) => testPiStacking(structure, infoA, infoB, distanceSq, opts)
};
}
};
export const CationPiProvider = {
name: 'cation-pi',
params: CationPiParams,
createTester: (props) => {
const opts = getCationPiOptions(props);
return {
maxDistance: props.distanceMax,
requiredFeatures: new Set([3 /* FeatureType.AromaticRing */, 1 /* FeatureType.PositiveCharge */]),
getType: (structure, infoA, infoB, distanceSq) => testCationPi(structure, infoA, infoB, distanceSq, opts)
};
}
};