use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.
the class ParameterPropertiesPanel method changeExpression.
private void changeExpression() {
try {
if (parameter == null) {
return;
}
String text = expressionTextField.getText();
if (text == null || text.trim().length() == 0) {
Expression exp = parameter.getDefaultExpression();
if (exp != null) {
parameter.setExpression(exp);
}
return;
}
if (expressionMatchesCurrentValue(text)) {
return;
}
if (parameter instanceof SpeciesContextSpec.SpeciesContextSpecParameter) {
SpeciesContextSpec.SpeciesContextSpecParameter scsParm = (SpeciesContextSpec.SpeciesContextSpecParameter) parameter;
scsParm.setExpression(new Expression(text));
} else if (parameter instanceof KineticsParameter) {
Expression exp1 = new Expression(text);
Kinetics kinetics = ((KineticsParameter) parameter).getKinetics();
kinetics.setParameterValue((Kinetics.KineticsParameter) parameter, exp1);
} else {
Expression exp1 = new Expression(text);
exp1.bindExpression(parameter.getNameScope().getScopedSymbolTable());
parameter.setExpression(exp1);
}
} catch (Exception e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(this, e.getMessage(), e);
}
}
use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.
the class FRAPStudy method createNewSimBioModel.
public static BioModel createNewSimBioModel(FRAPStudy sourceFrapStudy, Parameter[] params, TimeStep tStep, KeyValue simKey, User owner, int startingIndexForRecovery) throws Exception {
if (owner == null) {
throw new Exception("Owner is not defined");
}
ROI cellROI_2D = sourceFrapStudy.getFrapData().getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_CELL.name());
double df = params[FRAPModel.INDEX_PRIMARY_DIFF_RATE].getInitialGuess();
double ff = params[FRAPModel.INDEX_PRIMARY_FRACTION].getInitialGuess();
double bwmRate = params[FRAPModel.INDEX_BLEACH_MONITOR_RATE].getInitialGuess();
double dc = 0;
double fc = 0;
double bs = 0;
double onRate = 0;
double offRate = 0;
if (params.length == FRAPModel.NUM_MODEL_PARAMETERS_TWO_DIFF) {
dc = params[FRAPModel.INDEX_SECONDARY_DIFF_RATE].getInitialGuess();
fc = params[FRAPModel.INDEX_SECONDARY_FRACTION].getInitialGuess();
} else if (params.length == FRAPModel.NUM_MODEL_PARAMETERS_BINDING) {
dc = params[FRAPModel.INDEX_SECONDARY_DIFF_RATE].getInitialGuess();
fc = params[FRAPModel.INDEX_SECONDARY_FRACTION].getInitialGuess();
bs = params[FRAPModel.INDEX_BINDING_SITE_CONCENTRATION].getInitialGuess();
onRate = params[FRAPModel.INDEX_ON_RATE].getInitialGuess();
offRate = params[FRAPModel.INDEX_OFF_RATE].getInitialGuess();
}
// immobile fraction
double fimm = 1 - ff - fc;
if (fimm < FRAPOptimizationUtils.epsilon && fimm > (0 - FRAPOptimizationUtils.epsilon)) {
fimm = 0;
}
if (fimm < (1 + FRAPOptimizationUtils.epsilon) && fimm > (1 - FRAPOptimizationUtils.epsilon)) {
fimm = 1;
}
Extent extent = sourceFrapStudy.getFrapData().getImageDataset().getExtent();
double[] timeStamps = sourceFrapStudy.getFrapData().getImageDataset().getImageTimeStamps();
TimeBounds timeBounds = new TimeBounds(0.0, timeStamps[timeStamps.length - 1] - timeStamps[startingIndexForRecovery]);
double timeStepVal = timeStamps[startingIndexForRecovery + 1] - timeStamps[startingIndexForRecovery];
int numX = cellROI_2D.getRoiImages()[0].getNumX();
int numY = cellROI_2D.getRoiImages()[0].getNumY();
int numZ = cellROI_2D.getRoiImages().length;
short[] shortPixels = cellROI_2D.getRoiImages()[0].getPixels();
byte[] bytePixels = new byte[numX * numY * numZ];
final byte EXTRACELLULAR_PIXVAL = 0;
final byte CYTOSOL_PIXVAL = 1;
for (int i = 0; i < bytePixels.length; i++) {
if (shortPixels[i] != 0) {
bytePixels[i] = CYTOSOL_PIXVAL;
}
}
VCImage maskImage;
try {
maskImage = new VCImageUncompressed(null, bytePixels, extent, numX, numY, numZ);
} catch (ImageException e) {
e.printStackTrace();
throw new RuntimeException("failed to create mask image for geometry");
}
Geometry geometry = new Geometry("geometry", maskImage);
if (geometry.getGeometrySpec().getNumSubVolumes() != 2) {
throw new Exception("Cell ROI has no ExtraCellular.");
}
int subVolume0PixVal = ((ImageSubVolume) geometry.getGeometrySpec().getSubVolume(0)).getPixelValue();
geometry.getGeometrySpec().getSubVolume(0).setName((subVolume0PixVal == EXTRACELLULAR_PIXVAL ? EXTRACELLULAR_NAME : CYTOSOL_NAME));
int subVolume1PixVal = ((ImageSubVolume) geometry.getGeometrySpec().getSubVolume(1)).getPixelValue();
geometry.getGeometrySpec().getSubVolume(1).setName((subVolume1PixVal == CYTOSOL_PIXVAL ? CYTOSOL_NAME : EXTRACELLULAR_NAME));
geometry.getGeometrySurfaceDescription().updateAll();
BioModel bioModel = new BioModel(null);
bioModel.setName("unnamed");
Model model = new Model("model");
bioModel.setModel(model);
model.addFeature(EXTRACELLULAR_NAME);
Feature extracellular = (Feature) model.getStructure(EXTRACELLULAR_NAME);
model.addFeature(CYTOSOL_NAME);
Feature cytosol = (Feature) model.getStructure(CYTOSOL_NAME);
// Membrane mem = model.addMembrane(EXTRACELLULAR_CYTOSOL_MEM_NAME);
// model.getStructureTopology().setInsideFeature(mem, cytosol);
// model.getStructureTopology().setOutsideFeature(mem, extracellular);
String roiDataName = FRAPStudy.ROI_EXTDATA_NAME;
final int SPECIES_COUNT = 4;
final int FREE_SPECIES_INDEX = 0;
final int BS_SPECIES_INDEX = 1;
final int COMPLEX_SPECIES_INDEX = 2;
final int IMMOBILE_SPECIES_INDEX = 3;
Expression[] diffusionConstants = null;
Species[] species = null;
SpeciesContext[] speciesContexts = null;
Expression[] initialConditions = null;
diffusionConstants = new Expression[SPECIES_COUNT];
species = new Species[SPECIES_COUNT];
speciesContexts = new SpeciesContext[SPECIES_COUNT];
initialConditions = new Expression[SPECIES_COUNT];
// total initial condition
FieldFunctionArguments postBleach_first = new FieldFunctionArguments(roiDataName, "postbleach_first", new Expression(0), VariableType.VOLUME);
FieldFunctionArguments prebleach_avg = new FieldFunctionArguments(roiDataName, "prebleach_avg", new Expression(0), VariableType.VOLUME);
Expression expPostBleach_first = new Expression(postBleach_first.infix());
Expression expPreBleach_avg = new Expression(prebleach_avg.infix());
Expression totalIniCondition = Expression.div(expPostBleach_first, expPreBleach_avg);
// Free Species
diffusionConstants[FREE_SPECIES_INDEX] = new Expression(df);
species[FREE_SPECIES_INDEX] = new Species(FRAPStudy.SPECIES_NAME_PREFIX_MOBILE, "Mobile bleachable species");
speciesContexts[FREE_SPECIES_INDEX] = new SpeciesContext(null, species[FREE_SPECIES_INDEX].getCommonName(), species[FREE_SPECIES_INDEX], cytosol);
initialConditions[FREE_SPECIES_INDEX] = Expression.mult(new Expression(ff), totalIniCondition);
// Immobile Species (No diffusion)
// Set very small diffusion rate on immobile to force evaluation as state variable (instead of FieldData function)
// If left as a function errors occur because functions involving FieldData require a database connection
final String IMMOBILE_DIFFUSION_KLUDGE = "1e-14";
diffusionConstants[IMMOBILE_SPECIES_INDEX] = new Expression(IMMOBILE_DIFFUSION_KLUDGE);
species[IMMOBILE_SPECIES_INDEX] = new Species(FRAPStudy.SPECIES_NAME_PREFIX_IMMOBILE, "Immobile bleachable species");
speciesContexts[IMMOBILE_SPECIES_INDEX] = new SpeciesContext(null, species[IMMOBILE_SPECIES_INDEX].getCommonName(), species[IMMOBILE_SPECIES_INDEX], cytosol);
initialConditions[IMMOBILE_SPECIES_INDEX] = Expression.mult(new Expression(fimm), totalIniCondition);
// BS Species
diffusionConstants[BS_SPECIES_INDEX] = new Expression(IMMOBILE_DIFFUSION_KLUDGE);
species[BS_SPECIES_INDEX] = new Species(FRAPStudy.SPECIES_NAME_PREFIX_BINDING_SITE, "Binding Site species");
speciesContexts[BS_SPECIES_INDEX] = new SpeciesContext(null, species[BS_SPECIES_INDEX].getCommonName(), species[BS_SPECIES_INDEX], cytosol);
initialConditions[BS_SPECIES_INDEX] = Expression.mult(new Expression(bs), totalIniCondition);
// Complex species
diffusionConstants[COMPLEX_SPECIES_INDEX] = new Expression(dc);
species[COMPLEX_SPECIES_INDEX] = new Species(FRAPStudy.SPECIES_NAME_PREFIX_SLOW_MOBILE, "Slower mobile bleachable species");
speciesContexts[COMPLEX_SPECIES_INDEX] = new SpeciesContext(null, species[COMPLEX_SPECIES_INDEX].getCommonName(), species[COMPLEX_SPECIES_INDEX], cytosol);
initialConditions[COMPLEX_SPECIES_INDEX] = Expression.mult(new Expression(fc), totalIniCondition);
// add reactions to species if there is bleachWhileMonitoring rate.
for (int i = 0; i < initialConditions.length; i++) {
model.addSpecies(species[i]);
model.addSpeciesContext(speciesContexts[i]);
// reaction with BMW rate, which should not be applied to binding site
if (!(species[i].getCommonName().equals(FRAPStudy.SPECIES_NAME_PREFIX_BINDING_SITE))) {
SimpleReaction simpleReaction = new SimpleReaction(model, cytosol, speciesContexts[i].getName() + "_bleach", true);
model.addReactionStep(simpleReaction);
simpleReaction.addReactant(speciesContexts[i], 1);
MassActionKinetics massActionKinetics = new MassActionKinetics(simpleReaction);
simpleReaction.setKinetics(massActionKinetics);
KineticsParameter kforward = massActionKinetics.getForwardRateParameter();
simpleReaction.getKinetics().setParameterValue(kforward, new Expression(new Double(bwmRate)));
}
}
// add the binding reaction: F + BS <-> C
SimpleReaction simpleReaction2 = new SimpleReaction(model, cytosol, "reac_binding", true);
model.addReactionStep(simpleReaction2);
simpleReaction2.addReactant(speciesContexts[FREE_SPECIES_INDEX], 1);
simpleReaction2.addReactant(speciesContexts[BS_SPECIES_INDEX], 1);
simpleReaction2.addProduct(speciesContexts[COMPLEX_SPECIES_INDEX], 1);
MassActionKinetics massActionKinetics = new MassActionKinetics(simpleReaction2);
simpleReaction2.setKinetics(massActionKinetics);
KineticsParameter kforward = massActionKinetics.getForwardRateParameter();
KineticsParameter kreverse = massActionKinetics.getReverseRateParameter();
simpleReaction2.getKinetics().setParameterValue(kforward, new Expression(new Double(onRate)));
simpleReaction2.getKinetics().setParameterValue(kreverse, new Expression(new Double(offRate)));
// create simulation context
SimulationContext simContext = new SimulationContext(bioModel.getModel(), geometry);
bioModel.addSimulationContext(simContext);
FeatureMapping cytosolFeatureMapping = (FeatureMapping) simContext.getGeometryContext().getStructureMapping(cytosol);
FeatureMapping extracellularFeatureMapping = (FeatureMapping) simContext.getGeometryContext().getStructureMapping(extracellular);
// Membrane plasmaMembrane = model.getStructureTopology().getMembrane(cytosol, extracellular);
// MembraneMapping plasmaMembraneMapping = (MembraneMapping)simContext.getGeometryContext().getStructureMapping(plasmaMembrane);
SubVolume cytSubVolume = geometry.getGeometrySpec().getSubVolume(CYTOSOL_NAME);
SubVolume exSubVolume = geometry.getGeometrySpec().getSubVolume(EXTRACELLULAR_NAME);
SurfaceClass pmSurfaceClass = geometry.getGeometrySurfaceDescription().getSurfaceClass(exSubVolume, cytSubVolume);
cytosolFeatureMapping.setGeometryClass(cytSubVolume);
extracellularFeatureMapping.setGeometryClass(exSubVolume);
// plasmaMembraneMapping.setGeometryClass(pmSurfaceClass);
cytosolFeatureMapping.getUnitSizeParameter().setExpression(new Expression(1.0));
extracellularFeatureMapping.getUnitSizeParameter().setExpression(new Expression(1.0));
for (int i = 0; i < speciesContexts.length; i++) {
SpeciesContextSpec scs = simContext.getReactionContext().getSpeciesContextSpec(speciesContexts[i]);
scs.getInitialConditionParameter().setExpression(initialConditions[i]);
scs.getDiffusionParameter().setExpression(diffusionConstants[i]);
}
MathMapping mathMapping = simContext.createNewMathMapping();
MathDescription mathDesc = mathMapping.getMathDescription();
// Add total fluorescence as function of mobile(optional: and slower mobile) and immobile fractions
mathDesc.addVariable(new Function(FRAPStudy.SPECIES_NAME_PREFIX_COMBINED, new Expression(species[FREE_SPECIES_INDEX].getCommonName() + "+" + species[COMPLEX_SPECIES_INDEX].getCommonName() + "+" + species[IMMOBILE_SPECIES_INDEX].getCommonName()), null));
simContext.setMathDescription(mathDesc);
SimulationVersion simVersion = new SimulationVersion(simKey, "sim1", owner, new GroupAccessNone(), new KeyValue("0"), new BigDecimal(0), new Date(), VersionFlag.Current, "", null);
Simulation newSimulation = new Simulation(simVersion, mathDesc);
simContext.addSimulation(newSimulation);
newSimulation.getSolverTaskDescription().setTimeBounds(timeBounds);
newSimulation.getMeshSpecification().setSamplingSize(cellROI_2D.getISize());
// newSimulation.getSolverTaskDescription().setTimeStep(timeStep); // Sundials doesn't need time step
newSimulation.getSolverTaskDescription().setSolverDescription(SolverDescription.SundialsPDE);
// use exp time step as output time spec
newSimulation.getSolverTaskDescription().setOutputTimeSpec(new UniformOutputTimeSpec(timeStepVal));
return bioModel;
}
use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.
the class Model method vetoableChange.
/**
* This method was created in VisualAge.
* @param e java.beans.PropertyChangeEvent
* @exception java.beans.PropertyVetoException The exception description.
*/
public void vetoableChange(PropertyChangeEvent e) throws ModelPropertyVetoException {
if (e.getSource() instanceof Structure) {
if (e.getPropertyName().equals("name") && !e.getOldValue().equals(e.getNewValue())) {
if (getStructure((String) e.getNewValue()) != null) {
throw new ModelPropertyVetoException("another structure already using name " + e.getNewValue(), e);
}
}
}
if (e.getSource() instanceof ReactionStep) {
if (e.getPropertyName().equals("name") && !e.getOldValue().equals(e.getNewValue())) {
String newName = (String) e.getNewValue();
if (getReactionStep(newName) != null) {
throw new ModelPropertyVetoException("another reaction step is already using name '" + newName + "'", e);
}
// validateNamingConflicts("Reaction",ReactionStep.class, newName, e);
}
}
if (e.getSource() instanceof ReactionRule && e.getPropertyName().equals(PropertyConstants.PROPERTY_NAME_NAME)) {
if (!e.getOldValue().equals(e.getNewValue())) {
String newName = (String) e.getNewValue();
if (getRbmModelContainer().getReactionRule(newName) != null) {
throw new ModelPropertyVetoException("another reaction rule is already using name '" + newName + "'", e);
}
validateNamingConflicts("Rule", ReactionRule.class, newName, e);
}
}
if (e.getSource() instanceof SpeciesContext) {
if (e.getPropertyName().equals("name") && !e.getOldValue().equals(e.getNewValue())) {
String newName = (String) e.getNewValue();
SpeciesContext sc = getSpeciesContext(newName);
if (sc != null) {
throw new ModelPropertyVetoException("another " + SpeciesContext.getTerm() + " defined in '" + sc.getStructure().getName() + "' already uses name '" + e.getNewValue() + "'", e);
}
validateNamingConflicts("SpeciesContext", SpeciesContext.class, newName, e);
}
}
if (e.getSource() instanceof MembraneVoltage) {
if (e.getPropertyName().equals("name") && !e.getOldValue().equals(e.getNewValue())) {
String newName = (String) e.getNewValue();
SymbolTableEntry existingSTE = getLocalEntry(newName);
if (existingSTE instanceof MembraneVoltage) {
throw new ModelPropertyVetoException("new name \"" + newName + "\" conflicts with the voltage parameter name for membrane \"" + ((MembraneVoltage) existingSTE).getMembrane().getName() + "\"", e);
}
validateNamingConflicts("MembraneVoltage", MembraneVoltage.class, newName, e);
}
}
if (e.getSource() instanceof StructureSize) {
if (e.getPropertyName().equals("name") && !e.getOldValue().equals(e.getNewValue())) {
String newName = (String) e.getNewValue();
SymbolTableEntry existingSTE = getLocalEntry(newName);
if (existingSTE instanceof StructureSize) {
throw new ModelPropertyVetoException("new name \"" + newName + "\" conflicts with the size parameter name for structure \"" + ((StructureSize) existingSTE).getStructure().getName() + "\"", e);
}
validateNamingConflicts("StructureSize", StructureSize.class, newName, e);
}
}
if (e.getSource() instanceof ModelParameter) {
if (e.getPropertyName().equals("name") && !e.getOldValue().equals(e.getNewValue())) {
String newName = (String) e.getNewValue();
if (getModelParameter(newName) != null) {
throw new ModelPropertyVetoException("Model Parameter with name '" + newName + "' already exists.", e);
}
validateNamingConflicts("Model Parameter", ModelParameter.class, newName, e);
}
}
if (e.getSource() instanceof MolecularType) {
if (e.getPropertyName().equals("name") && !e.getOldValue().equals(e.getNewValue())) {
String newName = (String) e.getNewValue();
if (getRbmModelContainer().getMolecularType(newName) != null) {
throw new ModelPropertyVetoException(MolecularType.typeName + " with name '" + newName + "' already exists.", e);
}
validateNamingConflicts(((MolecularType) e.getSource()).getDisplayType(), MolecularType.class, newName, e);
}
}
if (e.getSource() instanceof RbmObservable) {
if (e.getPropertyName().equals("name") && !e.getOldValue().equals(e.getNewValue())) {
String newName = (String) e.getNewValue();
if (getRbmModelContainer().getObservable(newName) != null) {
throw new ModelPropertyVetoException(((RbmObservable) e.getSource()).getDisplayType() + " with name '" + newName + "' already exists.", e);
}
validateNamingConflicts(((RbmObservable) e.getSource()).getDisplayType(), RbmObservable.class, newName, e);
}
}
if (e.getSource() instanceof Species) {
if (e.getPropertyName().equals("commonName") && !e.getOldValue().equals(e.getNewValue())) {
String commonName = (String) e.getNewValue();
if (commonName == null) {
throw new ModelPropertyVetoException("species name cannot be null", e);
}
//
if (getSpecies(commonName) != null) {
throw new ModelPropertyVetoException("Species with common name '" + commonName + "' already defined", e);
}
if (getReservedSymbolByName(commonName) != null) {
throw new ModelPropertyVetoException("cannot use reserved symbol '" + commonName + "' as a Species common name", e, ModelPropertyVetoException.Category.RESERVED_SYMBOL);
}
}
}
if (e.getSource() == this && e.getPropertyName().equals(RbmModelContainer.PROPERTY_NAME_MOLECULAR_TYPE_LIST)) {
ArrayList<MolecularType> newMolecularTypes = (ArrayList<MolecularType>) e.getNewValue();
if (newMolecularTypes == null) {
throw new ModelPropertyVetoException(MolecularType.typeName + " list cannot be null", e);
}
for (MolecularType mt : getRbmModelContainer().getMolecularTypeList()) {
validateNamingConflicts(MolecularType.typeName, MolecularType.class, mt.getName(), e);
}
}
if (e.getSource() == this && e.getPropertyName().equals(RbmModelContainer.PROPERTY_NAME_OBSERVABLE_LIST)) {
ArrayList<RbmObservable> newObservables = (ArrayList<RbmObservable>) e.getNewValue();
if (newObservables == null) {
throw new ModelPropertyVetoException(RbmObservable.typeName + " list cannot be null", e);
}
for (RbmObservable observable : getRbmModelContainer().getObservableList()) {
validateNamingConflicts(RbmObservable.typeName, RbmObservable.class, observable.getName(), e);
}
}
if (e.getSource() == this && e.getPropertyName().equals(RbmModelContainer.PROPERTY_NAME_REACTION_RULE_LIST)) {
ArrayList<ReactionRule> newReactionRules = (ArrayList<ReactionRule>) e.getNewValue();
if (newReactionRules == null) {
throw new ModelPropertyVetoException(ReactionRule.typeName + " list cannot be null", e);
}
for (ReactionRule rr : getRbmModelContainer().getReactionRuleList()) {
validateNamingConflicts(ReactionRule.typeName, ReactionRule.class, rr.getName(), e);
}
}
if (e.getSource() == this && e.getPropertyName().equals(PROPERTY_NAME_STRUCTURES)) {
Structure[] newStructures = (Structure[]) e.getNewValue();
if (newStructures == null) {
throw new ModelPropertyVetoException("structures cannot be null", e);
}
//
// look for duplicates of structure name, structure size name, membrane voltage name within new "structures" array
// and look for symbol conflicts for StructureSize name and for MembraneVoltage name in existing "local" symbols.
//
HashSet<String> structNameSet = new HashSet<String>();
HashSet<String> structSymbolSet = new HashSet<String>();
for (int i = 0; i < newStructures.length; i++) {
String newStructureName = newStructures[i].getName();
if (structNameSet.contains(newStructureName)) {
throw new ModelPropertyVetoException("multiple structures with name '" + newStructureName + "' defined", e);
}
structNameSet.add(newStructureName);
if (newStructures[i] instanceof Membrane) {
String newMembraneVoltageName = ((Membrane) newStructures[i]).getMembraneVoltage().getName();
if (structSymbolSet.contains(newMembraneVoltageName)) {
// throw new ModelPropertyVetoException("membrane '"+newStructureName+"' has Voltage name '"+newMembraneVoltageName+"' that conflicts with another Voltage name or Size name",e);
}
structSymbolSet.add(newMembraneVoltageName);
validateNamingConflicts("MembraneVoltage", MembraneVoltage.class, newMembraneVoltageName, e);
}
String newStructureSizeName = newStructures[i].getStructureSize().getName();
if (structSymbolSet.contains(newStructureSizeName)) {
throw new ModelPropertyVetoException("structure '" + newStructureName + "' has structure Size name '" + newStructureSizeName + "' that conflicts with another Voltage name or Size name", e);
}
structSymbolSet.add(newStructureSizeName);
validateNamingConflicts("StructureSize", StructureSize.class, newStructureSizeName, e);
}
}
if (e.getSource() == this && e.getPropertyName().equals(PROPERTY_NAME_SPECIES)) {
Species[] newSpeciesArray = (Species[]) e.getNewValue();
if (newSpeciesArray == null) {
throw new ModelPropertyVetoException("species cannot be null", e);
}
//
// check that names are not duplicated and that no common names are ReservedSymbols
//
HashSet<String> commonNameSet = new HashSet<String>();
for (int i = 0; i < newSpeciesArray.length; i++) {
String commonName = newSpeciesArray[i].getCommonName();
if (commonNameSet.contains(commonName)) {
throw new ModelPropertyVetoException("multiple species with common name '" + commonName + "' defined", e);
}
if (getReservedSymbolByName(commonName) != null) {
throw new ModelPropertyVetoException("cannot use reserved symbol '" + commonName + "' as a Species common name", e, ModelPropertyVetoException.Category.RESERVED_SYMBOL);
}
commonNameSet.add(commonName);
}
//
for (int j = 0; j < fieldSpeciesContexts.length; j++) {
SpeciesContext sc = fieldSpeciesContexts[j];
boolean bFound = false;
for (int i = 0; i < newSpeciesArray.length; i++) {
if (newSpeciesArray[i] == sc.getSpecies()) {
bFound = true;
}
}
if (!bFound) {
throw new ModelPropertyVetoException("species[] missing '" + sc.getSpecies().getCommonName() + "' referenced in SpeciesContext '" + sc.getName() + "'", e);
}
}
}
if (e.getSource() == this && e.getPropertyName().equals(Model.PROPERTY_NAME_MODEL_PARAMETERS)) {
ModelParameter[] newModelParams = (ModelParameter[]) e.getNewValue();
//
// check that names are not duplicated and that no common names are ReservedSymbols
//
HashSet<String> namesSet = new HashSet<String>();
for (int i = 0; i < newModelParams.length; i++) {
if (namesSet.contains(newModelParams[i].getName())) {
throw new ModelPropertyVetoException("Multiple model/global parameters with same name '" + newModelParams[i].getName() + "' defined", e);
}
namesSet.add(newModelParams[i].getName());
validateNamingConflicts("Model Parameter", ModelParameter.class, newModelParams[i].getName(), e);
}
//
// make sure that kinetics of reactionSteps do not refer to modelParameter to be deleted
// Find this model parameter, missing from 'newModelParams'; loop thro' all reactionStep kinetics to determine if it is used
//
ModelParameter[] oldModelParameters = (ModelParameter[]) e.getOldValue();
if (oldModelParameters.length > newModelParams.length) {
// if 'newModelParameter' is smaller than 'oldModelParameter', one of the modelParams has been removed, find the missing one
ModelParameter missingMP = null;
for (int i = 0; i < oldModelParameters.length; i++) {
if (!BeanUtils.arrayContains(newModelParams, oldModelParameters[i])) {
missingMP = oldModelParameters[i];
}
}
// use this missing model parameter (to be deleted) to determine if it is used in any reaction kinetic parameters.
if (missingMP != null) {
Vector<String> referencingRxnsVector = new Vector<String>();
for (int i = 0; i < fieldReactionSteps.length; i++) {
KineticsParameter[] kParams = fieldReactionSteps[i].getKinetics().getKineticsParameters();
for (int k = 0; k < kParams.length; k++) {
if (kParams[k].getExpression().hasSymbol(missingMP.getName()) && (fieldReactionSteps[i].getKinetics().getProxyParameter(missingMP.getName()) != null)) {
referencingRxnsVector.add(fieldReactionSteps[i].getName());
break;
}
}
}
// if there are any reactionSteps referencing the global, list them all in error msg.
if (referencingRxnsVector.size() > 0) {
String msg = "Model Parameter '" + missingMP.getName() + "' is used in reaction(s): ";
for (int i = 0; i < referencingRxnsVector.size(); i++) {
msg = msg + "'" + referencingRxnsVector.elementAt(i) + "'";
if (i < referencingRxnsVector.size() - 1) {
msg = msg + ", ";
} else {
msg = msg + ". ";
}
}
msg = msg + "\n\nCannot delete '" + missingMP.getName() + "'.";
throw new ModelPropertyVetoException(msg, e);
}
// At this point, it is not referenced in a reactionStep, find out if the missing model is used in other model parameters
// Enough to check in newModelParams array
Vector<String> referencingModelParamsVector = new Vector<String>();
for (int i = 0; i < newModelParams.length; i++) {
if (newModelParams[i].getExpression().hasSymbol(missingMP.getName())) {
referencingModelParamsVector.add(newModelParams[i].getName());
}
}
// if there are any model parameters referencing the global, list them all in error msg.
if (referencingModelParamsVector.size() > 0) {
String msg = "Model Parameter '" + missingMP.getName() + "' is used in expression of other model parameter(s): ";
for (int i = 0; i < referencingModelParamsVector.size(); i++) {
msg = msg + "'" + referencingModelParamsVector.elementAt(i) + "'";
if (i < referencingModelParamsVector.size() - 1) {
msg = msg + ", ";
} else {
msg = msg + ". ";
}
}
msg = msg + "\n\nCannot delete '" + missingMP.getName() + "'.";
throw new ModelPropertyVetoException(msg, e);
}
}
}
}
if (e.getSource() == this && e.getPropertyName().equals(PROPERTY_NAME_SPECIES_CONTEXTS)) {
SpeciesContext[] newSpeciesContextArray = (SpeciesContext[]) e.getNewValue();
if (newSpeciesContextArray == null) {
throw new ModelPropertyVetoException("speciesContexts cannot be null", e);
}
//
for (int i = 0; i < newSpeciesContextArray.length; i++) {
if (!contains(newSpeciesContextArray[i].getSpecies())) {
throw new ModelPropertyVetoException("can't add speciesContext '" + newSpeciesContextArray[i].getName() + "' before species '" + newSpeciesContextArray[i].getSpecies().getCommonName() + "'", e);
}
if (!contains(newSpeciesContextArray[i].getStructure())) {
throw new ModelPropertyVetoException("can't add speciesContext '" + newSpeciesContextArray[i].getName() + "' before structure '" + newSpeciesContextArray[i].getStructure().getName() + "'", e);
}
}
//
// check that names are not duplicated and that no names are ReservedSymbols
//
HashSet<String> nameSet = new HashSet<String>();
for (int i = 0; i < newSpeciesContextArray.length; i++) {
if (nameSet.contains(newSpeciesContextArray[i].getName())) {
throw new ModelPropertyVetoException("multiple speciesContexts with name '" + newSpeciesContextArray[i].getName() + "' defined", e);
}
nameSet.add(newSpeciesContextArray[i].getName());
validateNamingConflicts("SpeciesContext", newSpeciesContextArray[i].getClass(), newSpeciesContextArray[i].getName(), e);
}
//
for (int i = 0; i < fieldReactionSteps.length; i++) {
ReactionParticipant[] rpArray = fieldReactionSteps[i].getReactionParticipants();
for (int k = 0; k < rpArray.length; k++) {
boolean bFound = false;
for (int j = 0; j < newSpeciesContextArray.length; j++) {
if (newSpeciesContextArray[j] == rpArray[k].getSpeciesContext()) {
bFound = true;
break;
}
}
if (!bFound) {
throw new ModelPropertyVetoException("reaction '" + fieldReactionSteps[i].getName() + "' requires '" + rpArray[k].getSpeciesContext().getName() + "'", e);
}
}
}
}
if (e.getSource() == this && e.getPropertyName().equals(PROPERTY_NAME_REACTION_STEPS)) {
ReactionStep[] newReactionStepArr = (ReactionStep[]) e.getNewValue();
//
for (int i = 0; i < newReactionStepArr.length; i += 1) {
if (newReactionStepArr[i] == null) {
throw new ModelPropertyVetoException("Null cannot be added to ReactionStep", e);
}
}
//
// check that names are not duplicated and that no names are ReservedSymbols
// because math generator complained if reactionsteps had same name
//
HashSet<String> nameSet = new HashSet<String>();
for (int i = 0; i < newReactionStepArr.length; i++) {
String rxnName = newReactionStepArr[i].getName();
if (nameSet.contains(rxnName)) {
throw new ModelPropertyVetoException("multiple reactionSteps with name '" + rxnName + "' defined", e);
}
if (getReservedSymbolByName(rxnName) != null) {
throw new ModelPropertyVetoException("cannot use reserved symbol '" + rxnName + "' as a Reaction name", e, ModelPropertyVetoException.Category.RESERVED_SYMBOL);
}
nameSet.add(rxnName);
// validateNamingConflicts("Reaction", ReactionStep.class, newReactionStepArr[i].getName(), e);
}
//
for (int i = 0; i < newReactionStepArr.length; i++) {
ReactionParticipant[] rpArray = newReactionStepArr[i].getReactionParticipants();
for (int k = 0; k < rpArray.length; k++) {
boolean bFound = false;
for (int j = 0; j < fieldSpeciesContexts.length; j++) {
if (fieldSpeciesContexts[j] == rpArray[k].getSpeciesContext()) {
bFound = true;
break;
}
}
if (!bFound) {
throw new ModelPropertyVetoException("reaction '" + newReactionStepArr[i].getName() + "' requires '" + rpArray[k].getSpeciesContext().getName() + "'", e);
}
}
}
}
}
use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.
the class SEDMLExporter method getTargetAttributeXPath.
private XPathTarget getTargetAttributeXPath(SymbolTableEntry ste, Map<Pair<String, String>, String> l2gMap) {
// to get Xpath string for variables.
SBMLSupport sbmlSupport = new SBMLSupport();
XPathTarget targetXpath = null;
if (ste instanceof SpeciesContext || ste instanceof SpeciesContextSpecParameter) {
String speciesId = ste.getName();
// can change species initial concentration or amount
String speciesAttr = "";
if (ste instanceof SpeciesContextSpecParameter) {
SpeciesContextSpecParameter scsp = (SpeciesContextSpecParameter) ste;
speciesId = (scsp).getSpeciesContext().getName();
if (scsp.getRole() == SpeciesContextSpec.ROLE_InitialConcentration) {
speciesAttr = scsp.getName();
}
if (scsp.getRole() == SpeciesContextSpec.ROLE_InitialCount) {
speciesAttr = scsp.getName();
}
}
if (speciesAttr.length() < 1) {
targetXpath = new XPathTarget(sbmlSupport.getXPathForCompartment(speciesId));
} else if (speciesAttr.equalsIgnoreCase("initialConcentration") || speciesAttr.equalsIgnoreCase("initConc")) {
targetXpath = new XPathTarget(sbmlSupport.getXPathForSpecies(speciesId, SpeciesAttribute.initialConcentration));
} else if (speciesAttr.equalsIgnoreCase("initialCount")) {
targetXpath = new XPathTarget(sbmlSupport.getXPathForSpecies(speciesId, SpeciesAttribute.initialAmount));
} else {
throw new RuntimeException("Unknown species attribute '" + speciesAttr + "'; cannot get xpath target for species '" + speciesId + "'.");
}
targetXpath = new XPathTarget(sbmlSupport.getXPathForSpecies(speciesId));
} else if (ste instanceof ModelParameter) {
// can only change parameter value.
targetXpath = new XPathTarget(sbmlSupport.getXPathForGlobalParameter(ste.getName(), ParameterAttribute.value));
} else if (ste instanceof Structure || ste instanceof Structure.StructureSize || (ste instanceof StructureMappingParameter && ((StructureMappingParameter) ste).getRole() == StructureMapping.ROLE_Size)) {
String compartmentId = ste.getName();
// can change compartment size or spatial dimension, but in vcell, we cannot change compartment dimension.
String compartmentAttr = "";
if (ste instanceof Structure.StructureSize) {
compartmentId = ((StructureSize) ste).getStructure().getName();
compartmentAttr = ((StructureSize) ste).getName();
}
if (ste instanceof StructureMappingParameter) {
StructureMappingParameter smp = (StructureMappingParameter) ste;
compartmentId = smp.getStructure().getName();
if (smp.getRole() == StructureMapping.ROLE_Size) {
compartmentAttr = smp.getName();
}
}
if (compartmentAttr.length() < 1) {
targetXpath = new XPathTarget(sbmlSupport.getXPathForCompartment(compartmentId));
} else if (compartmentAttr.equalsIgnoreCase("size")) {
targetXpath = new XPathTarget(sbmlSupport.getXPathForCompartment(compartmentId, CompartmentAttribute.size));
} else {
throw new RuntimeException("Unknown compartment attribute '" + compartmentAttr + "'; cannot get xpath target for compartment '" + compartmentId + "'.");
}
} else if (ste instanceof KineticsParameter) {
KineticsParameter kp = (KineticsParameter) ste;
String reactionID = kp.getKinetics().getReactionStep().getName();
String parameterID = kp.getName();
Pair<String, String> key = new Pair(reactionID, parameterID);
String value = l2gMap.get(key);
if (value == null) {
// stays as local parameter
targetXpath = new XPathTarget(sbmlSupport.getXPathForKineticLawParameter(reactionID, parameterID, ParameterAttribute.value));
} else {
// became a global in SBML, we need to refer to that global
targetXpath = new XPathTarget(sbmlSupport.getXPathForGlobalParameter(value, ParameterAttribute.value));
}
} else {
System.err.println("Entity should be SpeciesContext, Structure, ModelParameter : " + ste.getClass());
throw new RuntimeException("Unknown entity in SBML model");
}
return targetXpath;
}
use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.
the class ModelTest method getExampleWithCurrent.
/**
* This method was created by a SmartGuide.
*/
public static Model getExampleWithCurrent() throws Exception {
double A_init = 10.0;
double B_init = 20.0;
double C_init = 30.0;
double D_init = 40.0;
double A_ec_init = 10.0;
double B_ec_init = 20.0;
double C_ec_init = 30.0;
double D_ec_init = 40.0;
double Vmax = 1.0;
double Kd_Bcyt = 0.02;
Model model = new Model("modelElectrical");
model.addSpecies(new Species("A", "A"));
Species A = model.getSpecies("A");
model.addSpecies(new Species("B", "B"));
Species B = model.getSpecies("B");
model.addSpecies(new Species("C", "C"));
Species C = model.getSpecies("C");
model.addSpecies(new Species("D", "D"));
Species D = model.getSpecies("D");
model.addFeature("extracellular");
Feature extracellular = (Feature) model.getStructure("extracellular");
model.addFeature("cytosol");
Feature cytosol = (Feature) model.getStructure("cytosol");
Membrane PM = (Membrane) model.getStructure("PM");
model.addSpeciesContext(A, cytosol);
SpeciesContext A_cyt = model.getSpeciesContext(A, cytosol);
// A_cyt.setInitialValue(A_init);
model.addSpeciesContext(B, cytosol);
SpeciesContext B_cyt = model.getSpeciesContext(B, cytosol);
// B_cyt.setInitialValue(B_init);
model.addSpeciesContext(C, cytosol);
SpeciesContext C_cyt = model.getSpeciesContext(C, cytosol);
// C_cyt.setInitialValue(C_init);
model.addSpeciesContext(D, cytosol);
SpeciesContext D_cyt = model.getSpeciesContext(D, cytosol);
// D_cyt.setInitialValue(D_init);
model.addSpeciesContext(A, extracellular);
SpeciesContext A_ec = model.getSpeciesContext(A, cytosol);
// A_cyt.setInitialValue(A_ec_init);
model.addSpeciesContext(B, extracellular);
SpeciesContext B_ec = model.getSpeciesContext(B, cytosol);
// B_cyt.setInitialValue(B_ec_init);
model.addSpeciesContext(C, extracellular);
SpeciesContext C_ec = model.getSpeciesContext(C, cytosol);
// C_cyt.setInitialValue(C_ec_init);
model.addSpeciesContext(D, extracellular);
SpeciesContext D_ec = model.getSpeciesContext(D, cytosol);
// D_cyt.setInitialValue(D_ec_init);
SimpleReaction sr;
//
// CYTOSOL REACTIONS
//
double K1 = 1.0;
double K2 = 2.0;
double K3 = 3.0;
double K4 = 4.0;
sr = new SimpleReaction(model, cytosol, "SIMPLE_REACTION_ABC", true);
sr.addReactant(A_cyt, 1);
sr.addReactant(B_cyt, 1);
sr.addProduct(C_cyt, 1);
MassActionKinetics massAct = new MassActionKinetics(sr);
sr.setKinetics(massAct);
massAct.setParameterValue(massAct.getForwardRateParameter(), new Expression(K1));
massAct.setParameterValue(massAct.getReverseRateParameter(), new Expression(K2));
massAct.renameParameter(massAct.getForwardRateParameter().getName(), "K1");
massAct.renameParameter(massAct.getReverseRateParameter().getName(), "K2");
model.addReactionStep(sr);
sr = new SimpleReaction(model, cytosol, "SIMPLE_REACION_CDA", true);
sr.addReactant(C_cyt, 1);
sr.addReactant(D_cyt, 1);
sr.addProduct(A_cyt, 1);
massAct = new MassActionKinetics(sr);
sr.setKinetics(massAct);
massAct.setParameterValue(massAct.getForwardRateParameter(), new Expression(K3));
massAct.setParameterValue(massAct.getReverseRateParameter(), new Expression(K4));
massAct.renameParameter(massAct.getForwardRateParameter().getName(), "K3");
massAct.renameParameter(massAct.getReverseRateParameter().getName(), "K4");
model.addReactionStep(sr);
Diagram diagram = model.getDiagram(cytosol);
String diagramFile = " { " + " SpeciesContextSpec A_cyt 100 100 " + " SpeciesContextSpec B_cyt 50 200 " + " SpeciesContextSpec C_cyt 200 200 " + " SpeciesContextSpec D_cyt 200 50 " + " SimpleReaction SIMPLE_REACTION_ABC 75 150 " + " SimpleReaction SIMPLE_REACION_CDA 200 125 " + "} ";
org.vcell.util.CommentStringTokenizer st = new org.vcell.util.CommentStringTokenizer(diagramFile);
diagram.fromTokens(st);
FluxReaction fr;
//
// PlasmaMembrane REACTIONS
//
fr = new FluxReaction(model, PM, null, "A Flux", true);
fr.addCatalyst(B_cyt);
GHKKinetics ghk = new GHKKinetics(fr);
fr.setKinetics(ghk);
KineticsParameter chargeValenceParameter = fr.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_ChargeValence);
if (chargeValenceParameter != null) {
chargeValenceParameter.setExpression(new Expression(2));
}
// ghk.setPermeability(new Expression("(Vmax*B_cyt/(Kd_Bcyt+B_cyt))"));
ghk.setParameterValue(ghk.getPermeabilityParameter(), new Expression(8.314e-2));
// ghk.setParameterValue("Vmax",Double.toString(Vmax));
// ghk.setParameterValue("Kd_Bcyt",Double.toString(Kd_Bcyt));
model.addReactionStep(fr);
return model;
}
Aggregations