mmcif.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { RawData } from '../formats'
  7. import { Frame as mmCIF } from '../../../mol-io/reader/cif/schema/mmcif'
  8. import Model from '../../model'
  9. //import Column from '../../../mol-base/collections/column'
  10. import Interval from '../../../mol-base/collections/integer/interval'
  11. import Segmentation from '../../../mol-base/collections/integer/segmentation'
  12. import uuId from '../../../mol-base/utils/uuid'
  13. function findModelBounds(data: mmCIF, startIndex: number) {
  14. const num = data.atom_site.pdbx_PDB_model_num;
  15. const atomCount = num.rowCount;
  16. if (!num.isDefined) return Interval.ofBounds(startIndex, atomCount);
  17. let endIndex = startIndex + 1;
  18. while (endIndex < atomCount && num.areValuesEqual(startIndex, endIndex)) endIndex++;
  19. return Interval.ofBounds(startIndex, endIndex);
  20. }
  21. function segmentOffsets(data: mmCIF, bounds: Interval) {
  22. const start = Interval.start(bounds), end = Interval.end(bounds);
  23. const residues = [start], chains = [start];
  24. const { label_entity_id, auth_asym_id, auth_seq_id, pdbx_PDB_ins_code, label_comp_id } = data.atom_site;
  25. for (let i = start + 1; i < end; i++) {
  26. const newChain = !label_entity_id.areValuesEqual(i - 1, i) || !auth_asym_id.areValuesEqual(i - 1, i);
  27. const newResidue = newChain
  28. || !auth_seq_id.areValuesEqual(i - 1, i)
  29. || !pdbx_PDB_ins_code.areValuesEqual(i - 1, i)
  30. || !label_comp_id.areValuesEqual(i - 1, i);
  31. if (newResidue) residues[residues.length] = i;
  32. if (newChain) chains[chains.length] = i;
  33. }
  34. return { residues, chains };
  35. }
  36. function createModel(raw: RawData, data: mmCIF, bounds: Interval): Model {
  37. const segments = segmentOffsets(data, bounds);
  38. return {
  39. id: uuId(),
  40. sourceData: raw,
  41. model_num: 0, // TODO: fix
  42. //common: 0 as any,
  43. macromolecule: 0 as any,
  44. conformation: 0 as any,
  45. version: { data: 0, conformation: 0 },
  46. atomCount: Interval.size(bounds),
  47. segments: {
  48. residues: Segmentation.ofOffsets(segments.residues, bounds),
  49. chains: Segmentation.ofOffsets(segments.chains, bounds),
  50. }
  51. };
  52. }
  53. function buildModels(data: mmCIF): ArrayLike<Model> {
  54. const raw: RawData = { source: 'mmCIF', data };
  55. const models: Model[] = [];
  56. const atomCount = data.atom_site._rowCount;
  57. if (atomCount === 0) return models;
  58. let modelStart = 0;
  59. while (modelStart < atomCount) {
  60. const bounds = findModelBounds(data, modelStart);
  61. const model = createModel(raw, data, bounds);
  62. models.push(model);
  63. modelStart = Interval.end(bounds);
  64. }
  65. return models;
  66. }
  67. export default buildModels;