Search in sources :

Example 1 with SubstanceRecord

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

the class SubstanceLookupTest method verifyResponseJSON.

public boolean verifyResponseJSON(String uri, MediaType media, InputStreamReader reader) throws Exception {
    // JsonNode node = parseResponseJSON(uri, media, in);
    SubstanceStudyParser p = new SubstanceStudyParser(reader);
    int i = 0;
    while (p.hasNext()) {
        Object o = p.next();
        Assert.assertTrue(o instanceof SubstanceRecord);
        Assert.assertEquals(testUUID, ((SubstanceRecord) o).getSubstanceUUID());
        System.out.println(o);
        i++;
    }
    return i == 1;
}
Also used : SubstanceRecord(ambit2.base.data.SubstanceRecord) SubstanceStudyParser(ambit2.core.io.json.SubstanceStudyParser)

Example 2 with SubstanceRecord

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

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

the class CallableSubstanceBundle method getTarget.

@Override
protected SubstanceRecord getTarget(Form input) throws Exception {
    if (Method.POST.equals(method)) {
        SubstanceRecord record = new SubstanceRecord();
        record = parseForm(input, record);
        command = update_command.add;
        return record;
    } else if (Method.PUT.equals(method)) {
        command = null;
        SubstanceRecord record = new SubstanceRecord();
        record = parseForm(input, record);
        if (command != null)
            return record;
        else
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
    }
    throw new ResourceException(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
}
Also used : SubstanceRecord(ambit2.base.data.SubstanceRecord) ResourceException(org.restlet.resource.ResourceException)

Example 4 with SubstanceRecord

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

the class SubstanceCSVReporter method processItem.

@Override
public Object processItem(SubstanceRecord item) throws AmbitException {
    try {
        Writer writer = getOutput();
        if (item.getSubstanceName() != null)
            writer.write(JSONUtils.jsonQuote(item.getSubstanceName()));
        writer.write(comma);
        writer.write(item.getSubstanceUUID());
        writer.write(comma);
        writer.write(JSONUtils.jsonQuote(item.getSubstancetype()));
        writer.write(comma);
        if (item.getPublicName() != null)
            writer.write(JSONUtils.jsonQuote(item.getPublicName()));
        writer.write(comma);
        if (item.getReferenceSubstanceUUID() != null)
            writer.write(JSONUtils.jsonQuote(item.getReferenceSubstanceUUID()));
        writer.write(comma);
        writer.write(JSONUtils.jsonQuote(item.getOwnerName()));
        writer.write(comma);
        if (item.getOwnerUUID() != null)
            writer.write(item.getOwnerUUID());
        writer.write(comma);
        if (item.getExternalids() != null) {
            for (ExternalIdentifier id : item.getExternalids()) {
                writer.write(id.getSystemDesignator());
                writer.write(":");
                writer.write(id.getSystemIdentifier());
                writer.write(" ");
            }
        } else
            writer.write(" ");
        writer.write("\n");
    } catch (Exception x) {
        logger.log(java.util.logging.Level.SEVERE, x.getMessage(), x);
    }
    return item;
}
Also used : ExternalIdentifier(ambit2.base.data.substance.ExternalIdentifier) Writer(java.io.Writer) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 5 with SubstanceRecord

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

Aggregations

SubstanceRecord (ambit2.base.data.SubstanceRecord)92 IStructureRecord (ambit2.base.interfaces.IStructureRecord)31 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)31 CompositionRelation (ambit2.base.relation.composition.CompositionRelation)24 ResourceException (org.restlet.resource.ResourceException)22 SubstanceEndpointsBundle (ambit2.base.data.substance.SubstanceEndpointsBundle)19 ProtocolApplication (ambit2.base.data.study.ProtocolApplication)16 Form (org.restlet.data.Form)16 Test (org.junit.Test)14 StructureRecord (ambit2.base.data.StructureRecord)12 ArrayList (java.util.ArrayList)12 File (java.io.File)11 Reference (org.restlet.data.Reference)11 Property (ambit2.base.data.Property)10 Protocol (ambit2.base.data.study.Protocol)9 ExternalIdentifier (ambit2.base.data.substance.ExternalIdentifier)8 Proportion (ambit2.base.relation.composition.Proportion)8 DBSubstanceWriter (ambit2.db.substance.processor.DBSubstanceWriter)7 FileInputStream (java.io.FileInputStream)7 InputStreamReader (java.io.InputStreamReader)7