Coverage for src / molecular_simulations / build / __init__.py: 32%
38 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-12 10:07 -0600
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-12 10:07 -0600
1from pathlib import Path
2from typing import Union
4from .build_amber import ExplicitSolvent, ImplicitSolvent
5from .build_interface import InterfaceBuilder
7try:
8 from .build_ligand import LigandBuilder, PLINDERBuilder, ComplexBuilder
9except ImportError: # no rdkit in environment
10 pass
12PathLike = Union[Path, str]
14def convert_cif_with_biopython(cif: PathLike) -> PathLike:
15 """
16 Helper function to convert a cif file to a pdb file using biopython.
17 """
18 from Bio.PDB import MMCIFParser, PDBIO
20 if not isinstance(cif, Path):
21 cif = Path(cif)
22 pdb = cif.with_suffix('.pdb')
24 parser = MMCIFParser()
25 structure = parser.get_structure('protein', str(cif))
27 io = PDBIO()
28 io.set_structure(structure)
29 io.save(str(pdb))
31 return pdb
33def convert_cif_with_gemmi(cif: PathLike) -> PathLike:
34 import gemmi
35 structure = gemmi.read_structure(str(cif))
36 structure.write(str(cif.with_suffix('.pdb')))
38def add_chains(pdb: PathLike,
39 first_res: int=1,
40 last_res: int=-1) -> PathLike:
41 """
42 Helper function to add chain IDs to a model.
43 """
44 import MDAnalysis as mda
46 u = mda.Universe(pdb)
47 u.add_TopologyAttr('chainID')
49 if last_res == -1:
50 last_res = len(u.n_residues)
52 chain_A = u.select_atoms(f'resid {first_res} to {last_res}')
53 chain_A.atoms.chainIDs = 'A'
55 if last_res != -1:
56 final_res = len(u.n_residues)
58 chain_B = u.select_atoms(f'resid {last_res} to {final_res}')
59 chain_B.atoms.chainIDs = 'B'
61 with mda.Writer(Path(pdb).with_suffix('_withchains.pdb')) as W:
62 W.write(u.atoms)