ANVIL.ts 17 KB

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