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

1from pathlib import Path 

2from typing import Union 

3 

4from .build_amber import ExplicitSolvent, ImplicitSolvent 

5from .build_interface import InterfaceBuilder 

6 

7try: 

8 from .build_ligand import LigandBuilder, PLINDERBuilder, ComplexBuilder 

9except ImportError: # no rdkit in environment 

10 pass 

11 

12PathLike = Union[Path, str] 

13 

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 

19 

20 if not isinstance(cif, Path): 

21 cif = Path(cif) 

22 pdb = cif.with_suffix('.pdb') 

23 

24 parser = MMCIFParser() 

25 structure = parser.get_structure('protein', str(cif)) 

26 

27 io = PDBIO() 

28 io.set_structure(structure) 

29 io.save(str(pdb)) 

30 

31 return pdb 

32 

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'))) 

37 

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 

45 

46 u = mda.Universe(pdb) 

47 u.add_TopologyAttr('chainID') 

48 

49 if last_res == -1: 

50 last_res = len(u.n_residues) 

51 

52 chain_A = u.select_atoms(f'resid {first_res} to {last_res}') 

53 chain_A.atoms.chainIDs = 'A' 

54 

55 if last_res != -1: 

56 final_res = len(u.n_residues) 

57 

58 chain_B = u.select_atoms(f'resid {last_res} to {final_res}') 

59 chain_B.atoms.chainIDs = 'B' 

60 

61 with mda.Writer(Path(pdb).with_suffix('_withchains.pdb')) as W: 

62 W.write(u.atoms)