annotate merge_top.py @ 1:9987b0933704 draft

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