Skip to content
Snippets Groups Projects
Commit 5be6c917 authored by McMenemy's avatar McMenemy
Browse files

move to separate script

parent f293a024
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env node
/**
* Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Josh McMenemy <josh.mcmenemy@gmail.com>
*/
import * as argparse from 'argparse';
import * as path from 'path';
import util from 'util';
import fs from 'fs';
require('util.promisify').shim();
const writeFile = util.promisify(fs.writeFile);
import { DatabaseCollection } from '../../mol-data/db';
import { CCD_Schema } from '../../mol-io/reader/cif/schema/ccd';
import { ensureDataAvailable, readCCD } from './create-table';
function extractIonNames(ccd: DatabaseCollection<CCD_Schema>) {
const ionNames: string[] = [];
for (const k in ccd) {
const {chem_comp} = ccd[k];
if (chem_comp.name.value(0).toUpperCase().includes(' ION')) {
ionNames.push(chem_comp.id.value(0));
}
}
// these are extra ions that don't have ION in their name
ionNames.push('NCO', 'OHX');
return ionNames;
}
function writeIonNamesFile(filePath: string, ionNames: string[]) {
const output = `/**
* Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* Code-generated ion names params file. Names extracted from CCD components.
*
* @author molstar/chem-comp-dict/create-table cli
*/
export const IonNames = new Set(${JSON.stringify(ionNames).replace(/"/g, "'").replace(/,/g, ', ')});
`;
writeFile(filePath, output);
}
async function run(out: string, forceDownload = false) {
await ensureDataAvailable(forceDownload);
const ccd = await readCCD();
const ionNames = extractIonNames(ccd);
if (!fs.existsSync(path.dirname(out))) {
fs.mkdirSync(path.dirname(out));
}
writeIonNamesFile(out, ionNames);
}
const parser = new argparse.ArgumentParser({
addHelp: true,
description: 'Extract and save IonNames from CCD.'
});
parser.addArgument('out', {
help: 'Generated file output path.'
});
parser.addArgument([ '--forceDownload', '-f' ], {
action: 'storeTrue',
help: 'Force download of CCD and PVCD.'
});
interface Args {
out: string,
forceDownload?: boolean,
}
const args: Args = parser.parseArgs();
run(args.out, args.forceDownload);
...@@ -25,8 +25,8 @@ import { DefaultMap } from '../../mol-util/map'; ...@@ -25,8 +25,8 @@ import { DefaultMap } from '../../mol-util/map';
import { mmCIF_chemCompBond_schema } from '../../mol-io/reader/cif/schema/mmcif-extras'; import { mmCIF_chemCompBond_schema } from '../../mol-io/reader/cif/schema/mmcif-extras';
import { ccd_chemCompAtom_schema } from '../../mol-io/reader/cif/schema/ccd-extras'; import { ccd_chemCompAtom_schema } from '../../mol-io/reader/cif/schema/ccd-extras';
export async function ensureAvailable(path: string, url: string) { export async function ensureAvailable(path: string, url: string, forceDownload = false) {
if (FORCE_DOWNLOAD || !fs.existsSync(path)) { if (forceDownload || !fs.existsSync(path)) {
console.log(`downloading ${url}...`); console.log(`downloading ${url}...`);
const data = await fetch(url); const data = await fetch(url);
if (!fs.existsSync(DATA_DIR)) { if (!fs.existsSync(DATA_DIR)) {
...@@ -41,9 +41,9 @@ export async function ensureAvailable(path: string, url: string) { ...@@ -41,9 +41,9 @@ export async function ensureAvailable(path: string, url: string) {
} }
} }
export async function ensureDataAvailable() { export async function ensureDataAvailable(forceDownload = false) {
await ensureAvailable(CCD_PATH, CCD_URL); await ensureAvailable(CCD_PATH, CCD_URL, forceDownload);
await ensureAvailable(PVCD_PATH, PVCD_URL); await ensureAvailable(PVCD_PATH, PVCD_URL, forceDownload);
} }
export async function readFileAsCollection<S extends Database.Schema>(path: string, schema: S) { export async function readFileAsCollection<S extends Database.Schema>(path: string, schema: S) {
...@@ -243,33 +243,8 @@ function createAtoms(ccd: DatabaseCollection<CCD_Schema>) { ...@@ -243,33 +243,8 @@ function createAtoms(ccd: DatabaseCollection<CCD_Schema>) {
); );
} }
function extractIonNames(ccd: DatabaseCollection<CCD_Schema>) { async function run(out: string, binary = false, forceDownload = false, ccaOut?: string) {
const ionNames: string[] = []; await ensureDataAvailable(forceDownload);
for (const k in ccd) {
const {chem_comp} = ccd[k];
if (chem_comp.name.value(0).toUpperCase().includes(' ION')) {
ionNames.push(chem_comp.id.value(0));
}
}
return ionNames;
}
function writeIonNamesFile(filePath: string, ionNames: string[]) {
const output = `/**
* Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* Code-generated ion names params file. Names extracted from CCD components.
*
* @author molstar/chem-comp-dict/create-table cli
*/
export const IonNames = new Set(${JSON.stringify(ionNames).replace(/"/g, "'").replace(/,/g, ', ')});
`;
writeFile(filePath, output);
}
async function run(out: string, binary = false, ionNamesOut?: string, ccaOut?: string) {
await ensureDataAvailable();
const ccd = await readCCD(); const ccd = await readCCD();
const pvcd = await readPVCD(); const pvcd = await readPVCD();
...@@ -288,14 +263,6 @@ async function run(out: string, binary = false, ionNamesOut?: string, ccaOut?: s ...@@ -288,14 +263,6 @@ async function run(out: string, binary = false, ionNamesOut?: string, ccaOut?: s
} }
writeFile(ccaOut, ccaCif); writeFile(ccaOut, ccaCif);
} }
if (!!ionNamesOut) {
const ionNames = extractIonNames(ccd);
if (!fs.existsSync(path.dirname(ionNamesOut))) {
fs.mkdirSync(path.dirname(ionNamesOut));
}
writeIonNamesFile(ionNamesOut, ionNames);
}
} }
const CCB_TABLE_NAME = 'CHEM_COMP_BONDS'; const CCB_TABLE_NAME = 'CHEM_COMP_BONDS';
...@@ -322,10 +289,6 @@ parser.addArgument([ '--binary', '-b' ], { ...@@ -322,10 +289,6 @@ parser.addArgument([ '--binary', '-b' ], {
action: 'storeTrue', action: 'storeTrue',
help: 'Output as BinaryCIF.' help: 'Output as BinaryCIF.'
}); });
parser.addArgument([ '--ionNamesOut', '-ino' ], {
help: 'Generated file output path to save extracted ion names.',
required: false
});
parser.addArgument(['--ccaOut', '-a'], { parser.addArgument(['--ccaOut', '-a'], {
help: 'Optional generated file output path for chem_comp_atom data.', help: 'Optional generated file output path for chem_comp_atom data.',
required: false required: false
...@@ -334,11 +297,8 @@ interface Args { ...@@ -334,11 +297,8 @@ interface Args {
out: string, out: string,
forceDownload?: boolean, forceDownload?: boolean,
binary?: boolean, binary?: boolean,
ionNamesOut?: string,
ccaOut?: string ccaOut?: string
} }
const args: Args = parser.parseArgs(); const args: Args = parser.parseArgs();
const FORCE_DOWNLOAD = args.forceDownload; run(args.out, args.binary, args.forceDownload, args.ccaOut);
run(args.out, args.binary, args.ionNamesOut, args.ccaOut);
...@@ -6,4 +6,4 @@ ...@@ -6,4 +6,4 @@
* @author molstar/chem-comp-dict/create-table cli * @author molstar/chem-comp-dict/create-table cli
*/ */
export const IonNames = new Set(['118', '119', '543', '1AL', '1CU', '2FK', '2HP', '2OF', '3CO', '3MT', '3NI', '3OF', '3P8', '4MO', '4PU', '4TI', '6MO', 'ACT', 'AG', 'AL', 'ALF', 'AM', 'ATH', 'AU', 'AU3', 'AUC', 'AZI', 'BA', 'BCT', 'BEF', 'BF4', 'BO4', 'BR', 'BS3', 'BSY', 'CA', 'CAC', 'CD', 'CD1', 'CD3', 'CD5', 'CE', 'CF', 'CHT', 'CL', 'CO', 'CO3', 'CO5', 'CON', 'CR', 'CS', 'CSB', 'CU', 'CU1', 'CU3', 'CUA', 'CUZ', 'CYN', 'DME', 'DMI', 'DSC', 'DTI', 'DY', 'E4N', 'EDR', 'EMC', 'ER3', 'EU', 'EU3', 'F', 'FE', 'FE2', 'FPO', 'GA', 'GD3', 'GEP', 'HAI', 'HG', 'HGC', 'IN', 'IOD', 'IR', 'IR3', 'IRI', 'IUM', 'K', 'KO4', 'LA', 'LCO', 'LCP', 'LI', 'LU', 'MAC', 'MG', 'MH2', 'MH3', 'MLI', 'MMC', 'MN', 'MN3', 'MN5', 'MN6', 'MO1', 'MO2', 'MO3', 'MO4', 'MO5', 'MO6', 'MOO', 'MOS', 'MOW', 'MW1', 'MW2', 'MW3', 'NA', 'NA2', 'NA5', 'NA6', 'NAO', 'NAW', 'NET', 'NH4', 'NI', 'NI1', 'NI2', 'NI3', 'NO2', 'NO3', 'NRU', 'O4M', 'OAA', 'OC1', 'OC2', 'OC3', 'OC4', 'OC5', 'OC6', 'OC7', 'OC8', 'OCL', 'OCM', 'OCN', 'OCO', 'OF1', 'OF2', 'OF3', 'OH', 'OS', 'OS4', 'OXL', 'PB', 'PBM', 'PD', 'PDV', 'PER', 'PI', 'PO3', 'PO4', 'PR', 'PT', 'PT4', 'PTN', 'RB', 'RH3', 'RHD', 'RU', 'SB', 'SCN', 'SE4', 'SEK', 'SM', 'SMO', 'SO3', 'SO4', 'SR', 'T1A', 'TB', 'TBA', 'TCN', 'TEA', 'TH', 'THE', 'TL', 'TMA', 'TRA', 'UNX', 'V', 'VN3', 'VO4', 'W', 'WO5', 'Y1', 'YB', 'YB2', 'YH', 'YT3', 'ZCM', 'ZN', 'ZN2', 'ZN3', 'ZNO', 'ZO3', 'ZR']); export const IonNames = new Set(['118', '119', '543', '1AL', '1CU', '2FK', '2HP', '2OF', '3CO', '3MT', '3NI', '3OF', '3P8', '4MO', '4PU', '4TI', '6MO', 'ACT', 'AG', 'AL', 'ALF', 'AM', 'ATH', 'AU', 'AU3', 'AUC', 'AZI', 'BA', 'BCT', 'BEF', 'BF4', 'BO4', 'BR', 'BS3', 'BSY', 'CA', 'CAC', 'CD', 'CD1', 'CD3', 'CD5', 'CE', 'CF', 'CHT', 'CL', 'CO', 'CO3', 'CO5', 'CON', 'CR', 'CS', 'CSB', 'CU', 'CU1', 'CU3', 'CUA', 'CUZ', 'CYN', 'DME', 'DMI', 'DSC', 'DTI', 'DY', 'E4N', 'EDR', 'EMC', 'ER3', 'EU', 'EU3', 'F', 'FE', 'FE2', 'FPO', 'GA', 'GD3', 'GEP', 'HAI', 'HG', 'HGC', 'IN', 'IOD', 'IR', 'IR3', 'IRI', 'IUM', 'K', 'KO4', 'LA', 'LCO', 'LCP', 'LI', 'LU', 'MAC', 'MG', 'MH2', 'MH3', 'MLI', 'MMC', 'MN', 'MN3', 'MN5', 'MN6', 'MO1', 'MO2', 'MO3', 'MO4', 'MO5', 'MO6', 'MOO', 'MOS', 'MOW', 'MW1', 'MW2', 'MW3', 'NA', 'NA2', 'NA5', 'NA6', 'NAO', 'NAW', 'NET', 'NH4', 'NI', 'NI1', 'NI2', 'NI3', 'NO2', 'NO3', 'NRU', 'O4M', 'OAA', 'OC1', 'OC2', 'OC3', 'OC4', 'OC5', 'OC6', 'OC7', 'OC8', 'OCL', 'OCM', 'OCN', 'OCO', 'OF1', 'OF2', 'OF3', 'OH', 'OS', 'OS4', 'OXL', 'PB', 'PBM', 'PD', 'PDV', 'PER', 'PI', 'PO3', 'PO4', 'PR', 'PT', 'PT4', 'PTN', 'RB', 'RH3', 'RHD', 'RU', 'SB', 'SCN', 'SE4', 'SEK', 'SM', 'SMO', 'SO3', 'SO4', 'SR', 'T1A', 'TB', 'TBA', 'TCN', 'TEA', 'TH', 'THE', 'TL', 'TMA', 'TRA', 'UNX', 'V', 'VN3', 'VO4', 'W', 'WO5', 'Y1', 'YB', 'YB2', 'YH', 'YT3', 'ZCM', 'ZN', 'ZN2', 'ZN3', 'ZNO', 'ZO3', 'ZR', 'NCO', 'OHX']);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment