data-model.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Copyright (c) 2017-2018 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 { Column } from 'mol-data/db'
  8. import { Tensor } from 'mol-math/linear-algebra'
  9. import { getNumberType, NumberType } from '../common/text/number-parser';
  10. import { Encoding } from '../../common/binary-cif';
  11. export interface CifFile {
  12. readonly name?: string,
  13. readonly blocks: ReadonlyArray<CifBlock>
  14. }
  15. export function CifFile(blocks: ArrayLike<CifBlock>, name?: string): CifFile {
  16. return { name, blocks: blocks as any };
  17. }
  18. export interface CifFrame {
  19. readonly header: string,
  20. // Category names stored separately so that the ordering can be preserved.
  21. readonly categoryNames: ReadonlyArray<string>,
  22. readonly categories: CifCategories
  23. }
  24. export interface CifBlock extends CifFrame {
  25. readonly saveFrames: CifFrame[]
  26. }
  27. export function CifBlock(categoryNames: string[], categories: CifCategories, header: string, saveFrames: CifFrame[] = []): CifBlock {
  28. return { categoryNames, header, categories, saveFrames };
  29. }
  30. export function CifSafeFrame(categoryNames: string[], categories: CifCategories, header: string): CifFrame {
  31. return { categoryNames, header, categories };
  32. }
  33. export type CifCategories = { readonly [name: string]: CifCategory }
  34. export interface CifCategory {
  35. readonly rowCount: number,
  36. readonly name: string,
  37. readonly fieldNames: ReadonlyArray<string>,
  38. getField(name: string): CifField | undefined
  39. }
  40. export function CifCategory(name: string, rowCount: number, fieldNames: string[], fields: { [name: string]: CifField }): CifCategory {
  41. return { rowCount, name, fieldNames: [...fieldNames], getField(name) { return fields[name]; } };
  42. }
  43. export namespace CifCategory {
  44. export function empty(name: string): CifCategory {
  45. return { rowCount: 0, name, fieldNames: [], getField(name: string) { return void 0; } };
  46. };
  47. }
  48. /**
  49. * Implementation note:
  50. * Always implement without using "this." in any of the interface functions.
  51. * This is to ensure that the functions can invoked without having to "bind" them.
  52. */
  53. export interface CifField {
  54. readonly __array: ArrayLike<any> | undefined,
  55. readonly binaryEncoding: Encoding[] | undefined,
  56. readonly isDefined: boolean,
  57. readonly rowCount: number,
  58. str(row: number): string,
  59. int(row: number): number,
  60. float(row: number): number,
  61. valueKind(row: number): Column.ValueKind,
  62. areValuesEqual(rowA: number, rowB: number): boolean,
  63. toStringArray(params?: Column.ToArrayParams<string>): ReadonlyArray<string>,
  64. toIntArray(params?: Column.ToArrayParams<number>): ReadonlyArray<number>,
  65. toFloatArray(params?: Column.ToArrayParams<number>): ReadonlyArray<number>
  66. }
  67. export function getTensor(category: CifCategory, field: string, space: Tensor.Space, row: number, zeroIndexed: boolean): Tensor.Data {
  68. const ret = space.create();
  69. const offset = zeroIndexed ? 0 : 1;
  70. if (space.rank === 1) {
  71. const rows = space.dimensions[0];
  72. for (let i = 0; i < rows; i++) {
  73. const f = category.getField(`${field}[${i + offset}]`);
  74. space.set(ret, i, !!f ? f.float(row) : 0.0);
  75. }
  76. } else if (space.rank === 2) {
  77. const rows = space.dimensions[0], cols = space.dimensions[1];
  78. for (let i = 0; i < rows; i++) {
  79. for (let j = 0; j < cols; j++) {
  80. const f = category.getField(`${field}[${i + offset}][${j + offset}]`);
  81. space.set(ret, i, j, !!f ? f.float(row) : 0.0);
  82. }
  83. }
  84. } else if (space.rank === 3) {
  85. const d0 = space.dimensions[0], d1 = space.dimensions[1], d2 = space.dimensions[2];
  86. for (let i = 0; i < d0; i++) {
  87. for (let j = 0; j < d1; j++) {
  88. for (let k = 0; k < d2; k++) {
  89. const f = category.getField(`${field}[${i + offset}][${j + offset}][${k + offset}]`);
  90. space.set(ret, i, j, k, !!f ? f.float(row) : 0.0);
  91. }
  92. }
  93. }
  94. } else throw new Error('Tensors with rank > 3 or rank 0 are currently not supported.');
  95. return ret;
  96. }
  97. export function getCifFieldType(field: CifField): Column.Schema.Int | Column.Schema.Float | Column.Schema.Str {
  98. let floatCount = 0, hasString = false, undefinedCount = 0;
  99. for (let i = 0, _i = field.rowCount; i < _i; i++) {
  100. const k = field.valueKind(i);
  101. if (k !== Column.ValueKind.Present) {
  102. undefinedCount++;
  103. continue;
  104. }
  105. const type = getNumberType(field.str(i));
  106. if (type === NumberType.Int) continue;
  107. else if (type === NumberType.Float) floatCount++;
  108. else { hasString = true; break; }
  109. }
  110. if (hasString || undefinedCount === field.rowCount) return Column.Schema.str;
  111. if (floatCount > 0) return Column.Schema.float;
  112. return Column.Schema.int;
  113. }