Search in sources :

Example 1 with Substance2BucketJsonReporter

use of ambit2.rest.substance.study.Substance2BucketJsonReporter 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 Substance2BucketJsonReporter

use of ambit2.rest.substance.study.Substance2BucketJsonReporter in project ambit-mirror by ideaconsult.

the class SubstanceExportResource method createJSONReporter.

@Override
protected IProcessor<Q, Representation> createJSONReporter(String filenamePrefix) {
    String jsonpcallback = getParams().getFirstValue("jsonp");
    if (jsonpcallback == null)
        jsonpcallback = getParams().getFirstValue("callback");
    String command = "results";
    try {
        if (Boolean.parseBoolean(getParams().getFirstValue("array").toString()))
            command = null;
    } catch (Exception x) {
    }
    ProcessorsChain chain = new ProcessorsChain<>();
    chain.add(new SubstanceStudyDetailsProcessor());
    getCompositionProcessors(chain);
    SubstanceRecordAnnotationProcessor annotator = null;
    try {
        annotator = new SubstanceRecordAnnotationProcessor(new File(((AmbitFreeMarkerApplication) getApplication()).getProperties().getMapFolder()), false);
    } catch (Exception x) {
        Logger.getGlobal().log(Level.WARNING, x.getMessage());
        annotator = null;
    }
    return new OutputWriterConvertor<SubstanceRecord, Q>((QueryAbstractReporter<SubstanceRecord, Q, Writer>) new Substance2BucketJsonReporter(command, chain, jsonmode, summaryMeasurement, dbTag, annotator), jsonpcallback == null ? MediaType.APPLICATION_JSON : MediaType.APPLICATION_JAVASCRIPT, filenamePrefix);
}
Also used : ProcessorsChain(net.idea.modbcum.i.processors.ProcessorsChain) OutputWriterConvertor(net.idea.restnet.db.convertors.OutputWriterConvertor) SubstanceStudyDetailsProcessor(ambit2.db.substance.study.SubstanceStudyDetailsProcessor) SubstanceRecordAnnotationProcessor(ambit2.base.ro.SubstanceRecordAnnotationProcessor) SubstanceRecord(ambit2.base.data.SubstanceRecord) AmbitFreeMarkerApplication(ambit2.rest.AmbitFreeMarkerApplication) File(java.io.File) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) Writer(java.io.Writer)

Aggregations

SubstanceRecord (ambit2.base.data.SubstanceRecord)2 File (java.io.File)2 EffectRecord (ambit2.base.data.study.EffectRecord)1 IParams (ambit2.base.data.study.IParams)1 Params (ambit2.base.data.study.Params)1 Protocol (ambit2.base.data.study.Protocol)1 ProtocolApplication (ambit2.base.data.study.ProtocolApplication)1 ExternalIdentifier (ambit2.base.data.substance.ExternalIdentifier)1 IStructureRecord (ambit2.base.interfaces.IStructureRecord)1 SubstanceRecordAnnotationProcessor (ambit2.base.ro.SubstanceRecordAnnotationProcessor)1 RawIteratingCSVReader (ambit2.core.io.RawIteratingCSVReader)1 SubstanceStudyDetailsProcessor (ambit2.db.substance.study.SubstanceStudyDetailsProcessor)1 AmbitFreeMarkerApplication (ambit2.rest.AmbitFreeMarkerApplication)1 Substance2BucketJsonReporter (ambit2.rest.substance.study.Substance2BucketJsonReporter)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1