label.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, Element, StructureProperties as Props } from 'mol-model/structure';
  8. import { Loci } from 'mol-model/loci';
  9. import { OrderedSet } from 'mol-data/int';
  10. const elementLocA = Element.Location()
  11. const elementLocB = Element.Location()
  12. function setElementLocation(loc: Element.Location, unit: Unit, index: number) {
  13. loc.unit = unit
  14. loc.element = unit.elements[index]
  15. }
  16. export function labelFirst(loci: Loci): string {
  17. switch (loci.kind) {
  18. case 'element-loci':
  19. const e = loci.elements[0]
  20. if (e) {
  21. const el = e.unit.elements[OrderedSet.getAt(e.indices, 0)];
  22. return elementLabel(Element.Location(e.unit, el))
  23. } else {
  24. return 'Unknown'
  25. }
  26. case 'link-loci':
  27. const bond = loci.links[0]
  28. if (bond) {
  29. setElementLocation(elementLocA, bond.aUnit, bond.aIndex)
  30. setElementLocation(elementLocB, bond.bUnit, bond.bIndex)
  31. return `${elementLabel(elementLocA)} - ${elementLabel(elementLocB)}`
  32. } else {
  33. return 'Unknown'
  34. }
  35. case 'every-loci':
  36. return 'Everything'
  37. case 'empty-loci':
  38. return 'Nothing'
  39. }
  40. }
  41. export function elementLabel(loc: Element.Location) {
  42. const model = loc.unit.model.label
  43. const instance = loc.unit.conformation.operator.name
  44. let element = ''
  45. if (Unit.isAtomic(loc.unit)) {
  46. const asym_id = Props.chain.auth_asym_id(loc)
  47. const seq_id = Props.residue.auth_seq_id(loc)
  48. const comp_id = Props.residue.auth_comp_id(loc)
  49. const atom_id = Props.atom.auth_atom_id(loc)
  50. element = `[${comp_id}]${seq_id}:${asym_id}.${atom_id}`
  51. } else if (Unit.isCoarse(loc.unit)) {
  52. const asym_id = Props.coarse.asym_id(loc)
  53. const seq_id_begin = Props.coarse.seq_id_begin(loc)
  54. const seq_id_end = Props.coarse.seq_id_end(loc)
  55. if (seq_id_begin === seq_id_end) {
  56. const entityKey = Props.coarse.entityKey(loc)
  57. const seq = loc.unit.model.sequence.byEntityKey[entityKey]
  58. const comp_id = seq.compId.value(seq_id_begin - 1) // 1-indexed
  59. element = `[${comp_id}]${seq_id_begin}:${asym_id}`
  60. } else {
  61. element = `${seq_id_begin}-${seq_id_end}:${asym_id}`
  62. }
  63. } else {
  64. element = 'unknown'
  65. }
  66. return `${model} ${instance} ${element}`
  67. }