label.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { Unit, StructureElement, StructureProperties as Props } from 'mol-model/structure';
  8. import { Loci } from 'mol-model/loci';
  9. import { OrderedSet } from 'mol-data/int';
  10. // for `labelFirst`, don't create right away to avaiod problems with circular dependencies/imports
  11. let elementLocA: StructureElement
  12. let elementLocB: StructureElement
  13. function setElementLocation(loc: StructureElement, unit: Unit, index: StructureElement.UnitIndex) {
  14. loc.unit = unit
  15. loc.element = unit.elements[index]
  16. }
  17. export function labelFirst(loci: Loci): string {
  18. if (!elementLocA) elementLocA = StructureElement.create()
  19. if (!elementLocB) elementLocB = StructureElement.create()
  20. switch (loci.kind) {
  21. case 'element-loci':
  22. const e = loci.elements[0]
  23. if (e) {
  24. const el = e.unit.elements[OrderedSet.getAt(e.indices, 0)];
  25. return elementLabel(StructureElement.create(e.unit, el))
  26. } else {
  27. return 'Unknown'
  28. }
  29. case 'link-loci':
  30. const bond = loci.links[0]
  31. if (bond) {
  32. setElementLocation(elementLocA, bond.aUnit, bond.aIndex)
  33. setElementLocation(elementLocB, bond.bUnit, bond.bIndex)
  34. return `${elementLabel(elementLocA)} - ${elementLabel(elementLocB)}`
  35. } else {
  36. return 'Unknown'
  37. }
  38. case 'group-loci':
  39. const g = loci.groups[0]
  40. if (g) {
  41. return g.shape.labels.ref.value[OrderedSet.getAt(g.ids, 0)]
  42. } else {
  43. return 'Unknown'
  44. }
  45. case 'every-loci':
  46. return 'Everything'
  47. case 'empty-loci':
  48. return 'Nothing'
  49. }
  50. }
  51. export function elementLabel(loc: StructureElement) {
  52. const model = loc.unit.model.label
  53. const instance = loc.unit.conformation.operator.name
  54. let element = ''
  55. if (Unit.isAtomic(loc.unit)) {
  56. const asym_id = Props.chain.auth_asym_id(loc)
  57. const seq_id = Props.residue.auth_seq_id(loc)
  58. const comp_id = Props.residue.auth_comp_id(loc)
  59. const atom_id = Props.atom.auth_atom_id(loc)
  60. element = `[${comp_id}]${seq_id}:${asym_id}.${atom_id}`
  61. } else if (Unit.isCoarse(loc.unit)) {
  62. const asym_id = Props.coarse.asym_id(loc)
  63. const seq_id_begin = Props.coarse.seq_id_begin(loc)
  64. const seq_id_end = Props.coarse.seq_id_end(loc)
  65. if (seq_id_begin === seq_id_end) {
  66. const entityKey = Props.coarse.entityKey(loc)
  67. const seq = loc.unit.model.sequence.byEntityKey[entityKey]
  68. const comp_id = seq.compId.value(seq_id_begin - 1) // 1-indexed
  69. element = `[${comp_id}]${seq_id_begin}:${asym_id}`
  70. } else {
  71. element = `${seq_id_begin}-${seq_id_end}:${asym_id}`
  72. }
  73. } else {
  74. element = 'unknown'
  75. }
  76. return `${model} ${instance} ${element}`
  77. }