polymer.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { StructureSelection, StructureQuery, Structure, Queries, StructureProperties as SP, StructureElement, Unit } from '../../../mol-model/structure';
  7. import { SequenceWrapper } from './util';
  8. import { OrderedSet, Interval } from '../../../mol-data/int';
  9. import { Loci } from '../../../mol-model/loci';
  10. import { Sequence } from '../../../mol-model/sequence';
  11. import { Color } from '../../../mol-util/color';
  12. export type StructureUnit = { structure: Structure, unit: Unit }
  13. export class PolymerSequenceWrapper extends SequenceWrapper<StructureUnit> {
  14. private readonly location: StructureElement
  15. private readonly sequence: Sequence
  16. seqId(i: number) {
  17. return this.sequence.offset + i + 1
  18. }
  19. residueLabel(i: number) {
  20. return this.sequence.sequence[i]
  21. }
  22. residueColor(i: number) {
  23. return Color(0)
  24. }
  25. eachResidue(loci: Loci, apply: (interval: Interval) => boolean) {
  26. let changed = false
  27. const { structure, unit } = this.data
  28. if (!StructureElement.isLoci(loci)) return false
  29. if (!Structure.areParentsEquivalent(loci.structure, structure)) return false
  30. const { location } = this
  31. for (const e of loci.elements) {
  32. let rIprev = -1
  33. location.unit = e.unit
  34. const { index: residueIndex } = e.unit.model.atomicHierarchy.residueAtomSegments
  35. OrderedSet.forEach(e.indices, v => {
  36. location.element = e.unit.elements[v]
  37. const rI = residueIndex[location.element]
  38. // avoid checking for the same residue multiple times
  39. if (rI !== rIprev) {
  40. if (SP.unit.id(location) !== unit.id) return
  41. if (apply(getSeqIdInterval(location))) changed = true
  42. rIprev = rI
  43. }
  44. })
  45. }
  46. return changed
  47. }
  48. getLoci(seqId: number) {
  49. const query = createResidueQuery(this.data.unit.id, seqId);
  50. return StructureSelection.toLoci2(StructureQuery.run(query, this.data.structure));
  51. }
  52. constructor(data: StructureUnit) {
  53. const l = StructureElement.create(data.unit, data.unit.elements[0])
  54. const sequence = data.unit.model.sequence.byEntityKey[SP.entity.key(l)].sequence
  55. const markerArray = new Uint8Array(sequence.sequence.length)
  56. super(data, markerArray, sequence.sequence.length)
  57. this.sequence = sequence
  58. this.location = StructureElement.create()
  59. }
  60. }
  61. function createResidueQuery(unitId: number, label_seq_id: number) {
  62. return Queries.generators.atoms({
  63. unitTest: ctx => {
  64. return SP.unit.id(ctx.element) === unitId
  65. },
  66. residueTest: ctx => {
  67. if (ctx.element.unit.kind === Unit.Kind.Atomic) {
  68. return SP.residue.label_seq_id(ctx.element) === label_seq_id
  69. } else {
  70. return (
  71. SP.coarse.seq_id_begin(ctx.element) <= label_seq_id &&
  72. SP.coarse.seq_id_end(ctx.element) >= label_seq_id
  73. )
  74. }
  75. }
  76. });
  77. }
  78. /** Zero-indexed */
  79. function getSeqIdInterval(location: StructureElement): Interval {
  80. const { unit, element } = location
  81. const { model } = unit
  82. switch (unit.kind) {
  83. case Unit.Kind.Atomic:
  84. const residueIndex = model.atomicHierarchy.residueAtomSegments.index[element]
  85. const seqId = model.atomicHierarchy.residues.label_seq_id.value(residueIndex)
  86. return Interval.ofSingleton(seqId - 1)
  87. case Unit.Kind.Spheres:
  88. return Interval.ofRange(
  89. model.coarseHierarchy.spheres.seq_id_begin.value(element) - 1,
  90. model.coarseHierarchy.spheres.seq_id_end.value(element) - 1
  91. )
  92. case Unit.Kind.Gaussians:
  93. return Interval.ofRange(
  94. model.coarseHierarchy.gaussians.seq_id_begin.value(element) - 1,
  95. model.coarseHierarchy.gaussians.seq_id_end.value(element) - 1
  96. )
  97. }
  98. }