It uses a lib called itp-parser (code is open source an can be modified, the lib isn't perfect :D)
An example build for Node.js of a TOP system file:
import ItpFile, { TopFile } from 'itp-parser';
import * as path from 'path';
const f = TopFile.readFromString('');
const itps = itp_files; // Array of strings (file paths) !
// Insert special go tag
const has_go = itps.some(e => path.basename(e).startsWith('BB-part-def_VirtGoSites'));
if (has_go) {
f.headlines.push('#define GO_VIRT');
}
// mandatory martini include for gromacs
f.headlines.push('#include "martini.itp"');
// Include every itp except the one with VirtGoSites
const real_itps = itps.filter(itp => !itp.includes('VirtGoSites') && !itp.includes('go4view_harm'));
for (const itp of real_itps) {
// Include the file (and keep only its basename (not the full path))
f.headlines.push(`#include "${path.basename(itp)}"`);
}
f.setField('system', ['This is an auto generated system']);
// Read every ITP and extract molecule type
f.appendFieldLine('molecules', ';moleculetype\tcount');
for (const itp of real_itps) {
// Just construct the molecules array of the TOP file (and provides rudimentary checks to avoid line duplications)
const locals = await ItpFile.readMany(itp);
for (const local of locals) {
if (!local.name) {
continue;
}
f.appendFieldLine('molecules', `${local.name}\t1`);
}
}
// Render TopFile as a string
const content = f.toString();
An example of TOP generation can be found in martinize-db-client.
It uses a lib called itp-parser (code is open source an can be modified, the lib isn't perfect :D)
An example build for Node.js of a TOP system file: