Skip to main content

Python bindings for COSMolKit

Project description

COSMolKit Python

COSMolKit is a Python package for molecule graph workflows, SMILES/SDF IO, coordinate access, molecule depiction, and high-throughput batch processing.

API Model: Copy-On-Write (COW) Molecule Values

COSMolKit's Python API uses copy-on-write (COW) value semantics for molecule transforms. Methods such as with_hydrogens(), without_hydrogens(), with_kekulized_bonds(), and with_2d_coords() return a new Molecule; they do not mutate the original object in place.

This is intentionally different from common RDKit Python workflows, where many operations mutate an RWMol or update a molecule object directly. In COSMolKit, keep the returned value:

mol = Molecule.from_smiles("CCO")
mol_h = mol.with_hydrogens()

assert mol is not mol_h
print(mol.to_smiles())
print(mol_h.to_smiles())

Installation

pip install cosmolkit

Quick Start

from cosmolkit import Molecule

mol = Molecule.from_smiles("c1ccccc1O")
drawn = mol.with_2d_coords()

print(mol.to_smiles())
print(drawn.atoms()[0])

drawn.write_png("phenol.png", width=400, height=300)

Chiral tags are available directly on atoms, so code that works with the SMILES/RDKit-style CW and CCW path does not need to switch to the ordered tetrahedral view:

chiral = Molecule.from_smiles("F[C@H](Cl)Br")

print(chiral.to_smiles())
print(chiral.to_smiles(isomeric_smiles=False))

for atom in chiral.atoms():
    if atom.chiral_tag() != "CHI_UNSPECIFIED":
        print(atom.idx(), atom.chiral_tag())

Use Molecule.edit() when you want an explicit editing workflow:

editor = mol.edit()
cl = editor.add_atom("Cl")
editor.add_bond(0, cl, order="single")
mol2 = editor.commit()

Batch Workflows

from cosmolkit import MoleculeBatch

smiles = ["CCO", "c1ccccc1", "not-smiles"]
batch = MoleculeBatch.from_smiles_list(smiles, errors="keep")

prepared = batch.add_hydrogens(errors="keep").compute_2d_coords(errors="keep")
report = prepared.to_images("molecule_images", format="png", errors="skip")

print(prepared.valid_mask())
print(prepared.errors())
print(report)

The errors option controls invalid records:

  • errors="raise" raises on the first batch validation failure.
  • errors="keep" preserves failed records and exposes structured errors.
  • errors="skip" omits failed records from the returned result or export.

Batch SMILES output accepts formatting options:

canonical = prepared.to_smiles_list(canonical=True)
explicit = prepared.to_smiles_list(
    all_bonds_explicit=True,
    all_hs_explicit=True,
)
without_maps = prepared.to_smiles_list(ignore_atom_map_numbers=True)

SDF and Arrays

mol = Molecule.from_smiles("CCO").with_2d_coords()

sdf_text = mol.to_sdf_string(format="v2000")
restored = Molecule.read_sdf_record_from_str(sdf_text, coordinate_dim="2d")

coords = restored.coords_2d()
bounds = restored.dg_bounds_matrix()

print(coords.shape)
print(bounds.shape)

Main Features

  • SMILES parsing and writing with Molecule.from_smiles() and to_smiles()
  • copy-on-write molecule value semantics for transforms
  • SDF file and string IO
  • atom and bond feature inspection
  • hydrogen add/remove transforms
  • Kekule bond representation
  • CW/CCW chiral tags, chiral centers, and tetrahedral stereo inspection
  • 2D coordinate generation and NumPy coordinate arrays
  • distance-geometry bounds matrix export
  • SVG and PNG molecule depictions
  • ordered batch construction, transformation, filtering, and export
  • explicit molecule editing with Molecule.edit()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cosmolkit-0.1.0.tar.gz (488.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cosmolkit-0.1.0-cp39-abi3-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.9+Windows x86-64

cosmolkit-0.1.0-cp39-abi3-manylinux_2_34_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.34+ x86-64

cosmolkit-0.1.0-cp39-abi3-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

Details for the file cosmolkit-0.1.0.tar.gz.

File metadata

  • Download URL: cosmolkit-0.1.0.tar.gz
  • Upload date:
  • Size: 488.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cosmolkit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f0158211d840e3e2202e6233d27225a2c299e3004a02c1a0e25387840fd3a94a
MD5 89d9a1d3fde400e1d5688b18a50fa0d1
BLAKE2b-256 837582f093d3fefecf615fd0666f37484c02d3ccb2fc67f52e15602adea9656b

See more details on using hashes here.

File details

Details for the file cosmolkit-0.1.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: cosmolkit-0.1.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.2 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cosmolkit-0.1.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 54b14915b2c2ec81eef63fd540fa378730bfa077e188782f402802501eceae00
MD5 4b58ca60b2f26b86b8e2b6ace3e94da2
BLAKE2b-256 bf2e9b4a1fdac8d5a45c2e94a3597976d29ba185366d3706b81e39076141d801

See more details on using hashes here.

File details

Details for the file cosmolkit-0.1.0-cp39-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for cosmolkit-0.1.0-cp39-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 be6e14eda2e2112d16d713c1b7275dc1b8aaca4b1ea5913816bca1b9ae95eb42
MD5 5e645744185dde59bf1fa12d77799f1f
BLAKE2b-256 9aa7c61c1757fc58979ddcb60bdd0c2068be67ef0dab881e422a24583f7ed3a7

See more details on using hashes here.

File details

Details for the file cosmolkit-0.1.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cosmolkit-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a8df6d654481563efcd6986bca27074d95625e68f52f7250b1932c3723eee95f
MD5 1d15c7b435ff77a5a5e7ac9aa7a31b29
BLAKE2b-256 413ec285267d7456e334bdd4df99834e2b373559c34742550df6d82ddfda70ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page