Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Introduce the Eclipse IProgressMonitor into the OpenTox API client co…
…de, so that calls can be canceled
  • Loading branch information
egonw committed Aug 23, 2011
1 parent 2128a3d commit da208ce
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 40 deletions.
Expand Up @@ -4,16 +4,16 @@
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;

import net.bioclipse.cdk.domain.ICDKMolecule;
import net.bioclipse.ds.model.AbstractDSTest;
import net.bioclipse.ds.model.DSException;
import net.bioclipse.ds.model.ITestResult;
import net.bioclipse.opentox.Activator;
import net.bioclipse.opentox.OpenToxService;
import net.bioclipse.opentox.business.IOpentoxManager;
import net.bioclipse.opentox.business.OpentoxManager;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;

/**
* DSModel for predicting an OpenTox model
Expand All @@ -25,7 +25,7 @@ public class OpenToxModel extends AbstractDSTest {

private static final Logger logger = Logger.getLogger(OpenToxModel.class);

IOpentoxManager opentox;
OpentoxManager opentox;
private String model;

public OpenToxModel(String model) {
Expand All @@ -34,7 +34,7 @@ public OpenToxModel(String model) {

@Override
public void initialize(IProgressMonitor monitor) throws DSException {
opentox=Activator.getDefault().getJavaOpentoxManager();
opentox = new OpentoxManager();
}

@Override
Expand Down Expand Up @@ -69,7 +69,7 @@ protected List<? extends ITestResult> doRunTest(ICDKMolecule cdkmol,
logger.debug(" - Model: " + model + " retry number " + i);

try{
OTres = opentox.predictWithModelWithLabel(service, model, cdkmol);
OTres = opentox.predictWithModelWithLabel(service, model, cdkmol, monitor);

}catch(Exception e){
logger.error(" == Opentox model calculation failed for: " + model);
Expand Down
Expand Up @@ -41,6 +41,8 @@
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.openscience.cdk.AtomContainer;
import org.openscience.cdk.io.SDFWriter;

Expand Down Expand Up @@ -281,37 +283,42 @@ public static void addMolecules(String datasetURI, String sdFile)
method.releaseConnection();
}

public static String createNewDataset(String service, List<IMolecule> molecules)
public static String createNewDataset(String service,
List<IMolecule> molecules, IProgressMonitor monitor)
throws Exception {
StringWriter strWriter = new StringWriter();
SDFWriter writer = new SDFWriter(strWriter);
for (IMolecule mol : molecules) {
writer.write(cdk.asCDKMolecule(mol).getAtomContainer());
}
writer.close();
return createNewDataset(normalizeURI(service), strWriter.toString());
return createNewDataset(normalizeURI(service), strWriter.toString(), monitor);
}

public static String createNewDataset(String service, IMolecule mol)
public static String createNewDataset(String service, IMolecule mol,
IProgressMonitor monitor)
throws Exception {
StringWriter strWriter = new StringWriter();
SDFWriter writer = new SDFWriter(strWriter);
writer.write(cdk.asCDKMolecule(mol).getAtomContainer());
writer.close();
return createNewDataset(service, strWriter.toString());
return createNewDataset(service, strWriter.toString(), monitor);
}

public static String createNewDataset(String service)
public static String createNewDataset(String service, IProgressMonitor monitor)
throws Exception {
StringWriter strWriter = new StringWriter();
SDFWriter writer = new SDFWriter(strWriter);
writer.write(new AtomContainer());
writer.close();
return createNewDataset(service, strWriter.toString());
return createNewDataset(service, strWriter.toString(), monitor);
}

public static String createNewDataset(String service, String sdFile)
public static String createNewDataset(
String service, String sdFile, IProgressMonitor monitor)
throws Exception {
if (monitor == null) monitor = new NullProgressMonitor();

HttpClient client = new HttpClient();
PostMethod method = new PostMethod(service + "dataset");
HttpMethodHelper.addMethodHeaders(method,
Expand All @@ -327,19 +334,27 @@ public static String createNewDataset(String service, String sdFile)
String dataset = "";
String responseString = method.getResponseBodyAsString();
logger.debug("Response: " + responseString);
int tailing = 1;
if (status == 200 || status == 202) {
if (responseString.contains("/task/")) {
logger.debug("Task: " + responseString);
// OK, we got a task... let's wait until it is done
String task = method.getResponseBodyAsString();
Thread.sleep(1000); // let's be friendly, and wait 1 sec
TaskState state = Task.getState(task);
while (!state.isFinished()) {
Thread.sleep(3000); // let's be friendly, and wait 3 sec
while (!state.isFinished() && !monitor.isCanceled()) {
// let's be friendly, and wait 2 secs and a bit and increase
// that time after each wait
int waitingTime = andABit(2000*tailing);
logger.debug("Waiting " + waitingTime + "ms.");
Thread.sleep(waitingTime);
state = Task.getState(task);
if (state.isRedirected()) {
task = state.getResults();
logger.debug(" new task, new task!!: " + task);
}
// but wait at most 20 secs and a bit
if (tailing < 10) tailing++;
}
// OK, it should be finished now
dataset = state.getResults();
Expand All @@ -350,17 +365,22 @@ public static String createNewDataset(String service, String sdFile)
}
}
method.releaseConnection();
if (monitor.isCanceled()) return "";
logger.debug("Data set: " + dataset);
dataset = dataset.replaceAll("\n", "");
return dataset;
}

private static int andABit(int minimum) {
return (minimum + (int)Math.round(minimum*Math.random()));
}

public static void main(String[] args) throws Exception {
// String service = "http://194.141.0.136:8080/";
String service = "http://apps.ideaconsult.net:8080/ambit2/";
// List<String> sets = getListOfAvailableDatasets(service);
// for (String set : sets) System.out.println(set);
String dataset = createNewDataset(service);
String dataset = createNewDataset(service, null);
List<IMolecule> mols = new ArrayList<IMolecule>();
mols.add(cdk.fromSMILES("COC"));
mols.add(cdk.fromSMILES("CNC"));
Expand Down
Expand Up @@ -26,13 +26,18 @@
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

public abstract class ModelAlgorithm extends Algorithm {

private static final Logger logger = Logger.getLogger(ModelAlgorithm.class);

public static String calculate(String service, String model, String dataSetURI)
public static String calculate(String service, String model,
String dataSetURI, IProgressMonitor monitor)
throws HttpException, IOException, InterruptedException {
if (monitor == null) monitor = new NullProgressMonitor();

HttpClient client = new HttpClient();
dataSetURI = Dataset.normalizeURI(dataSetURI);
PostMethod method = new PostMethod(model);
Expand All @@ -55,10 +60,11 @@ public static String calculate(String service, String model, String dataSetURI)
logger.debug("response: " + task);
Thread.sleep(andABit(500)); // let's be friendly, and wait 1 sec
TaskState state = Task.getState(task);
while (!state.isFinished()) {
while (!state.isFinished() && !monitor.isCanceled()) {
// let's be friendly, and wait 2 secs and a bit and increase
// that time after each wait
Thread.sleep(andABit(2000*tailing));
int waitingTime = andABit(2000*tailing);
logger.debug("Waiting " + waitingTime + "ms.");
state = Task.getState(task);
if (state.isRedirected()) {
task = state.getResults();
Expand Down
Expand Up @@ -26,13 +26,18 @@
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

public abstract class MolecularDescriptorAlgorithm extends Algorithm {

private static final Logger logger = Logger.getLogger(MolecularDescriptorAlgorithm.class);

public static String calculate(String service, String descriptor, String dataSetURI)
public static String calculate(String service, String descriptor,
String dataSetURI, IProgressMonitor monitor)
throws HttpException, IOException, InterruptedException {
if (monitor == null) monitor = new NullProgressMonitor();

HttpClient client = new HttpClient();
dataSetURI = Dataset.normalizeURI(dataSetURI);
PostMethod method = new PostMethod(descriptor);
Expand All @@ -58,7 +63,7 @@ public static String calculate(String service, String descriptor, String dataSet
logger.debug("OK, we got a task assigned: " + task);
Thread.sleep(andABit(500)); // let's be friendly, and wait 1 sec
TaskState state = Task.getState(task);
while (!state.isFinished()) {
while (!state.isFinished() && !monitor.isCanceled()) {
// let's be friendly, and wait 2 secs and a bit and increase
// that time after each wait
int waitingTime = andABit(2000*tailing);
Expand Down
Expand Up @@ -477,7 +477,7 @@ public void createDataset(String service, IReturner<String> returner, IProgressM

monitor.beginTask("Creating an OpenTox API data set ...", 1);
try {
String dataset = Dataset.createNewDataset(service);
String dataset = Dataset.createNewDataset(service, monitor);
monitor.done();
returner.completeReturn( dataset );
} catch (Exception exc) {
Expand All @@ -493,7 +493,7 @@ public void createDataset(String service, List<IMolecule> molecules, IReturner<S

monitor.beginTask("Creating an OpenTox API data set ...", 1);
try {
String dataset = Dataset.createNewDataset(service, molecules);
String dataset = Dataset.createNewDataset(service, molecules, monitor);
monitor.done();
returner.completeReturn( dataset );
} catch (Exception exc) {
Expand All @@ -509,7 +509,7 @@ public void createDataset(String service, IMolecule molecule, IReturner<String>

monitor.beginTask("Creating an OpenTox API data set ...", 1);
try {
String dataset = Dataset.createNewDataset(service, molecule);
String dataset = Dataset.createNewDataset(service, molecule, monitor);
monitor.done();
returner.completeReturn( dataset );
} catch (Exception exc) {
Expand Down Expand Up @@ -616,8 +616,12 @@ public List<String> calculateDescriptor(

List<String> calcResults = new ArrayList<String>();
for (IMolecule molecule : molecules) {
String dataset = Dataset.createNewDataset(service, molecule);
String results = MolecularDescriptorAlgorithm.calculate(service, descriptor, dataset);
String dataset = Dataset.createNewDataset(service, molecule, monitor);
if (monitor.isCanceled()) continue;
String results = MolecularDescriptorAlgorithm.calculate(
service, descriptor, dataset, monitor
);
if (monitor.isCanceled()) continue;
StringMatrix features = Dataset.listPredictedFeatures(results);
calcResults.addAll(removeDataType(features.getColumn("numval")));
Dataset.deleteDataset(dataset);
Expand All @@ -636,9 +640,13 @@ public List<String> calculateDescriptor(

List<String> calcResults = new ArrayList<String>();
logger.debug("Creating data set");
String dataset = Dataset.createNewDataset(service, molecule);
String dataset = Dataset.createNewDataset(service, molecule, monitor);
logger.debug("Calculating descriptor");
String results = MolecularDescriptorAlgorithm.calculate(service, descriptor, dataset);
if (monitor.isCanceled()) return Collections.emptyList();
String results = MolecularDescriptorAlgorithm.calculate(
service, descriptor, dataset, monitor
);
if (monitor.isCanceled()) return Collections.emptyList();
logger.debug("Listing features");
StringMatrix features = Dataset.listPredictedFeatures(results);
logger.debug("Pred: " + features);
Expand All @@ -660,8 +668,10 @@ public List<String> predictWithModel(String service, String model, List<IMolecul

List<String> calcResults = new ArrayList<String>();
for (IMolecule molecule : molecules) {
String dataset = Dataset.createNewDataset(service, molecule);
String results = ModelAlgorithm.calculate(service, model, dataset);
String dataset = Dataset.createNewDataset(service, molecule, monitor);
if (monitor.isCanceled()) return calcResults;
String results = ModelAlgorithm.calculate(service, model, dataset, monitor);
if (monitor.isCanceled()) return calcResults;
StringMatrix features = Dataset.listPredictedFeatures(results);
calcResults.addAll(removeDataType(features.getColumn("numval")));
Dataset.deleteDataset(dataset);
Expand All @@ -671,7 +681,8 @@ public List<String> predictWithModel(String service, String model, List<IMolecul
return calcResults;
}

public Map<String,String> predictWithModelWithLabel(String service, String model, List<IMolecule> molecules, IProgressMonitor monitor)
public Map<String,String> predictWithModelWithLabel(String service, String model,
List<IMolecule> molecules, IProgressMonitor monitor)
throws Exception {
if (service == null) throw new BioclipseException("Service is null");
if (model == null) throw new BioclipseException("Model is null");
Expand All @@ -681,8 +692,10 @@ public Map<String,String> predictWithModelWithLabel(String service, String model

Map<String,String> calcResults = new HashMap<String, String>();
for (IMolecule molecule : molecules) {
String dataset = Dataset.createNewDataset(service, molecule);
String results = ModelAlgorithm.calculate(service, model, dataset);
String dataset = Dataset.createNewDataset(service, molecule, monitor);
if (monitor.isCanceled()) return calcResults;
String results = ModelAlgorithm.calculate(service, model, dataset, monitor);
if (monitor.isCanceled()) return calcResults;
StringMatrix features = Dataset.listPredictedFeatures(results);
List<String> fcol = removeDataType(features.getColumn("numval"));
List<String> lcol = features.getColumn("desc");
Expand All @@ -697,7 +710,8 @@ public Map<String,String> predictWithModelWithLabel(String service, String model
return calcResults;
}

public List<String> predictWithModel(String service, String model, IMolecule molecule, IProgressMonitor monitor)
public List<String> predictWithModel(String service, String model,
IMolecule molecule, IProgressMonitor monitor)
throws Exception {
if (service == null) throw new BioclipseException("Service is null");
if (model == null) throw new BioclipseException("Model is null");
Expand All @@ -706,8 +720,10 @@ public List<String> predictWithModel(String service, String model, IMolecule mol
monitor.beginTask("Calculate model for molecule", 1);

List<String> calcResults = new ArrayList<String>();
String dataset = Dataset.createNewDataset(service, molecule);
String results = ModelAlgorithm.calculate(service, model, dataset);
String dataset = Dataset.createNewDataset(service, molecule, monitor);
if (monitor.isCanceled()) return calcResults;
String results = ModelAlgorithm.calculate(service, model, dataset, monitor);
if (monitor.isCanceled()) return calcResults;
StringMatrix features = Dataset.listPredictedFeatures(results);
calcResults.addAll(removeDataType(features.getColumn("numval")));
Dataset.deleteDataset(dataset);
Expand All @@ -716,7 +732,8 @@ public List<String> predictWithModel(String service, String model, IMolecule mol
return calcResults;
}

public Map<String,String> predictWithModelWithLabel(String service, String model, IMolecule molecule, IProgressMonitor monitor)
public Map<String,String> predictWithModelWithLabel(String service, String model,
IMolecule molecule, IProgressMonitor monitor)
throws Exception {
if (service == null) throw new BioclipseException("Service is null");
if (model == null) throw new BioclipseException("Model is null");
Expand All @@ -725,8 +742,10 @@ public Map<String,String> predictWithModelWithLabel(String service, String model
monitor.beginTask("Calculate model for molecule", 1);

Map<String,String> calcResults = new HashMap<String, String>();
String dataset = Dataset.createNewDataset(service, molecule);
String results = ModelAlgorithm.calculate(service, model, dataset);
String dataset = Dataset.createNewDataset(service, molecule, monitor);
if (monitor.isCanceled()) return calcResults;
String results = ModelAlgorithm.calculate(service, model, dataset, monitor);
if (monitor.isCanceled()) return calcResults;
StringMatrix features = Dataset.listPredictedFeatures(results);
List<String> fcol = removeDataType(features.getColumn("numval"));
List<String> lcol = features.getColumn("label");
Expand Down

0 comments on commit da208ce

Please sign in to comment.