loci.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 { StructureElement } from './structure'
  7. import { Link } from './structure/structure/unit/links'
  8. import { Shape, ShapeGroup } from './shape';
  9. import { Sphere3D } from '../mol-math/geometry';
  10. import { CentroidHelper } from '../mol-math/geometry/centroid-helper';
  11. import { Vec3 } from '../mol-math/linear-algebra';
  12. import { OrderedSet } from '../mol-data/int';
  13. import { Structure } from './structure/structure';
  14. import { capitalize } from '../mol-util/string';
  15. /** A Loci that includes every loci */
  16. export const EveryLoci = { kind: 'every-loci' as 'every-loci' }
  17. export type EveryLoci = typeof EveryLoci
  18. export function isEveryLoci(x?: Loci): x is EveryLoci {
  19. return !!x && x.kind === 'every-loci';
  20. }
  21. /** A Loci that is empty */
  22. export const EmptyLoci = { kind: 'empty-loci' as 'empty-loci' }
  23. export type EmptyLoci = typeof EmptyLoci
  24. export function isEmptyLoci(x?: Loci): x is EmptyLoci {
  25. return !!x && x.kind === 'empty-loci';
  26. }
  27. /** A generic data loci */
  28. export interface DataLoci {
  29. readonly kind: 'data-loci',
  30. readonly data: any,
  31. readonly tag: string
  32. readonly indices: OrderedSet<number>
  33. }
  34. export function isDataLoci(x?: Loci): x is DataLoci {
  35. return !!x && x.kind === 'data-loci';
  36. }
  37. export function areDataLociEqual(a: DataLoci, b: DataLoci) {
  38. return a.data === b.data && a.tag === b.tag && OrderedSet.areEqual(a.indices, b.indices)
  39. }
  40. export function isDataLociEmpty(loci: DataLoci) {
  41. return OrderedSet.size(loci.indices) === 0 ? true : false
  42. }
  43. export function createDataLoci(data: any, tag: string, indices: OrderedSet<number>): DataLoci {
  44. return { kind: 'data-loci', data, tag, indices }
  45. }
  46. export { Loci }
  47. type Loci = StructureElement.Loci | Structure.Loci | Link.Loci | EveryLoci | EmptyLoci | DataLoci | Shape.Loci | ShapeGroup.Loci
  48. namespace Loci {
  49. export function areEqual(lociA: Loci, lociB: Loci) {
  50. if (isEveryLoci(lociA) && isEveryLoci(lociB)) return true
  51. if (isEmptyLoci(lociA) && isEmptyLoci(lociB)) return true
  52. if (isDataLoci(lociA) && isDataLoci(lociB)) {
  53. return areDataLociEqual(lociA, lociB)
  54. }
  55. if (Structure.isLoci(lociA) && Structure.isLoci(lociB)) {
  56. return Structure.areLociEqual(lociA, lociB)
  57. }
  58. if (StructureElement.Loci.is(lociA) && StructureElement.Loci.is(lociB)) {
  59. return StructureElement.Loci.areEqual(lociA, lociB)
  60. }
  61. if (Link.isLoci(lociA) && Link.isLoci(lociB)) {
  62. return Link.areLociEqual(lociA, lociB)
  63. }
  64. if (Shape.isLoci(lociA) && Shape.isLoci(lociB)) {
  65. return Shape.areLociEqual(lociA, lociB)
  66. }
  67. if (ShapeGroup.isLoci(lociA) && ShapeGroup.isLoci(lociB)) {
  68. return ShapeGroup.areLociEqual(lociA, lociB)
  69. }
  70. return false
  71. }
  72. export function isEvery(loci?: Loci): loci is EveryLoci {
  73. return !!loci && loci.kind === 'every-loci';
  74. }
  75. export function isEmpty(loci: Loci): loci is EmptyLoci {
  76. if (isEveryLoci(loci)) return false
  77. if (isEmptyLoci(loci)) return true
  78. if (isDataLoci(loci)) return isDataLociEmpty(loci)
  79. if (Structure.isLoci(loci)) return Structure.isLociEmpty(loci)
  80. if (StructureElement.Loci.is(loci)) return StructureElement.Loci.isEmpty(loci)
  81. if (Link.isLoci(loci)) return Link.isLociEmpty(loci)
  82. if (Shape.isLoci(loci)) return Shape.isLociEmpty(loci)
  83. if (ShapeGroup.isLoci(loci)) return ShapeGroup.isLociEmpty(loci)
  84. return false
  85. }
  86. export function remap<T>(loci: Loci, data: T) {
  87. if (data instanceof Structure) {
  88. if (StructureElement.Loci.is(loci)) {
  89. loci = StructureElement.Loci.remap(loci, data)
  90. } else if (Structure.isLoci(loci)) {
  91. loci = Structure.remapLoci(loci, data)
  92. } else if (Link.isLoci(loci)) {
  93. loci = Link.remapLoci(loci, data)
  94. }
  95. }
  96. return loci
  97. }
  98. const sphereHelper = new CentroidHelper(), tempPos = Vec3.zero();
  99. export function getBoundingSphere(loci: Loci, boundingSphere?: Sphere3D): Sphere3D | undefined {
  100. if (loci.kind === 'every-loci' || loci.kind === 'empty-loci') return void 0;
  101. if (!boundingSphere) boundingSphere = Sphere3D()
  102. sphereHelper.reset();
  103. if (loci.kind === 'structure-loci') {
  104. return Sphere3D.copy(boundingSphere, loci.structure.boundary.sphere)
  105. } else if (loci.kind === 'element-loci') {
  106. return StructureElement.Loci.getBoundary(loci).sphere;
  107. } else if (loci.kind === 'link-loci') {
  108. for (const e of loci.links) {
  109. e.aUnit.conformation.position(e.aUnit.elements[e.aIndex], tempPos);
  110. sphereHelper.includeStep(tempPos);
  111. e.bUnit.conformation.position(e.bUnit.elements[e.bIndex], tempPos);
  112. sphereHelper.includeStep(tempPos);
  113. }
  114. sphereHelper.finishedIncludeStep();
  115. for (const e of loci.links) {
  116. e.aUnit.conformation.position(e.aUnit.elements[e.aIndex], tempPos);
  117. sphereHelper.radiusStep(tempPos);
  118. e.aUnit.conformation.position(e.bUnit.elements[e.bIndex], tempPos);
  119. sphereHelper.radiusStep(tempPos);
  120. }
  121. } else if (loci.kind === 'shape-loci') {
  122. // TODO
  123. return void 0;
  124. } else if (loci.kind === 'group-loci') {
  125. // TODO
  126. return void 0;
  127. } else if (loci.kind === 'data-loci') {
  128. // TODO maybe add loci.getBoundingSphere()???
  129. return void 0;
  130. }
  131. Vec3.copy(boundingSphere.center, sphereHelper.center)
  132. boundingSphere.radius = Math.sqrt(sphereHelper.radiusSq)
  133. return boundingSphere
  134. }
  135. const tmpSphere3D = Sphere3D.zero()
  136. export function getCenter(loci: Loci, center?: Vec3): Vec3 | undefined {
  137. const boundingSphere = getBoundingSphere(loci, tmpSphere3D)
  138. return boundingSphere ? Vec3.copy(center || Vec3.zero(), boundingSphere.center) : undefined
  139. }
  140. //
  141. const Granularity = {
  142. 'element': (loci: Loci) => loci,
  143. 'residue': (loci: Loci) => {
  144. return StructureElement.Loci.is(loci)
  145. ? StructureElement.Loci.extendToWholeResidues(loci, true)
  146. : loci
  147. },
  148. 'chain': (loci: Loci) => {
  149. return StructureElement.Loci.is(loci)
  150. ? StructureElement.Loci.extendToWholeChains(loci)
  151. : loci
  152. },
  153. 'structure': (loci: Loci) => {
  154. return StructureElement.Loci.is(loci)
  155. ? Structure.toStructureElementLoci(loci.structure)
  156. : loci
  157. }
  158. }
  159. export type Granularity = keyof typeof Granularity
  160. export const GranularityOptions = Object.keys(Granularity).map(n => [n, capitalize(n)]) as [Granularity, string][]
  161. export function applyGranularity(loci: Loci, granularity: Granularity) {
  162. return Granularity[granularity](loci)
  163. }
  164. /**
  165. * Converts structure related loci to StructureElement.Loci and applies
  166. * granularity if given
  167. */
  168. export function normalize(loci: Loci, granularity?: Granularity) {
  169. if (granularity !== 'element' && Link.isLoci(loci)) {
  170. // convert Link.Loci to a StructureElement.Loci so granularity can be applied
  171. loci = Link.toStructureElementLoci(loci)
  172. }
  173. if (Structure.isLoci(loci)) {
  174. // convert to StructureElement.Loci
  175. loci = Structure.toStructureElementLoci(loci.structure)
  176. }
  177. if (StructureElement.Loci.is(loci)) {
  178. // ensure the root structure is used
  179. loci = StructureElement.Loci.remap(loci, loci.structure.root)
  180. }
  181. if (granularity) {
  182. // needs to be applied AFTER remapping to root
  183. loci = applyGranularity(loci, granularity)
  184. }
  185. return loci
  186. }
  187. }