Search in sources :

Example 1 with Unit

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

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

use of org.sbml.jsbml.Unit 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)

Example 4 with Unit

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

the class SBMLSpatialTest method test.

// @Test
public void test() throws Exception {
    // BioModel bioModel1 = BioModelTest.getExampleWithImage();
    URL vcmlURL = SBMLSpatialTest.class.getResource("Solver_Suite_6_2.vcml");
    File vcmlFile = new File(vcmlURL.toURI());
    BioModel bioModel1 = XmlHelper.XMLToBioModel(new XMLSource(vcmlFile));
    bioModel1.refreshDependencies();
    // for (int i = 0; i<bioModel1.getNumSimulationContexts(); i++){
    for (int i = 5; i == 5; i++) {
        SimulationContext sc1 = bioModel1.getSimulationContext(i);
        if (sc1.getApplicationType() != Application.NETWORK_DETERMINISTIC) {
            System.err.println(sc1.getName() + " is not a network determistic application");
            continue;
        }
        boolean isSpatial = sc1.getGeometry().getDimension() > 0;
        SBMLExporter exporter = new SBMLExporter(bioModel1, 3, 1, isSpatial);
        sc1.refreshMathDescription(null, NetworkGenerationRequirements.ComputeFullNoTimeout);
        // sc1.setMathDescription(sc1.createNewMathMapping(null, NetworkGenerationRequirements.ComputeFullNoTimeout).getMathDescription());
        exporter.setSelectedSimContext(sc1);
        VCellSBMLDoc sbmlDoc = exporter.convertToSBML();
        for (UnitDefinition unitDefn : sbmlDoc.model.getListOfUnitDefinitions()) {
            for (Unit unit : unitDefn.getListOfUnits()) {
                System.out.println(unit.getKind());
                if (!unit.isSetKind()) {
                    throw new RuntimeException("kind of unit " + unit.printUnit() + " of UnitDefn " + UnitDefinition.printUnits(unitDefn) + " is not set");
                }
            }
        }
        // sbmlDoc.document.setConsistencyChecks(CHECK_CATEGORY.UNITS_CONSISTENCY, false);
        // int numErrors = sbmlDoc.document.checkConsistency();
        // System.out.println("consistency check, num errors = "+numErrors);
        // if (numErrors>0){
        // SBMLErrorLog errorLog = sbmlDoc.document.getListOfErrors();
        // for (int err=0; err<errorLog.getErrorCount(); err++){
        // System.err.println("ERROR IN EXPORTED SBML: "+errorLog.getError(err).getMessage());
        // }
        // //Assert.fail("generated SBML document was found to be inconsistent");
        // }
        String sbmlString = sbmlDoc.xmlString;
        File tempFile = File.createTempFile("sbmlSpatialTest_SBML_", ".sbml.xml");
        FileUtils.write(tempFile, sbmlString);
        System.out.println(tempFile);
        try {
            VCLogger argVCLogger = new TLogger();
            SBMLImporter importer = new SBMLImporter(tempFile.getAbsolutePath(), argVCLogger, isSpatial);
            BioModel bioModel2 = importer.getBioModel();
            File tempFile2 = File.createTempFile("sbmlSpatialTest_Biomodel_", ".vcml.xml");
            FileUtils.write(tempFile2, XmlHelper.bioModelToXML(bioModel2));
            System.out.println(tempFile2);
            // if (true) { throw new RuntimeException("stop"); }
            bioModel2.refreshDependencies();
            SimulationContext sc2 = bioModel2.getSimulationContext(0);
            // sc2.refreshMathDescription(null, NetworkGenerationRequirements.ComputeFullNoTimeout);
            sc2.setMathDescription(sc2.createNewMathMapping(null, NetworkGenerationRequirements.ComputeFullNoTimeout).getMathDescription());
            if (!sc1.getMathDescription().isValid()) {
                throw new RuntimeException("sc1.math is not valid");
            }
            if (!sc2.getMathDescription().isValid()) {
                throw new RuntimeException("sc2.math is not valid");
            }
            MathCompareResults mathCompareResults = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(), sc1.getMathDescription(), sc2.getMathDescription());
            if (!mathCompareResults.isEquivalent()) {
                System.out.println("MATH DESCRIPTION 1 <UNCHANGED>");
                System.out.println(sc1.getMathDescription().getVCML_database());
                System.out.println("MATH DESCRIPTION 2 <UNCHANGED>");
                System.out.println(sc2.getMathDescription().getVCML_database());
                // if (mathCompareResults.decision  == Decision.MathDifferent_SUBDOMAINS_DONT_MATCH){
                // BioModel bioModel1_copy = XmlHelper.XMLToBioModel(new XMLSource(vcmlFile));
                // bioModel1_copy.refreshDependencies();
                // SimulationContext sc1_copy = bioModel1_copy.getSimulationContext(i);
                // VCImage image = sc1_copy.getGeometry().getGeometrySpec().getImage();
                // if (image!=null){
                // ArrayList<VCPixelClass> pcList = new ArrayList<VCPixelClass>();
                // for (VCPixelClass pc : image.getPixelClasses()){
                // pcList.add(new VCPixelClass(pc.getKey(),SBMLExporter.DOMAIN_TYPE_PREFIX+pc.getPixelClassName(),pc.getPixel()));
                // }
                // image.setPixelClasses(pcList.toArray(new VCPixelClass[0]));
                // }
                // for (GeometryClass gc : sc1_copy.getGeometry().getGeometryClasses()){
                // System.out.println("name before "+gc.getName());
                // gc.setName(SBMLExporter.DOMAIN_TYPE_PREFIX+gc.getName());
                // System.out.println("name after "+gc.getName());
                // }
                // sc1_copy.checkValidity();
                // bioModel1_copy.refreshDependencies();
                // sc1_copy.getGeometry().precomputeAll(new GeometryThumbnailImageFactoryAWT(), true, true);
                // sc1_copy.setMathDescription(sc1_copy.createNewMathMapping(null, NetworkGenerationRequirements.ComputeFullNoTimeout).getMathDescription());
                // MathCompareResults mathCompareResults_renamedSubdomains = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(),sc1_copy.getMathDescription(), sc2.getMathDescription());
                // if (!mathCompareResults_renamedSubdomains.isEquivalent()){
                // System.out.println("MATH DESCRIPTION 1 <RENAMED>");
                // System.out.println(sc1_copy.getMathDescription().getVCML_database());
                // Assert.fail(mathCompareResults_renamedSubdomains.decision+" "+mathCompareResults_renamedSubdomains.details);
                // }
                // }else{
                System.err.println(mathCompareResults.decision + " " + mathCompareResults.details);
            // }
            } else {
                System.out.println("MATHS WERE EQUIVALENT");
            }
        } finally {
            tempFile.delete();
        }
    }
    // loop over determinstic applications
    System.out.println("done");
}
Also used : SBMLImporter(org.vcell.sbml.vcell.SBMLImporter) SBMLExporter(org.vcell.sbml.vcell.SBMLExporter) SimulationContext(cbit.vcell.mapping.SimulationContext) Unit(org.sbml.jsbml.Unit) URL(java.net.URL) BioModel(cbit.vcell.biomodel.BioModel) VCellSBMLDoc(org.vcell.sbml.vcell.SBMLExporter.VCellSBMLDoc) MathCompareResults(cbit.vcell.math.MathCompareResults) File(java.io.File) XMLSource(cbit.vcell.xml.XMLSource) UnitDefinition(org.sbml.jsbml.UnitDefinition) VCLogger(cbit.util.xml.VCLogger)

Example 5 with Unit

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

the class SBMLUnitTranslatorTest method testSBMLtoVCell.

@Test
public void testSBMLtoVCell() throws XMLStreamException, IOException, SbmlException {
    File[] sbmlFiles = getBiomodelsCuratedSBMLFiles();
    // };
    for (File sbmlFile : sbmlFiles) {
        if (sbmlFile.getName().equals("BIOMD0000000539.xml")) {
            System.err.println("skipping this model, seems like a bug in jsbml  RenderParser.processEndDocument() ... line 403 ... wrong constant for extension name");
            continue;
        }
        SBMLDocument doc = SBMLReader.read(sbmlFile);
        BioModel bioModel = new BioModel(null);
        VCUnitSystem unitSystem = bioModel.getModel().getUnitSystem();
        Model sbmlModel = doc.getModel();
        ListOf<UnitDefinition> listOfUnitDefinitions = sbmlModel.getListOfUnitDefinitions();
        for (UnitDefinition sbmlUnitDef : listOfUnitDefinitions) {
            VCUnitDefinition vcUnit = SBMLUnitTranslator.getVCUnitDefinition(sbmlUnitDef, unitSystem);
            UnitDefinition new_sbmlUnitDef = SBMLUnitTranslator.getSBMLUnitDefinition(vcUnit, 3, 1, unitSystem);
            VCUnitDefinition new_vcUnit = SBMLUnitTranslator.getVCUnitDefinition(new_sbmlUnitDef, unitSystem);
            if (!vcUnit.getSymbol().equals(new_vcUnit.getSymbol())) {
                System.err.println("orig vcUnit '" + vcUnit.getSymbol() + "' doesn't match new vcUnit '" + new_vcUnit.getSymbol() + "'");
            }
            // System.out.println("sbmlUnit = "+sbmlUnitDef.toString()+", vcUnit = "+vcUnit.getSymbol());
            System.out.println("sbmlUnit(" + sbmlUnitDef.getClass().getName() + ", builtin=" + sbmlUnitDef.isVariantOfSubstance() + ") = " + sbmlUnitDef.toString() + ", id=" + sbmlUnitDef.getId() + ",  name=" + sbmlUnitDef.getName() + ",   vcUnit = " + vcUnit.getSymbol());
            if (sbmlUnitDef.getNumUnits() > 1) {
                System.out.println("vcUnit = " + vcUnit.getSymbol());
                for (Unit unit : sbmlUnitDef.getListOfUnits()) {
                    try {
                        // VCUnitDefinition vcUnit = unitSystem.getInstance(unit.getKind().getName());
                        System.out.println("    vcUnit = " + unit);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("found bigger unit, " + sbmlUnitDef);
            }
        }
        if (sbmlFile == sbmlFiles[0]) {
            System.out.println("sbml length unit = " + sbmlModel.getLengthUnitsInstance() + ", idref=" + sbmlModel.getLengthUnits());
            System.out.println("sbml area unit = " + sbmlModel.getAreaUnitsInstance() + ", idref=" + sbmlModel.getAreaUnits());
            System.out.println("sbml volume unit = " + sbmlModel.getVolumeUnitsInstance() + ", idref=" + sbmlModel.getVolumeUnits());
            System.out.println("sbml time unit = " + sbmlModel.getTimeUnitsInstance() + ", idref=" + sbmlModel.getTimeUnits());
            System.out.println("sbml extent unit = " + sbmlModel.getExtentUnitsInstance() + ", idref=" + sbmlModel.getExtentUnits());
            System.out.println("sbml substance unit = " + sbmlModel.getSubstanceUnitsInstance() + ", idref=" + sbmlModel.getSubstanceUnits());
            for (UnitDefinition sbmlUnitDef : sbmlModel.getListOfPredefinedUnitDefinitions()) {
                if (sbmlUnitDef.getNumUnits() == 1 && sbmlUnitDef.getUnit(0).isAvogadro()) {
                    continue;
                }
                if (sbmlUnitDef.getNumUnits() == 1 && sbmlUnitDef.getUnit(0).isKatal()) {
                    continue;
                }
                VCUnitDefinition vcUnit = SBMLUnitTranslator.getVCUnitDefinition(sbmlUnitDef, unitSystem);
                // System.out.println("sbmlUnit = "+sbmlUnitDef.toString()+", vcUnit = "+vcUnit.getSymbol());
                System.out.println("sbmlUnit(" + sbmlUnitDef.getClass().getName() + ", builtin=" + sbmlUnitDef.isVariantOfSubstance() + ") = " + sbmlUnitDef.toString() + ", id=" + sbmlUnitDef.getId() + ",  name=" + sbmlUnitDef.getName() + ",   vcUnit = " + vcUnit.getSymbol());
            // for (Unit unit : sbmlUnitDef.getListOfUnits()){
            // try {
            // VCUnitDefinition vcUnit = unitSystem.getInstance(unit.getKind().getName());
            // System.out.println("    vcUnit = "+vcUnit.getSymbol());
            // }catch (Exception e){
            // e.printStackTrace();
            // }
            // }
            }
        }
    }
}
Also used : VCUnitSystem(cbit.vcell.units.VCUnitSystem) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) SBMLDocument(org.sbml.jsbml.SBMLDocument) BioModel(cbit.vcell.biomodel.BioModel) Model(org.sbml.jsbml.Model) BioModel(cbit.vcell.biomodel.BioModel) Unit(org.sbml.jsbml.Unit) File(java.io.File) UnitDefinition(org.sbml.jsbml.UnitDefinition) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) Test(org.junit.Test)

Aggregations

Unit (org.sbml.jsbml.Unit)5 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)3 UnitDefinition (org.sbml.jsbml.UnitDefinition)3 BioModel (cbit.vcell.biomodel.BioModel)2 File (java.io.File)2 Kind (org.sbml.jsbml.Unit.Kind)2 VCLogger (cbit.util.xml.VCLogger)1 SimulationContext (cbit.vcell.mapping.SimulationContext)1 MathCompareResults (cbit.vcell.math.MathCompareResults)1 VCUnitSystem (cbit.vcell.units.VCUnitSystem)1 XMLSource (cbit.vcell.xml.XMLSource)1 IOException (java.io.IOException)1 URL (java.net.URL)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Test (org.junit.Test)1 Model (org.sbml.jsbml.Model)1 SBMLDocument (org.sbml.jsbml.SBMLDocument)1 SBMLExporter (org.vcell.sbml.vcell.SBMLExporter)1 VCellSBMLDoc (org.vcell.sbml.vcell.SBMLExporter.VCellSBMLDoc)1 SBMLImporter (org.vcell.sbml.vcell.SBMLImporter)1