ANVIL.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 { Task, RuntimeContext } from '../../../mol-task';
  8. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  9. import { Structure, StructureElement, StructureProperties } from '../../../mol-model/structure';
  10. import { getElementMoleculeType } from '../../../mol-model/structure/util';
  11. import { MoleculeType } from '../../../mol-model/structure/model/types';
  12. import { CentroidHelper } from '../../../mol-math/geometry/centroid-helper';
  13. import { Vec3 } from '../../../mol-math/linear-algebra';
  14. import { AccessibleSurfaceArea } from '../accessible-surface-area/shrake-rupley';
  15. import { AccessibleSurfaceAreaProvider } from '../accessible-surface-area';
  16. import { ANVILContext } from './anvil/common';
  17. import { MembraneOrientation } from '../../../mol-model/structure/model/properties/membrane-orientation';
  18. export const ANVILParams = {
  19. numberOfSpherePoints: PD.Numeric(120, { min: 35, max: 700, step: 1 }, { description: 'Number of spheres/directions to test for membrane placement. Original value is 350.' }),
  20. stepSize: PD.Numeric(1, { min: 0.25, max: 4, step: 0.25 }, { description: 'Thickness of membrane slices that will be tested' }),
  21. minThickness: PD.Numeric(20, { min: 10, max: 30, step: 1}, { description: 'Minimum membrane thickness used during refinement' }),
  22. maxThickness: PD.Numeric(40, { min: 30, max: 50, step: 1}, { description: 'Maximum membrane thickness used during refinement' }),
  23. afilter: PD.Numeric(40, { min: 10, max: 100, step: 1 }, { description: 'Absolute ASA cutoff above which residues will be considered' }),
  24. membranePointDensity: PD.Numeric(2, { min: 0.1, max: 10, step: 0.1 }, { description: 'Distance between points representing membrane layer'})
  25. };
  26. export type ANVILParams = typeof ANVILParams
  27. export type ANVILProps = PD.Values<ANVILParams>
  28. /**
  29. * Implements:
  30. * Membrane positioning for high- and low-resolution protein structures through a binary classification approach
  31. * Guillaume Postic, Yassine Ghouzam, Vincent Guiraud, and Jean-Christophe Gelly
  32. * Protein Engineering, Design & Selection, 2015, 1–5
  33. * doi: 10.1093/protein/gzv063
  34. */
  35. export function computeANVIL(structure: Structure, props: ANVILProps) {
  36. return Task.create('Compute Membrane Topology', async runtime => {
  37. return await calculate(runtime, structure, props);
  38. });
  39. }
  40. const l = StructureElement.Location.create(void 0);
  41. const centroidHelper = new CentroidHelper();
  42. function initialize(structure: Structure, props: ANVILProps): ANVILContext {
  43. const { label_atom_id, x, y, z } = StructureProperties.atom;
  44. const elementCount = structure.polymerResidueCount;
  45. centroidHelper.reset();
  46. l.structure = structure;
  47. let offsets = new Int32Array(elementCount);
  48. let exposed = new Array<boolean>(elementCount);
  49. const accessibleSurfaceArea = structure && AccessibleSurfaceAreaProvider.get(structure);
  50. const asa = accessibleSurfaceArea.value!;
  51. const vec = Vec3();
  52. let m = 0;
  53. for (let i = 0, il = structure.units.length; i < il; ++i) {
  54. const unit = structure.units[i];
  55. const { elements } = unit;
  56. l.unit = unit;
  57. for (let j = 0, jl = elements.length; j < jl; ++j) {
  58. const eI = elements[j];
  59. l.element = eI;
  60. // consider only amino acids
  61. if (getElementMoleculeType(unit, eI) !== MoleculeType.Protein) {
  62. continue;
  63. }
  64. // only CA is considered for downstream operations
  65. if (label_atom_id(l) !== 'CA') {
  66. continue;
  67. }
  68. // while iterating use first pass to compute centroid
  69. Vec3.set(vec, x(l), y(l), z(l));
  70. centroidHelper.includeStep(vec);
  71. // keep track of offsets and exposed state to reuse
  72. offsets[m] = l.element;
  73. exposed[m] = AccessibleSurfaceArea.getValue(l, asa) > props.afilter;
  74. m++;
  75. }
  76. }
  77. // omit potentially empty tail
  78. offsets = offsets.slice(0, m);
  79. exposed = exposed.slice(0, m);
  80. // calculate centroid and extent
  81. centroidHelper.finishedIncludeStep();
  82. const centroid = centroidHelper.center;
  83. for (let k = 0, kl = offsets.length; k < kl; k++) {
  84. setLocation(l, structure, offsets[k]);
  85. Vec3.set(vec, x(l), y(l), z(l));
  86. centroidHelper.radiusStep(vec);
  87. }
  88. const extent = 1.2 * Math.sqrt(centroidHelper.radiusSq);
  89. return {
  90. ...props,
  91. structure: structure,
  92. offsets: offsets,
  93. exposed: exposed,
  94. centroid: centroid,
  95. extent: extent
  96. };
  97. }
  98. export async function calculate(runtime: RuntimeContext, structure: Structure, params: ANVILProps): Promise<MembraneOrientation> {
  99. const { label_comp_id } = StructureProperties.residue;
  100. const ctx = initialize(structure, params);
  101. const initialHphobHphil = HphobHphil.filtered(ctx, label_comp_id);
  102. const initialMembrane = findMembrane(ctx, generateSpherePoints(ctx, ctx.numberOfSpherePoints), initialHphobHphil, label_comp_id);
  103. const alternativeMembrane = findMembrane(ctx, findProximateAxes(ctx, initialMembrane), initialHphobHphil, label_comp_id);
  104. const membrane = initialMembrane.qmax! > alternativeMembrane.qmax! ? initialMembrane : alternativeMembrane;
  105. return MembraneOrientation(membrane.planePoint1, membrane.planePoint2, membrane.normalVector!, ctx.extent * ctx.extent);
  106. }
  107. interface MembraneCandidate {
  108. planePoint1: Vec3,
  109. planePoint2: Vec3,
  110. stats: HphobHphil,
  111. normalVector?: Vec3,
  112. spherePoint?: Vec3,
  113. qmax?: number
  114. }
  115. namespace MembraneCandidate {
  116. export function initial(c1: Vec3, c2: Vec3, stats: HphobHphil): MembraneCandidate {
  117. return {
  118. planePoint1: c1,
  119. planePoint2: c2,
  120. stats: stats
  121. };
  122. }
  123. export function scored(spherePoint: Vec3, c1: Vec3, c2: Vec3, stats: HphobHphil, qmax: number, centroid: Vec3): MembraneCandidate {
  124. const diam_vect = Vec3();
  125. Vec3.sub(diam_vect, centroid, spherePoint);
  126. return {
  127. planePoint1: c1,
  128. planePoint2: c2,
  129. stats: stats,
  130. normalVector: diam_vect,
  131. spherePoint: spherePoint,
  132. qmax: qmax
  133. };
  134. }
  135. }
  136. function findMembrane(ctx: ANVILContext, spherePoints: Vec3[], initialStats: HphobHphil, label_comp_id: StructureElement.Property<string>): MembraneCandidate {
  137. const { centroid, stepSize, minThickness, maxThickness } = ctx;
  138. // best performing membrane
  139. let membrane: MembraneCandidate;
  140. // score of the best performing membrane
  141. let qmax = 0;
  142. // construct slices of thickness 1.0 along the axis connecting the centroid and the spherePoint
  143. const diam = Vec3();
  144. for (let i = 0, il = spherePoints.length; i < il; i++) {
  145. const spherePoint = spherePoints[i];
  146. Vec3.sub(diam, centroid, spherePoint);
  147. Vec3.scale(diam, diam, 2);
  148. const diamNorm = Vec3.magnitude(diam);
  149. const qvartemp = [];
  150. for (let i = 0, il = diamNorm - stepSize; i < il; i += stepSize) {
  151. const c1 = Vec3();
  152. const c2 = Vec3();
  153. Vec3.scaleAndAdd(c1, spherePoint, diam, i / diamNorm);
  154. Vec3.scaleAndAdd(c2, spherePoint, diam, (i + stepSize) / diamNorm);
  155. // evaluate how well this membrane slice embeddeds the peculiar residues
  156. const stats = HphobHphil.filtered(ctx, label_comp_id, (testPoint: Vec3) => isInMembranePlane(testPoint, diam, c1, c2));
  157. qvartemp.push(MembraneCandidate.initial(c1, c2, stats));
  158. }
  159. let jmax = (minThickness / stepSize) - 1;
  160. for (let width = 0, widthl = maxThickness; width < widthl;) {
  161. const imax = qvartemp.length - 1 - jmax;
  162. for (let i = 0, il = imax; i < il; i++) {
  163. const c1 = qvartemp[i].planePoint1;
  164. const c2 = qvartemp[i + jmax].planePoint2;
  165. let hphob = 0;
  166. let hphil = 0;
  167. let total = 0;
  168. for (let j = 0; j < jmax; j++) {
  169. const ij = qvartemp[i + j];
  170. if (j === 0 || j === jmax - 1) {
  171. hphob += 0.5 * ij.stats.hphob;
  172. hphil += 0.5 * ij.stats.hphil;
  173. } else {
  174. hphob += ij.stats.hphob;
  175. hphil += ij.stats.hphil;
  176. }
  177. total += ij.stats.total;
  178. }
  179. const stats = HphobHphil.of(hphob, hphil, total);
  180. if (hphob !== 0) {
  181. const qvaltest = qValue(stats, initialStats);
  182. if (qvaltest > qmax) {
  183. qmax = qvaltest;
  184. membrane = MembraneCandidate.scored(spherePoint, c1, c2, HphobHphil.of(hphob, hphil, total), qmax, centroid);
  185. }
  186. }
  187. }
  188. jmax++;
  189. width = (jmax + 1) * stepSize;
  190. }
  191. }
  192. return membrane!;
  193. }
  194. function qValue(currentStats: HphobHphil, initialStats: HphobHphil): number {
  195. if(initialStats.hphob < 1) {
  196. initialStats.hphob = 0.1;
  197. }
  198. if(initialStats.hphil < 1) {
  199. initialStats.hphil += 1;
  200. }
  201. const part_tot = currentStats.hphob + currentStats.hphil;
  202. return (currentStats.hphob * (initialStats.hphil - currentStats.hphil) - currentStats.hphil * (initialStats.hphob - currentStats.hphob)) /
  203. Math.sqrt(part_tot * initialStats.hphob * initialStats.hphil * (initialStats.hphob + initialStats.hphil - part_tot));
  204. }
  205. function isInMembranePlane(testPoint: Vec3, normalVector: Vec3, planePoint1: Vec3, planePoint2: Vec3): boolean {
  206. const d1 = -Vec3.dot(normalVector, planePoint1);
  207. const d2 = -Vec3.dot(normalVector, planePoint2);
  208. const d = -Vec3.dot(normalVector, testPoint);
  209. return d > Math.min(d1, d2) && d < Math.max(d1, d2);
  210. }
  211. // generates a defined number of points on a sphere with radius = extent around the specified centroid
  212. function generateSpherePoints(ctx: ANVILContext, numberOfSpherePoints: number): Vec3[] {
  213. const { centroid, extent } = ctx;
  214. const points = [];
  215. let oldPhi = 0, h, theta, phi;
  216. for(let k = 1, kl = numberOfSpherePoints + 1; k < kl; k++) {
  217. h = -1 + 2 * (k - 1) / (numberOfSpherePoints - 1);
  218. theta = Math.acos(h);
  219. phi = (k === 1 || k === numberOfSpherePoints) ? 0 : (oldPhi + 3.6 / Math.sqrt(numberOfSpherePoints * (1 - h * h))) % (2 * Math.PI);
  220. const point = Vec3.create(
  221. extent * Math.sin(phi) * Math.sin(theta) + centroid[0],
  222. extent * Math.cos(theta) + centroid[1],
  223. extent * Math.cos(phi) * Math.sin(theta) + centroid[2]
  224. );
  225. points[k - 1] = point;
  226. oldPhi = phi;
  227. }
  228. return points;
  229. }
  230. // generates sphere points close to that of the initial membrane
  231. function findProximateAxes(ctx: ANVILContext, membrane: MembraneCandidate): Vec3[] {
  232. const { numberOfSpherePoints, extent } = ctx;
  233. const points = generateSpherePoints(ctx, 30000);
  234. let j = 4;
  235. let sphere_pts2: Vec3[] = [];
  236. while (sphere_pts2.length < numberOfSpherePoints) {
  237. const d = 2 * extent / numberOfSpherePoints + j;
  238. const dsq = d * d;
  239. sphere_pts2 = [];
  240. for (let i = 0, il = points.length; i < il; i++) {
  241. if (Vec3.squaredDistance(points[i], membrane.spherePoint!) < dsq) {
  242. sphere_pts2.push(points[i]);
  243. }
  244. }
  245. j += 0.2;
  246. }
  247. return sphere_pts2;
  248. }
  249. interface HphobHphil {
  250. hphob: number,
  251. hphil: number,
  252. total: number
  253. }
  254. namespace HphobHphil {
  255. export function of(hphob: number, hphil: number, total?: number) {
  256. return {
  257. hphob: hphob,
  258. hphil: hphil,
  259. total: !!total ? total : hphob + hphil
  260. };
  261. }
  262. const testPoint = Vec3();
  263. export function filtered(ctx: ANVILContext, label_comp_id: StructureElement.Property<string>, filter?: (test: Vec3) => boolean): HphobHphil {
  264. const { offsets, exposed, structure } = ctx;
  265. const { x, y, z } = StructureProperties.atom;
  266. let hphob = 0;
  267. let hphil = 0;
  268. for (let k = 0, kl = offsets.length; k < kl; k++) {
  269. // ignore buried residues
  270. if (!exposed[k]) {
  271. continue;
  272. }
  273. setLocation(l, structure, offsets[k]);
  274. Vec3.set(testPoint, x(l), y(l), z(l));
  275. // testPoints have to be in putative membrane layer
  276. if (filter && !filter(testPoint)) {
  277. continue;
  278. }
  279. if (isHydrophobic(label_comp_id(l))) {
  280. hphob++;
  281. } else {
  282. hphil++;
  283. }
  284. }
  285. return of(hphob, hphil);
  286. }
  287. // ANVIL-specific (not general) definition of membrane-favoring amino acids
  288. const HYDROPHOBIC_AMINO_ACIDS = new Set(['ALA', 'CYS', 'GLY', 'HIS', 'ILE', 'LEU', 'MET', 'PHE', 'SER', 'THR', 'VAL']);
  289. function isHydrophobic(label_comp_id: string): boolean {
  290. return HYDROPHOBIC_AMINO_ACIDS.has(label_comp_id);
  291. }
  292. }
  293. function setLocation(l: StructureElement.Location, structure: Structure, serialIndex: number) {
  294. l.structure = structure;
  295. l.unit = structure.units[structure.serialMapping.unitIndices[serialIndex]];
  296. l.element = structure.serialMapping.elementIndices[serialIndex];
  297. return l;
  298. }