From 613cdc3145867ab7d37cdc5920905afec74efad5 Mon Sep 17 00:00:00 2001
From: Alexander Rose <alexander.rose@weirdbyte.de>
Date: Mon, 25 May 2020 13:46:46 -0700
Subject: [PATCH] fix building of cli tools

---
 .gitignore                                    |  1 +
 README.md                                     | 14 ++---
 package.json                                  |  4 +-
 .../chem-comp-bond/create-table.ts            |  1 +
 src/{apps => cli}/cif2bcif/converter.ts       |  0
 src/{apps => cli}/cif2bcif/index.ts           |  1 +
 src/{apps => cli}/cifschema/index.ts          |  1 +
 src/{apps => cli}/cifschema/util/cif-dic.ts   |  0
 src/{apps => cli}/cifschema/util/generate.ts  |  0
 src/{apps => cli}/cifschema/util/helper.ts    |  0
 src/{apps => cli}/cifschema/util/schema.ts    |  0
 src/cli/lipid-params/index.ts                 | 59 +++++++++++++++++++
 src/{apps => cli}/state-docs/index.ts         |  1 +
 src/{apps => cli}/state-docs/pd-to-md.ts      |  0
 src/{apps => cli}/structure-info/helpers.ts   |  0
 src/{apps => cli}/structure-info/model.ts     |  1 +
 src/{apps => cli}/structure-info/volume.ts    |  1 +
 src/perf-tests/mol-script.ts                  |  2 +-
 tsconfig.json                                 |  2 +-
 tsconfig.servers.json                         |  4 +-
 20 files changed, 79 insertions(+), 13 deletions(-)
 rename src/{apps => cli}/chem-comp-bond/create-table.ts (99%)
 rename src/{apps => cli}/cif2bcif/converter.ts (100%)
 rename src/{apps => cli}/cif2bcif/index.ts (98%)
 rename src/{apps => cli}/cifschema/index.ts (99%)
 rename src/{apps => cli}/cifschema/util/cif-dic.ts (100%)
 rename src/{apps => cli}/cifschema/util/generate.ts (100%)
 rename src/{apps => cli}/cifschema/util/helper.ts (100%)
 rename src/{apps => cli}/cifschema/util/schema.ts (100%)
 create mode 100644 src/cli/lipid-params/index.ts
 rename src/{apps => cli}/state-docs/index.ts (99%)
 rename src/{apps => cli}/state-docs/pd-to-md.ts (100%)
 rename src/{apps => cli}/structure-info/helpers.ts (100%)
 rename src/{apps => cli}/structure-info/model.ts (99%)
 rename src/{apps => cli}/structure-info/volume.ts (99%)

diff --git a/.gitignore b/.gitignore
index 202483f20..d0ab23e57 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,7 @@ node_modules/
 debug.log
 npm-debug.log
 tsconfig.tsbuildinfo
+tsconfig.servers.tsbuildinfo
 
 *.sublime-workspace
 .idea
diff --git a/README.md b/README.md
index d30244e44..ec482ded2 100644
--- a/README.md
+++ b/README.md
@@ -41,7 +41,7 @@ Moreover, the project contains the implementation of `servers`, including
 - `servers/volume` A tool for accessing volumetric experimental data related to molecular structures.
 - `servers/plugin-state` A basic server to store Mol* Plugin states.
 
-The project also contains performance tests (`perf-tests`), `examples`, and basic proof of concept `apps` (CIF to BinaryCIF converter and JSON domain annotation to CIF converter).
+The project also contains performance tests (`perf-tests`), `examples`, and basic proof of concept `cli` apps (CIF to BinaryCIF converter and JSON domain annotation to CIF converter).
 
 ## Previous Work
 This project builds on experience from previous solutions:
@@ -90,10 +90,10 @@ and navigate to `build/viewer`
 ### Code generation
 **CIF schemas**
 
-    node ./lib/apps/cifschema -mip ../../../../mol-data -o src/mol-io/reader/cif/schema/mmcif.ts -p mmCIF
-    node ./lib/apps/cifschema -mip ../../../../mol-data -o src/mol-io/reader/cif/schema/ccd.ts -p CCD
-    node ./lib/apps/cifschema -mip ../../../../mol-data -o src/mol-io/reader/cif/schema/bird.ts -p BIRD
-    node ./lib/apps/cifschema -mip ../../../../mol-data -o src/mol-io/reader/cif/schema/cif-core.ts -p CifCore -aa
+    node ./lib/cli/cifschema -mip ../../../../mol-data -o src/mol-io/reader/cif/schema/mmcif.ts -p mmCIF
+    node ./lib/cli/cifschema -mip ../../../../mol-data -o src/mol-io/reader/cif/schema/ccd.ts -p CCD
+    node ./lib/cli/cifschema -mip ../../../../mol-data -o src/mol-io/reader/cif/schema/bird.ts -p BIRD
+    node ./lib/cli/cifschema -mip ../../../../mol-data -o src/mol-io/reader/cif/schema/cif-core.ts -p CifCore -aa
 
 **GraphQL schemas**
 
@@ -102,7 +102,7 @@ and navigate to `build/viewer`
 ### Other scripts
 **Create chem comp bond table**
 
-    node --max-old-space-size=4096 lib/apps/chem-comp-bond/create-table.js build/data/ccb.bcif -b
+    node --max-old-space-size=4096 lib/cli/chem-comp-bond/create-table.js build/data/ccb.bcif -b
 
 **Test model server**
 
@@ -120,7 +120,7 @@ To see all available commands, use ``node lib/servers/model/preprocess -h``.
 
 Or
 
-    node ./lib/apps/cif2bcif
+    node ./lib/cli/cif2bcif
 
 ## Development
 
diff --git a/package.json b/package.json
index c5aab99a0..72986faaf 100644
--- a/package.json
+++ b/package.json
@@ -43,8 +43,8 @@
     "build/viewer/"
   ],
   "bin": {
-    "cif2bcif": "lib/apps/cif2bcif/index.js",
-    "cifschema": "lib/apps/cifschema/index.js",
+    "cif2bcif": "lib/cli/cif2bcif/index.js",
+    "cifschema": "lib/cli/cifschema/index.js",
     "model-server": "lib/servers/servers/model/server.js",
     "model-server-query": "lib/servers/servers/model/query.js",
     "model-server-preprocess": "lib/servers/servers/model/preprocess.js",
diff --git a/src/apps/chem-comp-bond/create-table.ts b/src/cli/chem-comp-bond/create-table.ts
similarity index 99%
rename from src/apps/chem-comp-bond/create-table.ts
rename to src/cli/chem-comp-bond/create-table.ts
index ef09d66cc..1837028e6 100644
--- a/src/apps/chem-comp-bond/create-table.ts
+++ b/src/cli/chem-comp-bond/create-table.ts
@@ -1,3 +1,4 @@
+#!/usr/bin/env node
 /**
  * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
diff --git a/src/apps/cif2bcif/converter.ts b/src/cli/cif2bcif/converter.ts
similarity index 100%
rename from src/apps/cif2bcif/converter.ts
rename to src/cli/cif2bcif/converter.ts
diff --git a/src/apps/cif2bcif/index.ts b/src/cli/cif2bcif/index.ts
similarity index 98%
rename from src/apps/cif2bcif/index.ts
rename to src/cli/cif2bcif/index.ts
index c3b278d6f..c862695ec 100644
--- a/src/apps/cif2bcif/index.ts
+++ b/src/cli/cif2bcif/index.ts
@@ -1,3 +1,4 @@
+#!/usr/bin/env node
 /**
  * Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
diff --git a/src/apps/cifschema/index.ts b/src/cli/cifschema/index.ts
similarity index 99%
rename from src/apps/cifschema/index.ts
rename to src/cli/cifschema/index.ts
index b20754ce8..41032a038 100644
--- a/src/apps/cifschema/index.ts
+++ b/src/cli/cifschema/index.ts
@@ -1,3 +1,4 @@
+#!/usr/bin/env node
 /**
  * Copyright (c) 2017-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
diff --git a/src/apps/cifschema/util/cif-dic.ts b/src/cli/cifschema/util/cif-dic.ts
similarity index 100%
rename from src/apps/cifschema/util/cif-dic.ts
rename to src/cli/cifschema/util/cif-dic.ts
diff --git a/src/apps/cifschema/util/generate.ts b/src/cli/cifschema/util/generate.ts
similarity index 100%
rename from src/apps/cifschema/util/generate.ts
rename to src/cli/cifschema/util/generate.ts
diff --git a/src/apps/cifschema/util/helper.ts b/src/cli/cifschema/util/helper.ts
similarity index 100%
rename from src/apps/cifschema/util/helper.ts
rename to src/cli/cifschema/util/helper.ts
diff --git a/src/apps/cifschema/util/schema.ts b/src/cli/cifschema/util/schema.ts
similarity index 100%
rename from src/apps/cifschema/util/schema.ts
rename to src/cli/cifschema/util/schema.ts
diff --git a/src/cli/lipid-params/index.ts b/src/cli/lipid-params/index.ts
new file mode 100644
index 000000000..1aec421c4
--- /dev/null
+++ b/src/cli/lipid-params/index.ts
@@ -0,0 +1,59 @@
+#!/usr/bin/env node
+/**
+ * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author Alexander Rose <alexander.rose@weirdbyte.de>
+ */
+
+import * as argparse from 'argparse';
+import * as fs from 'fs';
+import * as path from 'path';
+import fetch from 'node-fetch';
+
+const BUILD_DIR = path.resolve(__dirname, '../build/');
+const LIPIDS_DIR = path.resolve(BUILD_DIR, 'lipids/');
+
+const MARTINI_LIPIDS_PATH = path.resolve(LIPIDS_DIR, 'martini_lipids.itp');
+const MARTINI_LIPIDS_URL = 'http://www.cgmartini.nl/images/parameters/lipids/Collections/martini_v2.0_lipids_all_201506.itp';
+
+async function ensureAvailable(path: string, url: string) {
+    if (FORCE_DOWNLOAD || !fs.existsSync(path)) {
+        const name = url.substr(url.lastIndexOf('/') + 1);
+        console.log(`downloading ${name}...`);
+        const data = await fetch(url);
+        if (!fs.existsSync(LIPIDS_DIR)) {
+            fs.mkdirSync(LIPIDS_DIR);
+        }
+        fs.writeFileSync(path, await data.text());
+        console.log(`done downloading ${name}`);
+    }
+}
+
+async function ensureLipidsAvailable() { await ensureAvailable(MARTINI_LIPIDS_PATH, MARTINI_LIPIDS_URL); }
+
+async function run() {
+    await ensureLipidsAvailable();
+    const lipidsItpStr = fs.readFileSync(MARTINI_LIPIDS_PATH, 'utf8');
+
+    const m = lipidsItpStr.match(/\[moleculetype\]\n; molname      nrexcl\n(DGPC)/g);
+    console.log(m);
+}
+
+const parser = new argparse.ArgumentParser({
+    addHelp: true,
+    description: 'Create lipid params (from martini lipids itp)'
+});
+parser.addArgument([ '--forceDownload', '-f' ], {
+    action: 'storeTrue',
+    help: 'Force download of martini lipids itp'
+});
+interface Args {
+    forceDownload: boolean
+}
+const args: Args = parser.parseArgs();
+
+const FORCE_DOWNLOAD = args.forceDownload;
+
+run().catch(e => {
+    console.error(e);
+});
\ No newline at end of file
diff --git a/src/apps/state-docs/index.ts b/src/cli/state-docs/index.ts
similarity index 99%
rename from src/apps/state-docs/index.ts
rename to src/cli/state-docs/index.ts
index bdc50e654..36ed9a903 100644
--- a/src/apps/state-docs/index.ts
+++ b/src/cli/state-docs/index.ts
@@ -1,3 +1,4 @@
+#!/usr/bin/env node
 /**
  * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
diff --git a/src/apps/state-docs/pd-to-md.ts b/src/cli/state-docs/pd-to-md.ts
similarity index 100%
rename from src/apps/state-docs/pd-to-md.ts
rename to src/cli/state-docs/pd-to-md.ts
diff --git a/src/apps/structure-info/helpers.ts b/src/cli/structure-info/helpers.ts
similarity index 100%
rename from src/apps/structure-info/helpers.ts
rename to src/cli/structure-info/helpers.ts
diff --git a/src/apps/structure-info/model.ts b/src/cli/structure-info/model.ts
similarity index 99%
rename from src/apps/structure-info/model.ts
rename to src/cli/structure-info/model.ts
index f50df0dab..39c9f73ed 100644
--- a/src/apps/structure-info/model.ts
+++ b/src/cli/structure-info/model.ts
@@ -1,3 +1,4 @@
+#!/usr/bin/env node
 /**
  * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
diff --git a/src/apps/structure-info/volume.ts b/src/cli/structure-info/volume.ts
similarity index 99%
rename from src/apps/structure-info/volume.ts
rename to src/cli/structure-info/volume.ts
index cfedb46e9..27b13ba0e 100644
--- a/src/apps/structure-info/volume.ts
+++ b/src/cli/structure-info/volume.ts
@@ -1,3 +1,4 @@
+#!/usr/bin/env node
 /**
  * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
diff --git a/src/perf-tests/mol-script.ts b/src/perf-tests/mol-script.ts
index 3b4fc5030..8e4481de2 100644
--- a/src/perf-tests/mol-script.ts
+++ b/src/perf-tests/mol-script.ts
@@ -1,7 +1,7 @@
 import { MolScriptBuilder } from '../mol-script/language/builder';
 import { compile, QuerySymbolRuntime, DefaultQueryRuntimeTable } from '../mol-script/runtime/query/compiler';
 import { QueryContext, Structure, StructureQuery } from '../mol-model/structure';
-import { readCifFile, getModelsAndStructure } from '../apps/structure-info/model';
+import { readCifFile, getModelsAndStructure } from '../cli/structure-info/model';
 import { CustomPropSymbol } from '../mol-script/language/symbol';
 import Type from '../mol-script/language/type';
 import { parseMolScript } from '../mol-script/language/parser';
diff --git a/tsconfig.json b/tsconfig.json
index df2e30317..8f10a37be 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -20,5 +20,5 @@
         "outDir": "lib"
     },
     "include": [ "src/**/*" ],
-    "exclude": [ "src/servers/**/*" ]
+    "exclude": [ "src/servers/**/*", "src/perf-tests/*", "src/cli/**/*" ]
 }
\ No newline at end of file
diff --git a/tsconfig.servers.json b/tsconfig.servers.json
index 023a1cec3..0a29bd69b 100644
--- a/tsconfig.servers.json
+++ b/tsconfig.servers.json
@@ -17,7 +17,7 @@
         "jsx": "react",
         "lib": [ "es6", "dom", "esnext.asynciterable", "es2016" ],
         "rootDir": "src",
-        "outDir": "lib/servers"
+        "outDir": "lib"
     },
-    "include": [ "src/servers/**/*", "src/perf-tests/*" ]
+    "include": [ "src/servers/**/*", "src/perf-tests/*", "src/cli/**/*" ]
 }
\ No newline at end of file
-- 
GitLab