Exemplo n.º 1
0
 def composition(self):
     composition = CompositionType()
     for element_relation in self.elements:
         symbol = element_relation.element
         isotope, element = re.search(r"(?P<isotope>\d*)?(?P<element>\S+)", symbol).groups()
         if isotope != "":
             isotope = int(isotope)
             iso_str = _make_isotope_string(element, isotope)
         else:
             iso_str = element
         count = element_relation.count
         composition[str(iso_str)] = count
     return composition
Exemplo n.º 2
0
def _formula_parser(formula, session):
    '''
    Parse a unimod formula composed of elements,
    isotopes, and other bricks.

    In order to look up a Brick's composition, this
    function must have access to a session.

    Parameters
    ----------
    formula: str
        A Unimod formula of the form `A(n) B(m)...`
        where A, B, ... are element names or bricks and
        (n), (m)... are parenthesized possibly signed integers or
        omitted in which case they are interpreted as 1
    session: Session
        An active SQLAlchemy session for looking up bricks in the database

    Returns
    -------
    CompositionType
    '''
    composition = CompositionType()
    for token in formula.split(" "):
        match = re.search(r"(?P<isotope>\d+)?(?P<elemet>[^\(]+)(?:\((?P<count>-?\d+)\))?", token)
        if match:
            isotope, element, count = match.groups()
            if count is not None:
                count = int(count)
            else:
                count = 1
            if isotope is not None:
                name = _make_isotope_string(element, int(isotope))
            else:
                name = element
            is_brick = session.query(Brick).filter(Brick.brick == name).first()
            if is_brick is None:
                composition[str(name)] += count
            else:
                composition += is_brick.composition * count
    return composition
Exemplo n.º 3
0
 def composition(self):
     composition = CompositionType()
     session = object_session(self)
     for fragment_composition_relation in self._fragment_composition:
         symbol = fragment_composition_relation.brick_string
         isotope, element = re.search(r"(?P<isotope>\d*)?(?P<element>\S+)", symbol).groups()
         count = fragment_composition_relation.count
         if count is not None:
             count = int(count)
         else:
             count = 1
         if isotope != "":
             name = _make_isotope_string(element, isotope)
         else:
             name = element
         is_brick = session.query(Brick).filter(Brick.brick == name).first()
         if is_brick is None:
             composition[str(name)] += count
         else:
             composition += is_brick.composition * count
     return composition