Exact Mass Database Manager (emdb)
emdb is a JavaScript/TypeScript library designed for managing and querying databases of molecular formulas, primarily focused on exact mass calculations in cheminformatics. It is part of the larger 'mass-tools' monorepo maintained by cheminfo. The library is currently stable at version 7.56.0 (as of April 2026), demonstrating a rapid release cadence with multiple updates often shipped monthly, indicating very active development. Key functionalities include loading various predefined chemical databases (e.g., test, KnapSack, commercial, contaminants), creating custom molecular formula databases from structured arrays or Google Sheets, and providing robust utility modules for generating molecular formulas from peptide or nucleotide sequences. It enhances cheminformatics applications by offering precise mass calculations and comprehensive molecular formula manipulation, including detailed information such as exact mass, molecular weight, charge, OCL (Open Chemistry Libre), and mass spectrometry filter data.
Common errors
-
ReferenceError: require is not defined
cause This error occurs when CommonJS `require()` syntax is used in an ES Module (ESM) context, such as a `.mjs` file or a Node.js project with `"type": "module"` in its `package.json`.fixReplace `const emdb = require('emdb');` with `import emdb from 'emdb';` to use the ESM syntax. -
TypeError: emdb.Util.MF is not a constructor
cause This typically indicates that `emdb.Util.MF` is `undefined` or not correctly resolved as a class. Common causes include an incorrect import of `emdb` itself, or trying to access `MF` directly as a named export instead of through `emdb.Util`.fixVerify that `emdb` is correctly imported as a default export (`import emdb from 'emdb';`) and that `emdb.Util.MF` is properly accessed and instantiated using `new`. -
TypeError: emdb.Util.Peptide.sequenceToMF is not a function
cause The `sequenceToMF` method or the `emdb.Util.Peptide` object itself is `undefined`, usually due to an incorrect import path or an attempt to directly import `Peptide`.fixEnsure `emdb` is imported as the default export and that `Util` and `Peptide` are correctly accessed as properties: `emdb.Util.Peptide.sequenceToMF(...)`. Double-check method casing and arguments.
Warnings
- breaking The package version provided in the prompt (3.5.0) is severely outdated. The `emdb` package is currently at version 7.56.0 (as of April 2026), being actively developed as part of the `mass-tools` monorepo. Significant API changes and breaking modifications are highly probable between versions 3.x and 7.x, even if not explicitly detailed in the provided truncated changelog.
- gotcha The examples in the `emdb` README predominantly use CommonJS `require()` syntax. While `emdb` is likely distributed with both CJS and ESM entry points, using `require()` directly may lead to `ReferenceError: require is not defined` errors in pure ES Module (ESM) environments or modern TypeScript projects configured for ESM outputs.
- gotcha All specialized utility classes and methods (e.g., `MF`, `Peptide`, `Nucleotide`, `IsotopicDistribution`) are nested within the `Util` property of the default `emdb` export. Attempting to destructure these or import them directly as named exports (e.g., `import { MF } from 'emdb';`) will result in runtime errors as they are not exported at the top level.
Install
-
npm install emdb -
yarn add emdb -
pnpm add emdb
Imports
- emdb
const emdb = require('emdb');import emdb from 'emdb';
- emdb.Util.MF
import { MF } from 'emdb'; const mf = new MF('C6H6');import emdb from 'emdb'; const mf = new emdb.Util.MF('C6H6'); - emdb.Util.Peptide.sequenceToMF
import { Peptide } from 'emdb'; const peptideMF = Peptide.sequenceToMF('AAA');import emdb from 'emdb'; const peptideMF = emdb.Util.Peptide.sequenceToMF('AAA'); - emdb.Util.Nucleotide.mfFromSequence
import { Nucleotide } from 'emdb'; const dnaMF = Nucleotide.mfFromSequence('GATTACA', { kind: 'dna' });import emdb from 'emdb'; const dnaMF = emdb.Util.Nucleotide.mfFromSequence('GATTACA', { kind: 'dna' });
Quickstart
import emdb from 'emdb';
// 1. Initialize the EMDB manager and load a test database
const dbManager = emdb;
dbManager.loadTest(); // Loads a database named 'test' with MFs from C1 to C100
console.log('Test database loaded. Number of entries (approx):', Object.keys(dbManager.db.test).length);
// 2. Use the Molecular Formula utility to parse and get information
const mfParser = new dbManager.Util.MF('C6H6O');
console.log('MF Parser info for C6H6O:', mfParser.getInfo());
// 3. Generate a molecular formula from a peptide sequence
const peptideSequence = 'VALINE';
const peptideMF = dbManager.Util.Peptide.sequenceToMF(peptideSequence);
console.log(`Molecular formula for peptide '${peptideSequence}': ${peptideMF}`);
// 4. Generate a molecular formula from a DNA nucleotide sequence
const dnaSequence = 'GATTACA';
const dnaMF = dbManager.Util.Nucleotide.mfFromSequence(dnaSequence, { kind: 'dna', circular: false });
console.log(`Molecular formula for DNA sequence '${dnaSequence}': ${dnaMF}`);
// 5. Example of loading a database from an array of molecular formulas
const customMFs = ['C2H4O2', 'C3H8O'];
dbManager.fromArray(customMFs, { database: 'myCustomDB' });
console.log('Custom database loaded. Entries:', dbManager.db.myCustomDB);