Skip to content

Commit a49efd7

Browse files
committed
Cleaned up getEnergy.py.
1 parent 7ada264 commit a49efd7

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

src/MolecularDockingKit/getEnergy.py

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,58 @@
1-
import os, glob, re, sys
1+
import os
22
import numpy as np
33

4-
#######################################################################
5-
# #
6-
# This script extracts the energies for the three components required #
7-
# in the binding energy calculations, which are (protein + ligand), #
8-
# protein, and ligand energies. It calculates the binding energies #
9-
# for all the drug molecules and prints them in kcal/mol. #
10-
# #
11-
#######################################################################
12-
13-
14-
os.chdir('../../moleculeLists')
15-
molList = open('fileList.txt', 'r').readlines()
16-
molList = [mol.strip() for mol in molList]
17-
os.chdir('../../../')
18-
19-
e1 = np.loadtxt('energy_protein/energies.txt')
20-
21-
os.chdir('rDock_inputs')
22-
for mol in molList:
23-
os.chdir(mol)
24-
try:
25-
eCombined = np.loadtxt('energies.txt')
26-
print("%s, %3f, %3f, %3f, %3f, %3f" % (mol, eCombined[0], eCombined[1], e1, (eCombined[0] - eCombined[1] - e1), (eCombined[0] - eCombined[1] - e1)*627.503))
27-
os.chdir('../../../')
28-
except:
29-
os.chdir('../../../')
30-
continue
31-
print(mol, 'skipped due to some error')
32-
os.chdir('../../../')
4+
def calculate_binding_energy(mol_list: list[str], protein_energy: float) -> None:
5+
"""
6+
Extracts the energies for the three components required in the binding energy calculations,
7+
which are (protein + ligand), protein, and ligand energies. It calculates the binding energies
8+
for all the drug molecules and prints them in kcal/mol.
9+
10+
Args:
11+
mol_list (list[str]): List of molecules.
12+
protein_energy (float): Energy of the protein component.
13+
14+
Returns:
15+
None
16+
"""
17+
# Change directory to moleculeLists to access fileList.txt
18+
os.chdir('../../moleculeLists')
19+
mol_list = open('fileList.txt', 'r').readlines()
20+
mol_list = [mol.strip() for mol in mol_list]
21+
os.chdir('../../../')
22+
23+
e_protein = np.loadtxt('energy_protein/energies.txt')
24+
25+
os.chdir('rDock_inputs')
26+
for mol in mol_list:
27+
os.chdir(mol)
28+
try:
29+
e_combined = np.loadtxt('energies.txt')
30+
ligand_energy, binding_energy = calculate_energy_components(e_combined, e_protein)
31+
print(f"{mol}, {e_combined[0]}, {e_combined[1]}, {e_protein}, {binding_energy}, {binding_energy * 627.503}")
32+
os.chdir('../../../')
33+
except Exception as e:
34+
os.chdir('../../../')
35+
print(f"{mol} skipped due to error: {e}")
36+
continue
37+
os.chdir('../../../')
38+
39+
def calculate_energy_components(e_combined: np.ndarray, e_protein: float) -> tuple[float, float]:
40+
"""
41+
Calculates the energy of the ligand and binding energy.
42+
43+
Args:
44+
e_combined (np.ndarray): Array containing energies for (protein + ligand).
45+
e_protein (float): Energy of the protein component.
46+
47+
Returns:
48+
Tuple containing ligand energy and binding energy.
49+
"""
50+
ligand_energy = e_combined[0] - e_combined[1]
51+
binding_energy = ligand_energy - e_protein
52+
return ligand_energy, binding_energy
53+
54+
# Example usage
55+
if __name__ == "__main__":
56+
molList = ['mol1', 'mol2'] # Example list of molecules
57+
proteinEnergy = 10.0 # Example protein energy
58+
calculate_binding_energy(molList, proteinEnergy)

0 commit comments

Comments
 (0)