unit.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { SymmetryOperator } from '../../../mol-math/geometry/symmetry-operator'
  8. import { Model } from '../model'
  9. import { GridLookup3D, Lookup3D } from '../../../mol-math/geometry'
  10. import { IntraUnitLinks, computeIntraUnitBonds } from './unit/links'
  11. import { CoarseElements, CoarseSphereConformation, CoarseGaussianConformation } from '../model/properties/coarse';
  12. import { ValueRef } from '../../../mol-util';
  13. import { UnitRings } from './unit/rings';
  14. import StructureElement from './element'
  15. import { ChainIndex, ResidueIndex, ElementIndex } from '../model/indexing';
  16. import { IntMap, SortedArray, Segmentation } from '../../../mol-data/int';
  17. import { hash2, hashFnv32a } from '../../../mol-data/util';
  18. import { getAtomicPolymerElements, getCoarsePolymerElements, getAtomicGapElements, getCoarseGapElements, getNucleotideElements, getProteinElements } from './util/polymer';
  19. import { mmCIF_Schema } from '../../../mol-io/reader/cif/schema/mmcif';
  20. /**
  21. * A building block of a structure that corresponds to an atomic or
  22. * a coarse grained representation 'conveniently grouped together'.
  23. */
  24. type Unit = Unit.Atomic | Unit.Spheres | Unit.Gaussians
  25. namespace Unit {
  26. export const enum Kind { Atomic, Spheres, Gaussians }
  27. export function isAtomic(u: Unit): u is Atomic { return u.kind === Kind.Atomic; }
  28. export function isCoarse(u: Unit): u is Spheres | Gaussians { return u.kind === Kind.Spheres || u.kind === Kind.Gaussians; }
  29. export function isSpheres(u: Unit): u is Spheres { return u.kind === Kind.Spheres; }
  30. export function isGaussians(u: Unit): u is Gaussians { return u.kind === Kind.Gaussians; }
  31. export function create(id: number, invariantId: number, chainGroupId: number, kind: Kind, model: Model, operator: SymmetryOperator, elements: StructureElement.Set): Unit {
  32. switch (kind) {
  33. case Kind.Atomic: return new Atomic(id, invariantId, chainGroupId, model, elements, SymmetryOperator.createMapping(operator, model.atomicConformation, void 0), AtomicProperties());
  34. case Kind.Spheres: return createCoarse(id, invariantId, chainGroupId, model, Kind.Spheres, elements, SymmetryOperator.createMapping(operator, model.coarseConformation.spheres, getSphereRadiusFunc(model)), CoarseProperties());
  35. case Kind.Gaussians: return createCoarse(id, invariantId, chainGroupId, model, Kind.Gaussians, elements, SymmetryOperator.createMapping(operator, model.coarseConformation.gaussians, getGaussianRadiusFunc(model)), CoarseProperties());
  36. }
  37. }
  38. /** A group of units that differ only by symmetry operators. */
  39. export type SymmetryGroup = {
  40. readonly elements: StructureElement.Set
  41. readonly units: ReadonlyArray<Unit>
  42. /** Maps unit.id to index of unit in units array */
  43. readonly unitIndexMap: IntMap<number>
  44. /** Hash based on unit.invariantId which is the same for all units in the group */
  45. readonly hashCode: number
  46. /** Hash based on all unit.id values in the group, reflecting the units transformation*/
  47. readonly transformHash: number
  48. }
  49. function getUnitIndexMap(units: Unit[]) {
  50. const unitIndexMap = IntMap.Mutable<number>();
  51. for (let i = 0, _i = units.length; i < _i; i++) {
  52. unitIndexMap.set(units[i].id, i);
  53. }
  54. return unitIndexMap
  55. }
  56. export function SymmetryGroup(units: Unit[]) {
  57. const props: {
  58. unitIndexMap?: IntMap<number>
  59. } = {}
  60. return {
  61. elements: units[0].elements,
  62. units,
  63. get unitIndexMap () {
  64. if (props.unitIndexMap) return props.unitIndexMap
  65. props.unitIndexMap = getUnitIndexMap(units)
  66. return props.unitIndexMap
  67. },
  68. hashCode: hashUnit(units[0]),
  69. transformHash: hashFnv32a(units.map(u => u.id))
  70. }
  71. }
  72. export namespace SymmetryGroup {
  73. export function areInvariantElementsEqual(a: SymmetryGroup, b: SymmetryGroup) {
  74. if (a.hashCode !== b.hashCode) return false;
  75. return SortedArray.areEqual(a.elements, b.elements);
  76. }
  77. }
  78. export function conformationId (unit: Unit) {
  79. return Unit.isAtomic(unit) ? unit.model.atomicConformation.id : unit.model.coarseConformation.id
  80. }
  81. export function hashUnit(u: Unit) {
  82. return hash2(u.invariantId, SortedArray.hashCode(u.elements));
  83. }
  84. export interface Base {
  85. readonly id: number,
  86. /** invariant ID stays the same even if the Operator/conformation changes. */
  87. readonly invariantId: number,
  88. readonly chainGroupId: number,
  89. readonly elements: StructureElement.Set,
  90. readonly model: Model,
  91. readonly conformation: SymmetryOperator.ArrayMapping<ElementIndex>,
  92. getChild(elements: StructureElement.Set): Unit,
  93. applyOperator(id: number, operator: SymmetryOperator, dontCompose?: boolean /* = false */): Unit,
  94. readonly lookup3d: Lookup3D
  95. readonly polymerElements: SortedArray<ElementIndex>
  96. readonly gapElements: SortedArray<ElementIndex>
  97. /**
  98. * From mmCIF/IHM schema: `_ihm_model_representation_details.model_object_primitive`.
  99. */
  100. readonly objectPrimitive: mmCIF_Schema['ihm_model_representation_details']['model_object_primitive']['T']
  101. }
  102. function getSphereRadiusFunc(model: Model) {
  103. const r = model.coarseConformation.spheres.radius;
  104. return (i: number) => r[i];
  105. }
  106. function getGaussianRadiusFunc(model: Model) {
  107. // TODO: compute radius for gaussians
  108. return (i: number) => 0;
  109. }
  110. /**
  111. * A bulding block of a structure that corresponds
  112. * to a "natural group of atoms" (most often a "chain")
  113. * together with a tranformation (rotation and translation)
  114. * that is dynamically applied to the underlying atom set.
  115. *
  116. * An atom set can be referenced by multiple different units which
  117. * makes construction of assemblies and spacegroups very efficient.
  118. */
  119. export class Atomic implements Base {
  120. readonly kind = Kind.Atomic;
  121. readonly objectPrimitive = 'atomistic';
  122. readonly id: number;
  123. readonly invariantId: number;
  124. /** Used to identify a single chain split into multiple units. */
  125. readonly chainGroupId: number;
  126. readonly elements: StructureElement.Set;
  127. readonly model: Model;
  128. readonly conformation: SymmetryOperator.ArrayMapping<ElementIndex>;
  129. /** Reference `residueIndex` from `model` for faster access. */
  130. readonly residueIndex: ArrayLike<ResidueIndex>;
  131. /** Reference `chainIndex` from `model` for faster access. */
  132. readonly chainIndex: ArrayLike<ChainIndex>;
  133. private props: AtomicProperties;
  134. getChild(elements: StructureElement.Set): Unit {
  135. if (elements.length === this.elements.length) return this;
  136. return new Atomic(this.id, this.invariantId, this.chainGroupId, this.model, elements, this.conformation, AtomicProperties());
  137. }
  138. applyOperator(id: number, operator: SymmetryOperator, dontCompose = false): Unit {
  139. const op = dontCompose ? operator : SymmetryOperator.compose(this.conformation.operator, operator);
  140. return new Atomic(id, this.invariantId, this.chainGroupId, this.model, this.elements, SymmetryOperator.createMapping(op, this.model.atomicConformation, this.conformation.r), this.props);
  141. }
  142. get lookup3d() {
  143. if (this.props.lookup3d.ref) return this.props.lookup3d.ref;
  144. const { x, y, z } = this.model.atomicConformation;
  145. this.props.lookup3d.ref = GridLookup3D({ x, y, z, indices: this.elements });
  146. return this.props.lookup3d.ref;
  147. }
  148. get links() {
  149. if (this.props.links.ref) return this.props.links.ref;
  150. this.props.links.ref = computeIntraUnitBonds(this);
  151. return this.props.links.ref;
  152. }
  153. get rings() {
  154. if (this.props.rings.ref) return this.props.rings.ref;
  155. this.props.rings.ref = UnitRings.create(this);
  156. return this.props.rings.ref;
  157. }
  158. get polymerElements() {
  159. if (this.props.polymerElements.ref) return this.props.polymerElements.ref;
  160. this.props.polymerElements.ref = getAtomicPolymerElements(this);
  161. return this.props.polymerElements.ref;
  162. }
  163. get gapElements() {
  164. if (this.props.gapElements.ref) return this.props.gapElements.ref;
  165. this.props.gapElements.ref = getAtomicGapElements(this);
  166. return this.props.gapElements.ref;
  167. }
  168. get nucleotideElements() {
  169. if (this.props.nucleotideElements.ref) return this.props.nucleotideElements.ref;
  170. this.props.nucleotideElements.ref = getNucleotideElements(this);
  171. return this.props.nucleotideElements.ref;
  172. }
  173. get proteinElements() {
  174. if (this.props.proteinElements.ref) return this.props.proteinElements.ref;
  175. this.props.proteinElements.ref = getProteinElements(this);
  176. return this.props.proteinElements.ref;
  177. }
  178. get residueCount(): number {
  179. if (this.props.residueCount.ref !== undefined) return this.props.residueCount.ref;
  180. let residueCount = 0
  181. const residueIt = Segmentation.transientSegments(this.model.atomicHierarchy.residueAtomSegments, this.elements)
  182. while (residueIt.hasNext) {
  183. residueIt.move()
  184. residueCount += 1
  185. }
  186. this.props.residueCount.ref = residueCount;
  187. return this.props.residueCount.ref!;
  188. }
  189. getResidueIndex(elementIndex: StructureElement.UnitIndex) {
  190. return this.model.atomicHierarchy.residueAtomSegments.index[this.elements[elementIndex]];
  191. }
  192. constructor(id: number, invariantId: number, chainGroupId: number, model: Model, elements: StructureElement.Set, conformation: SymmetryOperator.ArrayMapping<ElementIndex>, props: AtomicProperties) {
  193. this.id = id;
  194. this.invariantId = invariantId;
  195. this.chainGroupId = chainGroupId;
  196. this.model = model;
  197. this.elements = elements;
  198. this.conformation = conformation;
  199. this.residueIndex = model.atomicHierarchy.residueAtomSegments.index;
  200. this.chainIndex = model.atomicHierarchy.chainAtomSegments.index;
  201. this.props = props;
  202. }
  203. }
  204. interface AtomicProperties {
  205. lookup3d: ValueRef<Lookup3D | undefined>,
  206. links: ValueRef<IntraUnitLinks | undefined>,
  207. rings: ValueRef<UnitRings | undefined>
  208. polymerElements: ValueRef<SortedArray<ElementIndex> | undefined>
  209. gapElements: ValueRef<SortedArray<ElementIndex> | undefined>
  210. nucleotideElements: ValueRef<SortedArray<ElementIndex> | undefined>
  211. proteinElements: ValueRef<SortedArray<ElementIndex> | undefined>
  212. residueCount: ValueRef<number | undefined>
  213. }
  214. function AtomicProperties(): AtomicProperties {
  215. return {
  216. lookup3d: ValueRef.create(void 0),
  217. links: ValueRef.create(void 0),
  218. rings: ValueRef.create(void 0),
  219. polymerElements: ValueRef.create(void 0),
  220. gapElements: ValueRef.create(void 0),
  221. nucleotideElements: ValueRef.create(void 0),
  222. proteinElements: ValueRef.create(void 0),
  223. residueCount: ValueRef.create(void 0),
  224. };
  225. }
  226. class Coarse<K extends Kind.Gaussians | Kind.Spheres, C extends CoarseSphereConformation | CoarseGaussianConformation> implements Base {
  227. readonly kind: K;
  228. readonly objectPrimitive: 'sphere' | 'gaussian';
  229. readonly id: number;
  230. readonly invariantId: number;
  231. readonly chainGroupId: number;
  232. readonly elements: StructureElement.Set;
  233. readonly model: Model;
  234. readonly conformation: SymmetryOperator.ArrayMapping<ElementIndex>;
  235. readonly coarseElements: CoarseElements;
  236. readonly coarseConformation: C;
  237. private props: CoarseProperties;
  238. getChild(elements: StructureElement.Set): Unit {
  239. if (elements.length === this.elements.length) return this as any as Unit /** lets call this an ugly temporary hack */;
  240. return createCoarse(this.id, this.invariantId, this.chainGroupId, this.model, this.kind, elements, this.conformation, CoarseProperties());
  241. }
  242. applyOperator(id: number, operator: SymmetryOperator, dontCompose = false): Unit {
  243. const op = dontCompose ? operator : SymmetryOperator.compose(this.conformation.operator, operator);
  244. const ret = createCoarse(id, this.invariantId, this.chainGroupId, this.model, this.kind, this.elements, SymmetryOperator.createMapping(op, this.getCoarseConformation(), this.conformation.r), this.props);
  245. // (ret as Coarse<K, C>)._lookup3d = this._lookup3d;
  246. return ret;
  247. }
  248. get lookup3d() {
  249. if (this.props.lookup3d.ref) return this.props.lookup3d.ref;
  250. // TODO: support sphere radius?
  251. const { x, y, z } = this.getCoarseConformation();
  252. this.props.lookup3d.ref = GridLookup3D({ x, y, z, indices: this.elements });
  253. return this.props.lookup3d.ref;
  254. }
  255. get polymerElements() {
  256. if (this.props.polymerElements.ref) return this.props.polymerElements.ref;
  257. this.props.polymerElements.ref = getCoarsePolymerElements(this as Unit.Spheres | Unit.Gaussians); // TODO get rid of casting
  258. return this.props.polymerElements.ref;
  259. }
  260. get gapElements() {
  261. if (this.props.gapElements.ref) return this.props.gapElements.ref;
  262. this.props.gapElements.ref = getCoarseGapElements(this as Unit.Spheres | Unit.Gaussians); // TODO get rid of casting
  263. return this.props.gapElements.ref;
  264. }
  265. private getCoarseConformation() {
  266. return this.kind === Kind.Spheres ? this.model.coarseConformation.spheres : this.model.coarseConformation.gaussians;
  267. }
  268. constructor(id: number, invariantId: number, chainGroupId: number, model: Model, kind: K, elements: StructureElement.Set, conformation: SymmetryOperator.ArrayMapping<ElementIndex>, props: CoarseProperties) {
  269. this.kind = kind;
  270. this.objectPrimitive = kind === Kind.Spheres ? 'sphere' : 'gaussian'
  271. this.id = id;
  272. this.invariantId = invariantId;
  273. this.chainGroupId = chainGroupId;
  274. this.model = model;
  275. this.elements = elements;
  276. this.conformation = conformation;
  277. this.coarseElements = kind === Kind.Spheres ? model.coarseHierarchy.spheres : model.coarseHierarchy.gaussians;
  278. this.coarseConformation = (kind === Kind.Spheres ? model.coarseConformation.spheres : model.coarseConformation.gaussians) as C;
  279. this.props = props;
  280. }
  281. }
  282. interface CoarseProperties {
  283. lookup3d: ValueRef<Lookup3D | undefined>,
  284. polymerElements: ValueRef<SortedArray<ElementIndex> | undefined>
  285. gapElements: ValueRef<SortedArray<ElementIndex> | undefined>
  286. }
  287. function CoarseProperties(): CoarseProperties {
  288. return {
  289. lookup3d: ValueRef.create(void 0),
  290. polymerElements: ValueRef.create(void 0),
  291. gapElements: ValueRef.create(void 0),
  292. };
  293. }
  294. function createCoarse<K extends Kind.Gaussians | Kind.Spheres>(id: number, invariantId: number, chainGroupId: number, model: Model, kind: K, elements: StructureElement.Set, conformation: SymmetryOperator.ArrayMapping<ElementIndex>, props: CoarseProperties): Unit {
  295. return new Coarse(id, invariantId, chainGroupId, model, kind, elements, conformation, props) as any as Unit /** lets call this an ugly temporary hack */;
  296. }
  297. export class Spheres extends Coarse<Kind.Spheres, CoarseSphereConformation> { }
  298. export class Gaussians extends Coarse<Kind.Gaussians, CoarseGaussianConformation> { }
  299. }
  300. export default Unit;