diff --git a/src/mol-base/collections/database/database.ts b/src/mol-base/collections/database/database.ts index b4d5d0053aef32b997bb6aaeff328ca9b57bde6f..0e4d378b446a8361d610828800b239528d173313 100644 --- a/src/mol-base/collections/database/database.ts +++ b/src/mol-base/collections/database/database.ts @@ -9,15 +9,15 @@ import Table from './table' /** A collection of tables */ type Database<Schema extends Database.Schema> = { readonly _name: string, - readonly _tableNames: string[], + readonly _tableNames: ReadonlyArray<string>, readonly _schema: Schema } & Database.Tables<Schema> namespace Database { - export type Tables<S extends Schema> = { [T in keyof Schema]: Table<S[T]> } + export type Tables<S extends Schema> = { [T in keyof S]: Table<S[T]> } export type Schema = { [table: string]: Table.Schema } - export function ofTables<S extends Schema, Db = Database<S>>(name: string, schema: Schema, tables: Tables<S>): Db { + export function ofTables<S extends Schema>(name: string, schema: Schema, tables: Tables<S>) { const keys = Object.keys(tables); const ret = Object.create(null); const tableNames: string[] = []; diff --git a/src/mol-base/collections/table.ts b/src/mol-base/collections/table___.ts similarity index 100% rename from src/mol-base/collections/table.ts rename to src/mol-base/collections/table___.ts diff --git a/src/mol-data/structure/model/builders/mmcif.ts b/src/mol-data/structure/model/builders/mmcif.ts index 40e713590e1865edae13c50f39d04f4a0e13dea6..6e38b03a3f821d026d7acd48ca2ff6d868121f16 100644 --- a/src/mol-data/structure/model/builders/mmcif.ts +++ b/src/mol-data/structure/model/builders/mmcif.ts @@ -6,7 +6,7 @@ import { mmCIF } from '../data-format' import Model from '../model' -import { Column, Table } from 'mol-base/collections/table' +import { Column, Table } from 'mol-base/collections/database' import { Interval, Segmentation } from 'mol-base/collections/integer' import { newUUID } from 'mol-base/utils/uuid' import * as Hierarchy from '../properties/hierarchy' @@ -45,7 +45,7 @@ function findHierarchyOffsets({ data }: mmCIF, bounds: Interval) { function createHierarchyData({ data }: mmCIF, bounds: Interval, offsets: { residues: ArrayLike<number>, chains: ArrayLike<number> }): Hierarchy.Data { const { atom_site } = data; const start = Interval.start(bounds), end = Interval.end(bounds); - const atoms = Table.ofColumns<Hierarchy.AtomsSchema>({ + const atoms = Table.ofColumns(Hierarchy.AtomsSchema, { type_symbol: Column.ofArray({ array: Column.mapToArray(Column.window(atom_site.type_symbol, start, end), Hierarchy.ElementSymbol), type: Column.Type.aliased<Hierarchy.ElementSymbol>(Column.Type.str) }), label_atom_id: Column.window(atom_site.label_atom_id, start, end), auth_atom_id: Column.window(atom_site.auth_atom_id, start, end), diff --git a/src/mol-data/structure/model/data-format.ts b/src/mol-data/structure/model/data-format.ts index 2ffd22ca306da2752ff18f352a738fb06d0e13d0..a12b0a5d08d4f16361cf325a14ffdb9f041e1d01 100644 --- a/src/mol-data/structure/model/data-format.ts +++ b/src/mol-data/structure/model/data-format.ts @@ -4,9 +4,9 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Frame as mmCIF_Frame } from 'mol-io/reader/cif/schema/mmcif' +import { Database as mmCIF_Database } from 'mol-io/reader/cif/schema/mmcif' -export interface mmCIF { kind: 'mmCIF', data: mmCIF_Frame } +export interface mmCIF { kind: 'mmCIF', data: mmCIF_Database } type Format = | mmCIF diff --git a/src/mol-data/structure/model/properties/conformation.ts b/src/mol-data/structure/model/properties/conformation.ts index 651504d822245861731d8745aa7868cafdc46ad1..2d415686360705dbde092429b743cbea45f137b4 100644 --- a/src/mol-data/structure/model/properties/conformation.ts +++ b/src/mol-data/structure/model/properties/conformation.ts @@ -4,7 +4,7 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Column } from 'mol-base/collections/table' +import { Column } from 'mol-base/collections/database' import UUID from 'mol-base/utils/uuid' interface Conformation { diff --git a/src/mol-data/structure/model/properties/hierarchy.ts b/src/mol-data/structure/model/properties/hierarchy.ts index c44ca9052a90966b2a0edc4255a6574c45b86180..9f6c0d81e3b7a065bdcf4ff49112a9a4149bb3a3 100644 --- a/src/mol-data/structure/model/properties/hierarchy.ts +++ b/src/mol-data/structure/model/properties/hierarchy.ts @@ -4,7 +4,7 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Column, Table } from 'mol-base/collections/table' +import { Column, Table } from 'mol-base/collections/database' import { Segmentation } from 'mol-base/collections/integer' import { Schema as mmCIF } from 'mol-io/reader/cif/schema/mmcif' @@ -63,7 +63,7 @@ export interface Segments { } export interface Keys { - // indicate whether the keys form an increasing sequence and within each chain, sequence numbers + // indicate whether the keys form an increasing sequence and within each chain, sequence numbers // are in increasing order. // monotonous sequences enable for example faster secodnary structure assignment. isMonotonous: boolean, diff --git a/src/mol-data/structure/model/utils/hierarchy-keys.ts b/src/mol-data/structure/model/utils/hierarchy-keys.ts index 06aa40957cbf36d063a2afb606059f42efee9a2b..0b00badb9f1d53438940392d767eaaf64bb3e916 100644 --- a/src/mol-data/structure/model/utils/hierarchy-keys.ts +++ b/src/mol-data/structure/model/utils/hierarchy-keys.ts @@ -4,7 +4,7 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Column } from 'mol-base/collections/table' +import { Column } from 'mol-base/collections/database' import { Data, Segments, Keys } from '../properties/hierarchy' import { Interval, Segmentation } from 'mol-base/collections/integer' diff --git a/src/mol-io/reader/_spec/cif.spec.ts b/src/mol-io/reader/_spec/cif.spec.ts index 553c01917afae6eb1343754471d78ecefb158f9d..cf35dc3e90be92fb68c637f9efb0a3852a6ae1bb 100644 --- a/src/mol-io/reader/_spec/cif.spec.ts +++ b/src/mol-io/reader/_spec/cif.spec.ts @@ -7,6 +7,7 @@ import * as Data from '../cif/data-model' import TextField from '../cif/text/field' import * as Schema from '../cif/schema' +import { Column } from 'mol-base/collections/database' const columnData = `123abc`; @@ -21,20 +22,20 @@ const testBlock = Data.Block({ }, 'test'); namespace TestSchema { - export const atoms = { x: Schema.Types.int, name: Schema.Types.str } + export const atoms = { x: Column.Type.int, name: Column.Type.str } export const schema = { atoms } } describe('schema', () => { - const data = Schema.toTypedFrame(TestSchema.schema, testBlock); + const db = Schema.toDatabase(TestSchema.schema, testBlock); it('property access', () => { - const { x, name } = data.atoms; + const { x, name } = db.atoms; expect(x.value(0)).toBe(1); expect(name.value(1)).toBe('b'); }); it('toArray', () => { - const ret = data.atoms.x.toArray({ array: Int32Array }); + const ret = db.atoms.x.toArray({ array: Int32Array }); expect(ret.length).toBe(3); expect(ret[0]).toBe(1); expect(ret[1]).toBe(2); diff --git a/src/mol-io/reader/_spec/column.spec.ts b/src/mol-io/reader/_spec/column.spec.ts index 556df5d9f7c7b31beb6962a712fe2eaacc70a195..27f0a4e8b44e4021d492b35ffa4477a2a817a468 100644 --- a/src/mol-io/reader/_spec/column.spec.ts +++ b/src/mol-io/reader/_spec/column.spec.ts @@ -7,7 +7,7 @@ import FixedColumn from '../common/text/column/fixed' import TokenColumn from '../common/text/column/token' -import { Column, ColumnHelpers } from 'mol-base/collections/table' +import { Column, ColumnHelpers } from 'mol-base/collections/database' const lines = [ '1.123 abc', diff --git a/src/mol-io/reader/cif.ts b/src/mol-io/reader/cif.ts index 29a734f04605785d2f8fc49a5e3d2a6b4b6db60a..8b22eec8c103d76da655d3605b3372f111ff70d5 100644 --- a/src/mol-io/reader/cif.ts +++ b/src/mol-io/reader/cif.ts @@ -7,15 +7,15 @@ import parseText from './cif/text/parser' import parseBinary from './cif/binary/parser' import { Frame } from './cif/data-model' -import { toTypedFrame as applySchema } from './cif/schema' -import { Schema as mmCIF_Schema, Frame as mmCIF_Frame } from './cif/schema/mmcif' +import { toDatabase } from './cif/schema' +import { Schema as mmCIF_Schema, Database as mmCIF_Database } from './cif/schema/mmcif' export default { parseText, parseBinary, - applySchema, + toDatabase, schema: { - mmCIF: (frame: Frame) => applySchema<typeof mmCIF_Schema, mmCIF_Frame>(mmCIF_Schema, frame) + mmCIF: (frame: Frame) => toDatabase<mmCIF_Schema, mmCIF_Database>(mmCIF_Schema, frame) } } diff --git a/src/mol-io/reader/cif/binary/field.ts b/src/mol-io/reader/cif/binary/field.ts index 22165904c61fde08ca0eefd554e5366a92b1ff6c..5d08ce21888ca8d411a1e825d2c66e0940df8544 100644 --- a/src/mol-io/reader/cif/binary/field.ts +++ b/src/mol-io/reader/cif/binary/field.ts @@ -4,7 +4,7 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Column, ColumnHelpers } from 'mol-base/collections/table' +import { Column, ColumnHelpers } from 'mol-base/collections/database' import * as Data from '../data-model' import { EncodedColumn } from './encoding' import decode from './decoder' diff --git a/src/mol-io/reader/cif/data-model.ts b/src/mol-io/reader/cif/data-model.ts index 4037ae87cbb71223a95ae659e9141e8486e5f59b..3153731627882a35a49cbba7e60944c2d02d1f81 100644 --- a/src/mol-io/reader/cif/data-model.ts +++ b/src/mol-io/reader/cif/data-model.ts @@ -4,7 +4,7 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Column } from 'mol-base/collections/table' +import { Column } from 'mol-base/collections/database' export interface File { readonly name?: string, diff --git a/src/mol-io/reader/cif/schema.ts b/src/mol-io/reader/cif/schema.ts index abc78e5854f0b64f89e3867e1742d171411464f3..5f1c5aa6b9a0403527ad7262e105edc886f22ecc 100644 --- a/src/mol-io/reader/cif/schema.ts +++ b/src/mol-io/reader/cif/schema.ts @@ -5,24 +5,16 @@ */ import * as Data from './data-model' -import { Table, Column, ColumnHelpers } from 'mol-base/collections/table' +import { Database, Table, Column, ColumnHelpers } from 'mol-base/collections/database' -export function toTypedFrame<Schema extends FrameSchema, Frame extends TypedFrame<Schema> = TypedFrame<Schema>>(schema: Schema, frame: Data.Frame): Frame { - return createTypedFrame(schema, frame) as Frame; +export function toDatabase<Schema extends Database.Schema, Frame extends Database<Schema> = Database<Schema>>(schema: Schema, frame: Data.Frame): Frame { + return createDatabase(schema, frame) as Frame; } export function toTable<Schema extends Table.Schema, R extends Table<Schema> = Table<Schema>>(schema: Schema, category: Data.Category): R { - return new _TypedCategory(category, schema, true) as any; + return new CategoryTable(category, schema, true) as any; } -export const Types = Column.Type - -export type FrameSchema = { [category: string]: Table.Schema } -export type TypedFrame<Schema extends FrameSchema> = { - readonly _header?: string, - readonly _frame: Data.Frame -} & { [C in keyof Schema]: Table<Schema[C]> } - type ColumnCtor = (field: Data.Field, category: Data.Category, key: string) => Column<any> function getColumnCtor(t: Column.Type): ColumnCtor { @@ -57,23 +49,17 @@ function createColumn<T>(type: Column.Type, field: Data.Field, value: (row: numb }; } -class _TypedFrame implements TypedFrame<any> { // tslint:disable-line:class-name - header = this._frame.header; - [k: string]: any; - constructor(public _frame: Data.Frame, schema: FrameSchema) { - for (const k of Object.keys(schema)) { - Object.defineProperty(this, k, { value: createTypedCategory(k, schema[k], _frame), enumerable: true, writable: false, configurable: false }); - } - } -} - -class _TypedCategory implements Table<any> { // tslint:disable-line:class-name - _rowCount = this._category.rowCount; +class CategoryTable implements Table<any> { // tslint:disable-line:class-name + _rowCount: number; _columns: ReadonlyArray<string>; + _schema: any; [k: string]: any; - constructor(public _category: Data.Category, schema: Table.Schema, public _isDefined: boolean) { + + constructor(category: Data.Category, schema: Table.Schema, public _isDefined: boolean) { const fieldKeys = Object.keys(schema); + this._rowCount = category.rowCount; this._columns = fieldKeys; + this._schema = schema; const cache = Object.create(null); for (const k of fieldKeys) { const cType = schema[k]; @@ -81,8 +67,8 @@ class _TypedCategory implements Table<any> { // tslint:disable-line:class-name Object.defineProperty(this, k, { get: function() { if (cache[k]) return cache[k]; - const field = _category.getField(k); - cache[k] = !!field ? ctor(field, _category, k) : Column.Undefined(_category.rowCount, cType); + const field = category.getField(k); + cache[k] = !!field ? ctor(field, category, k) : Column.Undefined(category.rowCount, cType); return cache[k]; }, enumerable: true, @@ -92,11 +78,15 @@ class _TypedCategory implements Table<any> { // tslint:disable-line:class-name } } -function createTypedFrame(schema: FrameSchema, frame: Data.Frame): any { - return new _TypedFrame(frame, schema); +function createDatabase(schema: Database.Schema, frame: Data.Frame): Database<any> { + const tables = Object.create(null); + for (const k of Object.keys(schema)) { + tables[k] = createTable(k, (schema as any)[k], frame); + } + return Database.ofTables(frame.header, schema, tables); } -function createTypedCategory(key: string, schema: Table.Schema, frame: Data.Frame) { +function createTable(key: string, schema: Table.Schema, frame: Data.Frame) { const cat = frame.categories[key[0] === '_' ? key : '_' + key]; - return new _TypedCategory(cat || Data.Category.Empty, schema, !!cat); + return new CategoryTable(cat || Data.Category.Empty, schema, !!cat); } \ No newline at end of file diff --git a/src/mol-io/reader/cif/schema/dic.ts b/src/mol-io/reader/cif/schema/dic.ts index 4d20eccc89ad032c556fdbe8df256109a330cb15..fc6c0dd02a90772141bba8ef5a844c266f417a61 100644 --- a/src/mol-io/reader/cif/schema/dic.ts +++ b/src/mol-io/reader/cif/schema/dic.ts @@ -4,7 +4,9 @@ * @author Alexander Rose <alexander.rose@weirdbyte.de> */ -import { Types, TypedFrame } from '../schema' +import { Database, Column } from 'mol-base/collections/database' +import Types = Column.Type + const str = Types.str const float = Types.float @@ -69,7 +71,5 @@ export const Schema = { item_units_conversion } -export interface Frame extends TypedFrame<typeof Schema> { } - -// type dic = TypedFrame<typeof dic> -//export default dic +export type Schema = typeof Schema; +export interface Database extends Database.Tables<typeof Schema> { } \ No newline at end of file diff --git a/src/mol-io/reader/cif/schema/mmcif.ts b/src/mol-io/reader/cif/schema/mmcif.ts index f9b7b9a6cfe5a58ba08dd4ce6784f3ffd7c12acd..9682fef280ee359fa0ef22f0122ec81912ec8795 100644 --- a/src/mol-io/reader/cif/schema/mmcif.ts +++ b/src/mol-io/reader/cif/schema/mmcif.ts @@ -4,7 +4,8 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Types, TypedFrame } from '../schema' +import { Database, Column } from 'mol-base/collections/database' +import Types = Column.Type const str = Types.str; const int = Types.int; @@ -245,4 +246,5 @@ export const Schema = { atom_site }; -export interface Frame extends TypedFrame<typeof Schema> { } \ No newline at end of file +export type Schema = typeof Schema; +export interface Database extends Database<typeof Schema> { } \ No newline at end of file diff --git a/src/mol-io/reader/cif/text/field.ts b/src/mol-io/reader/cif/text/field.ts index 6c1ad0fe437f24f6f09c0976b10e3c925fab316f..2dd357812e59efc4d02dc61d7b35c5f03570dded 100644 --- a/src/mol-io/reader/cif/text/field.ts +++ b/src/mol-io/reader/cif/text/field.ts @@ -4,7 +4,7 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Column, ColumnHelpers } from 'mol-base/collections/table' +import { Column, ColumnHelpers } from 'mol-base/collections/database' import * as TokenColumn from '../../common/text/column/token' import { Tokens } from '../../common/text/tokenizer' import * as Data from '../data-model' diff --git a/src/mol-io/reader/common/text/column/fixed.ts b/src/mol-io/reader/common/text/column/fixed.ts index c9d4aa6a193df865e786e25a800faeb6fe29880b..6438f3728c5711cd944cd925782bfa73f2f0eb77 100644 --- a/src/mol-io/reader/common/text/column/fixed.ts +++ b/src/mol-io/reader/common/text/column/fixed.ts @@ -4,7 +4,7 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Column, ColumnHelpers } from 'mol-base/collections/table' +import { Column, ColumnHelpers } from 'mol-base/collections/database' import { trimStr, Tokens } from '../tokenizer' import { parseIntSkipLeadingWhitespace, parseFloatSkipLeadingWhitespace } from '../number-parser' diff --git a/src/mol-io/reader/common/text/column/token.ts b/src/mol-io/reader/common/text/column/token.ts index 7ed57ed1ed163448e8580da93f439be7125fdf49..e80027e92dbcf539f89797843e1eee239c5370d6 100644 --- a/src/mol-io/reader/common/text/column/token.ts +++ b/src/mol-io/reader/common/text/column/token.ts @@ -4,7 +4,7 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Column, ColumnHelpers } from 'mol-base/collections/table' +import { Column, ColumnHelpers } from 'mol-base/collections/database' import { Tokens } from '../tokenizer' import { parseInt as fastParseInt, parseFloat as fastParseFloat } from '../number-parser' diff --git a/src/mol-io/reader/gro/parser.ts b/src/mol-io/reader/gro/parser.ts index a257e5020d67d371b2eae0c0c92a329b6cc2093b..3210578239c85fc0fe49c6d6e7761808e7166d83 100644 --- a/src/mol-io/reader/gro/parser.ts +++ b/src/mol-io/reader/gro/parser.ts @@ -5,7 +5,7 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Column } from 'mol-base/collections/table' +import { Column } from 'mol-base/collections/database' import Tokenizer from '../common/text/tokenizer' import FixedColumn from '../common/text/column/fixed' import * as Schema from './schema' diff --git a/src/mol-io/reader/gro/schema.d.ts b/src/mol-io/reader/gro/schema.d.ts index fb2d454b7216d68caa036cdf2566ddef67bfe4f3..8f9cfdf5875abd4b2689ce2d1ddf135d88884ad2 100644 --- a/src/mol-io/reader/gro/schema.d.ts +++ b/src/mol-io/reader/gro/schema.d.ts @@ -5,7 +5,7 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { Column } from 'mol-base/collections/table' +import { Column } from 'mol-base/collections/database' export interface Header { title: string, diff --git a/src/mol-io/reader/mol2/schema.d.ts b/src/mol-io/reader/mol2/schema.d.ts index f092b0736fd4ed5b1ac1562abf479644a34efa96..8bbf35cec494c25723fd6f9a24c6d8df059b8e7a 100644 --- a/src/mol-io/reader/mol2/schema.d.ts +++ b/src/mol-io/reader/mol2/schema.d.ts @@ -4,7 +4,7 @@ * @author Alexander Rose <alexander.rose@weirdbyte.de> */ -import { Column } from 'mol-base/collections/table' +import { Column } from 'mol-base/collections/database' // Full format http://chemyang.ccnu.edu.cn/ccb/server/AIMMS/mol2.pdf // there are many records but for now ignore (pass over) all but the following diff --git a/src/perf-tests/column.ts b/src/perf-tests/column.ts index 2d0699c71268b3684115e6508ca51dd5cb299a01..0232e28e0e638798b61744746d46af1b8267d4b0 100644 --- a/src/perf-tests/column.ts +++ b/src/perf-tests/column.ts @@ -1,5 +1,5 @@ import * as B from 'benchmark' -import { Column as C } from 'mol-base/collections/table' +import { Column as C } from 'mol-base/collections/database' export namespace Column { function createData(n: number) { diff --git a/src/perf-tests/structure.ts b/src/perf-tests/structure.ts index 5f14762b8d1465f51dc4cb54a55e6efc367b5add..23bd4e97a79049230c7c0e17d3d9da30127a3a79 100644 --- a/src/perf-tests/structure.ts +++ b/src/perf-tests/structure.ts @@ -55,16 +55,6 @@ export async function readCIF(path: string) { } export namespace PropertyAccess { - function baselineRaw(model: Model) { - const atom_site = model.sourceData.data._frame.categories['_atom_site']; - const id = atom_site.getField('id')!.int; - let s = 0; - for (let i = 0, _i = atom_site.rowCount; i < _i; i++) { - s += id(i); - } - return s; - } - function baseline(model: Model) { const atom_site = model.sourceData.data.atom_site; const id = atom_site.id.value; @@ -245,7 +235,6 @@ export namespace PropertyAccess { console.log('parsed'); console.log(baseline(models[0])); - console.log(baselineRaw(models[0])); console.log(sumProperty(structures[0], l => l.unit.model.conformation.atomId.value(l.atom))); console.log(sumPropertySegmented(structures[0], l => l.unit.model.conformation.atomId.value(l.atom))); //console.log(sumPropertySegmentedMutable(structures[0], l => l.unit.model.conformation.atomId.value(l.atom))); @@ -265,7 +254,6 @@ export namespace PropertyAccess { const suite = new B.Suite(); suite //.add('test int', () => sumProperty(structures[0], l => col(l.atom))) - // .add('baseline raw', () => baselineRaw(models[0])) .add('sum residue', () => sumPropertyResidue(structures[0], l => l.unit.hierarchy.residues.auth_seq_id.value(l.unit.residueIndex[l.atom]))) .add('baseline', () => baseline(models[0]))