Search in sources :

Example 1 with StructureRecordValidator

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

the class ISATABImportTest method testWriteISATAB.

@Test
public void testWriteISATAB() throws Exception {
    setUpDatabase("src/test/resources/ambit2/db/processors/test/empty-datasets.xml");
    IDatabaseConnection c = getConnection();
    IRawReader<IStructureRecord> parser = null;
    try {
        LiteratureEntry entry = new LiteratureEntry("ISA-TAB", "test");
        entry.setType(_type.Dataset);
        String root = "C://ToxBank//ISAcreator.SEURAT-v1.7.2//ISAcreator.SEURAT";
        // String investigation = root + "//isatab files//BII-I-1";
        String investigation = root + "//isatab files//qHTSexample";
        ISAConfigurationSet.setConfigPath(root + "//Configurations/toxbank-config");
        parser = new ISAReader(new File(investigation));
        validator = new StructureRecordValidator(investigation, true);
        write(parser, c.getConnection(), new ReferenceSubstanceUUID(), false);
    } finally {
        parser.close();
        c.close();
    }
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) ReferenceSubstanceUUID(ambit2.core.processors.structure.key.ReferenceSubstanceUUID) LiteratureEntry(ambit2.base.data.LiteratureEntry) ILiteratureEntry(ambit2.base.data.ILiteratureEntry) StructureRecordValidator(ambit2.base.data.study.StructureRecordValidator) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) File(java.io.File) ISAReader(net.idea.loom.isa.ISAReader) DbUnitTest(ambit2.db.processors.test.DbUnitTest) Test(org.junit.Test)

Example 2 with StructureRecordValidator

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

the class Context method validate.

protected void validate(StructureRecordValidator validator, IStructureRecord record) throws Exception {
    if (validator != null) {
        if (record instanceof SubstanceRecord) {
            _parsertype mode = getParserType();
            SubstanceRecord srecord = (SubstanceRecord) record;
            if (mode == null) {
                cleanReferenceStructure(srecord);
                List<ProtocolApplication> m = srecord.getMeasurements();
                cleanupEmptyRecords(srecord, m);
                validator.process((IStructureRecord) record);
            } else
                switch(mode) {
                    case i5z:
                        {
                            if (validator != null)
                                validator.process((IStructureRecord) record);
                            break;
                        }
                    case i6z:
                        {
                            if (validator != null)
                                validator.process((IStructureRecord) record);
                            break;
                        }
                    case xlsx:
                        {
                            cleanReferenceStructure(srecord);
                            List<ProtocolApplication> m = srecord.getMeasurements();
                            cleanupEmptyRecords(srecord, m);
                            validator.process((IStructureRecord) record);
                            break;
                        }
                    case xls:
                        {
                            cleanReferenceStructure(srecord);
                            List<ProtocolApplication> m = srecord.getMeasurements();
                            cleanupEmptyRecords(srecord, m);
                            validator.process((IStructureRecord) record);
                            break;
                        }
                    case nanowiki:
                        {
                            validator.process((IStructureRecord) record);
                            break;
                        }
                    default:
                        {
                            cleanReferenceStructure(srecord);
                            List<ProtocolApplication> m = srecord.getMeasurements();
                            cleanupEmptyRecords(srecord, m);
                            validator.process((IStructureRecord) record);
                            break;
                        }
                }
        }
    }
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) ProtocolApplication(ambit2.base.data.study.ProtocolApplication) SubstanceRecord(ambit2.base.data.SubstanceRecord)

Example 3 with StructureRecordValidator

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

the class Context method importFile.

protected int importFile(boolean splitRecord, final boolean xlsx, boolean importBundles) throws Exception {
    IRawReader<IStructureRecord> parser = null;
    Connection c = null;
    try (FileInputStream fin = new FileInputStream(inputFile)) {
        parser = createParser(fin, xlsx);
        logger_cli.log(Level.INFO, "MSG_IMPORT", new Object[] { parser.getClass().getName(), inputFile.getAbsolutePath() });
        StructureRecordValidator validator = createValidator(xlsx);
        IProcessor<IStructureRecord, IStructureRecord> mapper = createMapper(xlsx);
        DBConnectionConfigurable<Context> dbc = null;
        dbc = getConnection(getConfigFile());
        c = dbc.getConnection();
        c.setAutoCommit(true);
        return write(parser, c, matchByKey, splitRecord, clearMeasurements, clearComposition, validator, mapper, false, importBundles);
    } catch (Exception x) {
        throw x;
    } finally {
        if (parser != null)
            parser.close();
        try {
            c.close();
        } catch (Exception x) {
        }
    }
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) Connection(java.sql.Connection) MySQLSingleConnection(net.idea.modbcum.c.MySQLSingleConnection) StructureRecordValidator(ambit2.base.data.study.StructureRecordValidator) FileInputStream(java.io.FileInputStream) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) CommunicationsException(com.mysql.jdbc.CommunicationsException) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) ConnectException(java.net.ConnectException) IOException(java.io.IOException)

Example 4 with StructureRecordValidator

use of ambit2.base.data.study.StructureRecordValidator 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 5 with StructureRecordValidator

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

the class Context method write.

public int write(IRawReader<IStructureRecord> reader, Connection connection, IStructureKey key, boolean splitRecord, boolean clearMeasurements, boolean clearComposition, StructureRecordValidator validator, IProcessor<IStructureRecord, IStructureRecord> mapper, boolean i5mode, boolean importBundles) throws Exception {
    DBSubstanceWriter writer = new DBSubstanceWriter(DBSubstanceWriter.datasetMeta(), new SubstanceRecord(), clearMeasurements, clearComposition, key);
    writer.setImportBundles(importBundles);
    writer.setI5mode(i5mode);
    writer.setSplitRecord(splitRecord);
    writer.setConnection(connection);
    writer.setClearComposition(clearComposition);
    writer.setClearMeasurements(clearMeasurements);
    writer.setKeepEffectRecordsForDocUUID(keepEffectRecords);
    writer.open();
    int records = 0;
    try {
        while (reader.hasNext()) {
            Object record = reader.next();
            if (record == null)
                continue;
            if (record instanceof SubstanceRecord)
                configure(writer, (SubstanceRecord) record);
            if (mapper != null)
                record = map(mapper, (IStructureRecord) record);
            validate(validator, (IStructureRecord) record);
            writer.process((IStructureRecord) record);
            records++;
        }
    } catch (Exception x) {
        x.printStackTrace();
    } finally {
        writer.close();
        logger_cli.log(Level.INFO, "MSG_IMPORTED", new Object[] { records });
    }
    return records;
}
Also used : DBSubstanceWriter(ambit2.db.substance.processor.DBSubstanceWriter) SubstanceRecord(ambit2.base.data.SubstanceRecord) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) CommunicationsException(com.mysql.jdbc.CommunicationsException) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) ConnectException(java.net.ConnectException) IOException(java.io.IOException)

Aggregations

StructureRecordValidator (ambit2.base.data.study.StructureRecordValidator)6 IStructureRecord (ambit2.base.interfaces.IStructureRecord)5 SubstanceRecord (ambit2.base.data.SubstanceRecord)4 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)4 ProtocolApplication (ambit2.base.data.study.ProtocolApplication)3 CommunicationsException (com.mysql.jdbc.CommunicationsException)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 ConnectException (java.net.ConnectException)3 SQLException (java.sql.SQLException)3 LiteratureEntry (ambit2.base.data.LiteratureEntry)2 CompositionRelation (ambit2.base.relation.composition.CompositionRelation)2 DBSubstanceWriter (ambit2.db.substance.processor.DBSubstanceWriter)2 InputStreamReader (java.io.InputStreamReader)2 Connection (java.sql.Connection)2 GenericExcelParser (net.enanomapper.parser.GenericExcelParser)2 I5ZReader (net.idea.i5.io.I5ZReader)2 I6ZReader (net.idea.i6.io.I6ZReader)2