SDF and MOL files explained for people who open them in Python
A MOL file holds one molecule with coordinates and bonds, and an SDF stacks many with data fields. Here is the layout, and how to read both with RDKit.
A MOL file, or molfile, describes one molecule as a table of atoms with coordinates followed by a table of bonds. An SDF, or structure-data file, is a stack of molfiles, each followed by named data fields and a $$$$ terminator. The format was created by MDL Information Systems, every chemistry program reads it, and it is the usual way a vendor catalogue, a docking library or a set of results reaches your disk. Python with RDKit reads an SDF in three lines, and knowing the layout tells you what those three lines can and cannot recover.
Anatomy of a V2000 molfile
A molfile begins with a three-line header. Line one is the molecule name, line two is a program and timestamp line, and line three is a comment. Any of them may be blank. Line four is the counts line, which gives the number of atoms, the number of bonds, several fields that are now obsolete, a chiral flag and the version tag V2000.
The atom block follows, one line per atom, with x, y and z coordinates in ångströms, the element symbol and a run of small integer fields. The bond block comes next, one line per bond, with the two atom indices, a bond order from 1 to 3 and a stereo flag used for wedge and hash bonds in 2D drawings. Properties such as formal charges and isotopes then appear as M CHG and M ISO lines, and M END closes the molecule. A water molecule looks like this:
water
RDKit 3D
3 2 0 0 0 0 0 0 0 0999 V2000
0.0000 0.0000 0.1173 O 0 0 0 0 0 0 0 0 0 0 0 0
0.0000 0.7572 -0.4692 H 0 0 0 0 0 0 0 0 0 0 0 0
0.0000 -0.7572 -0.4692 H 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0
1 3 1 0
M END
Everything is fixed-width. A shifted column breaks the file, which is why you should never edit a molfile by hand in a word processor.
From MOL to SDF, the data fields
An SDF record is a molfile followed by zero or more data items, then a line containing only $$$$. Each data item starts with a header line in angle brackets, such as > <IC50_nM>, followed by one or more lines of value and a blank line. The field names are free text chosen by whoever wrote the file, so one vendor's MW is another's MolWeight, and a third omits it. Values are strings. Nothing in the format says a field is numeric, so a column of IC50 values may contain >10000 and n.d. next to the numbers. Read them as text first and convert deliberately.
Thousands of records can sit in one file. Nothing links the records except their order, so if a program drops one unreadable molecule, the row numbers in your results shift unless you carry an identifier in a data field.
V3000 when V2000 runs out
The V2000 counts line has three characters per count, so it stops at 999 atoms or bonds. V3000 molfiles replace the fixed-width blocks with M V30 lines that have no such limit and that add enhanced stereochemistry, with groups marking relative, absolute and racemic centres. Proteins, polymers and modern registration systems use V3000. RDKit reads both and writes V2000 by default, switching to V3000 when the molecule needs it or when you ask.
Reading an SDF with RDKit
SDMolSupplier iterates over records and yields a molecule or None for each one. The data fields arrive as properties on the molecule.
from rdkit import Chem
supplier = Chem.SDMolSupplier("library.sdf", removeHs=False)
records = []
for i, mol in enumerate(supplier):
if mol is None:
print(f"record {i} could not be parsed")
continue
row = mol.GetPropsAsDict()
row["name"] = mol.GetProp("_Name")
row["smiles"] = Chem.MolToSmiles(mol)
row["has_3d"] = mol.GetConformer().Is3D()
records.append(row)
print(len(records), "molecules read")
removeHs=False keeps explicit hydrogens, which you want for a docking library and do not want for fingerprinting. GetPropsAsDict returns every data field as a string unless RDKit can parse it as a number. PandasTools.LoadSDF does the same job and returns a DataFrame with the molecules in a column. Writing is symmetrical. SDWriter takes molecules and writes their properties as data fields, so a notebook can round-trip a library with annotations added.
What goes wrong
- Aromaticity is not stored. Bond orders are 1, 2 or 3, so an aromatic ring arrives in Kekulé form and the reading toolkit decides what is aromatic. Bond type 4 exists in the specification but means an aromatic query bond, and some programs misuse it.
- Hydrogens may be explicit, implicit or mixed. A 2D drawing usually omits them and a 3D file from a docking program usually includes them.
- Coordinates may be 2D with z equal to zero. Stereochemistry is then carried by the wedge flags in the bond block, and a program that ignores those flags loses the configuration. The guide on stereochemistry in SMILES explains what the resulting unmarked structure means.
- The chiral flag on the counts line signals absolute versus relative stereochemistry in V2000, and many writers set it inconsistently.
- Charges can appear in an obsolete column of the atom block as well as in
M CHGlines, and the two can disagree. - Line endings and encodings travel badly. A file edited on one operating system may gain carriage returns that break a strict parser on another.
Checking the formula computed from the structure block against any formula in the data fields catches a surprising share of these problems. For the 3D case, ask whether the coordinates came from a crystal structure, a conformer generator or a docking pose, because the file does not say. Docking input needs the first two kinds, as the guide on ligand preparation for docking explains, and a geometry fed to density-functional theory for a σ-profile needs an optimised structure, which the MFsig guide on what a σ-profile is describes.
The Molecule Builder exports a drawn molecule as SMILES or as an SDF with 3D coordinates, and the guide on exporting and sharing results covers which fields to include so that a colleague can read the file without asking you.
Key points
- A molfile is a header, a counts line, a fixed-width atom block, a bond block and
M END. An SDF stacks molfiles with named data fields and$$$$terminators. - Data field names and values are free text. Read them as strings and convert deliberately.
- V2000 stops at 999 atoms and has limited stereo. V3000 lifts both limits.
SDMolSupplieryieldsNonefor unreadable records, so carry an identifier in a field and check for gaps.- Aromaticity, hydrogens, 2D versus 3D and charges are the usual sources of trouble. Verify the formula and the coordinate origin.
Questions people ask
- What is the difference between a MOL file and an SDF?
- A MOL file describes one molecule with a header, a counts line, an atom block with coordinates and a bond block. An SDF is a sequence of such molfiles, each followed by named data fields and a line containing $$$$, so it can carry a whole library with annotations.
- How do I read an SDF file in Python?
- Use RDKit's Chem.SDMolSupplier on the file path and iterate over it. Each item is a molecule, or None if the record could not be parsed, and the data fields are available through GetPropsAsDict. PandasTools.LoadSDF does the same and returns a DataFrame.
- Does an SDF file store aromaticity?
- No. Bond orders are stored as 1, 2 or 3, so aromatic rings are written in Kekulé form and the reading program decides which rings are aromatic. Bond type 4 exists in the specification as an aromatic query bond and should not appear in a structure file.
- What is the difference between V2000 and V3000?
- V2000 uses fixed-width blocks and stops at 999 atoms or bonds, with limited stereochemistry. V3000 uses M V30 lines with no size limit and adds enhanced stereo groups for relative, absolute and racemic centres. RDKit reads both.
Further reading
In practice: The ADMET dashboard resolves a name, a PubChem CID or a SMILES to one compound and shows the identifiers side by side, so a mismatch is visible before anything is computed.