use of cbit.vcell.model.Model in project vcell by virtualcell.
the class BioModelEditorConversionTableModel method createTableRowForTransportParticipant.
private ConversionTableRow createTableRowForTransportParticipant(BioPaxObject bpObject, String interactionId, String interactionLabel, String participantType, double stoich, HashSet<RelationshipObject> relationshipObjects) {
String location = "";
ConversionTableRow conversionTableRow = new ConversionTableRow(bpObject);
conversionTableRow.setInteractionId(interactionId);
conversionTableRow.setInteractionLabel(interactionLabel);
conversionTableRow.setParticipantType(participantType);
// stoichiometry and location
Model model = bioModel.getModel();
StructureTopology structTopology = model.getStructureTopology();
if (participantType.equals("Reactant")) {
// stoichiometry
if (stoich != 0)
conversionTableRow.setStoich(stoich);
else
conversionTableRow.setStoich(1.0);
// else{
if (model.getMembranes().size() > 0)
location = structTopology.getOutsideFeature(model.getMembranes().get(0)).getName();
else
location = model.getStructures()[0].getName();
// }
conversionTableRow.setLocation(location);
} else if (participantType.equals("Product")) {
// stoichiometry
if (stoich != 0)
conversionTableRow.setStoich(stoich);
else
conversionTableRow.setStoich(1.0);
// else{
if (model.getMembranes().size() > 0)
location = structTopology.getInsideFeature(model.getMembranes().get(0)).getName();
else
location = model.getStructures()[0].getName();
// }
conversionTableRow.setLocation(location);
} else {
conversionTableRow.setStoich(1.0);
// else
if (bpObject instanceof Transport) {
if (model.getMembranes().size() > 0)
location = model.getMembranes().get(0).getName();
else
location = model.getStructures()[0].getName();
} else
location = model.getStructures()[0].getName();
conversionTableRow.setLocation(location);
}
// id
if (relationshipObjects == null) {
if (bpObject instanceof Entity) {
String id = (BioPAXUtil.getName((Entity) bpObject) + "_" + location).trim();
if (isValid(id))
conversionTableRow.setId(id);
else
conversionTableRow.setId(changeID(id));
}
} else {
String id = null;
for (RelationshipObject relationshipObject : relationshipObjects) {
if (relationshipObject.getBioModelEntityObject().getStructure().getName().equalsIgnoreCase(location)) {
id = relationshipObject.getBioModelEntityObject().getName();
}
}
if (id != null) {
// the linked bmObject with the same location will be used
conversionTableRow.setId(id);
} else {
// a new bmObject will be created if no linked bmObject in the same location
if (bpObject instanceof Entity) {
id = (BioPAXUtil.getName((Entity) bpObject) + "_" + location).trim();
if (isValid(id))
conversionTableRow.setId(id);
else
conversionTableRow.setId(changeID(id));
}
}
}
return conversionTableRow;
}
use of cbit.vcell.model.Model in project vcell by virtualcell.
the class BioModelEditorModelPanel method duplicateReactionRule.
private ReactionRule duplicateReactionRule(ReactionRule oldRule, Structure structure) {
try {
ReactionRule newRule = ReactionRule.duplicate(oldRule, structure);
Model m = oldRule.getModel();
m.getRbmModelContainer().addReactionRule(newRule);
return newRule;
} catch (PropertyVetoException | ExpressionBindingException e) {
e.printStackTrace();
throw new RuntimeException("Problem duplicating " + ReactionRule.typeName + " " + oldRule.getDisplayName());
}
}
use of cbit.vcell.model.Model in project vcell by virtualcell.
the class XmlReader method getBioModel.
/**
* This method returns a Biomodel object from a XML Element.
* Creation date: (3/13/2001 12:35:00 PM)
* @return cbit.vcell.biomodel.BioModel
* @param param org.jdom.Element
*/
public BioModel getBioModel(Element param, VCellSoftwareVersion docVcellSoftwareVersion) throws XmlParseException {
this.docVCellSoftwareVersion = docVcellSoftwareVersion;
// long l1 = System.currentTimeMillis();
// Get metadata information Version (if available)
Version version = getVersion(param.getChild(XMLTags.VersionTag, vcNamespace));
// Create new biomodel
BioModel biomodel = new BioModel(version);
// Set name
String name = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
try {
biomodel.setName(name);
// String annotation = param.getAttributeValue(XMLTags.AnnotationAttrTag);
// if (annotation!=null) {
// biomodel.setDescription(unMangle(annotation));
// }
// get annotation
String annotationText = param.getChildText(XMLTags.AnnotationTag, vcNamespace);
if (annotationText != null && annotationText.length() > 0) {
biomodel.setDescription(unMangle(annotationText));
}
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace();
throw new XmlParseException(e);
}
// long l2 = System.currentTimeMillis();
// System.out.println("biomodel-------- "+((double)(l2-l1))/1000);
// ***Add biomodel to the dictionnary***
// dictionnary.put(simcontext.getClass().getName()+":"+simcontext.getName(), simcontext);
// Set model
Model newmodel = getModel(param.getChild(XMLTags.ModelTag, vcNamespace));
biomodel.setModel(newmodel);
// Set simulation contexts
java.util.List<Element> children = param.getChildren(XMLTags.SimulationSpecTag, vcNamespace);
java.util.Iterator<Element> iterator = children.iterator();
// System.out.println("model-------- "+((double)(l3-l2))/1000);
while (iterator.hasNext()) {
// long l4 = System.currentTimeMillis();
Element tempElement = iterator.next();
SimulationContext simContext = getSimulationContext(tempElement, biomodel);
try {
biomodel.addSimulationContext(simContext);
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace();
throw new XmlParseException("An error occurred while trying to add the SimContext " + simContext.getName() + " to the BioModel Object!", e);
}
// process the simulations within this Simspec
Iterator<Element> simIterator = tempElement.getChildren(XMLTags.SimulationTag, vcNamespace).iterator();
// System.out.println("simcontext-------- "+((double)(l5-l4))/1000);
while (simIterator.hasNext()) {
try {
biomodel.addSimulation(getSimulation((Element) simIterator.next(), simContext.getMathDescription()));
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace();
throw new XmlParseException("A PropertyVetoException occurred when adding a Simulation entity to the BioModel " + name, e);
}
}
// long l6 = System.currentTimeMillis();
// System.out.println("sims-------- "+((double)(l6-l5))/1000);
}
// biomodel.getVCMetaData().setAnnotation(biomodel, param);
// biomodel.getVCMetaData().setNotes(biomodel, param);
boolean bMetaDataPopulated = false;
List<Element> elementsMetaData = param.getChildren(XMLMetaData.VCMETADATA_TAG, VCMetaData.nsVCML);
if (elementsMetaData != null && elementsMetaData.size() > 0) {
for (Element elementMetaData : elementsMetaData) {
XMLMetaDataReader.readFromElement(biomodel.getVCMetaData(), biomodel, elementMetaData);
}
bMetaDataPopulated = true;
} else {
// no metadata was found, populate vcMetaData from biomodel (mainly free text annotation for identifiables)
if (!bMetaDataPopulated) {
biomodel.populateVCMetadata(bMetaDataPopulated);
}
}
Element pathwayElement = param.getChild(XMLTags.PathwayModelTag, vcNamespace);
if (pathwayElement != null) {
Element rdfElement = pathwayElement.getChild(XMLRDF.tagRDF, XMLRDF.nsRDF);
if (rdfElement != null) {
PathwayReaderBiopax3 pathwayReader = new PathwayReaderBiopax3(new RDFXMLContext());
PathwayModel pathwayModel = pathwayReader.parse(rdfElement, false);
// ??? is this needed ???
pathwayModel.reconcileReferences(null);
// we keep as lvl 1 only the objects which we want to show in the diagram
pathwayModel.filterDiagramObjects();
biomodel.getPathwayModel().merge(pathwayModel);
} else {
throw new XmlParseException("expecting RDF element as child of pathwayModel within VCML document");
}
}
Element relationshipElement = param.getChild(XMLTags.RelationshipModelTag, vcNamespace);
if (relationshipElement != null) {
Element rmnsElement = relationshipElement.getChild("RMNS", vcNamespace);
if (rmnsElement != null) {
RelationshipReader relationshipReader = new RelationshipReader();
RelationshipModel relationshipModel = relationshipReader.parse(rmnsElement, biomodel);
biomodel.getRelationshipModel().merge(relationshipModel);
} else {
// throw new XmlParseException("expecting RMNS element as child of pathwayModel within VCML document");
}
}
return biomodel;
}
use of cbit.vcell.model.Model in project vcell by virtualcell.
the class XmlReader method getModel.
/**
* This method creates a Model object from a XML element.
* Creation date: (3/14/2001 6:14:37 PM)
* @return cbit.vcell.model.Model
* @param param org.jdom.Element
*/
private Model getModel(Element param) throws XmlParseException {
if (param == null) {
throw new XmlParseException("Invalid 'NULL' XML 'model' element arrived!");
}
// Get version, if any
Model newmodel = null;
Version version = getVersion(param.getChild(XMLTags.VersionTag, vcNamespace));
// if forcedModelUnitSystem has been set, ues that (could be overriding unit system for SBML export)
if (forcedModelUnitSystem != null) {
newmodel = new Model(version, forcedModelUnitSystem);
} else {
Element unitSystemNode = param.getChild(XMLTags.ModelUnitSystemTag, vcNamespace);
if (unitSystemNode != null) {
ModelUnitSystem modelUnitSystem = getUnitSystem(unitSystemNode);
newmodel = new Model(version, modelUnitSystem);
} else {
newmodel = new Model(version);
}
}
try {
// Set attributes
newmodel.setName(unMangle(param.getAttributeValue(XMLTags.NameAttrTag)));
// Add annotation
String annotationText = param.getChildText(XMLTags.AnnotationTag, vcNamespace);
if (annotationText != null && annotationText.length() > 0) {
newmodel.setDescription(unMangle(annotationText));
}
// Add global parameters
Element globalParamsElement = param.getChild(XMLTags.ModelParametersTag, vcNamespace);
if (globalParamsElement != null) {
ModelParameter[] modelParams = getModelParams(globalParamsElement, newmodel);
// add global/model param to model - done inside getModelParam by passing newModel
newmodel.setModelParameters(modelParams);
}
// Add Species (Compounds)
Iterator<Element> iterator = param.getChildren(XMLTags.SpeciesTag, vcNamespace).iterator();
ArrayList<Species> speciesList = new ArrayList<Species>();
while (iterator.hasNext()) {
org.jdom.Element temp = (Element) iterator.next();
speciesList.add(getSpecies(temp));
}
newmodel.setSpecies(speciesList.toArray(new Species[speciesList.size()]));
// Add Structures
LinkedList<Structure> newstructures = new LinkedList<Structure>();
// (features)
List<Element> children = param.getChildren(XMLTags.FeatureTag, vcNamespace);
for (Element featureElement : children) {
newstructures.add(getFeature(featureElement));
}
// (Membrane)
children = param.getChildren(XMLTags.MembraneTag, vcNamespace);
for (Element memElement : children) {
newstructures.add(getMembrane(newmodel, memElement, newstructures));
}
if (newstructures.size() > 0) {
Structure[] structarray = new Structure[newstructures.size()];
newstructures.toArray(structarray);
// Add all the retrieved structures
newmodel.setStructures(structarray);
}
// retrieve the RbmModelContainer, if present - must be done before we retrieve species context!
Element element = param.getChild(XMLTags.RbmModelContainerTag, vcNamespace);
if (element != null) {
getRbmModelContainer(element, newmodel);
} else {
lg.info("RbmModelContainer is missing.");
}
// Add SpeciesContexts
children = param.getChildren(XMLTags.SpeciesContextTag, vcNamespace);
SpeciesContext[] newspeccon = new SpeciesContext[children.size()];
int scCounter = 0;
for (Element scElement : children) {
newspeccon[scCounter] = getSpeciesContext(scElement, newmodel);
scCounter++;
}
newmodel.setSpeciesContexts(newspeccon);
// Retrieve rateRules and add to model
// Element rateRuleVarsElement = param.getChild(XMLTags.RateRuleVariablesTag, vcNamespace);
// if(rateRuleVarsElement != null){
// RateRuleVariable[] rateRuleVars = getRateRuleVariables(rateRuleVarsElement, newmodel);
// newmodel.setRateRuleVariables(rateRuleVars);
// }
// Add Reaction steps (if available)
// (Simplereaction)
// Create a varHash with reserved symbols and global parameters, if any, to pass on to Kinetics
// must create new hash for each reaction and flux, since each kinetics uses new variables hash
iterator = param.getChildren(XMLTags.SimpleReactionTag, vcNamespace).iterator();
ArrayList<ReactionStep> reactionStepList = new ArrayList<ReactionStep>();
while (iterator.hasNext()) {
org.jdom.Element temp = iterator.next();
reactionStepList.add(getSimpleReaction(temp, newmodel));
}
// (fluxStep)
iterator = param.getChildren(XMLTags.FluxStepTag, vcNamespace).iterator();
while (iterator.hasNext()) {
org.jdom.Element temp = iterator.next();
reactionStepList.add(getFluxReaction(temp, newmodel));
}
newmodel.setReactionSteps(reactionStepList.toArray(new ReactionStep[reactionStepList.size()]));
// Add Diagrams
children = param.getChildren(XMLTags.DiagramTag, vcNamespace);
if (children.size() > 0) {
Diagram[] newdiagrams = new Diagram[children.size()];
int diagramCounter = 0;
for (Element diagramElement : children) {
newdiagrams[diagramCounter] = getDiagram(diagramElement, newmodel);
diagramCounter++;
}
reorderDiagramsInPlace_UponRead(docVCellSoftwareVersion, newdiagrams, newmodel.getStructureTopology());
// if(docVCellSoftwareVersion != null && !docVCellSoftwareVersion.isValid() && docVCellSoftwareVersion.getMajorVersion()<=5 && docVCellSoftwareVersion.getMinorVersion() <=2){
// //In Vcell 5.2 and previous we need to order diagrams topologically, in 5.3 and later the diagrams are displayed as they are ordered when read from document
// final StructureTopology structureTopology = newmodel.getStructureTopology();
// Arrays.sort(newdiagrams, new Comparator<Diagram>() {
// @Override
// public int compare(Diagram o1, Diagram o2) {
// return getStructureLevel(o1.getStructure(), structureTopology) - getStructureLevel(o2.getStructure(), structureTopology);
// }
// });
// }
newmodel.setDiagrams(newdiagrams);
}
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace();
throw new XmlParseException(e);
} catch (ModelException e) {
e.printStackTrace();
}
// model param expresions are not bound when they are read in, since they could be functions of each other or structures/speciesContexts.
// Hence bind the model param exprs at the end, after reading all model level quantities.
ModelParameter[] modelParameters = newmodel.getModelParameters();
for (int i = 0; modelParameters != null && i < modelParameters.length; i++) {
try {
modelParameters[i].getExpression().bindExpression(newmodel);
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
throw new RuntimeException("Error binding global parameter '" + modelParameters[i].getName() + "' to model." + e.getMessage());
}
}
return newmodel;
}
use of cbit.vcell.model.Model in project vcell by virtualcell.
the class RbmNetworkGenerator method writeBngl_internal.
public static void writeBngl_internal(SimulationContext simulationContext, PrintWriter writer, Map<FakeReactionRuleRateParameter, LocalParameter> kineticsParameterMap, Map<FakeSeedSpeciesInitialConditionsParameter, Pair<SpeciesContext, Expression>> speciesEquivalenceMap, NetworkGenerationRequirements networkGenerationRequirements, CompartmentMode compartmentMode) {
String callerClassName = new Exception().getStackTrace()[1].getClassName();
String networkTransformerClassName = NetworkTransformer.class.getName();
String rulebasedTransformerClassName = RulebasedTransformer.class.getName();
if (!callerClassName.equals(networkTransformerClassName) && !callerClassName.equals(rulebasedTransformerClassName)) {
throw new UnsupportedOperationException("This method may only be called from within a " + networkTransformerClassName + " or " + rulebasedTransformerClassName + " instance.");
}
Model model = simulationContext.getModel();
RbmModelContainer rbmModelContainer = model.getRbmModelContainer();
checkConsistency(model);
// first we prepare the fake parameters we need to maintain the relationship between the species context and the seed species
List<FakeSeedSpeciesInitialConditionsParameter> fakeParameterList = new ArrayList<FakeSeedSpeciesInitialConditionsParameter>();
List<String> seedSpeciesList = new ArrayList<String>();
SpeciesContext[] speciesContexts = model.getSpeciesContexts();
for (int i = 0; i < speciesContexts.length; i++) {
SpeciesContext sc = speciesContexts[i];
if (!sc.hasSpeciesPattern()) {
continue;
}
SpeciesContextSpec scs = simulationContext.getReactionContext().getSpeciesContextSpec(sc);
Expression initialConcentration = scs.getParameter(SpeciesContextSpec.ROLE_InitialConcentration).getExpression();
// fake initial values for the seed species, we need to present them to bngl as parameters
FakeSeedSpeciesInitialConditionsParameter fakeSeedSpeciesParam = new FakeSeedSpeciesInitialConditionsParameter(sc.getName());
Pair<SpeciesContext, Expression> p = new Pair<SpeciesContext, Expression>(sc, initialConcentration);
speciesEquivalenceMap.put(fakeSeedSpeciesParam, p);
String modified;
if (compartmentMode == CompartmentMode.show) {
modified = RbmUtils.toBnglString(sc.getSpeciesPattern(), null, CompartmentMode.hide, 0);
modified = "@" + sc.getStructure().getName() + ":" + modified;
} else if (compartmentMode == CompartmentMode.asSite) {
modified = RbmUtils.toBnglString(sc.getSpeciesPattern(), sc.getStructure(), CompartmentMode.asSite, 0);
} else {
// CompartmentMode.hide
modified = RbmUtils.toBnglString(sc.getSpeciesPattern(), null, CompartmentMode.hide, 0);
}
modified += " " + fakeSeedSpeciesParam.fakeParameterName;
// we label the seed species with the index
modified = (i + 1) + " " + modified;
// we build the seed species list now, we write it later (in the BEGIN SPECIES block)
seedSpeciesList.add(modified);
fakeParameterList.add(fakeSeedSpeciesParam);
}
// second we produce the bngl file
writer.println(BEGIN_MODEL);
writer.println();
for (ReactionRuleSpec rrs : simulationContext.getReactionContext().getReactionRuleSpecs()) {
if (!rrs.isExcluded()) {
ReactionRule reactionRule = rrs.getReactionRule();
RbmKineticLaw kineticLaw = reactionRule.getKineticLaw();
switch(kineticLaw.getRateLawType()) {
case MassAction:
{
FakeReactionRuleRateParameter fakeRateParameterForward = new FakeReactionRuleRateParameter(reactionRule, RbmKineticLawParameterType.MassActionForwardRate);
LocalParameter origForwardRateParameter = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MassActionForwardRate);
kineticsParameterMap.put(fakeRateParameterForward, origForwardRateParameter);
if (reactionRule.isReversible()) {
FakeReactionRuleRateParameter fakeRateParameterReverse = new FakeReactionRuleRateParameter(reactionRule, RbmKineticLawParameterType.MassActionReverseRate);
LocalParameter origReverseRateParameter = kineticLaw.getLocalParameter(RbmKineticLawParameterType.MassActionReverseRate);
kineticsParameterMap.put(fakeRateParameterReverse, origReverseRateParameter);
}
break;
}
default:
{
throw new RuntimeException("kinetic law type " + kineticLaw.getRateLawType().name() + " not yet implemented");
}
}
}
}
if (compartmentMode == CompartmentMode.show) {
RbmNetworkGenerator.writeCompartments(writer, model, simulationContext);
}
writer.println(BEGIN_PARAMETERS);
// the fake parameters used for reaction rule kinetics
for (FakeReactionRuleRateParameter p : kineticsParameterMap.keySet()) {
writer.println(p.fakeParameterName + "\t\t1");
}
// the fake parameters used at initial values for the seed species
for (FakeSeedSpeciesInitialConditionsParameter s : fakeParameterList) {
writer.println(s.fakeParameterName + "\t\t1");
}
writer.println(END_PARAMETERS);
writer.println();
RbmNetworkGenerator.writeMolecularTypes(writer, model, compartmentMode);
// write modified version of seed species while maintaining the connection between the species context and the real seed species
writer.println(BEGIN_SPECIES);
for (String s : seedSpeciesList) {
writer.println(s);
}
writer.println(END_SPECIES);
writer.println();
RbmNetworkGenerator.writeObservables(writer, rbmModelContainer, compartmentMode);
RbmNetworkGenerator.writeReactions_internal(writer, simulationContext, compartmentMode);
writer.println(END_MODEL);
writer.println();
if (callerClassName.equals(networkTransformerClassName)) {
RbmNetworkGenerator.writeNetworkConstraints(writer, rbmModelContainer, simulationContext, networkGenerationRequirements);
} else if (callerClassName.equals(rulebasedTransformerClassName)) {
writer.println();
writer.println("writeXML()");
}
writer.println();
}
Aggregations