use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.
the class AbstractStochMathMapping method getExpressionAmtToConc.
/**
* getExpressionAmtToConc : converts the particles expression ('particlesExpr') to an expression for concentration.
* If argument 'speciesContext' is on a membrane, concExpr = particlesExpr/size_of_Mem. If 'speciesContext' is in
* feature, concExpr = (particlesExpr/size_of_Feature)*KMOLE.
* @param particlesExpr
* @param speciesContext
* @return
* @throws MappingException
* @throws ExpressionException
*/
protected Expression getExpressionAmtToConc(Expression particlesExpr, Structure structure) throws MappingException, ExpressionException {
ModelUnitSystem unitSystem = getSimulationContext().getModel().getUnitSystem();
VCUnitDefinition substanceUnit = unitSystem.getSubstanceUnit(structure);
Expression unitFactor = getUnitFactor(substanceUnit.divideBy(unitSystem.getStochasticSubstanceUnit()));
Expression scStructureSize = new Expression(structure.getStructureSize(), getNameScope());
Expression concentrationExpr = Expression.mult(particlesExpr, Expression.div(unitFactor, scStructureSize));
return concentrationExpr;
}
use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.
the class BioModelParametersTableModel method setValueAt.
public void setValueAt(Object value, int row, int col) {
if (value == null) {
return;
}
try {
String inputValue = (String) value;
inputValue = inputValue.trim();
EditableSymbolTableEntry parameter = getValueAt(row);
switch(col) {
case COLUMN_NAME:
{
if (inputValue.length() == 0) {
return;
}
parameter.setName(inputValue);
break;
}
case COLUMN_EXPRESSION:
{
String newExpressionString = inputValue;
if (parameter instanceof SpeciesContextSpec.SpeciesContextSpecParameter) {
SpeciesContextSpec.SpeciesContextSpecParameter scsParm = (SpeciesContextSpec.SpeciesContextSpecParameter) parameter;
Expression newExp = null;
if (newExpressionString == null || newExpressionString.trim().length() == 0) {
if (scsParm.getRole() == SpeciesContextSpec.ROLE_InitialConcentration || scsParm.getRole() == SpeciesContextSpec.ROLE_DiffusionRate || scsParm.getRole() == SpeciesContextSpec.ROLE_InitialCount) {
newExp = new Expression(0.0);
}
} else {
newExp = new Expression(newExpressionString);
}
scsParm.setExpression(newExp);
} else if (parameter instanceof KineticsParameter) {
Expression exp1 = new Expression(inputValue);
Kinetics kinetics = ((KineticsParameter) parameter).getKinetics();
kinetics.setParameterValue((Kinetics.KineticsParameter) parameter, exp1);
} else {
Expression exp1 = new Expression(inputValue);
exp1.bindExpression(parameter.getNameScope().getScopedSymbolTable());
parameter.setExpression(exp1);
}
break;
}
case COLUMN_UNIT:
{
ModelUnitSystem modelUnitSystem = getModel().getUnitSystem();
if (inputValue.length() == 0) {
parameter.setUnitDefinition(modelUnitSystem.getInstance_TBD());
} else {
if (!parameter.getUnitDefinition().getSymbol().equals(inputValue)) {
parameter.setUnitDefinition(modelUnitSystem.getInstance(inputValue));
}
}
break;
}
}
} catch (Exception e) {
e.printStackTrace(System.out);
DialogUtils.showErrorDialog(ownerTable, e.getMessage());
}
}
use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.
the class XmlReader method getKinetics.
/**
* This method returns a Kinetics object from a XML Element based on the value of the kinetics type attribute.
* Creation date: (3/19/2001 4:42:04 PM)
* @return cbit.vcell.model.Kinetics
* @param param org.jdom.Element
*/
private Kinetics getKinetics(Element param, ReactionStep reaction, Model model) throws XmlParseException {
VariableHash varHash = new VariableHash();
addResevedSymbols(varHash, model);
String type = param.getAttributeValue(XMLTags.KineticsTypeAttrTag);
Kinetics newKinetics = null;
try {
if (type.equalsIgnoreCase(XMLTags.KineticsTypeGeneralKinetics)) {
// create a general kinetics
newKinetics = new GeneralKinetics(reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeGeneralCurrentKinetics)) {
// Create GeneralCurrentKinetics
newKinetics = new GeneralCurrentKinetics(reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeMassAction) && reaction instanceof SimpleReaction) {
// create a Mass Action kinetics
newKinetics = new MassActionKinetics((SimpleReaction) reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeNernst) && reaction instanceof FluxReaction) {
// create NernstKinetics
newKinetics = new NernstKinetics((FluxReaction) reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeGHK) && reaction instanceof FluxReaction) {
// create GHKKinetics
newKinetics = new GHKKinetics((FluxReaction) reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeHMM_Irr) && reaction instanceof SimpleReaction) {
// create HMM_IrrKinetics
newKinetics = new HMM_IRRKinetics((SimpleReaction) reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeHMM_Rev) && reaction instanceof SimpleReaction) {
// create HMM_RevKinetics
newKinetics = new HMM_REVKinetics((SimpleReaction) reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeGeneralTotal_oldname)) {
// create GeneralTotalKinetics
newKinetics = new GeneralLumpedKinetics(reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeGeneralLumped)) {
// create GeneralLumpedKinetics
newKinetics = new GeneralLumpedKinetics(reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeGeneralCurrentLumped)) {
// create GeneralCurrentLumpedKinetics
newKinetics = new GeneralCurrentLumpedKinetics(reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeGeneralPermeability) && reaction instanceof FluxReaction) {
// create GeneralPermeabilityKinetics
newKinetics = new GeneralPermeabilityKinetics((FluxReaction) reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeMacroscopic_Irr) && reaction instanceof SimpleReaction) {
// create Macroscopic_IRRKinetics
newKinetics = new Macroscopic_IRRKinetics((SimpleReaction) reaction);
} else if (type.equalsIgnoreCase(XMLTags.KineticsTypeMicroscopic_Irr) && reaction instanceof SimpleReaction) {
// create Microscopic_IRRKinetics
newKinetics = new Microscopic_IRRKinetics((SimpleReaction) reaction);
} else {
throw new XmlParseException("Unknown kinetics type: " + type);
}
} catch (ExpressionException e) {
e.printStackTrace();
throw new XmlParseException("Error creating the kinetics for reaction: " + reaction.getName(), e);
}
try {
// transaction begin flag ... yeah, this is a hack
newKinetics.reading(true);
// Read all of the parameters
List<Element> list = param.getChildren(XMLTags.ParameterTag, vcNamespace);
// add constants that may be used in kinetics.
// VariableHash varHash = getVariablesHash();
ArrayList<String> reserved = new ArrayList<String>();
ReservedSymbol[] reservedSymbols = reaction.getModel().getReservedSymbols();
for (ReservedSymbol rs : reservedSymbols) {
reserved.add(rs.getName());
}
try {
if (reaction.getStructure() instanceof Membrane) {
Membrane membrane = (Membrane) reaction.getStructure();
varHash.addVariable(new Constant(membrane.getMembraneVoltage().getName(), new Expression(0.0)));
reserved.add(membrane.getMembraneVoltage().getName());
}
//
// add Reactants, Products, and Catalysts (ReactionParticipants)
//
ReactionParticipant[] rp = reaction.getReactionParticipants();
for (int i = 0; i < rp.length; i++) {
varHash.addVariable(new Constant(rp[i].getName(), new Expression(0.0)));
}
} catch (MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException("error reordering parameters according to dependencies: ", e);
}
//
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) {
varHash.addVariable(new Function(paramName, paramExp, null));
} else {
if (reserved.contains(paramName)) {
varHash.removeVariable(paramName);
varHash.addVariable(new Function(paramName, paramExp, null));
}
}
} catch (MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException("error reordering parameters according to dependencies: ", e);
}
Kinetics.KineticsParameter tempParam = null;
if (!role.equals(XMLTags.ParamRoleUserDefinedTag)) {
tempParam = newKinetics.getKineticsParameterFromRole(Kinetics.getParamRoleFromDefaultDesc(role));
} else {
continue;
}
// hack for bringing in General Total kinetics without breaking.
if (tempParam == null && newKinetics instanceof GeneralLumpedKinetics) {
if (role.equals(Kinetics.GTK_AssumedCompartmentSize_oldname) || role.equals(Kinetics.GTK_ReactionRate_oldname) || role.equals(Kinetics.GTK_CurrentDensity_oldname)) {
continue;
} else if (role.equals(VCMODL.TotalRate_oldname)) {
tempParam = newKinetics.getKineticsParameterFromRole(Kinetics.ROLE_LumpedReactionRate);
}
}
// hack from bringing in chargeValence parameters without breaking
if (tempParam == null && Kinetics.getParamRoleFromDefaultDesc(role) == Kinetics.ROLE_ChargeValence) {
tempParam = newKinetics.getChargeValenceParameter();
}
if (tempParam == null) {
throw new XmlParseException("parameter with role '" + role + "' not found in kinetics type '" + type + "'");
}
//
if (!tempParam.getName().equals(paramName)) {
Kinetics.KineticsParameter multNameParam = newKinetics.getKineticsParameter(paramName);
int n = 0;
while (multNameParam != null) {
String tempName = paramName + "_" + n++;
newKinetics.renameParameter(paramName, tempName);
multNameParam = newKinetics.getKineticsParameter(tempName);
}
newKinetics.renameParameter(tempParam.getName(), paramName);
}
}
//
// create unresolved parameters for all unresolved symbols
//
String unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
while (unresolvedSymbol != null) {
try {
// will turn into an UnresolvedParameter.
varHash.addVariable(new Function(unresolvedSymbol, new Expression(0.0), null));
} catch (MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException(e);
}
newKinetics.addUnresolvedParameter(unresolvedSymbol);
unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
}
Variable[] sortedVariables = varHash.getTopologicallyReorderedVariables();
ModelUnitSystem modelUnitSystem = reaction.getModel().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);
}
Kinetics.KineticsParameter tempParam = newKinetics.getKineticsParameter(paramFunction.getName());
if (tempParam == null) {
newKinetics.addUserDefinedKineticsParameter(paramFunction.getName(), paramFunction.getExpression(), unit);
} else {
newKinetics.setParameterValue(tempParam, paramFunction.getExpression());
tempParam.setUnitDefinition(unit);
}
}
}
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace(System.out);
throw new XmlParseException("Exception while setting parameters for Reaction : " + reaction.getName(), e);
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new XmlParseException("Exception while settings parameters for Reaction : " + reaction.getName(), e);
} finally {
newKinetics.reading(false);
}
return newKinetics;
}
use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.
the class XmlReader method getModel.
/**
* This method creates a Model object from a XML element.
* Creation date: (3/14/2001 6:14:37 PM)
* @return cbit.vcell.model.Model
* @param param org.jdom.Element
*/
private Model getModel(Element param) throws XmlParseException {
if (param == null) {
throw new XmlParseException("Invalid 'NULL' XML 'model' element arrived!");
}
// Get version, if any
Model newmodel = null;
Version version = getVersion(param.getChild(XMLTags.VersionTag, vcNamespace));
// if forcedModelUnitSystem has been set, ues that (could be overriding unit system for SBML export)
if (forcedModelUnitSystem != null) {
newmodel = new Model(version, forcedModelUnitSystem);
} else {
Element unitSystemNode = param.getChild(XMLTags.ModelUnitSystemTag, vcNamespace);
if (unitSystemNode != null) {
ModelUnitSystem modelUnitSystem = getUnitSystem(unitSystemNode);
newmodel = new Model(version, modelUnitSystem);
} else {
newmodel = new Model(version);
}
}
try {
// Set attributes
newmodel.setName(unMangle(param.getAttributeValue(XMLTags.NameAttrTag)));
// Add annotation
String annotationText = param.getChildText(XMLTags.AnnotationTag, vcNamespace);
if (annotationText != null && annotationText.length() > 0) {
newmodel.setDescription(unMangle(annotationText));
}
// Add global parameters
Element globalParamsElement = param.getChild(XMLTags.ModelParametersTag, vcNamespace);
if (globalParamsElement != null) {
ModelParameter[] modelParams = getModelParams(globalParamsElement, newmodel);
// add global/model param to model - done inside getModelParam by passing newModel
newmodel.setModelParameters(modelParams);
}
// Add Species (Compounds)
Iterator<Element> iterator = param.getChildren(XMLTags.SpeciesTag, vcNamespace).iterator();
ArrayList<Species> speciesList = new ArrayList<Species>();
while (iterator.hasNext()) {
org.jdom.Element temp = (Element) iterator.next();
speciesList.add(getSpecies(temp));
}
newmodel.setSpecies(speciesList.toArray(new Species[speciesList.size()]));
// Add Structures
LinkedList<Structure> newstructures = new LinkedList<Structure>();
// (features)
List<Element> children = param.getChildren(XMLTags.FeatureTag, vcNamespace);
for (Element featureElement : children) {
newstructures.add(getFeature(featureElement));
}
// (Membrane)
children = param.getChildren(XMLTags.MembraneTag, vcNamespace);
for (Element memElement : children) {
newstructures.add(getMembrane(newmodel, memElement, newstructures));
}
if (newstructures.size() > 0) {
Structure[] structarray = new Structure[newstructures.size()];
newstructures.toArray(structarray);
// Add all the retrieved structures
newmodel.setStructures(structarray);
}
// retrieve the RbmModelContainer, if present - must be done before we retrieve species context!
Element element = param.getChild(XMLTags.RbmModelContainerTag, vcNamespace);
if (element != null) {
getRbmModelContainer(element, newmodel);
} else {
lg.info("RbmModelContainer is missing.");
}
// Add SpeciesContexts
children = param.getChildren(XMLTags.SpeciesContextTag, vcNamespace);
SpeciesContext[] newspeccon = new SpeciesContext[children.size()];
int scCounter = 0;
for (Element scElement : children) {
newspeccon[scCounter] = getSpeciesContext(scElement, newmodel);
scCounter++;
}
newmodel.setSpeciesContexts(newspeccon);
// Retrieve rateRules and add to model
// Element rateRuleVarsElement = param.getChild(XMLTags.RateRuleVariablesTag, vcNamespace);
// if(rateRuleVarsElement != null){
// RateRuleVariable[] rateRuleVars = getRateRuleVariables(rateRuleVarsElement, newmodel);
// newmodel.setRateRuleVariables(rateRuleVars);
// }
// Add Reaction steps (if available)
// (Simplereaction)
// Create a varHash with reserved symbols and global parameters, if any, to pass on to Kinetics
// must create new hash for each reaction and flux, since each kinetics uses new variables hash
iterator = param.getChildren(XMLTags.SimpleReactionTag, vcNamespace).iterator();
ArrayList<ReactionStep> reactionStepList = new ArrayList<ReactionStep>();
while (iterator.hasNext()) {
org.jdom.Element temp = iterator.next();
reactionStepList.add(getSimpleReaction(temp, newmodel));
}
// (fluxStep)
iterator = param.getChildren(XMLTags.FluxStepTag, vcNamespace).iterator();
while (iterator.hasNext()) {
org.jdom.Element temp = iterator.next();
reactionStepList.add(getFluxReaction(temp, newmodel));
}
newmodel.setReactionSteps(reactionStepList.toArray(new ReactionStep[reactionStepList.size()]));
// Add Diagrams
children = param.getChildren(XMLTags.DiagramTag, vcNamespace);
if (children.size() > 0) {
Diagram[] newdiagrams = new Diagram[children.size()];
int diagramCounter = 0;
for (Element diagramElement : children) {
newdiagrams[diagramCounter] = getDiagram(diagramElement, newmodel);
diagramCounter++;
}
reorderDiagramsInPlace_UponRead(docVCellSoftwareVersion, newdiagrams, newmodel.getStructureTopology());
// if(docVCellSoftwareVersion != null && !docVCellSoftwareVersion.isValid() && docVCellSoftwareVersion.getMajorVersion()<=5 && docVCellSoftwareVersion.getMinorVersion() <=2){
// //In Vcell 5.2 and previous we need to order diagrams topologically, in 5.3 and later the diagrams are displayed as they are ordered when read from document
// final StructureTopology structureTopology = newmodel.getStructureTopology();
// Arrays.sort(newdiagrams, new Comparator<Diagram>() {
// @Override
// public int compare(Diagram o1, Diagram o2) {
// return getStructureLevel(o1.getStructure(), structureTopology) - getStructureLevel(o2.getStructure(), structureTopology);
// }
// });
// }
newmodel.setDiagrams(newdiagrams);
}
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace();
throw new XmlParseException(e);
} catch (ModelException e) {
e.printStackTrace();
}
// model param expresions are not bound when they are read in, since they could be functions of each other or structures/speciesContexts.
// Hence bind the model param exprs at the end, after reading all model level quantities.
ModelParameter[] modelParameters = newmodel.getModelParameters();
for (int i = 0; modelParameters != null && i < modelParameters.length; i++) {
try {
modelParameters[i].getExpression().bindExpression(newmodel);
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
throw new RuntimeException("Error binding global parameter '" + modelParameters[i].getName() + "' to model." + e.getMessage());
}
}
return newmodel;
}
use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.
the class Xmlproducer method getXML.
/**
* Outputs a XML version of a Model object
* Creation date: (2/15/2001 11:39:27 AM)
* @return Element
* @param param cbit.vcell.model.Model
*/
private Element getXML(Model param) throws XmlParseException /*, cbit.vcell.parser.ExpressionException */
{
Element modelnode = new Element(XMLTags.ModelTag);
String versionName = (param.getName() != null) ? mangle(param.getName()) : "unnamed_model";
// get Attributes
modelnode.setAttribute(XMLTags.NameAttrTag, versionName);
// modelnode.setAttribute(XMLTags.AnnotationAttrTag, this.mangle(param.getDescription()));
if (param.getDescription() != null && param.getDescription().length() > 0) {
Element annotationElem = new Element(XMLTags.AnnotationTag);
annotationElem.setText(mangle(param.getDescription()));
modelnode.addContent(annotationElem);
}
// get global parameters
ModelParameter[] modelGlobals = param.getModelParameters();
if (modelGlobals != null && modelGlobals.length > 0) {
modelnode.addContent(getXML(modelGlobals));
}
// Get Species
Species[] array = param.getSpecies();
for (int i = 0; i < array.length; i++) {
modelnode.addContent(getXML(array[i]));
}
// Get Structures(Features and Membranes). Add them in an ordered fashion, but it does not matter who comes first.
try {
ArrayList<Element> list = new ArrayList<Element>();
Structure[] structarray = param.getStructures();
for (int i = 0; i < structarray.length; i++) {
Element structure = getXML(structarray[i], param);
if (structarray[i] instanceof Feature)
modelnode.addContent(structure);
else
list.add(structure);
}
for (int i = 0; i < list.size(); i++) {
modelnode.addContent((Element) list.get(i));
}
} catch (XmlParseException e) {
e.printStackTrace();
throw new XmlParseException("An error occurred while procesing a Structure for the model " + versionName, e);
}
// Process SpeciesContexts
SpeciesContext[] specarray = param.getSpeciesContexts();
for (int i = 0; i < specarray.length; i++) {
modelnode.addContent(getXML(specarray[i]));
}
// Get reaction Steps(Simple Reactions and Fluxtep)
ReactionStep[] reactarray = param.getReactionSteps();
for (int i = 0; i < reactarray.length; i++) {
modelnode.addContent(getXML(reactarray[i]));
}
// add the rbmModelContainer elements
RbmModelContainer rbmModelContainer = param.getRbmModelContainer();
if (rbmModelContainer != null && !rbmModelContainer.isEmpty()) {
Element rbmModelContainerElement = getXML(rbmModelContainer);
{
// for testing purposes only
Document doc = new Document();
Element clone = (Element) rbmModelContainerElement.clone();
doc.setRootElement(clone);
String xmlString = XmlUtil.xmlToString(doc, false);
System.out.println(xmlString);
}
modelnode.addContent(rbmModelContainerElement);
}
// // Add rate rules
// if (param.getRateRuleVariables()!=null && param.getRateRuleVariables().length>0){
// modelnode.addContent( getXML(param.getRateRuleVariables()) );
// }
// Get Diagrams
Diagram[] diagarray = param.getDiagrams();
for (int i = 0; i < diagarray.length; i++) {
modelnode.addContent(getXML(diagarray[i]));
}
// Add Metadata information
if (param.getVersion() != null) {
modelnode.addContent(getXML(param.getVersion(), param));
}
// add model UnitSystem
ModelUnitSystem unitSystem = param.getUnitSystem();
if (unitSystem != null) {
modelnode.addContent(getXML(unitSystem));
}
return modelnode;
}
Aggregations