algorithm.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Sebastian Bittrich <sebastian.bittrich@rcsb.org>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { Structure, StructureElement, StructureProperties, Unit } from '../../mol-model/structure';
  8. import { Task, RuntimeContext } from '../../mol-task';
  9. import { CentroidHelper } from '../../mol-math/geometry/centroid-helper';
  10. import { AccessibleSurfaceAreaParams } from '../../mol-model-props/computed/accessible-surface-area';
  11. import { Vec3 } from '../../mol-math/linear-algebra';
  12. import { getElementMoleculeType } from '../../mol-model/structure/util';
  13. import { MoleculeType } from '../../mol-model/structure/model/types';
  14. import { AccessibleSurfaceArea } from '../../mol-model-props/computed/accessible-surface-area/shrake-rupley';
  15. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  16. import { MembraneOrientation } from './prop';
  17. const LARGE_CA_THRESHOLD = 5000;
  18. const UPDATE_INTERVAL = 10;
  19. interface ANVILContext {
  20. structure: Structure,
  21. numberOfSpherePoints: number,
  22. stepSize: number,
  23. minThickness: number,
  24. maxThickness: number,
  25. asaCutoff: number,
  26. adjust: number,
  27. offsets: ArrayLike<number>,
  28. exposed: ArrayLike<number>,
  29. hydrophobic: ArrayLike<boolean>,
  30. centroid: Vec3,
  31. extent: number
  32. };
  33. export const ANVILParams = {
  34. numberOfSpherePoints: PD.Numeric(140, { min: 35, max: 700, step: 1 }, { description: 'Number of spheres/directions to test for membrane placement. Original value is 350.' }),
  35. stepSize: PD.Numeric(1, { min: 0.25, max: 4, step: 0.25 }, { description: 'Thickness of membrane slices that will be tested' }),
  36. minThickness: PD.Numeric(20, { min: 10, max: 30, step: 1}, { description: 'Minimum membrane thickness used during refinement' }),
  37. maxThickness: PD.Numeric(40, { min: 30, max: 50, step: 1}, { description: 'Maximum membrane thickness used during refinement' }),
  38. asaCutoff: PD.Numeric(40, { min: 10, max: 100, step: 1 }, { description: 'Relative ASA cutoff above which residues will be considered' }),
  39. adjust: PD.Numeric(14, { min: 0, max: 30, step: 1 }, { description: 'Minimum length of membrane-spanning regions (original values: 14 for alpha-helices and 5 for beta sheets). Set to 0 to not optimize membrane thickness.' })
  40. };
  41. export type ANVILParams = typeof ANVILParams
  42. export type ANVILProps = PD.Values<ANVILParams>
  43. /**
  44. * Implements:
  45. * Membrane positioning for high- and low-resolution protein structures through a binary classification approach
  46. * Guillaume Postic, Yassine Ghouzam, Vincent Guiraud, and Jean-Christophe Gelly
  47. * Protein Engineering, Design & Selection, 2015, 1–5
  48. * doi: 10.1093/protein/gzv063
  49. */
  50. export function computeANVIL(structure: Structure, props: ANVILProps) {
  51. return Task.create('Compute Membrane Orientation', async runtime => {
  52. return await calculate(runtime, structure, props);
  53. });
  54. }
  55. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  56. const v3add = Vec3.add;
  57. const v3clone = Vec3.clone;
  58. const v3create = Vec3.create;
  59. const v3distance = Vec3.distance;
  60. const v3dot = Vec3.dot;
  61. const v3magnitude = Vec3.magnitude;
  62. const v3normalize = Vec3.normalize;
  63. const v3scale = Vec3.scale;
  64. const v3scaleAndAdd = Vec3.scaleAndAdd;
  65. const v3set = Vec3.set;
  66. const v3squaredDistance = Vec3.squaredDistance;
  67. const v3sub = Vec3.sub;
  68. const v3zero = Vec3.zero;
  69. const centroidHelper = new CentroidHelper();
  70. async function initialize(structure: Structure, props: ANVILProps, accessibleSurfaceArea: AccessibleSurfaceArea): Promise<ANVILContext> {
  71. const l = StructureElement.Location.create(structure);
  72. const { label_atom_id, label_comp_id, x, y, z } = StructureProperties.atom;
  73. const asaCutoff = props.asaCutoff / 100;
  74. centroidHelper.reset();
  75. const offsets = new Array<number>();
  76. const exposed = new Array<number>();
  77. const hydrophobic = new Array<boolean>();
  78. const vec = v3zero();
  79. for (let i = 0, il = structure.units.length; i < il; ++i) {
  80. const unit = structure.units[i];
  81. const { elements } = unit;
  82. l.unit = unit;
  83. for (let j = 0, jl = elements.length; j < jl; ++j) {
  84. const eI = elements[j];
  85. l.element = eI;
  86. // consider only amino acids
  87. if (getElementMoleculeType(unit, eI) !== MoleculeType.Protein) {
  88. continue;
  89. }
  90. // only CA is considered for downstream operations
  91. if (label_atom_id(l) !== 'CA' && label_atom_id(l) !== 'BB') {
  92. continue;
  93. }
  94. // original ANVIL only considers canonical amino acids
  95. if (!MaxAsa[label_comp_id(l)]) {
  96. continue;
  97. }
  98. // while iterating use first pass to compute centroid
  99. v3set(vec, x(l), y(l), z(l));
  100. centroidHelper.includeStep(vec);
  101. // keep track of offsets and exposed state to reuse
  102. offsets.push(structure.serialMapping.getSerialIndex(l.unit, l.element));
  103. if (AccessibleSurfaceArea.getValue(l, accessibleSurfaceArea) / MaxAsa[label_comp_id(l)] > asaCutoff) {
  104. exposed.push(structure.serialMapping.getSerialIndex(l.unit, l.element));
  105. hydrophobic.push(isHydrophobic(label_comp_id(l)));
  106. }
  107. }
  108. }
  109. // calculate centroid and extent
  110. centroidHelper.finishedIncludeStep();
  111. const centroid = v3clone(centroidHelper.center);
  112. for (let k = 0, kl = offsets.length; k < kl; k++) {
  113. setLocation(l, structure, offsets[k]);
  114. v3set(vec, x(l), y(l), z(l));
  115. centroidHelper.radiusStep(vec);
  116. }
  117. const extent = 1.2 * Math.sqrt(centroidHelper.radiusSq);
  118. return {
  119. ...props,
  120. structure,
  121. offsets,
  122. exposed,
  123. hydrophobic,
  124. centroid,
  125. extent
  126. };
  127. }
  128. export async function calculate(runtime: RuntimeContext, structure: Structure, params: ANVILProps): Promise<MembraneOrientation> {
  129. // can't get away with the default 92 points here
  130. const asaProps = { ...PD.getDefaultValues(AccessibleSurfaceAreaParams), probeSize: 4.0, traceOnly: true, numberOfSpherePoints: 184 };
  131. const accessibleSurfaceArea = await AccessibleSurfaceArea.compute(structure, asaProps).runInContext(runtime);
  132. const ctx = await initialize(structure, params, accessibleSurfaceArea);
  133. const initialHphobHphil = HphobHphil.initial(ctx);
  134. const initialMembrane = (await findMembrane(runtime, 'Placing initial membrane...', ctx, generateSpherePoints(ctx, ctx.numberOfSpherePoints), initialHphobHphil))!;
  135. const refinedMembrane = (await findMembrane(runtime, 'Refining membrane placement...', ctx, findProximateAxes(ctx, initialMembrane), initialHphobHphil))!;
  136. let membrane = initialMembrane.qmax! > refinedMembrane.qmax! ? initialMembrane : refinedMembrane;
  137. if (ctx.adjust && ctx.offsets.length < LARGE_CA_THRESHOLD) {
  138. membrane = await adjustThickness(runtime, 'Adjusting membrane thickness...', ctx, membrane, initialHphobHphil);
  139. }
  140. const normalVector = v3zero();
  141. const center = v3zero();
  142. v3sub(normalVector, membrane.planePoint1, membrane.planePoint2);
  143. v3normalize(normalVector, normalVector);
  144. v3add(center, membrane.planePoint1, membrane.planePoint2);
  145. v3scale(center, center, 0.5);
  146. const extent = adjustExtent(ctx, membrane, center);
  147. return {
  148. planePoint1: membrane.planePoint1,
  149. planePoint2: membrane.planePoint2,
  150. normalVector,
  151. centroid: center,
  152. radius: extent
  153. };
  154. }
  155. interface MembraneCandidate {
  156. planePoint1: Vec3,
  157. planePoint2: Vec3,
  158. stats: HphobHphil,
  159. normalVector?: Vec3,
  160. spherePoint?: Vec3,
  161. qmax?: number
  162. }
  163. namespace MembraneCandidate {
  164. export function initial(c1: Vec3, c2: Vec3, stats: HphobHphil): MembraneCandidate {
  165. return {
  166. planePoint1: c1,
  167. planePoint2: c2,
  168. stats
  169. };
  170. }
  171. export function scored(spherePoint: Vec3, planePoint1: Vec3, planePoint2: Vec3, stats: HphobHphil, qmax: number, centroid: Vec3): MembraneCandidate {
  172. const normalVector = v3zero();
  173. v3sub(normalVector, centroid, spherePoint);
  174. return {
  175. planePoint1,
  176. planePoint2,
  177. stats,
  178. normalVector,
  179. spherePoint,
  180. qmax
  181. };
  182. }
  183. }
  184. async function findMembrane(runtime: RuntimeContext, message: string | undefined, ctx: ANVILContext, spherePoints: Vec3[], initialStats: HphobHphil): Promise<MembraneCandidate | undefined> {
  185. const { centroid, stepSize, minThickness, maxThickness } = ctx;
  186. // best performing membrane
  187. let membrane: MembraneCandidate | undefined;
  188. // score of the best performing membrane
  189. let qmax = 0;
  190. // construct slices of thickness 1.0 along the axis connecting the centroid and the spherePoint
  191. const diam = v3zero();
  192. for (let n = 0, nl = spherePoints.length; n < nl; n++) {
  193. if (runtime.shouldUpdate && message && (n + 1) % UPDATE_INTERVAL === 0) {
  194. await runtime.update({ message, current: (n + 1), max: nl });
  195. }
  196. const spherePoint = spherePoints[n];
  197. v3sub(diam, centroid, spherePoint);
  198. v3scale(diam, diam, 2);
  199. const diamNorm = v3magnitude(diam);
  200. const sliceStats = HphobHphil.sliced(ctx, stepSize, spherePoint, diam, diamNorm);
  201. const qvartemp = [];
  202. for (let i = 0, il = diamNorm - stepSize; i < il; i += stepSize) {
  203. const c1 = v3zero();
  204. const c2 = v3zero();
  205. v3scaleAndAdd(c1, spherePoint, diam, i / diamNorm);
  206. v3scaleAndAdd(c2, spherePoint, diam, (i + stepSize) / diamNorm);
  207. // evaluate how well this membrane slice embeddeds the peculiar residues
  208. const stats = sliceStats[Math.round(i / stepSize)];
  209. qvartemp.push(MembraneCandidate.initial(c1, c2, stats));
  210. }
  211. let jmax = Math.floor((minThickness / stepSize) - 1);
  212. for (let width = 0, widthl = maxThickness; width <= widthl;) {
  213. for (let i = 0, il = qvartemp.length - 1 - jmax; i < il; i++) {
  214. let hphob = 0;
  215. let hphil = 0;
  216. for (let j = 0; j < jmax; j++) {
  217. const ij = qvartemp[i + j];
  218. if (j === 0 || j === jmax - 1) {
  219. hphob += Math.floor(0.5 * ij.stats.hphob);
  220. hphil += 0.5 * ij.stats.hphil;
  221. } else {
  222. hphob += ij.stats.hphob;
  223. hphil += ij.stats.hphil;
  224. }
  225. }
  226. if (hphob !== 0) {
  227. const stats = { hphob, hphil };
  228. const qvaltest = qValue(stats, initialStats);
  229. if (qvaltest >= qmax) {
  230. qmax = qvaltest;
  231. membrane = MembraneCandidate.scored(spherePoint, qvartemp[i].planePoint1, qvartemp[i + jmax].planePoint2, stats, qmax, centroid);
  232. }
  233. }
  234. }
  235. jmax++;
  236. width = (jmax + 1) * stepSize;
  237. }
  238. }
  239. return membrane;
  240. }
  241. /** Adjust membrane thickness by maximizing the number of membrane segments. */
  242. async function adjustThickness(runtime: RuntimeContext, message: string | undefined, ctx: ANVILContext, membrane: MembraneCandidate, initialHphobHphil: HphobHphil): Promise<MembraneCandidate> {
  243. const { minThickness } = ctx;
  244. const step = 0.3;
  245. let maxThickness = v3distance(membrane.planePoint1, membrane.planePoint2);
  246. let maxNos = membraneSegments(ctx, membrane).length;
  247. let optimalThickness = membrane;
  248. let n = 0;
  249. const nl = Math.ceil((maxThickness - minThickness) / step);
  250. while (maxThickness > minThickness) {
  251. n++;
  252. if (runtime.shouldUpdate && message && n % UPDATE_INTERVAL === 0) {
  253. await runtime.update({ message, current: n, max: nl });
  254. }
  255. const p = {
  256. ...ctx,
  257. maxThickness,
  258. stepSize: step
  259. };
  260. const temp = await findMembrane(runtime, void 0, p, [membrane.spherePoint!], initialHphobHphil);
  261. if (temp) {
  262. const nos = membraneSegments(ctx, temp).length;
  263. if (nos > maxNos) {
  264. maxNos = nos;
  265. optimalThickness = temp;
  266. }
  267. }
  268. maxThickness -= step;
  269. }
  270. return optimalThickness;
  271. }
  272. /** Report auth_seq_ids for all transmembrane segments. Will reject segments that are shorter than the adjust parameter specifies. Missing residues are considered in-membrane. */
  273. function membraneSegments(ctx: ANVILContext, membrane: MembraneCandidate): ArrayLike<{ start: number, end: number }> {
  274. const { offsets, structure, adjust } = ctx;
  275. const { normalVector, planePoint1, planePoint2 } = membrane;
  276. const { units } = structure;
  277. const { elementIndices, unitIndices } = structure.serialMapping;
  278. const testPoint = v3zero();
  279. const { auth_seq_id } = StructureProperties.residue;
  280. const d1 = -v3dot(normalVector!, planePoint1);
  281. const d2 = -v3dot(normalVector!, planePoint2);
  282. const dMin = Math.min(d1, d2);
  283. const dMax = Math.max(d1, d2);
  284. const inMembrane: { [k: string]: Set<number> } = Object.create(null);
  285. const outMembrane: { [k: string]: Set<number> } = Object.create(null);
  286. const segments: Array<{ start: number, end: number }> = [];
  287. let authAsymId;
  288. let lastAuthAsymId = null;
  289. let authSeqId;
  290. let lastAuthSeqId = units[0].model.atomicHierarchy.residues.auth_seq_id.value((units[0] as Unit.Atomic).chainIndex[0]) - 1;
  291. let startOffset = 0;
  292. let endOffset = 0;
  293. // collect all residues in membrane layer
  294. for (let k = 0, kl = offsets.length; k < kl; k++) {
  295. const unit = units[unitIndices[offsets[k]]];
  296. if (!Unit.isAtomic(unit)) throw 'Property only available for atomic models.';
  297. const elementIndex = elementIndices[offsets[k]];
  298. authAsymId = unit.model.atomicHierarchy.chains.auth_asym_id.value(unit.chainIndex[elementIndex]);
  299. if (authAsymId !== lastAuthAsymId) {
  300. if (!inMembrane[authAsymId]) inMembrane[authAsymId] = new Set<number>();
  301. if (!outMembrane[authAsymId]) outMembrane[authAsymId] = new Set<number>();
  302. lastAuthAsymId = authAsymId;
  303. }
  304. authSeqId = unit.model.atomicHierarchy.residues.auth_seq_id.value(unit.residueIndex[elementIndex]);
  305. v3set(testPoint, unit.conformation.x(elementIndex), unit.conformation.y(elementIndex), unit.conformation.z(elementIndex));
  306. if (_isInMembranePlane(testPoint, normalVector!, dMin, dMax)) {
  307. inMembrane[authAsymId].add(authSeqId);
  308. } else {
  309. outMembrane[authAsymId].add(authSeqId);
  310. }
  311. }
  312. for (let k = 0, kl = offsets.length; k < kl; k++) {
  313. const unit = units[unitIndices[offsets[k]]];
  314. if (!Unit.isAtomic(unit)) throw 'Property only available for atomic models.';
  315. const elementIndex = elementIndices[offsets[k]];
  316. authAsymId = unit.model.atomicHierarchy.chains.auth_asym_id.value(unit.chainIndex[elementIndex]);
  317. authSeqId = unit.model.atomicHierarchy.residues.auth_seq_id.value(unit.residueIndex[elementIndex]);
  318. if (inMembrane[authAsymId].has(authSeqId)) {
  319. // chain change
  320. if (authAsymId !== lastAuthAsymId) {
  321. segments.push({ start: startOffset, end: endOffset });
  322. lastAuthAsymId = authAsymId;
  323. startOffset = k;
  324. endOffset = k;
  325. }
  326. // sequence gaps
  327. if (authSeqId !== lastAuthSeqId + 1) {
  328. if (outMembrane[authAsymId].has(lastAuthSeqId + 1)) {
  329. segments.push({ start: startOffset, end: endOffset });
  330. startOffset = k;
  331. }
  332. lastAuthSeqId = authSeqId;
  333. endOffset = k;
  334. } else {
  335. lastAuthSeqId++;
  336. endOffset++;
  337. }
  338. }
  339. }
  340. segments.push({ start: startOffset, end: endOffset });
  341. const l = StructureElement.Location.create(structure);
  342. let startAuth;
  343. let endAuth;
  344. const refinedSegments: Array<{ start: number, end: number }> = [];
  345. for (let k = 0, kl = segments.length; k < kl; k++) {
  346. const { start, end } = segments[k];
  347. if (start === 0 || end === offsets.length - 1) continue;
  348. // evaluate residues 1 pos outside of membrane
  349. setLocation(l, structure, offsets[start - 1]);
  350. v3set(testPoint, l.unit.conformation.x(l.element), l.unit.conformation.y(l.element), l.unit.conformation.z(l.element));
  351. const d3 = -v3dot(normalVector!, testPoint);
  352. setLocation(l, structure, offsets[end + 1]);
  353. v3set(testPoint, l.unit.conformation.x(l.element), l.unit.conformation.y(l.element), l.unit.conformation.z(l.element));
  354. const d4 = -v3dot(normalVector!, testPoint);
  355. if (Math.min(d3, d4) < dMin && Math.max(d3, d4) > dMax) {
  356. // reject this refinement
  357. setLocation(l, structure, offsets[start]);
  358. startAuth = auth_seq_id(l);
  359. setLocation(l, structure, offsets[end]);
  360. endAuth = auth_seq_id(l);
  361. if (Math.abs(startAuth - endAuth) + 1 < adjust) {
  362. return [];
  363. }
  364. refinedSegments.push(segments[k]);
  365. }
  366. }
  367. return refinedSegments;
  368. }
  369. /** Filter for membrane residues and calculate the final extent of the membrane layer */
  370. function adjustExtent(ctx: ANVILContext, membrane: MembraneCandidate, centroid: Vec3): number {
  371. const { offsets, structure } = ctx;
  372. const { normalVector, planePoint1, planePoint2 } = membrane;
  373. const l = StructureElement.Location.create(structure);
  374. const testPoint = v3zero();
  375. const { x, y, z } = StructureProperties.atom;
  376. const d1 = -v3dot(normalVector!, planePoint1);
  377. const d2 = -v3dot(normalVector!, planePoint2);
  378. const dMin = Math.min(d1, d2);
  379. const dMax = Math.max(d1, d2);
  380. let extent = 0;
  381. for (let k = 0, kl = offsets.length; k < kl; k++) {
  382. setLocation(l, structure, offsets[k]);
  383. v3set(testPoint, x(l), y(l), z(l));
  384. if (_isInMembranePlane(testPoint, normalVector!, dMin, dMax)) {
  385. const dsq = v3squaredDistance(testPoint, centroid);
  386. if (dsq > extent) extent = dsq;
  387. }
  388. }
  389. return Math.sqrt(extent);
  390. }
  391. function qValue(currentStats: HphobHphil, initialStats: HphobHphil): number {
  392. if(initialStats.hphob < 1) {
  393. initialStats.hphob = 0.1;
  394. }
  395. if(initialStats.hphil < 1) {
  396. initialStats.hphil += 1;
  397. }
  398. const part_tot = currentStats.hphob + currentStats.hphil;
  399. return (currentStats.hphob * (initialStats.hphil - currentStats.hphil) - currentStats.hphil * (initialStats.hphob - currentStats.hphob)) /
  400. Math.sqrt(part_tot * initialStats.hphob * initialStats.hphil * (initialStats.hphob + initialStats.hphil - part_tot));
  401. }
  402. export function isInMembranePlane(testPoint: Vec3, normalVector: Vec3, planePoint1: Vec3, planePoint2: Vec3): boolean {
  403. const d1 = -v3dot(normalVector, planePoint1);
  404. const d2 = -v3dot(normalVector, planePoint2);
  405. return _isInMembranePlane(testPoint, normalVector, Math.min(d1, d2), Math.max(d1, d2));
  406. }
  407. function _isInMembranePlane(testPoint: Vec3, normalVector: Vec3, min: number, max: number): boolean {
  408. const d = -v3dot(normalVector, testPoint);
  409. return d > min && d < max;
  410. }
  411. /** Generates a defined number of points on a sphere with radius = extent around the specified centroid */
  412. function generateSpherePoints(ctx: ANVILContext, numberOfSpherePoints: number): Vec3[] {
  413. const { centroid, extent } = ctx;
  414. const points = [];
  415. let oldPhi = 0, h, theta, phi;
  416. for(let k = 1, kl = numberOfSpherePoints + 1; k < kl; k++) {
  417. h = -1 + 2 * (k - 1) / (numberOfSpherePoints - 1);
  418. theta = Math.acos(h);
  419. phi = (k === 1 || k === numberOfSpherePoints) ? 0 : (oldPhi + 3.6 / Math.sqrt(numberOfSpherePoints * (1 - h * h))) % (2 * Math.PI);
  420. const point = v3create(
  421. extent * Math.sin(phi) * Math.sin(theta) + centroid[0],
  422. extent * Math.cos(theta) + centroid[1],
  423. extent * Math.cos(phi) * Math.sin(theta) + centroid[2]
  424. );
  425. points[k - 1] = point;
  426. oldPhi = phi;
  427. }
  428. return points;
  429. }
  430. /** Generates sphere points close to that of the initial membrane */
  431. function findProximateAxes(ctx: ANVILContext, membrane: MembraneCandidate): Vec3[] {
  432. const { numberOfSpherePoints, extent } = ctx;
  433. const points = generateSpherePoints(ctx, 30000);
  434. let j = 4;
  435. let sphere_pts2: Vec3[] = [];
  436. const s = 2 * extent / numberOfSpherePoints;
  437. while (sphere_pts2.length < numberOfSpherePoints) {
  438. const dsq = (s + j) * (s + j);
  439. sphere_pts2 = [];
  440. for (let i = 0, il = points.length; i < il; i++) {
  441. if (v3squaredDistance(points[i], membrane.spherePoint!) < dsq) {
  442. sphere_pts2.push(points[i]);
  443. }
  444. }
  445. j += 0.2;
  446. }
  447. return sphere_pts2;
  448. }
  449. interface HphobHphil {
  450. hphob: number,
  451. hphil: number
  452. }
  453. namespace HphobHphil {
  454. export function initial(ctx: ANVILContext): HphobHphil {
  455. const { exposed, hydrophobic } = ctx;
  456. let hphob = 0;
  457. let hphil = 0;
  458. for (let k = 0, kl = exposed.length; k < kl; k++) {
  459. if (hydrophobic[k]) {
  460. hphob++;
  461. } else {
  462. hphil++;
  463. }
  464. }
  465. return { hphob, hphil };
  466. }
  467. const testPoint = v3zero();
  468. export function sliced(ctx: ANVILContext, stepSize: number, spherePoint: Vec3, diam: Vec3, diamNorm: number): HphobHphil[] {
  469. const { exposed, hydrophobic, structure } = ctx;
  470. const { units, serialMapping } = structure;
  471. const { unitIndices, elementIndices } = serialMapping;
  472. const sliceStats: HphobHphil[] = [];
  473. for (let i = 0, il = diamNorm - stepSize; i < il; i += stepSize) {
  474. sliceStats[sliceStats.length] = { hphob: 0, hphil: 0 };
  475. }
  476. for (let i = 0, il = exposed.length; i < il; i++) {
  477. const unit = units[unitIndices[exposed[i]]];
  478. const elementIndex = elementIndices[exposed[i]];
  479. v3set(testPoint, unit.conformation.x(elementIndex), unit.conformation.y(elementIndex), unit.conformation.z(elementIndex));
  480. v3sub(testPoint, testPoint, spherePoint);
  481. if (hydrophobic[i]) {
  482. sliceStats[Math.floor(v3dot(testPoint, diam) / diamNorm / stepSize)].hphob++;
  483. } else {
  484. sliceStats[Math.floor(v3dot(testPoint, diam) / diamNorm / stepSize)].hphil++;
  485. }
  486. }
  487. return sliceStats;
  488. }
  489. }
  490. /** ANVIL-specific (not general) definition of membrane-favoring amino acids */
  491. const HYDROPHOBIC_AMINO_ACIDS = new Set(['ALA', 'CYS', 'GLY', 'HIS', 'ILE', 'LEU', 'MET', 'PHE', 'SER', 'TRP', 'VAL']);
  492. /** Returns true if ANVIL considers this as amino acid that favors being embedded in a membrane */
  493. export function isHydrophobic(label_comp_id: string): boolean {
  494. return HYDROPHOBIC_AMINO_ACIDS.has(label_comp_id);
  495. }
  496. /** Accessible surface area used for normalization. ANVIL uses 'Total-Side REL' values from NACCESS, from: Hubbard, S. J., & Thornton, J. M. (1993). naccess. Computer Program, Department of Biochemistry and Molecular Biology, University College London, 2(1). */
  497. export const MaxAsa: { [k: string]: number } = {
  498. 'ALA': 69.41,
  499. 'ARG': 201.25,
  500. 'ASN': 106.24,
  501. 'ASP': 102.69,
  502. 'CYS': 96.75,
  503. 'GLU': 134.74,
  504. 'GLN': 140.99,
  505. 'GLY': 32.33,
  506. 'HIS': 147.08,
  507. 'ILE': 137.96,
  508. 'LEU': 141.12,
  509. 'LYS': 163.30,
  510. 'MET': 156.64,
  511. 'PHE': 164.11,
  512. 'PRO': 119.90,
  513. 'SER': 78.11,
  514. 'THR': 101.70,
  515. 'TRP': 211.26,
  516. 'TYR': 177.38,
  517. 'VAL': 114.28
  518. };
  519. function setLocation(l: StructureElement.Location, structure: Structure, serialIndex: number) {
  520. l.structure = structure;
  521. l.unit = structure.units[structure.serialMapping.unitIndices[serialIndex]];
  522. l.element = structure.serialMapping.elementIndices[serialIndex];
  523. return l;
  524. }