Skip to main content

Fast library for loading and manipulating macromolecular structures

Project description

Overview

ciffy is a fast CIF file parser for molecular structures, with a C backend and Python interface. It supports both NumPy and PyTorch backends for array operations.

Performance

ciffy is 55-115x faster than BioPython and Biotite for parsing CIF files:

Structure Atoms ciffy BioPython Biotite
3SKW 2,826 0.52 ms 39 ms (75x) 31 ms (59x)
9GCM 4,466 0.75 ms 53 ms (71x) 41 ms (55x)
9MDS 102,216 12 ms 1326 ms (107x) 1016 ms (82x)

Run python tests/profiling/profile_io.py to reproduce.

Installation

From PyPI

pip install ciffy

From Source

git clone https://github.com/hmblair/ciffy.git
cd ciffy
pip install -e .

Backends

ciffy supports two array backends:

  • NumPy: Lightweight, no additional dependencies required
  • PyTorch: For GPU support (CUDA/MPS) and integration with deep learning workflows

Specify the backend when loading structures:

import ciffy

# Load with NumPy backend (default)
polymer = ciffy.load("structure.cif", backend="numpy")

# Load with PyTorch backend
polymer = ciffy.load("structure.cif", backend="torch")

Convert between backends:

torch_polymer = polymer.torch()
numpy_polymer = polymer.numpy()

Move tensors to GPU (PyTorch only):

polymer_gpu = polymer.torch().to("cuda")
polymer_mps = polymer.torch().to("mps")

Loading Structures

import ciffy

# Load from CIF file
polymer = ciffy.load("structure.cif")

# Load specific chains
polymer = ciffy.load("structure.cif", chains=["A", "B"])

# Load specific molecule types
polymer = ciffy.load("structure.cif", molecule_types=ciffy.RNA)

# Print summary
print(polymer)

Example output:

PDB 9GCM (numpy)
──────────────────────
   Type     Res  Atoms
A  RNA      135   1413
B  PROTEIN  132   1032
C  PROTEIN  246   1261
D  PROTEIN  485    760
──────────────────────
            998   4466

Working with Polymers

Properties

polymer.coordinates       # (N, 3) atom positions
polymer.atoms             # (N,) atom type indices
polymer.elements          # (N,) element indices
polymer.sequence          # (R,) residue type indices
polymer.bonds             # (B, 2) covalent bond pairs
polymer.molecule_types    # (C,) molecule type per chain
polymer.names             # Chain names ["A", "B", ...]
polymer.lengths           # (C,) residues per chain

polymer.size()                      # Total atoms
polymer.size(ciffy.RESIDUE)         # Total residues
polymer.size(ciffy.CHAIN)           # Total chains
polymer.sequence_str()              # "acgu..." sequence string

Selection

# Select by chain
chain_a = polymer.chain(0)
chains_ab = polymer.chain([0, 1])

# Select by residue
first_residue = polymer.residue(0)
some_residues = polymer.residue([0, 5, 10])

# Select by molecule type
rna_only = polymer.molecule_type(ciffy.RNA)
protein_only = polymer.molecule_type(ciffy.PROTEIN)

# Select by residue type
adenines = polymer.residue_type(ciffy.Residue.A)

# Structural selections
backbone = polymer.backbone()          # Backbone atoms only
bases = polymer.nucleobase()           # Nucleobase atoms (RNA/DNA)
sidechains = polymer.sidechain()       # Sidechain atoms
heavy = polymer.heavy()                # Heavy atoms (no hydrogens)

# Remove unresolved residues
resolved = polymer.strip()

Iteration

# Iterate over all chains
for chain in polymer.chains():
    print(chain.sequence_str())

# Iterate over RNA chains only
for chain in polymer.molecule_type(ciffy.RNA).chains():
    print(chain.pdb_id, chain.sequence_str())

Hierarchy Operations

# Counts at different scales
atoms_per_residue = polymer.counts(ciffy.RESIDUE)    # (R,)
residues_per_chain = polymer.counts(ciffy.CHAIN)     # (C,)

# Membership indices
chain_per_atom = polymer.membership(ciffy.CHAIN)     # (N,) chain index per atom
residue_per_atom = polymer.membership(ciffy.RESIDUE) # (N,) residue index per atom

# Reduce atom features to residue level (mean pooling)
residue_coords = polymer.reduce(polymer.coordinates, ciffy.RESIDUE)  # (R, 3)

# Expand residue features to atom level
atom_features = polymer.expand(residue_features, ciffy.RESIDUE)  # (N, ...)

Geometry

# Center coordinates
centered, centroids = polymer.center(ciffy.MOLECULE)
centered, centroids = polymer.center(ciffy.CHAIN)

# PCA alignment
aligned, rotations = polymer.pca(ciffy.CHAIN)

# Pairwise distances
distances = polymer.pairwise_distances()                    # Atom-atom
distances = polymer.pairwise_distances(ciffy.RESIDUE)       # Residue centroids

# K-nearest neighbors
neighbors = polymer.knn(k=16)

Saving

polymer.write("output.cif")

Building Polymers

From Sequence

Create template polymers (no coordinates) from sequence strings:

import ciffy

# RNA (lowercase with u)
rna = ciffy.template("acguacgu")

# DNA (lowercase with t)
dna = ciffy.template("acgtacgt")

# Protein (uppercase)
protein = ciffy.template("MGKLF")

# Multi-chain
multi = ciffy.template(["acgu", "MGKLF"])

Building Chains Residue-by-Residue

Build polymers incrementally using append():

from ciffy import Polymer, Residue

# Build a template (no coordinates)
p = Polymer()
for res in [Residue.A, Residue.C, Residue.G, Residue.U]:
    p = p.append(res)

# Build with coordinates
p = Polymer()
p = p.append(Residue.A, coords)  # First residue with absolute coords

For autoregressive generation with relative positioning:

from ciffy import Polymer, Residue
from ciffy.geometry import LocalCoordinates

p = Polymer()
p = p.append(Residue.A, first_coords)  # Absolute coordinates

# Subsequent residues use LocalCoordinates(coords, transform)
# where transform is an SE(3) transform [axis-angle (3), translation (3)]
p = p.append(Residue.C, LocalCoordinates(coords, transform))
p = p.append(Residue.G, LocalCoordinates(coords, transform))

Structural Metrics

import ciffy

# RMSD (Kabsch-aligned)
rmsd = ciffy.rmsd(polymer1, polymer2)
rmsd = ciffy.rmsd(polymer1, polymer2, scale=ciffy.CHAIN)  # Per-chain

# Also works on raw coordinates
rmsd = ciffy.rmsd(coords1, coords2)

# TM-score
tm = ciffy.tm_score(pred, ref)

# lDDT
lddt = ciffy.lddt(pred, ref)

# Radius of gyration
rg = ciffy.rg(polymer)

# Clash detection
clashes = ciffy.clashes(polymer)

Command Line Interface

# View structure summary
ciffy info structure.cif

# Show sequences
ciffy info structure.cif --sequence

# Show entity descriptions
ciffy info structure.cif --desc

# Multiple files
ciffy info *.cif

Testing

pytest tests/

Contributing

See CONTRIBUTING.md for development setup and guidelines.

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

ciffy-1.0.0.tar.gz (703.3 kB view details)

Uploaded Source

Built Distributions

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

ciffy-1.0.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (961.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

ciffy-1.0.0-cp312-cp312-macosx_11_0_arm64.whl (507.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

ciffy-1.0.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (960.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

ciffy-1.0.0-cp311-cp311-macosx_11_0_arm64.whl (507.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

ciffy-1.0.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (958.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

ciffy-1.0.0-cp310-cp310-macosx_11_0_arm64.whl (507.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

ciffy-1.0.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (958.6 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

ciffy-1.0.0-cp39-cp39-macosx_11_0_arm64.whl (507.9 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

Details for the file ciffy-1.0.0.tar.gz.

File metadata

  • Download URL: ciffy-1.0.0.tar.gz
  • Upload date:
  • Size: 703.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ciffy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 36482ce5057c4f2e8bcfcdf20cad14f2991da294c79519babb01c9efa23aa637
MD5 40cd29a4dde0add24ce2b10ed931cff8
BLAKE2b-256 a1e5600fe81b96741b1d81286f7ad24eefc5f814e99957e4ab560da6e4dc3b32

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.0.tar.gz:

Publisher: pypi.yml on hmblair/ciffy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciffy-1.0.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ciffy-1.0.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f1e8a112d36a9ec07cf98ee611ab0153b8c735d22ccc33a13343ae2bd4559887
MD5 5cb4391b8d1e359ecccf201e709dd9c8
BLAKE2b-256 d16bdf7d5fab8107e1081a714f1ee1304102578c07c1d294b5c9541602821c86

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:

Publisher: pypi.yml on hmblair/ciffy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciffy-1.0.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ciffy-1.0.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5a464f0fe1f56189319edb9b58e753e074b782c58e8e203772b1cf366a31fbf5
MD5 766445f4894f084c97d314764de3beeb
BLAKE2b-256 da21be55d47183be1546ae17f5003a151c3d025ff95f20c7a2e273456db3cb35

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: pypi.yml on hmblair/ciffy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciffy-1.0.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ciffy-1.0.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0e8d77e5625c5d9394e285425451e3d0ce82bfab667f0f475d68d05445c6eb41
MD5 44feecb287cee826bc69630c25ff1d74
BLAKE2b-256 c75a65d711cbcd0a332aac7d972c497cc3df6e2d76ad930c1d7f34bc89e1dfc2

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:

Publisher: pypi.yml on hmblair/ciffy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciffy-1.0.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ciffy-1.0.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 466a55d0105615cb04ac3c8933b7434fdd0d0903b614ed2fe1335d7a9f0ab0ab
MD5 0c9734e034cf7ba335bca80a37d47c59
BLAKE2b-256 47c55149ad5ea2d4cc6d92c038b1afa0dd7f1ec37ed7c79386d8a758b6f34da9

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: pypi.yml on hmblair/ciffy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciffy-1.0.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ciffy-1.0.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d17f00bcefefb13748ac5632fc4ccdf41975a6d003b0e3d9c313bbcf7dbcde6c
MD5 ada04b2a0f01aa89162e46c03e1c8683
BLAKE2b-256 8ab06a2bc1b30aa6664f24c7d89385a5b0540fde437e8aa2d7c02b1ad39b988b

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:

Publisher: pypi.yml on hmblair/ciffy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciffy-1.0.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ciffy-1.0.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3972a39b96b9bfd55eaf762e853ab0ae388d7b7fa383022f1c5fa111f98aec78
MD5 8f235e94b371af2b98318e95bcdbc3b2
BLAKE2b-256 59df837a6ad4e06ff1abe27019441a51906326161437369b30a626a6a4ca7798

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.0-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: pypi.yml on hmblair/ciffy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciffy-1.0.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ciffy-1.0.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2c2aa40c41feac7d31db13f916112ccb6dd14e511547faeb44c8c0aa98459df4
MD5 812bee2121618c5855c13518c7a48097
BLAKE2b-256 83f5d5ab73d3ac9413db4fb3990988bffdd96e0310e222a3bcd549c89111f925

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:

Publisher: pypi.yml on hmblair/ciffy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciffy-1.0.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

  • Download URL: ciffy-1.0.0-cp39-cp39-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 507.9 kB
  • Tags: CPython 3.9, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ciffy-1.0.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4ecb21486530f0b9c87e1d82ca9b352206eaae3a1757527f011987c64e8e3635
MD5 a84f59f5566e8e63a449bc7cc87d5cac
BLAKE2b-256 8cb38b44dc4e2a07af2fed41abee5bacb0ff5cb534ad15b2c1a323f7fb0c783a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.0-cp39-cp39-macosx_11_0_arm64.whl:

Publisher: pypi.yml on hmblair/ciffy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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