Search in sources :

Example 1 with Kind

use of org.sbml.jsbml.Unit.Kind 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 2 with Kind

use of org.sbml.jsbml.Unit.Kind 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 3 with Kind

use of org.sbml.jsbml.Unit.Kind in project vcell by virtualcell.

the class SBMLUnitTranslator method convertVCUnitsToSbmlUnits_NOT_USED.

/**
 *   BaseUnit definitions copied from ucar.units_vcell.SI (as modified for Virtual Cell and SBML ... including item and molecules)
 *
 *   (PRIMARY BASE SI UNITS)
 *   AMPERE = 		bu("ampere",    "A",   BaseQuantity.ELECTRIC_CURRENT)
 *   CANDELA = 		bu("candela",   "cd",  BaseQuantity.LUMINOUS_INTENSITY)
 *   KELVIN = 		bu("kelvin",    "K",   BaseQuantity.THERMODYNAMIC_TEMPERATURE)
 *   KILOGRAM = 	bu("kilogram",  "kg",  BaseQuantity.MASS)
 *   METER = 		bu("metre",     "m",   BaseQuantity.LENGTH)	// NIST
 *   METRE = 		bu("metre",     "m",   BaseQuantity.LENGTH)	// ISO
 *   MOLE = 		bu("mole",      "mol", BaseQuantity.AMOUNT_OF_SUBSTANCE)
 *   SECOND = 		bu("second",    "s",   BaseQuantity.TIME)
 *
 *   (SUPPLEMENTARY BASE SI UNITS ... DIMENSIONLESS DERIVED UNITS) ... note that BaseUnit
 *   RADIAN = 		bu("radian",    "rad", BaseQuantity.PLANE_ANGLE)
 *   STERADIAN = 	bu("steradian", "sr",  BaseQuantity.SOLID_ANGLE)
 *
 *   (VCELL/SBML introduced base SI unit for items/molecules)
 *   ITEM =			bu("item",      "item", BaseQuantity.ITEM)
 */
/**
 * @param unitMultiplier
 * @param vcUcarUnit
 * @param allSbmlUnitsList
 * @param level
 * @param version
 * @return
 */
/*
 *  convertVCUnitsToSbmlUnits :
 *  --------- !!!! Ignoring OFFSET for UNITS, since SBML L2V2 gets rid of the offset field. !!!! ---------
 */
private static ArrayList<Unit> convertVCUnitsToSbmlUnits_NOT_USED(double unitMultiplier, ucar.units_vcell.Unit vcUcarUnit, ArrayList<Unit> allSbmlUnitsList, int level, int version) {
    int unitScale = 0;
    if (vcUcarUnit instanceof ucar.units_vcell.UnitImpl) {
        ucar.units_vcell.UnitImpl unitImpl = (ucar.units_vcell.UnitImpl) vcUcarUnit;
        if (unitImpl instanceof ucar.units_vcell.DerivedUnitImpl) {
            ucar.units_vcell.DerivedUnitImpl baseUnit = (ucar.units_vcell.DerivedUnitImpl) unitImpl;
            ucar.units_vcell.Factor[] factors = baseUnit.getDimension().getFactors();
            for (int i = 0; i < factors.length; i++) {
                ucar.units_vcell.RationalNumber exponent = factors[i].getExponent();
                String baseName = ((ucar.units_vcell.BaseUnit) factors[i].getBase()).getName();
                Unit sbmlUnit = null;
                if (factors.length > 1) {
                    // To avoid that, add a dimensionless unit.
                    if (i == 0) {
                        Unit dimensionlessUnit = new Unit(level, version);
                        dimensionlessUnit.setKind(Kind.DIMENSIONLESS);
                        dimensionlessUnit.setExponent(1);
                        dimensionlessUnit.setScale(unitScale);
                        dimensionlessUnit.setMultiplier(unitMultiplier);
                        allSbmlUnitsList.add(dimensionlessUnit);
                    }
                    sbmlUnit = new Unit(level, version);
                    Kind kind = Kind.valueOf(baseName);
                    sbmlUnit.setKind(kind);
                    sbmlUnit.setExponent(exponent.intValue());
                    sbmlUnit.setScale(unitScale);
                    sbmlUnit.setMultiplier(1.0);
                    allSbmlUnitsList.add(sbmlUnit);
                } else {
                    sbmlUnit = new Unit(level, version);
                    Kind kind = Kind.valueOf(baseName);
                    sbmlUnit.setKind(kind);
                    sbmlUnit.setExponent(exponent.intValue());
                    sbmlUnit.setScale(unitScale);
                    sbmlUnit.setMultiplier(Math.pow(unitMultiplier, exponent.inverse().doubleValue()));
                    allSbmlUnitsList.add(sbmlUnit);
                }
            }
            return allSbmlUnitsList;
        } else if (unitImpl instanceof ucar.units_vcell.ScaledUnit) {
            ucar.units_vcell.ScaledUnit multdUnit = (ucar.units_vcell.ScaledUnit) unitImpl;
            unitMultiplier *= multdUnit.getScale();
            if (multdUnit.getUnit() != multdUnit.getDerivedUnit()) {
                return convertVCUnitsToSbmlUnits_NOT_USED(unitMultiplier, multdUnit.getUnit(), allSbmlUnitsList, level, version);
            }
        }
        /**
         *** COMMENTED OUT SINCE OFFSET IS NOT GOING TO BE USED FROM SBML L2 V2 ... ****
         *		  else if (unitImpl instanceof ucar.units_vcell.OffsetUnit) {
         *			ucar.units_vcell.OffsetUnit offsetUnit = (ucar.units_vcell.OffsetUnit)unitImpl;
         *			unitOffset += offsetUnit.getOffset();
         *			if (offsetUnit.getUnit() != offsetUnit.getDerivedUnit()){
         *				return convertVCUnitsToSbmlUnits(unitMultiplier, offsetUnit.getUnit(), allSbmlUnitsList);
         *			}
         *		}
         */
        if (unitImpl.getDerivedUnit() != vcUcarUnit) {
            // i.e. we have not reached the base unit, yet
            return convertVCUnitsToSbmlUnits_NOT_USED(unitMultiplier, unitImpl.getDerivedUnit(), allSbmlUnitsList, level, version);
        }
    } else {
        System.err.println("Unable to process unit translation: " + " " + vcUcarUnit.getSymbol());
    }
    throw new RuntimeException("unexpected vcell unit during transformation to sbml units: " + vcUcarUnit);
// return null;
}
Also used : Unit(org.sbml.jsbml.Unit) Kind(org.sbml.jsbml.Unit.Kind)

Aggregations

Kind (org.sbml.jsbml.Unit.Kind)3 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)2 Unit (org.sbml.jsbml.Unit)2 UnitDefinition (org.sbml.jsbml.UnitDefinition)1