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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#! /usr/bin/env python3
#
# Copyright 2009-2024 The VOTCA Development Team (http://www.votca.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import lxml.etree as lxml
import re
import argparse
VERSION = '@PROJECT_VERSION@ #VOTCA_GIT_ID#'
PROGTITLE = 'THE VOTCA::XTP MAPPING FILE UPDATER'
PROGDESCR = 'Updates the CTP mapping file to VOTCA XTP'
VOTCAHEADER = '''\
==================================================
======== VOTCA (http://www.votca.org) ========
==================================================
{progtitle}
please read and cite: @PROJECT_CITATION@
and submit bugs to @PROJECT_CONTACT@
xtp_update_mapfile, version {version}
'''.format(version=VERSION, progtitle=PROGTITLE)
# =============================================================================
# PROGRAM OPTIONS
# =============================================================================
class XtpHelpFormatter(argparse.HelpFormatter):
def _format_usage(self, usage, action, group, prefix):
return VOTCAHEADER
progargs = argparse.ArgumentParser(prog='xtp_update_mapfile',
formatter_class=lambda prog: XtpHelpFormatter(
prog, max_help_position=70),
description=PROGDESCR)
progargs.add_argument('-sin', '--S_input',
dest='S_input',
required=True,
type=argparse.FileType('r'),
default='',
help='Mapping file to update.')
progargs.add_argument('-sout', '--S_output',
dest='S_output',
required=True,
type=argparse.FileType('w'),
default='',
help='Filename of new Mapping file.')
OPTIONS = progargs.parse_args()
# =============================================================================
# LXML
# =============================================================================
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
def changeframe(localframetext):
ids = localframetext.split()
newids = []
for i in ids:
if RepresentsInt(i):
integer = int(i)
else:
print("Cannot convert {} to integer".format(i))
newinteger = integer - 1
newids.append("{:d}".format(newinteger))
return ' '.join(newids)
def changeids(atomidtext):
re_s = re.compile(r'(\S+)')
atoms_and_whitespaces = re_s.split(atomidtext)
newstring = []
for atom in atoms_and_whitespaces:
if not atom or atom.isspace():
newstring.append(atom)
else:
array = atom.split(":")
atomtype = array[1]
if RepresentsInt(array[0]):
integer = int(array[0])
else:
print("Cannot convert {} to integer".format(array[0]))
newinteger = integer - 1
newstring.append(f"{newinteger}:{atomtype}")
return ''.join(newstring)
print("Reading in {}".format(OPTIONS.S_input.name))
Tree = lxml.parse(OPTIONS.S_input.name)
Root = Tree.getroot()
Molecules = Root.find("molecules")
for molecule in Molecules.iter('molecule'):
segments = molecule.find("segments")
for segment in segments.iter('segment'):
fragments = segment.find("fragments")
for fragment in fragments.iter('fragment'):
qmatoms = fragment.find("qmatoms")
qmatoms.text = changeids(qmatoms.text)
mpoles = fragment.find("mpoles")
mpoles.text = changeids(mpoles.text)
localframe = fragment.find("localframe")
localframe.text = changeframe(localframe.text)
print("Writing to {}".format(OPTIONS.S_output.name))
Tree.write(OPTIONS.S_output.name, pretty_print=True)
|