Convert a CSV file containing SMILES strings into an SDF file using RDKit.
This is useful when you have tabular compound data and want to export molecules for docking, visualization, cheminformatics workflows, or tools that expect Structure Data Format (.sdf) files.
- Reads a CSV file with a SMILES column.
- Detects the SMILES column case-insensitively.
- Also accepts column names containing
smile, such ascanonical_smilesorstd_smiles. - Skips blank, missing, or invalid SMILES rows.
- Computes 2D coordinates for easier viewing in molecule viewers.
- Preserves all non-SMILES CSV columns as SDF molecule properties.
- Python 3.8+
- pandas
- RDKit
Recommended installation with conda:
conda install -c conda-forge rdkit pandasThe input must be a CSV file with one molecule per row and a SMILES column.
Example:
smiles,name,source_id
CCO,ethanol,CMPD001
c1ccccc1,benzene,CMPD002Any columns other than the SMILES column are written into the SDF as molecule properties.
python csv2sdf.py input.csv output.sdfExample:
python csv2sdf.py reframe_std.csv reframe_std.sdfThe script writes an SDF file at the output path you provide.
Each valid molecule receives:
- A structure generated from the SMILES string.
- 2D coordinates when RDKit can compute them.
- A molecule title based on the CSV row index.
- SDF properties copied from the other CSV columns.
At the end, the script prints a short summary:
Read 100 rows. Converted 97 molecules, skipped 3 rows.
The script exits with an error if:
- The CSV is empty.
- No SMILES-like column can be found.
- The input file cannot be read.
Rows with invalid SMILES are skipped rather than stopping the full conversion.