Przeglądaj źródła

added some mol-io helper methods

Alexander Rose 7 lat temu
rodzic
commit
d1bbe015ad

+ 4 - 1
src/mol-data/db.ts

@@ -2,6 +2,7 @@
  * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author David Sehnal <david.sehnal@gmail.com>
+ * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
 import Database from './db/database'
@@ -9,4 +10,6 @@ import Table from './db/table'
 import Column from './db/column'
 import * as ColumnHelpers from './db/column-helpers'
 
-export { Database, Table, Column, ColumnHelpers }
+type DatabaseCollection = { [name: string]: Database<Database.Schema> }
+
+export { DatabaseCollection, Database, Table, Column, ColumnHelpers }

+ 1 - 0
src/mol-io/reader/cif.ts

@@ -13,6 +13,7 @@ import { mmCIF_Schema, mmCIF_Database } from './cif/schema/mmcif'
 import { CCD_Schema, CCD_Database } from './cif/schema/ccd'
 
 export default {
+    parse: (data: string|Uint8Array) => typeof data === 'string' ? parseText(data) : parseBinary(data),
     parseText,
     parseBinary,
     toDatabase,

+ 21 - 0
src/mol-io/writer/cif.ts

@@ -2,14 +2,35 @@
  * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @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 BinaryCIFEncoder from './cif/encoder/binary'
+import { CategoryDefinition } from './cif/encoder'
 
 export * from './cif/encoder'
 
 export function create(params?: { binary?: boolean, encoderName?: string }) {
     const { binary = false, encoderName = 'mol*' } = params || {};
     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])
+    }
 }

+ 11 - 0
src/mol-io/writer/cif/encoder.ts

@@ -153,4 +153,15 @@ export namespace CategoryDefinition {
     export function ofTable<S extends Table.Schema>(name: string, table: Table<S>): CategoryDefinition<number> {
         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
+            };
+        }
+    }
 }