use of cbit.vcell.model.Model in project vcell by virtualcell.
the class SBMLImporter method addSpecies.
protected void addSpecies(VCMetaData metaData) {
if (sbmlModel == null) {
throw new SBMLImportException("SBML model is NULL");
}
ListOf listOfSpecies = sbmlModel.getListOfSpecies();
if (listOfSpecies == null) {
System.out.println("No Spcecies");
return;
}
HashMap<String, Species> vcSpeciesHash = new HashMap<String, Species>();
HashMap<Species, org.sbml.jsbml.Species> vc_sbmlSpeciesHash = new HashMap<Species, org.sbml.jsbml.Species>();
SpeciesContext[] vcSpeciesContexts = new SpeciesContext[(int) sbmlModel.getNumSpecies()];
// Get species from SBMLmodel; Add/get speciesContext
try {
// First pass - add the speciesContexts
for (int i = 0; i < sbmlModel.getNumSpecies(); i++) {
org.sbml.jsbml.Species sbmlSpecies = (org.sbml.jsbml.Species) listOfSpecies.get(i);
// Sometimes, the species name can be null or a blank string; in
// that case, use species id as the name.
String speciesName = sbmlSpecies.getId();
Species vcSpecies = null;
// create a species with speciesName as commonName. If it is
// different in the annotation, can change it later
Element sbmlImportRelatedElement = sbmlAnnotationUtil.readVCellSpecificAnnotation(sbmlSpecies);
if (sbmlImportRelatedElement != null) {
Element embeddedElement = getEmbeddedElementInAnnotation(sbmlImportRelatedElement, SPECIES_NAME);
if (embeddedElement != null) {
// species.
if (embeddedElement.getName().equals(XMLTags.SpeciesTag)) {
String vcSpeciesName = embeddedElement.getAttributeValue(XMLTags.NameAttrTag);
vcSpecies = vcSpeciesHash.get(vcSpeciesName);
if (vcSpecies == null) {
vcSpecies = new Species(vcSpeciesName, vcSpeciesName);
vcSpeciesHash.put(vcSpeciesName, vcSpecies);
}
}
// if embedded element is not speciesTag, do I have to
// do something?
} else {
// Annotation element is present, but doesn't contain
// the species element.
vcSpecies = new Species(speciesName, speciesName);
vcSpeciesHash.put(speciesName, vcSpecies);
}
} else {
vcSpecies = new Species(speciesName, speciesName);
vcSpeciesHash.put(speciesName, vcSpecies);
}
// store vc & sbml species in hash to read in annotation later
vc_sbmlSpeciesHash.put(vcSpecies, sbmlSpecies);
// Get matching compartment name (of sbmlSpecies[i]) from
// feature list
String compartmentId = sbmlSpecies.getCompartment();
Structure spStructure = vcBioModel.getSimulationContext(0).getModel().getStructure(compartmentId);
vcSpeciesContexts[i] = new SpeciesContext(vcSpecies, spStructure);
vcSpeciesContexts[i].setName(speciesName);
// Adjust units of species, convert to VC units.
// Units in SBML, compute this using some of the attributes of
// sbmlSpecies
Compartment sbmlCompartment = sbmlModel.getCompartment(sbmlSpecies.getCompartment());
int dimension = 3;
if (sbmlCompartment.isSetSpatialDimensions()) {
dimension = (int) sbmlCompartment.getSpatialDimensions();
}
if (dimension == 0 || dimension == 1) {
logger.sendMessage(VCLogger.Priority.HighPriority, VCLogger.ErrorType.UnitError, dimension + " dimensional compartment " + compartmentId + " not supported");
}
}
// end - for sbmlSpecies
// set the species & speciesContexts on model
Model vcModel = vcBioModel.getSimulationContext(0).getModel();
vcModel.setSpecies(vcSpeciesHash.values().toArray(new Species[0]));
vcModel.setSpeciesContexts(vcSpeciesContexts);
// Set annotations and notes from SBML to VCMetadata
Species[] vcSpeciesArray = vc_sbmlSpeciesHash.keySet().toArray(new Species[0]);
for (Species vcSpecies : vcSpeciesArray) {
org.sbml.jsbml.Species sbmlSpecies = vc_sbmlSpeciesHash.get(vcSpecies);
sbmlAnnotationUtil.readAnnotation(vcSpecies, sbmlSpecies);
sbmlAnnotationUtil.readNotes(vcSpecies, sbmlSpecies);
}
} catch (ModelPropertyVetoException e) {
throw new SBMLImportException("Error adding species context; " + e.getMessage(), e);
} catch (Exception e) {
e.printStackTrace(System.out);
throw new SBMLImportException("Error adding species context; " + e.getMessage(), e);
}
}
use of cbit.vcell.model.Model in project vcell by virtualcell.
the class SBMLImporter method getReactionStructure.
/**
* getReactionStructure :
*/
private Structure getReactionStructure(org.sbml.jsbml.Reaction sbmlRxn, SpeciesContext[] speciesContexts, Element sbmlImportElement) throws Exception {
Structure struct = null;
String structName = null;
Model vcModel = vcBioModel.getSimulationContext(0).getModel();
// return structure from vcmodel, if present.
if (bSpatial) {
structName = sbmlRxn.getCompartment();
if (structName != null && structName.length() > 0) {
struct = vcModel.getStructure(structName);
if (struct != null) {
return struct;
}
}
}
// If annotation has structure name, return the corresponding structure.
if (sbmlImportElement != null) {
// Get the embedded element in the annotation str (fluxStep or
// simpleReaction), and the structure attribute from the element.
Element embeddedElement = getEmbeddedElementInAnnotation(sbmlImportElement, REACTION);
if (embeddedElement != null) {
structName = embeddedElement.getAttributeValue(XMLTags.StructureAttrTag);
// Using the structName, get the structure from the structures
// (compartments) list.
struct = vcModel.getStructure(structName);
return struct;
}
}
if (sbmlRxn.isSetKineticLaw()) {
// String rxnName = sbmlRxn.getId();
KineticLaw kLaw = sbmlRxn.getKineticLaw();
Expression kRateExp = getExpressionFromFormula(kLaw.getMath());
String[] symbols = kRateExp.getSymbols();
if (symbols != null) {
for (String symbol : symbols) {
Compartment sbmlCompartment = sbmlModel.getCompartment(symbol);
if (sbmlCompartment != null) {
return vcBioModel.getSimulationContext(0).getModel().getStructure(sbmlCompartment.getId());
}
}
}
}
HashSet<String> refSpeciesNameHash = new HashSet<String>();
getReferencedSpecies(sbmlRxn, refSpeciesNameHash);
java.util.Iterator<String> refSpIterator = refSpeciesNameHash.iterator();
HashSet<String> compartmentNamesHash = new HashSet<String>();
while (refSpIterator.hasNext()) {
String spName = refSpIterator.next();
String rxnCompartmentName = sbmlModel.getSpecies(spName).getCompartment();
compartmentNamesHash.add(rxnCompartmentName);
}
if (compartmentNamesHash.size() == 1) {
struct = vcModel.getStructure(compartmentNamesHash.iterator().next());
return struct;
} else if (compartmentNamesHash.size() == 0) {
struct = vcModel.getStructures()[0];
return struct;
} else {
// more than one structure in reaction participants, try to figure
// out which one to choose
HashMap<String, Integer> structureFrequencyHash = new HashMap<String, Integer>();
for (String structureName : compartmentNamesHash) {
if (structureFrequencyHash.containsKey(structureName)) {
structureFrequencyHash.put(structureName, structureFrequencyHash.get(structName) + 1);
} else {
structureFrequencyHash.put(structureName, 1);
}
}
Iterator<Entry<String, Integer>> iterator = structureFrequencyHash.entrySet().iterator();
Entry<String, Integer> mostUsedStructureEntry = iterator.next();
while (iterator.hasNext()) {
Entry<String, Integer> currentStructureEntry = iterator.next();
if (currentStructureEntry.getValue() > mostUsedStructureEntry.getValue()) {
mostUsedStructureEntry = currentStructureEntry;
}
}
String mostUsedStructureName = mostUsedStructureEntry.getKey();
struct = vcModel.getStructure(mostUsedStructureName);
return struct;
}
}
use of cbit.vcell.model.Model in project vcell by virtualcell.
the class SBMLImporter method addEvents.
protected void addEvents() {
if (sbmlModel.getNumEvents() > 0) {
// VCell does not support events in spatial model
if (bSpatial) {
throw new SBMLImportException("Events are not supported in a spatial VCell model.");
}
ListOf<Event> listofEvents = sbmlModel.getListOfEvents();
Model vcModel = vcBioModel.getSimulationContext(0).getModel();
for (int i = 0; i < sbmlModel.getNumEvents(); i++) {
try {
Event event = listofEvents.get(i);
// trigger - adjust for species context and time conversion
// factors if necessary
Expression triggerExpr = null;
if (event.isSetTrigger()) {
triggerExpr = getExpressionFromFormula(event.getTrigger().getMath());
triggerExpr = adjustExpression(triggerExpr, vcModel);
}
// create bioevent
String eventName = event.getId();
if (eventName == null || eventName.length() == 0) {
eventName = TokenMangler.mangleToSName(event.getName());
// vcBioModel.getSimulationContext(0).
if (eventName == null || eventName.length() == 0) {
eventName = vcBioModel.getSimulationContext(0).getFreeEventName(null);
}
}
// delay
BioEvent vcEvent = new BioEvent(eventName, TriggerType.GeneralTrigger, true, vcBioModel.getSimulationContext(0));
if (event.isSetDelay()) {
Expression durationExpr = null;
durationExpr = getExpressionFromFormula(event.getDelay().getMath());
durationExpr = adjustExpression(durationExpr, vcModel);
boolean bUseValsFromTriggerTime = true;
if (event.isSetUseValuesFromTriggerTime()) {
bUseValsFromTriggerTime = event.isSetUseValuesFromTriggerTime();
} else {
if (durationExpr != null && !durationExpr.isZero()) {
bUseValsFromTriggerTime = false;
}
}
if (durationExpr != null && !durationExpr.isZero()) {
bUseValsFromTriggerTime = false;
}
vcEvent.setUseValuesFromTriggerTime(bUseValsFromTriggerTime);
vcEvent.getParameter(BioEventParameterType.TriggerDelay).setExpression(durationExpr);
}
// event assignments
ArrayList<EventAssignment> vcEvntAssgnList = new ArrayList<EventAssignment>();
for (int j = 0; j < event.getNumEventAssignments(); j++) {
org.sbml.jsbml.EventAssignment sbmlEvntAssgn = event.getEventAssignment(j);
String varName = sbmlEvntAssgn.getVariable();
SymbolTableEntry varSTE = vcBioModel.getSimulationContext(0).getEntry(varName);
if (varSTE != null) {
Expression evntAssgnExpr = getExpressionFromFormula(sbmlEvntAssgn.getMath());
evntAssgnExpr = adjustExpression(evntAssgnExpr, vcModel);
EventAssignment vcEvntAssgn = vcEvent.new EventAssignment(varSTE, evntAssgnExpr);
vcEvntAssgnList.add(vcEvntAssgn);
} else {
logger.sendMessage(VCLogger.Priority.HighPriority, VCLogger.ErrorType.UnsupportedConstruct, "No symbolTableEntry for '" + varName + "'; Cannot add event assignment.");
}
}
vcEvent.setEventAssignmentsList(vcEvntAssgnList);
vcEvent.bind();
vcBioModel.getSimulationContext(0).addBioEvent(vcEvent);
} catch (Exception e) {
e.printStackTrace(System.out);
throw new SBMLImportException(e.getMessage(), e);
}
// end - try/catch
}
// end - for(sbmlEvents)
}
// end - if numEvents > 0)
}
use of cbit.vcell.model.Model in project vcell by virtualcell.
the class SBMLImporter method addInitialAssignments.
protected void addInitialAssignments() {
if (sbmlModel == null) {
throw new SBMLImportException("SBML model is NULL");
}
ListOf listofInitialAssgns = sbmlModel.getListOfInitialAssignments();
if (listofInitialAssgns == null) {
System.out.println("No Initial Assignments specified");
return;
}
Model vcModel = vcBioModel.getSimulationContext(0).getModel();
for (int i = 0; i < sbmlModel.getNumInitialAssignments(); i++) {
try {
InitialAssignment initAssgn = (InitialAssignment) listofInitialAssgns.get(i);
String initAssgnSymbol = initAssgn.getSymbol();
Expression initAssignMathExpr = getExpressionFromFormula(initAssgn.getMath());
// support compartmentSize expressions, warn and bail out.
if (sbmlModel.getCompartment(initAssgnSymbol) != null) {
if (!initAssignMathExpr.isNumeric()) {
logger.sendMessage(VCLogger.Priority.HighPriority, VCLogger.ErrorType.CompartmentError, "compartment '" + initAssgnSymbol + "' size has an initial assignment, cannot handle it at this time.");
}
// if init assgn for compartment is numeric, the numeric
// value for size is set in addCompartments().
}
// or other species. Not allowed for species.
if (!bSpatial && sbmlModel.getSpecies(initAssgnSymbol) != null) {
if (initAssignMathExpr.hasSymbol(vcModel.getX().getName()) || initAssignMathExpr.hasSymbol(vcModel.getY().getName()) || initAssignMathExpr.hasSymbol(vcModel.getZ().getName())) {
logger.sendMessage(VCLogger.Priority.HighPriority, VCLogger.ErrorType.SpeciesError, "species '" + initAssgnSymbol + "' initial assignment expression cannot contain 'x', 'y', 'z'.");
}
}
initAssignMathExpr = adjustExpression(initAssignMathExpr, vcModel);
// set the init assgn expr on VCell species init condn or global
// parameter expression
SpeciesContextSpec scs = vcBioModel.getSimulationContext(0).getReactionContext().getSpeciesContextSpec(vcBioModel.getSimulationContext(0).getModel().getSpeciesContext(initAssgnSymbol));
ModelParameter mp = vcBioModel.getSimulationContext(0).getModel().getModelParameter(initAssgnSymbol);
if (scs != null) {
scs.getInitialConditionParameter().setExpression(initAssignMathExpr);
} else if (mp != null) {
mp.setExpression(initAssignMathExpr);
} else {
localIssueList.add(new Issue(new SBMLIssueSource(initAssgn), issueContext, IssueCategory.SBMLImport_UnsupportedAttributeOrElement, "Symbol '" + initAssgnSymbol + "' not a species or global parameter in VCell; initial assignment ignored.", Issue.SEVERITY_WARNING));
// logger.sendMessage(VCLogger.Priority.MediumPriority,
// VCLogger.ErrorType.UnsupportedConstruct,
// "Symbol '"+initAssgnSymbol+"' not a species or global parameter in VCell; initial assignment ignored..");
}
} catch (Exception e) {
e.printStackTrace(System.out);
throw new RuntimeException("Error reading InitialAssignment : " + e.getMessage());
}
}
}
use of cbit.vcell.model.Model 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);
}
}
Aggregations