def __init__(self, smarts_string, parser=None, name=None, overrides=None, typemap=None, *args, **kwargs): super(SMARTSGraph, self).__init__(*args, **kwargs) self.smarts_string = smarts_string self.name = name self.overrides = overrides self.typemap = typemap if parser is None: self.ast = SMARTS().parse(smarts_string) else: self.ast = parser.parse(smarts_string) self._atom_indices = OrderedDict() self._add_nodes() self._add_edges(self.ast) self._add_label_edges() self._graph_matcher = None
def test_optional_name_parser(): optional_names = ['_C', '_CH2', '_CH'] S = SMARTS(optional_names=optional_names) ast = S.parse('_CH2_C_CH') symbols = [a.tail[0] for a in ast.select('atom_symbol').strees] for name in optional_names: assert name in symbols
def test_optional_name_parser(self): optional_names = ["_C", "_CH2", "_CH"] S = SMARTS(optional_names=optional_names) ast = S.parse("_CH2_C_CH") symbols = [a.children[0] for a in ast.find_data("atom_symbol")] for name in optional_names: assert name in symbols
def test_optional_name_parser(): optional_names = ['_C', '_CH2', '_CH'] S = SMARTS(optional_names=optional_names) ast = S.parse('_CH2_C_CH') symbols = [a.children[0] for a in ast.find_data('atom_symbol')] for name in optional_names: assert name in symbols
def __init__( self, atomtype_definitions, atomtype_overrides, non_element_types, parser=None, ): self.atomtype_definitions = atomtype_definitions self.atomtype_overrides = atomtype_overrides self.non_element_types = non_element_types self.parser = parser or SMARTS(self.non_element_types)
def element_by_smarts_string(smarts_string): """Search for an element by a given SMARTS string Look up an element from a list of known elements by SMARTS string. Return None if no match found. Parameters ---------- smarts_string : str SMARTS string representation of an atom type or its local chemical context. The Foyer SMARTS parser will be used to find the central atom and look up an Element. Note that this means some SMARTS grammar may not be parsed properly. For details, see https://github.com/mosdef-hub/foyer/issues/63 Returns ------- matched_element : element.Element Return an element from the periodic table if we find a match Raises ------ GMSOError If no matching element is found for the provided smarts string """ from foyer.smarts import SMARTS PARSER = SMARTS() symbols = PARSER.parse(smarts_string).iter_subtrees_topdown() first_symbol = None for symbol in symbols: if symbol.data == 'atom_symbol': first_symbol = symbol.children[0] break matched_element = None if first_symbol is not None: matched_element = element_by_symbol(first_symbol) if matched_element is None: raise GMSOError( f'Failed to find an element from SMARTS string {smarts_string}. The ' f'parser detected a central node with name {first_symbol}') return matched_element
def __init__(self, smarts_str): self.PARSER = SMARTS() self.AST = self.PARSER.parse(smarts_str) self.AST.select('start') self.atom_with_id = {} self.atom_name = {} self.atom_with_label = {} self.NetworkX = nx.Graph() # invoke initial functions to initialize. self._assign_id() self._set_atoms_with_label() # invoke converting functions to generate graph. self._add_nodes() self._add_edges(self.AST) self._add_label_edges()
def element_by_smarts_string(smarts_string): """Search for an element by a given SMARTS string Look up an element from a list of known elements by SMARTS string. Return None if no match found. Parameters ---------- smarts_string : str SMARTS string representation of an atom type or its local chemical context. The Foyer SMARTS parser will be used to find the central atom and look up an Element. Note that this means some SMARTS grammar may not be parsed properly. For details, see https://github.com/mosdef-hub/foyer/issues/63 Returns ------- matched_element : element.Element or None Return an element from the periodict table if we find a match, otherwise return None """ from foyer.smarts import SMARTS PARSER = SMARTS() symbol = next( PARSER.parse(smarts_string).find_data('atom_symbol')).children[0] print(symbol) matched_element = element_by_symbol(symbol) if matched_element is None: raise GMSOError( f'' 'Failed to find an element from SMARTS string {smarts_string). The' 'parser detected a central node with name {symbol}') return matched_element
import parmed as pmd import plyplus import pytest from foyer.forcefield import generate_topology, Forcefield from foyer.rule import Rule from foyer.smarts import SMARTS from foyer.tests.utils import get_fn PARSER = SMARTS() def _rule_match(atom, smart, result): rule = Rule('test', parser=PARSER, smarts_string=smart) assert rule.matches(atom) is result def test_ast(): ast = PARSER.parse('O([H&X1])(H)') assert ast.head == "start" assert ast.tail[0].head == "atom" assert ast.tail[0].tail[0].head == "atom_symbol" assert ast.tail[0].tail[0].head == "atom_symbol" assert str(ast.tail[0].tail[0].tail[0]) == "O" def test_parse(): smarts = ['[#6][#1](C)H', '[O;X2]([C;X4](F)(*)(*))[C;X4]'] for pattern in smarts: ast = PARSER.parse(pattern)
def test_optional_names_good_syntax(self): good_optional_names = ["_C", "_CH2", "_CH"] S = SMARTS(optional_names=good_optional_names)
def test_optional_names_bad_syntax(self): bad_optional_names = ["_C", "XXX", "C"] with pytest.raises(FoyerError): S = SMARTS(optional_names=bad_optional_names)
def smarts_parser(self): return SMARTS()
def test_optional_names_good_syntax(): good_optional_names = ['_C', '_CH2', '_CH'] S = SMARTS(optional_names=good_optional_names)
def test_optional_names_bad_syntax(): bad_optional_names = ['_C', 'XXX', 'C'] with pytest.raises(FoyerError): S = SMARTS(optional_names=bad_optional_names)