labeling.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Loci } from '../../mol-model/loci';
  2. import { StructureElement } from '../../mol-model/structure';
  3. import { LociLabel, LociLabelProvider } from '../../mol-plugin-state/manager/loci-label';
  4. import { TmDetChainListCache, TmDetDescriptorCache } from './prop';
  5. import { createResidueListsPerChain, getChainAndResidueIds } from './tmdet-color-theme';
  6. import { ChainList, getResidue, ResidueItem } from './types';
  7. const siteLabels = [
  8. "Side1",
  9. "Side2",
  10. "TM alpha",
  11. "TM beta",
  12. "TM re-entrant loop",
  13. "Interfacial Helix",
  14. "Unknown localization",
  15. "Membrane Inside"
  16. ];
  17. const DefaultResidueLabel = 6; // Unknown localization
  18. export const TmDetLabelProvider: LociLabelProvider = {
  19. label: (loci: Loci): LociLabel => {
  20. let labelText = siteLabels[DefaultResidueLabel];
  21. if (loci.kind == 'element-loci') {
  22. const unit = loci.elements[0].unit;
  23. const pdbId = unit.model.entryId;
  24. const descriptor = TmDetDescriptorCache.get(pdbId);
  25. if (!descriptor) {
  26. return labelText;
  27. }
  28. let chainList = TmDetChainListCache.get(pdbId);
  29. if (!chainList) {
  30. chainList = createResidueListsPerChain(descriptor.chains);
  31. TmDetChainListCache.set(pdbId, chainList);
  32. }
  33. const location = StructureElement.Loci.getFirstLocation(loci);
  34. const { chainId, residueId } = getChainAndResidueIds(location!);
  35. const residue = getResidue(chainList, chainId!, residueId!);
  36. if (residue) {
  37. labelText = siteLabels[residue?.siteId];
  38. let regionText = getRegionText(chainList, chainId!, residue);
  39. labelText = `${labelText}: ${regionText}`
  40. }
  41. }
  42. return labelText;
  43. }
  44. }
  45. function getRegionText(chainList: ChainList, chainId: string, residue: ResidueItem): string {
  46. let value = "Unknown region range";
  47. const chain = chainList.filter((chain) => chain.chainId === chainId)[0];
  48. if (chain) {
  49. // find start of region
  50. const residues = chain.residues;
  51. const authId = parseInt(residue.authId!)
  52. let previous = residues[residues.length-1];
  53. for (let i = residues.length-1; i > 0; i--) {
  54. const current = residues[i];
  55. const currentId = parseInt(current.authId!);
  56. // cancel loop when siteId changes
  57. if (currentId < authId && current.siteId != residue.siteId) {
  58. break;
  59. }
  60. previous = current;
  61. }
  62. value = `[ ${previous.authId}`;
  63. // find end of region
  64. previous = residues[0];
  65. for (let current of residues) {
  66. const currentId = parseInt(current.authId!);
  67. // cancel loop when siteId changes
  68. if (authId < currentId && current.siteId !== residue.siteId) {
  69. break;
  70. }
  71. previous = current;
  72. }
  73. value = `${value}-${previous.authId} ]`;
  74. }
  75. return value;
  76. }