Skip to content

Instantly share code, notes, and snippets.

@fredludlow
Last active May 23, 2024 06:30
Show Gist options
  • Save fredludlow/e0a2a4af29d902350c872162315538d1 to your computer and use it in GitHub Desktop.
Save fredludlow/e0a2a4af29d902350c872162315538d1 to your computer and use it in GitHub Desktop.
Run NGL.autoLoad on whole PDB (usuing local mirror)
/**
* Quick script to check cif parsing in NGL by attempting to
* autoLoad all entries from a local mirror.
* Smoke test rather than thorough validation!
*/
const _ = require('browser-env')()
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const NGL = require("./build/js/ngl.dev.js")
// Set pdbRoot appropriately to local mirror of PDB
const pdbRoot = "";
const cifRoot = pdbRoot + "/mmCIF/entries";
// Promisify the required functions
const readdir = promisify(fs.readdir);
const readFile = promisify(fs.readFile);
async function parseCifFiles() {
try {
// Read all subdirectories
const subdirs = await readdir(cifRoot);
for (const subdir of subdirs) {
const subdirPath = path.join(cifRoot, subdir);
// Ensure it's a directory
if (!fs.lstatSync(subdirPath).isDirectory()) {
continue;
}
// Read all CIF files in the subdirectory
const files = await readdir(subdirPath);
for (const file of files) {
if (file.endsWith('.cif.gz')) {
const filePath = path.join(subdirPath, file);
await loadCif(filePath)
}
}
}
} catch (err) {
console.error('Error reading directories:', err);
}
}
async function loadCif(filePath) {
try {
const data = await readFile(filePath)
const f = new File([data], filePath)
await NGL.autoLoad(f)
console.log(`Successfully parsed: ${filePath}`);
} catch (err) {
console.log(`Error parsing ${filePath}:`, err);
}
}
parseCifFiles();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment