23
23
import os
24
24
import platform
25
25
import textwrap
26
+ import tempfile
26
27
27
28
import parmed
28
29
from GMXMMPBSA .exceptions import *
@@ -822,36 +823,34 @@ def cleantop(top_file, ndx, id='complex'):
822
823
top_file = Path (top_file )
823
824
molsect = False
824
825
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 )
852
851
853
852
# 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 )
855
854
# get the residues in the top from the com_ndx
856
855
res_list = []
857
856
@@ -866,7 +865,9 @@ def cleantop(top_file, ndx, id='complex'):
866
865
867
866
ranges = list2range (res_list )
868
867
rtemp_top .strip (f"!:{ ',' .join (ranges ['string' ])} " )
869
- ttp_file .unlink ()
868
+
869
+ # Clean temporal file
870
+ Path (temp_top .name ).unlink ()
870
871
return rtemp_top
871
872
872
873
def get_masks (self ):
0 commit comments