Search in sources :

Example 6 with VCellApiClient

use of org.vcell.api.client.VCellApiClient in project vcell by virtualcell.

the class CopasiOptimizationSolver method solveRemoteApi.

public static OptimizationResultSet solveRemoteApi(ParameterEstimationTaskSimulatorIDA parestSimulator, ParameterEstimationTask parameterEstimationTask, CopasiOptSolverCallbacks optSolverCallbacks, MathMappingCallback mathMappingCallback) throws IOException, ExpressionException, OptimizationException {
    try {
        OptProblem optProblem = CopasiServicePython.makeOptProblem(parameterEstimationTask);
        boolean bIgnoreCertProblems = true;
        boolean bIgnoreHostMismatch = true;
        // e.g. vcell.serverhost=vcellapi.cam.uchc.edu:8080
        String serverHost = PropertyLoader.getRequiredProperty(PropertyLoader.vcellServerHost);
        String[] parts = serverHost.split(":");
        String host = parts[0];
        int port = Integer.parseInt(parts[1]);
        VCellApiClient apiClient = new VCellApiClient(host, port, bIgnoreCertProblems, bIgnoreHostMismatch);
        TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
        String optProblemJson = serializer.toString(optProblem);
        String optimizationId = apiClient.submitOptimization(optProblemJson);
        // 20 second minute timeout
        final long TIMEOUT_MS = 1000 * 20;
        long startTime = System.currentTimeMillis();
        OptRun optRun = null;
        while ((System.currentTimeMillis() - startTime) < TIMEOUT_MS) {
            if (optSolverCallbacks.getStopRequested()) {
                throw new RuntimeException(STOP_REQUESTED);
            }
            String optRunJson = apiClient.getOptRunJson(optimizationId);
            TDeserializer deserializer = new TDeserializer(new TJSONProtocol.Factory());
            optRun = new OptRun();
            deserializer.deserialize(optRun, optRunJson.getBytes());
            OptRunStatus status = optRun.status;
            if (status == OptRunStatus.Complete) {
                System.out.println("job " + optimizationId + ": status " + status + " " + optRun.getOptResultSet().toString());
                break;
            }
            if (status == OptRunStatus.Failed) {
                throw new RuntimeException("optimization failed, message=" + optRun.statusMessage);
            }
            System.out.println("job " + optimizationId + ": status " + status);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        System.out.println("done with optimization");
        OptResultSet optResultSet = optRun.getOptResultSet();
        int numFittedParameters = optResultSet.getOptParameterValues().size();
        String[] paramNames = new String[numFittedParameters];
        double[] paramValues = new double[numFittedParameters];
        for (int pIndex = 0; pIndex < numFittedParameters; pIndex++) {
            OptParameterValue optParamValue = optResultSet.getOptParameterValues().get(pIndex);
            paramNames[pIndex] = optParamValue.parameterName;
            paramValues[pIndex] = optParamValue.bestValue;
        }
        OptimizationStatus status = new OptimizationStatus(OptimizationStatus.NORMAL_TERMINATION, optRun.statusMessage);
        OptRunResultSet optRunResultSet = new OptRunResultSet(paramValues, optResultSet.objectiveFunction, optResultSet.numFunctionEvaluations, status);
        OptSolverResultSet copasiOptSolverResultSet = new OptSolverResultSet(paramNames, optRunResultSet);
        RowColumnResultSet copasiRcResultSet = parestSimulator.getRowColumnRestultSetByBestEstimations(parameterEstimationTask, paramNames, paramValues);
        OptimizationResultSet copasiOptimizationResultSet = new OptimizationResultSet(copasiOptSolverResultSet, copasiRcResultSet);
        System.out.println("-----------SOLUTION FROM VCellAPI---------------\n" + optResultSet.toString());
        return copasiOptimizationResultSet;
    } catch (Exception e) {
        e.printStackTrace(System.out);
        if (e.getMessage() != null && e.getMessage().equals(STOP_REQUESTED)) {
            throw UserCancelException.CANCEL_GENERIC;
        }
        throw new OptimizationException(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
    }
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) TDeserializer(org.apache.thrift.TDeserializer) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) OptParameterValue(org.vcell.optimization.thrift.OptParameterValue) OptSolverResultSet(cbit.vcell.opt.OptSolverResultSet) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) OptRunStatus(org.vcell.optimization.thrift.OptRunStatus) OptRun(org.vcell.optimization.thrift.OptRun) OptResultSet(org.vcell.optimization.thrift.OptResultSet) OptimizationStatus(cbit.vcell.opt.OptimizationStatus) RowColumnResultSet(cbit.vcell.math.RowColumnResultSet) OptProblem(org.vcell.optimization.thrift.OptProblem) VCellApiClient(org.vcell.api.client.VCellApiClient) OptimizationException(cbit.vcell.opt.OptimizationException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) UserCancelException(org.vcell.util.UserCancelException) TSerializer(org.apache.thrift.TSerializer) OptRunResultSet(cbit.vcell.opt.OptSolverResultSet.OptRunResultSet)

Example 7 with VCellApiClient

use of org.vcell.api.client.VCellApiClient in project vcell by virtualcell.

the class VCellApiClientTest method main.

/**
 * @param args
 */
public static void main(String[] args) {
    VCellApiClient vcellApiClient = null;
    try {
        if (args.length != 4 && args.length != 5) {
            System.out.println("usage: VCellApiClient host port userid password [clientID]");
            System.exit(1);
        }
        String host = args[0];
        int port = Integer.parseInt(args[1]);
        String username = args[2];
        String password = args[3];
        boolean bIgnoreCertProblems = true;
        boolean bIgnoreHostMismatch = true;
        vcellApiClient = new VCellApiClient(host, port, bIgnoreCertProblems, bIgnoreHostMismatch);
        vcellApiClient.authenticate(username, password, false);
        // test /biomodel[? query string]
        BiomodelRepresentation[] biomodelReps = vcellApiClient.getBioModels(new BioModelsQuerySpec());
        for (BiomodelRepresentation biomodelRep : biomodelReps) {
            System.out.println("biomodel : " + biomodelRep.getBmKey() + " : " + biomodelRep.getName());
            for (ApplicationRepresentation appRep : biomodelRep.getApplications()) {
                System.out.println("   app : " + appRep.getName());
            }
            for (SimulationRepresentation simRep : biomodelRep.getSimulations()) {
                System.out.println("   sim : " + simRep.getName());
            }
        }
        boolean bFirstSimulationToStartStop = true;
        if (biomodelReps.length > 0) {
            // test /biomodel/[bmkey]
            System.out.println(" ... re-fetching first biomodel owned by me ...");
            BioModelsQuerySpec bioModelsQuerySpec = new BioModelsQuerySpec();
            bioModelsQuerySpec.owner = username;
            BiomodelRepresentation firstBiomodelRep = vcellApiClient.getBioModels(bioModelsQuerySpec)[0];
            System.out.println("biomodel : " + firstBiomodelRep.getBmKey() + " : " + firstBiomodelRep.getName());
            for (ApplicationRepresentation appRep : firstBiomodelRep.getApplications()) {
                System.out.println("   appRep : " + appRep.getName());
            }
            for (SimulationRepresentation simRep : firstBiomodelRep.getSimulations()) {
                System.out.println("   simRep (returned with BioModelRep) : " + simRep.getKey() + " : " + simRep.getName());
                // test /biomodel/[bmkey]/simulation/simkey
                SimulationRepresentation simulation = vcellApiClient.getSimulation(firstBiomodelRep.getBmKey(), simRep.getKey());
                System.out.println("   simRep (retrieved separately) : " + simulation.getKey() + " : " + simulation.getName());
                if (bFirstSimulationToStartStop) {
                    bFirstSimulationToStartStop = false;
                    // test /biomodel/[bmkey]/simulation/[simkey]/startSimulation
                    SimTasksQuerySpec simTasksQuerySpec = new SimTasksQuerySpec();
                    simTasksQuerySpec.simId = simRep.getKey();
                    SimulationTaskRepresentation[] beforeStartSimTasks = vcellApiClient.getSimTasks(simTasksQuerySpec);
                    System.out.println("SENDING START SIMULATION");
                    SimulationTaskRepresentation[] justAfterStartSimTasks = vcellApiClient.startSimulation(firstBiomodelRep.getBmKey(), simRep.getKey());
                    System.out.println("SENT START SIMULATION");
                    System.out.println("WAITING 5 seconds");
                    try {
                        Thread.sleep(5000);
                    } catch (Exception e) {
                    }
                    SimulationTaskRepresentation[] longAfterStartSimTasks = vcellApiClient.getSimTasks(simTasksQuerySpec);
                    System.out.println("SENDING STOP SIMULATION");
                    SimulationTaskRepresentation[] justAfterStopSimTasks = vcellApiClient.stopSimulation(firstBiomodelRep.getBmKey(), simRep.getKey());
                    System.out.println("SENT STOP SIMULATION");
                    System.out.println("WAITING 5 seconds");
                    try {
                        Thread.sleep(5000);
                    } catch (Exception e) {
                    }
                    SimulationTaskRepresentation[] longAfterStopSimTasks = vcellApiClient.getSimTasks(simTasksQuerySpec);
                    System.out.println("\n\nsimulation status:");
                    for (SimulationTaskRepresentation simTaskRep : beforeStartSimTasks) {
                        System.out.println("    BEFORE START Job = " + simTaskRep.getJobIndex() + ", Task = " + simTaskRep.getTaskId() + ", Status = " + simTaskRep.getStatus());
                    }
                    for (SimulationTaskRepresentation simTaskRep : justAfterStartSimTasks) {
                        System.out.println("    JUST AFTER START Job = " + simTaskRep.getJobIndex() + ", Task = " + simTaskRep.getTaskId() + ", Status = " + simTaskRep.getStatus());
                    }
                    for (SimulationTaskRepresentation simTaskRep : longAfterStartSimTasks) {
                        System.out.println("    LONG AFTER START Job = " + simTaskRep.getJobIndex() + ", Task = " + simTaskRep.getTaskId() + ", Status = " + simTaskRep.getStatus());
                    }
                    for (SimulationTaskRepresentation simTaskRep : justAfterStopSimTasks) {
                        System.out.println("    JUST AFTER STOP Job = " + simTaskRep.getJobIndex() + ", Task = " + simTaskRep.getTaskId() + ", Status = " + simTaskRep.getStatus());
                    }
                    for (SimulationTaskRepresentation simTaskRep : longAfterStopSimTasks) {
                        System.out.println("    LONG AFTER STOP Job = " + simTaskRep.getJobIndex() + ", Task = " + simTaskRep.getTaskId() + ", Status = " + simTaskRep.getStatus());
                    }
                    System.out.println("\n\n");
                }
                System.out.println("\n");
            }
        }
        // test /simtask
        SimulationTaskRepresentation[] simTaskReps = vcellApiClient.getSimTasks(new SimTasksQuerySpec());
        for (SimulationTaskRepresentation simTaskRep : simTaskReps) {
            System.out.println("simTask : " + simTaskRep.getSimKey() + " : " + simTaskRep.getSimName());
        }
    } catch (Throwable e) {
        e.printStackTrace(System.out);
    } finally {
        if (vcellApiClient != null) {
            try {
                vcellApiClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : BiomodelRepresentation(org.vcell.api.common.BiomodelRepresentation) IOException(java.io.IOException) VCellApiClient(org.vcell.api.client.VCellApiClient) IOException(java.io.IOException) SimulationRepresentation(org.vcell.api.common.SimulationRepresentation) SimulationTaskRepresentation(org.vcell.api.common.SimulationTaskRepresentation) SimTasksQuerySpec(org.vcell.api.client.query.SimTasksQuerySpec) ApplicationRepresentation(org.vcell.api.common.ApplicationRepresentation) BioModelsQuerySpec(org.vcell.api.client.query.BioModelsQuerySpec)

Example 8 with VCellApiClient

use of org.vcell.api.client.VCellApiClient in project vcell by virtualcell.

the class VCellModelService method getModels.

public Task<List<VCellModel>, String> getModels(VCellService vCellService) {
    final Task<List<VCellModel>, String> task = new Task<List<VCellModel>, String>() {

        @Override
        protected List<VCellModel> doInBackground() throws Exception {
            boolean bIgnoreCertProblems = true;
            boolean bIgnoreHostMismatch = true;
            VCellApiClient vCellApiClient = null;
            List<VCellModel> vCellModels = new ArrayList<VCellModel>();
            try {
                vCellApiClient = new VCellApiClient(HOST, PORT, bIgnoreCertProblems, bIgnoreHostMismatch);
                vCellApiClient.authenticate("ImageJ", "richarddberlin", false);
                BioModelsQuerySpec querySpec = new BioModelsQuerySpec();
                querySpec.owner = "tutorial";
                final BiomodelRepresentation[] biomodelReps = vCellApiClient.getBioModels(querySpec);
                final int modelsToLoad = biomodelReps.length;
                int modelsLoaded = 0;
                for (BiomodelRepresentation biomodelRep : biomodelReps) {
                    setSubtask(biomodelRep.getName());
                    ApplicationRepresentation[] applicationReps = biomodelRep.getApplications();
                    if (applicationReps.length > 0) {
                        String vcml = getVCML(biomodelRep);
                        if (vcml != null) {
                            SBMLDocument sbml = vCellService.getSBML(vcml, applicationReps[0].getName());
                            VCellModel vCellModel = new VCellModel(biomodelRep.getName(), biomodelRep.getBmKey(), sbml);
                            vCellModels.add(vCellModel);
                            modelsLoaded++;
                            setProgress(modelsLoaded * 100 / modelsToLoad);
                        } else {
                            System.err.println("failed to return VCML for " + biomodelRep.bmKey);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace(System.out);
            }
            return vCellModels;
        }
    };
    return task;
}
Also used : BiomodelRepresentation(org.vcell.api.common.BiomodelRepresentation) Task(org.vcell.imagej.common.gui.Task) SBMLDocument(org.sbml.jsbml.SBMLDocument) ArrayList(java.util.ArrayList) VCellApiClient(org.vcell.api.client.VCellApiClient) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ApplicationRepresentation(org.vcell.api.common.ApplicationRepresentation) ArrayList(java.util.ArrayList) List(java.util.List) BioModelsQuerySpec(org.vcell.api.client.query.BioModelsQuerySpec)

Aggregations

VCellApiClient (org.vcell.api.client.VCellApiClient)8 IOException (java.io.IOException)7 KeyManagementException (java.security.KeyManagementException)4 KeyStoreException (java.security.KeyStoreException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 RemoteProxyException (cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException)2 ArrayList (java.util.ArrayList)2 TDeserializer (org.apache.thrift.TDeserializer)2 TSerializer (org.apache.thrift.TSerializer)2 TJSONProtocol (org.apache.thrift.protocol.TJSONProtocol)2 BioModelsQuerySpec (org.vcell.api.client.query.BioModelsQuerySpec)2 ApplicationRepresentation (org.vcell.api.common.ApplicationRepresentation)2 BiomodelRepresentation (org.vcell.api.common.BiomodelRepresentation)2 OptProblem (org.vcell.optimization.thrift.OptProblem)2 OptRun (org.vcell.optimization.thrift.OptRun)2 OptRunStatus (org.vcell.optimization.thrift.OptRunStatus)2 MathException (cbit.vcell.math.MathException)1 RowColumnResultSet (cbit.vcell.math.RowColumnResultSet)1 OptSolverResultSet (cbit.vcell.opt.OptSolverResultSet)1 OptRunResultSet (cbit.vcell.opt.OptSolverResultSet.OptRunResultSet)1