use of org.vcell.util.document.KeyValue in project vcell by virtualcell.
the class MathVerifier method scanBioModels.
/**
* Insert the method's description here.
* Creation date: (2/2/01 3:40:29 PM)
*/
public void scanBioModels(boolean bUpdateDatabase, KeyValue[] bioModelKeys) throws MathException, MappingException, SQLException, DataAccessException, ModelException, ExpressionException {
java.util.Calendar calendar = java.util.GregorianCalendar.getInstance();
// calendar.set(2002,java.util.Calendar.MAY,7+1);
calendar.set(2002, java.util.Calendar.JULY, 1);
final java.util.Date fluxCorrectionOrDisablingBugFixDate = calendar.getTime();
// calendar.set(2001,java.util.Calendar.JUNE,13+1);
calendar.set(2002, java.util.Calendar.JANUARY, 1);
final java.util.Date totalVolumeCorrectionFixDate = calendar.getTime();
User user = new User(PropertyLoader.ADMINISTRATOR_ACCOUNT, new KeyValue(PropertyLoader.ADMINISTRATOR_ID));
for (int i = 0; i < bioModelKeys.length; i++) {
BioModelInfo bioModelInfo = dbServerImpl.getBioModelInfo(user, bioModelKeys[i]);
if (lg.isTraceEnabled())
lg.trace("Testing bioModel with key '" + bioModelKeys[i] + "'");
java.sql.Connection con = null;
java.sql.Statement stmt = null;
try {
//
// read in the BioModel from the database
//
BigString bioModelXML = dbServerImpl.getBioModelXML(user, bioModelInfo.getVersion().getVersionKey());
BioModel bioModelFromDB = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
BioModel bioModelNewMath = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
bioModelFromDB.refreshDependencies();
bioModelNewMath.refreshDependencies();
//
// get all Simulations for this model
//
Simulation[] modelSimsFromDB = bioModelFromDB.getSimulations();
//
// for each application, recompute mathDescription, and verify it is equivalent
// then check each associated simulation to ensure math overrides are applied in an equivalent manner also.
//
SimulationContext[] simContextsFromDB = bioModelFromDB.getSimulationContexts();
SimulationContext[] simContextsNewMath = bioModelNewMath.getSimulationContexts();
for (int k = 0; k < simContextsFromDB.length; k++) {
SimulationContext simContextFromDB = simContextsFromDB[k];
Simulation[] appSimsFromDB = simContextFromDB.getSimulations();
SimulationContext simContextNewMath = simContextsNewMath[k];
Simulation[] appSimsNewMath = simContextNewMath.getSimulations();
MathCompareResults mathCompareResults = null;
try {
MathDescription origMathDesc = simContextFromDB.getMathDescription();
//
// find out if any simulation belonging to this Application has data
//
boolean bApplicationHasData = false;
for (int l = 0; l < modelSimsFromDB.length; l++) {
SimulationStatusPersistent simulationStatus = dbServerImpl.getSimulationStatus(modelSimsFromDB[l].getKey());
if (simulationStatus != null && simulationStatus.getHasData()) {
bApplicationHasData = true;
}
}
//
try {
if (simContextNewMath.getGeometry().getDimension() > 0 && simContextNewMath.getGeometry().getGeometrySurfaceDescription().getGeometricRegions() == null) {
simContextNewMath.getGeometry().getGeometrySurfaceDescription().updateAll();
}
} catch (Exception e) {
e.printStackTrace(System.out);
}
//
// updated mathdescription loaded into copy of biomodel, then test for equivalence.
//
cbit.vcell.mapping.MathMapping mathMapping = simContextNewMath.createNewMathMapping();
MathDescription newMathDesc = mathMapping.getMathDescription();
String issueString = null;
org.vcell.util.Issue[] issues = mathMapping.getIssues();
if (issues != null && issues.length > 0) {
StringBuffer buffer = new StringBuffer("Issues(" + issues.length + "):");
for (int l = 0; l < issues.length; l++) {
buffer.append(" <<" + issues[l].toString() + ">>");
}
issueString = buffer.toString();
}
simContextNewMath.setMathDescription(newMathDesc);
MathCompareResults testIfSameResults = cbit.vcell.math.MathUtilities.testIfSame(SimulationSymbolTable.createMathSymbolTableFactory(), origMathDesc, newMathDesc);
mathCompareResults = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(), origMathDesc, newMathDesc);
StringBuffer buffer = new StringBuffer();
buffer.append(">>>BioModel(" + bioModelFromDB.getVersion().getVersionKey() + ") '" + bioModelFromDB.getName() + "':" + bioModelFromDB.getVersion().getDate() + ", Application(" + simContextFromDB.getKey() + ") '" + simContextFromDB.getName() + "' <<EQUIV=" + mathCompareResults.isEquivalent() + ">>: " + mathCompareResults.toDatabaseStatus());
//
if (bUpdateDatabase) {
con = null;
stmt = null;
try {
con = conFactory.getConnection(new Object());
stmt = con.createStatement();
// KeyValue mathKey = origMathDesc.getKey();
String UPDATESTATUS = "UPDATE " + SimContextStat2Table.table.getTableName() + " SET " + SimContextStat2Table.table.hasData.getUnqualifiedColName() + " = " + ((bApplicationHasData) ? (1) : (0)) + ", " + SimContextStat2Table.table.equiv.getUnqualifiedColName() + " = " + (mathCompareResults.isEquivalent() ? (1) : (0)) + ", " + SimContextStat2Table.table.status.getUnqualifiedColName() + " = '" + org.vcell.util.TokenMangler.getSQLEscapedString(mathCompareResults.toDatabaseStatus()) + "'" + ((issueString != null) ? (", " + SimContextStat2Table.table.comments.getUnqualifiedColName() + " = '" + org.vcell.util.TokenMangler.getSQLEscapedString(issueString, 255) + "'") : ("")) + " WHERE " + SimContextStat2Table.table.simContextRef.getUnqualifiedColName() + " = " + simContextFromDB.getKey();
int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
if (numRowsChanged != 1) {
System.out.println("failed to update status");
}
con.commit();
if (lg.isTraceEnabled())
lg.trace("-------------- Update=true, saved 'newMath' for Application '" + simContextFromDB.getName() + "'");
} catch (SQLException e) {
lg.error(e.getMessage(), e);
if (lg.isWarnEnabled())
lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO UPDATE MATH for Application '" + simContextFromDB.getName() + "'");
} finally {
if (stmt != null) {
stmt.close();
}
con.close();
}
}
} catch (Throwable e) {
// exception in SimContext
lg.error(e.getMessage(), e);
if (bUpdateDatabase) {
con = null;
stmt = null;
try {
con = conFactory.getConnection(new Object());
stmt = con.createStatement();
String status = "'EXCEPTION: " + org.vcell.util.TokenMangler.getSQLEscapedString(e.toString()) + "'";
if (status.length() > 255)
status = status.substring(0, 254) + "'";
String UPDATESTATUS = "UPDATE " + SimContextStat2Table.table.getTableName() + " SET " + SimContextStat2Table.table.status.getUnqualifiedColName() + " = " + status + " WHERE " + SimContextStat2Table.table.simContextRef.getUnqualifiedColName() + " = " + simContextFromDB.getKey();
int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
if (numRowsChanged != 1) {
System.out.println("failed to update status with exception");
}
con.commit();
if (lg.isTraceEnabled())
lg.trace("-------------- Update=true, saved exception for Application '" + simContextFromDB.getName() + "'");
} catch (SQLException e2) {
lg.error(e2.getMessage(), e2);
if (lg.isWarnEnabled())
lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO save exception status for Application '" + simContextFromDB.getName() + "'");
} finally {
if (stmt != null) {
stmt.close();
}
con.close();
}
}
}
//
for (int l = 0; l < appSimsFromDB.length; l++) {
try {
boolean bSimEquivalent = Simulation.testEquivalency(appSimsNewMath[l], appSimsFromDB[l], mathCompareResults);
if (lg.isTraceEnabled())
lg.trace("Application(" + simContextFromDB.getKey() + ") '" + simContextFromDB.getName() + "', " + "Simulation(" + modelSimsFromDB[l].getKey() + ") '" + modelSimsFromDB[l].getName() + "':" + modelSimsFromDB[l].getVersion().getDate() + "mathEquivalency=" + mathCompareResults.isEquivalent() + ", simEquivalency=" + bSimEquivalent);
//
if (bUpdateDatabase) {
con = null;
stmt = null;
try {
con = conFactory.getConnection(new Object());
stmt = con.createStatement();
String UPDATESTATUS = "UPDATE " + SimStatTable.table.getTableName() + " SET " + SimStatTable.table.equiv.getUnqualifiedColName() + " = " + ((bSimEquivalent) ? (1) : (0)) + ", " + SimStatTable.table.status.getUnqualifiedColName() + " = '" + org.vcell.util.TokenMangler.getSQLEscapedString(mathCompareResults.decision.description) + "'" + " WHERE " + SimStatTable.table.simRef.getUnqualifiedColName() + " = " + appSimsFromDB[l].getKey();
int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
if (numRowsChanged != 1) {
System.out.println("failed to update status");
}
con.commit();
if (lg.isTraceEnabled())
lg.trace("-------------- Update=true, saved 'simulation status for simulation '" + appSimsFromDB[l].getName() + "'");
} catch (SQLException e) {
lg.error(e.getMessage(), e);
if (lg.isWarnEnabled())
lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO UPDATE status for simulation '" + appSimsFromDB[l].getName() + "'");
} finally {
if (stmt != null) {
stmt.close();
}
con.close();
}
}
} catch (Throwable e) {
// exception in SimContext
lg.error(e.getMessage(), e);
if (bUpdateDatabase) {
con = null;
stmt = null;
try {
con = conFactory.getConnection(new Object());
stmt = con.createStatement();
String status = "'EXCEPTION: " + org.vcell.util.TokenMangler.getSQLEscapedString(e.toString()) + "'";
if (status.length() > 255)
status = status.substring(0, 254) + "'";
String UPDATESTATUS = "UPDATE " + SimStatTable.table.getTableName() + " SET " + SimStatTable.table.status.getUnqualifiedColName() + " = " + status + " WHERE " + SimStatTable.table.simRef.getUnqualifiedColName() + " = " + appSimsFromDB[l].getKey();
int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
if (numRowsChanged != 1) {
System.out.println("failed to update status with exception");
}
con.commit();
if (lg.isTraceEnabled())
lg.trace("-------------- Update=true, saved exception for Simulation '" + appSimsFromDB[l].getName() + "'");
} catch (SQLException e2) {
lg.error(e2.getMessage(), e2);
if (lg.isWarnEnabled())
lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO save exception status for simulation '" + appSimsFromDB[l].getName() + "'");
} finally {
if (stmt != null) {
stmt.close();
}
con.close();
}
}
}
}
}
} catch (Throwable e) {
// exception in whole BioModel
lg.error(e.getMessage(), e);
// can't update anything in database, since we don't know what simcontexts are involved
}
}
}
use of org.vcell.util.document.KeyValue in project vcell by virtualcell.
the class ModelDbDriver method getSpeciesContext.
/**
* This method was created in VisualAge.
* @return cbit.vcell.model.SpeciesContext
* @param rset java.sql.ResultSet
*/
private SpeciesContext getSpeciesContext(QueryHashtable dbc, Connection con, ResultSet rset) throws SQLException, DataAccessException {
//
// if in object cache, no need to create it
//
KeyValue scKey = new KeyValue(rset.getBigDecimal(SpeciesContextModelTable.table.id.toString()));
SpeciesContext speciesContext = (SpeciesContext) dbc.get(scKey);
if (speciesContext != null) {
return speciesContext;
}
//
// get foreign keys
//
KeyValue structKey = new KeyValue(rset.getBigDecimal(SpeciesContextModelTable.table.structRef.toString()));
KeyValue speciesKey = new KeyValue(rset.getBigDecimal(SpeciesContextModelTable.table.speciesRef.toString()));
//
// get object (ignoring foreign keys)
//
speciesContext = speciesContextModelTable.getSpeciesContext(rset, scKey);
String speciesPatternString = speciesContext.getSpeciesPatternString();
//
// add objects corresponding to foreign keys
//
Structure structure = reactStepDB.getStructure(dbc, con, structKey);
Species species = reactStepDB.getSpecies(dbc, con, speciesKey);
speciesContext = new SpeciesContext(scKey, speciesContext.getName(), species, structure);
speciesContext.setSpeciesPatternString(speciesPatternString);
//
// put SpeciesContext into object cache
//
dbc.put(scKey, speciesContext);
return speciesContext;
}
use of org.vcell.util.document.KeyValue in project vcell by virtualcell.
the class ModelDbDriver method getModel.
/**
* This method was created in VisualAge.
* @return cbit.vcell.model.Model
* @param rset java.sql.ResultSet
*/
private Model getModel(QueryHashtable dbc, ResultSet rset, Connection con, User user) throws SQLException, DataAccessException {
// User owner = new User(ownerName, ownerRef);
try {
Model model = modelTable.getModel(rset, con);
// model.setOwner(owner);
KeyValue modelKey = model.getVersion().getVersionKey();
//
// set structures for this model
//
StructureTopology structureTopology = model.getStructureTopology();
Structure[] structures = reactStepDB.getStructuresFromModel(dbc, con, modelKey);
if (structures != null && structures.length > 0) {
model.setStructures(structures);
}
HashMap<KeyValue, StructureKeys> structureKeysMap = reactStepDB.getStructureParentMapByModel(dbc, con, modelKey);
ReactStepDbDriver.populateStructureAndElectricalTopology(model, structureKeysMap);
//
// set species for this model
//
SpeciesContext[] speciesContexts = getSpeciesContextFromModel(dbc, con, user, modelKey, structureTopology);
if (speciesContexts != null) {
Vector<Species> speciesList = new Vector<Species>();
for (int i = 0; i < speciesContexts.length; i++) {
if (!speciesList.contains(speciesContexts[i].getSpecies())) {
speciesList.addElement(speciesContexts[i].getSpecies());
}
}
Species[] speciesArray = new Species[speciesList.size()];
speciesList.copyInto(speciesArray);
model.setSpecies(speciesArray);
}
//
if (speciesContexts != null) {
model.setSpeciesContexts(speciesContexts);
}
//
// Add global parameters to the model
//
GlobalModelParameterTable.table.setModelParameters(con, model);
//
// add reactionSteps for this model
//
ReactionStep[] reactSteps = reactStepDB.getReactionStepsFromModel(dbc, con, model, modelKey);
if (reactSteps != null) {
model.setReactionSteps(reactSteps);
for (int i = 0; i < reactSteps.length; i++) {
try {
//
// fix any improperly defined reactionSteps (which have parameters that should be catalysts)
// name space of kinetic parameters should be unique with respect to SpeciesContexts (so if they overlap, should be a catalyst).
//
Kinetics.KineticsParameter[] params = reactSteps[i].getKinetics().getKineticsParameters();
for (int j = 0; j < params.length; j++) {
SpeciesContext speciesContext = model.getSpeciesContext(params[j].getName());
if (speciesContext != null) {
reactSteps[i].addCatalyst(speciesContext);
if (lg.isWarnEnabled())
lg.warn("ModelDbDriver.getModel(), Parameter '" + params[j].getName() + "' in Reaction " + reactSteps[i].getName() + " in Model(" + model.getKey() + ") conflicts with SpeciesContext, added as a catalyst");
}
}
} catch (Throwable e) {
lg.error(e.getMessage(), e);
}
try {
reactSteps[i].rebindAllToModel(model);
} catch (cbit.vcell.parser.ExpressionBindingException e) {
throw new DataAccessException("bindingException: " + e.getMessage());
} catch (cbit.vcell.parser.ExpressionException e) {
throw new DataAccessException(e.getMessage());
} catch (PropertyVetoException e) {
throw new DataAccessException("PropertyVetoException: " + e.getMessage());
} catch (cbit.vcell.model.ModelException e) {
throw new DataAccessException(e.getMessage());
}
}
}
//
// add diagrams for this model
//
Diagram[] diagrams = getDiagramsFromModel(dbc, con, modelKey, structureTopology, dbSyntax);
model.setDiagrams(diagrams);
//
// add rbm
//
ModelTable.readRbmElement(con, model, dbSyntax);
if (!model.getRbmModelContainer().isEmpty()) {
for (SpeciesContext sc : model.getSpeciesContexts()) {
sc.parseSpeciesPatternString(model);
}
}
return model;
} catch (PropertyVetoException e) {
lg.error(e.getMessage(), e);
throw new DataAccessException(e.getMessage());
}
}
use of org.vcell.util.document.KeyValue in project vcell by virtualcell.
the class ReactStepTable method getUserReactionList.
/**
* Insert the method's description here.
* Creation date: (7/12/2003 2:59:27 PM)
* @return java.lang.String
* @param likeString java.lang.String
*/
public ReactionDescription[] getUserReactionList(java.sql.ResultSet rset) throws java.sql.SQLException {
java.util.Vector resultV = new java.util.Vector();
ReactionDescription[] result = null;
java.util.Vector rxidV = null;
//
java.math.BigDecimal rxid = null;
ReactionDescription dbfr = null;
//
while (rset.next()) {
java.math.BigDecimal currRXID = rset.getBigDecimal(ReactStepTable.table.id.toString());
if (rset.isFirst()) {
rxid = currRXID;
}
if (rxid != null && !rxid.equals(currRXID)) {
rxid = currRXID;
dbfr = null;
}
//
if (dbfr == null) {
ReactionType rxType = ReactionType.fromDatabaseName(rset.getString(ReactStepTable.table.reactType.toString()));
String rxName = rset.getString(ReactStepTable.table.name.toString());
java.math.BigDecimal currBioModelID = rset.getBigDecimal("bmid");
java.math.BigDecimal currStructID = rset.getBigDecimal(ReactStepTable.table.structRef.toString());
dbfr = new ReactionDescription(rxName, rxType, new KeyValue(rxid), new KeyValue(currBioModelID), new KeyValue(currStructID));
resultV.add(dbfr);
}
String name = rset.getString(SpeciesTable.table.commonName.toString());
DBNonFormalUnboundSpecies dbnfu = new DBNonFormalUnboundSpecies(name);
//
String partRole = rset.getString(ReactPartTable.table.role.toString());
char partRoleType;
if (partRole.equals("reactant")) {
partRoleType = ReactionDescription.RX_ELEMENT_REACTANT;
} else if (partRole.equals("flux")) {
partRoleType = ReactionDescription.RX_ELEMENT_FLUX;
} else if (partRole.equals("catalyst")) {
partRoleType = ReactionDescription.RX_ELEMENT_CATALYST;
} else if (partRole.equals("product")) {
partRoleType = ReactionDescription.RX_ELEMENT_PRODUCT;
} else {
throw new RuntimeException("Unknown role in UserList Query results");
}
//
int stoich = rset.getInt(ReactPartTable.table.stoich.toString());
dbfr.addReactionElement(dbnfu, stoich, partRoleType);
if (partRole.equals("flux")) {
dbfr.addReactionElement(new DBNonFormalUnboundSpecies(name), stoich, partRoleType);
}
}
//
if (resultV.size() > 0) {
result = new ReactionDescription[resultV.size()];
resultV.copyInto(result);
}
return result;
}
use of org.vcell.util.document.KeyValue in project vcell by virtualcell.
the class ResultSetCrawler method scanUserDirectory.
/**
* Insert the method's description here.
* Creation date: (2/2/01 3:40:29 PM)
*/
private static void scanUserDirectory(File userDir, ExternalDataIdentifier[] extDataIDArr, SimulationInfo[] simulationInfos, File outputDir, final boolean bScanOnly, AmplistorUtils.AmplistorCredential amplistorCredential) throws Exception {
File outputFile = null;
java.io.PrintWriter writer = null;
try {
long timestamp = new Date().getTime();
outputFile = new File(outputDir, "ResultSetCrawler_" + userDir.getName() + "_" + timestamp + ".txt");
writer = new java.io.PrintWriter(new java.io.FileOutputStream(outputFile));
final PrintWriter pw = writer;
pw.println("scanning directory : " + userDir.getPath());
if (lg.isTraceEnabled())
lg.trace("scanning directory : " + userDir.getPath());
//
// gather list of keys that should be retained.
//
final HashSet<KeyValue> referencedKeys = new HashSet<KeyValue>();
for (ExternalDataIdentifier extDataId : extDataIDArr) {
referencedKeys.add(extDataId.getKey());
}
for (SimulationInfo simulationInfo : simulationInfos) {
referencedKeys.add(simulationInfo.getSimulationVersion().getVersionKey());
}
if (amplistorCredential != null) {
try {
if (bScanOnly) {
ArrayList<String> shouldBeDeleted = AmplistorUtils.deleteSimFilesNotInHash(AmplistorUtils.DEFAULT_AMPLI_SERVICE_VCELL_URL + userDir.getName(), referencedKeys, bScanOnly, amplistorCredential);
for (String fileName : shouldBeDeleted) {
pw.println("Should delete Amplistor " + fileName);
if (lg.isTraceEnabled())
lg.trace("Should delete Amplistor " + fileName);
}
} else {
ArrayList<String> wasDeleted = AmplistorUtils.deleteSimFilesNotInHash(AmplistorUtils.DEFAULT_AMPLI_SERVICE_VCELL_URL + userDir.getName(), referencedKeys, bScanOnly, amplistorCredential);
for (String fileName : wasDeleted) {
pw.println("deleted Amplistor " + fileName);
if (lg.isTraceEnabled())
lg.trace("deleted Amplistor " + fileName);
}
}
} catch (Exception e) {
if (lg.isTraceEnabled())
lg.trace("Amplistor delete failed url=" + AmplistorUtils.DEFAULT_AMPLI_SERVICE_VCELL_URL + userDir.getName() + " : " + e.getMessage());
pw.println("Amplistor delete failed url=" + AmplistorUtils.DEFAULT_AMPLI_SERVICE_VCELL_URL + userDir.getName() + " : " + e.getMessage());
}
}
final HashMap<KeyValue, Integer> deletedKeyMap = new HashMap<KeyValue, Integer>();
//
// visit each file and delete it on the fly (through a FileFilter)
//
FileFilter fileVisitor = new FileFilter() {
@Override
public boolean accept(File file) {
String filename = file.getName();
String[] parts = filename.split("\\_|\\.");
if (parts.length > 0 && parts[0].equals("SimID")) {
if (parts.length > 1) {
String simkeyString = parts[1];
try {
KeyValue simKey = new KeyValue(simkeyString);
if (!referencedKeys.contains(simKey)) {
Integer count = deletedKeyMap.get(simKey);
if (count == null) {
deletedKeyMap.put(simKey, 1);
count = 1;
} else {
deletedKeyMap.put(simKey, 1 + count.intValue());
}
if (bScanOnly) {
pw.println("Should delete file(" + count + ") with key " + simKey + " : " + file.getPath());
if (lg.isTraceEnabled())
lg.trace("Should delete file(" + count + ") with key " + simKey + " : " + file.getPath());
} else {
pw.println("deleted file(" + simKey + ":" + count + ") " + file.getPath());
if (lg.isTraceEnabled())
lg.trace("deleted file(" + simKey + ":" + count + ") " + file.getPath());
file.delete();
}
}
} catch (Exception e) {
if (lg.isTraceEnabled())
lg.trace("failed to process file " + file.getPath() + ": " + e.getMessage());
}
}
}
return false;
}
};
//
// visit all of the files and delete if bScanOnly=false
//
userDir.listFiles(fileVisitor);
pw.println("done scanning directory : " + userDir.getPath());
if (lg.isTraceEnabled())
lg.trace("done scanning directory : " + userDir.getPath());
} finally {
if (writer != null) {
writer.close();
}
if (lg.isTraceEnabled())
lg.trace("User " + userDir.getName() + ", See " + outputFile.getAbsolutePath() + " for details");
}
}
Aggregations