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;
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations