structure.ts 13 KB

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