use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.
the class XmlReader method readParameters.
private void readParameters(List<Element> parameterElements, ParameterContext parameterContext, HashMap<String, ParameterRoleEnum> roleHash, ParameterRoleEnum userDefinedRole, HashSet<String> xmlRolesTagsToIgnore, Model model) throws XmlParseException {
String contextName = parameterContext.getNameScope().getName();
try {
//
// prepopulate varHash with reserved symbols
//
VariableHash varHash = new VariableHash();
addResevedSymbols(varHash, model);
//
for (Element xmlParam : parameterElements) {
String parsedParamName = unMangle(xmlParam.getAttributeValue(XMLTags.NameAttrTag));
String parsedRoleString = xmlParam.getAttributeValue(XMLTags.ParamRoleAttrTag);
String parsedExpressionString = xmlParam.getText();
//
if (xmlRolesTagsToIgnore.contains(parsedRoleString)) {
varHash.removeVariable(parsedParamName);
continue;
}
Expression paramExp = null;
if (parsedExpressionString.trim().length() > 0) {
paramExp = unMangleExpression(parsedExpressionString);
}
if (varHash.getVariable(parsedParamName) == null) {
Domain domain = null;
varHash.addVariable(new Function(parsedParamName, paramExp, domain));
} else {
if (model.getReservedSymbolByName(parsedParamName) != null) {
varHash.removeVariable(parsedParamName);
Domain domain = null;
varHash.addVariable(new Function(parsedParamName, paramExp, domain));
}
}
//
// get the parameter for this xml role string
//
ParameterRoleEnum paramRole = roleHash.get(parsedRoleString);
if (paramRole == null) {
throw new XmlParseException("parameter '" + parsedParamName + "' has unexpected role '" + parsedRoleString + "' in '" + contextName + "'");
}
//
if (paramRole != userDefinedRole) {
LocalParameter paramWithSameRole = parameterContext.getLocalParameterFromRole(paramRole);
if (paramWithSameRole == null) {
throw new XmlParseException("can't find parameter with role '" + parsedRoleString + "' in '" + contextName + "'");
}
//
if (!paramWithSameRole.getName().equals(parsedParamName)) {
//
// first rename other parameters with same name
//
LocalParameter paramWithSameNameButDifferentRole = parameterContext.getLocalParameterFromName(parsedParamName);
if (paramWithSameNameButDifferentRole != null) {
//
// find available name
//
int n = 0;
String newName = parsedParamName + "_" + n++;
while (parameterContext.getEntry(newName) != null) {
newName = parsedParamName + "_" + n++;
}
parameterContext.renameLocalParameter(parsedParamName, newName);
}
//
// then rename parameter with correct role
//
parameterContext.renameLocalParameter(paramWithSameRole.getName(), parsedParamName);
}
}
}
//
// create unresolved parameters for all unresolved symbols
//
String unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
while (unresolvedSymbol != null) {
try {
Domain domain = null;
// will turn into an UnresolvedParameter.
varHash.addVariable(new Function(unresolvedSymbol, new Expression(0.0), domain));
} catch (MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException(e.getMessage());
}
parameterContext.addUnresolvedParameter(unresolvedSymbol);
unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
}
//
// in topological order, add parameters to model (getting units also).
// note that all pre-defined parameters already have the correct names
// here we set expressions on pre-defined parameters and add user-defined parameters
//
Variable[] sortedVariables = varHash.getTopologicallyReorderedVariables();
ModelUnitSystem modelUnitSystem = model.getUnitSystem();
for (int i = sortedVariables.length - 1; i >= 0; i--) {
if (sortedVariables[i] instanceof Function) {
Function paramFunction = (Function) sortedVariables[i];
Element xmlParam = null;
for (int j = 0; j < parameterElements.size(); j++) {
Element tempParam = (Element) parameterElements.get(j);
if (paramFunction.getName().equals(unMangle(tempParam.getAttributeValue(XMLTags.NameAttrTag)))) {
xmlParam = tempParam;
break;
}
}
if (xmlParam == null) {
// must have been an unresolved parameter
continue;
}
String symbol = xmlParam.getAttributeValue(XMLTags.VCUnitDefinitionAttrTag);
VCUnitDefinition unit = null;
if (symbol != null) {
unit = modelUnitSystem.getInstance(symbol);
}
LocalParameter tempParam = parameterContext.getLocalParameterFromName(paramFunction.getName());
if (tempParam == null) {
tempParam = parameterContext.addLocalParameter(paramFunction.getName(), new Expression(0.0), userDefinedRole, unit, userDefinedRole.getDescription());
parameterContext.setParameterValue(tempParam, paramFunction.getExpression(), true);
} else {
if (tempParam.getExpression() != null) {
// if the expression is null, it should remain null.
parameterContext.setParameterValue(tempParam, paramFunction.getExpression(), true);
}
tempParam.setUnitDefinition(unit);
}
}
}
} catch (PropertyVetoException | ExpressionException | MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException("Exception while setting parameters for '" + contextName + "': " + e.getMessage(), e);
}
}
use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.
the class XmlReader method getElectricalStimulus.
/**
* This method process Electrical Stimulus, also called Clamps.
* Creation date: (6/6/2002 4:46:18 PM)
* @return cbit.vcell.mapping.ElectricalStimulus
* @param param org.jdom.Element
*/
private ElectricalStimulus getElectricalStimulus(Element param, SimulationContext currentSimulationContext) throws XmlParseException {
ElectricalStimulus clampStimulus = null;
// get name
// String name = unMangle( param.getAttributeValue(XMLTags.NameAttrTag) );
// get Electrode
Electrode electrode = getElectrode(param.getChild(XMLTags.ElectrodeTag, vcNamespace), currentSimulationContext);
if (param.getAttributeValue(XMLTags.TypeAttrTag).equalsIgnoreCase(XMLTags.VoltageClampTag)) {
// is a voltage clamp
clampStimulus = new VoltageClampStimulus(electrode, "voltClampElectrode", new Expression(0.0), currentSimulationContext);
} else if (param.getAttributeValue(XMLTags.TypeAttrTag).equalsIgnoreCase(XMLTags.CurrentDensityClampTag) || param.getAttributeValue(XMLTags.TypeAttrTag).equalsIgnoreCase(XMLTags.CurrentDensityClampTag_oldName)) {
// is a current density clamp
clampStimulus = new CurrentDensityClampStimulus(electrode, "currDensityClampElectrode", new Expression(0.0), currentSimulationContext);
} else if (param.getAttributeValue(XMLTags.TypeAttrTag).equalsIgnoreCase(XMLTags.TotalCurrentClampTag)) {
// is a "total" current clamp
clampStimulus = new TotalCurrentClampStimulus(electrode, "totalCurrClampElectrode", new Expression(0.0), currentSimulationContext);
}
try {
// transaction begin flag ... yeah, this is a hack
clampStimulus.reading(true);
// Read all of the parameters
List<Element> list = param.getChildren(XMLTags.ParameterTag, vcNamespace);
// add constants that may be used in the electrical stimulus.
VariableHash varHash = new VariableHash();
Model model = currentSimulationContext.getModel();
addResevedSymbols(varHash, model);
//
for (Element xmlParam : list) {
String paramName = unMangle(xmlParam.getAttributeValue(XMLTags.NameAttrTag));
String role = xmlParam.getAttributeValue(XMLTags.ParamRoleAttrTag);
String paramExpStr = xmlParam.getText();
Expression paramExp = unMangleExpression(paramExpStr);
try {
if (varHash.getVariable(paramName) == null) {
Domain domain = null;
varHash.addVariable(new Function(paramName, paramExp, domain));
} else {
if (model.getReservedSymbolByName(paramName) != null) {
varHash.removeVariable(paramName);
Domain domain = null;
varHash.addVariable(new Function(paramName, paramExp, domain));
}
}
} catch (MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException("error reordering parameters according to dependencies:", e);
}
LocalParameter tempParam = null;
if (!role.equals(XMLTags.ParamRoleUserDefinedTag)) {
if (role.equals(XMLTags.ParamRoleTotalCurrentTag)) {
if (clampStimulus instanceof TotalCurrentClampStimulus) {
tempParam = ((TotalCurrentClampStimulus) clampStimulus).getCurrentParameter();
} else {
varHash.removeVariable(paramName);
continue;
}
} else if (role.equals(XMLTags.ParamRoleTotalCurrentDensityTag) || role.equals(XMLTags.ParamRoleTotalCurrentDensityOldNameTag)) {
if (clampStimulus instanceof CurrentDensityClampStimulus) {
tempParam = ((CurrentDensityClampStimulus) clampStimulus).getCurrentDensityParameter();
} else {
varHash.removeVariable(paramName);
continue;
}
} else if (role.equals(XMLTags.ParamRolePotentialDifferenceTag)) {
if (clampStimulus instanceof VoltageClampStimulus) {
tempParam = ((VoltageClampStimulus) clampStimulus).getVoltageParameter();
} else {
varHash.removeVariable(paramName);
continue;
}
}
} else {
continue;
}
if (tempParam == null) {
throw new XmlParseException("parameter with role '" + role + "' not found in electricalstimulus");
}
//
if (!tempParam.getName().equals(paramName)) {
LocalParameter multNameParam = clampStimulus.getLocalParameter(paramName);
int n = 0;
while (multNameParam != null) {
String tempName = paramName + "_" + n++;
clampStimulus.renameParameter(paramName, tempName);
multNameParam = clampStimulus.getLocalParameter(tempName);
}
clampStimulus.renameParameter(tempParam.getName(), paramName);
}
}
//
// create unresolved parameters for all unresolved symbols
//
String unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
while (unresolvedSymbol != null) {
try {
Domain domain = null;
// will turn into an UnresolvedParameter.
varHash.addVariable(new Function(unresolvedSymbol, new Expression(0.0), domain));
} catch (MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException(e.getMessage());
}
clampStimulus.addUnresolvedParameter(unresolvedSymbol);
unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
}
Variable[] sortedVariables = varHash.getTopologicallyReorderedVariables();
ModelUnitSystem modelUnitSystem = model.getUnitSystem();
for (int i = sortedVariables.length - 1; i >= 0; i--) {
if (sortedVariables[i] instanceof Function) {
Function paramFunction = (Function) sortedVariables[i];
Element xmlParam = null;
for (int j = 0; j < list.size(); j++) {
Element tempParam = (Element) list.get(j);
if (paramFunction.getName().equals(unMangle(tempParam.getAttributeValue(XMLTags.NameAttrTag)))) {
xmlParam = tempParam;
break;
}
}
if (xmlParam == null) {
// must have been an unresolved parameter
continue;
}
String symbol = xmlParam.getAttributeValue(XMLTags.VCUnitDefinitionAttrTag);
VCUnitDefinition unit = null;
if (symbol != null) {
unit = modelUnitSystem.getInstance(symbol);
}
LocalParameter tempParam = clampStimulus.getLocalParameter(paramFunction.getName());
if (tempParam == null) {
clampStimulus.addUserDefinedParameter(paramFunction.getName(), paramFunction.getExpression(), unit);
} else {
if (tempParam.getExpression() != null) {
// if the expression is null, it should remain null.
clampStimulus.setParameterValue(tempParam, paramFunction.getExpression());
}
tempParam.setUnitDefinition(unit);
}
}
}
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace(System.out);
throw new XmlParseException("Exception while setting parameters for simContext : " + currentSimulationContext.getName(), e);
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new XmlParseException("Exception while settings parameters for simContext : " + currentSimulationContext.getName(), e);
} finally {
clampStimulus.reading(false);
}
return clampStimulus;
}
use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.
the class XmlReader method getSimulationContextParameter.
public SimulationContextParameter getSimulationContextParameter(Element paramElement, SimulationContext simContext) {
// get its attributes : name, role and unit definition
String appParamName = unMangle(paramElement.getAttributeValue(XMLTags.NameAttrTag));
String role = paramElement.getAttributeValue(XMLTags.ParamRoleAttrTag);
ModelUnitSystem modelUnitSystem = simContext.getModel().getUnitSystem();
int appParamRole = -1;
if (role.equals(XMLTags.ParamRoleUserDefinedTag)) {
appParamRole = SimulationContext.ROLE_UserDefined;
} else {
throw new RuntimeException("unknown type of application parameter (not user-defined)");
}
String unitSymbol = paramElement.getAttributeValue(XMLTags.VCUnitDefinitionAttrTag);
VCUnitDefinition appParamUnit = null;
if (unitSymbol != null) {
appParamUnit = modelUnitSystem.getInstance(unitSymbol);
}
// get parameter contents : expression; annotation, if any.
String appParamExpStr = paramElement.getText();
Expression appParamExp = unMangleExpression(appParamExpStr);
// String appParamAnnotation = null;
// String annotationText = paramElement.getChildText(XMLTags.AnnotationTag, vcNamespace);
// if (annotationText != null && annotationText.length() > 0) {
// appParamAnnotation = unMangle(annotationText);
// }
// create new global parameter
SimulationContextParameter newAppParam = simContext.new SimulationContextParameter(appParamName, appParamExp, appParamRole, appParamUnit);
return newAppParam;
}
use of cbit.vcell.model.ModelUnitSystem 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.model.ModelUnitSystem in project vcell by virtualcell.
the class BioModelParametersPanel method changeUnitsButtonPressed.
public void changeUnitsButtonPressed() {
UnitSystemSelectionPanel unitSystemSelectionPanel = new UnitSystemSelectionPanel();
unitSystemSelectionPanel.initialize(bioModel.getModel().getUnitSystem());
int retcode = DialogUtils.showComponentOKCancelDialog(this, unitSystemSelectionPanel, "select new unit system");
while (retcode == JOptionPane.OK_OPTION) {
ModelUnitSystem forcedModelUnitSystem;
try {
forcedModelUnitSystem = unitSystemSelectionPanel.createModelUnitSystem();
BioModel newBioModel = ModelUnitConverter.createBioModelWithNewUnitSystem(bioModel, forcedModelUnitSystem);
this.bioModelWindowManager.resetDocument(newBioModel);
break;
} catch (Exception e) {
e.printStackTrace(System.out);
DialogUtils.showErrorDialog(this, e.getMessage(), e);
retcode = DialogUtils.showComponentOKCancelDialog(this, unitSystemSelectionPanel, "select new unit system");
}
}
}
Aggregations