Skip to main content

Save and load common biology file formats

Project description

Bio Files: Read and write common biology file formats

Crate Docs PyPI DOI

This Rust and Python library contains functionality to load and save data in common biology file formats. It operates on data structures that are specific to each file format; you will need to convert to and from the structures used by your application. The API docs, and examples below are sufficient to get started.

Note: Install the pip version with pip install biology-files due to a name conflict.

Supported formats:

  • mmCIF (Protein atom, residue, chain, and related data like secondary structure)
  • mmCIF (structure factors / 2fo-fc: Electron density data, raw)
  • Mol2 (Small molecules, e.g. ligands)
  • SDF (Small molecules, e.g. ligands)
  • PDBQT (Small molecules, e.g. ligands. Includes docking-specific fields.)
  • Map (Electron density, e.g. from crystallography, Cryo EM. Processed using Fourier transforms)
  • AB1 (Sequence tracing)
  • DAT (Amber force field data for small molecules)
  • FRCMOD (Amber force field patch data for small molecules)
  • Amber .lib files, e.g. with charge data for amino acids and proteins.
  • GRO (Gromacs molecules)
  • TOP (Gromacs topology) - WIP

Planned:

  • MTZ (Exists in Daedalus; needs to be decoupled)
  • DNA (Exists in PlasCAD; needs to be decoupled)

Generic data types

This library includes a number of relatively generic data types which are returned by various load functions, and required to save data. These may be used in your application directly, or converted into a more specific format. Examples:

For Genbank, we recommend gb-io. We do not plan to support this format, due to this high quality library.

Each module represents a file format, and most have dedicated structs dedicated to operating on that format.

It operates using structs with public fields, which you can explore using the API docs, or your IDE. These structs generally include these three methods: new(), save() and load(). new() accepts &str for text files, and a R: Read + Seek for binary. save() and load() accept &Path. The Force Field formats use load_dat, save_frcmod instead, as they use the same structs for both formats.

Serial numbers

Serial numbers for atoms, residues, secondary structure, and chains are generally pulled directly from atom data files (mmCIF, Mol2 etc). These lists reference atoms, or residues, stored as Vec<u32>, with the u32 being the serial number. In your application, you may wish to adapt these generic types to custom ones that use index lookups instead of serial numbers. We use SNs here because they're more robust, and match the input files directly; add optimizations downstream, like converting to indices, and/or applying back-references. (e.g. the index of the residue an atom's in, in your derived Atom struct).

Example use

Small molecule save and load, Python.

from biology_files import Sdf

sdf_data = Sdf.load("./molecules/DB03496.sdf")

sdf_data.atoms[0]
#AtomGeneric { serial_number: 1, posit: Vec3 { x: 2.3974, y: 1.1259, z: 2.5289 }, element: Chlorine, 
type_in_res: None, force_field_type: None, occupancy: None, partial_charge: None, hetero: true }

sdf_data.atoms[0].posit
# [2.3974, 1.1259, 2.5289]

sdf_data.save("test.sdf")

mol2_data = sdf_data.to_mol2()
mol2_data.save("test.mol2")

# Load molecules from databases using identifiers:
mol = Sdf.load_drugbank("DB00198")
mol = Sdf.load_pubchem(12345)
mol = Sdf.load_pdbe("CPB")
mol = Mol2.load_amber_geostd("CPB")

peptide = MmCif.load_rcsb("8S6P")

# (See the Rust examples and API docs for more functionality; most
# is exposed in Python as well)

Small molecule save and load, Rust.

use bio_files::{Sdf, Mol2};

// ...
let sdf_data = Sdf::load("./molecules/DB03496.sdf");

sdf_data.atoms[0]; // (as above)
sdf_data.atoms[0].posit;  // (as above, but lin_alg::Vec3))

sdf_data.save("test.sdf");

let mol2_data: Mol2 = sdf_data.into();
mol2_data.save("test.mol2");


// Loading Force field parameters:
let p = Path::new("gaff2.dat")
let params = ForceFieldParams::load_dat(p)?;


// Load electron density structure factors data, to be processed with a FFT:
let p = Path::new("8s6p_validation_2fo-fc_map_coef.cif")
let data = CifStructureFactors::new_from_path(path)?;

// These functions aren't included; an example of turning loaded structure factor data
// into a density map.
let mut fft_planner = FftPlanner::new();
let dm = density_map_from_mmcif(&data, &mut fft_planner)?;

// Or if you have a Map file:
let p = Path::new("8s6p.map")
let dm = DensityMap::load(path)?;


// Load molecules from databases using identifiers:
let mol = Sdf::load_drugbank("DB00198")?;
let mol = Sdf::load_pubchem(12345)?;
let mol = Sdf::load_pdbe("CPB")?;
let mol = Mol2::load_amber_geostd("CPB")?;

let peptide = MmCif::load_rcsb("8S6P")?;

You can use similar syntax for mmCIF protein files.

Amber force fields

Reference the Amber 2025 Reference Manual, section 15 for details on how we parse its files, and how to use the results. In some cases, we change the format from the raw Amber data. For example, we store angles as radians (vice degrees), and σ vice R_min for Van der Waals parameters. Structs and fields are documented with reference manual references.

The Amber forcefield parameter format has fields which each contain a Vec of a certain type of data. (Bond stretching parameters, angle between 3 atoms, torsion/dihedral angles etc.) You may wish to parse these into a format that has faster lookups for your application.

Note that the above examples expect that your application has a struct representing the molecule that has From<Mol2>, and to_mol2(&self) (etc) methods. The details of these depend on the application. For example:

impl From<Sdf> for Molecule {
    fn from(m: Sdf) -> Self {
        // We've implemented `From<AtomGeneric>` and `From<ResidueGeneric>` for our application's `Atom` and
        // `Residue`
        let atoms = m.atoms.iter().map(|a| a.into()).collect();
        let residues = m.residues.iter().map(|r| r.into()).collect();

        Self::new(m.ident, atoms, m.chains.clone(), residues, None, None);
    }
}

A practical example of parsing a molecule from a mmCIF as parsed from bio_files into an application-specific format:

fn load() {
    let cif_data = mmcif::load("./1htm.cif");
    let mol: Molecule = cif_data.try_into().unwrap();
}

impl TryFrom<MmCif> for Molecule {
    type Error = io::Error;

    fn try_from(m: MmCif) -> Result<Self, Self::Error> {
        let mut atoms: Vec<_> = m.atoms.iter().map(|a| a.into()).collect();

        let mut residues = Vec::with_capacity(m.residues.len());
        for res in &m.residues {
            residues.push(Residue::from_generic(res, &atoms)?);
        }

        let mut chains = Vec::with_capacity(m.chains.len());
        for c in &m.chains {
            chains.push(Chain::from_generic(c, &atoms, &residues)?);
        }

        // Now that chains and residues are loaded, update atoms with their back-ref index.
        for atom in &mut atoms {
            for (i, res) in residues.iter().enumerate() {
                if res.atom_sns.contains(&atom.serial_number) {
                    atom.residue = Some(i);
                    break;
                }
            }

            for (i, chain) in chains.iter().enumerate() {
                if chain.atom_sns.contains(&atom.serial_number) {
                    atom.chain = Some(i);
                    break;
                }
            }
        }

        let mut result = Self::new(m.ident.clone(), atoms, chains, residues, None, None);

        result.experimental_method = m.experimental_method.clone();
        result.secondary_structure = m.secondary_structure.clone();

        result.bonds_hydrogen = Vec::new();
        result.adjacency_list = result.build_adjacency_list();

        Ok(result)
    }
}

A protein loading and prep example:

Python:

use biology_files::{Mol2, MmCif, ForceFieldParams, FfParamSet, prepare_peptide, load_prmtop};

mol = Mol2.load("CPB.mol2")
protein = MmCif.load("1c8k.cif")

param_set = FfParamSet.new_amber()
lig_specific = ForceFieldParams.load_frcmod("CPB.frcmod")

# Or, instead of loading atoms and mol-specific params separately:
# mol, lig_specific = load_prmtop("my_mol.prmtop")

# Add Hydrogens, force field type, and partial charge to atoms in the protein; these usually aren't
# included from RSCB PDB. You can also call `populate_hydrogens_dihedrals()`, and
# `populate_peptide_ff_and_q() separately. Add bonds.
protein.atoms, protein.bonds = prepare_peptide(
    protein.atoms,
    protein.bonds,
    protein.residues,
    protein.chains,
    param_set.peptide_ff_q_map,
    7.0,
)

Rust:

use bio_files::{MmCif, Mol2, ForceFieldParams, FfParamSet, prepare_peptide, load_prmtop};
use std::path::Path;

fn load() {
    let param_set = FfParamSet::new_amber().unwrap();

    let mut protein = MmCif::load(Path::new("1c8k.cif")).unwrap();
    let mol = Mol2::load(Path::new("CPB.mol2")).unwrap();
    let mol_specific = ForceFieldParams::load_frcmod(Path::new("CPB.frcmod")).unwrap();

    // Or, instead of loading atoms and mol-specific params separately:
    // let (mol, lig_specific) = load_prmtop("my_mol.prmtop");

    // Or, if you have a small molecule available in Amber Geostd, load it remotely:
    // let data = bio_apis::amber_geostd::load_mol_files("CPB");
    // let mol = Mol2::new(&data.mol2);
    // let mol_specific = ForceFieldParams::from_frcmod(&data.frcmod);

    // Add Hydrogens, force field type, and partial charge to atoms in the protein; these usually aren't
    // included from RSCB PDB. You can also call `populate_hydrogens_dihedrals()`, and
    // `populate_peptide_ff_and_q() separately. Add bonds.
    prepare_peptide(
        &mut protein.atoms,
        &mut protein.bonds,
        &mut protein.residues,
        &mut protein.chains,
        &param_set.peptide_ff_q_map.as_ref().unwrap(),
        7.0,
    )
        .unwrap();
}

Note: The Python version is currently missing support for some formats, and not all fields are exposed.

References

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

biology_files-0.3.4.tar.gz (94.6 kB view details)

Uploaded Source

Built Distributions

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

biology_files-0.3.4-cp310-abi3-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.10+Windows x86-64

biology_files-0.3.4-cp310-abi3-musllinux_1_2_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

biology_files-0.3.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB view details)

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

biology_files-0.3.4-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (2.4 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ s390x

biology_files-0.3.4-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (2.9 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

biology_files-0.3.4-cp310-abi3-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

biology_files-0.3.4-cp310-abi3-macosx_10_12_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file biology_files-0.3.4.tar.gz.

File metadata

  • Download URL: biology_files-0.3.4.tar.gz
  • Upload date:
  • Size: 94.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for biology_files-0.3.4.tar.gz
Algorithm Hash digest
SHA256 e7839a909d1d386ee62cb0d518b3971d75eb6d12927dda2833789df62ef508d3
MD5 ee043e000dfb759b47b4e4ec4268ccb6
BLAKE2b-256 3924ffa812a4351c08e228560f0301f788806cc0e5ba479d638623cd9034f96b

See more details on using hashes here.

File details

Details for the file biology_files-0.3.4-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for biology_files-0.3.4-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 bbfc96f689e5c1a59a17e0325fa09d54adf2d0484e7d596c4741c4744fe28f3e
MD5 6aa4fc089342764a582616e19e35fb3a
BLAKE2b-256 08a2570c946bcfb8941e176d9179b730facccf2f39cc4a630331dddaa5a27d9a

See more details on using hashes here.

File details

Details for the file biology_files-0.3.4-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for biology_files-0.3.4-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 908c512cb394f3ff4255ae6147c4d5b22f0d0509b736467b8fe8a29267ec23aa
MD5 ffe0af834ba44cc504232a22db9df6a6
BLAKE2b-256 5965c121ce8db71c253d75a32eab5047fe87f41ae43a358fcf152bfd85cf7505

See more details on using hashes here.

File details

Details for the file biology_files-0.3.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for biology_files-0.3.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cd1de969d9ec1869b7096eb8f3af640f92766f0ac78e1799d0f0803e8f736de6
MD5 d8542e723ee901cb06ee6ebac89a27a3
BLAKE2b-256 b1355f898e8f69ccd0e38ef04a9d81b80336cea861d7ed0a4e4ca30c886046d9

See more details on using hashes here.

File details

Details for the file biology_files-0.3.4-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for biology_files-0.3.4-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 c7818ac8439b3eca5ebb0ae88a3cf937f9bb741ebe63ca660a51690ef4fb373b
MD5 351f0c2db9fcd28113b946e6bddf04b8
BLAKE2b-256 ed3cb3c90cf7ef6821a6dae32922621203fbe38a08ba15f0129803e0e26aa660

See more details on using hashes here.

File details

Details for the file biology_files-0.3.4-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for biology_files-0.3.4-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 fc62d74658fb09fa4768298028ff7be52d551730653b9884ff275abca89c7ff6
MD5 c9b20e45efad06e82a9c2d5a62c441e4
BLAKE2b-256 4357ac3234777f38655002df9ee659d29f6b625057c8f647904e3822f503c5d6

See more details on using hashes here.

File details

Details for the file biology_files-0.3.4-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for biology_files-0.3.4-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 90367861db0f1edb17ca871fb7bcee19b0c547dbb94595a095b39bab83709607
MD5 04bdc66c3aac525ec52f0b2b52b1e527
BLAKE2b-256 21b1a7988b5349dce058d99cf4cbba5200efe37349e5bdba8137716e119e566f

See more details on using hashes here.

File details

Details for the file biology_files-0.3.4-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for biology_files-0.3.4-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 194bdaa4f02e1546ad52d6b08c0308df8b2d59069d11536231a6710f23dd1ee7
MD5 b972de713b56ef8f52209fa11107b3bb
BLAKE2b-256 06fa2f21318731adf27c1bf13890053338ba8a4455f5e7cee4d3f172de29ae51

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