use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class FRAPStudy method createNewRefBioModel.
public static BioModel createNewRefBioModel(FRAPStudy sourceFrapStudy, String baseDiffusionRate, TimeStep tStep, KeyValue simKey, User owner, FieldDataIdentifierSpec psfFDIS, 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());
Extent extent = sourceFrapStudy.getFrapData().getImageDataset().getExtent();
TimeBounds timeBounds = FRAPOptData.getEstimatedRefTimeBound(sourceFrapStudy);
double timeStepVal = FRAPOptData.REFERENCE_DIFF_DELTAT;
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);
Feature extracellular = model.addFeature(EXTRACELLULAR_NAME);
Feature cytosol = model.addFeature(CYTOSOL_NAME);
Membrane plasmaMembrane = model.addMembrane(PLASMAMEMBRANE_NAME);
String roiDataName = FRAPStudy.ROI_EXTDATA_NAME;
final int ONE_DIFFUSION_SPECIES_COUNT = 1;
final int MOBILE_SPECIES_INDEX = 0;
Expression[] diffusionConstants = new Expression[ONE_DIFFUSION_SPECIES_COUNT];
Species[] species = new Species[ONE_DIFFUSION_SPECIES_COUNT];
SpeciesContext[] speciesContexts = new SpeciesContext[ONE_DIFFUSION_SPECIES_COUNT];
Expression[] initialConditions = new Expression[ONE_DIFFUSION_SPECIES_COUNT];
// Mobile Species
diffusionConstants[MOBILE_SPECIES_INDEX] = new Expression(baseDiffusionRate);
species[MOBILE_SPECIES_INDEX] = new Species(SPECIES_NAME_PREFIX_MOBILE, "Mobile bleachable species");
speciesContexts[MOBILE_SPECIES_INDEX] = new SpeciesContext(null, species[MOBILE_SPECIES_INDEX].getCommonName(), species[MOBILE_SPECIES_INDEX], cytosol);
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());
initialConditions[MOBILE_SPECIES_INDEX] = Expression.div(expPostBleach_first, expPreBleach_avg);
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));
for (int i = 0; i < initialConditions.length; i++) {
model.addSpecies(species[i]);
model.addSpeciesContext(speciesContexts[i]);
}
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 PSF function
mathDesc.addVariable(new Function(Simulation.PSF_FUNCTION_NAME, new Expression(psfFDIS.getFieldFuncArgs().infix()), 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, 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;
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class SpeciesContextShape method paintSelf.
@Override
public void paintSelf(Graphics2D g, int absPosX, int absPosY) {
boolean isBound = false;
SpeciesContext sc = (SpeciesContext) getModelObject();
boolean bHasPCLink = false;
if (graphModel instanceof ModelCartoon) {
ModelCartoon mc = (ModelCartoon) graphModel;
// check if species has Pathway Commons link by querying VCMetadata : if it does, need to change color of speciesContext.
try {
MiriamManager miriamManager = mc.getModel().getVcMetaData().getMiriamManager();
Map<MiriamRefGroup, MIRIAMQualifier> miriamRefGroups = miriamManager.getAllMiriamRefGroups(sc.getSpecies());
if (miriamRefGroups != null && miriamRefGroups.size() > 0) {
bHasPCLink = true;
}
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
if (sc.getSpecies().getDBSpecies() != null || bHasPCLink) {
isBound = true;
}
int shapeHeight = getSpaceManager().getSize().height;
int shapeWidth = getSpaceManager().getSize().width;
int offsetX = (shapeWidth - circleDiameter) / 2;
int offsetY = (shapeHeight - circleDiameter) / 2;
Graphics2D g2D = g;
// if (icon == null) {
icon = new Area();
icon.add(new Area(new Ellipse2D.Double(offsetX, offsetY, circleDiameter, circleDiameter)));
// icon.add(new Area(new RoundRectangle2D.Double(offsetX, offsetY,circleDiameter,circleDiameter,circleDiameter/2,circleDiameter/2)));
// }
Area movedIcon = icon.createTransformedArea(AffineTransform.getTranslateInstance(absPosX, absPosY));
if (sc.getSpeciesPattern() == null) {
defaultBG = java.awt.Color.green;
} else {
defaultBG = java.awt.Color.blue;
}
if (isCatalyst == true) {
defaultBG = java.awt.Color.magenta;
}
backgroundColor = defaultBG;
darkerBackground = backgroundColor.darker().darker();
// g.setColor((!isBound && !isSelected()?darkerBackground:backgroundColor));
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color exterior = !isBound && !isSelected() ? darkerBackground : backgroundColor;
// Color interior = exterior.brighter().brighter();
Point2D center = new Point2D.Float(absPosX + circleDiameter * 0.5f, absPosY + circleDiameter * 0.5f);
float radius = circleDiameter * 0.5f;
Point2D focus = new Point2D.Float(absPosX + circleDiameter * 0.4f, absPosY + circleDiameter * 0.4f);
float[] dist = { 0.1f, 1.0f };
Color[] colors = { Color.white, exterior };
// Color[] colors = {interior, exterior};
RadialGradientPaint p = new RadialGradientPaint(center, radius, focus, dist, colors, CycleMethod.NO_CYCLE);
g2D.setPaint(p);
g2D.fill(movedIcon);
g.setColor(forgroundColor);
g2D.draw(movedIcon);
// draw label
if (getLabel() != null && getLabel().length() > 0) {
if (isSelected()) {
// clear background and outline to make selected label stand out
Rectangle outlineRectangle = getLabelOutline(absPosX, absPosY);
drawRaisedOutline(outlineRectangle.x, outlineRectangle.y, outlineRectangle.width, outlineRectangle.height, g, Color.white, forgroundColor, Color.gray);
}
g.setColor(forgroundColor);
g.drawString((isSelected() || smallLabel == null ? getLabel() : smallLabel), (isSelected() || smallLabel == null ? getLabelPos().x : smallLabelPos.x) + absPosX, getLabelPos().y + absPosY);
}
if (linkText != null && linkText != "") {
ShapePaintUtil.paintLinkMark(g2D, this, Color.BLACK);
}
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class SpeciesPatternRoundShape method paintCompartment.
public void paintCompartment(Graphics g) {
Color structureColor = Color.black;
Structure structure = null;
if (owner instanceof ReactionRule && !speciesShapes.isEmpty()) {
ReactionRule rr = (ReactionRule) owner;
ReactantPattern rp = rr.getReactantPattern(sp);
ProductPattern pp = rr.getProductPattern(sp);
if (rp != null) {
structure = rp.getStructure();
} else if (pp != null) {
structure = pp.getStructure();
} else {
structure = ((ReactionRule) owner).getStructure();
}
} else if (owner instanceof SpeciesContext && ((SpeciesContext) owner).hasSpeciesPattern()) {
structure = ((SpeciesContext) owner).getStructure();
structureColor = Color.gray;
} else if (owner instanceof RbmObservable && !speciesShapes.isEmpty()) {
structure = ((RbmObservable) owner).getStructure();
} else {
// other things don't have structure
return;
}
if (structure == null) {
return;
}
// don't have a real structure, so we show them structureless (for now)
if (structure.getName() == null) {
return;
}
Graphics2D g2 = (Graphics2D) g;
Color colorOld = g2.getColor();
Paint paintOld = g2.getPaint();
Font fontOld = g2.getFont();
Font font;
// width of compartment shape, adjusted continuously based on zoom factor
int w;
String name = structure.getName();
int z = shapePanel.getZoomFactor();
if (z > -3) {
font = fontOld.deriveFont(Font.BOLD);
g.setFont(font);
w = 46 + 3 * z;
name = buildCompartmentName(g, name, "..", w);
} else if (z < LargeShapeCanvas.SmallestZoomFactorWithText) {
font = fontOld.deriveFont(fontOld.getSize2D() * 0.8f);
g.setFont(font);
w = 20;
name = buildCompartmentName(g, name, ".", w);
} else {
font = fontOld;
g.setFont(font);
w = 44 + 3 * z;
name = buildCompartmentName(g, name, "..", w);
}
// a bit darker for border
Color darker = Color.gray;
Rectangle2D border = new Rectangle2D.Double(xPos - 9, yPos - 4, w, 58);
g2.setColor(darker);
g2.draw(border);
Color lighter = new Color(224, 224, 224);
Rectangle2D filling = new Rectangle2D.Double(xPos - 9, yPos - 3, w, 57);
g2.setPaint(lighter);
g2.fill(filling);
g.setColor(structureColor);
g2.drawString(name, xPos - 4, yPos + 48);
g2.setFont(fontOld);
g2.setPaint(paintOld);
g2.setColor(colorOld);
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class AbstractMathMapping method getDefaultGeometryClass.
protected GeometryClass getDefaultGeometryClass(Expression expr) throws ExpressionException, MappingException {
GeometryClass geometryClass = null;
if (simContext.getGeometry().getDimension() == 0) {
return null;
}
String[] symbols = expr.getSymbols();
// if expr has no symbols, model param cannot be localized to a domain (its a const).
if (symbols == null) {
return null;
} else {
Expression modelParamExpr = substituteGlobalParameters(expr);
symbols = modelParamExpr.getSymbols();
for (int k = 0; symbols != null && k < symbols.length; k++) {
Structure symbolStructure = null;
SymbolTableEntry ste = modelParamExpr.getSymbolBinding(symbols[k]);
if (ste instanceof SpeciesContext) {
symbolStructure = ((SpeciesContext) ste).getStructure();
} else if (ste instanceof StructureSize) {
symbolStructure = ((StructureSize) ste).getStructure();
} else if (ste instanceof MembraneVoltage) {
symbolStructure = ((MembraneVoltage) ste).getMembrane();
}
if (symbolStructure != null) {
StructureMapping sm = simContext.getGeometryContext().getStructureMapping(symbolStructure);
GeometryClass symbolGeomClass = sm.getGeometryClass();
if (geometryClass == null) {
geometryClass = symbolGeomClass;
} else {
if (geometryClass != symbolGeomClass) {
if (geometryClass instanceof SurfaceClass) {
if (symbolGeomClass instanceof SurfaceClass) {
throw new MappingException("The expression '" + expr.infix() + "' references variables in surface domain '" + geometryClass.getName() + "' & surface domain '" + symbolGeomClass.getName() + "' that cannot be evaluated.");
} else if (symbolGeomClass instanceof SubVolume) {
// geomClass : surfaceClass; symbolGeomClass : subVol
if (!((SurfaceClass) geometryClass).isAdjacentTo((SubVolume) symbolGeomClass)) {
throw new MappingException("The expression '" + expr.infix() + "' references variables in surface domain '" + geometryClass.getName() + "' & volume domain '" + symbolGeomClass.getName() + "' that cannot be evaluated.");
}
} else {
throw new MappingException("unexpected geometry class : " + symbolGeomClass.getClass());
}
} else if (geometryClass instanceof SubVolume) {
// geometryClass is a SubVolume
if (symbolGeomClass instanceof SubVolume) {
// check if adjacent; if so, choose separating membrane.
SurfaceClass surfaceClass = simContext.getGeometry().getGeometrySurfaceDescription().getSurfaceClass((SubVolume) symbolGeomClass, (SubVolume) geometryClass);
if (surfaceClass != null) {
geometryClass = surfaceClass;
} else {
throw new MappingException("The expression '" + expr.infix() + "' references variables in volume domain '" + geometryClass.getName() + "' & volume domain '" + symbolGeomClass.getName() + "' that cannot be evaluated.");
}
} else {
// geomClass : subVol; symbolGeomClass = surfaceClass
SurfaceClass surfaceSymbolGeomClass = (SurfaceClass) symbolGeomClass;
if (!surfaceSymbolGeomClass.isAdjacentTo((SubVolume) geometryClass)) {
throw new MappingException("The expression '" + expr.infix() + "' references variables in surface domain '" + surfaceSymbolGeomClass.getName() + "' & volume domain '" + geometryClass.getName() + "' that cannot be evaluated.");
} else {
geometryClass = symbolGeomClass;
}
}
} else {
throw new MappingException("unexpected geometry class : " + geometryClass.getClass());
}
}
}
}
}
}
return geometryClass;
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class StructureAnalyzer method refreshFastMatrices.
/**
* This method was created in VisualAge.
*/
private void refreshFastMatrices() throws Exception {
// System.out.println("StructureAnalyzer.refreshFastMatrices()");
//
// update scheme matrix for fast system
//
fastSchemeMatrix = new RationalNumberMatrix(fastSpeciesContextMappings.length, fastReactionSteps.length);
for (int i = 0; i < fastSpeciesContextMappings.length; i++) {
for (int j = 0; j < fastReactionSteps.length; j++) {
fastSchemeMatrix.set_elem(i, j, fastReactionSteps[j].getStoichiometry(fastSpeciesContextMappings[i].getSpeciesContext()));
}
}
//
for (int i = 0; i < fastSpeciesContextMappings.length; i++) {
SpeciesContextMapping scm = fastSpeciesContextMappings[i];
SpeciesContext sc = scm.getSpeciesContext();
//
// collect fast rate expression
//
Expression exp = new Expression(0.0);
for (int j = 0; j < fastReactionSteps.length; j++) {
int stoichiometry = fastReactionSteps[j].getStoichiometry(sc);
ReactionSpec reactionSpec = mathMapping_4_8.getSimulationContext().getReactionContext().getReactionSpec(fastReactionSteps[j]);
if (stoichiometry != 0) {
if (!reactionSpec.isFast()) {
throw new Exception("expected only fast rates");
}
if (reactionSpec.isExcluded()) {
throw new Exception("expected only included rates");
}
ReactionParticipant[] rps = fastReactionSteps[j].getReactionParticipants();
ReactionParticipant rp0 = null;
for (ReactionParticipant rp : rps) {
if (rp.getSpeciesContext() == sc) {
rp0 = rp;
break;
}
}
//
if (rp0 != null) {
Structure structure = fastReactionSteps[j].getStructure();
Expression fastRateExpression = getReactionRateExpression(fastReactionSteps[j], rp0).renameBoundSymbols(mathMapping_4_8.getNameScope());
if ((structure instanceof Membrane) && (rp0.getStructure() != structure)) {
Membrane membrane = (Membrane) structure;
MembraneMapping membraneMapping = (MembraneMapping) mathMapping_4_8.getSimulationContext().getGeometryContext().getStructureMapping(membrane);
Expression fluxCorrection = new Expression(mathMapping_4_8.getFluxCorrectionParameter(membraneMapping, (Feature) rp0.getStructure()), mathMapping_4_8.getNameScope());
exp = Expression.add(exp, Expression.mult(fluxCorrection, fastRateExpression));
} else {
exp = Expression.add(exp, new Expression(fastRateExpression));
}
}
}
}
// exp.bindExpression(mathMapping);
scm.setFastRate(exp.flatten());
}
// System.out.println("StructureAnalyzer.refreshFastMatrices(), scheme matrix:");
// fastSchemeMatrix.show();
//
// update null space matrix
//
fastNullSpaceMatrix = fastSchemeMatrix.findNullSpace();
// if (fastNullSpaceMatrix==null){
// System.out.println("fast system has full rank");
// }else{
// System.out.println("StructureAnalyzer.refreshFastMatrices(), nullSpace matrix:");
// fastNullSpaceMatrix.show();
// }
}
Aggregations