Search in sources :

Example 26 with RawIteratingSDFReader

use of ambit2.core.io.RawIteratingSDFReader in project ambit-mirror by ideaconsult.

the class RepositoryWriterTest method testWriteBondOrder4.

@Test
public void testWriteBondOrder4() throws Exception {
    setUpDatabaseFromResource("ambit2/db/processors/test/empty-datasets.xml");
    IDatabaseConnection c = getConnection();
    ITable chemicals = c.createQueryTable("EXPECTED", "SELECT * FROM chemicals");
    Assert.assertEquals(0, chemicals.getRowCount());
    ITable strucs = c.createQueryTable("EXPECTED", "SELECT * FROM structure");
    Assert.assertEquals(0, strucs.getRowCount());
    ITable srcdataset = c.createQueryTable("EXPECTED", "SELECT * FROM src_dataset");
    Assert.assertEquals(0, srcdataset.getRowCount());
    ITable struc_src = c.createQueryTable("EXPECTED", "SELECT * FROM struc_dataset");
    Assert.assertEquals(0, struc_src.getRowCount());
    ITable property = c.createQueryTable("EXPECTED", "SELECT * FROM properties");
    Assert.assertEquals(0, property.getRowCount());
    ITable property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values");
    Assert.assertEquals(0, property_values.getRowCount());
    InputStream in = this.getClass().getClassLoader().getResourceAsStream("ambit2/db/processors/sdf/warfarin_aromatic.sdf");
    Assert.assertNotNull(in);
    RawIteratingSDFReader reader = new RawIteratingSDFReader(new InputStreamReader(in));
    reader.setReference(LiteratureEntry.getInstance("warfarin_aromatic.sdf"));
    write(reader, c.getConnection());
    c.close();
    c = getConnection();
    chemicals = c.createQueryTable("EXPECTED", "SELECT * FROM chemicals");
    Assert.assertEquals(1, chemicals.getRowCount());
    chemicals = c.createQueryTable("EXPECTED", "SELECT * FROM chemicals where smiles is not null and inchi is not null and formula is not null");
    Assert.assertEquals(1, chemicals.getRowCount());
    IStructureRecord structure = new StructureRecord();
    strucs = c.createQueryTable("EXPECTED", "SELECT idchemical,idstructure FROM structure");
    Assert.assertEquals(2, strucs.getRowCount());
    structure.setIdchemical(((BigInteger) strucs.getValue(0, "idchemical")).intValue());
    structure.setIdstructure(((BigInteger) strucs.getValue(0, "idstructure")).intValue());
    RetrieveStructure q = new RetrieveStructure();
    MoleculeReader molreader = new MoleculeReader();
    q.setValue(structure);
    QueryExecutor x = new QueryExecutor<>();
    try {
        x.setConnection(c.getConnection());
        x.setCloseConnection(false);
        ResultSet rs = x.process(q);
        while (rs.next()) {
            IStructureRecord result = q.getObject(rs);
            IAtomContainer mol = molreader.process(result);
            for (IBond bond : mol.bonds()) {
                Assert.assertNotSame(IBond.Order.UNSET, bond.getOrder());
            }
        }
    } finally {
        x.close();
    }
    c.close();
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) RawIteratingSDFReader(ambit2.core.io.RawIteratingSDFReader) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IBond(org.openscience.cdk.interfaces.IBond) IStructureRecord(ambit2.base.interfaces.IStructureRecord) MoleculeReader(ambit2.core.processors.structure.MoleculeReader) StructureRecord(ambit2.base.data.StructureRecord) IStructureRecord(ambit2.base.interfaces.IStructureRecord) QueryExecutor(net.idea.modbcum.p.QueryExecutor) ResultSet(java.sql.ResultSet) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) RetrieveStructure(ambit2.db.readers.RetrieveStructure) Test(org.junit.Test)

Example 27 with RawIteratingSDFReader

use of ambit2.core.io.RawIteratingSDFReader in project ambit-mirror by ideaconsult.

the class RepositoryWriterTest method testWriteEmptySmiles.

@Test
public void testWriteEmptySmiles() throws Exception {
    setUpDatabaseFromResource("ambit2/db/processors/test/empty-datasets.xml");
    IDatabaseConnection c = getConnection();
    ITable chemicals = c.createQueryTable("EXPECTED", "SELECT * FROM chemicals");
    Assert.assertEquals(0, chemicals.getRowCount());
    ITable strucs = c.createQueryTable("EXPECTED", "SELECT * FROM structure");
    Assert.assertEquals(0, strucs.getRowCount());
    ITable srcdataset = c.createQueryTable("EXPECTED", "SELECT * FROM src_dataset");
    Assert.assertEquals(0, srcdataset.getRowCount());
    ITable struc_src = c.createQueryTable("EXPECTED", "SELECT * FROM struc_dataset");
    Assert.assertEquals(0, struc_src.getRowCount());
    ITable property = c.createQueryTable("EXPECTED", "SELECT * FROM properties");
    Assert.assertEquals(0, property.getRowCount());
    ITable property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values");
    Assert.assertEquals(0, property_values.getRowCount());
    InputStream in = this.getClass().getClassLoader().getResourceAsStream("ambit2/db/processors/test/problem.sdf");
    Assert.assertNotNull(in);
    RawIteratingSDFReader reader = new RawIteratingSDFReader(new InputStreamReader(in));
    reader.setReference(LiteratureEntry.getInstance("Empty smiles"));
    write(reader, c.getConnection());
    c.close();
    c = getConnection();
    chemicals = c.createQueryTable("EXPECTED", "SELECT * FROM chemicals");
    Assert.assertEquals(3, chemicals.getRowCount());
    strucs = c.createQueryTable("EXPECTED", "SELECT * FROM structure");
    Assert.assertEquals(3, strucs.getRowCount());
    srcdataset = c.createQueryTable("EXPECTED", "SELECT * FROM src_dataset where name='TEST INPUT'");
    Assert.assertEquals(1, srcdataset.getRowCount());
    struc_src = c.createQueryTable("EXPECTED", "SELECT * FROM struc_dataset");
    Assert.assertEquals(3, struc_src.getRowCount());
    property = c.createQueryTable("EXPECTED", "SELECT * FROM properties");
    Assert.assertEquals(12, property.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values");
    Assert.assertEquals(36, property_values.getRowCount());
    srcdataset = c.createQueryTable("EXPECTED", "SELECT * FROM src_dataset join template_def using(idtemplate) where name='TEST INPUT'");
    Assert.assertEquals(12, srcdataset.getRowCount());
    c.close();
/**
 * Removing redundant properties insert ignore into property_values
 * select id,idproperty,idstructure,idvalue,idtype,user_name,status from
 * property_values where idstructure>3 on duplicate key update
 * idstructure=3 delete from property_values where idstructure>3
 *
 * insert ignore into struc_dataset select idstructure,id_srcdataset
 * from struc_dataset where idstructure>3 on duplicate key update
 * idstructure=3 delete from struc_dataset where idstructure>3
 */
}
Also used : RawIteratingSDFReader(ambit2.core.io.RawIteratingSDFReader) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) Test(org.junit.Test)

Example 28 with RawIteratingSDFReader

use of ambit2.core.io.RawIteratingSDFReader in project ambit-mirror by ideaconsult.

the class RepositoryWriterTest method testImport3Same.

@Test
public void testImport3Same() throws Exception {
    setUpDatabaseFromResource("ambit2/db/processors/test/empty-datasets.xml");
    IDatabaseConnection c = getConnection();
    InputStream in = this.getClass().getClassLoader().getResourceAsStream("ambit2/db/processors/match/99-72-9.sdf");
    Assert.assertNotNull(in);
    RawIteratingSDFReader reader = new RawIteratingSDFReader(new InputStreamReader(in));
    reader.setReference(LiteratureEntry.getInstance("input.sdf"));
    write(reader, c.getConnection());
    c.close();
    c = getConnection();
    ITable chemicals = c.createQueryTable("EXPECTED", "SELECT * FROM chemicals");
    Assert.assertEquals(1, chemicals.getRowCount());
    chemicals = c.createQueryTable("EXPECTED", "SELECT * FROM chemicals where smiles is not null and inchi is not null and formula is not null");
    Assert.assertEquals(1, chemicals.getRowCount());
    ITable strucs = c.createQueryTable("EXPECTED", "SELECT * FROM structure");
    Assert.assertEquals(2, strucs.getRowCount());
    ITable srcdataset = c.createQueryTable("EXPECTED", "SELECT id_srcdataset,idtemplate FROM src_dataset where name='TEST INPUT'");
    Assert.assertEquals(1, srcdataset.getRowCount());
    in = this.getClass().getClassLoader().getResourceAsStream("ambit2/db/processors/match/99-72-9.csv");
    Assert.assertNotNull(in);
    IIteratingChemObjectReader reader1 = FileInputState.getReader(in, "99-72-9.csv");
    RawIteratingWrapper wrapper = new RawIteratingWrapper(reader1);
    wrapper.setReference(LiteratureEntry.getInstance("input.csv"));
    write(wrapper, c.getConnection());
    c = getConnection();
    chemicals = c.createQueryTable("EXPECTED", "SELECT * FROM chemicals");
    Assert.assertEquals(3, chemicals.getRowCount());
    // Should be 2, but structures from SDF and SMILES come with different
    // stereo ...
    // Assert.assertEquals(2,chemicals.getRowCount());
    c.close();
}
Also used : RawIteratingSDFReader(ambit2.core.io.RawIteratingSDFReader) InputStreamReader(java.io.InputStreamReader) RawIteratingWrapper(ambit2.core.io.RawIteratingWrapper) IIteratingChemObjectReader(org.openscience.cdk.io.iterator.IIteratingChemObjectReader) InputStream(java.io.InputStream) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) Test(org.junit.Test)

Example 29 with RawIteratingSDFReader

use of ambit2.core.io.RawIteratingSDFReader in project ambit-mirror by ideaconsult.

the class RepositoryWriterTest method testImportTox21.

@Test
public void testImportTox21() throws Exception {
    setUpDatabaseFromResource("ambit2/db/processors/test/empty-datasets.xml");
    IDatabaseConnection c = getConnection();
    ITable chemicals = c.createQueryTable("EXPECTED", "SELECT * FROM chemicals");
    Assert.assertEquals(0, chemicals.getRowCount());
    ITable strucs = c.createQueryTable("EXPECTED", "SELECT * FROM structure");
    Assert.assertEquals(0, strucs.getRowCount());
    ITable srcdataset = c.createQueryTable("EXPECTED", "SELECT * FROM src_dataset");
    Assert.assertEquals(0, srcdataset.getRowCount());
    ITable struc_src = c.createQueryTable("EXPECTED", "SELECT * FROM struc_dataset");
    Assert.assertEquals(0, struc_src.getRowCount());
    ITable property = c.createQueryTable("EXPECTED", "SELECT * FROM properties");
    Assert.assertEquals(0, property.getRowCount());
    ITable property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values");
    Assert.assertEquals(0, property_values.getRowCount());
    InputStream in = this.getClass().getClassLoader().getResourceAsStream("ambit2/core/pubchem/tox21_excerpt.sdf");
    Assert.assertNotNull(in);
    IRawReader<IStructureRecord> reader = new RawIteratingSDFReader(new InputStreamReader(in));
    // reader.setReference("predictions.sdf");
    write((RawIteratingSDFReader) reader, c.getConnection(), new CASKey());
    reader.close();
    c.close();
    c = getConnection();
    chemicals = c.createQueryTable("EXPECTED", "SELECT * FROM chemicals");
    Assert.assertEquals(1, chemicals.getRowCount());
    strucs = c.createQueryTable("EXPECTED", "SELECT * FROM structure");
    Assert.assertEquals(3, strucs.getRowCount());
    // srcdataset =
    // c.createQueryTable("EXPECTED","SELECT * FROM src_dataset where name='Imported properties'");
    // Assert.assertEquals(1,srcdataset.getRowCount());
    // struc_src =
    // c.createQueryTable("EXPECTED","SELECT * FROM struc_dataset join src_dataset using(id_srcdataset) where name='Imported properties'");
    // Assert.assertEquals(1,struc_src.getRowCount());
    property = c.createQueryTable("EXPECTED", "SELECT * FROM properties");
    Assert.assertEquals(19, property.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values");
    Assert.assertEquals(57, property_values.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = 'PUBCHEM_SID'");
    Assert.assertEquals(3, property_values.getRowCount());
    // ITable p_tuples =
    // c.createQueryTable("EXPECTED","SELECT * FROM property_tuples join tuples using(idtuple) join src_dataset using(id_srcdataset) where name='Imported properties'");
    // Assert.assertEquals(66,p_tuples.getRowCount());
    c.close();
    c = getConnection();
    in = this.getClass().getClassLoader().getResourceAsStream("ambit2/core/pubchem/aid720516.csv");
    Assert.assertNotNull(in);
    reader = new RawIteratingWrapper<IIteratingChemObjectReader>(FileInputState.getReader(in, ".csv"));
    // reader.setReference(LiteratureEntry.getDXReference());
    write(reader, c.getConnection(), new PubchemSID(), true);
    reader.close();
    c = getConnection();
    strucs = c.createQueryTable("EXPECTED", "SELECT * FROM structure");
    // Assert.assertEquals(3,strucs.getRowCount());
    property = c.createQueryTable("EXPECTED", "SELECT * FROM properties");
    // does not write
    Assert.assertEquals(19 + 9, property.getRowCount());
    // another
    // PUBCHEM_SID
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = 'PUBCHEM_CID'");
    Assert.assertEquals(3, property_values.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = 'PUBCHEM_ACTIVITY_OUTCOME'");
    Assert.assertEquals(3, property_values.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = 'PUBCHEM_ACTIVITY_SCORE'");
    Assert.assertEquals(3, property_values.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = '1^Activity_Summary^STRING^^^^'");
    Assert.assertEquals(3, property_values.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = '2^ATAD5_Activity^STRING^^^^'");
    Assert.assertEquals(3, property_values.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = '3^ATAD5 Potency (uM)^FLOAT^uM^AC^^'");
    Assert.assertEquals(0, property_values.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = '4^ATAD5_Efficacy_(_)^FLOAT^_^^^'");
    Assert.assertEquals(3, property_values.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = '5^Viability_Activity^STRING^^^^'");
    Assert.assertEquals(3, property_values.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = '6^Viability_Potency (uM)^FLOAT^uM^^^'");
    Assert.assertEquals(0, property_values.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = '7^Viability_Efficacy_(_)^FLOAT^_^^^'");
    Assert.assertEquals(3, property_values.getRowCount());
    property_values = c.createQueryTable("EXPECTED", "SELECT * FROM property_values join properties using(idproperty) where name = '8^Sample_Source^STRING^^^^'");
    Assert.assertEquals(3, property_values.getRowCount());
    // Assert.assertEquals(57+9*3,property_values.getRowCount());
    c.close();
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) RawIteratingSDFReader(ambit2.core.io.RawIteratingSDFReader) InputStreamReader(java.io.InputStreamReader) PubchemSID(ambit2.core.processors.structure.key.PubchemSID) CASKey(ambit2.core.processors.structure.key.CASKey) IIteratingChemObjectReader(org.openscience.cdk.io.iterator.IIteratingChemObjectReader) InputStream(java.io.InputStream) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) Test(org.junit.Test)

Example 30 with RawIteratingSDFReader

use of ambit2.core.io.RawIteratingSDFReader in project ambit-mirror by ideaconsult.

the class PUGProcessor method processDownloadURL.

protected static List<IStructureRecord> processDownloadURL(Document doc, Element download) throws ProcessorException {
    List<IStructureRecord> result = new ArrayList<IStructureRecord>();
    Element urlNode = getNodes(doc, download, tag_PCT_Download_URL);
    Element url = getNodes(doc, urlNode, tag_PCT_Download_URL_url);
    try {
        URL theUrl = new URL(url.getTextContent());
        URLConnection connection = theUrl.openConnection();
        RawIteratingSDFReader reader = new RawIteratingSDFReader(new InputStreamReader(new GZIPInputStream(connection.getInputStream())));
        while (reader.hasNext()) {
            Object o = reader.next();
            if (o instanceof IStructureRecord) {
                result.add((IStructureRecord) o);
            } else
                result.add(new StructureRecord(-1, -1, o.toString(), PCT_download_format[PCT_download_format_sdf]));
        }
    /*
	        StringBuilder b = new StringBuilder();
	        while((input = in.readLine()) != null) {
	            b.append(input);
	            b.append("\r");  
	        }
	        result.add(new StructureRecord(-1,-1,b.toString(),PCT_download_format[PCT_download_format_sdf]));
	        */
    } catch (CDKException x) {
        throw new ProcessorException(null, x);
    } catch (IOException x) {
        throw new ProcessorException(null, x);
    }
    return result;
}
Also used : ProcessorException(ambit2.base.processors.ProcessorException) RawIteratingSDFReader(ambit2.core.io.RawIteratingSDFReader) InputStreamReader(java.io.InputStreamReader) CDKException(org.openscience.cdk.exception.CDKException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) GZIPInputStream(java.util.zip.GZIPInputStream) IStructureRecord(ambit2.base.interfaces.IStructureRecord) StructureRecord(ambit2.base.data.StructureRecord) IStructureRecord(ambit2.base.interfaces.IStructureRecord)

Aggregations

RawIteratingSDFReader (ambit2.core.io.RawIteratingSDFReader)30 InputStreamReader (java.io.InputStreamReader)26 Test (org.junit.Test)21 IStructureRecord (ambit2.base.interfaces.IStructureRecord)19 InputStream (java.io.InputStream)18 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)13 ITable (org.dbunit.dataset.ITable)12 MoleculeReader (ambit2.core.processors.structure.MoleculeReader)7 FileReader (java.io.FileReader)6 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)6 File (java.io.File)5 IIteratingChemObjectReader (org.openscience.cdk.io.iterator.IIteratingChemObjectReader)5 IOException (java.io.IOException)4 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)4 AtomConfigurator (ambit2.core.processors.structure.AtomConfigurator)3 BitSet (java.util.BitSet)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 Property (ambit2.base.data.Property)2 StructureRecord (ambit2.base.data.StructureRecord)2 IteratingDelimitedFileReader (ambit2.core.io.IteratingDelimitedFileReader)2