Search in sources :

Example 1 with StructureRecord

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

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

the class PropertyValueResource method createQuery.

@Override
protected IQueryRetrieval<T> createQuery(Context context, Request request, Response response) throws ResourceException {
    RetrieveFieldPropertyValue field = new RetrieveFieldPropertyValue();
    field.setSearchByAlias(true);
    IStructureRecord record = new StructureRecord();
    try {
        record.setIdchemical(Integer.parseInt(Reference.decode(request.getAttributes().get(DataResources.idcompound_resource).toString())));
    } catch (NumberFormatException x) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, String.format("Invalid resource id %d", request.getAttributes().get(DataResources.idcompound_resource)), x);
    }
    try {
        record.setIdstructure(Integer.parseInt(Reference.decode(request.getAttributes().get(DataResources.idconformer_resource).toString())));
        field.setChemicalsOnly(false);
    } catch (Exception x) {
        field.setChemicalsOnly(true);
    } finally {
        field.setValue(record);
    }
    try {
        field.setFieldname(null);
        Object name = request.getAttributes().get("name");
        if (name != null) {
            name = Reference.decode(name.toString());
            field.setFieldname(Property.getInstance(name.toString(), LiteratureEntry.getInstance()));
        }
    } catch (Exception x) {
        field.setFieldname(null);
    }
    return (IQueryRetrieval) field;
}
Also used : RetrieveFieldPropertyValue(ambit2.db.readers.RetrieveFieldPropertyValue) IStructureRecord(ambit2.base.interfaces.IStructureRecord) IStructureRecord(ambit2.base.interfaces.IStructureRecord) StructureRecord(ambit2.base.data.StructureRecord) ResourceException(org.restlet.resource.ResourceException) IQueryRetrieval(net.idea.modbcum.i.IQueryRetrieval) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException)

Example 3 with StructureRecord

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

the class ConformerResource method createQuery.

@Override
protected QueryStructureByID createQuery(Context context, Request request, Response response) throws ResourceException {
    media = getMediaParameter(request);
    Form form = request.getResourceRef().getQueryAsForm();
    try {
        headless = Boolean.parseBoolean(form.getFirstValue("headless"));
    } catch (Exception x) {
        headless = false;
    }
    try {
        includeMol = "true".equals(form.getFirstValue("mol"));
    } catch (Exception x) {
        includeMol = false;
    }
    try {
        setTemplate(createTemplate(context, request, response));
        IStructureRecord record = new StructureRecord();
        try {
            record.setIdchemical(Integer.parseInt(Reference.decode(request.getAttributes().get(DataResources.idcompound_resource).toString())));
        } catch (NumberFormatException x) {
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid resource id", x);
        }
        QueryStructureByID query = new QueryStructureByID();
        query.setPageSize(-1);
        Object idconformer = request.getAttributes().get(DataResources.idconformer_resource);
        try {
            record.setIdstructure(Integer.parseInt(Reference.decode(idconformer.toString())));
            query.setChemicalsOnly(false);
        } catch (Exception x) {
            record.setIdstructure(-1);
            query.setChemicalsOnly(true);
            query.setPageSize(-1);
            query.setValue(record);
        }
        query.setValue(record);
        return query;
    } catch (Exception x) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Invalid resource id", x);
    }
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) Form(org.restlet.data.Form) IStructureRecord(ambit2.base.interfaces.IStructureRecord) StructureRecord(ambit2.base.data.StructureRecord) ResourceException(org.restlet.resource.ResourceException) ResourceException(org.restlet.resource.ResourceException) QueryStructureByID(ambit2.db.search.structure.QueryStructureByID)

Example 4 with StructureRecord

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

the class DatasetsByStructureResource method createQuery.

@Override
protected IQueryRetrieval<SourceDataset> createQuery(Context context, Request request, Response response) throws ResourceException {
    try {
        Form form = request.getResourceRef().getQueryAsForm();
        headless = Boolean.parseBoolean(form.getFirstValue("headless"));
    } catch (Exception x) {
        headless = false;
    }
    int idcompound = -1;
    int idstructure = -1;
    try {
        idcompound = OpenTox.URI.compound.getIntValue(getRequest());
        try {
            idstructure = OpenTox.URI.conformer.getIntValue(getRequest());
        } catch (Exception x) {
            idstructure = -1;
        }
    } catch (Exception x) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, OpenTox.URI.compound.getKey());
    }
    IStructureRecord record = new StructureRecord(idcompound, idstructure, null, null);
    RetrieveDatasets q = new RetrieveDatasets();
    q.setFieldname(record);
    return q;
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) Form(org.restlet.data.Form) StructureRecord(ambit2.base.data.StructureRecord) IStructureRecord(ambit2.base.interfaces.IStructureRecord) RetrieveDatasets(ambit2.db.readers.RetrieveDatasets) ResourceException(org.restlet.resource.ResourceException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException)

Example 5 with StructureRecord

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

the class CallableCompoundBundle method getTarget.

@Override
protected IStructureRecord getTarget(Form input) throws Exception {
    if (Method.POST.equals(method)) {
        IStructureRecord record = new StructureRecord();
        parseForm(input, record);
        command = update_command.add;
        return record;
    } else if (Method.PUT.equals(method)) {
        command = null;
        IStructureRecord record = new StructureRecord();
        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 : IStructureRecord(ambit2.base.interfaces.IStructureRecord) IStructureRecord(ambit2.base.interfaces.IStructureRecord) StructureRecord(ambit2.base.data.StructureRecord) ResourceException(org.restlet.resource.ResourceException)

Aggregations

StructureRecord (ambit2.base.data.StructureRecord)123 IStructureRecord (ambit2.base.interfaces.IStructureRecord)111 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)30 Property (ambit2.base.data.Property)23 ResourceException (org.restlet.resource.ResourceException)20 Test (org.junit.Test)19 SQLException (java.sql.SQLException)18 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)14 ArrayList (java.util.ArrayList)12 SubstanceRecord (ambit2.base.data.SubstanceRecord)11 CompositionRelation (ambit2.base.relation.composition.CompositionRelation)10 ITable (org.dbunit.dataset.ITable)10 Form (org.restlet.data.Form)10 SubstanceEndpointsBundle (ambit2.base.data.substance.SubstanceEndpointsBundle)9 MoleculeReader (ambit2.core.processors.structure.MoleculeReader)8 Proportion (ambit2.base.relation.composition.Proportion)7 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)7 LiteratureEntry (ambit2.base.data.LiteratureEntry)6 SourceDataset (ambit2.base.data.SourceDataset)6 IChemical (ambit2.base.interfaces.IChemical)5