diff merge_top.py @ 0:33ed3c26b8c2 draft

planemo upload for repository https://github.com/galaxycomputationalchemistry/galaxy-tools-compchem/tools/gromacs commit 7ba07c3130a2170e8f91acc4a81f5626563a79eb
author chemteam
date Thu, 28 Mar 2019 10:12:00 -0400
parents
children 06ea4e040d45
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/merge_top.py	Thu Mar 28 10:12:00 2019 -0400
@@ -0,0 +1,38 @@
+import re
+import sys
+
+
+def combine_tops(top_text, itp_texts):
+    """
+    Search through parent topology top_text and replace
+    #include lines with the relevant child topologies
+    from the dictionary itp_texts
+    """
+    for itp in itp_texts:
+        # split on include string, then rejoin around itp file
+        spl = re.split('#include ".*{}"\n'.format(itp), top_text)
+        top_text = itp_texts[itp].join(spl)
+    return top_text
+
+
+top = sys.argv[1]  # parent topology file
+itps_file = sys.argv[2]  # file with list of child topologies (.itp files)
+
+with open(itps_file) as f:
+    itps = f.read().split()
+
+with open(top, 'r') as f:
+    top_text = f.read()
+
+itp_texts = {}  # create dictionary of child topologies
+for itp in itps:
+    with open(itp, 'r') as f:
+        itp_texts[itp] = f.read()
+
+for itp in itp_texts:
+    # child tops may also refer to each other; we need to check this
+    itp_texts[itp] = combine_tops(itp_texts[itp], itp_texts)
+
+with open('top_output.top', 'w') as f:
+    # now combine all children into the parent
+    f.write(combine_tops(top_text, itp_texts))