Skip to content

Commit 27929e0

Browse files
Merge pull request #493 from ale94mleon/fix/temporal-top
Corrected _temp_top.top behaviour to accommodate asynchronous scenarios
2 parents 2b7a8be + ef4bf3e commit 27929e0

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

GMXMMPBSA/make_top.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import os
2424
import platform
2525
import textwrap
26+
import tempfile
2627

2728
import parmed
2829
from GMXMMPBSA.exceptions import *
@@ -822,36 +823,34 @@ def cleantop(top_file, ndx, id='complex'):
822823
top_file = Path(top_file)
823824
molsect = False
824825

825-
ttp_file = top_file.parent.joinpath('_temp_top.top')
826-
temp_top = ttp_file.open(mode='w')
827-
# temp_top.write('; Modified by gmx_MMPBSA\n')
828-
# TODO: keep solvent when n-wat is implemented
829-
with open(top_file) as topf:
830-
for line in topf:
831-
if '[ molecules ]' in line:
832-
molsect = True
833-
if molsect:
834-
# not copy ions and solvent
835-
sol_ion = [
836-
# standard gmx form
837-
'NA', 'CL', 'SOL', 'K'
838-
# charmm-GUI form ??
839-
'SOD', 'Na+', 'CLA', 'Cl-', 'POT', 'K+',
840-
'TIP3P', 'TIP3', 'TP3', 'TIPS3P', 'TIP3o',
841-
'TIP4P', 'TIP4PEW', 'T4E', 'TIP4PD',
842-
'TIP5P',
843-
'SPC', 'SPC/E', 'SPCE',
844-
'WAT',
845-
'OPC']
846-
if not line.split():
847-
continue
848-
if line.split()[0].strip() in sol_ion:
849-
continue
850-
temp_top.write(line)
851-
temp_top.close()
826+
with tempfile.NamedTemporaryFile(dir=top_file.parent, prefix='_temp_top', suffix='.top', mode='w', delete=False) as temp_top:
827+
# temp_top.write('; Modified by gmx_MMPBSA\n')
828+
# TODO: keep solvent when n-wat is implemented
829+
with open(top_file) as topf:
830+
for line in topf:
831+
if '[ molecules ]' in line:
832+
molsect = True
833+
if molsect:
834+
# not copy ions and solvent
835+
sol_ion = [
836+
# standard gmx form
837+
'NA', 'CL', 'SOL', 'K'
838+
# charmm-GUI form ??
839+
'SOD', 'Na+', 'CLA', 'Cl-', 'POT', 'K+',
840+
'TIP3P', 'TIP3', 'TP3', 'TIPS3P', 'TIP3o',
841+
'TIP4P', 'TIP4PEW', 'T4E', 'TIP4PD',
842+
'TIP5P',
843+
'SPC', 'SPC/E', 'SPCE',
844+
'WAT',
845+
'OPC']
846+
if not line.split():
847+
continue
848+
if line.split()[0].strip() in sol_ion:
849+
continue
850+
temp_top.write(line)
852851

853852
# read the temp topology with parmed
854-
rtemp_top = parmed.gromacs.GromacsTopologyFile(ttp_file.as_posix())
853+
rtemp_top = parmed.gromacs.GromacsTopologyFile(temp_top.name)
855854
# get the residues in the top from the com_ndx
856855
res_list = []
857856

@@ -866,7 +865,9 @@ def cleantop(top_file, ndx, id='complex'):
866865

867866
ranges = list2range(res_list)
868867
rtemp_top.strip(f"!:{','.join(ranges['string'])}")
869-
ttp_file.unlink()
868+
869+
# Clean temporal file
870+
Path(temp_top.name).unlink()
870871
return rtemp_top
871872

872873
def get_masks(self):

0 commit comments

Comments
 (0)