Skip to content
Snippets Groups Projects
Commit d1bbe015 authored by Alexander Rose's avatar Alexander Rose
Browse files

added some mol-io helper methods

parent 2446334b
Branches
Tags
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/ */
import Database from './db/database' import Database from './db/database'
...@@ -9,4 +10,6 @@ import Table from './db/table' ...@@ -9,4 +10,6 @@ import Table from './db/table'
import Column from './db/column' import Column from './db/column'
import * as ColumnHelpers from './db/column-helpers' import * as ColumnHelpers from './db/column-helpers'
export { Database, Table, Column, ColumnHelpers } type DatabaseCollection = { [name: string]: Database<Database.Schema> }
\ No newline at end of file
export { DatabaseCollection, Database, Table, Column, ColumnHelpers }
\ No newline at end of file
...@@ -13,6 +13,7 @@ import { mmCIF_Schema, mmCIF_Database } from './cif/schema/mmcif' ...@@ -13,6 +13,7 @@ import { mmCIF_Schema, mmCIF_Database } from './cif/schema/mmcif'
import { CCD_Schema, CCD_Database } from './cif/schema/ccd' import { CCD_Schema, CCD_Database } from './cif/schema/ccd'
export default { export default {
parse: (data: string|Uint8Array) => typeof data === 'string' ? parseText(data) : parseBinary(data),
parseText, parseText,
parseBinary, parseBinary,
toDatabase, toDatabase,
......
...@@ -2,10 +2,14 @@ ...@@ -2,10 +2,14 @@
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/ */
import { Database, DatabaseCollection } from 'mol-data/db'
import TextCIFEncoder from './cif/encoder/text' import TextCIFEncoder from './cif/encoder/text'
import BinaryCIFEncoder from './cif/encoder/binary' import BinaryCIFEncoder from './cif/encoder/binary'
import { CategoryDefinition } from './cif/encoder'
export * from './cif/encoder' export * from './cif/encoder'
...@@ -13,3 +17,20 @@ export function create(params?: { binary?: boolean, encoderName?: string }) { ...@@ -13,3 +17,20 @@ export function create(params?: { binary?: boolean, encoderName?: string }) {
const { binary = false, encoderName = 'mol*' } = params || {}; const { binary = false, encoderName = 'mol*' } = params || {};
return binary ? new BinaryCIFEncoder(encoderName) : new TextCIFEncoder(); return binary ? new BinaryCIFEncoder(encoderName) : new TextCIFEncoder();
} }
type CIFEncoder = BinaryCIFEncoder<{}> | TextCIFEncoder<{}>
export function writeDatabase(encoder: CIFEncoder, name: string, database: Database<Database.Schema>) {
encoder.startDataBlock(name);
for (const table of database._tableNames) {
encoder.writeCategory(
CategoryDefinition.instanceProviderOfTable(table, database[table])
);
}
}
export function writeDatabaseCollection(encoder: CIFEncoder, collection: DatabaseCollection) {
for (const name of Object.keys(collection)) {
writeDatabase(encoder, name, collection[name])
}
}
\ No newline at end of file
...@@ -153,4 +153,15 @@ export namespace CategoryDefinition { ...@@ -153,4 +153,15 @@ export namespace CategoryDefinition {
export function ofTable<S extends Table.Schema>(name: string, table: Table<S>): CategoryDefinition<number> { export function ofTable<S extends Table.Schema>(name: string, table: Table<S>): CategoryDefinition<number> {
return { name, fields: FieldDefinitions.ofSchema(table._schema) } return { name, fields: FieldDefinitions.ofSchema(table._schema) }
} }
export function instanceProviderOfTable(name: string, table: Table<Table.Schema>): CategoryProvider {
return function (ctx: any) {
return {
data: table,
definition: ofTable(name, table),
keys: () => Iterator.Range(0, table._rowCount - 1),
rowCount: table._rowCount
};
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment