Mercurial > repos > chemteam > gmx_restraints
comparison merge_top.py @ 0:72c2b7992c26 draft
"planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit a71d03531d57b87b5e3ce40ee7d974fbe53a1dfa"
author | chemteam |
---|---|
date | Wed, 20 Nov 2019 11:14:52 -0500 |
parents | |
children | 42e4eda6eb5d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:72c2b7992c26 |
---|---|
1 import re | |
2 import sys | |
3 | |
4 | |
5 def combine_tops(top_text, itp_texts): | |
6 """ | |
7 Search through parent topology top_text and replace | |
8 #include lines with the relevant child topologies | |
9 from the dictionary itp_texts | |
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 | |
17 | |
18 top = sys.argv[1] # parent topology file | |
19 itps_file = sys.argv[2] # file with list of child topologies (.itp files) | |
20 | |
21 with open(itps_file) as f: | |
22 itps = f.read().split() | |
23 | |
24 with open(top, 'r') as f: | |
25 top_text = f.read() | |
26 | |
27 itp_texts = {} # create dictionary of child topologies | |
28 for itp in itps: | |
29 with open(itp, 'r') as f: | |
30 itp_texts[itp] = f.read() | |
31 | |
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 | |
36 with open('top_output.top', 'w') as f: | |
37 # now combine all children into the parent | |
38 f.write(combine_tops(top_text, itp_texts)) |