pdb.ts 2.4 KB

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