annotate merge_top.py @ 0:bb0053c4e4f2 draft

"planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
author chemteam
date Tue, 21 Jan 2020 07:29:43 -0500
parents
children b2acdbff8dfb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
1 import re
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
2 import sys
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
3
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
4
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
5 def combine_tops(top_text, itp_texts):
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
6 """
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
7 Search through parent topology top_text and replace
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
8 #include lines with the relevant child topologies
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
9 from the dictionary itp_texts
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
10 """
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
11 for itp in itp_texts:
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
12 # split on include string, then rejoin around itp file
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
13 spl = re.split('#include ".*{}"\n'.format(itp), top_text)
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
14 top_text = itp_texts[itp].join(spl)
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
15 return top_text
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
16
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
17
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
18 top = sys.argv[1] # parent topology file
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
19 itps_file = sys.argv[2] # file with list of child topologies (.itp files)
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
20
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
21 with open(itps_file) as f:
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
22 itps = f.read().split()
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
23
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
24 with open(top, 'r') as f:
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
25 top_text = f.read()
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
26
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
27 itp_texts = {} # create dictionary of child topologies
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
28 for itp in itps:
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
29 with open(itp, 'r') as f:
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
30 itp_texts[itp] = f.read()
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
31
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
32 for itp in itp_texts:
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
33 # child tops may also refer to each other; we need to check this
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
34 itp_texts[itp] = combine_tops(itp_texts[itp], itp_texts)
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
35
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
36 with open('top_output.top', 'w') as f:
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
37 # now combine all children into the parent
bb0053c4e4f2 "planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 03127e495a0a1a022928c1a03527974c9e81b5a1"
chemteam
parents:
diff changeset
38 f.write(combine_tops(top_text, itp_texts))