algorithm.ts 14 KB

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