Skip to content

Commit

Permalink
[VIPR] Parser for [ meta ] information (#517)
Browse files Browse the repository at this point in the history
* parse_block_meta

* add comment

* change typo, add docstring, convert numeric value

* update docstring

* add test_meta , remove meta float parsing

* meta test done

* typo float docstring _parse_block_meta
  • Loading branch information
Romumrn committed May 4, 2023
1 parent 1fba29e commit fb4fc36
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
38 changes: 38 additions & 0 deletions vermouth/ffinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,44 @@ def _block(self, line, lineno=0):
def _block_atoms(self, line, lineno=0):
tokens = collections.deque(_tokenize(line))
_parse_block_atom(tokens, self.current_block)

@SectionLineParser.section_parser('moleculetype', 'meta')
def _parse_block_meta(self, line, lineno=0):
"""
Parse the meta section and update the object current_block with meta information.
Allow the dictionnary value to be None (in case of flag), string, int or float.
Example :
[meta]
flag1
key value
key2 value1 0.37
will give :
{
'flag1' : None,
'key': 'value',
'key2': ['value1' , '0.37']
}
Parameters
----------
line: str
lineno: str
"""

split_line = line.split()
key = split_line[0]

# depend of the number of value(s)
if len(split_line[1:]) == 0 :
value = None
elif len(split_line[1:]) == 1 :
value = split_line[1]
else:
value = split_line[1:]

self.current_block.meta[key] = value

@SectionLineParser.section_parser('moleculetype', 'edges',
negate=False, context_type='block')
Expand Down
20 changes: 20 additions & 0 deletions vermouth/tests/test_ff_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ def test_atoms():
'charge_group': 3, 'custom': 4,
'other': 'plop'}

@staticmethod
def test_meta():
lines = """
[ moleculetype ]
XXX 1
[ meta ]
flag
key1 0.15 ;test
key2 value1 value2
"""
lines = textwrap.dedent(lines).splitlines()
ff = vermouth.forcefield.ForceField(name='test_ff')
vermouth.ffinput.read_ff(lines, ff)
block = ff.blocks['XXX']
assert len(block.meta) == 3
assert block.meta['flag'] == None
assert block.meta['key1'] == '0.15'
assert block.meta['key2'] == ['value1', 'value2']


@staticmethod
def test_fixed_number_interaction():
"""
Expand Down

0 comments on commit fb4fc36

Please sign in to comment.