use of cbit.vcell.model.Species in project vcell by virtualcell.
the class SBMLImporter method createSBMLUnitSystemForVCModel.
private ModelUnitSystem createSBMLUnitSystemForVCModel() throws Exception {
if (sbmlModel == null) {
throw new SBMLImportException("SBML model is NULL");
}
ListOf listofUnitDefns = sbmlModel.getListOfUnitDefinitions();
if (listofUnitDefns == null) {
System.out.println("No Unit Definitions");
// @TODO: deal with SBML level < 3.
return ModelUnitSystem.createDefaultVCModelUnitSystem();
}
@SuppressWarnings("serial") VCUnitSystem tempVCUnitSystem = new VCUnitSystem() {
};
sbmlUnitIdentifierHash = new HashMap<String, VCUnitDefinition>();
// add base SI unit identifiers (as defined in SBML spec) to hash
sbmlUnitIdentifierHash.put("ampere", tempVCUnitSystem.getInstance("A"));
sbmlUnitIdentifierHash.put("avogadro", tempVCUnitSystem.getInstance("6.02e23"));
// sbmlUnitIdentifierHash.put("becquerel",
// tempVCUnitSystem.getInstance("becquerel"));
// sbmlUnitIdentifierHash.put("candela",
// tempVCUnitSystem.getInstance("candela"));
sbmlUnitIdentifierHash.put("coulomb", tempVCUnitSystem.getInstance("C"));
sbmlUnitIdentifierHash.put("dimensionless", tempVCUnitSystem.getInstance("1"));
sbmlUnitIdentifierHash.put("farad", tempVCUnitSystem.getInstance("F"));
sbmlUnitIdentifierHash.put("gram", tempVCUnitSystem.getInstance("g"));
// sbmlUnitIdentifierHash.put("gray",
// tempVCUnitSystem.getInstance("gray"));
sbmlUnitIdentifierHash.put("henry", tempVCUnitSystem.getInstance("H"));
sbmlUnitIdentifierHash.put("hertz", tempVCUnitSystem.getInstance("Hz"));
sbmlUnitIdentifierHash.put("item", tempVCUnitSystem.getInstance("molecules"));
sbmlUnitIdentifierHash.put("joule", tempVCUnitSystem.getInstance("J"));
// sbmlUnitIdentifierHash.put("katal",
// tempVCUnitSystem.getInstance("katal"));
sbmlUnitIdentifierHash.put("kelvin", tempVCUnitSystem.getInstance("K"));
sbmlUnitIdentifierHash.put("kilogram", tempVCUnitSystem.getInstance("kg"));
sbmlUnitIdentifierHash.put("litre", tempVCUnitSystem.getInstance("litre"));
// sbmlUnitIdentifierHash.put("lumen",
// tempVCUnitSystem.getInstance("lumen"));
// sbmlUnitIdentifierHash.put("lux",
// tempVCUnitSystem.getInstance("lux"));
sbmlUnitIdentifierHash.put("metre", tempVCUnitSystem.getInstance("m"));
sbmlUnitIdentifierHash.put("mole", tempVCUnitSystem.getInstance("mol"));
sbmlUnitIdentifierHash.put("newton", tempVCUnitSystem.getInstance("N"));
// sbmlUnitIdentifierHash.put("ohm",
// tempVCUnitSystem.getInstance("ohm"));
// sbmlUnitIdentifierHash.put("pascal",
// tempVCUnitSystem.getInstance("pascal"));
// sbmlUnitIdentifierHash.put("radian",
// tempVCUnitSystem.getInstance("radian"));
sbmlUnitIdentifierHash.put("second", tempVCUnitSystem.getInstance("s"));
sbmlUnitIdentifierHash.put("siemens", tempVCUnitSystem.getInstance("S"));
// sbmlUnitIdentifierHash.put("sievert",
// tempVCUnitSystem.getInstance("sievert"));
// sbmlUnitIdentifierHash.put("steradian",
// tempVCUnitSystem.getInstance("steradian"));
// sbmlUnitIdentifierHash.put("tesla",
// tempVCUnitSystem.getInstance("tesla"));
sbmlUnitIdentifierHash.put("volt", tempVCUnitSystem.getInstance("V"));
sbmlUnitIdentifierHash.put("watt", tempVCUnitSystem.getInstance("W"));
sbmlUnitIdentifierHash.put("weber", tempVCUnitSystem.getInstance("Wb"));
long sbmlLevel = sbmlModel.getLevel();
if (sbmlLevel < 3) {
// SBML predefined unit identifiers
sbmlUnitIdentifierHash.put(UnitDefinition.SUBSTANCE, tempVCUnitSystem.getInstance("mole"));
sbmlUnitIdentifierHash.put(UnitDefinition.VOLUME, tempVCUnitSystem.getInstance("litre"));
sbmlUnitIdentifierHash.put(UnitDefinition.AREA, tempVCUnitSystem.getInstance("m2"));
sbmlUnitIdentifierHash.put(UnitDefinition.LENGTH, tempVCUnitSystem.getInstance("m"));
sbmlUnitIdentifierHash.put(UnitDefinition.TIME, tempVCUnitSystem.getInstance("s"));
}
if (sbmlModel.isSetSubstanceUnits()) {
UnitDefinition ud = sbmlModel.getSubstanceUnitsInstance();
VCUnitDefinition vcUnitDef = SBMLUnitTranslator.getVCUnitDefinition(ud, tempVCUnitSystem);
sbmlUnitIdentifierHash.put(UnitDefinition.SUBSTANCE, vcUnitDef);
}
if (sbmlModel.isSetVolumeUnits()) {
UnitDefinition ud = sbmlModel.getVolumeUnitsInstance();
VCUnitDefinition vcUnitDef = SBMLUnitTranslator.getVCUnitDefinition(ud, tempVCUnitSystem);
sbmlUnitIdentifierHash.put(UnitDefinition.VOLUME, vcUnitDef);
}
if (sbmlModel.isSetAreaUnits()) {
UnitDefinition ud = sbmlModel.getAreaUnitsInstance();
VCUnitDefinition vcUnitDef = SBMLUnitTranslator.getVCUnitDefinition(ud, tempVCUnitSystem);
sbmlUnitIdentifierHash.put(UnitDefinition.AREA, vcUnitDef);
}
if (sbmlModel.isSetLengthUnits()) {
UnitDefinition ud = sbmlModel.getLengthUnitsInstance();
VCUnitDefinition vcUnitDef = SBMLUnitTranslator.getVCUnitDefinition(ud, tempVCUnitSystem);
sbmlUnitIdentifierHash.put(UnitDefinition.LENGTH, vcUnitDef);
}
if (sbmlModel.isSetTimeUnits()) {
UnitDefinition ud = sbmlModel.getTimeUnitsInstance();
VCUnitDefinition vcUnitDef = SBMLUnitTranslator.getVCUnitDefinition(ud, tempVCUnitSystem);
sbmlUnitIdentifierHash.put(UnitDefinition.TIME, vcUnitDef);
}
// read unit definition (identifiers) declared in SBML model
for (int i = 0; i < sbmlModel.getNumUnitDefinitions(); i++) {
UnitDefinition ud = (org.sbml.jsbml.UnitDefinition) listofUnitDefns.get(i);
String unitName = ud.getId();
VCUnitDefinition vcUnitDef = SBMLUnitTranslator.getVCUnitDefinition(ud, tempVCUnitSystem);
sbmlUnitIdentifierHash.put(unitName, vcUnitDef);
}
// For SBML level 2
// default units
VCUnitDefinition defaultSubstanceUnit = sbmlUnitIdentifierHash.get(UnitDefinition.SUBSTANCE);
VCUnitDefinition defaultVolumeUnit = sbmlUnitIdentifierHash.get(UnitDefinition.VOLUME);
VCUnitDefinition defaultAreaUnit = sbmlUnitIdentifierHash.get(UnitDefinition.AREA);
VCUnitDefinition defaultLengthUnit = sbmlUnitIdentifierHash.get(UnitDefinition.LENGTH);
VCUnitDefinition defaultTimeUnit = sbmlUnitIdentifierHash.get(UnitDefinition.TIME);
VCUnitDefinition modelSubstanceUnit = null;
VCUnitDefinition modelVolumeUnit = null;
VCUnitDefinition modelAreaUnit = null;
VCUnitDefinition modelLengthUnit = null;
VCUnitDefinition modelTimeUnit = null;
// units in SBML model
// compartments
ListOf<Compartment> listOfCompartments = sbmlModel.getListOfCompartments();
for (int i = 0; i < listOfCompartments.size(); i++) {
Compartment sbmlComp = listOfCompartments.get(i);
double dim = 3;
if (sbmlComp.isSetSpatialDimensions()) {
dim = sbmlComp.getSpatialDimensions();
}
String unitStr = sbmlComp.getUnits();
VCUnitDefinition sbmlUnitDefinition = null;
if (unitStr != null && unitStr.length() > 0) {
sbmlUnitDefinition = sbmlUnitIdentifierHash.get(unitStr);
} else {
// applying default unit if not defined for this compartment
if (dim == 3) {
sbmlUnitDefinition = defaultVolumeUnit;
} else if (dim == 2) {
sbmlUnitDefinition = defaultAreaUnit;
} else if (dim == 1) {
sbmlUnitDefinition = defaultLengthUnit;
}
}
if (dim == 3) {
if (sbmlUnitDefinition == null) {
sbmlUnitDefinition = defaultVolumeUnit;
}
if (modelVolumeUnit == null) {
modelVolumeUnit = sbmlUnitDefinition;
} else if (!sbmlUnitDefinition.isEquivalent(modelVolumeUnit)) {
localIssueList.add(new Issue(new SBMLIssueSource(sbmlComp), issueContext, IssueCategory.Units, "unit for compartment '" + sbmlComp.getId() + "' (" + unitStr + ") : (" + sbmlUnitDefinition.getSymbol() + ") not compatible with current vol unit (" + modelVolumeUnit.getSymbol() + ")", Issue.SEVERITY_WARNING));
// logger.sendMessage(VCLogger.Priority.MediumPriority,
// VCLogger.ErrorType.UnitError, "unit for compartment '" +
// sbmlComp.getId() + "' (" + unitStr + ") : (" +
// sbmlUnitDefinition.getSymbol() +
// ") not compatible with current vol unit (" +
// modelVolumeUnit.getSymbol() + ")");
}
} else if (dim == 2) {
if (modelAreaUnit == null) {
modelAreaUnit = sbmlUnitDefinition;
} else if (!sbmlUnitDefinition.isEquivalent(modelAreaUnit)) {
localIssueList.add(new Issue(new SBMLIssueSource(sbmlComp), issueContext, IssueCategory.Units, "unit for compartment '" + sbmlComp.getId() + "' (" + unitStr + ") : (" + sbmlUnitDefinition.getSymbol() + ") not compatible with current area unit (" + modelAreaUnit.getSymbol() + ")", Issue.SEVERITY_WARNING));
// logger.sendMessage(VCLogger.Priority.MediumPriority,
// VCLogger.ErrorType.UnitError, "unit for compartment '" +
// sbmlComp.getId() + "' (" + unitStr + ") : (" +
// sbmlUnitDefinition.getSymbol() +
// ") not compatible with current area unit (" +
// modelAreaUnit.getSymbol() + ")");
}
}
}
// species
ListOf<org.sbml.jsbml.Species> listOfSpecies = sbmlModel.getListOfSpecies();
for (int i = 0; i < listOfSpecies.size(); i++) {
org.sbml.jsbml.Species sbmlSpecies = listOfSpecies.get(i);
String unitStr = sbmlSpecies.getSubstanceUnits();
VCUnitDefinition sbmlUnitDefinition = null;
if (unitStr != null && unitStr.length() > 0) {
sbmlUnitDefinition = sbmlUnitIdentifierHash.get(unitStr);
} else {
// apply default substance unit
sbmlUnitDefinition = defaultSubstanceUnit;
}
if (modelSubstanceUnit == null) {
modelSubstanceUnit = sbmlUnitDefinition;
} else if (!sbmlUnitDefinition.isEquivalent(modelSubstanceUnit)) {
localIssueList.add(new Issue(new SBMLIssueSource(sbmlSpecies), issueContext, IssueCategory.Units, "unit for species '" + sbmlSpecies.getId() + "' (" + unitStr + ") : (" + sbmlUnitDefinition.getSymbol() + ") not compatible with current substance unit (" + modelSubstanceUnit.getSymbol() + ")", Issue.SEVERITY_WARNING));
// logger.sendMessage(VCLogger.Priority.MediumPriority,
// VCLogger.ErrorType.UnitError, "unit for species '" +
// sbmlSpecies.getId() + "' (" + unitStr + ") : (" +
// sbmlUnitDefinition.getSymbol() +
// ") not compatible with current substance unit (" +
// modelSubstanceUnit.getSymbol() + ")");
}
}
// reactions for SBML level 2 version < 3
long sbmlVersion = sbmlModel.getVersion();
if (sbmlVersion < 3) {
ListOf<Reaction> listOfReactions = sbmlModel.getListOfReactions();
for (int i = 0; i < listOfReactions.size(); i++) {
Reaction sbmlReaction = listOfReactions.get(i);
KineticLaw kineticLaw = sbmlReaction.getKineticLaw();
if (kineticLaw != null) {
// first check substance unit
String unitStr = kineticLaw.getSubstanceUnits();
VCUnitDefinition sbmlUnitDefinition = null;
if (unitStr != null && unitStr.length() > 0) {
sbmlUnitDefinition = sbmlUnitIdentifierHash.get(unitStr);
} else {
// apply default substance unit
sbmlUnitDefinition = defaultSubstanceUnit;
}
if (modelSubstanceUnit == null) {
modelSubstanceUnit = sbmlUnitDefinition;
} else if (!sbmlUnitDefinition.isEquivalent(modelSubstanceUnit)) {
localIssueList.add(new Issue(new SBMLIssueSource(sbmlReaction), issueContext, IssueCategory.Units, "substance unit for reaction '" + sbmlReaction.getId() + "' (" + unitStr + ") : (" + sbmlUnitDefinition.getSymbol() + ") not compatible with current substance unit (" + modelSubstanceUnit.getSymbol() + ")", Issue.SEVERITY_WARNING));
// logger.sendMessage(VCLogger.Priority.MediumPriority,
// VCLogger.ErrorType.UnitError,
// "substance unit for reaction '" +
// sbmlReaction.getId() + "' (" + unitStr + ") : (" +
// sbmlUnitDefinition.getSymbol() +
// ") not compatible with current substance unit (" +
// modelSubstanceUnit.getSymbol() + ")");
}
// check time unit
unitStr = kineticLaw.getTimeUnits();
if (unitStr != null && unitStr.length() > 0) {
sbmlUnitDefinition = sbmlUnitIdentifierHash.get(unitStr);
} else {
// apply default time unit
sbmlUnitDefinition = defaultTimeUnit;
}
if (modelTimeUnit == null) {
modelTimeUnit = sbmlUnitDefinition;
} else if (!sbmlUnitDefinition.isEquivalent(modelTimeUnit)) {
localIssueList.add(new Issue(new SBMLIssueSource(sbmlReaction), issueContext, IssueCategory.Units, "time unit for reaction '" + sbmlReaction.getId() + "' (" + unitStr + ") : (" + sbmlUnitDefinition.getSymbol() + ") not compatible with current time unit (" + modelTimeUnit.getSymbol() + ")", Issue.SEVERITY_WARNING));
// logger.sendMessage(VCLogger.Priority.MediumPriority,
// VCLogger.ErrorType.UnitError,
// "time unit for reaction '" + sbmlReaction.getId() +
// "' (" + unitStr + ") : (" +
// sbmlUnitDefinition.getSymbol() +
// ") not compatible with current time unit (" +
// modelTimeUnit.getSymbol() + ")");
}
}
}
}
if (modelSubstanceUnit == null) {
modelSubstanceUnit = defaultSubstanceUnit;
}
if (modelVolumeUnit == null) {
modelVolumeUnit = defaultVolumeUnit;
}
if (modelAreaUnit == null) {
modelAreaUnit = defaultAreaUnit;
}
if (modelLengthUnit == null) {
modelLengthUnit = defaultLengthUnit;
}
if (modelTimeUnit == null) {
modelTimeUnit = defaultTimeUnit;
}
if (modelSubstanceUnit == null && modelVolumeUnit == null && modelAreaUnit == null && modelLengthUnit == null && modelTimeUnit == null) {
// default (VC)modelUnitSystem
return ModelUnitSystem.createDefaultVCModelUnitSystem();
} else {
return ModelUnitSystem.createSBMLUnitSystem(modelSubstanceUnit, modelVolumeUnit, modelAreaUnit, modelLengthUnit, modelTimeUnit);
}
}
use of cbit.vcell.model.Species in project vcell by virtualcell.
the class SBMLImporter method checkIdentifiersNameLength.
private void checkIdentifiersNameLength() throws Exception {
// Check compartment name lengths
ListOf listofIds = sbmlModel.getListOfCompartments();
boolean bLongCompartmentName = false;
SBase issueSource = null;
for (int i = 0; i < sbmlModel.getNumCompartments(); i++) {
Compartment compartment = (Compartment) listofIds.get(i);
String compartmentName = compartment.getId();
if (compartmentName.length() > 64) {
bLongCompartmentName = true;
issueSource = compartment;
}
}
// Check species name lengths
listofIds = sbmlModel.getListOfSpecies();
boolean bLongSpeciesName = false;
for (int i = 0; i < sbmlModel.getNumSpecies(); i++) {
org.sbml.jsbml.Species species = (org.sbml.jsbml.Species) listofIds.get(i);
String speciesName = species.getId();
if (speciesName.length() > 64) {
bLongSpeciesName = true;
issueSource = species;
}
}
// Check parameter name lengths
listofIds = sbmlModel.getListOfParameters();
boolean bLongParameterName = false;
for (int i = 0; i < sbmlModel.getNumParameters(); i++) {
Parameter param = (Parameter) listofIds.get(i);
String paramName = param.getId();
if (paramName.length() > 64) {
bLongParameterName = true;
issueSource = param;
}
}
// Check reaction name lengths
listofIds = sbmlModel.getListOfReactions();
boolean bLongReactionName = false;
for (int i = 0; i < sbmlModel.getNumReactions(); i++) {
Reaction rxn = (Reaction) listofIds.get(i);
String rxnName = rxn.getId();
if (rxnName.length() > 64) {
bLongReactionName = true;
issueSource = rxn;
}
}
if (bLongCompartmentName || bLongSpeciesName || bLongParameterName || bLongReactionName) {
String warningMsg = "WARNING: The imported model has one or more ";
if (bLongCompartmentName) {
warningMsg = warningMsg + "compartments, ";
}
if (bLongSpeciesName) {
warningMsg = warningMsg + "species, ";
}
if (bLongParameterName) {
warningMsg = warningMsg + "global parameters, ";
}
if (bLongReactionName) {
warningMsg = warningMsg + "reactions ";
}
warningMsg = warningMsg + "that have ids/names that are longer than 64 characters. \n\nUser is STRONGLY recommeded to shorten " + "the names to avoid problems with the length of expressions these names might be used in.";
localIssueList.add(new Issue(new SBMLIssueSource(issueSource), issueContext, IssueCategory.SBMLImport_UnsupportedAttributeOrElement, warningMsg, Issue.SEVERITY_WARNING));
// logger.sendMessage(VCLogger.Priority.MediumPriority,
// VCLogger.ErrorType.UnsupportedConstruct, warningMsg);
}
}
use of cbit.vcell.model.Species in project vcell by virtualcell.
the class SBMLImporter method setSpeciesInitialConditions.
/**
* setSpeciesInitialConditions : called after speciesContexts and global
* parameters have been set. Checks for init conditions set on species in
* the Sbml model, and if it is set using an assignment rule, obtain the
* corresponding expression. Obtain the sbml -> vc unit conversion factor
* for species concentrations to adjust the species initial condition
* units/factor.
*/
private void setSpeciesInitialConditions() {
try {
// fill in SpeciesContextSpec for each speciesContext
Model vcModel = vcBioModel.getSimulationContext(0).getModel();
SpeciesContext[] vcSpeciesContexts = vcModel.getSpeciesContexts();
for (int i = 0; i < vcSpeciesContexts.length; i++) {
org.sbml.jsbml.Species sbmlSpecies = (org.sbml.jsbml.Species) sbmlModel.getSpecies(vcSpeciesContexts[i].getName());
// Sometimes, the species name can be null or a blank string; in
// that case, use species id as the name.
String speciesName = sbmlSpecies.getId();
Compartment compartment = (Compartment) sbmlModel.getCompartment(sbmlSpecies.getCompartment());
Expression initExpr = null;
if (sbmlSpecies.isSetInitialConcentration()) {
// If initial
// Concentration
// is set
Expression initConcentration = new Expression(sbmlSpecies.getInitialConcentration());
// check if initConc is set by a (assignment) rule. That
// takes precedence over initConc value set on species.
initExpr = getValueFromAssignmentRule(speciesName);
if (initExpr == null) {
initExpr = new Expression(initConcentration);
}
} else if (sbmlSpecies.isSetInitialAmount()) {
// If initial
// amount is set
double initAmount = sbmlSpecies.getInitialAmount();
// initConcentration. Else, throw exception.
if (compartment.isSetSize()) {
double compartmentSize = compartment.getSize();
Expression initConcentration = new Expression(0.0);
if (compartmentSize != 0.0) {
initConcentration = new Expression(initAmount / compartmentSize);
} else {
logger.sendMessage(VCLogger.Priority.HighPriority, VCLogger.ErrorType.UnitError, "compartment '" + compartment.getId() + "' has zero size, unable to determine initial concentration for species " + speciesName);
}
// check if initConc is set by a (assignment) rule. That
// takes precedence over initConc/initAmt value set on
// species.
initExpr = getValueFromAssignmentRule(speciesName);
if (initExpr == null) {
initExpr = new Expression(initConcentration);
}
} else {
logger.sendMessage(VCLogger.Priority.HighPriority, VCLogger.ErrorType.SpeciesError, " Compartment '" + compartment.getId() + "' size not set or is defined by a rule; cannot calculate initConc.");
}
} else {
// initConc/initAmt not set; check if species has a
// (assignment) rule.
initExpr = getValueFromAssignmentRule(speciesName);
if (initExpr == null) {
// warning and set it to 0.0
if (sbmlModel.getInitialAssignment(speciesName) == null) {
localIssueList.add(new Issue(new SBMLIssueSource(sbmlModel.getSpecies(speciesName)), issueContext, IssueCategory.SBMLImport_MissingSpeciesInitCondition, "no initial condition for species " + speciesName + ", assuming 0.0", Issue.SEVERITY_WARNING));
// logger.sendMessage(VCLogger.Priority.MediumPriority,
// VCLogger.ErrorType.UnitError,
// "no initial condition for species "+speciesName+", assuming 0.0");
}
initExpr = new Expression(0.0);
}
}
// similar to the conversion that is done in reactions.
if (initExpr != null) {
// initExpr will be changed
initExpr = adjustExpression(initExpr, vcModel);
}
// If any of the symbols in the expression for speciesConc is a
// rule, expand it.
substituteGlobalParamRulesInPlace(initExpr, false);
SpeciesContextSpec speciesContextSpec = vcBioModel.getSimulationContext(0).getReactionContext().getSpeciesContextSpec(vcSpeciesContexts[i]);
speciesContextSpec.getInitialConditionParameter().setExpression(initExpr);
speciesContextSpec.setConstant(sbmlSpecies.getBoundaryCondition() || sbmlSpecies.getConstant());
}
} catch (Throwable e) {
e.printStackTrace(System.out);
throw new SBMLImportException("Error setting initial condition for species context; " + e.getMessage(), e);
}
}
use of cbit.vcell.model.Species in project vcell by virtualcell.
the class SpeciesTable method getSpecies.
/**
* This method was created in VisualAge.
* @return cbit.vcell.model.ReactionParticipant
* @param rset java.sql.ResultSet
*/
public Species getSpecies(java.sql.ResultSet rset, DBSpecies dbSpecies) throws java.sql.SQLException, DataAccessException {
String annotation = rset.getString(SpeciesTable.table.annotation.toString());
if (annotation != null) {
annotation = org.vcell.util.TokenMangler.getSQLRestoredString(annotation);
}
String cNameStr = TokenMangler.getSQLRestoredString(rset.getString(SpeciesTable.table.commonName.toString()));
Species species = new Species(cNameStr, annotation, dbSpecies);
return species;
}
use of cbit.vcell.model.Species in project vcell by virtualcell.
the class RunRefSimulationFastOp method createRefSimBioModel.
private BioModel createRefSimBioModel(KeyValue simKey, User owner, Origin origin, Extent extent, ROI cellROI_2D, double timeStepVal, TimeBounds timeBounds, String varName, Expression initialConcentration, FieldFunctionArguments psfFFA, Expression chirpedDiffusionRate) throws Exception {
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);
geometry.getGeometrySpec().setOrigin(origin);
if (geometry.getGeometrySpec().getNumSubVolumes() != 2) {
throw new Exception("Cell ROI has no ExtraCellular.");
}
String EXTRACELLULAR_NAME = "ec";
String CYTOSOL_NAME = "cyt";
String PLASMAMEMBRANE_NAME = "pm";
ImageSubVolume subVolume0 = (ImageSubVolume) geometry.getGeometrySpec().getSubVolume(0);
ImageSubVolume subVolume1 = (ImageSubVolume) geometry.getGeometrySpec().getSubVolume(1);
if (subVolume0.getPixelValue() == EXTRACELLULAR_PIXVAL) {
subVolume0.setName(EXTRACELLULAR_NAME);
subVolume1.setName(CYTOSOL_NAME);
} else {
subVolume0.setName(CYTOSOL_NAME);
subVolume1.setName(EXTRACELLULAR_NAME);
}
geometry.getGeometrySurfaceDescription().updateAll();
BioModel bioModel = new BioModel(null);
bioModel.setName("unnamed");
Model model = new Model("model");
bioModel.setModel(model);
Feature extracellular = model.addFeature(EXTRACELLULAR_NAME);
Feature cytosol = model.addFeature(CYTOSOL_NAME);
Membrane plasmaMembrane = model.addMembrane(PLASMAMEMBRANE_NAME);
SimulationContext simContext = new SimulationContext(bioModel.getModel(), geometry);
bioModel.addSimulationContext(simContext);
FeatureMapping cytosolFeatureMapping = (FeatureMapping) simContext.getGeometryContext().getStructureMapping(cytosol);
FeatureMapping extracellularFeatureMapping = (FeatureMapping) simContext.getGeometryContext().getStructureMapping(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));
plasmaMembraneMapping.getUnitSizeParameter().setExpression(new Expression(1.0));
// Mobile Species
Species diffusingSpecies = model.addSpecies(new Species("species", "Mobile bleachable species"));
SpeciesContext diffusingSpeciesContext = model.addSpeciesContext(diffusingSpecies, cytosol);
diffusingSpeciesContext.setName(varName);
SpeciesContextSpec scs = simContext.getReactionContext().getSpeciesContextSpec(diffusingSpeciesContext);
scs.getInitialConditionParameter().setExpression(initialConcentration);
chirpedDiffusionRate.bindExpression(scs);
scs.getDiffusionParameter().setExpression(chirpedDiffusionRate);
// simContext.getMicroscopeMeasurement().addFluorescentSpecies(speciesContexts[0]);
// simContext.getMicroscopeMeasurement().setConvolutionKernel(new MicroscopeMeasurement.ProjectionZKernel());
MathDescription mathDescription = simContext.createNewMathMapping().getMathDescription();
// maybe there is a way that works for simContext.getMicroscopeMeasurement().... but this is needed now.
mathDescription.addVariable(new Function(Simulation.PSF_FUNCTION_NAME, new Expression(psfFFA.infix()), null));
simContext.setMathDescription(mathDescription);
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, simContext.getMathDescription());
newSimulation.getSolverTaskDescription().setSolverDescription(SolverDescription.FiniteVolumeStandalone);
simContext.addSimulation(newSimulation);
newSimulation.getSolverTaskDescription().setTimeBounds(timeBounds);
newSimulation.getSolverTaskDescription().setOutputTimeSpec(new UniformOutputTimeSpec(timeStepVal));
newSimulation.getMeshSpecification().setSamplingSize(cellROI_2D.getISize());
newSimulation.getSolverTaskDescription().setTimeStep(new TimeStep(timeStepVal, timeStepVal, timeStepVal));
return bioModel;
}
Aggregations