Skip to content
Snippets Groups Projects
Commit ecb0f8c4 authored by David Sehnal's avatar David Sehnal
Browse files

tables and data model

parent 4fb9af89
No related branches found
No related tags found
No related merge requests found
...@@ -83,11 +83,12 @@ describe('table', () => { ...@@ -83,11 +83,12 @@ describe('table', () => {
x: Column.ofArray({ array: [10, -1], type: Column.Type.int }), x: Column.ofArray({ array: [10, -1], type: Column.Type.int }),
n: Column.ofArray({ array: ['row1', 'row2'], type: Column.Type.str }), n: Column.ofArray({ array: ['row1', 'row2'], type: Column.Type.str }),
}); });
const s = { x: Column.Type.int }; const s = { x: Column.Type.int, y: Column.Type.int };
const picked = Table.pickColumns(s, t); const picked = Table.pickColumns(s, t, { y: Column.ofArray({ array: [3, 4], type: Column.Type.int })});
expect(picked._columns).toEqual(['x']); expect(picked._columns).toEqual(['x', 'y']);
expect(picked._rowCount).toEqual(2); expect(picked._rowCount).toEqual(2);
expect(picked.x.toArray()).toEqual([10, -1]); expect(picked.x.toArray()).toEqual([10, -1]);
expect(picked.y.toArray()).toEqual([3, 4]);
}); });
it('sort', () => { it('sort', () => {
......
...@@ -15,13 +15,18 @@ namespace Table { ...@@ -15,13 +15,18 @@ namespace Table {
export type Columns<S extends Schema> = { [C in keyof S]: Column<S[C]['T']> } export type Columns<S extends Schema> = { [C in keyof S]: Column<S[C]['T']> }
export type Row<S extends Schema> = { [C in keyof S]: S[C]['T'] } export type Row<S extends Schema> = { [C in keyof S]: S[C]['T'] }
export type Arrays<S extends Schema> = { [C in keyof S]: ArrayLike<S[C]['T']> } export type Arrays<S extends Schema> = { [C in keyof S]: ArrayLike<S[C]['T']> }
export type PartialTable<S extends Table.Schema> = { readonly _rowCount: number, readonly _columns: ReadonlyArray<string> } & { [C in keyof S]?: Column<S[C]['T']> }
export function pickColumns<S extends Schema, T extends S>(schema: S, table: Table<T>): Table<S> { export function pickColumns<S extends Schema>(schema: S, table: PartialTable<S>, guard: Partial<Columns<S>> = {}): Table<S> {
const ret = Object.create(null); const ret = Object.create(null);
const keys = Object.keys(schema); const keys = Object.keys(schema);
ret._rowCount = table._rowCount; ret._rowCount = table._rowCount;
ret._columns = keys; ret._columns = keys;
for (const k of keys) ret[k] = table[k]; for (const k of keys) {
if (!!table[k]) ret[k] = table[k];
else if (!!guard[k]) ret[k] = guard[k];
else throw Error(`Cannot find column '${k}'.`);
}
return ret; return ret;
} }
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
import * as Formats from './model/formats' import * as Formats from './model/formats'
//import CommonProperties from './model/properties/common' //import CommonProperties from './model/properties/common'
import HierarchyProperties from './model/properties/hierarchy' import MacromoleculeTree from './model/properties/macromolecule-tree'
import Conformation from './model/properties/conformation' import Conformation from './model/properties/conformation'
import Segmentation from '../mol-base/collections/integer/segmentation' import Segmentation from '../mol-base/collections/integer/segmentation'
...@@ -23,7 +23,7 @@ interface Model extends Readonly<{ ...@@ -23,7 +23,7 @@ interface Model extends Readonly<{
sourceData: Formats.RawData, sourceData: Formats.RawData,
//common: CommonProperties, //common: CommonProperties,
macromolecule: HierarchyProperties, macromolecule: MacromoleculeTree,
conformation: Conformation, conformation: Conformation,
// used for diffing. // used for diffing.
......
...@@ -8,10 +8,10 @@ import Column from '../../../mol-base/collections/column' ...@@ -8,10 +8,10 @@ import Column from '../../../mol-base/collections/column'
import Table from '../../../mol-base/collections/table' import Table from '../../../mol-base/collections/table'
import { Schema as mmCIF } from '../../../mol-io/reader/cif/schema/mmcif' import { Schema as mmCIF } from '../../../mol-io/reader/cif/schema/mmcif'
const _esCache = Object.create(null);
export interface ElementSymbol extends String { '@type': 'element-symbol' } export interface ElementSymbol extends String { '@type': 'element-symbol' }
export function ElementSymbol(s: string): ElementSymbol { export function ElementSymbol(s: string): ElementSymbol {
// TODO: optimize? return _esCache[s] || (_esCache[s] = s.toUpperCase());
return s.toUpperCase() as any;
} }
export const AtomsSchema = { export const AtomsSchema = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment