use of ambit2.rules.conditions.value.IValue.Relation in project ambit-mirror by ideaconsult.
the class QueryStructureRelationResource method createQuery.
@Override
protected Q createQuery(Context context, Request request, Response response) throws ResourceException {
Form form = getResourceRef(getRequest()).getQueryAsForm();
setTemplate(createTemplate(request.getResourceRef().getQueryAsForm()));
Object rid = request.getAttributes().get(relationKey);
if (rid != null)
try {
relation = STRUCTURE_RELATION.valueOf(rid.toString().toUpperCase());
} catch (Exception x) {
}
Object datasetURI = OpenTox.params.dataset_uri.getFirstValue(form);
if (datasetURI != null) {
String dataset = datasetURI.toString();
if (dataset.startsWith(request.getRootRef().toString())) {
if (dataset.indexOf("/dataset/") > 0) {
Object id = OpenTox.URI.dataset.getId(dataset, getRequest().getRootRef());
if (id == null)
;
else if (id instanceof Integer) {
return getQueryById((Integer) id);
} else {
return getQueryById(id.toString());
}
}
}
throw new ResourceException(Status.SERVER_ERROR_NOT_IMPLEMENTED, String.format("Dataset '%s' not supported", datasetURI));
} else
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Missing parameter dataset_uri");
/*
if (id != null) try {
id = Reference.decode(id.toString());
return getQueryById(new Integer(id.toString()));
} catch (NumberFormatException x) {
return getQueryById(id.toString());
} catch (ResourceException x) {
throw x;
} catch (Exception x) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL,x.getMessage(),x);
}
return null;
*/
}
use of ambit2.rules.conditions.value.IValue.Relation in project ambit-mirror by ideaconsult.
the class AlgorithmResourceTest method testTautomers.
@Test
public void testTautomers() throws Exception {
OTClient otclient = new OTClient();
CompoundClient cli = otclient.getCompoundClient();
Compound substance = new Compound();
substance.setName("warfarin");
String inchi = "InChI=1S/C19H16O4/c1-12(20)11-15(13-7-3-2-4-8-13)17-18(21)14-9-5-6-10-16(14)23-19(17)22/h2-10,15,21H,11H2,1H3";
String smiles = "CC(=O)CC(c1ccccc1)c2c(c3ccccc3oc2=O)O";
substance.setInChI(inchi);
substance.setSMILES(smiles);
URL serviceRoot = new URL(String.format("http://localhost:%d/", port));
net.idea.opentox.cli.task.RemoteTask task = cli.registerSubstanceAsync(serviceRoot, substance, null, null);
task.waitUntilCompleted(500);
Assert.assertNotNull(task.getResult());
Assert.assertEquals(HttpStatus.SC_OK, task.getStatus());
Assert.assertNull(task.getError());
Assert.assertTrue(task.getResult().toExternalForm().startsWith(String.format("http://localhost:%d/compound/", port)));
Form headers = new Form();
Reference model = testAsyncTask(String.format("http://localhost:%d/algorithm/tautomers", port), headers, Status.SUCCESS_OK, null);
Assert.assertEquals(getModelURI("Tautomer", port, true), model.toString());
headers.add("dataset_uri", String.format(task.getResult().toExternalForm(), port));
String expected = String.format("http://localhost:%d/query/relation/compound/HAS_TAUTOMER?dataset_uri=%s", port, Reference.encode(task.getResult().toExternalForm()));
Reference ref = testAsyncTask(model.toString(), headers, Status.SUCCESS_OK, expected);
Assert.assertEquals(expected, ref.toString());
IDatabaseConnection c = getConnection();
ITable table = c.createQueryTable("EXPECTED", "SELECT * FROM chem_relation");
Assert.assertEquals(9, table.getRowCount());
table = c.createQueryTable("EXPECTED", "SELECT * FROM src_dataset join struc_dataset using(id_srcdataset) where name='TAUTOMERS'");
Assert.assertEquals(9, table.getRowCount());
c.close();
List<IIdentifier> list = cli.listURI(new Identifier(expected));
Assert.assertEquals(9, list.size());
// now try it again
ref = testAsyncTask(model.toString(), headers, Status.SUCCESS_OK, expected);
c = getConnection();
table = c.createQueryTable("EXPECTED", "SELECT idchemical1,idchemical2,relation,metric FROM chem_relation");
Assert.assertEquals(9, table.getRowCount());
Assert.assertNotNull(table.getValue(0, "metric"));
c.close();
otclient.close();
}
use of ambit2.rules.conditions.value.IValue.Relation in project ambit-mirror by ideaconsult.
the class SubstanceCompositionResource method createQuery.
@Override
protected Q createQuery(Context context, Request request, Response response) throws ResourceException {
Form form = getRequest().getResourceRef().getQueryAsForm();
// i.e. hide hidden compositions , see 8.14 db schema
boolean showall = false;
try {
showall = Boolean.parseBoolean(form.getFirstValue("all"));
} catch (Exception x) {
showall = false;
}
Object key = request.getAttributes().get(SubstanceResource.idsubstance);
Object cmp = request.getAttributes().get(idcomposition);
if (key == null) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
} else {
CompositionRelation relation = null;
try {
if (cmp != null) {
STRUCTURE_RELATION srelation = STRUCTURE_RELATION.valueOf(cmp.toString());
relation = new CompositionRelation(null, null, srelation, null);
}
} catch (Exception x) {
}
ReadSubstanceComposition q = null;
try {
q = new ReadSubstanceComposition();
q.setExcludeHidden(!showall);
q.setFieldname(new SubstanceRecord(Integer.parseInt(key.toString())));
q.setValue(relation);
return (Q) q;
} catch (Exception x) {
int len = key.toString().trim().length();
if ((len > 40) && (len <= 45)) {
SubstanceRecord record = new SubstanceRecord();
record.setSubstanceUUID(key.toString());
q = new ReadSubstanceComposition();
q.setExcludeHidden(!showall);
q.setFieldname(record);
q.setValue(relation);
return (Q) q;
}
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
}
}
}
use of ambit2.rules.conditions.value.IValue.Relation in project ambit-mirror by ideaconsult.
the class SubstanceStructuresResource method createQuery.
@Override
protected IQueryRetrieval<IStructureRecord> createQuery(Context context, Request request, Response response) throws ResourceException {
Form form = getParams();
setTemplate(createTemplate(form));
setGroupProperties(context, request, response);
STRUCTURE_RELATION relation = null;
try {
Object cmp = request.getAttributes().get(compositionType);
relation = STRUCTURE_RELATION.valueOf(cmp.toString().toUpperCase());
} catch (Exception x) {
}
Object key = request.getAttributes().get(SubstanceResource.idsubstance);
if (key == null)
return new ReadSubstanceRelation(relation, null);
try {
return new ReadSubstanceRelation(relation, new SubstanceRecord(Integer.parseInt(key.toString())));
} catch (Exception x) {
int len = key.toString().trim().length();
if ((len > 40) && (len <= 45)) {
return new ReadSubstanceRelation(relation, new SubstanceRecord(key.toString()));
}
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
}
}
use of ambit2.rules.conditions.value.IValue.Relation in project ambit-mirror by ideaconsult.
the class SubstanceStudyResource method createQuery.
@Override
protected Q createQuery(Context context, Request request, Response response) throws ResourceException {
Object key = request.getAttributes().get(SubstanceResource.idsubstance);
if (key == null) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
} else {
substanceUUID = key.toString();
try {
Form form = getRequest().getResourceRef().getQueryAsForm();
String topCategory = form.getFirstValue("top");
String category = form.getFirstValue("category");
String property = form.getFirstValue("property");
String property_uri = form.getFirstValue("property_uri");
String document_uuid = form.getFirstValue("document_uuid");
String investigation_uuid = form.getFirstValue("investigation");
ReadSubstanceStudy q = new ReadSubstanceStudy();
q.setFieldname(substanceUUID);
if (topCategory != null || category != null || property != null || property_uri != null || investigation_uuid != null || document_uuid != null) {
Protocol p = new ambit2.base.data.study.Protocol("");
p.setTopCategory(topCategory);
p.setCategory(category);
ProtocolApplication papp = new ProtocolApplication(p);
papp.setDocumentUUID(document_uuid);
papp.setInvestigationUUID(investigation_uuid);
if (property_uri != null)
try {
// not nice REST style, but easiest to parse the URI
// not nice REST style, but easiest to parse the URI
Reference puri = new Reference(property_uri.endsWith("/") ? property_uri.substring(0, property_uri.length() - 2) : property_uri);
// the very last segment denotes protocol, then study type, then one is the endpoint hash
if (// this is the protocol
puri.getSegments().get(puri.getSegments().size() - 1).indexOf("-") > 0)
property = puri.getSegments().get(puri.getSegments().size() - 3);
else
property = puri.getSegments().get(puri.getSegments().size() - 2);
if (property.length() != 40)
property = null;
} catch (Exception x) {
}
if (property != null) {
EffectRecord effect = new EffectRecord();
effect.setSampleID(property);
papp.addEffect(effect);
}
q.setValue(papp);
}
// q.setFieldname(relation);
return (Q) q;
} catch (Exception x) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
}
}
}
Aggregations