label.ts 3.2 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, Link } 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 avoid 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. switch (loci.kind) {
  19. case 'element-loci':
  20. const e = loci.elements[0]
  21. if (e) {
  22. const el = e.unit.elements[OrderedSet.getAt(e.indices, 0)];
  23. return elementLabel(StructureElement.create(e.unit, el))
  24. } else {
  25. return 'Unknown'
  26. }
  27. case 'link-loci':
  28. const link = loci.links[0]
  29. return link ? linkLabel(link) : 'Unknown'
  30. case 'group-loci':
  31. const g = loci.groups[0]
  32. if (g) {
  33. return g.shape.labels.ref.value[OrderedSet.getAt(g.ids, 0)]
  34. } else {
  35. return 'Unknown'
  36. }
  37. case 'every-loci':
  38. return 'Everything'
  39. case 'empty-loci':
  40. return 'Nothing'
  41. }
  42. }
  43. export function linkLabel(link: Link.Location) {
  44. if (!elementLocA) elementLocA = StructureElement.create()
  45. if (!elementLocB) elementLocB = StructureElement.create()
  46. setElementLocation(elementLocA, link.aUnit, link.aIndex)
  47. setElementLocation(elementLocB, link.bUnit, link.bIndex)
  48. return `${elementLabel(elementLocA)} - ${elementLabel(elementLocB)}`
  49. }
  50. export function elementLabel(location: StructureElement) {
  51. const model = location.unit.model.label
  52. const instance = location.unit.conformation.operator.name
  53. let label = ''
  54. if (Unit.isAtomic(location.unit)) {
  55. const asym_id = Props.chain.auth_asym_id(location)
  56. const seq_id = Props.residue.auth_seq_id(location)
  57. const comp_id = Props.residue.auth_comp_id(location)
  58. const atom_id = Props.atom.auth_atom_id(location)
  59. const alt_id = Props.atom.label_alt_id(location)
  60. label = `[${comp_id}]${seq_id}:${asym_id}.${atom_id}${alt_id ? `%${alt_id}` : ''}`
  61. } else if (Unit.isCoarse(location.unit)) {
  62. const asym_id = Props.coarse.asym_id(location)
  63. const seq_id_begin = Props.coarse.seq_id_begin(location)
  64. const seq_id_end = Props.coarse.seq_id_end(location)
  65. if (seq_id_begin === seq_id_end) {
  66. const entityIndex = Props.coarse.entityKey(location)
  67. const seq = location.unit.model.sequence.byEntityKey[entityIndex]
  68. const comp_id = seq.compId.value(seq_id_begin - 1) // 1-indexed
  69. label = `[${comp_id}]${seq_id_begin}:${asym_id}`
  70. } else {
  71. label = `${seq_id_begin}-${seq_id_end}:${asym_id}`
  72. }
  73. } else {
  74. label = 'unknown'
  75. }
  76. return `${model} ${instance} ${label}`
  77. }