Search in sources :

Example 1 with NanoWikiRDFReader

use of net.idea.loom.nm.nanowiki.NanoWikiRDFReader in project ambit-mirror by ideaconsult.

the class NanoWikiRDFTest method testRead.

@Test
public void testRead() throws Exception {
    NanoWikiRDFReader reader = null;
    int records = 0;
    try {
        reader = new NanoWikiRDFReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(getNanoWikiFile()))));
        while (reader.hasNext()) {
            IStructureRecord record = reader.nextRecord();
            Assert.assertTrue(record instanceof SubstanceRecord);
            System.out.println(((SubstanceRecord) record).getPublicName());
            records++;
        }
    } finally {
        reader.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) IStructureRecord(ambit2.base.interfaces.IStructureRecord) InputStreamReader(java.io.InputStreamReader) SubstanceRecord(ambit2.base.data.SubstanceRecord) FileInputStream(java.io.FileInputStream) NanoWikiRDFReader(net.idea.loom.nm.nanowiki.NanoWikiRDFReader) DbUnitTest(ambit2.db.processors.test.DbUnitTest) Test(org.junit.Test)

Example 2 with NanoWikiRDFReader

use of net.idea.loom.nm.nanowiki.NanoWikiRDFReader in project ambit-mirror by ideaconsult.

the class NanoWikiRDFTest method testWriteNanoWikiRDF.

@Test
public void testWriteNanoWikiRDF() throws Exception {
    setUpDatabaseFromResource("ambit2/db/processors/test/empty-datasets.xml");
    IDatabaseConnection c = getConnection();
    IRawReader<IStructureRecord> parser = null;
    try {
        parser = new NanoWikiRDFReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(getNanoWikiFile()))));
        write(parser, c.getConnection(), new ReferenceSubstanceUUID(), false);
    } finally {
        parser.close();
        c.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) IStructureRecord(ambit2.base.interfaces.IStructureRecord) InputStreamReader(java.io.InputStreamReader) ReferenceSubstanceUUID(ambit2.core.processors.structure.key.ReferenceSubstanceUUID) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) FileInputStream(java.io.FileInputStream) NanoWikiRDFReader(net.idea.loom.nm.nanowiki.NanoWikiRDFReader) DbUnitTest(ambit2.db.processors.test.DbUnitTest) Test(org.junit.Test)

Example 3 with NanoWikiRDFReader

use of net.idea.loom.nm.nanowiki.NanoWikiRDFReader in project ambit-mirror by ideaconsult.

the class Export2RDFTest method testNanoWiki2RDF.

@Test
public void testNanoWiki2RDF() throws Exception {
    File nanowiki = NanoWikiRDFTest.getNanoWikiFile();
    Assert.assertTrue(nanowiki.exists());
    Request hack = new Request();
    hack.setRootRef(new Reference("http://localhost/ambit2"));
    SubstanceRDFReporter r = new SubstanceRDFReporter(hack, MediaType.TEXT_RDF_N3);
    Model model = ModelFactory.createDefaultModel();
    r.header(model, null);
    r.setOutput(model);
    NanoWikiRDFReader reader = null;
    OutputStream writer = null;
    StructureRecordValidator validator = new StructureRecordValidator() {

        int idresult = 1;

        int idcompound = 1;

        Map<String, Integer> smileslookup = new TreeMap<String, Integer>();

        @Override
        public IStructureRecord validate(SubstanceRecord record, CompositionRelation rel) throws Exception {
            /**
             * compounds will be assigned ids if imported into database
             * and resource url depends on the ids
             */
            if (rel.getSecondStructure().getIdchemical() <= 0) {
                String smi = rel.getSecondStructure().getSmiles();
                if (smi != null) {
                    Integer index = smileslookup.get(smi);
                    if (index != null)
                        rel.getSecondStructure().setIdchemical(index);
                    else {
                        smileslookup.put(smi, idcompound);
                        rel.getSecondStructure().setIdchemical(idcompound);
                        idcompound++;
                    }
                } else {
                    rel.getSecondStructure().setIdchemical(idcompound);
                    idcompound++;
                }
            }
            return super.validate(record, rel);
        }

        @Override
        public IStructureRecord validate(SubstanceRecord record, ProtocolApplication<Protocol, IParams, String, IParams, String> papp) throws Exception {
            /*
				 * EffectRecords resource URL depends on th eidresult, when
				 * imported into DB it's automatically assigned, here it's set
				 * to subsequent numbers
				 */
            for (EffectRecord<String, IParams, String> effect : papp.getEffects()) {
                if (effect.getIdresult() <= 0)
                    effect.setIdresult(idresult++);
            }
            return super.validate(record, papp);
        }
    };
    int records = 0;
    try {
        reader = new NanoWikiRDFReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(nanowiki))));
        while (reader.hasNext()) {
            IStructureRecord record = reader.nextRecord();
            Assert.assertTrue(record instanceof SubstanceRecord);
            validator.validate((SubstanceRecord) record);
            r.processItem((SubstanceRecord) record);
            records++;
        }
        r.footer(model, null);
        // one substance
        ResIterator substances = model.listSubjectsWithProperty(RDF.type, RDFTermsSubstance.CHEBI_59999.getResource(model));
        Assert.assertTrue(countResources(substances) >= 404);
        // asssay
        ResIterator assays = model.listSubjectsWithProperty(RDF.type, RDFTermsSubstance.BAO_0000015.getResource(model));
        Assert.assertTrue(countResources(assays) >= 866);
        // measure groups
        ResIterator measuregroups = model.listSubjectsWithProperty(RDF.type, RDFTermsSubstance.BAO_0000040.getResource(model));
        Assert.assertTrue(countResources(measuregroups) >= 86);
        // protocols
        ResIterator protocols = model.listSubjectsWithProperty(RDF.type, RDFTermsSubstance.OBI_0000272.getResource(model));
        Assert.assertTrue(countResources(protocols) >= 65);
        // endpoint
        ResIterator endpoints = model.listSubjectsWithProperty(RDF.type, RDFTermsSubstance.BAO_0000179.getResource(model));
        // thanks to the validator result ids are set and we have > 1 entry,
        // otherwise all endpoints collapse into one
        Assert.assertTrue(countResources(endpoints) >= 866);
        File output = new File(System.getProperty("java.io.tmpdir") + "/" + "nanowiki_export.ttl");
        System.out.println("Exported to " + output.getAbsolutePath());
        writer = new FileOutputStream(output);
        RDFDataMgr.write(writer, model, RDFFormat.TURTLE);
    } finally {
        reader.close();
        if (writer != null)
            writer.close();
    }
}
Also used : ResIterator(com.hp.hpl.jena.rdf.model.ResIterator) InputStreamReader(java.io.InputStreamReader) Reference(org.restlet.data.Reference) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Request(org.restlet.Request) StructureRecordValidator(ambit2.base.data.study.StructureRecordValidator) SubstanceRecord(ambit2.base.data.SubstanceRecord) SubstanceRDFReporter(ambit2.rest.substance.SubstanceRDFReporter) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ProtocolApplication(ambit2.base.data.study.ProtocolApplication) FileOutputStream(java.io.FileOutputStream) Model(com.hp.hpl.jena.rdf.model.Model) IParams(ambit2.base.data.study.IParams) File(java.io.File) Map(java.util.Map) TreeMap(java.util.TreeMap) NanoWikiRDFReader(net.idea.loom.nm.nanowiki.NanoWikiRDFReader) CompositionRelation(ambit2.base.relation.composition.CompositionRelation) Test(org.junit.Test)

Example 4 with NanoWikiRDFReader

use of net.idea.loom.nm.nanowiki.NanoWikiRDFReader in project ambit-mirror by ideaconsult.

the class NanoWikiRDFTest method testRoundtrip.

@Test
public void testRoundtrip() throws Exception {
    NanoWikiRDFReader reader = null;
    Request hack = new Request();
    hack.setRootRef(new Reference("http://localhost/ambit2"));
    SubstanceRDFReporter r = new SubstanceRDFReporter(hack, MediaType.TEXT_RDF_N3);
    Model model = ModelFactory.createDefaultModel();
    r.header(model, null);
    r.setOutput(model);
    try {
        reader = new NanoWikiRDFReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(getNanoWikiFile()))));
        while (reader.hasNext()) {
            IStructureRecord record = reader.nextRecord();
            Assert.assertTrue(record instanceof SubstanceRecord);
            r.processItem((SubstanceRecord) record);
        }
        r.footer(model, null);
        File output = new File(System.getProperty("java.io.tmpdir") + "/" + "nanordf_enm-export.ttl");
        System.out.println("Exported to " + output.getAbsolutePath());
        OutputStream writer = new FileOutputStream(output);
        RDFDataMgr.write(writer, model, RDFFormat.TURTLE);
    } finally {
        if (reader != null)
            reader.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reference(org.restlet.data.Reference) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Request(org.restlet.Request) SubstanceRecord(ambit2.base.data.SubstanceRecord) SubstanceRDFReporter(ambit2.rest.substance.SubstanceRDFReporter) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) IStructureRecord(ambit2.base.interfaces.IStructureRecord) FileOutputStream(java.io.FileOutputStream) Model(com.hp.hpl.jena.rdf.model.Model) File(java.io.File) NanoWikiRDFReader(net.idea.loom.nm.nanowiki.NanoWikiRDFReader) DbUnitTest(ambit2.db.processors.test.DbUnitTest) Test(org.junit.Test)

Example 5 with NanoWikiRDFReader

use of net.idea.loom.nm.nanowiki.NanoWikiRDFReader in project ambit-mirror by ideaconsult.

the class Context method createParser.

protected IRawReader<IStructureRecord> createParser(InputStream stream, boolean xlsx) throws Exception {
    _parsertype mode = getParserType();
    InputStream in = null;
    if (gzipped)
        in = new GZIPInputStream(stream);
    else
        in = stream;
    if (mode == null) {
        logger_cli.log(Level.INFO, "MSG_IMPORT", new Object[] { "parser type not specified, assuming", xlsx ? "xlsx" : "xls" });
        return new GenericExcelParser(in, jsonConfig, xlsx);
    } else
        switch(mode) {
            case enmrdf:
                return new ENanoMapperRDFReader(new InputStreamReader(in), "ENM3");
            case nanowiki:
                return new NanoWikiRDFReader(new InputStreamReader(in));
            default:
                if (jsonConfig == null)
                    throw new FileNotFoundException("JSON config file not specified!");
                return new GenericExcelParser(in, jsonConfig, xlsx, prefix);
        }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GenericExcelParser(net.enanomapper.parser.GenericExcelParser) InputStreamReader(java.io.InputStreamReader) ENanoMapperRDFReader(net.idea.loom.nm.nanowiki.ENanoMapperRDFReader) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) NanoWikiRDFReader(net.idea.loom.nm.nanowiki.NanoWikiRDFReader)

Aggregations

FileInputStream (java.io.FileInputStream)6 InputStreamReader (java.io.InputStreamReader)6 NanoWikiRDFReader (net.idea.loom.nm.nanowiki.NanoWikiRDFReader)6 IStructureRecord (ambit2.base.interfaces.IStructureRecord)5 GZIPInputStream (java.util.zip.GZIPInputStream)5 Test (org.junit.Test)4 SubstanceRecord (ambit2.base.data.SubstanceRecord)3 DbUnitTest (ambit2.db.processors.test.DbUnitTest)3 File (java.io.File)3 StructureRecordValidator (ambit2.base.data.study.StructureRecordValidator)2 SubstanceRDFReporter (ambit2.rest.substance.SubstanceRDFReporter)2 Model (com.hp.hpl.jena.rdf.model.Model)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 GenericExcelParser (net.enanomapper.parser.GenericExcelParser)2 ENanoMapperRDFReader (net.idea.loom.nm.nanowiki.ENanoMapperRDFReader)2 Request (org.restlet.Request)2 Reference (org.restlet.data.Reference)2 LiteratureEntry (ambit2.base.data.LiteratureEntry)1 EffectRecord (ambit2.base.data.study.EffectRecord)1