From d1bbe015ad96a78b3eac07fdd6ef138763e36d80 Mon Sep 17 00:00:00 2001
From: Alexander Rose <alex.rose@rcsb.org>
Date: Thu, 15 Mar 2018 16:06:17 -0700
Subject: [PATCH] added some mol-io helper methods

---
 src/mol-data/db.ts               |  5 ++++-
 src/mol-io/reader/cif.ts         |  1 +
 src/mol-io/writer/cif.ts         | 21 +++++++++++++++++++++
 src/mol-io/writer/cif/encoder.ts | 11 +++++++++++
 4 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/src/mol-data/db.ts b/src/mol-data/db.ts
index 367b533a0..8ae56bde7 100644
--- a/src/mol-data/db.ts
+++ b/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 }
\ No newline at end of file
+type DatabaseCollection = { [name: string]: Database<Database.Schema> }
+
+export { DatabaseCollection, Database, Table, Column, ColumnHelpers }
\ No newline at end of file
diff --git a/src/mol-io/reader/cif.ts b/src/mol-io/reader/cif.ts
index f53c8fd65..a8e899269 100644
--- a/src/mol-io/reader/cif.ts
+++ b/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,
diff --git a/src/mol-io/writer/cif.ts b/src/mol-io/writer/cif.ts
index 4a6df8380..36c5128fd 100644
--- a/src/mol-io/writer/cif.ts
+++ b/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])
+    }
 }
\ No newline at end of file
diff --git a/src/mol-io/writer/cif/encoder.ts b/src/mol-io/writer/cif/encoder.ts
index 9bf21af47..d5b51db16 100644
--- a/src/mol-io/writer/cif/encoder.ts
+++ b/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
+            };
+        }
+    }
 }
-- 
GitLab