label.ts 2.6 KB

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