util.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Michal Malý <michal.maly@ibt.cas.cz>
  5. * @author Jiří Černý <jiri.cerny@ibt.cas.cz>
  6. */
  7. import { ConfalPyramidsProvider } from './property';
  8. import { DnatcoTypes } from '../types';
  9. import { DnatcoUtil } from '../util';
  10. import { Segmentation } from '../../../mol-data/int';
  11. import { ChainIndex, ElementIndex, ResidueIndex, Structure, StructureElement, Unit } from '../../../mol-model/structure';
  12. export type Pyramid = {
  13. O3: ElementIndex,
  14. P: ElementIndex,
  15. OP1: ElementIndex,
  16. OP2: ElementIndex,
  17. O5: ElementIndex,
  18. confalScore: number,
  19. stepIdx: number,
  20. };
  21. function getPyramid(
  22. loc: StructureElement.Location,
  23. one: DnatcoUtil.Residue, two: DnatcoUtil.Residue,
  24. altIdOne: string, altIdTwo: string,
  25. insCodeOne: string, insCodeTwo: string,
  26. confalScore: number, stepIdx: number): Pyramid {
  27. const O3 = DnatcoUtil.getAtomIndex(loc, one, ['O3\'', 'O3*'], altIdOne, insCodeOne);
  28. const P = DnatcoUtil.getAtomIndex(loc, two, ['P'], altIdTwo, insCodeTwo);
  29. const OP1 = DnatcoUtil.getAtomIndex(loc, two, ['OP1'], altIdTwo, insCodeTwo);
  30. const OP2 = DnatcoUtil.getAtomIndex(loc, two, ['OP2'], altIdTwo, insCodeTwo);
  31. const O5 = DnatcoUtil.getAtomIndex(loc, two, ['O5\'', 'O5*'], altIdTwo, insCodeTwo);
  32. return { O3, P, OP1, OP2, O5, confalScore, stepIdx };
  33. }
  34. export class ConfalPyramidsIterator {
  35. private chainIt: Segmentation.SegmentIterator<ChainIndex>;
  36. private residueIt: Segmentation.SegmentIterator<ResidueIndex>;
  37. private residueOne?: DnatcoUtil.Residue;
  38. private residueTwo: DnatcoUtil.Residue;
  39. private data?: DnatcoTypes.Steps;
  40. private loc: StructureElement.Location;
  41. private moveStep() {
  42. this.residueOne = DnatcoUtil.copyResidue(this.residueTwo);
  43. this.residueTwo = DnatcoUtil.copyResidue(this.residueIt.move())!;
  44. return this.toPyramids(this.residueOne!, this.residueTwo);
  45. }
  46. private toPyramids(one: DnatcoUtil.Residue, two: DnatcoUtil.Residue) {
  47. const indices = DnatcoUtil.getStepIndices(this.data!, this.loc, one);
  48. const points = [];
  49. for (const idx of indices) {
  50. const step = this.data!.steps[idx];
  51. points.push(getPyramid(this.loc, one, two, step.label_alt_id_1, step.label_alt_id_2, step.PDB_ins_code_1, step.PDB_ins_code_2, step.confal_score, idx));
  52. }
  53. return points;
  54. }
  55. constructor(structure: Structure, unit: Unit) {
  56. this.chainIt = Segmentation.transientSegments(unit.model.atomicHierarchy.chainAtomSegments, unit.elements);
  57. this.residueIt = Segmentation.transientSegments(unit.model.atomicHierarchy.residueAtomSegments, unit.elements);
  58. const prop = ConfalPyramidsProvider.get(unit.model).value;
  59. this.data = prop?.data;
  60. if (this.chainIt.hasNext) {
  61. this.residueIt.setSegment(this.chainIt.move());
  62. if (this.residueIt.hasNext)
  63. this.residueTwo = this.residueIt.move();
  64. }
  65. this.loc = StructureElement.Location.create(structure, unit, -1 as ElementIndex);
  66. }
  67. get hasNext() {
  68. if (!this.data)
  69. return false;
  70. return this.residueIt.hasNext
  71. ? true
  72. : this.chainIt.hasNext;
  73. }
  74. move() {
  75. if (this.residueIt.hasNext) {
  76. return this.moveStep();
  77. } else {
  78. this.residueIt.setSegment(this.chainIt.move());
  79. if (this.residueIt.hasNext)
  80. this.residueTwo = this.residueIt.move();
  81. return this.moveStep();
  82. }
  83. }
  84. }