chem_comp.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Copyright (c) 2020 Mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. * @author Sebastian Bittrich <sebastian.bittrich@rcsb.org>
  7. */
  8. import { Model } from '../../../../mol-model/structure/model/model';
  9. import { CustomPropertyDescriptor } from '../../../../mol-model/custom-property';
  10. import { CifWriter } from '../../../../mol-io/writer/cif';
  11. import { Table } from '../../../../mol-data/db';
  12. import { FormatPropertyProvider } from '../../common/property';
  13. import { CCD_Schema } from '../../../../mol-io/reader/cif/schema/ccd';
  14. export interface ComponentAtom {
  15. readonly data: Table<CCD_Schema['chem_comp_atom']>
  16. readonly entries: ReadonlyMap<string, ComponentAtom.Entry>
  17. }
  18. export namespace ComponentAtom {
  19. export const Descriptor: CustomPropertyDescriptor = {
  20. name: 'chem_comp_atom',
  21. cifExport: {
  22. prefix: '',
  23. categories: [{
  24. name: 'chem_comp_atom',
  25. instance(ctx) {
  26. const p = Provider.get(ctx.firstModel);
  27. if (!p) return CifWriter.Category.Empty;
  28. const chem_comp_atom = p.data;
  29. if (!chem_comp_atom) return CifWriter.Category.Empty;
  30. const comp_names = ctx.structures[0].uniqueResidueNames;
  31. const { comp_id, _rowCount } = chem_comp_atom;
  32. const indices: number[] = [];
  33. for (let i = 0; i < _rowCount; i++) {
  34. if (comp_names.has(comp_id.value(i))) indices[indices.length] = i;
  35. }
  36. return CifWriter.Category.ofTable(chem_comp_atom, indices);
  37. }
  38. }]
  39. }
  40. };
  41. export const Provider = FormatPropertyProvider.create<ComponentAtom>(Descriptor);
  42. export function chemCompAtomFromTable(model: Model, table: Table<CCD_Schema['chem_comp_atom']>): Table<CCD_Schema['chem_comp_atom']> {
  43. return Table.pick(table, CCD_Schema.chem_comp_atom, (i: number) => {
  44. return model.properties.chemicalComponentMap.has(table.comp_id.value(i));
  45. });
  46. }
  47. export function getEntriesFromChemCompAtom(data: Table<CCD_Schema['chem_comp_atom']>) {
  48. const entries: Map<string, Entry> = new Map();
  49. function addEntry(id: string) {
  50. let e = new Entry(id);
  51. entries.set(id, e);
  52. return e;
  53. }
  54. const { comp_id, atom_id, charge, pdbx_stereo_config, _rowCount } = data;
  55. let entry = addEntry(comp_id.value(0)!);
  56. for (let i = 0; i < _rowCount; i++) {
  57. const name = atom_id.value(i)!;
  58. const id = comp_id.value(i)!;
  59. const ch = charge.value(i)!;
  60. const stereo = pdbx_stereo_config.value(i)!;
  61. if (entry.id !== id) {
  62. entry = addEntry(id);
  63. }
  64. entry.add(name, ch, stereo);
  65. }
  66. return entries;
  67. }
  68. export class Entry {
  69. readonly map: Map<string, { charge: number, stereo_config: CCD_Schema['chem_comp_atom']['pdbx_stereo_config']['T'] }> = new Map();
  70. add(a: string, charge: number, stereo_config: CCD_Schema['chem_comp_atom']['pdbx_stereo_config']['T']) {
  71. this.map.set(a, { charge, stereo_config });
  72. }
  73. constructor(public readonly id: string) { }
  74. }
  75. }