Giter VIP home page Giter VIP logo

Comments (3)

aebrahim avatar aebrahim commented on May 14, 2024

We still want to raise a warning for non-integer formulas, as Formulas are
supposed to have integer entries. You can always suppress warnings by using

with warnings.catch_warnings():

Also, I haven't really tested it out, but wouldn't the [0-9.]+ require a
number... would this parse H2O correctly, for example?

On Wed, Oct 22, 2014 at 12:02 PM, Pierre SALVY [email protected]
wrote:

Hi there,

Lately I've been working with some macromolecules that were used as pools
for several species, and that have formulas that look like:

C36.9098H69.3707N1.6615O4.5051P0.468

As the element counts are non-integers, the formulas are not parsed and an
error is thrown.
I just wanted to propose a workaround I rigged to avoid that, if you are
interested. It is just a modification of the matching regex and a tweak in
the function core.Formula.parse_composition():

element_re = re.compile("([A-Z][a-z]?)([0-9.]+[0-9.]?|(?=[A-Z])?)"
def parse_composition(self):
"""Breaks the chemical formula down by element."""
tmp_formula = self.formula.replace("*", "")
composition = {}
elements = element_re.findall(tmp_formula)[0]
counts = element_re.split(tmp_formula)[1] # output looks like ('C', '31.02')
for element, count in zip(elements, counts):
if count == '':
count = 1
else:
try:
count = float(count)
except:
warn("failed to parse %s (in formula %s)" % (count, self.formula))
self.elements = {}
return
if element in composition:
composition[element] += count
else:
composition[element] = count
self.elements = composition

Anyway let me know if this is useful.


Reply to this email directly or view it on GitHub
#128.

from cobrapy.

aebrahim avatar aebrahim commented on May 14, 2024

Still, I don't think parsing floating point formulas (even while raising an
error) would be the worst thing. I think we'd consider merging a pull
request.

On Thu, Oct 23, 2014 at 12:12 PM, Ali Ebrahim [email protected] wrote:

We still want to raise a warning for non-integer formulas, as Formulas are
supposed to have integer entries. You can always suppress warnings by using

with warnings.catch_warnings():

Also, I haven't really tested it out, but wouldn't the [0-9.]+ require a
number... would this parse H2O correctly, for example?

On Wed, Oct 22, 2014 at 12:02 PM, Pierre SALVY [email protected]
wrote:

Hi there,

Lately I've been working with some macromolecules that were used as pools
for several species, and that have formulas that look like:

C36.9098H69.3707N1.6615O4.5051P0.468

As the element counts are non-integers, the formulas are not parsed and
an error is thrown.
I just wanted to propose a workaround I rigged to avoid that, if you are
interested. It is just a modification of the matching regex and a tweak in
the function core.Formula.parse_composition():

element_re = re.compile("([A-Z][a-z]?)([0-9.]+[0-9.]?|(?=[A-Z])?)"
def parse_composition(self):
"""Breaks the chemical formula down by element."""
tmp_formula = self.formula.replace("*", "")
composition = {}
elements = element_re.findall(tmp_formula)[0]
counts = element_re.split(tmp_formula)[1] # output looks like ('C', '31.02')
for element, count in zip(elements, counts):
if count == '':
count = 1
else:
try:
count = float(count)
except:
warn("failed to parse %s (in formula %s)" % (count, self.formula))
self.elements = {}
return
if element in composition:
composition[element] += count
else:
composition[element] = count
self.elements = composition

Anyway let me know if this is useful.


Reply to this email directly or view it on GitHub
#128.

from cobrapy.

psalvy avatar psalvy commented on May 14, 2024

The

|(?=[A-Z])?

block in the regex takes care of cases where there is no count (meaning count of one) such as is H2O'' or O7PS. It returns

[('H', '2'), ('O', '')]

and

[ ('O', '7'), ('P', ''), ('S', '')]

I tested it on a bunch of different cases and it seemed to work quite well. If you are interested in a pull request, I can include the original warning in such a fashion:

def parse_composition(self):
        """Breaks the chemical formula down by element."""
        tmp_formula = f.replace("*", "")
        composition = {}
        parsed = element_re.findall(tmp_formula)
        for (element, count) in parsed:
            if count == '':
                count = 1
            else:
                try:
                    count = float(count)
                    if count.is_integer(): warn("%s is not an integer (in formula %s)" % (count, self.formula))
                except:
                    warn("failed to parse %s (in formula %s)" % (count, self.formula))
                    self.elements = {}
                    return
            if element in composition:
                composition[element] += count
            else:
                composition[element] = count
        self.elements = composition

from cobrapy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.