polymer.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. export type StructureUnit = { structure: Structure, unit: Unit }
  11. export class PolymerSequenceWrapper extends SequenceWrapper<StructureUnit> {
  12. private readonly location = StructureElement.create()
  13. private entityId: string
  14. private label_asym_id: string
  15. eachResidue(loci: Loci, apply: (interval: Interval) => boolean) {
  16. let changed = false
  17. const { structure, unit } = this.data
  18. if (!StructureElement.isLoci(loci)) return false
  19. if (!Structure.areParentsEquivalent(loci.structure, structure)) return false
  20. const { location, label_asym_id } = this
  21. for (const e of loci.elements) {
  22. let rIprev = -1
  23. location.unit = e.unit
  24. const { index: residueIndex } = e.unit.model.atomicHierarchy.residueAtomSegments
  25. OrderedSet.forEach(e.indices, v => {
  26. location.element = e.unit.elements[v]
  27. const rI = residueIndex[location.element]
  28. // avoid checking for the same residue multiple times
  29. if (rI !== rIprev) {
  30. if (SP.unit.id(location) !== unit.id) return
  31. if (SP.chain.label_asym_id(location) !== label_asym_id) return
  32. if (apply(getSeqIdInterval(location))) changed = true
  33. rIprev = rI
  34. }
  35. })
  36. }
  37. return changed
  38. }
  39. getLoci(seqId: number) {
  40. const query = createResidueQuery(this.data.unit.id, seqId);
  41. return StructureSelection.toLoci2(StructureQuery.run(query, this.data.structure));
  42. }
  43. constructor(readonly data: StructureUnit) {
  44. super()
  45. const l = this.location
  46. l.unit = data.unit
  47. l.element = data.unit.elements[0]
  48. this.entityId = SP.entity.id(l)
  49. this.label_asym_id = SP.chain.label_asym_id(l)
  50. this.label = `${this.label_asym_id}|${this.entityId}`
  51. this.sequence = data.unit.model.sequence.byEntityKey[SP.entity.key(l)].sequence
  52. this.markerArray = new Uint8Array(this.sequence.sequence.length)
  53. }
  54. }
  55. function createResidueQuery(unitId: number, label_seq_id: number) {
  56. return Queries.generators.atoms({
  57. unitTest: ctx => {
  58. return SP.unit.id(ctx.element) === unitId
  59. },
  60. residueTest: ctx => {
  61. if (ctx.element.unit.kind === Unit.Kind.Atomic) {
  62. return SP.residue.label_seq_id(ctx.element) === label_seq_id
  63. } else {
  64. return (
  65. SP.coarse.seq_id_begin(ctx.element) <= label_seq_id &&
  66. SP.coarse.seq_id_end(ctx.element) >= label_seq_id
  67. )
  68. }
  69. }
  70. });
  71. }
  72. /** Zero-indexed */
  73. function getSeqIdInterval(location: StructureElement): Interval {
  74. const { unit, element } = location
  75. const { model } = unit
  76. switch (unit.kind) {
  77. case Unit.Kind.Atomic:
  78. const residueIndex = model.atomicHierarchy.residueAtomSegments.index[element]
  79. const seqId = model.atomicHierarchy.residues.label_seq_id.value(residueIndex)
  80. return Interval.ofSingleton(seqId - 1)
  81. case Unit.Kind.Spheres:
  82. return Interval.ofRange(
  83. model.coarseHierarchy.spheres.seq_id_begin.value(element) - 1,
  84. model.coarseHierarchy.spheres.seq_id_end.value(element) - 1
  85. )
  86. case Unit.Kind.Gaussians:
  87. return Interval.ofRange(
  88. model.coarseHierarchy.gaussians.seq_id_begin.value(element) - 1,
  89. model.coarseHierarchy.gaussians.seq_id_end.value(element) - 1
  90. )
  91. }
  92. }