sdf-parser is a JavaScript/TypeScript library designed to parse Structure-Data File (SDF) format files, which are commonly used in cheminformatics to represent chemical structures and associated data. It efficiently converts the content of SDF files into an array of JavaScript objects, where each object represents a chemical entry including its molfile and associated data fields. The current stable version is 8.0.0. The package maintains an active release cadence, with frequent updates. Key features include robust parsing with options for field inclusion/exclusion, custom data transformations via modifiers, and filtering capabilities based on entry properties. For processing large SDF files, it offers an `iterator` API that leverages web streams for memory-efficient handling, compatible with both Node.js and browser environments.
Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Since v8.0.0, sdf-parser is an ESM-only package. CommonJS `require` syntax will not work.
import { parse } from 'sdf-parser';
v8.0.0 is ESM-only. The `iterator` function, introduced in v6.0.0, requires piping input through a `TextDecoderStream` since v7.0.0 for proper text decoding.
import { iterator } from 'sdf-parser';
For TypeScript projects, import `SDFEntry` type for type-safe handling of parsed SDF objects.
import type { SDFEntry } from 'sdf-parser';
This quickstart demonstrates both the synchronous `parse` function for smaller SDF strings, including options for filtering and modifying fields, and the asynchronous `iterator` function for efficient processing of larger SDF data via streams, highlighting the `TextDecoderStream` requirement.
import { parse, iterator } from 'sdf-parser';
import { Readable } from 'node:stream'; // For Node.js to create a stream from string
import { TextDecoderStream } from 'node:stream/web'; // For Node.js compatibility with Web Streams API
// Example SDF content with multiple entries
const sdfData = `
MOLFILE
-OEChem-0104201804252D
5 5 0 0 0 0 0 0 0 0999 V2000
0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.5000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0000 1.5000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.5000 1.5000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.0000 0.0000 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0 0
1 3 1 0 0 0 0
2 4 1 0 0 0 0
3 4 1 0 0 0 0
2 5 1 0 0 0 0
M END
> <ID>
CHEM_1
> <CLogP>
2.5
$$$$
MOLFILE
-OEChem-0104201804252D
3 2 0 0 0 0 0 0 0 0999 V2000
0.0000 0.0000 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
1.5000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.0000 0.0000 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0 0
2 3 1 0 0 0 0
M END
> <ID>
CHEM_2
> <CLogP>
-1.2
$$$$
`;
// --- Example 1: Parsing a full SDF string synchronously ---
const parsedEntries = parse(sdfData, {
include: ['ID', 'CLogP'],
modifiers: {
CLogP: (value: string) => parseFloat(value) // Convert CLogP field to a number
},
filter: (entry: any) => entry.CLogP > 0 // Keep only entries with positive CLogP
});
console.log('Parsed Entries (filtered by CLogP > 0):', parsedEntries);
// --- Example 2: Iterating over an SDF stream (for large files) ---
async function processSDFStream() {
// In Node.js, create a ReadableStream from string data.
// For actual files, use `fs.createReadStream('your.sdf').pipeThrough(new TextDecoderStream())`
const streamFromData = Readable.from([sdfData])
.pipeThrough(new TextDecoderStream()); // Crucial for v7.0.0+ for iterator
const iteratedEntries = [];
try {
for await (const entry of iterator(streamFromData)) {
iteratedEntries.push(entry);
}
console.log('Iterated Entries (all from stream):', iteratedEntries);
} catch (error) {
console.error('Error during stream iteration:', error);
}
}
processSDFStream();
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.
Agent activity
0 hits · last 30 days
No traffic data recorded yet.