structure.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 * as B from 'benchmark'
  7. import * as util from 'util'
  8. import * as fs from 'fs'
  9. import CIF from '../mol-io/reader/cif'
  10. import buildModels from '../mol-data/model/builders/mmcif'
  11. import { ofModel } from '../mol-data/structure/base'
  12. import Property from '../mol-data/structure/property'
  13. import Model from '../mol-data/Model'
  14. import Structure from '../mol-data/structure'
  15. import OrdSet from '../mol-base/collections/integer/ordered-set'
  16. import AtomSet from '../mol-data/structure/atom-set'
  17. require('util.promisify').shim();
  18. const readFileAsync = util.promisify(fs.readFile);
  19. async function readData(path: string) {
  20. if (path.match(/\.bcif$/)) {
  21. const input = await readFileAsync(path)
  22. const data = new Uint8Array(input.byteLength);
  23. for (let i = 0; i < input.byteLength; i++) data[i] = input[i];
  24. return data;
  25. } else {
  26. return readFileAsync(path, 'utf8');
  27. }
  28. }
  29. export async function readCIF(path: string) {
  30. const input = await readData(path)
  31. const comp = typeof input === 'string' ? CIF.parseText(input) : CIF.parseBinary(input);
  32. const parsed = await comp();
  33. if (parsed.isError) {
  34. throw parsed;
  35. }
  36. const data = parsed.result.blocks[0];
  37. const mmcif = CIF.schema.mmCIF(data);
  38. const models = buildModels(mmcif);
  39. const structures = models.map(ofModel);
  40. return { mmcif, models, structures };
  41. }
  42. export namespace PropertyAccess {
  43. function baselineRaw(model: Model) {
  44. const atom_site = model.sourceData.data._frame.categories['_atom_site'];
  45. const id = atom_site.getField('id')!.int;
  46. let s = 0;
  47. for (let i = 0, _i = atom_site.rowCount; i < _i; i++) {
  48. s += id(i);
  49. }
  50. return s;
  51. }
  52. function baseline(model: Model) {
  53. const atom_site = model.sourceData.data.atom_site;
  54. const id = atom_site.id.value;
  55. let s = 0;
  56. for (let i = 0, _i = atom_site._rowCount; i < _i; i++) {
  57. s += id(i);
  58. }
  59. return s;
  60. }
  61. function sumProperty(structure: Structure, p: Property<number>, initial: number) {
  62. const { atoms, units } = structure;
  63. const unitIds = AtomSet.unitIds(atoms);
  64. const l = Property.createLocation(structure);
  65. let s = initial;
  66. for (let i = 0, _i = unitIds.length; i < _i; i++) {
  67. const unitId = unitIds[i];
  68. l.unit = units[unitId];
  69. const set = AtomSet.unitGetByIndex(atoms, i);
  70. const { residueIndex, chainIndex } = l.unit;
  71. for (let j = 0, _j = OrdSet.size(set); j < _j; j++) {
  72. const aI = OrdSet.getAt(set, j);
  73. l.atomIndex = aI;
  74. l.residueIndex = residueIndex[aI];
  75. l.chainIndex = chainIndex[aI];
  76. s += p(l);
  77. }
  78. }
  79. return s;
  80. }
  81. function sumDirect(structure: Structure) {
  82. const { atoms, units } = structure;
  83. const unitIds = AtomSet.unitIds(atoms);
  84. let s = 0;
  85. for (let i = 0, _i = unitIds.length; i < _i; i++) {
  86. const unitId = unitIds[i];
  87. const unit = units[unitId];
  88. const set = AtomSet.unitGetByIndex(atoms, i);
  89. //const { residueIndex, chainIndex } = unit;
  90. const p = unit.model.conformation.atomId.value;
  91. for (let j = 0, _j = OrdSet.size(set); j < _j; j++) {
  92. const aI = OrdSet.getAt(set, j);
  93. s += p(aI);
  94. }
  95. }
  96. return s;
  97. }
  98. // function concatProperty(structure: Structure, p: Property<string>) {
  99. // const { atoms, units } = structure;
  100. // const unitIds = AtomSet.unitIds(atoms);
  101. // const l = Property.createLocation(structure);
  102. // let s = [];
  103. // for (let i = 0, _i = unitIds.length; i < _i; i++) {
  104. // const unitId = unitIds[i];
  105. // l.unit = units[unitId];
  106. // const set = AtomSet.unitGetByIndex(atoms, i);
  107. // const { residueIndex, chainIndex } = l.unit;
  108. // for (let j = 0, _j = OrdSet.size(set); j < _j; j++) {
  109. // const aI = OrdSet.getAt(set, j);
  110. // l.atomIndex = aI;
  111. // l.residueIndex = residueIndex[aI];
  112. // l.chainIndex = chainIndex[aI];
  113. // s[s.length] = p(l);
  114. // }
  115. // }
  116. // return s;
  117. // }
  118. export async function run() {
  119. const { structures, models } = await readCIF('./examples/1cbs_full.bcif');
  120. console.log(baseline(models[0]));
  121. console.log(baselineRaw(models[0]));
  122. console.log(sumProperty(structures[0], l => l.unit.model.conformation.atomId.value(l.atomIndex), 0));
  123. console.log(sumProperty(structures[0], Property.cachedAtomColumn(m => m.conformation.atomId), 0));
  124. console.log(sumDirect(structures[0]));
  125. const col = models[0].conformation.atomId.value;
  126. const suite = new B.Suite();
  127. suite
  128. .add('baseline raw', () => baselineRaw(models[0]))
  129. .add('baseline', () => baseline(models[0]))
  130. .add('direct', () => sumDirect(structures[0]))
  131. .add('normal int', () => sumProperty(structures[0], l => l.unit.model.conformation.atomId.value(l.atomIndex), 0))
  132. .add('test int', () => sumProperty(structures[0], l => col(l.atomIndex), 0))
  133. .add('cached int', () => sumProperty(structures[0], Property.cachedAtomColumn(m => m.conformation.atomId), 0))
  134. //.add('concat str', () => concatProperty(structures[0], l => l.unit.model.hierarchy.atoms.auth_atom_id.value(l.atomIndex)))
  135. //.add('cached concat str', () => concatProperty(structures[0], Property.cachedAtomColumn(m => m.hierarchy.atoms.auth_atom_id)))
  136. .on('cycle', (e: any) => console.log(String(e.target)))
  137. .run();
  138. }
  139. }
  140. PropertyAccess.run();