Search in sources :

Example 1 with Protocol

use of ambit2.base.data.study.Protocol in project ambit-mirror by ideaconsult.

the class SubstanceStudyReporterTest method main.

public static void main(String[] args) {
    Substance2BucketJsonReporter reporter = new Substance2BucketJsonReporter(null, null, Substance2BucketJsonReporter._JSON_MODE.substance, null, null, null);
    Reader in = null;
    try {
        OutputStreamWriter writer;
        if (args.length < 2)
            writer = new OutputStreamWriter(System.out);
        else
            writer = new OutputStreamWriter(new FileOutputStream(new File(args[1])));
        reporter.setOutput(writer);
        in = new FileReader(new File(args[0]));
        RawIteratingCSVReader reader = new RawIteratingCSVReader(in, CSVFormat.TDF) {

            protected SubstanceRecord prevrecord = null;

            protected String getRecordid(CSVRecord record) {
                return record.get(0);
            }

            protected String getExternalId(CSVRecord record) {
                return record.get(1);
            }

            protected String getExternalIdSystem(CSVRecord record) {
                return record.get(5);
            }

            protected String getAssayid(CSVRecord record) {
                return record.get(6);
            }

            protected String getSummaryActivity(CSVRecord record) {
                return record.get(3);
            }

            protected double getActivityValue(CSVRecord record) throws NumberFormatException {
                return Double.parseDouble(record.get(4));
            }

            protected String getOrthologgroup(CSVRecord record) {
                return record.get(9);
            }

            protected String getGeneSymbol(CSVRecord record) {
                return record.get(8);
            }

            protected String getSpecies(CSVRecord record) {
                return record.get(7);
            }

            @Override
            protected IStructureRecord transform(CSVRecord record) {
                SubstanceRecord substance = prevrecord;
                String id = getRecordid(record);
                if (prevrecord == null || !id.equals(prevrecord.getSubstanceName())) {
                    substance = new SubstanceRecord();
                    substance.setContent(null);
                    // substance.setSubstancetype("standardized");
                    substance.setSubstanceUUID(I5Utils.getPrefixedUUID("PC", UUID.nameUUIDFromBytes(id.getBytes())));
                    substance.setSubstanceName(id);
                    prevrecord = substance;
                }
                structureRecord = substance;
                String externalid = getExternalId(record);
                String externaldb = getExternalIdSystem(record);
                ExternalIdentifier eid = new ExternalIdentifier(externaldb, externalid);
                boolean newid = true;
                if (substance.getExternalids() == null)
                    substance.setExternalids(new ArrayList<ExternalIdentifier>());
                else
                    for (ExternalIdentifier e : substance.getExternalids()) if (e.getSystemDesignator().equals(eid.getSystemDesignator()) && e.getSystemIdentifier().equals(eid.getSystemIdentifier())) {
                        newid = false;
                        break;
                    }
                if (newid)
                    substance.getExternalids().add(eid);
                String assayid = getAssayid(record);
                Protocol p = new Protocol(String.format("%s", externalid));
                p.setTopCategory(null);
                p.setCategory(null);
                // p.addGuideline(String.format("%s_AID%s", externaldb,assayid));
                ProtocolApplication<Protocol, IParams, String, IParams, String> papp = new ProtocolApplication<Protocol, IParams, String, IParams, String>(p);
                // papp.setDocumentUUID(I5Utils.getPrefixedUUID("PC",UUID.nameUUIDFromBytes(p.getEndpoint().getBytes())));
                papp.setDocumentUUID(null);
                papp.setInterpretationResult(getSummaryActivity(record));
                papp.setReference(String.format("AID%s", assayid));
                papp.setReferenceOwner(externaldb);
                IParams params = new Params();
                params.put("gene", getGeneSymbol(record));
                params.put("taxid", "TaxId:" + getSpecies(record));
                // params.put("OG_GENE", String.format("OG%s_%s", record.get(9),	record.get(8)));
                params.put("og", "OG" + getOrthologgroup(record));
                params.put("ez", "entrez:" + record.get(2));
                papp.setParameters(params);
                try {
                    EffectRecord<String, IParams, String> effect = new EffectRecord<String, IParams, String>();
                    effect.setEndpoint("pXC50");
                    effect.setIdresult(1);
                    double value = getActivityValue(record);
                    effect.setLoValue(value);
                    effect.setUnit("nM");
                    papp.addEffect(effect);
                } catch (Exception x) {
                // x.printStackTrace();
                }
                substance.addMeasurement(papp);
                return structureRecord;
            }
        };
        int n = 0;
        IStructureRecord prevrecord = null;
        reporter.header(writer, null);
        while (reader.hasNext()) {
            IStructureRecord record = reader.nextRecord();
            if (prevrecord != null && (prevrecord != record)) {
                reporter.processItem((SubstanceRecord) prevrecord);
                writer.flush();
            }
            n++;
            prevrecord = record;
            if ((n % 100000) == 0) {
                System.err.println();
                System.err.print(n);
            } else if ((n % 10000) == 0)
                System.err.print(".");
        }
        reporter.processItem((SubstanceRecord) prevrecord);
        reporter.footer(writer, null);
        writer.flush();
    } catch (Exception x) {
        x.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception x) {
        }
        try {
            reporter.close();
        } catch (Exception x) {
        }
    }
}
Also used : RawIteratingCSVReader(ambit2.core.io.RawIteratingCSVReader) ExternalIdentifier(ambit2.base.data.substance.ExternalIdentifier) ArrayList(java.util.ArrayList) RawIteratingCSVReader(ambit2.core.io.RawIteratingCSVReader) Reader(java.io.Reader) FileReader(java.io.FileReader) SubstanceRecord(ambit2.base.data.SubstanceRecord) IParams(ambit2.base.data.study.IParams) Params(ambit2.base.data.study.Params) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ProtocolApplication(ambit2.base.data.study.ProtocolApplication) EffectRecord(ambit2.base.data.study.EffectRecord) FileOutputStream(java.io.FileOutputStream) Substance2BucketJsonReporter(ambit2.rest.substance.study.Substance2BucketJsonReporter) IParams(ambit2.base.data.study.IParams) OutputStreamWriter(java.io.OutputStreamWriter) FileReader(java.io.FileReader) CSVRecord(org.apache.commons.csv.CSVRecord) Protocol(ambit2.base.data.study.Protocol) File(java.io.File)

Example 2 with Protocol

use of ambit2.base.data.study.Protocol in project ambit-mirror by ideaconsult.

the class CallableSubstancePropertyCreatorTest method testCreateSubstanceProperty.

@Test
public void testCreateSubstanceProperty() throws Exception {
    Form form = new Form();
    form.add("endpointcategory", Protocol._categories.ZETA_POTENTIAL_SECTION.name());
    form.add("name", "ZETA POTENTIAL");
    form.add("unit", "mV");
    form.add("protocol", "Method: other: no data");
    Params p = new Params();
    Value v = new Value();
    v.setLoValue(4.26);
    p.put("pH", v);
    Assert.assertEquals("{\"pH\":{\"loValue\":4.26}}", p.toString());
    try {
        PropertyURIReporter reporter = new PropertyURIReporter(new Reference("http://localhost:8081/ambit2"));
        CallableSubstancePropertyCreator callable = new CallableSubstancePropertyCreator(reporter, Method.POST, form, null, null);
        TaskResult task = callable.call();
        Assert.assertTrue(task.getUri().startsWith("http://localhost:8081/ambit2/property/P-CHEM/ZETA_POTENTIAL_SECTION/ZETA+POTENTIAL/A597EBC063D7A5A605B9535656E09149A72F0C23"));
    // "http://localhost:8081/ambit2/property/P-CHEM/ZETA_POTENTIAL_SECTION/ZETA+POTENTIAL/945DEEC24F876DEE6116F92646EF729107F5B5FD"
    } catch (Exception x) {
        throw x;
    } finally {
    }
}
Also used : Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) Value(ambit2.base.data.study.Value) Params(ambit2.base.data.study.Params) IParams(ambit2.base.data.study.IParams) TaskResult(net.idea.restnet.i.task.TaskResult) CallableSubstancePropertyCreator(ambit2.rest.substance.property.CallableSubstancePropertyCreator) PropertyURIReporter(ambit2.rest.property.PropertyURIReporter) DbUnitTest(net.idea.restnet.db.test.DbUnitTest) Test(org.junit.Test)

Example 3 with Protocol

use of ambit2.base.data.study.Protocol in project ambit-mirror by ideaconsult.

the class CallableSubstancePropertyCreatorTest method testCreateSubstanceProperty_TO_BIODEG_WATER_SCREEN_SECTION.

/**
 * @throws Exception
 */
@Test
public void testCreateSubstanceProperty_TO_BIODEG_WATER_SCREEN_SECTION() throws Exception {
    Form form = new Form();
    form.add("endpointcategory", Protocol._categories.TO_BIODEG_WATER_SCREEN_SECTION.name());
    form.add("name", "% Degradation");
    form.add("unit", "%");
    form.add("protocol", "Method: other: no data");
    Params p = new Params();
    Value v = new Value();
    v.setUnits("d");
    v.setLoValue(7.0);
    p.put("Sampling time", v);
    form.add("conditions", p.toString());
    Assert.assertEquals("{\"Sampling time\":{\"loValue\":7,\"unit\":\"d\"}}", p.toString());
    try {
        PropertyURIReporter reporter = new PropertyURIReporter(new Reference("http://localhost:8081/ambit2"));
        CallableSubstancePropertyCreator callable = new CallableSubstancePropertyCreator(reporter, Method.POST, form, null, null);
        TaskResult task = callable.call();
        Assert.assertTrue(task.getUri().startsWith("http://localhost:8081/ambit2/property/ENV+FATE/TO_BIODEG_WATER_SCREEN_SECTION/%25+Degradation/3CF77D9EFE4E74744E691BF4880B4EF728B16FAE"));
    // /d4cd0dab-cf4c-3a22-ad92-fab40844c786
    /*
			 * "http://localhost:8080/ambit2/property/ENV+FATE/TO_BIODEG_WATER_SCREEN_SECTION/%25+Degradation/5FA6EFD4E83BEC47B626C6149DBBD3907084187F/1f255895-eb7d-37dc-8eb8-bc9580d8be25"
			 * :{
			 * 
			 * "type":"Feature", "title":"% Degradation", "units":"%",
			 * "isNominal":false, "isNumeric":false, "isMultiValue":true,
			 * "sameAs"
			 * :"http://www.opentox.org/echaEndpoints.owl#TO_BIODEG_WATER_SCREEN"
			 * , "isModelPredictionFeature":false,
			 * "creator":"Method: other: see below", "order":1087, "source":{
			 * "URI"
			 * :"http://localhost:8080/ambit2/dataset/Method%3A+other%3A+see+below"
			 * , "type":"Dataset" }, "annotation":[ { "p" : "Sampling time", "o"
			 * : "7.0 d"}]
			 * 
			 * },
			 */
    // http://localhost:8081/ambit2/property/P-CHEM/TO_BIODEG_WATER_SCREEN_SECTION/%25+Degradation/8C9DF1AF60CA3D8706C0156361E97E0DF4391C10/1f255895-eb7d-37dc-8eb8-bc9580d8be25
    } catch (Exception x) {
        throw x;
    } finally {
    }
}
Also used : Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) Value(ambit2.base.data.study.Value) Params(ambit2.base.data.study.Params) IParams(ambit2.base.data.study.IParams) TaskResult(net.idea.restnet.i.task.TaskResult) CallableSubstancePropertyCreator(ambit2.rest.substance.property.CallableSubstancePropertyCreator) PropertyURIReporter(ambit2.rest.property.PropertyURIReporter) DbUnitTest(net.idea.restnet.db.test.DbUnitTest) Test(org.junit.Test)

Example 4 with Protocol

use of ambit2.base.data.study.Protocol 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)

Example 5 with Protocol

use of ambit2.base.data.study.Protocol in project ambit-mirror by ideaconsult.

the class CallableSubstancePropertyCreator method parseForm.

protected SubstanceProperty parseForm(Form form) throws ResourceException {
    String topCategory = null;
    ProtocolEffectRecord<String, IParams, String> detail = new ProtocolEffectRecord<String, IParams, String>();
    String endpointcategory = form.getFirstValue(_param_endpointcategory);
    try {
        topCategory = Protocol._categories.valueOf(endpointcategory.trim()).getTopCategory();
    } catch (Exception x) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Unsupported endpoint category");
    }
    detail.setEndpoint(form.getFirstValue(_param_name));
    if (detail.getEndpoint() == null)
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Unsupported name");
    detail.setUnit(form.getFirstValue(_param_units));
    try {
        String p_conditions = form.getFirstValue(_param_conditions);
        if (p_conditions != null) {
            JsonNode conditions = mapper.readTree(new StringReader(p_conditions));
            if (conditions instanceof ObjectNode) {
                detail.setConditions(SubstanceStudyParser.parseParams((ObjectNode) conditions));
            }
        }
    } catch (Exception x) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "conditions: unexpected format");
    }
    String guideline = form.getFirstValue(_param_protocol);
    if (guideline == null)
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "protocol: undefined");
    Protocol protocol = new Protocol(guideline);
    // protocol.addGuideline("Method: other: see below");
    protocol.setCategory(endpointcategory);
    protocol.setTopCategory(topCategory);
    detail.setProtocol(protocol);
    try {
        SubstanceProperty p = processor.process(detail);
        p.setIdentifier(p.createHashedIdentifier(detail.getConditions()));
        return p;
    } catch (ResourceException x) {
        throw x;
    } catch (Exception x) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x.getMessage(), x);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProtocolEffectRecord(ambit2.base.data.study.ProtocolEffectRecord) StringReader(java.io.StringReader) IParams(ambit2.base.data.study.IParams) ResourceException(org.restlet.resource.ResourceException) JsonNode(com.fasterxml.jackson.databind.JsonNode) SubstanceProperty(ambit2.base.data.substance.SubstanceProperty) ProtocolEffectRecord2SubstanceProperty(ambit2.core.io.study.ProtocolEffectRecord2SubstanceProperty) Protocol(ambit2.base.data.study.Protocol) ResourceException(org.restlet.resource.ResourceException)

Aggregations

Protocol (ambit2.base.data.study.Protocol)41 IParams (ambit2.base.data.study.IParams)23 ProtocolApplication (ambit2.base.data.study.ProtocolApplication)22 Test (org.junit.Test)15 EffectRecord (ambit2.base.data.study.EffectRecord)14 Params (ambit2.base.data.study.Params)14 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)11 SubstanceRecord (ambit2.base.data.SubstanceRecord)10 ArrayList (java.util.ArrayList)9 SubstanceStudyFlatQuery (ambit2.db.substance.study.SubstanceStudyFlatQuery)8 ResourceException (org.restlet.resource.ResourceException)8 ResultSet (java.sql.ResultSet)7 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)7 Form (org.restlet.data.Form)7 SubstanceProperty (ambit2.base.data.substance.SubstanceProperty)6 CompositionRelation (ambit2.base.relation.composition.CompositionRelation)6 QueryTest (ambit2.db.search.test.QueryTest)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 IStructureRecord (ambit2.base.interfaces.IStructureRecord)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5