From 472269bc7eac5056181b899d5ea64abfcb5fb496 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Leon Date: Wed, 1 May 2024 18:19:39 +0200 Subject: [PATCH 1/3] Corrected _temp_top.top behavior to accommodate asynchronous scenarios --- GMXMMPBSA/make_top.py | 58 +++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/GMXMMPBSA/make_top.py b/GMXMMPBSA/make_top.py index 686ae16a..2fcbe12f 100644 --- a/GMXMMPBSA/make_top.py +++ b/GMXMMPBSA/make_top.py @@ -23,6 +23,7 @@ import os import platform import textwrap +import tempfile import parmed from GMXMMPBSA.exceptions import * @@ -822,36 +823,34 @@ def cleantop(top_file, ndx, id='complex'): top_file = Path(top_file) molsect = False - ttp_file = top_file.parent.joinpath('_temp_top.top') - temp_top = ttp_file.open(mode='w') - # temp_top.write('; Modified by gmx_MMPBSA\n') - # TODO: keep solvent when n-wat is implemented - with open(top_file) as topf: - for line in topf: - if '[ molecules ]' in line: - molsect = True - if molsect: - # not copy ions and solvent - sol_ion = [ - # standard gmx form - 'NA', 'CL', 'SOL', 'K' - # charmm-GUI form ?? - 'SOD', 'Na+', 'CLA', 'Cl-', 'POT', 'K+', - 'TIP3P', 'TIP3', 'TP3', 'TIPS3P', 'TIP3o', - 'TIP4P', 'TIP4PEW', 'T4E', 'TIP4PD', - 'TIP5P', - 'SPC', 'SPC/E', 'SPCE', - 'WAT', - 'OPC'] - if not line.split(): - continue - if line.split()[0].strip() in sol_ion: - continue - temp_top.write(line) - temp_top.close() + with tempfile.NamedTemporaryFile(dir=top_file.parent, prefix='_temp_top', suffix='.top', mode='w') as temp_top: + # temp_top.write('; Modified by gmx_MMPBSA\n') + # TODO: keep solvent when n-wat is implemented + with open(top_file) as topf: + for line in topf: + if '[ molecules ]' in line: + molsect = True + if molsect: + # not copy ions and solvent + sol_ion = [ + # standard gmx form + 'NA', 'CL', 'SOL', 'K' + # charmm-GUI form ?? + 'SOD', 'Na+', 'CLA', 'Cl-', 'POT', 'K+', + 'TIP3P', 'TIP3', 'TP3', 'TIPS3P', 'TIP3o', + 'TIP4P', 'TIP4PEW', 'T4E', 'TIP4PD', + 'TIP5P', + 'SPC', 'SPC/E', 'SPCE', + 'WAT', + 'OPC'] + if not line.split(): + continue + if line.split()[0].strip() in sol_ion: + continue + temp_top.write(line) - # read the temp topology with parmed - rtemp_top = parmed.gromacs.GromacsTopologyFile(ttp_file.as_posix()) + # read the temp topology with parmed + rtemp_top = parmed.gromacs.GromacsTopologyFile(temp_top.name) # get the residues in the top from the com_ndx res_list = [] @@ -866,7 +865,6 @@ def cleantop(top_file, ndx, id='complex'): ranges = list2range(res_list) rtemp_top.strip(f"!:{','.join(ranges['string'])}") - ttp_file.unlink() return rtemp_top def get_masks(self): From 34603a3e6dc91ed2f65486343f1ff2a1bc9bbc88 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Leon Date: Wed, 1 May 2024 21:05:40 +0200 Subject: [PATCH 2/3] Fix mode on temporal file --- GMXMMPBSA/make_top.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GMXMMPBSA/make_top.py b/GMXMMPBSA/make_top.py index 2fcbe12f..a0c95411 100644 --- a/GMXMMPBSA/make_top.py +++ b/GMXMMPBSA/make_top.py @@ -823,7 +823,7 @@ def cleantop(top_file, ndx, id='complex'): top_file = Path(top_file) molsect = False - with tempfile.NamedTemporaryFile(dir=top_file.parent, prefix='_temp_top', suffix='.top', mode='w') as temp_top: + with tempfile.NamedTemporaryFile(dir=top_file.parent, prefix='_temp_top', suffix='.top', mode='w+') as temp_top: # temp_top.write('; Modified by gmx_MMPBSA\n') # TODO: keep solvent when n-wat is implemented with open(top_file) as topf: From ef4bf3e4ac7663158b0acdc49800577ac0bb0b64 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Leon Date: Wed, 1 May 2024 23:44:18 +0200 Subject: [PATCH 3/3] Fix parmed reading of temporal file --- GMXMMPBSA/make_top.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/GMXMMPBSA/make_top.py b/GMXMMPBSA/make_top.py index a0c95411..9fabb38f 100644 --- a/GMXMMPBSA/make_top.py +++ b/GMXMMPBSA/make_top.py @@ -823,7 +823,7 @@ def cleantop(top_file, ndx, id='complex'): top_file = Path(top_file) molsect = False - with tempfile.NamedTemporaryFile(dir=top_file.parent, prefix='_temp_top', suffix='.top', mode='w+') as temp_top: + with tempfile.NamedTemporaryFile(dir=top_file.parent, prefix='_temp_top', suffix='.top', mode='w', delete=False) as temp_top: # temp_top.write('; Modified by gmx_MMPBSA\n') # TODO: keep solvent when n-wat is implemented with open(top_file) as topf: @@ -849,8 +849,8 @@ def cleantop(top_file, ndx, id='complex'): continue temp_top.write(line) - # read the temp topology with parmed - rtemp_top = parmed.gromacs.GromacsTopologyFile(temp_top.name) + # read the temp topology with parmed + rtemp_top = parmed.gromacs.GromacsTopologyFile(temp_top.name) # get the residues in the top from the com_ndx res_list = [] @@ -865,6 +865,9 @@ def cleantop(top_file, ndx, id='complex'): ranges = list2range(res_list) rtemp_top.strip(f"!:{','.join(ranges['string'])}") + + # Clean temporal file + Path(temp_top.name).unlink() return rtemp_top def get_masks(self):