Search in sources :

Example 1 with StructureKeys

use of cbit.vcell.modeldb.ReactStepDbDriver.StructureKeys 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());
    }
}
Also used : KeyValue(org.vcell.util.document.KeyValue) SpeciesContext(cbit.vcell.model.SpeciesContext) Structure(cbit.vcell.model.Structure) Species(cbit.vcell.model.Species) Vector(java.util.Vector) DataAccessException(org.vcell.util.DataAccessException) StructureTopology(cbit.vcell.model.Model.StructureTopology) Diagram(cbit.vcell.model.Diagram) PropertyVetoException(java.beans.PropertyVetoException) StructureKeys(cbit.vcell.modeldb.ReactStepDbDriver.StructureKeys) ReactionStep(cbit.vcell.model.ReactionStep) Model(cbit.vcell.model.Model)

Example 2 with StructureKeys

use of cbit.vcell.modeldb.ReactStepDbDriver.StructureKeys in project vcell by virtualcell.

the class ModelDbDriver method insertModel.

/**
 * This method was created in VisualAge.
 * @param model cbit.vcell.model.Model
 */
private void insertModel(InsertHashtable hash, Connection con, User user, Model model, Version newVersion) throws SQLException, DataAccessException, RecordChangedException {
    // 
    // insert model record
    // 
    insertModelSQL(con, user, model, newVersion);
    hash.put(model, newVersion.getVersionKey());
    // 
    // make sure all species are in the database and the hashtable
    // 
    StructureTopology structureTopology = model.getStructureTopology();
    ElectricalTopology electricalTopology = model.getElectricalTopology();
    Species[] speciesArray = model.getSpecies();
    for (int i = 0; i < speciesArray.length; i++) {
        // speciesArray[i].getKey();
        KeyValue speciesKey = null;
        // if (speciesKey == null) {
        speciesKey = reactStepDB.insertSpecies(hash, con, speciesArray[i], user);
        // throw new DataAccessException("Database error: species "+species.getName()+" has null key");
        // }else if (speciesArray[i].getOwnerKey()!=null && speciesArray[i].getOwnerKey()==user.getID()){
        // reactStepDB.updateSpecies(con,speciesArray[i],user);
        // }
        // if (hash.getDatabaseKey(speciesArray[i])==null){
        hash.put(speciesArray[i], speciesKey);
    // }
    }
    // 
    // make sure all structures are in the database and the hashtable (add entry to link table)
    // this does not populate the parent, negativeFeature, and positiveFeature columns ... done later with updateStructureKeys()
    // 
    Structure[] structures = model.getStructures();
    for (int i = 0; i < structures.length; i++) {
        Structure structure = (Structure) structures[i];
        KeyValue structureKey = null;
        if (hash.getDatabaseKey(structure) == null) {
            structureKey = reactStepDB.insertStructure(hash, con, structure);
        }
        KeyValue linkKey = keyFactory.getNewKey(con);
        insertModelStructLinkSQL(con, linkKey, newVersion.getVersionKey(), /*modelKey*/
        hash.getDatabaseKey(structure));
    }
    // 
    for (Structure structure : structures) {
        KeyValue structKey = hash.getDatabaseKey(structure);
        KeyValue parentKey = null;
        Structure parentStruct = structureTopology.getParentStructure(structure);
        if (parentStruct != null) {
            parentKey = hash.getDatabaseKey(parentStruct);
        }
        KeyValue negKey = null;
        KeyValue posKey = null;
        if (structure instanceof Membrane) {
            Membrane membrane = (Membrane) structure;
            Feature negFeature = electricalTopology.getNegativeFeature(membrane);
            if (negFeature != null) {
                negKey = hash.getDatabaseKey(negFeature);
            }
            Feature posFeature = electricalTopology.getPositiveFeature(membrane);
            if (posFeature != null) {
                posKey = hash.getDatabaseKey(posFeature);
            }
        }
        StructureKeys structureKeys = new StructureKeys(structKey, parentKey, posKey, negKey);
        reactStepDB.updateStructureKeys(con, structureKeys);
    }
    // 
    // make sure all speciesContexts are in database and the hashtable
    // 
    SpeciesContext[] speciesContexts = model.getSpeciesContexts();
    for (int i = 0; i < speciesContexts.length; i++) {
        SpeciesContext sc = speciesContexts[i];
        if (hash.getDatabaseKey(sc) == null) {
            insertSpeciesContextSQL(hash, con, keyFactory.getNewKey(con), sc, newVersion.getVersionKey());
        }
    }
    // 
    // insert all reactionSteps
    // 
    ReactionStep[] reactionSteps = model.getReactionSteps();
    for (int i = 0; i < reactionSteps.length; i++) {
        ReactionStep rs = reactionSteps[i];
        if (hash.getDatabaseKey(rs) == null) {
            reactStepDB.insertReactionStep(hash, con, user, rs, newVersion.getVersionKey());
        }
    }
    // 
    // insert diagrams
    // 
    Diagram[] diagrams = model.getDiagrams();
    for (int i = 0; i < diagrams.length; i++) {
        Diagram diagram = diagrams[i];
        KeyValue key = keyFactory.getNewKey(con);
        KeyValue structKey = hash.getDatabaseKey(diagram.getStructure());
        if (structKey != null) {
            insertDiagramSQL(con, key, diagram, newVersion.getVersionKey(), /*modelKey*/
            hash.getDatabaseKey(diagram.getStructure()));
        } else {
            if (lg.isWarnEnabled())
                lg.warn("ModelDbDriver.insertModel(),  diagram " + diagram.toString() + " is orphaned, check Model logic");
        }
    }
    // 
    // insert GlobalModelParameters
    // 
    // //-----------------testing remove
    // try{
    // Model.ModelParameter testParam = model.new ModelParameter("test",new Expression("1.0"),Model.ROLE_UserDefined,VCUnitDefinition.UNIT_molecules);
    // model.setModelParameters(new Model.ModelParameter[] {testParam});
    // }catch(Exception e){
    // e.printStackTrace();
    // }
    // //-----------------
    GlobalModelParameterTable.table.insertModelParameters(con, keyFactory, model.getModelParameters(), newVersion.getVersionKey());
}
Also used : KeyValue(org.vcell.util.document.KeyValue) StructureTopology(cbit.vcell.model.Model.StructureTopology) ElectricalTopology(cbit.vcell.model.Model.ElectricalTopology) SpeciesContext(cbit.vcell.model.SpeciesContext) Feature(cbit.vcell.model.Feature) Diagram(cbit.vcell.model.Diagram) StructureKeys(cbit.vcell.modeldb.ReactStepDbDriver.StructureKeys) ReactionStep(cbit.vcell.model.ReactionStep) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) Species(cbit.vcell.model.Species)

Aggregations

Diagram (cbit.vcell.model.Diagram)2 StructureTopology (cbit.vcell.model.Model.StructureTopology)2 ReactionStep (cbit.vcell.model.ReactionStep)2 Species (cbit.vcell.model.Species)2 SpeciesContext (cbit.vcell.model.SpeciesContext)2 Structure (cbit.vcell.model.Structure)2 StructureKeys (cbit.vcell.modeldb.ReactStepDbDriver.StructureKeys)2 KeyValue (org.vcell.util.document.KeyValue)2 Feature (cbit.vcell.model.Feature)1 Membrane (cbit.vcell.model.Membrane)1 Model (cbit.vcell.model.Model)1 ElectricalTopology (cbit.vcell.model.Model.ElectricalTopology)1 PropertyVetoException (java.beans.PropertyVetoException)1 Vector (java.util.Vector)1 DataAccessException (org.vcell.util.DataAccessException)1