Returning Denominator of Units

7 views
Skip to first unread message

Alex Enersen

unread,
Jan 10, 2019, 6:13:32 PM1/10/19
to libsbml-development
I am currently using libSBML with Java. I am trying to retrieve the numerator and denominator of a species' units with the following function:

private String getSpeciesUnits(Species species) {
     return species.getSubstanceUnits();
}

However, this only returns the numerator. For example, if I have a species with 'moles' as the numerator and 'liter' as the denominator, it will only return 'moles'. I've tried other methods with no success. Is there an easy way to extract the denominator units from a Species?

Keating, Sarah M.

unread,
Jan 11, 2019, 4:51:41 AM1/11/19
to libsbml-d...@googlegroups.com, Alex Enersen
Hi Alex

Thanks for your interest in SBML/libSBML.

The 'denominator' units of the species are the units of the compartment
in which it is located. The Species class has an attribute 'compartment'
which returns the id of the compartment in which it is located. You then
need to query the Model to get the compartment using the id; and then
get the units from the Compartment.

So you would need to do something like

String compartment = species.getCompartment()
Compartment c = model.getCompartment(compartment)
String denominatorUnits = c.getUnits()

Let me know if I can be of more assistance.

Sarah

Matthias König

unread,
Jan 14, 2019, 3:09:36 AM1/14/19
to libsbml-development
Hi Alex,
small addition to Sarah's answer.

I think in addition you have to check if the species is defined in amount or concentration. This is defined based on the `hasOnlySubstanceUnits` attribute on species.
If `hasOnlySubstanceUnits=False` then the species is in concentration (than you need the volume as a denominator), otherwise you don't need the volume because the species is defined in amount (using the volume denominator would be incorrect, or you have to convert also the numerical values by dividing via the compartment volumes)

There is also the `getDerivedUnits` or `getDerivedUnitsDefinition` function as far as I remember which will give you the derived units (taking into account the `hasOnlySubstanceUnits` attribute). But this will not help you in splitting in nominator and denominator.
Best Matthias

Alex Enersen

unread,
Jan 14, 2019, 5:09:29 PM1/14/19
to libsbml-development
Thanks for the advice! I have managed to get the code working. I only have one issue, and it's the fact that the variable for the denominator comes up as "MWBUILTINUNIT_liter" and I just want "liter". My current solution is to perform replace("MWBUILTINUNIT_",""), but this seems not rigorous. Will this cover all cases, or should I implement something more rigorous?

Matthias König

unread,
Jan 15, 2019, 2:33:28 AM1/15/19
to LibSBML Development List
Hi Alex,
if there is a self-defined unit it should give the id of the unit (which is the MWBUILTINUNIT_liter part). For the internal SBML units (not self defined) it should give directly a human readable string, i.e. liter or litre depending on which internal unit was used.
So in you case you seem to get the id of your self defined unit definition. The thing I am doing for my units is setting the name of the unit to a nice human readable string. You should set in your model for the UnitDefinition with id="MWBUILTINUNIT_liter" the name="liter" and same for the other units were want nice strings.
Than you do in your printing code something like

name = unit.getId()
if unit.isSetName():
    name = unit.getName()

Setting the names only works for your own models. If you write generic code for dealing with SBML models your only solution is to get the denominator unit definition and than create a human readable string from all the parts of the unit definition. If you want just nice printing of the units of your own model just set the names and use the names of the units for printing.

Best Matthias




On Mon, Jan 14, 2019 at 11:09 PM Alex Enersen <eners...@gmail.com> wrote:
Thanks for the advice! I have managed to get the code working. I only have one issue, and it's the fact that the variable for the denominator comes up as "MWBUILTINUNIT_liter" and I just want "liter". My current solution is to perform replace("MWBUILTINUNIT_",""), but this seems not rigorous. Will this cover all cases, or should I implement something more rigorous?

--
You received this message because you are subscribed to a topic in the Google Groups "libsbml-development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/libsbml-development/Ye0c3G5N8LY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to libsbml-develop...@googlegroups.com.
To post to this group, send email to libsbml-d...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/libsbml-development/a0382be6-0ad5-4fa4-838f-48483401001e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matthias König, PhD.
Junior Group Leader LiSyM - Systems Medicine of the Liver
Humboldt Universität zu Berlin, Institute of Biology, Institute for Theoretical Biology
  https://livermetabolism.com
koni...@googlemail.com
https://github.com/matthiaskoenig
Tel: +49 30 2093 98435
Reply all
Reply to author
Forward
0 new messages