summaryrefslogtreecommitdiff
path: root/sim/src/merge.parameters.py
blob: e452892812a401dbca0b482aef27f9bfd535499d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python
# Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details.

# usage:
#
# merge.parameters.py existingParameterFile newParameterFiles...
#
# creates existingParameterFile.new
#

import sys
import re

leadingWhitespacePattern = re.compile(r"^\s*")
trailingWhitespacePattern = re.compile(r"\s*$")
idPattern = re.compile(r"(\$Id\:.*\$)")
commentPattern = re.compile("#")
firstField = re.compile(r"^(\S+)\s+(.*)")
parameterPattern = re.compile(r"^([^=]+)\s*\=\s*(\S+)\s*(.*)")

existing = sys.argv[1]
allfiles = sys.argv[1:]

print "existing parameter file: " + existing
newfile = open(existing + ".new", 'w')

# accumulates canonicalized lines for each unique "bond hybridization" pair
results = {}

for f in allfiles:
    print "processing " + f
    lines = open(f).readlines();
    for l in lines:
        # remove leading and trailing whitespace
        l = leadingWhitespacePattern.sub('', l)
        l = trailingWhitespacePattern.sub('', l)

        # find RCSID
        if f == existing and idPattern.search(l):
            newfile.write("#\n" + l + "\n#\n\n")
            continue

        # ignore comments and blank lines
        if commentPattern.match(l): continue
        if len(l) == 0: continue

        m = firstField.match(l)
        if m:
            bond = m.group(1)
            rest = m.group(2)
            canonical = bond + " "
            hybrid = "sp3"
            m = parameterPattern.match(rest)
            while m:
                key = m.group(1)
                value = m.group(2)
                rest = m.group(3)
                if key == "CenterHybridization":
                    hybrid = value
                canonical += key + "=" + value + " "
                m = parameterPattern.match(rest)
            sortfield = bond + " " + hybrid
            results[sortfield] = canonical

bondkeys = results.keys()
bondkeys.sort()
for key in bondkeys:
    newfile.write(results[key] + "\n")