Search in sources :

Example 26 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class SBPAXParameterExtractor method extractParameter.

public static Map<SBOTerm, ModelParameter> extractParameter(ReactionStep reaction, SBMeasurable measurable) throws PropertyVetoException {
    Map<SBOTerm, ModelParameter> sboToParameters = new HashMap<SBOTerm, ModelParameter>();
    Set<SBOTerm> sboTerms = SBPAXSBOExtractor.extractSBOTerms(measurable);
    String symbol = null;
    VCUnitDefinition targetUnit = null;
    Model model = reaction.getModel();
    ModelUnitSystem modelUnitSystem = model.getUnitSystem();
    Set<SBOTerm> termsWithUnituM = SetUtil.newSet(SBOList.sbo0000027MichaelisConstant, SBOList.sbo0000322MichaelisConstantForSubstrate);
    for (SBOTerm sboTerm : sboTerms) {
        if (termsWithUnituM.contains(sboTerm)) {
            targetUnit = modelUnitSystem.getVolumeConcentrationUnit();
        // targetUnit = VCUnitDefinition.UNIT_uM;
        }
        if (StringUtil.notEmpty(sboTerm.getSymbol())) {
            symbol = sboTerm.getSymbol();
        }
        for (int i = 0; i < symbol.length(); ++i) {
            char charAt = symbol.charAt(i);
            if (!Character.isJavaIdentifierPart(charAt)) {
                symbol = symbol.replace(charAt, '_');
            }
        }
    }
    VCUnitDefinition unit = UOMEUnitExtractor.extractVCUnitDefinition(measurable, modelUnitSystem);
    double conversionFactor = 1.0;
    if (targetUnit != null && unit != null && unit != modelUnitSystem.getInstance_TBD() && !targetUnit.equals(unit)) {
        // if(unit.equals(VCUnitDefinition.UNIT_M) && targetUnit.equals(VCUnitDefinition.UNIT_uM)) {
        if (unit.isCompatible(targetUnit)) {
            conversionFactor = unit.convertTo(conversionFactor, targetUnit);
            unit = targetUnit;
        }
    }
    ArrayList<Double> numbers = measurable.getNumber();
    if (StringUtil.isEmpty(symbol)) {
        symbol = "p" + (++nParameter);
    }
    for (Double number : numbers) {
        String parameterName = symbol + "_" + reaction.getName();
        if (model.getModelParameter(parameterName) != null) {
            int count = 0;
            while (model.getModelParameter(parameterName + "_" + count) != null) {
                ++count;
            }
            parameterName = parameterName + "_" + count;
        }
        ModelParameter parameter = model.new ModelParameter(parameterName, new Expression(conversionFactor * number.doubleValue()), Model.ROLE_UserDefined, unit);
        model.addModelParameter(parameter);
        for (SBOTerm sboTerm : sboTerms) {
            sboToParameters.put(sboTerm, parameter);
        }
    }
    return sboToParameters;
}
Also used : HashMap(java.util.HashMap) SBOTerm(org.vcell.pathway.sbo.SBOTerm) ModelParameter(cbit.vcell.model.Model.ModelParameter) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) Model(cbit.vcell.model.Model) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 27 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class VCUnitTranslator method processCellMLUnit.

// derives the ucar unit equivalent to a single cellml 'unit'
// part of the recursive retrieval of CellML units.
private static Unit processCellMLUnit(Element cellUnit, Namespace sNamespace, Namespace sAttNamespace, VCUnitSystem vcUnitSystem) {
    Unit unit = null;
    UnitName uName;
    String kind = cellUnit.getAttributeValue(CELLMLTags.units, sAttNamespace);
    if (kind == null || kind.length() == 0) {
        throw new RuntimeException("No kind specified for the CellML unit >>> 2");
    }
    String exp = cellUnit.getAttributeValue(CELLMLTags.exponent, sAttNamespace);
    String scaleStr = cellUnit.getAttributeValue(CELLMLTags.prefix, sAttNamespace);
    int scale;
    try {
        if (scaleStr == null || scaleStr.length() == 0) {
            scale = 0;
        } else {
            scale = Integer.parseInt(scaleStr);
        }
    } catch (NumberFormatException e) {
        // bad practice but no better way.
        scale = CELLMLTags.prefixToScale(scaleStr);
    }
    String multiplier = cellUnit.getAttributeValue(CELLMLTags.mult, sAttNamespace);
    String offset = cellUnit.getAttributeValue(CELLMLTags.offset, sAttNamespace);
    try {
        StandardUnitDB unitDB = StandardUnitDB.instance();
        if (kind.equals(CELLMLTags.noDimUnit)) {
            // special treatment. ignore all the other attributes. Not used in computing total unit.
            unit = DerivedUnitImpl.DIMENSIONLESS;
            uName = unit.getUnitName();
        } else if (CELLMLTags.isCellBaseUnit(kind)) {
            unit = unitDB.get(kind);
            uName = UnitName.newUnitName(kind);
        } else {
            Element owner = (Element) cellUnit.getParent().getParent();
            String ownerName = owner.getAttributeValue(CELLMLTags.name, sAttNamespace);
            // check if already added.
            VCUnitDefinition unitDef = getMatchingCellMLUnitDef(owner, sAttNamespace, kind, vcUnitSystem);
            if (unitDef != null) {
                unit = unitDef.getUcarUnit();
            } else {
                // recursive block. assumes model level units are added before component level ones,
                // so no need to recurse through those as well.
                @SuppressWarnings("unchecked") ArrayList<Element> siblings = new ArrayList<Element>(owner.getChildren(CELLMLTags.UNITS, sNamespace));
                Element cellUnitDef = null;
                for (Element cuElement : siblings) {
                    cellUnitDef = cuElement;
                    if (cellUnitDef.getAttributeValue(CELLMLTags.name, sAttNamespace).equals(kind)) {
                        break;
                    } else {
                        cellUnitDef = null;
                    }
                }
                if (cellUnitDef != null) {
                    unitDef = CellMLToVCUnit(cellUnitDef, sNamespace, sAttNamespace, vcUnitSystem);
                } else {
                    System.err.println("Unit definition: " + kind + " is missing. >>> 3");
                    return null;
                }
            }
            uName = UnitName.newUnitName(kind);
        }
        if (exp != null && Integer.parseInt(exp) != 1) {
            unit = unit.raiseTo(new RationalNumber(Integer.parseInt(exp)));
        }
        if (scale != 0) {
            unit = new ScaledUnit(Double.parseDouble("1.0e" + scale), unit, uName);
        }
        if (multiplier != null && Double.parseDouble(multiplier) != 1) {
            unit = new ScaledUnit(Double.parseDouble(multiplier), unit, uName);
        }
        if (offset != null && Double.parseDouble(offset) != 0) {
            unit = new OffsetUnit(unit, Double.parseDouble(offset), uName);
        }
    } catch (UnitException e) {
        e.printStackTrace();
        throw new cbit.vcell.units.VCUnitException("Unable to set unit value: " + e.getMessage());
    }
    return unit;
}
Also used : OffsetUnit(ucar.units_vcell.OffsetUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) StandardUnitDB(ucar.units_vcell.StandardUnitDB) Element(org.jdom.Element) UnitException(ucar.units_vcell.UnitException) ArrayList(java.util.ArrayList) UnitName(ucar.units_vcell.UnitName) BaseUnit(ucar.units_vcell.BaseUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) OffsetUnit(ucar.units_vcell.OffsetUnit) Unit(ucar.units_vcell.Unit) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) RationalNumber(ucar.units_vcell.RationalNumber)

Example 28 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class VCUnitTranslator method CellMLToVCUnit.

// takes a CellML units jdom element.Similar to its SBML counterpart.
// part of the recursive retrieval of CellML units.
public static VCUnitDefinition CellMLToVCUnit(Element source, Namespace sNamespace, Namespace sAttNamespace, VCUnitSystem vcUnitSystem) {
    @SuppressWarnings("unchecked") ArrayList<Element> list = new ArrayList<Element>(source.getChildren(CELLMLTags.UNIT, sNamespace));
    if (list.size() == 0) {
        throw new RuntimeException("No units found for CellML unit definition: " + source.getAttributeValue(CELLMLTags.name, sAttNamespace) + " >>> 1");
    }
    VCUnitDefinition totalUnit = null;
    Unit unit = null;
    for (Element temp : list) {
        unit = processCellMLUnit(temp, sNamespace, sAttNamespace, vcUnitSystem);
        if (unit == null) {
            // for dimensionless and unresolved?
            continue;
        }
        if (totalUnit == null) {
            totalUnit = vcUnitSystem.getInstance(unit.getSymbol());
        } else {
            // ?
            totalUnit = totalUnit.multiplyBy(vcUnitSystem.getInstance(unit.getSymbol()));
        }
    }
    if (totalUnit != null) {
        String unitName = source.getAttributeValue(CELLMLTags.name, sAttNamespace);
        String ownerName = ((Element) source.getParent()).getAttributeValue(CELLMLTags.name, sAttNamespace);
        storeCellMLUnit(ownerName, unitName, totalUnit);
    }
    return totalUnit;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Element(org.jdom.Element) ArrayList(java.util.ArrayList) BaseUnit(ucar.units_vcell.BaseUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) OffsetUnit(ucar.units_vcell.OffsetUnit) Unit(ucar.units_vcell.Unit)

Example 29 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class SimulationContext method createSimulationContextParameter.

public SimulationContextParameter createSimulationContextParameter() {
    String name = "appParm0";
    while (getSimulationContextParameter(name) != null) {
        name = TokenMangler.getNextEnumeratedToken(name);
    }
    Expression expression = new Expression(1.0);
    int role = SimulationContext.ROLE_UserDefined;
    VCUnitDefinition unit = getModel().getUnitSystem().getInstance_DIMENSIONLESS();
    SimulationContextParameter parameter = new SimulationContextParameter(name, expression, role, unit);
    try {
        addSimulationContextParameter(parameter);
        return parameter;
    } catch (PropertyVetoException e) {
        e.printStackTrace();
        throw new RuntimeException("error creating new application parameter: " + e.getMessage(), e);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression)

Example 30 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class SpeciesContextSpec method convertParticlesToConcentration.

public Expression convertParticlesToConcentration(Expression iniParticles) throws ExpressionException, MappingException {
    Expression iniConcentrationExpr = null;
    Structure structure = getSpeciesContext().getStructure();
    double structSize = computeStructureSize();
    if (structure instanceof Membrane) {
        // iniConcentration(molecules/um2) = particles/size(um2)
        try {
            iniConcentrationExpr = new Expression((iniParticles.evaluateConstant() * 1.0) / structSize);
        } catch (ExpressionException e) {
            iniConcentrationExpr = Expression.div(iniParticles, new Expression(structSize)).flatten();
        }
    } else {
        // convert concentration(particles/volume) to number of particles
        // particles = [iniParticles(uM)/size(um3)]*KMOLE
        // @Note : 'kMole' variable here is used only as a var name, it does not represent the previously known ReservedSymbol KMOLE.
        ModelUnitSystem modelUnitSystem = getSimulationContext().getModel().getUnitSystem();
        VCUnitDefinition stochasticToVolSubstance = modelUnitSystem.getVolumeSubstanceUnit().divideBy(modelUnitSystem.getStochasticSubstanceUnit());
        double stochasticToVolSubstanceScale = stochasticToVolSubstance.getDimensionlessScale().doubleValue();
        try {
            iniConcentrationExpr = new Expression((iniParticles.evaluateConstant() * stochasticToVolSubstanceScale / structSize));
        } catch (ExpressionException e) {
            Expression numeratorExpr = Expression.mult(iniParticles, new Expression(stochasticToVolSubstanceScale));
            Expression denominatorExpr = new Expression(structSize);
            iniConcentrationExpr = Expression.div(numeratorExpr, denominatorExpr).flatten();
        }
    }
    return iniConcentrationExpr;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) ExpressionException(cbit.vcell.parser.ExpressionException) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Aggregations

VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)113 Expression (cbit.vcell.parser.Expression)73 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)36 ExpressionException (cbit.vcell.parser.ExpressionException)26 PropertyVetoException (java.beans.PropertyVetoException)24 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)21 Element (org.jdom.Element)20 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)19 ArrayList (java.util.ArrayList)17 ModelParameter (cbit.vcell.model.Model.ModelParameter)16 Model (cbit.vcell.model.Model)14 Parameter (cbit.vcell.model.Parameter)14 SpeciesContext (cbit.vcell.model.SpeciesContext)13 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)13 Membrane (cbit.vcell.model.Membrane)12 Structure (cbit.vcell.model.Structure)12 StructureMapping (cbit.vcell.mapping.StructureMapping)11 MathException (cbit.vcell.math.MathException)10 ReactionStep (cbit.vcell.model.ReactionStep)10 Feature (cbit.vcell.model.Feature)9