pdb.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Copyright (c) 2019-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. */
  7. import { PdbFile } from '../../mol-io/reader/pdb/schema';
  8. import { pdbToMmCif } from './pdb/to-cif';
  9. import { Task } from '../../mol-task';
  10. import { MmcifFormat } from './mmcif';
  11. import { createModels } from './basic/parser';
  12. import { Column } from '../../mol-data/db';
  13. import { AtomPartialCharge } from './property/partial-charge';
  14. import { Trajectory } from '../../mol-model/structure';
  15. import { ModelFormat } from '../format';
  16. export { PdbFormat };
  17. type PdbFormat = ModelFormat<PdbFile>
  18. namespace PdbFormat {
  19. export function is(x?: ModelFormat): x is PdbFormat {
  20. return x?.kind === 'pdb';
  21. }
  22. export function create(pdb: PdbFile): PdbFormat {
  23. return { kind: 'pdb', name: pdb.id || '', data: pdb };
  24. }
  25. }
  26. export function trajectoryFromPDB(pdb: PdbFile): Task<Trajectory> {
  27. return Task.create('Parse PDB', async ctx => {
  28. await ctx.update('Converting to mmCIF');
  29. const cif = await pdbToMmCif(pdb);
  30. const format = MmcifFormat.fromFrame(cif, undefined, PdbFormat.create(pdb));
  31. const models = await createModels(format.data.db, format, ctx);
  32. const partial_charge = cif.categories['atom_site']?.getField('partial_charge');
  33. if (partial_charge) {
  34. // TODO works only for single, unsorted model, to work generally
  35. // would need to do model splitting again
  36. if (models.frameCount === 1) {
  37. const first = models.representative;
  38. const srcIndex = first.atomicHierarchy.atomSourceIndex;
  39. const isIdentity = Column.isIdentity(srcIndex);
  40. const srcIndexArray = isIdentity ? void 0 : srcIndex.toArray({ array: Int32Array });
  41. const q = partial_charge.toFloatArray();
  42. const partialCharge = srcIndexArray
  43. ? Column.ofFloatArray(Column.mapToArray(srcIndex, i => q[i], Float32Array))
  44. : Column.ofFloatArray(q);
  45. AtomPartialCharge.Provider.set(first, {
  46. data: partialCharge,
  47. type: 'GASTEIGER' // from PDBQT
  48. });
  49. }
  50. }
  51. return models;
  52. });
  53. }