Comments (3)
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 = compositionAnyway let me know if this is useful.
—
Reply to this email directly or view it on GitHub
#128.
from cobrapy.
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 usingwith 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 = compositionAnyway let me know if this is useful.
—
Reply to this email directly or view it on GitHub
#128.
from cobrapy.
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)
- problems reading certain AGORA2 models HOT 3
- Can Not Open the SBML File HOT 4
- Give parameters to model.optimize()
- How to set the model solver HOT 1
- ContainerAlreadyContains error with the fastcc() HOT 3
- createMultipleSpeciesModel function can not find in cobrapy HOT 4
- SBML file not getting uploaded HOT 2
- How to perform relaxedFBA with cobrapy? HOT 1
- Cannot import cobra: import error involving numpy HOT 4
- how to handle mixed-integer programming (MIP) problems with the glpk, cplex or/and gurobi solvers in python/with cobrapy? HOT 3
- Model valid but cannot be loaded HOT 2
- Flux-Samplig: Cannot escape sampling region, model seems to be numerically unstable. Reporting the model HOT 1
- Incompatibility with pydantic version 2 HOT 6
- gurobipy.Model.addConstr() DeprecationWarning HOT 4
- find_gene_knockout_reactions error HOT 1
- AttributeError: 'list' object has no attribute 'items' HOT 7
- Hello I am new to python and Cobra, i would be grateful if you can provide me with steps to open an XML file from the computer and/or from online database, i tried the commands on Cobra documentation but i can't apply it.
- upgrading dependency to allow pandas>=2.0 HOT 1
- [BUG] Biomodels web io access broekn due to redirects
- Adding new metabolites HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cobrapy.