nucleotide.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Copyright (c) 2018 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 } from 'mol-model/structure';
  7. import { LocationIterator } from '../../../../util/location-iterator';
  8. import { getNucleotideElements } from 'mol-model/structure/structure/util/nucleotide';
  9. import { PickingId } from '../../../../geometry/picking';
  10. import { Loci, EmptyLoci } from 'mol-model/loci';
  11. import { OrderedSet, Interval } from 'mol-data/int';
  12. export namespace NucleotideLocationIterator {
  13. export function fromGroup(group: Unit.SymmetryGroup): LocationIterator {
  14. const u = group.units[0]
  15. const nucleotideElementIndices = Unit.isAtomic(u) ? getNucleotideElements(u) : []
  16. const groupCount = nucleotideElementIndices.length
  17. const instanceCount = group.units.length
  18. const location = StructureElement.create()
  19. const getLocation = (groupIndex: number, instanceIndex: number) => {
  20. const unit = group.units[instanceIndex]
  21. location.unit = unit
  22. location.element = nucleotideElementIndices[groupIndex]
  23. return location
  24. }
  25. return LocationIterator(groupCount, instanceCount, getLocation)
  26. }
  27. }
  28. export function getNucleotideElementLoci(pickingId: PickingId, group: Unit.SymmetryGroup, id: number) {
  29. const { objectId, instanceId, groupId } = pickingId
  30. if (id === objectId) {
  31. const unit = group.units[instanceId]
  32. if (Unit.isAtomic(unit)) {
  33. const unitIndex = OrderedSet.indexOf(unit.elements, unit.nucleotideElements[groupId]) as StructureElement.UnitIndex
  34. if (unitIndex !== -1) {
  35. const indices = OrderedSet.ofSingleton(unitIndex)
  36. return StructureElement.Loci([{ unit, indices }])
  37. }
  38. }
  39. }
  40. return EmptyLoci
  41. }
  42. export function markNucleotideElement(loci: Loci, group: Unit.SymmetryGroup, apply: (interval: Interval) => boolean) {
  43. let changed = false
  44. const u = group.units[0]
  45. if (StructureElement.isLoci(loci) && Unit.isAtomic(u)) {
  46. const groupCount = u.nucleotideElements.length
  47. for (const e of loci.elements) {
  48. const unitIdx = group.unitIndexMap.get(e.unit.id)
  49. if (unitIdx !== undefined && Unit.isAtomic(e.unit)) {
  50. if (Interval.is(e.indices)) {
  51. const min = OrderedSet.indexOf(e.unit.nucleotideElements, e.unit.elements[Interval.min(e.indices)])
  52. const max = OrderedSet.indexOf(e.unit.nucleotideElements, e.unit.elements[Interval.max(e.indices)])
  53. if (min !== -1 && max !== -1) {
  54. if (apply(Interval.ofRange(unitIdx * groupCount + min, unitIdx * groupCount + max))) changed = true
  55. }
  56. } else {
  57. for (let i = 0, _i = e.indices.length; i < _i; i++) {
  58. const idx = OrderedSet.indexOf(e.unit.nucleotideElements, e.unit.elements[e.indices[i]])
  59. if (idx !== -1) {
  60. if (apply(Interval.ofSingleton(unitIdx * groupCount + idx))) changed = true
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. return changed
  68. }