Search in sources :

Example 1 with IStructureRecord

use of ambit2.base.interfaces.IStructureRecord 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 IStructureRecord

use of ambit2.base.interfaces.IStructureRecord in project ambit-mirror by ideaconsult.

the class StaXRDFTest method testRDFXML.

@Test
public void testRDFXML() throws Exception {
    // testGet(getTestURI(),MediaType.APPLICATION_RDF_XML);
    RDFStructuresIterator i = new RDFStructuresIterator(new Reference(getTestURI()), "test");
    i.setBaseReference(new Reference(String.format("http://localhost:%d", port)));
    int count = 0;
    boolean propertyfound = false;
    while (i.hasNext()) {
        IStructureRecord record = i.next();
        Assert.assertTrue(record.getIdchemical() > 0);
        Assert.assertTrue(record.getIdstructure() > 0);
        int props = 0;
        for (Property p : record.getRecordProperties()) {
            System.out.println(p.getName());
            propertyfound = propertyfound || "Property 1".equals(p.getName());
            props++;
        }
        System.out.println(props);
        count++;
    }
    i.close();
    Assert.assertEquals(4, count);
    Assert.assertTrue(propertyfound);
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) Reference(org.restlet.data.Reference) Property(ambit2.base.data.Property) RDFStructuresIterator(ambit2.rest.rdf.RDFStructuresIterator) Test(org.junit.Test) ResourceTest(ambit2.rest.test.ResourceTest)

Example 3 with IStructureRecord

use of ambit2.base.interfaces.IStructureRecord in project ambit-mirror by ideaconsult.

the class ConsensusLabelQueryResource method createQuery.

@Override
protected IQueryRetrieval<IStructureRecord> createQuery(Context context, Request request, Response response) throws ResourceException {
    // collapsed = true;
    setGroupProperties(context, request, response);
    setTemplate(createTemplate(context, request, response));
    QueryStructureByQualityPairLabel q = new QueryStructureByQualityPairLabel();
    Form form = request.getResourceRef().getQueryAsForm();
    Object key = form.getFirstValue(QueryResource.search_param);
    ConsensusLabel label = new ConsensusLabel();
    if (key != null) {
        try {
            label.setLabel(CONSENSUS_LABELS.valueOf(Reference.decode(key.toString())));
        } catch (Exception x) {
            StringBuilder b = new StringBuilder();
            b.append("Valid values are ");
            for (CONSENSUS_LABELS v : CONSENSUS_LABELS.values()) {
                b.append(v);
                b.append('\t');
            }
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, b.toString(), x);
        }
    } else {
        label.setLabel(CONSENSUS_LABELS.Consensus);
    }
    q.setValue(label);
    key = form.getFirstValue("text");
    if (key != null)
        label.setText(key.toString());
    Object id = request.getAttributes().get(OpenTox.URI.dataset.getKey());
    if (id != null)
        try {
            datasetID = Integer.parseInt(id.toString());
            SourceDataset dataset = new SourceDataset();
            dataset.setId(datasetID);
            q.setFieldname(dataset);
            return q;
        } catch (Exception x) {
            if (id.toString().startsWith("R")) {
                queryResultsID = Integer.parseInt(id.toString().substring(1));
                StoredQuery dataset = new StoredQuery();
                dataset.setId(queryResultsID);
                q.setFieldname(dataset);
            } else
                throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid dataset id");
        }
    return q;
// throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,"No dataset !");
}
Also used : StoredQuery(ambit2.db.search.StoredQuery) QueryStructureByQualityPairLabel(ambit2.db.search.structure.QueryStructureByQualityPairLabel) Form(org.restlet.data.Form) ConsensusLabel(ambit2.base.data.ConsensusLabel) CONSENSUS_LABELS(ambit2.base.data.ConsensusLabel.CONSENSUS_LABELS) ResourceException(org.restlet.resource.ResourceException) SourceDataset(ambit2.base.data.SourceDataset) ResourceException(org.restlet.resource.ResourceException)

Example 4 with IStructureRecord

use of ambit2.base.interfaces.IStructureRecord in project ambit-mirror by ideaconsult.

the class QLabelQueryResource method createQuery.

@Override
protected IQueryRetrieval<IStructureRecord> createQuery(Context context, Request request, Response response) throws ResourceException {
    QueryStructureByQuality q = new QueryStructureByQuality();
    Form form = request.getResourceRef().getQueryAsForm();
    try {
        includeMol = "true".equals(form.getFirstValue("mol"));
    } catch (Exception x) {
        includeMol = false;
    }
    Object key = form.getFirstValue(QueryResource.search_param);
    if (key != null) {
        try {
            q.setValue(new QLabel(QUALITY.valueOf(Reference.decode(key.toString()))));
        } catch (Exception x) {
            StringBuilder b = new StringBuilder();
            b.append("Valid values are ");
            for (QUALITY v : QUALITY.values()) {
                b.append(v);
                b.append('\t');
            }
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, b.toString(), x);
        }
    } else {
        q.setValue(new QLabel(QUALITY.OK));
    }
    Object id = request.getAttributes().get(OpenTox.URI.dataset.getKey());
    if (id != null)
        try {
            datasetID = Integer.parseInt(id.toString());
            SourceDataset dataset = new SourceDataset();
            dataset.setId(datasetID);
            q.setFieldname(dataset);
            return q;
        } catch (Exception x) {
            if (id.toString().startsWith("R")) {
                queryResultsID = Integer.parseInt(id.toString().substring(1));
                StoredQuery dataset = new StoredQuery();
                dataset.setId(queryResultsID);
                q.setFieldname(dataset);
                return q;
            } else
                throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid dataset id");
        }
    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No dataset !");
}
Also used : StoredQuery(ambit2.db.search.StoredQuery) QUALITY(ambit2.base.data.QLabel.QUALITY) Form(org.restlet.data.Form) QLabel(ambit2.base.data.QLabel) QueryStructureByQuality(ambit2.db.search.structure.QueryStructureByQuality) ResourceException(org.restlet.resource.ResourceException) SourceDataset(ambit2.base.data.SourceDataset) ResourceException(org.restlet.resource.ResourceException)

Example 5 with IStructureRecord

use of ambit2.base.interfaces.IStructureRecord in project ambit-mirror by ideaconsult.

the class StructureQueryResource method createConvertor.

@Override
public RepresentationConvertor createConvertor(Variant variant) throws AmbitException, ResourceException {
    /* workaround for clients not being able to set accept headers */
    if ((queryObject == null) && !(variant.getMediaType().equals(MediaType.TEXT_HTML)))
        throw new NotFoundException();
    setTemplate(template);
    Form acceptform = getResourceRef(getRequest()).getQueryAsForm();
    String media = acceptform.getFirstValue("accept-header");
    if (media != null) {
        variant.setMediaType(new MediaType(media));
    }
    String filenamePrefix = getRequest().getResourceRef().getPath();
    if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_MDLSDF)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SDFReporter<QueryStructureByID>(template, getGroupProperties(), changeLineSeparators), ChemicalMediaType.CHEMICAL_MDLSDF, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_MDLMOL)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SDFReporter<QueryStructureByID>(new Template(), getGroupProperties(), true, changeLineSeparators), ChemicalMediaType.CHEMICAL_MDLMOL, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_CML)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CMLReporter<QueryStructureByID>(), ChemicalMediaType.CHEMICAL_CML, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_SMILES)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SmilesReporter<QueryStructureByID>(true, getTemplate()), ChemicalMediaType.CHEMICAL_SMILES, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_INCHI)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SmilesReporter<QueryStructureByID>(false, Mode.InChI, getTemplate()), ChemicalMediaType.CHEMICAL_INCHI, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_PDF)) {
        return new PDFConvertor<IStructureRecord, QueryStructureByID, PDFReporter<QueryStructureByID>>(new PDFReporter<QueryStructureByID>(getTemplate(), getGroupProperties()), filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.TEXT_PLAIN)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createTXTReporter(), MediaType.TEXT_PLAIN);
    } else if (variant.getMediaType().equals(MediaType.TEXT_URI_LIST)) {
        ConformerURIReporter<QueryStructureByID> reporter = new ConformerURIReporter<QueryStructureByID>(getCompoundInDatasetPrefix(), getRequest(), queryObject.isPrescreen());
        return new StringConvertor(reporter, MediaType.TEXT_URI_LIST, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.IMAGE_PNG)) {
        return new ImageConvertor<IStructureRecord, QueryStructureByID>(new ImageReporter<QueryStructureByID>(MediaType.IMAGE_PNG.getMainType(), MediaType.IMAGE_PNG.getSubType()), MediaType.IMAGE_PNG);
    } else if (variant.getMediaType().equals(MediaType.IMAGE_GIF)) {
        return new ImageConvertor<IStructureRecord, QueryStructureByID>(new ImageReporter<QueryStructureByID>(MediaType.IMAGE_GIF.getMainType(), MediaType.IMAGE_GIF.getSubType()), MediaType.IMAGE_GIF);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_JSON)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CompoundJSONReporter(getTemplate(), getGroupProperties(), folders, bundles, getRequest(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix(), includeMol, null), MediaType.APPLICATION_JSON, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_JAVASCRIPT)) {
        String jsonpcallback = getParams().getFirstValue("jsonp");
        if (jsonpcallback == null)
            jsonpcallback = getParams().getFirstValue("callback");
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CompoundJSONReporter(getTemplate(), getGroupProperties(), folders, bundles, getRequest(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix(), includeMol, jsonpcallback), MediaType.APPLICATION_JAVASCRIPT, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.WEKA_ARFF)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new ARFFResourceReporter(getTemplate(), getGroupProperties(), getRequest(), String.format("%s%s", getRequest().getRootRef(), getCompoundInDatasetPrefix())), ChemicalMediaType.WEKA_ARFF, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.THREECOL_ARFF)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new ARFF3ColResourceReporter(getTemplate(), getGroupProperties(), getRequest(), String.format("%s%s", getRequest().getRootRef(), getCompoundInDatasetPrefix())), ChemicalMediaType.THREECOL_ARFF, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.TEXT_CSV)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createCSVReporter(), MediaType.TEXT_CSV, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_XML)) {
        switch(rdfwriter) {
            case stax:
                {
                    return new RDFStaXConvertor<IStructureRecord, IQueryRetrieval<IStructureRecord>>(new DatasetRDFStaxReporter(getCompoundInDatasetPrefix(), getRequest(), getTemplate(), getGroupProperties()), filenamePrefix);
                }
            default:
                {
                    // jena
                    return new RDFJenaConvertor<IStructureRecord, IQueryRetrieval<IStructureRecord>>(new DatasetRDFReporter(getCompoundInDatasetPrefix(), getRequest(), variant.getMediaType(), getTemplate(), getGroupProperties()), variant.getMediaType(), filenamePrefix);
                }
        }
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_TURTLE) || variant.getMediaType().equals(MediaType.TEXT_RDF_N3) || variant.getMediaType().equals(MediaType.TEXT_RDF_NTRIPLES) || variant.getMediaType().equals(MediaType.APPLICATION_RDF_TRIG) || variant.getMediaType().equals(MediaType.APPLICATION_RDF_TRIX) || variant.getMediaType().equals(ChemicalMediaType.APPLICATION_JSONLD)) {
        return new RDFJenaConvertor<IStructureRecord, IQueryRetrieval<IStructureRecord>>(new DatasetRDFReporter(getCompoundInDatasetPrefix(), getRequest(), variant.getMediaType(), getTemplate(), getGroupProperties()), variant.getMediaType(), filenamePrefix);
    } else
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SDFReporter<QueryStructureByID>(template, getGroupProperties(), changeLineSeparators), ChemicalMediaType.CHEMICAL_MDLSDF, filenamePrefix);
}
Also used : Form(org.restlet.data.Form) OutputWriterConvertor(net.idea.restnet.db.convertors.OutputWriterConvertor) PDFReporter(ambit2.db.reporters.PDFReporter) NotFoundException(net.idea.modbcum.i.exceptions.NotFoundException) QueryStructureByID(ambit2.db.search.structure.QueryStructureByID) Template(ambit2.base.data.Template) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ARFF3ColResourceReporter(ambit2.rest.dataset.ARFF3ColResourceReporter) SDFReporter(ambit2.db.reporters.SDFReporter) ChemicalMediaType(net.idea.restnet.c.ChemicalMediaType) MediaType(org.restlet.data.MediaType) RDFJenaConvertor(ambit2.rest.RDFJenaConvertor) CompoundJSONReporter(ambit2.rest.structure.CompoundJSONReporter) SmilesReporter(ambit2.db.reporters.SmilesReporter) DatasetRDFStaxReporter(ambit2.rest.dataset.DatasetRDFStaxReporter) StringConvertor(net.idea.restnet.c.StringConvertor) ImageReporter(ambit2.db.reporters.ImageReporter) RDFStaXConvertor(ambit2.rest.RDFStaXConvertor) ARFFResourceReporter(ambit2.rest.dataset.ARFFResourceReporter) ConformerURIReporter(ambit2.rest.structure.ConformerURIReporter) ImageConvertor(ambit2.rest.ImageConvertor) DatasetRDFReporter(ambit2.rest.dataset.DatasetRDFReporter)

Aggregations

IStructureRecord (ambit2.base.interfaces.IStructureRecord)252 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)119 StructureRecord (ambit2.base.data.StructureRecord)96 Property (ambit2.base.data.Property)93 Test (org.junit.Test)70 ResourceException (org.restlet.resource.ResourceException)49 SQLException (java.sql.SQLException)40 SubstanceRecord (ambit2.base.data.SubstanceRecord)37 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)35 DbAmbitException (net.idea.modbcum.i.exceptions.DbAmbitException)32 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)32 InputStreamReader (java.io.InputStreamReader)31 IOException (java.io.IOException)27 Form (org.restlet.data.Form)26 SourceDataset (ambit2.base.data.SourceDataset)23 File (java.io.File)23 ITable (org.dbunit.dataset.ITable)23 ArrayList (java.util.ArrayList)22 MoleculeReader (ambit2.core.processors.structure.MoleculeReader)21 InputStream (java.io.InputStream)19