Search in sources :

Example 1 with EffectRecord

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

the class SubstancePropertyresourceTest method testCreateEntry2.

@Test
public void testCreateEntry2() throws Exception {
    EffectRecord<String, IParams, String> effect = new EffectRecord<String, IParams, String>();
    effect.setConditions(new Params());
    effect.setEndpoint("EC10");
    effect.setUnit("mg/L");
    IParams conditions = new Params();
    effect.setConditions(conditions);
    /*
	Value v = new Value();
	v.setUnits("h");
	v.setLoValue(48.0);
	effect.getConditions().put("EC_DAPHNIATOX",v);
	*/
    // System.out.println(effect);
    SubstanceProperty prop = new SubstanceProperty(null, null, effect.getEndpoint(), effect.getUnit(), null);
    String key = prop.createHashedIdentifier(effect.getConditions());
    UUID protocol_uuid = UUID.nameUUIDFromBytes("http://localhost:8080/ambit2/dataset/Method%3A+other%3A+Am.+Soc.+Test.+Mater.+%281980%29+and+U.S.+EPA+%281975%29".getBytes());
    URL url = getClass().getClassLoader().getResource("feature.rdf");
    FileRepresentation rep = new FileRepresentation(url.getFile(), MediaType.APPLICATION_RDF_XML, 0);
    testAsyncPoll(new Reference(getTestURI()), MediaType.TEXT_URI_LIST, rep, Method.POST, new Reference(String.format("http://localhost:%d/property/ECOTOX/EC_DAPHNIATOX_SECTION/EC10/%s/NOTSPECIFIED/%s", port, key.toUpperCase(), protocol_uuid)));
}
Also used : EffectRecord(ambit2.base.data.study.EffectRecord) Reference(org.restlet.data.Reference) FileRepresentation(org.restlet.representation.FileRepresentation) IParams(ambit2.base.data.study.IParams) Params(ambit2.base.data.study.Params) IParams(ambit2.base.data.study.IParams) SubstanceProperty(ambit2.base.data.substance.SubstanceProperty) UUID(java.util.UUID) URL(java.net.URL) Test(org.junit.Test) ResourceTest(ambit2.rest.test.ResourceTest)

Example 2 with EffectRecord

use of ambit2.base.data.study.EffectRecord 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 3 with EffectRecord

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

the class SubstanceDatasetResource method getPropertyProcessors.

protected IProcessor getPropertyProcessors(final boolean removeIdentifiers, final boolean removeStringProperties) {
    IQueryRetrieval<ProtocolEffectRecord<String, String, String>> queryP = getEffectQuery();
    MasterDetailsProcessor<SubstanceRecord, ProtocolEffectRecord<String, String, String>, IQueryCondition> effectReader = new MasterDetailsProcessor<SubstanceRecord, ProtocolEffectRecord<String, String, String>, IQueryCondition>(queryP) {

        /**
         */
        private static final long serialVersionUID = -7354966336095750101L;

        ProtocolEffectRecord2SubstanceProperty processor = new ProtocolEffectRecord2SubstanceProperty();

        @Override
        public SubstanceRecord process(SubstanceRecord target) throws Exception {
            if (target == null || target.getSubstanceUUID() == null)
                return target;
            else
                return super.process(target);
        }

        @Override
        protected SubstanceRecord processDetail(SubstanceRecord master, ProtocolEffectRecord<String, String, String> detail) throws Exception {
            if (master.getIdsubstance() <= 0)
                return master;
            if (detail != null) {
                if (detail.getTextValue() != null && detail.getTextValue().toString().startsWith("{")) {
                    JsonNode node = dx.readTree(new StringReader(detail.getTextValue().toString()));
                    List<String> guideline = detail.getProtocol().getGuideline();
                    ILiteratureEntry ref = LiteratureEntry.getInstance(guideline == null ? null : guideline.size() == 0 ? null : guideline.get(0), guideline == null ? null : guideline.size() == 0 ? null : guideline.get(0));
                    Iterator<Entry<String, JsonNode>> i = node.fields();
                    while (i.hasNext()) {
                        Entry<String, JsonNode> val = i.next();
                        SubstanceProperty key = new SubstanceProperty(detail.getProtocol().getTopCategory(), detail.getProtocol().getCategory(), val.getKey(), detail.getUnit(), ref);
                        try {
                            key.setStudyResultType(_r_flags.valueOf(detail.getStudyResultType().replace(":", "").replace("_", "").replace(" ", "").replace("-", "").replace(")", "").replace("(", "")));
                        } catch (Exception x) {
                            key.setStudyResultType(null);
                        }
                        key.setExtendedURI(true);
                        key.setIdentifier(detail.getSampleID() + "/" + val.getKey());
                        groupProperties.add(key);
                        if (val.getValue().get(EffectRecord._fields.loValue.name()) != null) {
                            Number num = val.getValue().get(EffectRecord._fields.loValue.name()).asDouble();
                            master.setRecordProperty(key, num);
                            key.setClazz(Number.class);
                        } else {
                            master.setRecordProperty(key, val.getValue().textValue());
                            key.setClazz(String.class);
                        }
                    }
                } else {
                    boolean isTextValue = ((detail.getLoValue() == null) && (detail.getUpValue() == null));
                    if (isTextValue && removeStringProperties)
                        return master;
                    /*
						 * JsonNode conditions = detail.getConditions() == null
						 * ? null : dx.readTree(new StringReader(
						 * detail.getConditions()));
						 * 
						 * PropertyAnnotations ann = new PropertyAnnotations();
						 * 
						 * Iterator<Entry<String, JsonNode>> i = conditions ==
						 * null ? null : conditions.getFields();
						 * 
						 * if (i != null) while (i.hasNext()) { Entry<String,
						 * JsonNode> val = i.next(); if (val.getValue()
						 * instanceof NullNode) continue;
						 * 
						 * if (val.getValue().getTextValue() == null) try {
						 * PropertyAnnotation a = new PropertyAnnotation();
						 * String unit =
						 * val.getValue().get(EffectRecord._fields.unit.name())
						 * == null ? null :
						 * val.getValue().get(EffectRecord._fields
						 * .unit.name()).asText(); a.setPredicate(val.getKey());
						 * if (unit == null)
						 * a.setObject(val.getValue().get(EffectRecord
						 * ._fields.loValue.name()) .asText()); else {
						 * a.setObject(String.format("%s %s",
						 * val.getValue().get(
						 * EffectRecord._fields.loValue.name()).asText(),
						 * unit)); } ann.add(a); } catch (Exception x) { } else
						 * { PropertyAnnotation a = new PropertyAnnotation();
						 * a.setPredicate(val.getKey());
						 * a.setObject(val.getValue().getTextValue());
						 * ann.add(a); }
						 * 
						 * }
						 */
                    ProtocolEffectRecord<String, IParams, String> effect = new ProtocolEffectRecord<String, IParams, String>();
                    ProtocolEffectRecord2SubstanceProperty.copyEffectRecordValues(detail, effect);
                    try {
                        JsonNode conditions = detail.getConditions() == null ? null : dx.readTree(new StringReader(detail.getConditions()));
                        if (conditions instanceof ObjectNode) {
                            effect.setConditions(SubstanceStudyParser.parseParams((ObjectNode) conditions));
                        }
                    } catch (Exception x) {
                        logger.log(Level.FINE, x.getMessage());
                    }
                    SubstanceProperty key = processor.process(effect);
                    key.setIdentifier(key.createHashedIdentifier(effect.getConditions()));
                    Object oldValue = master.getRecordProperty(key);
                    groupProperties.add(key);
                    /*
						 * if (isTextValue) { //textvalue if (oldValue == null)
						 * master.setProperty(key, detail.getTextValue()); else
						 * { master.setProperty(key, String.format( "%s, %s",
						 * oldValue instanceof Number ? nf.format((Number)
						 * oldValue) : oldValue .toString(),
						 * detail.getTextValue())); }
						 * key.setClazz(String.class); } else { //numeric
						 */
                    Value value = processValue(detail, isTextValue);
                    if (value != null)
                        ProtocolEffectRecord2SubstanceProperty.addValues(master, key, value, oldValue);
                // }
                }
            }
            return master;
        }
    };
    return effectReader;
}
Also used : IQueryCondition(net.idea.modbcum.i.IQueryCondition) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProtocolEffectRecord2SubstanceProperty(ambit2.core.io.study.ProtocolEffectRecord2SubstanceProperty) ILiteratureEntry(ambit2.base.data.ILiteratureEntry) SubstanceRecord(ambit2.base.data.SubstanceRecord) JsonNode(com.fasterxml.jackson.databind.JsonNode) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) LiteratureEntry(ambit2.base.data.LiteratureEntry) Entry(java.util.Map.Entry) ILiteratureEntry(ambit2.base.data.ILiteratureEntry) ProtocolEffectRecord(ambit2.base.data.study.ProtocolEffectRecord) MasterDetailsProcessor(net.idea.modbcum.p.MasterDetailsProcessor) StringReader(java.io.StringReader) Value(ambit2.base.data.study.Value) IParams(ambit2.base.data.study.IParams) ProtocolEffectRecord2SubstanceProperty(ambit2.core.io.study.ProtocolEffectRecord2SubstanceProperty) SubstanceProperty(ambit2.base.data.substance.SubstanceProperty)

Example 4 with EffectRecord

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

the class BucketDenormalised method effectrecord2bucket.

protected void effectrecord2bucket(ProtocolApplication papp, EffectRecord<String, Object, String> e, Bucket bucket, boolean suffix) {
    if (papp.getInvestigationUUID() != null)
        bucket.put(ns("investigation_uuid", suffix, "_s"), papp.getInvestigationUUID());
    if (papp.getAssayUUID() != null)
        bucket.put(ns("assay_uuid", suffix, "_s"), papp.getAssayUUID());
    if (e.getEndpoint() != null)
        bucket.put(ns("effectendpoint", suffix, "_s"), e.getEndpoint().toUpperCase());
    String[] terms = annotator.annotateEndpoint(e.getEndpoint());
    if (terms != null) {
        bucket.put(ns("effectendpoint_synonym", suffix, "_ss"), Arrays.asList(terms));
    }
    if (e.getEndpointType() != null)
        bucket.put(ns("effectendpoint_type", suffix, "_s"), e.getEndpointType().toUpperCase());
    if (e.getEndpointGroup() != null)
        bucket.put(ns("effectendpoint_group", suffix, "_s"), e.getEndpointGroup());
    bucket.put(ns("unit", suffix, "_s"), e.getUnit() == null ? "" : e.getUnit());
    if (e.getLoValue() != null || e.getUpValue() != null) {
        if (e.getLoQualifier() != null && !"".equals(e.getLoQualifier()))
            bucket.put(ns("loQualifier", suffix, "_s"), e.getLoQualifier());
        if (e.getLoValue() != null)
            bucket.put(ns("loValue", suffix, "_d"), e.getLoValue());
        if (e.getUpQualifier() != null && !"".equals(e.getUpQualifier()))
            bucket.put(ns("upQualifier", suffix, "_s"), e.getUpQualifier());
        if (e.getUpValue() != null)
            bucket.put(ns("upValue", suffix, "_d"), e.getUpValue());
        if (e.getErrQualifier() != null && !"".equals(e.getErrQualifier()))
            bucket.put(ns("errQualifier", suffix, "_s"), e.getErrQualifier());
        if (e.getErrorValue() != null && !"".equals(e.getErrorValue()))
            bucket.put(ns("err", suffix, "_d"), e.getErrorValue());
    }
    final String catchall4search = "_text_";
    if (e.getTextValue() != null) {
        if (e.getTextValue().toString().startsWith("{")) {
            IParams nonzero = SubstanceStudyParser.parseTextValueProteomics(dx, e.getTextValue().toString());
            bucket.put(catchall4search, nonzero);
        } else {
            bucket.put(ns("textValue", suffix, "_s"), e.getTextValue());
        }
    }
// condition2bucket(e,bucket,"condition.");
}
Also used : IParams(ambit2.base.data.study.IParams)

Example 5 with EffectRecord

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

EffectRecord (ambit2.base.data.study.EffectRecord)25 IParams (ambit2.base.data.study.IParams)22 ProtocolApplication (ambit2.base.data.study.ProtocolApplication)18 Params (ambit2.base.data.study.Params)16 Protocol (ambit2.base.data.study.Protocol)15 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)8 ArrayList (java.util.ArrayList)7 SubstanceRecord (ambit2.base.data.SubstanceRecord)6 ResourceException (org.restlet.resource.ResourceException)6 IStructureRecord (ambit2.base.interfaces.IStructureRecord)5 CompositionRelation (ambit2.base.relation.composition.CompositionRelation)5 Value (ambit2.base.data.study.Value)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 Test (org.junit.Test)4 ExternalIdentifier (ambit2.base.data.substance.ExternalIdentifier)3 SubstanceEndpointsBundle (ambit2.base.data.substance.SubstanceEndpointsBundle)3 UpdateEffectRecords (ambit2.db.substance.study.UpdateEffectRecords)3 Reference (org.restlet.data.Reference)3 ILiteratureEntry (ambit2.base.data.ILiteratureEntry)2