Search in sources :

Example 1 with KisaoOntology

use of org.jlibsedml.modelsupport.KisaoOntology 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.getSedmlDocuments().get(0);
    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));
    }
}
Also used : AbstractTask(org.jlibsedml.AbstractTask) SolverDescription(cbit.vcell.solver.SolverDescription) ArrayList(java.util.ArrayList) OneStep(org.jlibsedml.OneStep) ModelResolver(org.jlibsedml.execution.ModelResolver) ArchiveModelResolver(org.jlibsedml.execution.ArchiveModelResolver) SteadyState(org.jlibsedml.SteadyState) SedML(org.jlibsedml.SedML) ArchiveComponents(org.jlibsedml.ArchiveComponents) Output(org.jlibsedml.Output) MathMappingCallbackTaskAdapter(cbit.vcell.mapping.MathMappingCallbackTaskAdapter) MathMappingCallback(cbit.vcell.mapping.SimulationContext.MathMappingCallback) BioModelsModelsRetriever(org.jlibsedml.modelsupport.BioModelsModelsRetriever) SimulationContext(cbit.vcell.mapping.SimulationContext) Algorithm(org.jlibsedml.Algorithm) FileInputStream(java.io.FileInputStream) KisaoTerm(org.jlibsedml.modelsupport.KisaoTerm) Simulation(cbit.vcell.solver.Simulation) URLResourceRetriever(org.jlibsedml.modelsupport.URLResourceRetriever) KisaoOntology(org.jlibsedml.modelsupport.KisaoOntology) ArchiveModelResolver(org.jlibsedml.execution.ArchiveModelResolver) Model(org.jlibsedml.Model) SEDMLDocument(org.jlibsedml.SEDMLDocument) DataGenerator(org.jlibsedml.DataGenerator) BioModel(cbit.vcell.biomodel.BioModel) BioModel(cbit.vcell.biomodel.BioModel) Model(org.jlibsedml.Model) UniformTimeCourse(org.jlibsedml.UniformTimeCourse) XMLSource(cbit.vcell.xml.XMLSource) Application(cbit.vcell.mapping.SimulationContext.Application)

Aggregations

BioModel (cbit.vcell.biomodel.BioModel)1 MathMappingCallbackTaskAdapter (cbit.vcell.mapping.MathMappingCallbackTaskAdapter)1 SimulationContext (cbit.vcell.mapping.SimulationContext)1 Application (cbit.vcell.mapping.SimulationContext.Application)1 MathMappingCallback (cbit.vcell.mapping.SimulationContext.MathMappingCallback)1 Simulation (cbit.vcell.solver.Simulation)1 SolverDescription (cbit.vcell.solver.SolverDescription)1 XMLSource (cbit.vcell.xml.XMLSource)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 AbstractTask (org.jlibsedml.AbstractTask)1 Algorithm (org.jlibsedml.Algorithm)1 ArchiveComponents (org.jlibsedml.ArchiveComponents)1 DataGenerator (org.jlibsedml.DataGenerator)1 Model (org.jlibsedml.Model)1 OneStep (org.jlibsedml.OneStep)1 Output (org.jlibsedml.Output)1 SEDMLDocument (org.jlibsedml.SEDMLDocument)1 SedML (org.jlibsedml.SedML)1 SteadyState (org.jlibsedml.SteadyState)1