Search in sources :

Example 1 with Relation

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;
		*/
}
Also used : Form(org.restlet.data.Form) ResourceException(org.restlet.resource.ResourceException) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) ResourceException(org.restlet.resource.ResourceException)

Example 2 with Relation

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();
}
Also used : CompoundClient(net.idea.opentox.cli.structure.CompoundClient) IIdentifier(net.idea.opentox.cli.id.IIdentifier) Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) Compound(net.idea.opentox.cli.structure.Compound) URL(java.net.URL) OTClient(net.idea.opentox.cli.OTClient) Identifier(net.idea.opentox.cli.id.Identifier) IIdentifier(net.idea.opentox.cli.id.IIdentifier) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) Test(org.junit.Test) ResourceTest(ambit2.rest.test.ResourceTest)

Example 3 with Relation

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);
        }
    }
}
Also used : Form(org.restlet.data.Form) STRUCTURE_RELATION(ambit2.base.relation.STRUCTURE_RELATION) SubstanceRecord(ambit2.base.data.SubstanceRecord) ResourceException(org.restlet.resource.ResourceException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) ReadSubstanceComposition(ambit2.db.substance.relation.ReadSubstanceComposition) CompositionRelation(ambit2.base.relation.composition.CompositionRelation)

Example 4 with Relation

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);
    }
}
Also used : Form(org.restlet.data.Form) STRUCTURE_RELATION(ambit2.base.relation.STRUCTURE_RELATION) SubstanceRecord(ambit2.base.data.SubstanceRecord) ResourceException(org.restlet.resource.ResourceException) ResourceException(org.restlet.resource.ResourceException) ReadSubstanceRelation(ambit2.db.substance.relation.ReadSubstanceRelation)

Example 5 with Relation

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);
        }
    }
}
Also used : Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) ReadSubstanceStudy(ambit2.db.substance.study.ReadSubstanceStudy) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) ProtocolApplication(ambit2.base.data.study.ProtocolApplication) EffectRecord(ambit2.base.data.study.EffectRecord) ResourceException(org.restlet.resource.ResourceException) Protocol(ambit2.base.data.study.Protocol)

Aggregations

CompositionRelation (ambit2.base.relation.composition.CompositionRelation)8 SubstanceRecord (ambit2.base.data.SubstanceRecord)6 Form (org.restlet.data.Form)6 ResourceException (org.restlet.resource.ResourceException)6 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)5 Property (ambit2.base.data.Property)3 STRUCTURE_RELATION (ambit2.base.relation.STRUCTURE_RELATION)3 StructureRecord (ambit2.base.data.StructureRecord)2 ProtocolApplication (ambit2.base.data.study.ProtocolApplication)2 BundleRoleFacet (ambit2.base.facet.BundleRoleFacet)2 Proportion (ambit2.base.relation.composition.Proportion)2 ReadSubstanceRelation (ambit2.db.substance.relation.ReadSubstanceRelation)2 DescriptorValueCondition (ambit2.rules.conditions.DescriptorValueCondition)2 IDescriptorValueCondition (ambit2.rules.conditions.IDescriptorValueCondition)2 Relation (ambit2.rules.conditions.value.IValue.Relation)2 Value (ambit2.rules.conditions.value.Value)2 Reference (org.restlet.data.Reference)2 ChEMBL_Property (ambit2.base.data.ChEMBLProperties.ChEMBL_Property)1 EffectRecord (ambit2.base.data.study.EffectRecord)1 IValue (ambit2.base.data.study.IValue)1