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:

Polymer 9GCM [2024-08-02]
─────────────────────────
   Type     Res  Atoms
─────────────────────────
A  RNA      135   1413
B  PROTEIN  132   1032
C  PROTEIN  246   1261
D  PROTEIN  485    760
─────────────────────────
Σ  4        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.3.tar.gz (870.0 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.3-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (977.9 kB view details)

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

ciffy-1.0.3-cp312-cp312-macosx_11_0_arm64.whl (517.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

ciffy-1.0.3-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (975.6 kB view details)

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

ciffy-1.0.3-cp311-cp311-macosx_11_0_arm64.whl (517.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

ciffy-1.0.3-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (974.0 kB view details)

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

ciffy-1.0.3-cp310-cp310-macosx_11_0_arm64.whl (517.2 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

ciffy-1.0.3-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (973.9 kB view details)

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

ciffy-1.0.3-cp39-cp39-macosx_11_0_arm64.whl (517.4 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: ciffy-1.0.3.tar.gz
  • Upload date:
  • Size: 870.0 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.3.tar.gz
Algorithm Hash digest
SHA256 41fbc9e41355a8bc5e5ecf1b36d8fcb9a85d3722a58ea4890a54d335ea8eec42
MD5 ad1d6a97181650508cf776f6e28bb241
BLAKE2b-256 6cc04c5c196201b86002b9f5e89353184283d8ca43b5974d8bc1a660216f68fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.3.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.3-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.3-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fc4fe141c00addbb8410865008cac800864ff168159c3a9d07eadd7b3f644c28
MD5 5eb36dd6abe1daa5b3be7c4d2ceedf31
BLAKE2b-256 0982161c1cccac3f36af692e489ed007c1993db435715912196bc64b8fb22fdf

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.3-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.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ciffy-1.0.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1257e820a21273062c14016e591991829fd9711a522d8c371b54cf8e0bd0764f
MD5 0417ed825b3ad2d41b788ea640e048ec
BLAKE2b-256 c71bfebf3b10b067a9adeaa425c9d95a6440143b4bf20be10ac2fc8d5ab6b288

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.3-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.3-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.3-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d3e69d3d2d4e1d83c622b6887e3f01278d17ab5ddf27cc0f32c7511a8bf5db2f
MD5 2d39d500de6108be6ed7ca1115066d3b
BLAKE2b-256 f6ce481fe69f28277ebc16625d631b5db85b1523d6295405cebf3224781bfd28

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.3-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.3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ciffy-1.0.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bf04d8d9d02ca57d81a3ae83f97c6d0835d14a3eea52cf4cdeca9e7e3b840dcf
MD5 7e48f962cfb0d9a05878a63acf089e95
BLAKE2b-256 aea2448c775f4efa87f79364617def85f14a5770ac5e9a2b7877d6c3d69a7faf

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.3-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.3-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.3-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5cfca9401852f60847caefc35f5dfda97077e30d8c24477ba2b268ffc59e2c00
MD5 b0ba7589f7fb121850656e1cec3392df
BLAKE2b-256 eb5c17ee3e7cc13c8ebeb9344d0f776eb26b11b0c50976e79f2fc941a68d0b20

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.3-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.3-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ciffy-1.0.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ccf039e8643b523e9e5b8f39a8858d3905c6c7dbdf9cd3a46fc78bfa9094892d
MD5 5a6c66b05608548a1e9933769ae7f2dd
BLAKE2b-256 03a66cb51375932ce48f1d052acdeeda37c3cb623d9786814fb004889e3c2c3f

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.3-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.3-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.3-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 851a03f730e0397a5cee4fa9aacc54d3e811964853ca8b2c4fd1d0351b0841eb
MD5 a97a4aca04ed1653cf61522753a655fa
BLAKE2b-256 cb0da5c02faf8cfee8e7582f994d5b209c2d2c0b0ba48d5a168b99468b16e929

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.3-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.3-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

  • Download URL: ciffy-1.0.3-cp39-cp39-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 517.4 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.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0565e65254ecaff3729fa9ca08550215aa317c75836fc1c4e1b6d16e21d3fbb2
MD5 ee9c7f6e8d68ab10cb32c89f8b1267c2
BLAKE2b-256 6b5ce4257c6e90a69ab8fe7437a5cae360738e23cc3c3a885be2bfe5b8a2220c

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciffy-1.0.3-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