structure.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 Model from '../mol-data/Model'
  11. import { Structure, Atom, AtomSet } from '../mol-data/structure'
  12. import * as Q from '../mol-data/query'
  13. import { OrderedSet as OrdSet, Segmentation } from '../mol-base/collections/integer'
  14. require('util.promisify').shim();
  15. const readFileAsync = util.promisify(fs.readFile);
  16. async function readData(path: string) {
  17. if (path.match(/\.bcif$/)) {
  18. const input = await readFileAsync(path)
  19. const data = new Uint8Array(input.byteLength);
  20. for (let i = 0; i < input.byteLength; i++) data[i] = input[i];
  21. return data;
  22. } else {
  23. return readFileAsync(path, 'utf8');
  24. }
  25. }
  26. export async function readCIF(path: string) {
  27. console.time('readData');
  28. const input = await readData(path)
  29. console.timeEnd('readData');
  30. console.time('parse');
  31. const comp = typeof input === 'string' ? CIF.parseText(input) : CIF.parseBinary(input);
  32. const parsed = await comp();
  33. console.timeEnd('parse');
  34. if (parsed.isError) {
  35. throw parsed;
  36. }
  37. const data = parsed.result.blocks[0];
  38. console.time('schema')
  39. const mmcif = CIF.schema.mmCIF(data);
  40. console.timeEnd('schema')
  41. console.time('buildModels')
  42. const models = Model.ofMmCIF(mmcif);
  43. console.timeEnd('buildModels')
  44. const structures = models.map(Structure.ofModel);
  45. return { mmcif, models, structures };
  46. }
  47. export namespace PropertyAccess {
  48. function baselineRaw(model: Model) {
  49. const atom_site = model.sourceData.data._frame.categories['_atom_site'];
  50. const id = atom_site.getField('id')!.int;
  51. let s = 0;
  52. for (let i = 0, _i = atom_site.rowCount; i < _i; i++) {
  53. s += id(i);
  54. }
  55. return s;
  56. }
  57. function baseline(model: Model) {
  58. const atom_site = model.sourceData.data.atom_site;
  59. const id = atom_site.id.value;
  60. let s = 0;
  61. for (let i = 0, _i = atom_site._rowCount; i < _i; i++) {
  62. s += id(i);
  63. }
  64. return s;
  65. }
  66. function sumProperty(structure: Structure, p: Atom.Property<number>) {
  67. const { atoms, units } = structure;
  68. const unitIds = AtomSet.unitIds(atoms);
  69. const l = Atom.Location();
  70. let s = 0;
  71. for (let i = 0, _i = unitIds.length; i < _i; i++) {
  72. l.unit = units[unitIds[i]];
  73. const set = AtomSet.unitGetByIndex(atoms, i);
  74. for (let j = 0, _j = OrdSet.size(set); j < _j; j++) {
  75. l.atom = OrdSet.getAt(set, j);
  76. s += p(l);
  77. }
  78. }
  79. return s;
  80. }
  81. function sumPropertySegmented(structure: Structure, p: Atom.Property<number>) {
  82. const { atoms, units } = structure;
  83. const unitIds = AtomSet.unitIds(atoms);
  84. const l = Atom.Location();
  85. let s = 0;
  86. for (let i = 0, _i = unitIds.length; i < _i; i++) {
  87. const unit = units[unitIds[i]];
  88. l.unit = unit;
  89. const set = AtomSet.unitGetByIndex(atoms, i);
  90. const chainsIt = Segmentation.transientSegments(unit.hierarchy.chainSegments, set);
  91. const residues = unit.hierarchy.residueSegments;
  92. while (chainsIt.hasNext) {
  93. const chainSegment = chainsIt.move();
  94. const residuesIt = Segmentation.transientSegments(residues, set, chainSegment);
  95. while (residuesIt.hasNext) {
  96. const residueSegment = residuesIt.move();
  97. for (let j = residueSegment.start, _j = residueSegment.end; j < _j; j++) {
  98. l.atom = OrdSet.getAt(set, j);
  99. s += p(l);
  100. }
  101. }
  102. }
  103. }
  104. return s;
  105. }
  106. function sumPropertyResidue(structure: Structure, p: Atom.Property<number>) {
  107. const { atoms, units } = structure;
  108. const unitIds = AtomSet.unitIds(atoms);
  109. const l = Atom.Location();
  110. let s = 0;
  111. for (let i = 0, _i = unitIds.length; i < _i; i++) {
  112. const unit = units[unitIds[i]];
  113. l.unit = unit;
  114. const set = AtomSet.unitGetByIndex(atoms, i);
  115. const residuesIt = Segmentation.transientSegments(unit.hierarchy.residueSegments, set);
  116. while (residuesIt.hasNext) {
  117. l.atom = OrdSet.getAt(set, residuesIt.move().start);
  118. s += p(l);
  119. }
  120. }
  121. return s;
  122. }
  123. function sumPropertyAtomSetIt(structure: Structure, p: Atom.Property<number>) {
  124. const { atoms, units } = structure;
  125. let s = 0;
  126. const atomsIt = AtomSet.atoms(atoms);
  127. const l = Atom.Location();
  128. while (atomsIt.hasNext) {
  129. const a = atomsIt.move();
  130. l.unit = units[Atom.unit(a)];
  131. l.atom = Atom.index(a);
  132. s += p(l);
  133. }
  134. return s;
  135. }
  136. // function sumPropertySegmentedMutable(structure: Structure, p: Property<number>) {
  137. // const { atoms, units } = structure;
  138. // const unitIds = AtomSet.unitIds(atoms);
  139. // const l = Property.createLocation();
  140. // let s = 0;
  141. // for (let i = 0, _i = unitIds.length; i < _i; i++) {
  142. // const unit = units[unitIds[i]];
  143. // l.unit = unit;
  144. // const set = AtomSet.unitGetByIndex(atoms, i);
  145. // const chainsIt = Segmentation.transientSegments(unit.hierarchy.chainSegments, set);
  146. // const residuesIt = Segmentation.transientSegments(unit.hierarchy.residueSegments, set);
  147. // while (chainsIt.hasNext) {
  148. // const chainSegment = chainsIt.move();
  149. // residuesIt.updateRange(chainSegment);
  150. // while (residuesIt.hasNext) {
  151. // const residueSegment = residuesIt.move();
  152. // for (let j = residueSegment.start, _j = residueSegment.end; j < _j; j++) {
  153. // l.atom = OrdSet.getAt(set, j);
  154. // s += p(l);
  155. // }
  156. // }
  157. // }
  158. // }
  159. // return s;
  160. // }
  161. function sumDirect(structure: Structure) {
  162. const { atoms, units } = structure;
  163. const unitIds = AtomSet.unitIds(atoms);
  164. let s = 0;
  165. for (let i = 0, _i = unitIds.length; i < _i; i++) {
  166. const unitId = unitIds[i];
  167. const unit = units[unitId];
  168. const set = AtomSet.unitGetByIndex(atoms, i);
  169. //const { residueIndex, chainIndex } = unit;
  170. const p = unit.conformation.atomId.value;
  171. for (let j = 0, _j = OrdSet.size(set); j < _j; j++) {
  172. const aI = OrdSet.getAt(set, j);
  173. s += p(aI);
  174. }
  175. }
  176. return s;
  177. }
  178. // function concatProperty(structure: Structure, p: Property<string>) {
  179. // const { atoms, units } = structure;
  180. // const unitIds = AtomSet.unitIds(atoms);
  181. // const l = Property.createLocation(structure);
  182. // let s = [];
  183. // for (let i = 0, _i = unitIds.length; i < _i; i++) {
  184. // const unitId = unitIds[i];
  185. // l.unit = units[unitId];
  186. // const set = AtomSet.unitGetByIndex(atoms, i);
  187. // const { residueIndex, chainIndex } = l.unit;
  188. // for (let j = 0, _j = OrdSet.size(set); j < _j; j++) {
  189. // const aI = OrdSet.getAt(set, j);
  190. // l.atom = aI;
  191. // l.residueIndex = residueIndex[aI];
  192. // l.chainIndex = chainIndex[aI];
  193. // s[s.length] = p(l);
  194. // }
  195. // }
  196. // return s;
  197. // }
  198. export async function run() {
  199. const { structures, models } = await readCIF('./examples/1cbs_full.bcif');
  200. //const { structures, models } = await readCIF('e:/test/quick/1jj2_full.bcif');
  201. //const { structures, models } = await readCIF('e:/test/quick/3j3q_updated.cif');
  202. console.log('parsed');
  203. console.log(baseline(models[0]));
  204. console.log(baselineRaw(models[0]));
  205. console.log(sumProperty(structures[0], l => l.unit.model.conformation.atomId.value(l.atom)));
  206. console.log(sumPropertySegmented(structures[0], l => l.unit.model.conformation.atomId.value(l.atom)));
  207. //console.log(sumPropertySegmentedMutable(structures[0], l => l.unit.model.conformation.atomId.value(l.atom)));
  208. console.log(sumPropertyAtomSetIt(structures[0], l => l.unit.model.conformation.atomId.value(l.atom)));
  209. //console.log(sumProperty(structures[0], Property.cachedAtomColumn(m => m.conformation.atomId)));
  210. console.log(sumDirect(structures[0]));
  211. console.log('r', sumPropertyResidue(structures[0], l => l.unit.hierarchy.residues.auth_seq_id.value(l.unit.residueIndex[l.atom])));
  212. //const authSeqId = Atom.property(l => l.unit.hierarchy.residues.auth_seq_id.value(l.unit.residueIndex[l.atom]));
  213. const auth_seq_id = Q.props.residue.auth_seq_id;
  214. const q = Q.generators.atomGroups({ atomTest: l => auth_seq_id(l) < 3 });
  215. const qr = q(structures[0]);
  216. console.log(qr);
  217. //const col = models[0].conformation.atomId.value;
  218. const suite = new B.Suite();
  219. suite
  220. //.add('test int', () => sumProperty(structures[0], l => col(l.atom)))
  221. // .add('baseline raw', () => baselineRaw(models[0]))
  222. .add('sum residue', () => sumPropertyResidue(structures[0], l => l.unit.hierarchy.residues.auth_seq_id.value(l.unit.residueIndex[l.atom])))
  223. .add('baseline', () => baseline(models[0]))
  224. .add('direct', () => sumDirect(structures[0]))
  225. //.add('normal int', () => sumProperty(structures[0], l => l.unit.model.conformation.atomId.value(l.atom)))
  226. //.add('atom set it int', () => sumPropertyAtomSetIt(structures[0], l => l.unit.conformation.atomId.value(l.atom)))
  227. // .add('segmented faster int', () => sumPropertySegmented(structures[0], l => l.unit.conformation.atomId.value(l.atom)))
  228. // .add('faster int', () => sumProperty(structures[0], l => l.unit.conformation.atomId.value(l.atom)))
  229. .add('segmented faster _x', () => sumPropertySegmented(structures[0], l => l.unit.conformation.__x[l.atom]))
  230. .add('faster _x', () => sumProperty(structures[0], l => l.unit.conformation.__x[l.atom] + l.unit.conformation.__y[l.atom] + l.unit.conformation.__z[l.atom]))
  231. //.add('segmented mut faster int', () => sumPropertySegmentedMutable(structures[0], l => l.unit.conformation.atomId.value(l.atom)))
  232. //.add('normal shortcut int', () => sumProperty(structures[0], l => l.conformation.atomId.value(l.atom)))
  233. //.add('cached int', () => sumProperty(structures[0], Property.cachedAtomColumn(m => m.conformation.atomId)))
  234. //.add('concat str', () => concatProperty(structures[0], l => l.unit.model.hierarchy.atoms.auth_atom_id.value(l.atom)))
  235. //.add('cached concat str', () => concatProperty(structures[0], Property.cachedAtomColumn(m => m.hierarchy.atoms.auth_atom_id)))
  236. .on('cycle', (e: any) => console.log(String(e.target)))
  237. .run();
  238. }
  239. }
  240. PropertyAccess.run();