Search in sources :

Example 1 with RetrieveStructure

use of ambit2.db.readers.RetrieveStructure in project ambit-mirror by ideaconsult.

the class ARFFIterator method getIterator.

@Override
public Iterator<IStructureRecord> getIterator(File[] target) throws AmbitException {
    try {
        input = new BufferedReader(new FileReader(target[0]));
        results = new BufferedReader(new FileReader(target[1]));
        arffInput = new ArffReader(input, 1);
        arffResults = new ArffReader(results, 1);
        dataInput = arffInput.getStructure();
        dataResults = arffResults.getStructure();
        properties = new Property[dataResults.numAttributes()];
        for (int i = 0; i < dataResults.numAttributes(); i++) {
            Attribute attribute = dataResults.attribute(i);
            RDFPropertyIterator reader = null;
            Property p = null;
            try {
                reader = new RDFPropertyIterator(new Reference(attribute.name()), "arff");
                reader.setBaseReference(baseReference);
                while (reader.hasNext()) {
                    p = reader.next();
                }
            } catch (Exception x) {
                p = null;
            // p.setId((Integer)OpenTox.URI.feature.getId(attribute.name(), baseReference));
            } finally {
                try {
                    reader.close();
                } catch (Exception x) {
                }
            }
            properties[i] = p;
        }
        return new Iterator<IStructureRecord>() {

            public boolean hasNext() {
                try {
                    inputInstance = arffInput.readInstance(dataInput);
                    resultInstance = arffResults.readInstance(dataResults);
                    return (inputInstance != null) && (resultInstance != null);
                } catch (Exception x) {
                    return false;
                }
            }

            public IStructureRecord next() {
                record.clear();
                Object[] ids = OpenTox.URI.conformer.getIds(inputInstance.toString(0), baseReference);
                if ((ids != null) && (ids[0] != null) && (ids[1] != null)) {
                    record.setIdchemical((Integer) ids[0]);
                    record.setIdstructure((Integer) ids[1]);
                } else {
                    Object id = OpenTox.URI.compound.getId(inputInstance.toString(0), baseReference);
                    if (id != null) {
                        record.setIdchemical((Integer) id);
                        record.setIdstructure(-1);
                    } else {
                    // retrieveStructure(uris[index]);
                    }
                }
                for (int i = 0; i < properties.length; i++) if (properties[i] != null) {
                    if (dataResults.attribute(i).isNumeric())
                        record.setRecordProperty(properties[i], resultInstance.value(i));
                    else
                        record.setRecordProperty(properties[i], resultInstance.toString(i));
                }
                return record;
            }

            public void remove() {
            }
        };
    } catch (FileNotFoundException x) {
        throw new AmbitException(x);
    } catch (Exception x) {
        throw new AmbitException(x);
    }
}
Also used : Attribute(weka.core.Attribute) RDFPropertyIterator(ambit2.rest.rdf.RDFPropertyIterator) Reference(org.restlet.data.Reference) ArffReader(weka.core.converters.ArffLoader.ArffReader) FileNotFoundException(java.io.FileNotFoundException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) FileNotFoundException(java.io.FileNotFoundException) BufferedReader(java.io.BufferedReader) Iterator(java.util.Iterator) RDFPropertyIterator(ambit2.rest.rdf.RDFPropertyIterator) FileReader(java.io.FileReader) Property(ambit2.base.data.Property) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 2 with RetrieveStructure

use of ambit2.db.readers.RetrieveStructure in project ambit-mirror by ideaconsult.

the class InChIChemicalsTableWriterTest method testProcess.

@Test
public void testProcess() throws Exception {
    StructureNormalizer normalizer = new StructureNormalizer();
    // no inchis as well
    setUpDatabaseFromResource("ambit2/db/processors/test/dataset_nofp.xml");
    IDatabaseConnection dbConnection = getConnection();
    ITable chemicals = dbConnection.createQueryTable("EXPECTED_FP", "SELECT * FROM chemicals where inchi is null");
    Assert.assertEquals(5, chemicals.getRowCount());
    RepositoryReader reader = new RepositoryReader();
    RetrieveStructure molReader = new RetrieveStructure(true);
    reader.setConnection(dbConnection.getConnection());
    InChIChemicalsWriter inchiWriter = new InChIChemicalsWriter();
    inchiWriter.setConnection(dbConnection.getConnection());
    inchiWriter.open();
    reader.open();
    int records = 0;
    IStructureRecord o;
    QueryExecutor<RetrieveStructure> exec = new QueryExecutor<RetrieveStructure>();
    exec.setConnection(dbConnection.getConnection());
    int errors = 0;
    while (reader.hasNext()) {
        o = reader.next();
        String content = reader.getStructure(o.getIdstructure());
        if (content == null)
            continue;
        molReader.setValue(o);
        ResultSet rs = exec.process(molReader);
        while (rs.next()) {
            IStructureRecord record = molReader.getObject(rs);
            try {
                record = normalizer.process(record);
                inchiWriter.process(record);
            } catch (Exception x) {
                errors++;
            }
        }
        rs.close();
        o.clear();
        records++;
    }
    reader.close();
    chemicals = dbConnection.createQueryTable("EXPECTED_FP", "SELECT count(*) as c FROM chemicals where inchi is not null");
    Assert.assertEquals(new BigInteger("4"), chemicals.getValue(0, "c"));
    inchiWriter.close();
}
Also used : StructureNormalizer(ambit2.core.processors.StructureNormalizer) InChIChemicalsWriter(ambit2.db.update.chemical.InChIChemicalsWriter) IStructureRecord(ambit2.base.interfaces.IStructureRecord) QueryExecutor(ambit2.db.search.QueryExecutor) ResultSet(java.sql.ResultSet) BigInteger(java.math.BigInteger) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) RetrieveStructure(ambit2.db.readers.RetrieveStructure) RepositoryReader(ambit2.db.RepositoryReader) Test(org.junit.Test)

Example 3 with RetrieveStructure

use of ambit2.db.readers.RetrieveStructure in project ambit-mirror by ideaconsult.

the class Structure_crud_test method createVerify.

@Override
protected void createVerify(IQueryUpdate<Object, IStructureRecord> query) throws Exception {
    IDatabaseConnection c = getConnection();
    QueryStructure q = new QueryStructure();
    q.setFieldname(ExactStructureSearchMode.idchemical);
    q.setValue("10");
    RetrieveStructure r = new RetrieveStructure();
    // structure
    r.setFieldname(false);
    QueryExecutor execR = new QueryExecutor();
    execR.setConnection(c.getConnection());
    QueryExecutor exec = new QueryExecutor();
    exec.setConnection(c.getConnection());
    ResultSet rs = exec.process(q);
    int count = 0;
    while (rs.next()) {
        IStructureRecord record = q.getObject(rs);
        r.setValue(record);
        ResultSet rs1 = execR.process(r);
        while (rs1.next()) {
            record = r.getObject(rs1);
            if ("content".equals(record.getContent()))
                count++;
        }
        execR.closeResults(rs1);
    }
    exec.closeResults(rs);
    Assert.assertEquals(1, count);
    c.close();
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) QueryExecutor(ambit2.db.search.QueryExecutor) ResultSet(java.sql.ResultSet) QueryStructure(ambit2.db.search.structure.QueryStructure) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) RetrieveStructure(ambit2.db.readers.RetrieveStructure)

Example 4 with RetrieveStructure

use of ambit2.db.readers.RetrieveStructure in project ambit-mirror by ideaconsult.

the class Structure_crud_test method createVerifyNew.

@Override
protected void createVerifyNew(IQueryUpdate<Object, IStructureRecord> query) throws Exception {
    IDatabaseConnection c = getConnection();
    ITable table = c.createQueryTable("EXPECTED", "SELECT idstructure,idchemical,type_structure FROM structure");
    Assert.assertTrue(table.getRowCount() > 0);
    RetrieveStructure r = new RetrieveStructure();
    r.setPreferedStructure(false);
    r.setFieldname(false);
    QueryExecutor execR = new QueryExecutor();
    execR.setConnection(c.getConnection());
    int count = 0;
    for (int i = 0; i < table.getRowCount(); i++) {
        IStructureRecord record = new StructureRecord();
        record.setIdstructure(Integer.parseInt(table.getValue(i, "idstructure").toString()));
        try {
            Object t = table.getValue(i, "type_structure");
            for (STRUC_TYPE type : STRUC_TYPE.values()) if (type.toString().equals(t)) {
                record.setType(type);
                break;
            }
        } catch (Exception x) {
            record.setType(STRUC_TYPE.NA);
        }
        r.setValue(record);
        ResultSet rs1 = execR.process(r);
        while (rs1.next()) {
            record = r.getObject(rs1);
            if ("content".equals(record.getContent()))
                count++;
        }
        execR.closeResults(rs1);
    }
    Assert.assertEquals(1, count);
    c.close();
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) QueryExecutor(ambit2.db.search.QueryExecutor) IStructureRecord(ambit2.base.interfaces.IStructureRecord) StructureRecord(ambit2.base.data.StructureRecord) STRUC_TYPE(ambit2.base.interfaces.IStructureRecord.STRUC_TYPE) ResultSet(java.sql.ResultSet) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) RetrieveStructure(ambit2.db.readers.RetrieveStructure)

Example 5 with RetrieveStructure

use of ambit2.db.readers.RetrieveStructure in project ambit-mirror by ideaconsult.

the class StructureRecordXLSXReporter method configureProcessors.

@Override
protected void configureProcessors(String baseRef, boolean includeMol) {
    if (includeMol) {
        RetrieveStructure r = new RetrieveStructure();
        r.setPage(0);
        r.setPageSize(1);
        getProcessors().add(new ProcessorStructureRetrieval(r));
    }
    configurePropertyProcessors();
    getProcessors().add(new ProcessorStructureRetrieval(new QuerySmilesByID()));
    // configureCollectionProcessors(baseRef);
    getProcessors().add(new DefaultAmbitProcessor<IStructureRecord, IStructureRecord>() {

        private static final long serialVersionUID = -7316415661186235101L;

        @Override
        public IStructureRecord process(IStructureRecord target) throws Exception {
            processItem(target);
            return target;
        }
    });
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) QuerySmilesByID(ambit2.db.search.QuerySmilesByID) ProcessorStructureRetrieval(ambit2.db.processors.ProcessorStructureRetrieval) RetrieveStructure(ambit2.db.readers.RetrieveStructure) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException) IOException(java.io.IOException)

Aggregations

IStructureRecord (ambit2.base.interfaces.IStructureRecord)13 RetrieveStructure (ambit2.db.readers.RetrieveStructure)13 ProcessorStructureRetrieval (ambit2.db.processors.ProcessorStructureRetrieval)6 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)6 ResultSet (java.sql.ResultSet)5 IBatchStatistics (net.idea.modbcum.i.batch.IBatchStatistics)5 IProcessor (net.idea.modbcum.i.processors.IProcessor)5 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)5 QueryExecutor (ambit2.db.search.QueryExecutor)4 ProcessorsChain (net.idea.modbcum.i.processors.ProcessorsChain)4 ITable (org.dbunit.dataset.ITable)4 StructureNormalizer (ambit2.core.processors.StructureNormalizer)3 FP1024Writer (ambit2.db.processors.FP1024Writer)3 BitSetGenerator (ambit2.descriptors.processors.BitSetGenerator)3 Test (org.junit.Test)3 Property (ambit2.base.data.Property)2 StructureRecord (ambit2.base.data.StructureRecord)2 RepositoryReader (ambit2.db.RepositoryReader)2 QuerySmilesByID (ambit2.db.search.QuerySmilesByID)2 InChIChemicalsWriter (ambit2.db.update.chemical.InChIChemicalsWriter)2