Search in sources :

Example 6 with VCUnitDefinition

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

the class SBMLUnitTranslator method getSBMLUnitDefinition.

public static UnitDefinition getSBMLUnitDefinition(VCUnitDefinition vcUnitDefn, int level, int version, VCUnitSystem vcUnitSystem) throws SbmlException {
    final UnitDefinition sbmlUnitDefn = new UnitDefinition(3, 1);
    if (vcUnitDefn.isCompatible(vcUnitSystem.getInstance_DIMENSIONLESS())) {
        double multiplier = Double.parseDouble(vcUnitDefn.getSymbol());
        sbmlUnitDefn.addUnit(new Unit(multiplier, 0, Kind.DIMENSIONLESS, 1.0, 3, 1));
        return sbmlUnitDefn;
    }
    String vcSymbol = vcUnitDefn.getSymbol();
    double overallMultiplier = 1.0;
    if (vcSymbol.contains(" ")) {
        String[] unitParts = vcSymbol.split(" ");
        overallMultiplier = Double.parseDouble(unitParts[0]);
        vcSymbol = unitParts[1];
    }
    // String sbmlUnitSymbol = TokenMangler.mangleToSName(vcSymbol);
    String[] symbols = vcSymbol.split("\\.");
    if (symbols.length == 0) {
        symbols = new String[] { vcSymbol };
    }
    for (int i = 0; i < symbols.length; i++) {
        double multiplier = 1.0;
        if (i == 0) {
            multiplier *= overallMultiplier;
        }
        double exponent = 1.0;
        int scale = 0;
        String symbol = symbols[i];
        // }
        if (symbol.contains("-")) {
            String[] symbolAndExp = symbol.split("\\-");
            symbol = symbolAndExp[0];
            exponent = -1 * Integer.parseInt(symbolAndExp[1]);
        } else if (Character.isDigit(symbol.charAt(symbol.length() - 1))) {
            exponent = Integer.parseInt(symbol.substring(symbol.length() - 1));
            symbol = symbol.substring(0, symbol.length() - 1);
        }
        VCUnitDefinition vcUnit = vcUnitSystem.getInstance(symbol);
        boolean bFoundMatch = false;
        // check sbml builtin units (base SI and supported derived units) first.
        for (Kind kind : Kind.values()) {
            String kindSymbol = kind.getSymbol();
            if (kind == Kind.AVOGADRO || kind == Kind.CELSIUS || kind == Kind.INVALID || kind == Kind.BECQUEREL || kind == Kind.HERTZ) {
                continue;
            }
            if (kind == Kind.OHM) {
                kindSymbol = "Ohm";
            }
            if (kind == Kind.ITEM) {
                kindSymbol = "molecules";
            }
            VCUnitDefinition kindVcUnit = vcUnitSystem.getInstance(kindSymbol);
            if (kindVcUnit.isCompatible(vcUnit)) {
                if (kindVcUnit.isEquivalent(vcUnit)) {
                    sbmlUnitDefn.addUnit(new Unit(multiplier, scale, kind, exponent, 3, 1));
                } else {
                    double factor = vcUnit.convertTo(1.0, kindVcUnit);
                    double logFactor = Math.log10(factor);
                    if (logFactor == (int) logFactor) {
                        scale = (int) logFactor;
                    } else {
                        scale = 0;
                        multiplier = multiplier * factor;
                    }
                    Unit sbmlUnit = new Unit(multiplier, scale, kind, exponent, 3, 1);
                    sbmlUnitDefn.addUnit(sbmlUnit);
                    System.err.println("kind = " + kind.name() + " is equivalent to vcUnit = " + vcUnit.getSymbol() + ",  SBML unit is " + sbmlUnit);
                }
                bFoundMatch = true;
                break;
            }
        }
        if (!bFoundMatch) {
            // check for molar, kind of crazy that this one is missing.
            VCUnitDefinition kindVcUnit = vcUnitSystem.getInstance("molar");
            if (kindVcUnit.isCompatible(vcUnit)) {
                if (kindVcUnit.isEquivalent(vcUnit)) {
                    sbmlUnitDefn.addUnit(new Unit(multiplier, scale, Kind.MOLE, exponent, 3, 1));
                    sbmlUnitDefn.addUnit(new Unit(1, 0, Kind.LITRE, -exponent, 3, 1));
                } else {
                    double factor = vcUnit.convertTo(1.0, kindVcUnit);
                    double logFactor = Math.log10(factor);
                    if (logFactor == (int) logFactor) {
                        scale = (int) logFactor;
                    } else {
                        scale = 0;
                        multiplier = multiplier * factor;
                    }
                    sbmlUnitDefn.addUnit(new Unit(multiplier, scale, Kind.MOLE, exponent, 3, 1));
                    sbmlUnitDefn.addUnit(new Unit(1, 0, Kind.LITRE, -exponent, 3, 1));
                    System.err.println("matched to liter ... had to create a replacement for molar, vcUnit = " + vcUnit.getSymbol() + ",  SBML unit is " + sbmlUnitDefn);
                }
                bFoundMatch = true;
            }
        }
        if (!bFoundMatch) {
            throw new RuntimeException("didn't find a match for vcUnit " + vcUnit.getSymbol());
        // System.out.println("Still didn't find a match for vcUnit "+vcUnit.getSymbol());
        }
    // ucar.units_vcell.Unit ucarUnit = vcUnit.getUcarUnit();
    // if (ucarUnit instanceof ScaledUnit){
    // ScaledUnit scaledUnit = (ScaledUnit)ucarUnit;
    // double parsedScale = scaledUnit.getScale();
    // double logScale = Math.log10(parsedScale);
    // if (logScale == (int)logScale){
    // scale = (int)logScale;
    // }else{
    // scale = 0;
    // multiplier = multiplier*parsedScale;
    // }
    // ucar.units_vcell.Unit insideUnit = scaledUnit.getUnit();
    // boolean bFoundMatch = false;
    // for (Kind kind : Kind.values()){
    // String kindSymbol = kind.getSymbol();
    // if (kind==Kind.AVOGADRO){
    // continue;
    // }
    // if (kind==Kind.CELSIUS){
    // continue;
    // }
    // if (kind==Kind.INVALID){
    // continue;
    // }
    // if (kind==Kind.OHM){
    // kindSymbol = "Ohm";
    // }
    // String sym = insideUnit.toString();
    // if (vcUnitSystem.getInstance(kindSymbol).isEquivalent(vcUnitSystem.getInstance(sym))){
    // sbmlUnitDefn.addUnit(new Unit(multiplier, scale, kind, exponent, 3, 1));
    // bFoundMatch = true;
    // break;
    // }
    // }
    // if (!bFoundMatch){
    // System.err.println("couldn't find an SBML unit for vcUnit "+vcUnit.getSymbol());
    // System.err.println("couldn't find an SBML unit for vcUnit "+vcUnit.getSymbol());
    // }
    // }
    // System.err.println("vcUnit is "+symbols[i]+",  ucarUnit is "+ucarUnit.getSymbol());
    }
    sbmlUnitDefn.setId(TokenMangler.mangleToSName(vcSymbol));
    return sbmlUnitDefn;
// 
// // If VC unit is DIMENSIONLESS ...
// if (vcUnitDefn.isTBD()) {
// throw new RuntimeException("TBD unit has no SBML equivalent");
// } else if (vcUnitDefn.isCompatible(vcUnitSystem.getInstance_DIMENSIONLESS())) {
// double multiplier = 1.0;
// multiplier = vcUnitDefn.convertTo(multiplier, vcUnitSystem.getInstance_DIMENSIONLESS());
// sbmlUnitDefn = new UnitDefinition(level, version);
// sbmlUnitDefn.setId(TokenMangler.mangleToSName(TokenMangler.mangleToSName(vcSymbol)));
// Unit dimensionlessUnit = new UnitD(level, version);
// dimensionlessUnit.setMultiplier(multiplier);
// sbmlUnitDefn.addUnit(dimensionlessUnit);
// } else {
// // Translate the VCUnitDef into libSBML UnitDef : convert the units of VCUnitDef into libSBML units and add them to sbmlUnitDefn
// 
// sbmlUnitDefn = new UnitDefinition(level, version);
// sbmlUnitDefn.setId(TokenMangler.mangleToSName(TokenMangler.mangleToSName(sbmlUnitSymbol)));
// ucar.units_vcell.Unit vcUcarUnit = vcUnitDefn.getUcarUnit();
// //ArrayList<Unit> sbmlUnitsList = convertVCUnitsToSbmlUnits(1.0, vcUcarUnit, new ArrayList<Unit>(), level, version);
// List<Unit> sbmlUnitsList = convert(vcUcarUnit, level, version);
// 
// for (int i = 0; i < sbmlUnitsList.size(); i++){
// Unit sbmlUnit = sbmlUnitsList.get(i);
// sbmlUnitDefn.addUnit(sbmlUnit);
// }
// }
// 
// return sbmlUnitDefn;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Kind(org.sbml.jsbml.Unit.Kind) Unit(org.sbml.jsbml.Unit) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) UnitDefinition(org.sbml.jsbml.UnitDefinition)

Example 7 with VCUnitDefinition

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

the class SBMLUnitTranslator method getVCUnitDefinition.

public static VCUnitDefinition getVCUnitDefinition(org.sbml.jsbml.UnitDefinition sbmlUnitDefn, VCUnitSystem vcUnitSystem) {
    // Each SBML UnitDefinition contains a list of Units, the total unit (VC unit) as represented by
    // an SBML UnitDefinition is the product of the list of units it contains.
    VCUnitDefinition vcUnitDefn = null;
    for (Unit sbmlUnit : sbmlUnitDefn.getListOfUnits()) {
        VCUnitDefinition vcUnit = getVCUnit(sbmlUnit, vcUnitSystem);
        if (vcUnitDefn == null) {
            vcUnitDefn = vcUnit;
        } else {
            vcUnitDefn = vcUnitDefn.multiplyBy(vcUnit);
        }
    }
    String originalSymbol = vcUnitDefn.getSymbol();
    String symbol = "." + originalSymbol + ".";
    final String[] moleSymbols = new String[] { "umol", "nmol", "mol", "mmol", "pmol" };
    final String[] molarSymbols = new String[] { "uM", "nM", "M", "mM", "pM" };
    for (int i = 0; i < moleSymbols.length; i++) {
        String mol = moleSymbols[i];
        String M = molarSymbols[i];
        if (symbol.contains(mol)) {
            symbol = symbol.replace("." + mol + ".l-1.", "." + M + ".");
            symbol = symbol.replace(".l-1." + mol + ".", "." + M + ".");
            symbol = symbol.replace("." + mol + "-1.l.", "." + M + "-1.");
            symbol = symbol.replace(".l." + mol + "-1.", "." + M + "-1.");
            symbol = symbol.replace("." + mol + "2.l-2.", "." + M + "2.");
            symbol = symbol.replace(".l-2." + mol + "2.", "." + M + "2.");
            symbol = symbol.replace("." + mol + "-2.l2.", "." + M + "-2.");
            symbol = symbol.replace(".l2." + mol + "-2.", "." + M + "-2.");
        }
    }
    symbol = symbol.substring(1, symbol.length() - 1);
    if (!symbol.equals(vcUnitDefn.getSymbol())) {
        System.err.println("new symbol is " + symbol + ",   old symbol is " + vcUnitDefn.getSymbol());
        VCUnitDefinition new_vcUnitDefn = vcUnitSystem.getInstance(symbol);
        if (!new_vcUnitDefn.isEquivalent(vcUnitDefn)) {
            throw new RuntimeException("failed to simplify unit " + vcUnitDefn.getSymbol() + ", created wrong symbol " + symbol);
        }
        return new_vcUnitDefn;
    } else {
        return vcUnitDefn;
    }
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Unit(org.sbml.jsbml.Unit)

Example 8 with VCUnitDefinition

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

the class SBMLUnitTranslator method getVCUnit.

/*
	 *  getVCUnit : 
	 */
private static VCUnitDefinition getVCUnit(org.sbml.jsbml.Unit unit, VCUnitSystem vcUnitSystem) {
    // Get the attributes of the unit 'element', 'kind', 'multiplier', 'scale', 'offset', etc.
    Kind unitKind = unit.getKind();
    if (unit.getExponent() != (int) unit.getExponent()) {
        throw new RuntimeException("non-integer units not supported for VCell/SBML unit translation: sbml unit = " + unit.toString());
    }
    int unitExponent = (int) unit.getExponent();
    int unitScale = unit.getScale();
    double unitMultiplier = unit.getMultiplier();
    String unitKindSymbol = unitKind.getName();
    if (unit.getKind().getSymbol() != null && "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".contains(unit.getKind().getSymbol().substring(0, 1))) {
        unitKindSymbol = unitKind.getSymbol();
    }
    if (unit.isSecond() && ((unitMultiplier == 60 && unitScale == 0) || (unitMultiplier == 6 && unitScale == 1))) {
        unitKindSymbol = "min";
        unitMultiplier = 1;
        unitScale = 0;
    }
    if (unit.isSecond() && ((unitMultiplier == 3600 && unitScale == 0) || (unitMultiplier == 3.6 && unitScale == 3))) {
        unitKindSymbol = "hour";
        unitMultiplier = 1;
        unitScale = 0;
    }
    if (unit.isSecond() && ((unitMultiplier == 86400 && unitScale == 0) || (unitMultiplier == 86.4 && unitScale == 3) || (unitMultiplier == 8.64 && unitScale == 4))) {
        unitKindSymbol = "day";
        unitMultiplier = 1;
        unitScale = 0;
    }
    if (unit.isItem()) {
        System.out.println("SBML 'item' unit found, interpreted as 'molecule'");
        unitKindSymbol = "molecules";
    }
    // convert the sbmlUnit into a vcell unit with the appropriate multiplier, scale, exponent, offset, etc ..
    if (unit.getKind() == Kind.DIMENSIONLESS) {
        // 'dimensionless' can be part of a bigger unit definition
        String vcScaleStr = Double.toString(Math.pow((unitMultiplier * Math.pow(10, unitScale)), unitExponent));
        VCUnitDefinition vcUnit = vcUnitSystem.getInstance(vcScaleStr);
        return vcUnit;
    } else {
        String prefix = "";
        switch(unitScale) {
            case -3:
                {
                    prefix = "m";
                    unitScale = 0;
                    break;
                }
            case -6:
                {
                    prefix = "u";
                    unitScale = 0;
                    break;
                }
            case -9:
                {
                    prefix = "n";
                    unitScale = 0;
                    break;
                }
            case -12:
                {
                    prefix = "p";
                    unitScale = 0;
                    break;
                }
        }
        String vcScaleStr = Double.toString(unitMultiplier * Math.pow((Math.pow(10, unitScale)), unitExponent));
        VCUnitDefinition vcUnit = vcUnitSystem.getInstance(vcScaleStr + " " + prefix + unitKindSymbol + unitExponent);
        return vcUnit;
    }
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Kind(org.sbml.jsbml.Unit.Kind)

Example 9 with VCUnitDefinition

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

the class ModelTest method getExample_GlobalParams.

/**
 * This method was created by a SmartGuide.
 */
public static Model getExample_GlobalParams() throws Exception {
    double A_init = 10.0;
    double B_init = 20.0;
    double C_init = 30.0;
    double D_init = 40.0;
    double A_diff = 11.0;
    double B_diff = 22.0;
    double C_diff = 33.0;
    double D_diff = 44.0;
    Model model = new Model("model1");
    model.addSpecies(new Species("A", "A"));
    Species A = model.getSpecies("A");
    model.addSpecies(new Species("B", "B"));
    Species B = model.getSpecies("B");
    model.addSpecies(new Species("C", "C"));
    Species C = model.getSpecies("C");
    model.addSpecies(new Species("D", "D"));
    Species D = model.getSpecies("D");
    model.addFeature("Cytosol");
    Feature cytosol = (Feature) model.getStructure("Cytosol");
    model.addSpeciesContext(A, cytosol);
    SpeciesContext A_cyt = model.getSpeciesContext(A, cytosol);
    model.addSpeciesContext(B, cytosol);
    SpeciesContext B_cyt = model.getSpeciesContext(B, cytosol);
    model.addSpeciesContext(C, cytosol);
    SpeciesContext C_cyt = model.getSpeciesContext(C, cytosol);
    model.addSpeciesContext(D, cytosol);
    SpeciesContext D_cyt = model.getSpeciesContext(D, cytosol);
    // add global parameters to the model
    VCUnitDefinition volumeReactionRateUnit = model.getUnitSystem().getVolumeReactionRateUnit();
    ModelParameter gp1 = model.new ModelParameter("Kon_1", new Expression(3.0), Model.ROLE_UserDefined, volumeReactionRateUnit);
    gp1.setDescription("first global parameter");
    model.addModelParameter(gp1);
    ModelParameter gp2 = model.new ModelParameter("Koff_1", new Expression(4.0), Model.ROLE_UserDefined, volumeReactionRateUnit);
    gp2.setDescription("second global parameter");
    model.addModelParameter(gp2);
    SimpleReaction sr;
    // 
    // CYTOSOL REACTIONS
    // 
    double K1 = 1.0;
    double K2 = 2.0;
    double K3 = 3.0;
    double K4 = 4.0;
    sr = new SimpleReaction(model, cytosol, "SIMPLE_REACTION_ABC", true);
    sr.setModel(model);
    sr.addReactant(A_cyt, 1);
    sr.addReactant(B_cyt, 1);
    sr.addProduct(C_cyt, 1);
    MassActionKinetics massAct = new MassActionKinetics(sr);
    massAct.setParameterValue(massAct.getForwardRateParameter(), new Expression(K1));
    massAct.setParameterValue(massAct.getReverseRateParameter(), new Expression(K2));
    massAct.renameParameter(massAct.getForwardRateParameter().getName(), "K1");
    massAct.renameParameter(massAct.getReverseRateParameter().getName(), "K2");
    sr.setKinetics(massAct);
    model.addReactionStep(sr);
    sr = new SimpleReaction(model, cytosol, "SIMPLE_REACION_CDA", true);
    sr.setModel(model);
    sr.addReactant(C_cyt, 1);
    sr.addReactant(D_cyt, 1);
    sr.addProduct(A_cyt, 1);
    massAct = new MassActionKinetics(sr);
    massAct.setParameterValue(massAct.getForwardRateParameter(), new Expression(K3));
    massAct.setParameterValue(massAct.getReverseRateParameter(), new Expression(K4));
    massAct.renameParameter(massAct.getForwardRateParameter().getName(), "K3");
    massAct.renameParameter(massAct.getReverseRateParameter().getName(), "K4");
    sr.setKinetics(massAct);
    model.addReactionStep(sr);
    Diagram diagram = model.getDiagram(cytosol);
    String diagramFile = " { " + "   SpeciesContextSpec A_cyt 100 100 " + "   SpeciesContextSpec B_cyt 50 200 " + "   SpeciesContextSpec C_cyt 200 200 " + "   SpeciesContextSpec D_cyt 200 50 " + "   SimpleReaction SIMPLE_REACTION_ABC 75 150 " + "   SimpleReaction SIMPLE_REACION_CDA 200 125 " + "} ";
    org.vcell.util.CommentStringTokenizer st = new org.vcell.util.CommentStringTokenizer(diagramFile);
    diagram.fromTokens(st);
    return model;
}
Also used : ModelParameter(cbit.vcell.model.Model.ModelParameter) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression)

Example 10 with VCUnitDefinition

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

the class VCUnitEvaluator method computeUnit.

// public VCUnitDefinition computeUnit(VCUnitDefinition units []) throws VCUnitException {
// return computeUnit(units, false);
// }
public VCUnitDefinition computeUnit(SimpleNode parent, VCUnitDefinition[] units, boolean assignTBDs) throws VCUnitException {
    if (units == null || units.length == 0)
        return null;
    VCUnitDefinition unit = unitSystem.getInstance_TBD();
    boolean first = true;
    for (int i = 0; i < units.length; i++) {
        if (units[i].isTBD()) {
            continue;
        } else {
            if (first) {
                unit = units[i];
                first = false;
                continue;
            }
        }
        if (!unit.isEquivalent(units[i])) {
            throw new VCUnitException("Incompatible units [" + unit.getSymbol() + "] and [" + units[i].getSymbol() + "] in Expression: " + parent.infixString(SimpleNode.LANGUAGE_DEFAULT));
        }
    }
    if (assignTBDs && unit != null) {
        for (int i = 0; i < units.length; i++) {
            if (units[i].isTBD()) {
                units[i] = unit;
            }
        }
    }
    return unit;
}
Also used : VCUnitException(cbit.vcell.units.VCUnitException) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition)

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