use of cbit.vcell.model.Membrane.MembraneVoltage in project vcell by virtualcell.
the class BioCartoonTool method pasteReactionSteps0.
/**
* pasteReactionSteps0 : does the actual pasting. First called with a cloned model, to track issues. If user still wants to proceed, the paste
* is performed on the original model.
*
* Insert the method's description here.
* Creation date: (5/10/2003 3:55:25 PM)
* @param pasteToModel cbit.vcell.model.Model
* @param pasteToStructure cbit.vcell.model.Structure
* @param bNew boolean
*/
private static final PasteHelper pasteReactionSteps0(HashMap<String, HashMap<ReactionParticipant, Structure>> rxPartMapStructure, Component parent, IssueContext issueContext, ReactionStep[] copyFromRxSteps, Model pasteToModel, Structure pasteToStructure, boolean bNew, /*boolean bUseDBSpecies,*/
UserResolvedRxElements userResolvedRxElements) throws Exception {
HashMap<BioModelEntityObject, BioModelEntityObject> reactionsAndSpeciesContexts = new HashMap<>();
if (copyFromRxSteps == null || copyFromRxSteps.length == 0 || pasteToModel == null || pasteToStructure == null) {
throw new IllegalArgumentException("CartoonTool.pasteReactionSteps Error " + (copyFromRxSteps == null || copyFromRxSteps.length == 0 ? "reactionStepsArr empty " : "") + (pasteToModel == null ? "model is null " : "") + (pasteToStructure == null ? "struct is null " : ""));
}
if (!pasteToModel.contains(pasteToStructure)) {
throw new IllegalArgumentException("CartoonTool.pasteReactionSteps model " + pasteToModel.getName() + " does not contain structure " + pasteToStructure.getName());
}
// Check PasteToModel has preferred targets if set
if (userResolvedRxElements != null) {
for (int i = 0; i < userResolvedRxElements.toSpeciesArr.length; i++) {
if (userResolvedRxElements.toSpeciesArr[i] != null) {
// Structure toNewStruct = userResolvedRxElements.toStructureArr[i];
// SpeciesContext[] toNewSC = pasteToModel.getSpeciesContexts(toNewStruct);
// SpeciesContext[] usersSC = userResolvedRxElements.fromSpeciesContextArr;
// boolean bFound = false;
// for (int j = 0; j < toNewSC.length; j++) {
// boolean structeql = toNewSC[j].getStructure().getName().equals(usersSC[i].getStructure().getName());
// boolean specieseql = toNewSC[j].getSpecies().getCommonName().equals(usersSC[i].getSpecies().getCommonName());
// System.out.println(toNewSC[j]+" "+structeql+" "+usersSC[i]+" "+specieseql);
// if(structeql && specieseql) {
// bFound = true;
// break;
// }
// }
// if(!bFound) {
// throw new Exception("Expecting speciesContext '"+usersSC[i].getSpecies().getCommonName()+"' to exist already in structure "+toNewStruct.getName());
// }
//
// // if(!pasteToModel.contains(userResolvedRxElements.toSpeciesArr[i])){
// // throw new RuntimeException("PasteToModel does not contain preferred Species "+userResolvedRxElements.toSpeciesArr[i]);
// // }
}
// }
if (userResolvedRxElements.toStructureArr[i] != null) {
if (!pasteToModel.contains(userResolvedRxElements.toStructureArr[i])) {
throw new RuntimeException("PasteToModel does not contain preferred Structure " + userResolvedRxElements.toStructureArr[i]);
}
}
}
}
int counter = 0;
Structure currentStruct = pasteToStructure;
String copiedStructName = copyFromRxSteps[counter].getStructure().getName();
StructureTopology structTopology = (copyFromRxSteps[counter].getModel() == null ? pasteToModel.getStructureTopology() : copyFromRxSteps[counter].getModel().getStructureTopology());
IdentityHashMap<Species, Species> speciesHash = new IdentityHashMap<Species, Species>();
IdentityHashMap<SpeciesContext, SpeciesContext> speciesContextHash = new IdentityHashMap<SpeciesContext, SpeciesContext>();
Vector<Issue> issueVector = new Vector<Issue>();
do {
// create a new reaction, instead of cloning the old one; set struc
ReactionStep copyFromReactionStep = copyFromRxSteps[counter];
String newName = copyFromReactionStep.getName();
while (pasteToModel.getReactionStep(newName) != null) {
newName = org.vcell.util.TokenMangler.getNextEnumeratedToken(newName);
}
ReactionStep newReactionStep = null;
if (copyFromReactionStep instanceof SimpleReaction) {
newReactionStep = new SimpleReaction(pasteToModel, currentStruct, newName, copyFromReactionStep.isReversible());
} else if (copyFromReactionStep instanceof FluxReaction && currentStruct instanceof Membrane) {
newReactionStep = new FluxReaction(pasteToModel, (Membrane) currentStruct, null, newName, copyFromReactionStep.isReversible());
}
pasteToModel.addReactionStep(newReactionStep);
reactionsAndSpeciesContexts.put(newReactionStep, copyFromReactionStep);
Structure toRxnStruct = newReactionStep.getStructure();
Structure fromRxnStruct = copyFromReactionStep.getStructure();
if (!fromRxnStruct.getClass().equals(pasteToStructure.getClass())) {
throw new Exception("Cannot copy reaction from " + fromRxnStruct.getTypeName() + " to " + pasteToStructure.getTypeName() + ".");
}
// add appropriate reactionParticipants to newReactionStep.
StructureTopology toStructureTopology = pasteToModel.getStructureTopology();
ReactionParticipant[] copyFromRxParticipantArr = copyFromReactionStep.getReactionParticipants();
if (rxPartMapStructure == null) {
// null during 'issues' trial
rxPartMapStructure = new HashMap<String, HashMap<ReactionParticipant, Structure>>();
}
// }
for (int i = 0; i < copyFromRxParticipantArr.length; i += 1) {
Structure pasteToStruct = currentStruct;
// if(toRxnStruct instanceof Membrane){
pasteToStruct = rxPartMapStructure.get(copyFromReactionStep.getName()).get(copyFromRxParticipantArr[i]);
// if(pasteToStruct == null){
// for(ReactionParticipant myRXPart:rxPartMapStructure.get(copyFromReactionStep.getName()).keySet()){
// if(myRXPart.getSpeciesContext().getName().equals(copyFromRxParticipantArr[i].getSpeciesContext().getName())){
// pasteToStruct = rxPartMapStructure.get(copyFromReactionStep.getName()).get(myRXPart);
// break;
// }
// }
// }
// }
// this adds the speciesContexts and species (if any) to the model)
SpeciesContext newSc = null;
for (int j = 0; j < userResolvedRxElements.fromSpeciesContextArr.length; j++) {
String forceName = userResolvedRxElements.finalNames.get(j).getText();
if (userResolvedRxElements.fromSpeciesContextArr[j] == copyFromRxParticipantArr[i].getSpeciesContext()) {
if (userResolvedRxElements.toSpeciesArr[j] == null) {
newSc = pasteSpecies(parent, copyFromRxParticipantArr[i].getSpecies(), null, pasteToModel, pasteToStruct, bNew, /*bUseDBSpecies,*/
speciesHash, UserResolvedRxElements.getPreferredReactionElement(userResolvedRxElements, copyFromRxParticipantArr[i]));
changeName(userResolvedRxElements, newSc, j, pasteToModel, forceName);
reactionsAndSpeciesContexts.put(newSc, copyFromRxParticipantArr[i].getSpeciesContext());
} else {
if (forceName != null && forceName.length() > 0 && pasteToModel.getSpeciesContext(forceName) != null) {
if (pasteToModel.getSpeciesContext(forceName).getStructure().getName() == userResolvedRxElements.toStructureArr[j].getName()) {
throw new Exception("Paste custom name error:\nSpeciesContext name '" + forceName + "' in structure '" + userResolvedRxElements.toStructureArr[j].getName() + "' already used");
}
}
newSc = pasteToModel.getSpeciesContext(userResolvedRxElements.toSpeciesArr[j], userResolvedRxElements.toStructureArr[j]);
if (newSc == null) {
newSc = pasteSpecies(parent, copyFromRxParticipantArr[i].getSpecies(), null, pasteToModel, pasteToStruct, bNew, /*bUseDBSpecies,*/
speciesHash, UserResolvedRxElements.getPreferredReactionElement(userResolvedRxElements, copyFromRxParticipantArr[i]));
changeName(userResolvedRxElements, newSc, j, pasteToModel, forceName);
} else if (forceName != null && forceName.length() > 0) {
throw new Exception("Paste custom name error:\nCan't rename existing speciesContext '" + newSc.getName() + "' in structure '" + newSc.getStructure().getName() + "' to '" + forceName + "'");
}
reactionsAndSpeciesContexts.put(newSc, copyFromRxParticipantArr[i].getSpeciesContext());
// String rootSC = ReactionCartoonTool.speciesContextRootFinder(copyFromRxParticipantArr[i].getSpeciesContext());
// SpeciesContext[] matchSC = pasteToModel.getSpeciesContexts();
// for(int k=0;matchSC != null && k<matchSC.length;k++){
// String matchRoot = ReactionCartoonTool.speciesContextRootFinder(matchSC[k]);
// if(matchRoot != null && matchRoot.equals(rootSC) && matchSC[k].getStructure().getName().equals(pasteToStruct.getName())){
// newSc = matchSC[k];
// reactionsAndSpeciesContexts.put(newSc, matchSC[k]);
// break;
// }
// }
}
if (newSc == null) {
throw new Exception("Couldn't assign speciesContext='" + copyFromRxParticipantArr[i].getSpeciesContext().getName() + "' to species='" + userResolvedRxElements.toSpeciesArr[j].getCommonName() + "' in structure='" + userResolvedRxElements.toStructureArr[j].getName() + "', species/structure not exist");
}
}
}
// String rootSC = ReactionCartoonTool.speciesContextRootFinder(copyFromRxParticipantArr[i].getSpeciesContext());
// SpeciesContext newSc = null;
// // if(!bNew) {
// SpeciesContext[] matchSC = pasteToModel.getSpeciesContexts();
// for(int j=0;matchSC != null && j<matchSC.length;j++){
// String matchRoot = ReactionCartoonTool.speciesContextRootFinder(matchSC[j]);
// if(matchRoot != null && matchRoot.equals(rootSC) && matchSC[j].getStructure().getName().equals(pasteToStruct.getName())){
// newSc = matchSC[j];
// reactionsAndSpeciesContexts.put(newSc, matchSC[j]);
// break;
// }
// }
// // }
//
// if(newSc == null){
// newSc = pasteSpecies(parent, copyFromRxParticipantArr[i].getSpecies(),rootSC,pasteToModel,pasteToStruct,bNew, /*bUseDBSpecies,*/speciesHash,
// UserResolvedRxElements.getPreferredReactionElement(userResolvedRxElements,copyFromRxParticipantArr[i]));
// reactionsAndSpeciesContexts.put(newSc,copyFromRxParticipantArr[i].getSpeciesContext());
// }
// record the old-new speciesContexts (reactionparticipants) in the IdHashMap, this is useful, esp for 'Paste new', while replacing proxyparams.
SpeciesContext oldSc = copyFromRxParticipantArr[i].getSpeciesContext();
if (speciesContextHash.get(oldSc) == null) {
speciesContextHash.put(oldSc, newSc);
}
if (copyFromRxParticipantArr[i] instanceof Reactant) {
newReactionStep.addReactionParticipant(new Reactant(null, newReactionStep, newSc, copyFromRxParticipantArr[i].getStoichiometry()));
} else if (copyFromRxParticipantArr[i] instanceof Product) {
newReactionStep.addReactionParticipant(new Product(null, newReactionStep, newSc, copyFromRxParticipantArr[i].getStoichiometry()));
} else if (copyFromRxParticipantArr[i] instanceof Catalyst) {
newReactionStep.addCatalyst(newSc);
}
}
// // If 'newReactionStep' is a fluxRxn, set its fluxCarrier
// if (newReactionStep instanceof FluxReaction) {
// if (fluxCarrierSp != null) {
// ((FluxReaction)newReactionStep).setFluxCarrier(fluxCarrierSp, pasteToModel);
// } else {
// throw new RuntimeException("Could not set FluxCarrier species for the flux reaction to be pasted");
// }
// }
// For each kinetic parameter expression for new kinetics, replace the proxyParams from old kinetics with proxyParams in new kinetics
// i.e., if the proxyParams are speciesContexts, replace with corresponding speciesContext in newReactionStep;
// if the proxyParams are structureSizes or MembraneVoltages, replace with corresponding structure quantity in newReactionStep
Kinetics oldKinetics = copyFromReactionStep.getKinetics();
KineticsParameter[] oldKps = oldKinetics.getKineticsParameters();
KineticsProxyParameter[] oldKprps = oldKinetics.getProxyParameters();
Hashtable<String, Expression> paramExprHash = new Hashtable<String, Expression>();
for (int i = 0; oldKps != null && i < oldKps.length; i++) {
Expression newExpression = new Expression(oldKps[i].getExpression());
for (int j = 0; oldKprps != null && j < oldKprps.length; j++) {
// check if kinetic proxy parameter is in kinetic parameter expression
if (newExpression.hasSymbol(oldKprps[j].getName())) {
SymbolTableEntry ste = oldKprps[j].getTarget();
Model pasteFromModel = copyFromReactionStep.getModel();
if (ste instanceof SpeciesContext) {
// if newRxnStruct is a feature/membrane, get matching spContexts from old reaction and replace them in new rate expr.
SpeciesContext oldSC = (SpeciesContext) ste;
SpeciesContext newSC = speciesContextHash.get(oldSC);
if (newSC == null) {
// check if oldSc is present in paste-model; if not, add it.
if (!pasteToModel.equals(pasteFromModel)) {
if (pasteToModel.getSpeciesContext(oldSC.getName()) == null) {
// if paste-model has oldSc struct, paste it there,
Structure newSCStruct = pasteToModel.getStructure(oldSC.getStructure().getName());
if (newSCStruct != null) {
newSC = pasteSpecies(parent, oldSC.getSpecies(), null, pasteToModel, newSCStruct, bNew, /*bUseDBSpecies,*/
speciesHash, UserResolvedRxElements.getPreferredReactionElement(userResolvedRxElements, oldSC));
speciesContextHash.put(oldSC, newSC);
} else {
// oldStruct wasn't found in paste-model, paste it in newRxnStruct and add warning to issues list
newSC = pasteSpecies(parent, oldSC.getSpecies(), null, pasteToModel, toRxnStruct, bNew, /*bUseDBSpecies,*/
speciesHash, UserResolvedRxElements.getPreferredReactionElement(userResolvedRxElements, oldSC));
speciesContextHash.put(oldSC, newSC);
Issue issue = new Issue(oldSC, issueContext, IssueCategory.CopyPaste, "SpeciesContext '" + oldSC.getSpecies().getCommonName() + "' was not found in compartment '" + oldSC.getStructure().getName() + "' in the model; the species was added to the compartment '" + toRxnStruct.getName() + "' where the reaction was pasted.", Issue.SEVERITY_WARNING);
issueVector.add(issue);
}
}
}
// if models are the same and newSc is null, then oldSc is not a rxnParticipant. Leave it as is in the expr.
}
if (newSC != null) {
reactionsAndSpeciesContexts.put(newSC, oldSC);
newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(newSC.getName()));
}
// SpeciesContext sc = null;
// Species newSp = model.getSpecies(oldSc.getSpecies().getCommonName());
// if (oldSc.getStructure() == (oldRxnStruct)) {
// sc = model.getSpeciesContext(newSp, newRxnStruct);
// } else {
// if (newRxnStruct instanceof Membrane) {
// // for a membrane, we need to make sure that inside-outside spContexts used are appropriately replaced.
// if (oldSc.getStructure() == ((Membrane)oldRxnStruct).getOutsideFeature()) {
// // old speciesContext is outside (old) membrane, new spContext should be outside new membrane
// sc = model.getSpeciesContext(newSp, ((Membrane)newRxnStruct).getOutsideFeature());
// } else if (oldSc.getStructure() == ((Membrane)oldRxnStruct).getInsideFeature()) {
// // old speciesContext is inside (old) membrane, new spContext should be inside new membrane
// sc = model.getSpeciesContext(newSp, ((Membrane)newRxnStruct).getInsideFeature());
// }
// }
// }
// if (sc != null) {
// newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(sc.getName()));
// }
} else if (ste instanceof StructureSize) {
Structure str = ((StructureSize) ste).getStructure();
// if the structure size used is same as the structure in which the reaction is present, change the structSize to appropriate new struct
if (str.compareEqual(fromRxnStruct)) {
newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(toRxnStruct.getStructureSize().getName()));
} else {
if (fromRxnStruct instanceof Membrane) {
if (str.equals(structTopology.getOutsideFeature((Membrane) fromRxnStruct))) {
newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(structTopology.getOutsideFeature((Membrane) toRxnStruct).getStructureSize().getName()));
} else if (str.equals(structTopology.getInsideFeature((Membrane) fromRxnStruct))) {
newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(structTopology.getInsideFeature((Membrane) toRxnStruct).getStructureSize().getName()));
}
}
}
} else if (ste instanceof MembraneVoltage) {
Membrane membr = ((MembraneVoltage) ste).getMembrane();
// if the MembraneVoltage used is same as that of the membrane in which the reaction is present, change the MemVoltage
if ((fromRxnStruct instanceof Membrane) && (membr.compareEqual(fromRxnStruct))) {
newExpression.substituteInPlace(new Expression(ste.getName()), new Expression(((Membrane) toRxnStruct).getMembraneVoltage().getName()));
}
} else if (ste instanceof ModelParameter) {
// see if model has this global parameter (if rxn is being pasted into another model, it won't)
if (!pasteToModel.equals(pasteFromModel)) {
ModelParameter oldMp = (ModelParameter) ste;
ModelParameter mp = pasteToModel.getModelParameter(oldMp.getName());
boolean bNonNumeric = false;
String newMpName = oldMp.getName();
if (mp != null) {
// new model has a model parameter with same name - are they the same param?
if (!mp.getExpression().equals(oldMp.getExpression())) {
// no, they are not the same param, so mangle the 'ste' name and add as global in the other model
while (pasteToModel.getModelParameter(newMpName) != null) {
newMpName = TokenMangler.getNextEnumeratedToken(newMpName);
}
// if expression if numeric, add it as such. If not, set it to 0.0 and add it as global
Expression exp = oldMp.getExpression();
if (!exp.flatten().isNumeric()) {
exp = new Expression(0.0);
bNonNumeric = true;
}
ModelParameter newMp = pasteToModel.new ModelParameter(newMpName, exp, Model.ROLE_UserDefined, oldMp.getUnitDefinition());
String annotation = "Copied from model : " + pasteFromModel.getNameScope();
newMp.setModelParameterAnnotation(annotation);
pasteToModel.addModelParameter(newMp);
// if global param name had to be changed, make sure newExpr is updated as well.
if (!newMpName.equals(oldMp.getName())) {
newExpression.substituteInPlace(new Expression(oldMp.getName()), new Expression(newMpName));
}
}
} else {
// no global param with same name was found in other model, so add it to other model.
// if expression if numeric, add it as such. If not, set it to 0.0 and add it as global
Expression exp = oldMp.getExpression();
if (!exp.flatten().isNumeric()) {
exp = new Expression(0.0);
bNonNumeric = true;
}
ModelParameter newMp = pasteToModel.new ModelParameter(newMpName, exp, Model.ROLE_UserDefined, oldMp.getUnitDefinition());
String annotation = "Copied from model : " + pasteFromModel.getNameScope();
newMp.setModelParameterAnnotation(annotation);
pasteToModel.addModelParameter(newMp);
}
// if a non-numeric parameter was encountered in the old model, it was added as a numeric (0.0), warn user of change.
if (bNonNumeric) {
Issue issue = new Issue(oldMp, issueContext, IssueCategory.CopyPaste, "Global parameter '" + oldMp.getName() + "' was non-numeric; it has been added " + "as global parameter '" + newMpName + "' in the new model with value = 0.0. " + "Please update its value, if required, before using it.", Issue.SEVERITY_WARNING);
issueVector.add(issue);
}
}
}
}
// end - if newExpr.hasSymbol(ProxyParam)
}
// now if store <param names, new expression> in hashTable
if (paramExprHash.get(oldKps[i].getName()) == null) {
paramExprHash.put(oldKps[i].getName(), newExpression);
}
}
// end for - oldKps (old kinetic parameters)
// use this new expression to generate 'vcml' for the (new) kinetics (easier way to transfer all kinetic parameters)
String newKineticsStr = oldKinetics.writeTokensWithReplacingProxyParams(paramExprHash);
// convert the kinetics 'vcml' to tokens.
CommentStringTokenizer kineticsTokens = new CommentStringTokenizer(newKineticsStr);
// skip the first token;
kineticsTokens.nextToken();
// second token is the kinetic type; use this to create a dummy kinetics
String kineticType = kineticsTokens.nextToken();
Kinetics newkinetics = KineticsDescription.fromVCMLKineticsName(kineticType).createKinetics(newReactionStep);
// use the remaining tokens to construct the new kinetics
newkinetics.fromTokens(newKineticsStr);
// bind newkinetics to newReactionStep and add it to newReactionStep
newkinetics.bind(newReactionStep);
newReactionStep.setKinetics(newkinetics);
counter += 1;
if (counter == copyFromRxSteps.length) {
break;
}
if (!copiedStructName.equals(fromRxnStruct.getName())) {
if (currentStruct instanceof Feature) {
currentStruct = structTopology.getMembrane((Feature) currentStruct);
} else if (currentStruct instanceof Membrane) {
currentStruct = structTopology.getInsideFeature((Membrane) currentStruct);
}
}
copiedStructName = fromRxnStruct.getName();
} while (true);
return new PasteHelper(issueVector, rxPartMapStructure, reactionsAndSpeciesContexts);
}
use of cbit.vcell.model.Membrane.MembraneVoltage in project vcell by virtualcell.
the class TestingFrameworkWindowManager method addTestCases.
/**
* Insert the method's description here.
* Creation date: (4/10/2003 11:27:32 AM)
* @param testCase cbit.vcell.numericstestingframework.TestCase
*/
public String addTestCases(final TestSuiteInfoNew tsInfo, final TestCaseNew[] testCaseArray, int regrRefFlag, ClientTaskStatusSupport pp) {
if (tsInfo == null) {
throw new IllegalArgumentException("TestSuiteInfo cannot be null");
}
if (testCaseArray == null || testCaseArray.length == 0) {
throw new IllegalArgumentException("TestCases cannot be null / empty");
}
// make modifiable list
List<TestCaseNew> testCases = new ArrayList<>(Arrays.asList(testCaseArray));
StringBuffer errors = new StringBuffer();
// When a testCase (mathmodel/biomodel) is added to a testSuite, a new version of the mathModel/biomodel should be created.
// Also, the simulations in the original mathmodel/biomodel should be rid of their parent simulation reference.
pp.setMessage("Getting testSuite");
pp.setProgress(1);
TestSuiteNew testSuite = null;
try {
testSuite = getRequestManager().getDocumentManager().getTestSuite(tsInfo.getTSKey());
} catch (Throwable e) {
throw new RuntimeException("couldn't get test suite " + tsInfo.getTSID() + "\n" + e.getClass().getName() + " mesg=" + e.getMessage() + "\n");
}
if (testSuite != null && testSuite.getTSInfoNew().isLocked()) {
throw new RuntimeException("Cannot addTestCases to locked table");
}
if (testSuite != null) {
// Saving BioModels
TestCaseNew[] existingTestCases = testSuite.getTestCases();
java.util.HashMap<KeyValue, BioModel> bioModelHashMap = new java.util.HashMap<KeyValue, BioModel>();
// if(existingTestCases != null){
// Find BioModels, Using the same BM reference for sibling Applications
int pcounter = 0;
// use iterator to allow removal of test case from collection if exception
Iterator<TestCaseNew> iter = testCases.iterator();
while (iter.hasNext()) {
TestCaseNew testCase = iter.next();
pp.setProgress(Math.max(1, ((int) ((pcounter++ / (double) (testCases.size() * 3)) * 100))));
pp.setMessage("Checking " + testCase.getVersion().getName());
try {
if (testCase instanceof TestCaseNewBioModel) {
TestCaseNewBioModel bioTestCase = (TestCaseNewBioModel) testCase;
//
if (bioModelHashMap.get(bioTestCase.getBioModelInfo().getVersion().getVersionKey()) == null) {
pp.setMessage("Getting BM " + testCase.getVersion().getName());
BioModel bioModel = getRequestManager().getDocumentManager().getBioModel(bioTestCase.getBioModelInfo().getVersion().getVersionKey());
if (!bioModel.getVersion().getOwner().equals(getRequestManager().getDocumentManager().getUser())) {
throw new Exception("BioModel does not belong to VCELLTESTACCOUNT, cannot proceed with test!");
}
//
// if biomodel already exists in same testsuite, then use this BioModel edition
//
BioModel newBioModel = null;
if (existingTestCases != null) {
for (int j = 0; newBioModel == null && j < existingTestCases.length; j++) {
if (existingTestCases[j] instanceof TestCaseNewBioModel) {
TestCaseNewBioModel existingTestCaseBioModel = (TestCaseNewBioModel) existingTestCases[j];
//
if (existingTestCaseBioModel.getBioModelInfo().getVersion().getBranchID().equals(bioTestCase.getBioModelInfo().getVersion().getBranchID())) {
//
if (existingTestCaseBioModel.getBioModelInfo().getVersion().getVersionKey().equals(bioTestCase.getBioModelInfo().getVersion().getVersionKey())) {
//
// same, store this "unchanged" in bioModelHashMap
//
newBioModel = bioModel;
} else {
//
throw new Exception("can't add new test case using (" + bioTestCase.getBioModelInfo().getVersion().getName() + " " + bioTestCase.getBioModelInfo().getVersion().getDate() + ")\n" + "a test case already exists with different edition of same BioModel dated " + existingTestCaseBioModel.getBioModelInfo().getVersion().getDate());
}
}
}
}
}
if (newBioModel == null) {
pp.setMessage("Saving BM " + testCase.getVersion().getName());
//
// some older models have membrane voltage variable names which are not unique
// (e.g. membranes 'pm' and 'nm' both have membrane voltage variables named 'Voltage_Membrane0')
//
// if this is the case, we will try to repair the conflict (for math testing purposes only) by renaming the voltage variables to their default values.
//
// Ordinarily, the conflict will be identified as an "Error" issue and the user will be prompted to repair before saving or math generation.
//
bioModel.refreshDependencies();
boolean bFoundIdentifierConflictUponLoading = hasDuplicateIdentifiers(bioModel);
if (bFoundIdentifierConflictUponLoading) {
//
// look for two MembraneVoltage instances with same variable name, rename all
//
HashSet<String> membraneVoltageVarNames = new HashSet<String>();
ArrayList<MembraneVoltage> membraneVoltageVars = new ArrayList<MembraneVoltage>();
for (Structure struct : bioModel.getModel().getStructures()) {
if (struct instanceof Membrane) {
MembraneVoltage membraneVoltage = ((Membrane) struct).getMembraneVoltage();
if (membraneVoltage != null) {
membraneVoltageVars.add(membraneVoltage);
membraneVoltageVarNames.add(membraneVoltage.getName());
}
}
}
if (membraneVoltageVars.size() != membraneVoltageVarNames.size()) {
// rename them all to the default names
for (MembraneVoltage memVoltage : membraneVoltageVars) {
memVoltage.setName(Membrane.getDefaultMembraneVoltageName(memVoltage.getMembrane().getName()));
}
}
}
SimulationContext[] simContexts = bioModel.getSimulationContexts();
for (int j = 0; j < simContexts.length; j++) {
simContexts[j].clearVersion();
GeometrySurfaceDescription gsd = simContexts[j].getGeometry().getGeometrySurfaceDescription();
if (gsd != null) {
GeometricRegion[] grArr = gsd.getGeometricRegions();
if (grArr == null) {
gsd.updateAll();
}
}
MathMapping mathMapping = simContexts[j].createNewMathMapping();
// for older models that do not have absolute compartment sizes set, but have relative sizes (SVR/VF); or if there is only one compartment with size not set,
// compute absolute compartment sizes using relative sizes and assuming a default value of '1' for one of the compartments.
// Otherwise, the math generation will fail, since for the relaxed topology (VCell 5.3 and later) absolute compartment sizes are required.
GeometryContext gc = simContexts[j].getGeometryContext();
if (simContexts[j].getGeometry().getDimension() == 0 && ((gc.isAllSizeSpecifiedNull() && !gc.isAllVolFracAndSurfVolSpecifiedNull()) || (gc.getModel().getStructures().length == 1 && gc.isAllSizeSpecifiedNull()))) {
// choose the first structure in model and set its size to '1'.
Structure struct = simContexts[j].getModel().getStructure(0);
double structSize = 1.0;
StructureSizeSolver.updateAbsoluteStructureSizes(simContexts[j], struct, structSize, struct.getStructureSize().getUnitDefinition());
}
simContexts[j].setMathDescription(mathMapping.getMathDescription());
}
Simulation[] sims = bioModel.getSimulations();
String[] simNames = new String[sims.length];
for (int j = 0; j < sims.length; j++) {
// prevents parent simulation (from the original mathmodel) reference connection
// Otherwise it will refer to data from previous (parent) simulation.
sims[j].clearVersion();
simNames[j] = sims[j].getName();
// if(sims[j].getSolverTaskDescription().getSolverDescription().equals(SolverDescription.FiniteVolume)){
// sims[j].getSolverTaskDescription().setSolverDescription(SolverDescription.FiniteVolumeStandalone);
// }
}
newBioModel = getRequestManager().getDocumentManager().save(bioModel, simNames);
}
bioModelHashMap.put(bioTestCase.getBioModelInfo().getVersion().getVersionKey(), newBioModel);
}
}
} catch (Throwable e) {
String identifier = testCase.getVersion() != null ? "Name=" + testCase.getVersion().getName() : "TCKey=" + testCase.getTCKey();
if (lg.isInfoEnabled()) {
lg.info(identifier, e);
}
errors.append("Error collecting BioModel for TestCase " + identifier + '\n' + e.getClass().getName() + " " + e.getMessage() + '\n');
// remove to avoid further processing attempts
iter.remove();
}
}
// }
// then process each BioModelTestCase individually
// if(bioModelHashMap != null){
pcounter = 0;
for (TestCaseNew testCase : testCases) {
pp.setProgress(Math.max(1, ((int) ((pcounter++ / (double) (testCases.size() * 3)) * 100))));
pp.setMessage("Checking " + testCase.getVersion().getName());
try {
AddTestCasesOP testCaseOP = null;
if (testCase instanceof TestCaseNewBioModel) {
pp.setMessage("Processing BM " + testCase.getVersion().getName());
TestCaseNewBioModel bioTestCase = (TestCaseNewBioModel) testCase;
BioModel newBioModel = (BioModel) bioModelHashMap.get(bioTestCase.getBioModelInfo().getVersion().getVersionKey());
if (newBioModel == null) {
throw new Exception("BioModel not found");
}
SimulationContext simContext = null;
for (int j = 0; j < newBioModel.getSimulationContexts().length; j++) {
if (newBioModel.getSimulationContext(j).getName().equals(bioTestCase.getSimContextName())) {
simContext = newBioModel.getSimulationContext(j);
}
}
Simulation[] newSimulations = simContext.getSimulations();
AddTestCriteriaOPBioModel[] testCriteriaOPs = new AddTestCriteriaOPBioModel[newSimulations.length];
for (int j = 0; j < newSimulations.length; j++) {
TestCriteriaNewBioModel tcritOrigForSimName = null;
for (int k = 0; bioTestCase.getTestCriterias() != null && k < bioTestCase.getTestCriterias().length; k += 1) {
if (bioTestCase.getTestCriterias()[k].getSimInfo().getName().equals(newSimulations[j].getName())) {
tcritOrigForSimName = (TestCriteriaNewBioModel) bioTestCase.getTestCriterias()[k];
break;
}
}
KeyValue regressionBioModelKey = null;
KeyValue regressionBioModelSimKey = null;
if (bioTestCase.getType().equals(TestCaseNew.REGRESSION)) {
if (regrRefFlag == TestingFrameworkWindowManager.COPY_REGRREF) {
regressionBioModelKey = (tcritOrigForSimName != null && tcritOrigForSimName.getRegressionBioModelInfo() != null ? tcritOrigForSimName.getRegressionBioModelInfo().getVersion().getVersionKey() : null);
regressionBioModelSimKey = (tcritOrigForSimName != null && tcritOrigForSimName.getRegressionSimInfo() != null ? tcritOrigForSimName.getRegressionSimInfo().getVersion().getVersionKey() : null);
} else if (regrRefFlag == TestingFrameworkWindowManager.ASSIGNORIGINAL_REGRREF) {
regressionBioModelKey = (tcritOrigForSimName != null ? bioTestCase.getBioModelInfo().getVersion().getVersionKey() : null);
regressionBioModelSimKey = (tcritOrigForSimName != null ? tcritOrigForSimName.getSimInfo().getVersion().getVersionKey() : null);
} else if (regrRefFlag == TestingFrameworkWindowManager.ASSIGNNEW_REGRREF) {
regressionBioModelKey = newBioModel.getVersion().getVersionKey();
regressionBioModelSimKey = newSimulations[j].getVersion().getVersionKey();
} else {
throw new IllegalArgumentException(this.getClass().getName() + ".addTestCases(...) BIOMODEL Unknown Regression Operation Flag");
}
}
testCriteriaOPs[j] = new AddTestCriteriaOPBioModel(testCase.getTCKey(), newSimulations[j].getVersion().getVersionKey(), regressionBioModelKey, regressionBioModelSimKey, (tcritOrigForSimName != null ? tcritOrigForSimName.getMaxAbsError() : new Double(1e-16)), (tcritOrigForSimName != null ? tcritOrigForSimName.getMaxRelError() : new Double(1e-9)), null);
}
testCaseOP = new AddTestCasesOPBioModel(new BigDecimal(tsInfo.getTSKey().toString()), newBioModel.getVersion().getVersionKey(), simContext.getKey(), bioTestCase.getType(), bioTestCase.getAnnotation(), testCriteriaOPs);
getRequestManager().getDocumentManager().doTestSuiteOP(testCaseOP);
}
} catch (Throwable e) {
errors.append("Error processing Biomodel for TestCase " + (testCase.getVersion() != null ? "Name=" + testCase.getVersion().getName() : "TCKey=" + testCase.getTCKey()) + "\n" + e.getClass().getName() + " " + e.getMessage() + "\n");
}
}
// }
// Process MathModels
pcounter = 0;
for (TestCaseNew testCase : testCases) {
pp.setProgress(Math.max(1, ((int) ((pcounter++ / (double) (testCases.size() * 3)) * 100))));
pp.setMessage("Checking " + testCase.getVersion().getName());
try {
AddTestCasesOP testCaseOP = null;
if (testCase instanceof TestCaseNewMathModel) {
TestCaseNewMathModel mathTestCase = (TestCaseNewMathModel) testCase;
pp.setMessage("Getting MathModel " + testCase.getVersion().getName());
MathModel mathModel = getRequestManager().getDocumentManager().getMathModel(mathTestCase.getMathModelInfo().getVersion().getVersionKey());
if (!mathModel.getVersion().getOwner().equals(getRequestManager().getDocumentManager().getUser())) {
throw new Exception("MathModel does not belong to VCELLTESTACCOUNT, cannot proceed with test!");
}
Simulation[] sims = mathModel.getSimulations();
String[] simNames = new String[sims.length];
for (int j = 0; j < sims.length; j++) {
// prevents parent simulation (from the original mathmodel) reference connection
// Otherwise it will refer to data from previous (parent) simulation.
sims[j].clearVersion();
simNames[j] = sims[j].getName();
// if(sims[j].getSolverTaskDescription().getSolverDescription().equals(SolverDescription.FiniteVolume)){
// sims[j].getSolverTaskDescription().setSolverDescription(SolverDescription.FiniteVolumeStandalone);
// }
}
pp.setMessage("Saving MathModel " + testCase.getVersion().getName());
MathModel newMathModel = getRequestManager().getDocumentManager().save(mathModel, simNames);
Simulation[] newSimulations = newMathModel.getSimulations();
AddTestCriteriaOPMathModel[] testCriteriaOPs = new AddTestCriteriaOPMathModel[newSimulations.length];
for (int j = 0; j < newSimulations.length; j++) {
TestCriteriaNewMathModel tcritOrigForSimName = null;
for (int k = 0; mathTestCase.getTestCriterias() != null && k < mathTestCase.getTestCriterias().length; k += 1) {
if (mathTestCase.getTestCriterias()[k].getSimInfo().getName().equals(newSimulations[j].getName())) {
tcritOrigForSimName = (TestCriteriaNewMathModel) mathTestCase.getTestCriterias()[k];
break;
}
}
KeyValue regressionMathModelKey = null;
KeyValue regressionMathModelSimKey = null;
if (mathTestCase.getType().equals(TestCaseNew.REGRESSION)) {
if (regrRefFlag == TestingFrameworkWindowManager.COPY_REGRREF) {
regressionMathModelKey = (tcritOrigForSimName != null && tcritOrigForSimName.getRegressionMathModelInfo() != null ? tcritOrigForSimName.getRegressionMathModelInfo().getVersion().getVersionKey() : null);
regressionMathModelSimKey = (tcritOrigForSimName != null && tcritOrigForSimName.getRegressionSimInfo() != null ? tcritOrigForSimName.getRegressionSimInfo().getVersion().getVersionKey() : null);
} else if (regrRefFlag == TestingFrameworkWindowManager.ASSIGNORIGINAL_REGRREF) {
regressionMathModelKey = (tcritOrigForSimName != null ? mathTestCase.getMathModelInfo().getVersion().getVersionKey() : null);
regressionMathModelSimKey = (tcritOrigForSimName != null ? tcritOrigForSimName.getSimInfo().getVersion().getVersionKey() : null);
} else if (regrRefFlag == TestingFrameworkWindowManager.ASSIGNNEW_REGRREF) {
regressionMathModelKey = newMathModel.getVersion().getVersionKey();
regressionMathModelSimKey = newSimulations[j].getVersion().getVersionKey();
} else {
throw new IllegalArgumentException(this.getClass().getName() + ".addTestCases(...) MATHMODEL Unknown Regression Operation Flag");
}
}
testCriteriaOPs[j] = new AddTestCriteriaOPMathModel(testCase.getTCKey(), newSimulations[j].getVersion().getVersionKey(), regressionMathModelKey, regressionMathModelSimKey, (tcritOrigForSimName != null ? tcritOrigForSimName.getMaxAbsError() : new Double(1e-16)), (tcritOrigForSimName != null ? tcritOrigForSimName.getMaxRelError() : new Double(1e-9)), null);
}
testCaseOP = new AddTestCasesOPMathModel(new BigDecimal(tsInfo.getTSKey().toString()), newMathModel.getVersion().getVersionKey(), mathTestCase.getType(), mathTestCase.getAnnotation(), testCriteriaOPs);
getRequestManager().getDocumentManager().doTestSuiteOP(testCaseOP);
}
} catch (Throwable e) {
errors.append("Error processing MathModel for TestCase " + (testCase.getVersion() != null ? "Name=" + testCase.getVersion().getName() : "TCKey=" + testCase.getTCKey()) + "\n" + e.getClass().getName() + " " + e.getMessage() + "\n");
}
}
}
if (errors.length() > 0) {
return errors.toString();
}
return null;
}
use of cbit.vcell.model.Membrane.MembraneVoltage in project vcell by virtualcell.
the class StructurePropertiesPanel method updateInterface.
/**
* Comment
*/
private void updateInterface() {
boolean bNonNullStructure = structure != null && fieldModel != null;
nameTextField.setEditable(bNonNullStructure);
// annotationTextArea.setEditable(bNonNullStructure);
boolean bMembrane = bNonNullStructure && structure instanceof Membrane;
voltageLabel.setVisible(bMembrane);
voltageTextField.setVisible(bMembrane);
electrophysiologyLabel.setVisible(bMembrane);
positiveFeatureLabel.setVisible(bMembrane);
positiveFeatureComboBox.setVisible(bMembrane);
negativeFeatureLabel.setVisible(bMembrane);
negativeFeatureComboBox.setVisible(bMembrane);
electrophysiologyExplanationLabel.setVisible(bMembrane);
if (bNonNullStructure) {
nameTextField.setText(structure.getName());
// annotationTextArea.setText(fieldModel.getVcMetaData().getFreeTextAnnotation(structure));
StructureSize structureSize = structure.getStructureSize();
sizeTextField.setText(structureSize.getName() + " [" + structureSize.getUnitDefinition().getSymbolUnicode() + "]");
if (bMembrane) {
Membrane membrane = (Membrane) structure;
MembraneVoltage memVoltage = membrane.getMembraneVoltage();
voltageTextField.setText(memVoltage.getName() + " [" + memVoltage.getUnitDefinition().getSymbolUnicode() + "]");
// if membrane has +ve/-ve feature set, set the comboBox with that selection.
ElectricalTopology electricalTopology = fieldModel.getElectricalTopology();
Feature positiveFeature = electricalTopology.getPositiveFeature(membrane);
if (positiveFeature != null) {
positiveFeatureComboBox.setSelectedItem(positiveFeature.getName());
}
Feature negativeFeature = electricalTopology.getNegativeFeature(membrane);
if (negativeFeature != null) {
negativeFeatureComboBox.setSelectedItem(negativeFeature.getName());
}
this.electrophysiologyExplanationLabel.setText(getExplanationText());
}
} else {
// annotationTextArea.setText(null);
nameTextField.setText(null);
sizeTextField.setText(null);
voltageTextField.setText(null);
}
}
use of cbit.vcell.model.Membrane.MembraneVoltage 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.Membrane.MembraneVoltage in project vcell by virtualcell.
the class ReactionRule method getAutoCompleteSymbolFilter.
public AutoCompleteSymbolFilter getAutoCompleteSymbolFilter() {
AutoCompleteSymbolFilter stef = new AutoCompleteSymbolFilter() {
public boolean accept(SymbolTableEntry ste) {
if (ste instanceof StructureSize) {
if (((StructureSize) ste).getStructure() != structure) {
return false;
}
} else {
if (structure instanceof Membrane) {
Membrane membrane = (Membrane) structure;
StructureTopology structTopology = getModel().getStructureTopology();
if (ste instanceof SpeciesContext) {
Structure entryStructure = ((SpeciesContext) ste).getStructure();
if (entryStructure != membrane && entryStructure != structTopology.getInsideFeature(membrane) && entryStructure != structTopology.getOutsideFeature(membrane)) {
return false;
}
} else if (ste instanceof MembraneVoltage) {
if (((MembraneVoltage) ste).getMembrane() != membrane) {
return false;
}
}
} else {
if (ste instanceof SpeciesContext) {
Structure entryStructure = ((SpeciesContext) ste).getStructure();
if (entryStructure != structure) {
return false;
}
} else if (ste instanceof MembraneVoltage) {
return false;
}
}
}
return true;
}
public boolean acceptFunction(String funcName) {
return true;
}
};
return stef;
}
Aggregations