structure.ts 12 KB

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