use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class SBMLImporter method getBioModel.
// /**
// * @ TODO: This method doesn't take care of adjusting species in nested
// parameter rules with the species_concetration_factor.
// * @param kinetics
// * @param paramExpr
// * @throws ExpressionException
// */
// private void substituteOtherGlobalParams(Kinetics kinetics, Expression
// paramExpr) throws ExpressionException, PropertyVetoException {
// String[] exprSymbols = paramExpr.getSymbols();
// if (exprSymbols == null || exprSymbols.length == 0) {
// return;
// }
// Model vcModel = vcBioModel.getSimulationContext(0).getModel();
// for (int kk = 0; kk < exprSymbols.length; kk++) {
// ModelParameter mp = vcModel.getModelParameter(exprSymbols[kk]);
// if (mp != null) {
// Expression expr = mp.getExpression();
// if (expr != null) {
// Expression newExpr = new Expression(expr);
// substituteGlobalParamRulesInPlace(newExpr, false);
// // param has constant value, add it as a kinetic parameter if it is not
// already in the kinetics
// kinetics.setParameterValue(exprSymbols[kk], newExpr.infix());
// kinetics.getKineticsParameter(exprSymbols[kk]).setUnitDefinition(getSBMLUnit(sbmlModel.getParameter(exprSymbols[kk]).getUnits(),
// null));
// if (newExpr.getSymbols() != null) {
// substituteOtherGlobalParams(kinetics, newExpr);
// }
// }
// }
// }
// }
/**
* parse SBML file into biomodel logs errors to log4j if present in source
* document
*
* @return new Biomodel
* @throws IOException
* @throws XMLStreamException
*/
public BioModel getBioModel() throws XMLStreamException, IOException {
SBMLDocument document;
String output = "didn't check";
try {
if (sbmlFileName != null) {
// Read SBML model into libSBML SBMLDocument and create an SBML model
SBMLReader reader = new SBMLReader();
document = reader.readSBML(sbmlFileName);
// document.checkConsistencyOffline();
// long numProblems = document.getNumErrors();
//
// System.out.println("\n\nSBML Import Error Report");
// ByteArrayOutputStream os = new ByteArrayOutputStream();
// PrintStream ps = new PrintStream(os);
// document.printErrors(ps);
// String output = os.toString();
// if (numProblems > 0 && lg.isEnabledFor(Level.WARN)) {
// lg.warn("Num problems in original SBML document : " + numProblems);
// lg.warn(output);
// }
sbmlModel = document.getModel();
if (sbmlModel == null) {
throw new SBMLImportException("Unable to read SBML file : \n" + output);
}
} else {
if (sbmlModel == null) {
throw new IllegalStateException("Expected non-null SBML model");
}
document = sbmlModel.getSBMLDocument();
}
// Convert SBML Model to VCell model
// An SBML model will correspond to a simcontext - which needs a
// Model and a Geometry
// SBML handles only nonspatial geometries at this time, hence
// creating a non-spatial default geometry
String modelName = sbmlModel.getId();
if (modelName == null || modelName.trim().equals("")) {
modelName = sbmlModel.getName();
}
// name, say 'newModel'
if (modelName == null || modelName.trim().equals("")) {
modelName = "newModel";
}
// get namespace based on SBML model level and version to use in
// SBMLAnnotationUtil
this.level = sbmlModel.getLevel();
// this.version = sbmlModel.getVersion();
String ns = document.getNamespace();
try {
// create SBML unit system for the model and create the bioModel.
ModelUnitSystem modelUnitSystem;
try {
modelUnitSystem = createSBMLUnitSystemForVCModel();
} catch (Exception e) {
e.printStackTrace(System.out);
throw new SBMLImportException("Inconsistent unit system. Cannot import SBML model into VCell", Category.INCONSISTENT_UNIT, e);
}
Geometry geometry = new Geometry(BioModelChildSummary.COMPARTMENTAL_GEO_STR, 0);
vcBioModel = new BioModel(null, modelUnitSystem);
SimulationContext simulationContext = new SimulationContext(vcBioModel.getModel(), geometry, null, null, Application.NETWORK_DETERMINISTIC);
vcBioModel.addSimulationContext(simulationContext);
simulationContext.setName(vcBioModel.getSimulationContext(0).getModel().getName());
// vcBioModel.getSimulationContext(0).setName(vcBioModel.getSimulationContext(0).getModel().getName()+"_"+vcBioModel.getSimulationContext(0).getGeometry().getName());
} catch (PropertyVetoException e) {
e.printStackTrace(System.out);
throw new SBMLImportException("Could not create simulation context corresponding to the input SBML model", e);
}
// SBML annotation
sbmlAnnotationUtil = new SBMLAnnotationUtil(vcBioModel.getVCMetaData(), vcBioModel, ns);
translateSBMLModel();
try {
// **** TEMPORARY BLOCK - to name the biomodel with proper name,
// rather than model id
String biomodelName = sbmlModel.getName();
// if name is not set, use id
if ((biomodelName == null) || biomodelName.trim().equals("")) {
biomodelName = sbmlModel.getId();
}
// if id is not set, use a default, say, 'newModel'
if ((biomodelName == null) || biomodelName.trim().equals("")) {
biomodelName = "newBioModel";
}
vcBioModel.setName(biomodelName);
// **** end - TEMPORARY BLOCK
} catch (Exception e) {
e.printStackTrace(System.out);
throw new SBMLImportException("Could not create Biomodel", e);
}
sbmlAnnotationUtil.readAnnotation(vcBioModel, sbmlModel);
sbmlAnnotationUtil.readNotes(vcBioModel, sbmlModel);
vcBioModel.refreshDependencies();
Issue[] warningIssues = localIssueList.toArray(new Issue[localIssueList.size()]);
if (warningIssues != null && warningIssues.length > 0) {
StringBuffer messageBuffer = new StringBuffer("Issues encountered during SBML Import:\n");
int issueCount = 0;
for (int i = 0; i < warningIssues.length; i++) {
if (warningIssues[i].getSeverity() == Issue.SEVERITY_WARNING || warningIssues[i].getSeverity() == Issue.SEVERITY_INFO) {
messageBuffer.append(warningIssues[i].getCategory() + " " + warningIssues[i].getSeverityName() + " : " + warningIssues[i].getMessage() + "\n");
issueCount++;
}
}
if (issueCount > 0) {
try {
logger.sendMessage(VCLogger.Priority.MediumPriority, VCLogger.ErrorType.OverallWarning, messageBuffer.toString());
} catch (Exception e) {
e.printStackTrace(System.out);
}
// PopupGenerator.showWarningDialog(requester,messageBuffer.toString(),new
// String[] { "OK" }, "OK");
}
}
} catch (Exception e) {
throw new SBMLImportException("Unable to read SBML file : \n" + output, e);
}
return vcBioModel;
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class SBMLImporterTest method testImport.
@Test
public void testImport() throws XMLStreamException, IOException {
HashMap<Integer, FAULT> faults = new HashMap();
faults.put(6, FAULT.RESERVED_WORD);
faults.put(15, FAULT.RESERVED_WORD);
faults.put(92, FAULT.RESERVED_WORD);
faults.put(114, FAULT.RESERVED_WORD);
faults.put(115, FAULT.RESERVED_WORD);
faults.put(117, FAULT.RESERVED_WORD);
faults.put(148, FAULT.RESERVED_WORD);
faults.put(154, FAULT.RESERVED_WORD);
faults.put(155, FAULT.RESERVED_WORD);
faults.put(154, FAULT.RESERVED_WORD);
faults.put(155, FAULT.RESERVED_WORD);
faults.put(156, FAULT.RESERVED_WORD);
faults.put(157, FAULT.RESERVED_WORD);
faults.put(158, FAULT.RESERVED_WORD);
faults.put(159, FAULT.RESERVED_WORD);
faults.put(274, FAULT.RESERVED_WORD);
faults.put(279, FAULT.RESERVED_WORD);
faults.put(282, FAULT.RESERVED_WORD);
faults.put(288, FAULT.RESERVED_WORD);
faults.put(346, FAULT.RESERVED_WORD);
faults.put(24, FAULT.DELAY);
faults.put(25, FAULT.DELAY);
faults.put(34, FAULT.DELAY);
faults.put(196, FAULT.DELAY);
faults.put(39, FAULT.NONINTEGER_STOICH);
faults.put(59, FAULT.NONINTEGER_STOICH);
faults.put(63, FAULT.NONINTEGER_STOICH);
faults.put(81, FAULT.NONINTEGER_STOICH);
faults.put(145, FAULT.NONINTEGER_STOICH);
faults.put(151, FAULT.NONINTEGER_STOICH);
faults.put(199, FAULT.NONINTEGER_STOICH);
faults.put(206, FAULT.NONINTEGER_STOICH);
faults.put(232, FAULT.NONINTEGER_STOICH);
faults.put(244, FAULT.NONINTEGER_STOICH);
faults.put(246, FAULT.NONINTEGER_STOICH);
faults.put(110, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(178, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(228, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(245, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(252, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(262, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(263, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(264, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(267, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(283, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(300, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(316, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(317, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(319, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(322, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(323, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(337, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(327, FAULT.EXPRESSION_BINDING_EXCEPTION);
faults.put(340, FAULT.XOR_MISSING);
faults.put(248, FAULT.EXPRESSION_BINDING_EXCEPTION);
faults.put(305, FAULT.EXPRESSION_BINDING_EXCEPTION);
faults.put(353, FAULT.NONINTEGER_STOICH);
faults.put(367, FAULT.RESERVED_WORD);
faults.put(382, FAULT.RESERVED_WORD);
faults.put(383, FAULT.NONINTEGER_STOICH);
faults.put(384, FAULT.NONINTEGER_STOICH);
faults.put(385, FAULT.NONINTEGER_STOICH);
faults.put(386, FAULT.NONINTEGER_STOICH);
faults.put(387, FAULT.NONINTEGER_STOICH);
faults.put(388, FAULT.NONINTEGER_STOICH);
faults.put(392, FAULT.NONINTEGER_STOICH);
faults.put(401, FAULT.NONINTEGER_STOICH);
faults.put(402, FAULT.RESERVED_WORD);
faults.put(403, FAULT.RESERVED_WORD);
faults.put(405, FAULT.INCONSISTENT_UNIT_SYSTEM);
faults.put(539, FAULT.JSBML_ERROR);
File[] sbmlFiles = SBMLUnitTranslatorTest.getBiomodelsCuratedSBMLFiles();
// File[] sbmlFiles = new File[] {
// new File("/Users/schaff/Documents/workspace-maven/BioModels_Database-r30_pub-sbml_files/curated/BIOMD0000000001.xml"),
// new File("/Users/schaff/Documents/workspace-maven/BioModels_Database-r30_pub-sbml_files/curated/BIOMD0000000101.xml"),
// new File("/Users/schaff/Documents/workspace-maven/sbml-test-suite/cases/semantic/00001/00001-sbml-l3v1.xml")
// };
VCLogger vcl = new TLogger();
int start = 401;
for (int index = start; index < sbmlFiles.length; index++) {
File sbmlFile = sbmlFiles[index];
int sbmlNumber = Integer.parseInt(sbmlFile.getName().substring(6).replace(".xml", ""));
if (faults.containsKey(sbmlNumber)) {
System.err.println("skipping this model, " + faults.get(sbmlNumber).name());
continue;
}
System.out.println("testing " + sbmlFile);
SBMLImporter importer = new SBMLImporter(sbmlFile.getAbsolutePath(), vcl, false);
BioModel bioModel = importer.getBioModel();
}
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class SBMLStandaloneImporterTest method main.
public static void main(String[] args) {
Logging.init();
try {
SBMLStandaloneImporter sa = new SBMLStandaloneImporter();
for (; ; ) {
/*
JFileChooser jfc = new JFileChooser( new File(System.getProperty("user.dir")));
int returnVal = jfc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File f= jfc.getSelectedFile();
BioModel bm = sa.importSBML(f);
System.out.println(bm.getName());
}
*/
BioModel bm = sa.importSBML(new File("samp.sbml"));
System.out.println(bm.getName());
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class StandaloneSEDMLTest method doit.
public static void doit(File archiveFile) throws Exception {
ArchiveComponents ac = null;
ac = Libsedml.readSEDMLArchive(new FileInputStream(archiveFile));
SEDMLDocument sedmlDoc = ac.getSedmlDocument();
SedML sedml = sedmlDoc.getSedMLModel();
if (sedml == null || sedml.getModels().isEmpty()) {
throw new RuntimeException("sedml null or empty");
}
ModelResolver resolver = new ModelResolver(sedml);
// resolver.add(new FileModelResolver());
resolver.add(new ArchiveModelResolver(ac));
resolver.add(new BioModelsModelsRetriever());
resolver.add(new URLResourceRetriever());
// resolver.add(new RelativeFileModelResolver(FileUtils.getFullPath(archiveFile.getAbsolutePath())));
//
// iterate through all the elements and show them at the console
//
List<org.jlibsedml.Model> mmm = sedml.getModels();
for (Model mm : mmm) {
System.out.println(mm.toString());
}
List<org.jlibsedml.Simulation> sss = sedml.getSimulations();
for (org.jlibsedml.Simulation ss : sss) {
System.out.println(ss.toString());
}
List<AbstractTask> ttt = sedml.getTasks();
for (AbstractTask tt : ttt) {
System.out.println(tt.toString());
}
List<DataGenerator> ddd = sedml.getDataGenerators();
for (DataGenerator dd : ddd) {
System.out.println(dd.toString());
}
List<Output> ooo = sedml.getOutputs();
for (Output oo : ooo) {
System.out.println(oo.toString());
}
//
// extract models referenced in tasks.
//
KisaoOntology kisaoInstance = KisaoOntology.getInstance();
// HashMap<String,Model> flattenedModels = new HashMap<String, Model>();
List<AbstractTask> taskList = sedml.getTasks();
for (AbstractTask task : taskList) {
String modelReference = task.getModelReference();
org.jlibsedml.Model sedmlOriginalModel = sedml.getModelWithId(modelReference);
String sbmlModelString = resolver.getModelString(sedmlOriginalModel);
// sbmlSource with all the changes applied
XMLSource sbmlSource = new XMLSource(sbmlModelString);
org.jlibsedml.Simulation sedmlSimulation = sedml.getSimulation(task.getSimulationReference());
Algorithm algorithm = sedmlSimulation.getAlgorithm();
KisaoTerm sedmlKisao = kisaoInstance.getTermById(algorithm.getKisaoID());
//
// try to find a VCell solverDescription to match the Kisao term
//
// UniformTimeCourse [initialTime=0.0, numberOfPoints=1000, outputEndTime=1.0, outputStartTime=0.0,
// Algorithm [kisaoID=KISAO:0000019], getId()=SimSlow]
// identify the vCell solvers that would match best the sedml solver kisao id
List<SolverDescription> solverDescriptions = new ArrayList<>();
for (SolverDescription sd : SolverDescription.values()) {
KisaoTerm solverKisaoTerm = kisaoInstance.getTermById(sd.getKisao());
if (solverKisaoTerm == null) {
break;
}
boolean isExactlySame = solverKisaoTerm.equals(sedmlKisao);
if (isExactlySame && !solverKisaoTerm.isObsolete()) {
// we make a list with all the solvers that match the kisao
solverDescriptions.add(sd);
}
}
if (solverDescriptions.isEmpty()) {
throw new RuntimeException("cannot find the solverDescription with exact match for Kisao term '" + sedmlKisao + "'");
}
// choose first one
SolverDescription solverDescription = solverDescriptions.get(0);
// find out everything else we need about the application we're going to use,
// some of the info will be needed when we parse the sbml file
boolean bSpatial = false;
Application appType = Application.NETWORK_DETERMINISTIC;
Set<SolverDescription.SolverFeature> sfList = solverDescription.getSupportedFeatures();
for (SolverDescription.SolverFeature sf : sfList) {
switch(sf) {
case Feature_Rulebased:
appType = Application.RULE_BASED_STOCHASTIC;
break;
case Feature_Stochastic:
appType = Application.NETWORK_STOCHASTIC;
break;
case Feature_Deterministic:
appType = Application.NETWORK_DETERMINISTIC;
break;
case Feature_Spatial:
bSpatial = true;
break;
default:
break;
}
}
BioModel bioModel = (BioModel) XmlHelper.importSBML(transLogger, sbmlSource, bSpatial);
//
// we already have an application loaded from the sbml file, with initial conditions and stuff
// which may be not be suitable because the sedml kisao may need a different app type
// so we do a "copy as" to the right type and then delete the original we loaded from the sbml file
//
// the new application we're making from the old one
SimulationContext newSimulationContext = null;
if (bioModel.getSimulationContexts().length == 1) {
SimulationContext oldSimulationContext = bioModel.getSimulationContext(0);
String newSCName = bioModel.getFreeSimulationContextName();
newSimulationContext = SimulationContext.copySimulationContext(oldSimulationContext, newSCName, bSpatial, appType);
bioModel.addSimulationContext(newSimulationContext);
bioModel.removeSimulationContext(oldSimulationContext);
} else {
newSimulationContext = bioModel.addNewSimulationContext("App1", appType);
}
//
// making the new vCell simulation based on the sedml simulation
//
newSimulationContext.refreshDependencies();
MathMappingCallback callback = new MathMappingCallbackTaskAdapter(progressListener);
newSimulationContext.refreshMathDescription(callback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
Simulation newSimulation = new Simulation(newSimulationContext.getMathDescription());
newSimulation.setName(sedmlSimulation.getName());
bioModel.addSimulation(newSimulation);
// and set the vCell simulation parameters accordingly
if (sedmlSimulation instanceof UniformTimeCourse) {
} else if (sedmlSimulation instanceof OneStep) {
} else if (sedmlSimulation instanceof SteadyState) {
} else {
}
System.out.println(XmlHelper.bioModelToXML(bioModel));
}
}
use of cbit.vcell.biomodel.BioModel in project vcell by virtualcell.
the class SimulationServiceImpl method computeModel.
public SimulationInfo computeModel(SBMLModel model, SimulationSpec simSpec) throws ThriftDataAccessException, TException {
try {
SBMLImporter importer = new SBMLImporter(model.getFilepath(), vcLogger(), true);
BioModel bioModel = importer.getBioModel();
return computeModel(bioModel, simSpec, null);
} catch (Exception exc) {
exc.printStackTrace(System.out);
return null;
}
}
Aggregations