From 4f70aa55f2622fe6dd7b50668758b090be8e1ca4 Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Sun, 5 Nov 2017 10:42:52 +0100
Subject: [PATCH] nicer CIF encoder API

---
 src/apps/cif2bcif/converter.ts         |  6 ++----
 src/mol-data/structure/export/mmcif.ts |  6 ++----
 src/mol-io/writer/cif.ts               | 15 +++++++++++++++
 src/perf-tests/cif-encoder.ts          |  5 ++---
 src/perf-tests/structure.ts            |  6 +++++-
 5 files changed, 26 insertions(+), 12 deletions(-)
 create mode 100644 src/mol-io/writer/cif.ts

diff --git a/src/apps/cif2bcif/converter.ts b/src/apps/cif2bcif/converter.ts
index deda28faf..5083500e4 100644
--- a/src/apps/cif2bcif/converter.ts
+++ b/src/apps/cif2bcif/converter.ts
@@ -6,9 +6,7 @@
 
 import Iterator from 'mol-base/collections/iterator'
 import CIF, { Category } from 'mol-io/reader/cif'
-import TextCIFEncoder from 'mol-io/writer/cif/encoder/text'
-import BinaryCIFEncoder from 'mol-io/writer/cif/encoder/binary'
-import * as Encoder from 'mol-io/writer/cif/encoder'
+import * as Encoder from 'mol-io/writer/cif'
 import * as fs from 'fs'
 import classify from './field-classifier'
 
@@ -42,7 +40,7 @@ function getCategoryInstanceProvider(cat: Category): Encoder.CategoryProvider {
 export default async function convert(path: string, asText = false) {
     const cif = await getCIF(path);
 
-    const encoder = asText ? new TextCIFEncoder() : new BinaryCIFEncoder('mol* cif2bcif');
+    const encoder = Encoder.create({ binary: !asText, encoderName: 'mol* cif2bcif' });
     for (const b of cif.blocks) {
         encoder.startDataBlock(b.header);
         for (const c of b.categoryNames) {
diff --git a/src/mol-data/structure/export/mmcif.ts b/src/mol-data/structure/export/mmcif.ts
index 8a78bc1cb..5f3111f04 100644
--- a/src/mol-data/structure/export/mmcif.ts
+++ b/src/mol-data/structure/export/mmcif.ts
@@ -6,10 +6,8 @@
 
 import { Column, Table } from 'mol-base/collections/database'
 import Iterator from 'mol-base/collections/iterator'
-import * as Encoder from 'mol-io/writer/cif/encoder'
+import * as Encoder from 'mol-io/writer/cif'
 //import { mmCIF_Schema } from 'mol-io/reader/cif/schema/mmcif'
-import TextCIFEncoder from 'mol-io/writer/cif/encoder/text'
-import BinaryCIFEncoder from 'mol-io/writer/cif/encoder/binary'
 import { Structure, Atom, AtomSet } from '../structure'
 import { Model } from '../model'
 import P from '../query/properties'
@@ -149,7 +147,7 @@ function to_mmCIF(name: string, structure: Structure, asBinary = false) {
     const model = models[0];
 
     const ctx: Context = { structure, model };
-    const w = asBinary ? new BinaryCIFEncoder('mol*') : new TextCIFEncoder();
+    const w = Encoder.create({ binary: asBinary });
 
     w.startDataBlock(name);
     w.writeCategory(entityProvider, [ctx]);
diff --git a/src/mol-io/writer/cif.ts b/src/mol-io/writer/cif.ts
new file mode 100644
index 000000000..4a6df8380
--- /dev/null
+++ b/src/mol-io/writer/cif.ts
@@ -0,0 +1,15 @@
+/**
+ * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import TextCIFEncoder from './cif/encoder/text'
+import BinaryCIFEncoder from './cif/encoder/binary'
+
+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();
+}
\ No newline at end of file
diff --git a/src/perf-tests/cif-encoder.ts b/src/perf-tests/cif-encoder.ts
index 08cfa6d7f..2b1193e3e 100644
--- a/src/perf-tests/cif-encoder.ts
+++ b/src/perf-tests/cif-encoder.ts
@@ -5,8 +5,7 @@
  */
 
 import Iterator from 'mol-base/collections/iterator'
-import * as Enc from 'mol-io/writer/cif/encoder'
-import CW from 'mol-io/writer/cif/encoder/text'
+import * as Enc from 'mol-io/writer/cif'
 
 const category1: Enc.CategoryDefinition<number> = {
     name: 'test',
@@ -51,7 +50,7 @@ function getInstance(ctx: { cat: Enc.CategoryDefinition<number>, rowCount: numbe
     }
 }
 
-const w = new CW();
+const w = Enc.create();
 
 w.startDataBlock('test');
 w.writeCategory(getInstance, [{ rowCount: 5, cat: category1 }]);
diff --git a/src/perf-tests/structure.ts b/src/perf-tests/structure.ts
index 006070d1a..801a98c79 100644
--- a/src/perf-tests/structure.ts
+++ b/src/perf-tests/structure.ts
@@ -237,6 +237,10 @@ export namespace PropertyAccess {
     //     return s;
     // }
 
+    export function write(s: Structure) {
+        console.log(to_mmCIF('test', s));
+    }
+
     export async function run() {
         //const { structures, models } = await readCIF('./examples/1cbs_full.bcif');
         const { structures, models } = await readCIF('e:/test/quick/3j3q_full.bcif');
@@ -286,7 +290,7 @@ export namespace PropertyAccess {
             chainTest: Q.pred.inSet(P.chain.auth_asym_id, ['A', 'B', 'C', 'D']),
             residueTest: Q.pred.eq(P.residue.auth_comp_id, 'ALA')
         });
-        const q0r = q(structures[0]);
+        q(structures[0]);
         //console.log(to_mmCIF('test', Selection.union(q0r)));
 
         console.time('q1')
-- 
GitLab