comparison merge_top.py @ 4:42e4eda6eb5d draft

"planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 71a3084d6e402b31563b1662bb629d5a959ce7b7"
author chemteam
date Wed, 15 Apr 2020 14:30:09 -0400
parents 72c2b7992c26
children
comparison
equal deleted inserted replaced
3:f79f4671eaf2 4:42e4eda6eb5d
1 import re 1 import argparse
2 import sys 2
3 import parmed as pmd
3 4
4 5
5 def combine_tops(top_text, itp_texts): 6 def merge_gro_files(prot_gro, lig_gro, cmplx_gro):
6 """ 7 prot = pmd.load_file(prot_gro)
7 Search through parent topology top_text and replace 8 lig = pmd.load_file(lig_gro)
8 #include lines with the relevant child topologies 9 cmplx = prot + lig
9 from the dictionary itp_texts 10 cmplx.save(cmplx_gro)
10 """
11 for itp in itp_texts:
12 # split on include string, then rejoin around itp file
13 spl = re.split('#include ".*{}"\n'.format(itp), top_text)
14 top_text = itp_texts[itp].join(spl)
15 return top_text
16 11
17 12
18 top = sys.argv[1] # parent topology file 13 def merge_top_files(prot_top, lig_top, cmplx_top):
19 itps_file = sys.argv[2] # file with list of child topologies (.itp files) 14 with open(lig_top, 'r') as f:
15 lig_top_sections = f.read().split('\n[')
20 16
21 with open(itps_file) as f: 17 # open ligand topology
22 itps = f.read().split() 18 for n in range(len(lig_top_sections)):
19 if 'atomtypes' in lig_top_sections[n][:10]:
20 lig_atomtypes = lig_top_sections[n]
21 del lig_top_sections[n]
22 break
23 else:
24 lig_atomtypes = None
25 lig_top_updated = '\n['.join(lig_top_sections)
23 26
24 with open(top, 'r') as f: 27 # open protein topology
25 top_text = f.read() 28 with open(prot_top, 'r') as f:
29 prot_top_combined = f.read()
30 if lig_atomtypes:
31 prot_top_sections = prot_top_combined.split('[ moleculetype ]\n')
32 prot_top_combined = (prot_top_sections[0] +
33 '; Include ligand atomtypes\n[' +
34 lig_atomtypes +
35 '\n[ moleculetype ]\n' +
36 prot_top_sections[1])
37 prot_top_sections = prot_top_combined.split('; Include water topology')
38 prot_top_combined = (prot_top_sections[0] +
39 '; Include ligand topology\n' +
40 lig_top_updated +
41 '\n; Include water topology' +
42 prot_top_sections[1])
43 prot_top_combined += 'base 1\n'
26 44
27 itp_texts = {} # create dictionary of child topologies 45 # save complex topology
28 for itp in itps: 46 with open(cmplx_top, 'w') as f:
29 with open(itp, 'r') as f: 47 f.write(prot_top_combined)
30 itp_texts[itp] = f.read()
31 48
32 for itp in itp_texts:
33 # child tops may also refer to each other; we need to check this
34 itp_texts[itp] = combine_tops(itp_texts[itp], itp_texts)
35 49
36 with open('top_output.top', 'w') as f: 50 def main():
37 # now combine all children into the parent 51 parser = argparse.ArgumentParser(
38 f.write(combine_tops(top_text, itp_texts)) 52 description='Perform SMD runs for dynamic undocking')
53 parser.add_argument('--lig-top', help='Ligand TOP file.')
54 parser.add_argument('--prot-top', help='Protein TOP file.')
55 parser.add_argument('--lig-gro', help='Ligand GRO file.')
56 parser.add_argument('--prot-gro', help='Protein GRO file.')
57 parser.add_argument('--complex-top', help='Complex TOP file.')
58 parser.add_argument('--complex-gro', help='Complex GRO file.')
59 args = parser.parse_args()
60 merge_gro_files(args.prot_gro, args.lig_gro, args.complex_gro)
61 merge_top_files(args.prot_top, args.lig_top, args.complex_top)
62
63
64 if __name__ == "__main__":
65 main()