mmcif.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 mmCIF from '../../../mol-io/reader/cif/schema/mmcif'
  8. import Model from '../../model'
  9. import Interval from '../../../mol-base/collections/integer/interval'
  10. import Segmentation from '../../../mol-base/collections/integer/segmentation'
  11. import uuId from '../../../mol-base/utils/uuid'
  12. function findModelBounds(data: mmCIF, startIndex: number) {
  13. const num = data.atom_site.pdbx_PDB_model_num;
  14. const atomCount = num.rowCount;
  15. if (!num.isDefined) return Interval.ofBounds(startIndex, atomCount);
  16. let endIndex = startIndex + 1;
  17. while (endIndex < atomCount && num.areValuesEqual(startIndex, endIndex)) endIndex++;
  18. return Interval.ofBounds(startIndex, endIndex);
  19. }
  20. function segment(data: mmCIF, bounds: Interval) {
  21. const start = Interval.start(bounds), end = Interval.end(bounds);
  22. const residues = [0], chains = [0], entities = [0];
  23. const { label_entity_id, auth_asym_id, auth_seq_id, pdbx_PDB_ins_code, label_comp_id } = data.atom_site;
  24. let offset = 1;
  25. for (let i = start + 1; i < end; i++) {
  26. const newEntity = !label_entity_id.areValuesEqual(i - 1, i);
  27. const newChain = newEntity || !auth_asym_id.areValuesEqual(i - 1, i);
  28. const newResidue = newChain
  29. || !auth_seq_id.areValuesEqual(i - 1, i)
  30. || !pdbx_PDB_ins_code.areValuesEqual(i - 1, i)
  31. || !label_comp_id.areValuesEqual(i - 1, i);
  32. if (newEntity) entities[entities.length] = offset;
  33. if (newResidue) residues[residues.length] = offset;
  34. if (newChain) chains[chains.length] = offset;
  35. offset++;
  36. }
  37. residues[residues.length] = offset;
  38. chains[chains.length] = offset;
  39. entities[entities.length] = offset;
  40. return {
  41. residues: Segmentation.create(residues),
  42. chains: Segmentation.create(chains),
  43. entities: Segmentation.create(entities)
  44. };
  45. }
  46. function createModel(raw: RawData, data: mmCIF, bounds: Interval): Model {
  47. const segments = segment(data, bounds);
  48. return {
  49. id: uuId(),
  50. sourceData: raw,
  51. common: 0 as any,
  52. macromolecule: 0 as any,
  53. conformation: 0 as any,
  54. version: { data: 0, conformation: 0 },
  55. atomCount: Interval.size(bounds),
  56. segments
  57. };
  58. }
  59. function buildModels(data: mmCIF): ArrayLike<Model> {
  60. const raw: RawData = { source: 'mmCIF', data };
  61. const models: Model[] = [];
  62. const atomCount = data.atom_site._rowCount;
  63. if (atomCount === 0) return models;
  64. let modelStart = 0;
  65. while (modelStart < atomCount) {
  66. const bounds = findModelBounds(data, modelStart);
  67. const model = createModel(raw, data, bounds);
  68. models.push(model);
  69. modelStart = Interval.end(bounds);
  70. }
  71. return models;
  72. }
  73. export default buildModels;