trace-iterator.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Unit, StructureElement, ElementIndex, ResidueIndex, Structure } from '../../../../../mol-model/structure';
  7. import { Segmentation, SortedArray } from '../../../../../mol-data/int';
  8. import { MoleculeType, SecondaryStructureType } from '../../../../../mol-model/structure/model/types';
  9. import Iterator from '../../../../../mol-data/iterator';
  10. import { Vec3 } from '../../../../../mol-math/linear-algebra';
  11. import SortedRanges from '../../../../../mol-data/int/sorted-ranges';
  12. import { CoarseSphereConformation, CoarseGaussianConformation } from '../../../../../mol-model/structure/model/properties/coarse';
  13. import { getPolymerRanges } from '../polymer';
  14. import { AtomicConformation } from '../../../../../mol-model/structure/model/properties/atomic';
  15. import { SecondaryStructureProvider } from '../../../../../mol-model-props/computed/secondary-structure';
  16. import { SecondaryStructure } from '../../../../../mol-model/structure/model/properties/seconday-structure';
  17. /**
  18. * Iterates over individual residues/coarse elements in polymers of a unit while
  19. * providing information about the neighbourhood in the underlying model for drawing splines
  20. */
  21. export function PolymerTraceIterator(unit: Unit, structure: Structure): Iterator<PolymerTraceElement> {
  22. switch (unit.kind) {
  23. case Unit.Kind.Atomic: return new AtomicPolymerTraceIterator(unit, structure);
  24. case Unit.Kind.Spheres:
  25. case Unit.Kind.Gaussians:
  26. return new CoarsePolymerTraceIterator(unit, structure);
  27. }
  28. }
  29. interface PolymerTraceElement {
  30. center: StructureElement.Location
  31. centerPrev: StructureElement.Location
  32. centerNext: StructureElement.Location
  33. first: boolean, last: boolean
  34. initial: boolean, final: boolean
  35. secStrucFirst: boolean, secStrucLast: boolean
  36. secStrucType: SecondaryStructureType
  37. moleculeType: MoleculeType
  38. isCoarseBackbone: boolean
  39. coarseBackboneFirst: boolean, coarseBackboneLast: boolean
  40. p0: Vec3, p1: Vec3, p2: Vec3, p3: Vec3, p4: Vec3
  41. d12: Vec3, d23: Vec3
  42. }
  43. const SecStrucTypeNA = SecondaryStructureType.create(SecondaryStructureType.Flag.NA);
  44. function createPolymerTraceElement (structure: Structure, unit: Unit): PolymerTraceElement {
  45. return {
  46. center: StructureElement.Location.create(structure, unit),
  47. centerPrev: StructureElement.Location.create(structure, unit),
  48. centerNext: StructureElement.Location.create(structure, unit),
  49. first: false, last: false,
  50. initial: false, final: false,
  51. secStrucFirst: false, secStrucLast: false,
  52. secStrucType: SecStrucTypeNA,
  53. moleculeType: MoleculeType.Unknown,
  54. coarseBackboneFirst: false, coarseBackboneLast: false,
  55. isCoarseBackbone: false,
  56. p0: Vec3(), p1: Vec3(), p2: Vec3(), p3: Vec3(), p4: Vec3(),
  57. d12: Vec3(), d23: Vec3()
  58. };
  59. }
  60. const enum AtomicPolymerTraceIteratorState { nextPolymer, nextResidue }
  61. const tmpDir = Vec3();
  62. const tmpVecA = Vec3();
  63. const tmpVecB = Vec3();
  64. export class AtomicPolymerTraceIterator implements Iterator<PolymerTraceElement> {
  65. private value: PolymerTraceElement
  66. private polymerIt: SortedRanges.Iterator<ElementIndex, number>
  67. private residueIt: Segmentation.SegmentIterator<ResidueIndex>
  68. private polymerSegment: Segmentation.Segment<number>
  69. private cyclicPolymerMap: Map<ResidueIndex, ResidueIndex>
  70. private secondaryStructureType: SecondaryStructure['type']
  71. private secondaryStructureGetIndex: SecondaryStructure['getIndex']
  72. private residueSegmentMin: ResidueIndex
  73. private residueSegmentMax: ResidueIndex
  74. private prevSecStrucType: SecondaryStructureType
  75. private currSecStrucType: SecondaryStructureType
  76. private nextSecStrucType: SecondaryStructureType
  77. private prevCoarseBackbone: boolean
  78. private currCoarseBackbone: boolean
  79. private nextCoarseBackbone: boolean
  80. private state: AtomicPolymerTraceIteratorState = AtomicPolymerTraceIteratorState.nextPolymer
  81. private polymerRanges: SortedArray<ElementIndex>
  82. private residueAtomSegments: Segmentation<ElementIndex, ResidueIndex>
  83. private traceElementIndex: ArrayLike<ElementIndex>
  84. private directionFromElementIndex: ArrayLike<ElementIndex | -1>
  85. private directionToElementIndex: ArrayLike<ElementIndex | -1>
  86. private moleculeType: ArrayLike<MoleculeType>
  87. private atomicConformation: AtomicConformation
  88. private p0 = Vec3()
  89. private p1 = Vec3()
  90. private p2 = Vec3()
  91. private p3 = Vec3()
  92. private p4 = Vec3()
  93. private p5 = Vec3()
  94. private p6 = Vec3()
  95. private d01 = Vec3()
  96. private d12 = Vec3()
  97. private d23 = Vec3()
  98. private d34 = Vec3()
  99. hasNext: boolean = false;
  100. private pos(target: Vec3, index: number) {
  101. if (index !== -1) {
  102. target[0] = this.atomicConformation.x[index];
  103. target[1] = this.atomicConformation.y[index];
  104. target[2] = this.atomicConformation.z[index];
  105. }
  106. }
  107. private updateResidueSegmentRange(polymerSegment: Segmentation.Segment<number>) {
  108. const { index } = this.residueAtomSegments;
  109. this.residueSegmentMin = index[this.polymerRanges[polymerSegment.index * 2]];
  110. this.residueSegmentMax = index[this.polymerRanges[polymerSegment.index * 2 + 1]];
  111. }
  112. private getResidueIndex(residueIndex: number) {
  113. if (residueIndex < this.residueSegmentMin) {
  114. const cyclicIndex = this.cyclicPolymerMap.get(this.residueSegmentMin);
  115. if (cyclicIndex !== undefined) {
  116. residueIndex = cyclicIndex - (this.residueSegmentMin - residueIndex - 1);
  117. } else {
  118. residueIndex = this.residueSegmentMin;
  119. }
  120. } else if (residueIndex > this.residueSegmentMax) {
  121. const cyclicIndex = this.cyclicPolymerMap.get(this.residueSegmentMax);
  122. if (cyclicIndex !== undefined) {
  123. residueIndex = cyclicIndex + (residueIndex - this.residueSegmentMax - 1);
  124. } else {
  125. residueIndex = this.residueSegmentMax;
  126. }
  127. }
  128. return residueIndex as ResidueIndex;
  129. }
  130. private getSecStruc(residueIndex: number) {
  131. return this.secondaryStructureType[this.secondaryStructureGetIndex(residueIndex as ResidueIndex)];
  132. }
  133. private setControlPoint(out: Vec3, p1: Vec3, p2: Vec3, p3: Vec3, residueIndex: ResidueIndex) {
  134. const ss = this.getSecStruc(residueIndex);
  135. if (SecondaryStructureType.is(ss, SecondaryStructureType.Flag.Beta)) {
  136. Vec3.scale(out, Vec3.add(out, p1, Vec3.add(out, p3, Vec3.add(out, p2, p2))), 1 / 4);
  137. } else {
  138. Vec3.copy(out, p2);
  139. }
  140. }
  141. private setFromToVector(out: Vec3, residueIndex: ResidueIndex) {
  142. if (this.value.isCoarseBackbone) {
  143. Vec3.set(out, 1, 0, 0);
  144. } else {
  145. this.pos(tmpVecA, this.directionFromElementIndex[residueIndex]);
  146. this.pos(tmpVecB, this.directionToElementIndex[residueIndex]);
  147. Vec3.sub(out, tmpVecB, tmpVecA);
  148. }
  149. }
  150. private setDirection(out: Vec3, v1: Vec3, v2: Vec3, v3: Vec3) {
  151. Vec3.matchDirection(tmpVecA, v1, v2);
  152. Vec3.matchDirection(tmpVecB, v3, v2);
  153. Vec3.scale(out, Vec3.add(out, tmpVecA, Vec3.add(out, tmpVecB, Vec3.add(out, v2, v2))), 1 / 4);
  154. }
  155. move() {
  156. const { residueIt, polymerIt, value } = this;
  157. if (this.state === AtomicPolymerTraceIteratorState.nextPolymer) {
  158. while (polymerIt.hasNext) {
  159. this.polymerSegment = polymerIt.move();
  160. residueIt.setSegment(this.polymerSegment);
  161. this.updateResidueSegmentRange(this.polymerSegment);
  162. if (residueIt.hasNext) {
  163. this.state = AtomicPolymerTraceIteratorState.nextResidue;
  164. const residueIndexBeg = this.residueAtomSegments.index[this.unit.elements[this.polymerSegment.start]];
  165. const residueIndexBegPrev = this.getResidueIndex(residueIndexBeg - 1);
  166. this.currSecStrucType = residueIndexBeg === residueIndexBegPrev ? SecStrucTypeNA : this.getSecStruc(residueIndexBegPrev);
  167. this.nextSecStrucType = this.getSecStruc(residueIndexBeg);
  168. this.currCoarseBackbone = this.directionFromElementIndex[residueIndexBegPrev] === -1 || this.directionToElementIndex[residueIndexBegPrev] === -1;
  169. this.nextCoarseBackbone = this.directionFromElementIndex[residueIndexBeg] === -1 || this.directionToElementIndex[residueIndexBeg] === -1;
  170. break;
  171. }
  172. }
  173. }
  174. if (this.state === AtomicPolymerTraceIteratorState.nextResidue) {
  175. const { index: residueIndex } = residueIt.move();
  176. const residueIndexPrev3 = this.getResidueIndex(residueIndex - 3);
  177. const residueIndexPrev2 = this.getResidueIndex(residueIndex - 2);
  178. const residueIndexPrev1 = this.getResidueIndex(residueIndex - 1);
  179. const residueIndexNext1 = this.getResidueIndex(residueIndex + 1);
  180. const residueIndexNext2 = this.getResidueIndex(residueIndex + 2);
  181. const residueIndexNext3 = this.getResidueIndex(residueIndex + 3);
  182. this.prevSecStrucType = this.currSecStrucType;
  183. this.currSecStrucType = this.nextSecStrucType;
  184. this.nextSecStrucType = residueIndex === residueIndexNext1 ? SecStrucTypeNA : this.getSecStruc(residueIndexNext1);
  185. this.prevCoarseBackbone = this.currCoarseBackbone;
  186. this.currCoarseBackbone = this.nextCoarseBackbone;
  187. this.nextCoarseBackbone = residueIndex === residueIndexNext1 ? false : (this.directionFromElementIndex[residueIndexNext1] === -1 || this.directionToElementIndex[residueIndexNext1] === -1);
  188. value.secStrucType = this.currSecStrucType;
  189. value.secStrucFirst = this.prevSecStrucType !== this.currSecStrucType;
  190. value.secStrucLast = this.currSecStrucType !== this.nextSecStrucType;
  191. value.isCoarseBackbone = this.currCoarseBackbone;
  192. value.coarseBackboneFirst = this.prevCoarseBackbone !== this.currCoarseBackbone;
  193. value.coarseBackboneLast = this.currCoarseBackbone !== this.nextCoarseBackbone;
  194. value.first = residueIndex === this.residueSegmentMin;
  195. value.last = residueIndex === this.residueSegmentMax;
  196. value.moleculeType = this.moleculeType[residueIndex];
  197. value.isCoarseBackbone = this.directionFromElementIndex[residueIndex] === -1 || this.directionToElementIndex[residueIndex] === -1;
  198. value.initial = residueIndex === residueIndexPrev1;
  199. value.final = residueIndex === residueIndexNext1;
  200. value.centerPrev.element = this.traceElementIndex[residueIndexPrev1];
  201. value.center.element = this.traceElementIndex[residueIndex];
  202. value.centerNext.element = this.traceElementIndex[residueIndexNext1];
  203. this.pos(this.p0, this.traceElementIndex[residueIndexPrev3]);
  204. this.pos(this.p1, this.traceElementIndex[residueIndexPrev2]);
  205. this.pos(this.p2, this.traceElementIndex[residueIndexPrev1]);
  206. this.pos(this.p3, this.traceElementIndex[residueIndex]);
  207. this.pos(this.p4, this.traceElementIndex[residueIndexNext1]);
  208. this.pos(this.p5, this.traceElementIndex[residueIndexNext2]);
  209. this.pos(this.p6, this.traceElementIndex[residueIndexNext3]);
  210. this.setFromToVector(this.d01, residueIndexPrev1);
  211. this.setFromToVector(this.d12, residueIndex);
  212. this.setFromToVector(this.d23, residueIndexNext1);
  213. this.setFromToVector(this.d34, residueIndexNext2);
  214. // extend termini
  215. const f = 0.5;
  216. if (residueIndex === residueIndexPrev1) {
  217. Vec3.scale(tmpDir, Vec3.sub(tmpDir, this.p3, this.p4), f);
  218. Vec3.add(this.p2, this.p3, tmpDir);
  219. Vec3.add(this.p1, this.p2, tmpDir);
  220. Vec3.add(this.p0, this.p1, tmpDir);
  221. } else if (residueIndexPrev1 === residueIndexPrev2) {
  222. Vec3.scale(tmpDir, Vec3.sub(tmpDir, this.p2, this.p3), f);
  223. Vec3.add(this.p1, this.p2, tmpDir);
  224. Vec3.add(this.p0, this.p1, tmpDir);
  225. } else if (residueIndexPrev2 === residueIndexPrev3) {
  226. Vec3.scale(tmpDir, Vec3.sub(tmpDir, this.p1, this.p2), f);
  227. Vec3.add(this.p0, this.p1, tmpDir);
  228. }
  229. if (residueIndex === residueIndexNext1) {
  230. Vec3.scale(tmpDir, Vec3.sub(tmpDir, this.p3, this.p2), f);
  231. Vec3.add(this.p4, this.p3, tmpDir);
  232. Vec3.add(this.p5, this.p4, tmpDir);
  233. Vec3.add(this.p6, this.p5, tmpDir);
  234. } else if (residueIndexNext1 === residueIndexNext2) {
  235. Vec3.scale(tmpDir, Vec3.sub(tmpDir, this.p4, this.p3), f);
  236. Vec3.add(this.p5, this.p4, tmpDir);
  237. Vec3.add(this.p6, this.p5, tmpDir);
  238. } else if (residueIndexNext2 === residueIndexNext3) {
  239. Vec3.scale(tmpDir, Vec3.sub(tmpDir, this.p5, this.p4), f);
  240. Vec3.add(this.p6, this.p5, tmpDir);
  241. }
  242. this.setControlPoint(value.p0, this.p0, this.p1, this.p2, residueIndexPrev2);
  243. this.setControlPoint(value.p1, this.p1, this.p2, this.p3, residueIndexPrev1);
  244. this.setControlPoint(value.p2, this.p2, this.p3, this.p4, residueIndex);
  245. this.setControlPoint(value.p3, this.p3, this.p4, this.p5, residueIndexNext1);
  246. this.setControlPoint(value.p4, this.p4, this.p5, this.p6, residueIndexNext2);
  247. this.setDirection(value.d12, this.d01, this.d12, this.d23);
  248. this.setDirection(value.d23, this.d12, this.d23, this.d34);
  249. if (!residueIt.hasNext) {
  250. this.state = AtomicPolymerTraceIteratorState.nextPolymer;
  251. }
  252. }
  253. this.hasNext = residueIt.hasNext || polymerIt.hasNext;
  254. return this.value;
  255. }
  256. constructor(private unit: Unit.Atomic, structure: Structure) {
  257. this.atomicConformation = unit.model.atomicConformation;
  258. this.residueAtomSegments = unit.model.atomicHierarchy.residueAtomSegments;
  259. this.polymerRanges = unit.model.atomicRanges.polymerRanges;
  260. this.traceElementIndex = unit.model.atomicHierarchy.derived.residue.traceElementIndex as ArrayLike<ElementIndex>; // can assume it won't be -1 for polymer residues
  261. this.directionFromElementIndex = unit.model.atomicHierarchy.derived.residue.directionFromElementIndex;
  262. this.directionToElementIndex = unit.model.atomicHierarchy.derived.residue.directionToElementIndex;
  263. this.moleculeType = unit.model.atomicHierarchy.derived.residue.moleculeType;
  264. this.cyclicPolymerMap = unit.model.atomicRanges.cyclicPolymerMap;
  265. this.polymerIt = SortedRanges.transientSegments(this.polymerRanges, unit.elements);
  266. this.residueIt = Segmentation.transientSegments(this.residueAtomSegments, unit.elements);
  267. this.value = createPolymerTraceElement(structure, unit);
  268. this.hasNext = this.residueIt.hasNext && this.polymerIt.hasNext;
  269. const secondaryStructure = SecondaryStructureProvider.get(structure).value?.get(unit.invariantId);
  270. if (!secondaryStructure) throw new Error('missing secondary structure');
  271. this.secondaryStructureType = secondaryStructure.type;
  272. this.secondaryStructureGetIndex = secondaryStructure.getIndex;
  273. }
  274. }
  275. const enum CoarsePolymerTraceIteratorState { nextPolymer, nextElement }
  276. export class CoarsePolymerTraceIterator implements Iterator<PolymerTraceElement> {
  277. private value: PolymerTraceElement
  278. private polymerIt: SortedRanges.Iterator<ElementIndex, ResidueIndex>
  279. private polymerSegment: Segmentation.Segment<ResidueIndex>
  280. private state: CoarsePolymerTraceIteratorState = CoarsePolymerTraceIteratorState.nextPolymer
  281. private conformation: CoarseSphereConformation | CoarseGaussianConformation
  282. private elementIndex: number
  283. hasNext: boolean = false;
  284. private getElementIndex(elementIndex: number) {
  285. return Math.min(Math.max(this.polymerSegment.start, elementIndex), this.polymerSegment.end - 1) as ElementIndex;
  286. }
  287. private pos(target: Vec3, elementIndex: number) {
  288. const index = this.unit.elements[elementIndex];
  289. target[0] = this.conformation.x[index];
  290. target[1] = this.conformation.y[index];
  291. target[2] = this.conformation.z[index];
  292. }
  293. move() {
  294. if (this.state === CoarsePolymerTraceIteratorState.nextPolymer) {
  295. while (this.polymerIt.hasNext) {
  296. this.polymerSegment = this.polymerIt.move();
  297. this.elementIndex = this.polymerSegment.start;
  298. if (this.elementIndex < this.polymerSegment.end) {
  299. this.state = CoarsePolymerTraceIteratorState.nextElement;
  300. break;
  301. }
  302. }
  303. }
  304. if (this.state === CoarsePolymerTraceIteratorState.nextElement) {
  305. const elementIndexPrev2 = this.getElementIndex(this.elementIndex - 2);
  306. const elementIndexPrev1 = this.getElementIndex(this.elementIndex - 1);
  307. const elementIndexNext1 = this.getElementIndex(this.elementIndex + 1);
  308. const elementIndexNext2 = this.getElementIndex(this.elementIndex + 2);
  309. this.value.centerPrev.element = this.value.center.unit.elements[elementIndexPrev1];
  310. this.value.center.element = this.value.center.unit.elements[this.elementIndex];
  311. this.value.centerNext.element = this.value.center.unit.elements[elementIndexNext1];
  312. this.pos(this.value.p0, elementIndexPrev2);
  313. this.pos(this.value.p1, elementIndexPrev1);
  314. this.pos(this.value.p2, this.elementIndex);
  315. this.pos(this.value.p3, elementIndexNext1);
  316. this.pos(this.value.p4, elementIndexNext2);
  317. // extend termini
  318. const f = 0.5;
  319. if (this.elementIndex === elementIndexPrev1) {
  320. Vec3.scale(tmpDir, Vec3.sub(tmpDir, this.value.p2, this.value.p3), f);
  321. Vec3.add(this.value.p1, this.value.p2, tmpDir);
  322. Vec3.add(this.value.p0, this.value.p1, tmpDir);
  323. } else if (elementIndexPrev1 === elementIndexPrev2) {
  324. Vec3.scale(tmpDir, Vec3.sub(tmpDir, this.value.p1, this.value.p2), f);
  325. Vec3.add(this.value.p0, this.value.p1, tmpDir);
  326. }
  327. if (this.elementIndex === elementIndexNext1) {
  328. Vec3.scale(tmpDir, Vec3.sub(tmpDir, this.value.p2, this.value.p1), f);
  329. Vec3.add(this.value.p3, this.value.p2, tmpDir);
  330. Vec3.add(this.value.p4, this.value.p3, tmpDir);
  331. } else if (elementIndexNext1 === elementIndexNext2) {
  332. Vec3.scale(tmpDir, Vec3.sub(tmpDir, this.value.p3, this.value.p2), f);
  333. Vec3.add(this.value.p4, this.value.p3, tmpDir);
  334. }
  335. this.value.first = this.elementIndex === this.polymerSegment.start;
  336. this.value.last = this.elementIndex === this.polymerSegment.end - 1;
  337. if (this.elementIndex + 1 >= this.polymerSegment.end) {
  338. this.state = CoarsePolymerTraceIteratorState.nextPolymer;
  339. }
  340. }
  341. this.hasNext = this.elementIndex + 1 < this.polymerSegment.end || this.polymerIt.hasNext;
  342. this.elementIndex += 1;
  343. return this.value;
  344. }
  345. constructor(private unit: Unit.Spheres | Unit.Gaussians, structure: Structure) {
  346. this.polymerIt = SortedRanges.transientSegments(getPolymerRanges(unit), unit.elements);
  347. this.value = createPolymerTraceElement(structure, unit);
  348. Vec3.set(this.value.d12, 1, 0, 0);
  349. Vec3.set(this.value.d23, 1, 0, 0);
  350. switch (unit.kind) {
  351. case Unit.Kind.Spheres: this.conformation = unit.model.coarseConformation.spheres; break;
  352. case Unit.Kind.Gaussians: this.conformation = unit.model.coarseConformation.gaussians; break;
  353. }
  354. this.hasNext = this.polymerIt.hasNext;
  355. }
  356. }