Example usage for java.sql Clob length

List of usage examples for java.sql Clob length

Introduction

In this page you can find the example usage for java.sql Clob length.

Prototype

long length() throws SQLException;

Source Link

Document

Retrieves the number of characters in the CLOB value designated by this Clob object.

Usage

From source file:uk.ac.ebi.orchem.search.SimilaritySearch.java

/**
 * Similarity score calculation between one database compound and a user's query compound.
 * @param userQuery  query structure in SMILES or MOL
 * @param queryType  MOL or SMILES//from   www.  j  ava  2s  .  c o  m
 * @param compoundId ID of database compound to calculate similarity with
 * @return tanimoto similarity score
 * @throws Exception
 */
public static float singleCompoundSimilarity(Clob userQuery, String queryType, String compoundId)
        throws Exception {

    OracleConnection conn = null;
    PreparedStatement pstmtFp = null;
    ResultSet resFp = null;
    float tanimotoCoeff = 0;

    try {
        //User query
        int clobLen = new Long(userQuery.length()).intValue();
        String query = (userQuery.getSubString(1, clobLen));
        IAtomContainer molecule = null;
        if (queryType.equals(Utils.QUERY_TYPE_MOL)) {
            molecule = MoleculeCreator.getMoleculeFromMolfile(query);
        } else if (queryType.equals(Utils.QUERY_TYPE_SMILES)) {
            SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
            molecule = sp.parseSmiles(query);
        } else
            throw new RuntimeException("Query type not recognized");
        BitSet queryFp = FingerPrinterAgent.FP.getExtendedFingerPrinter().getFingerprint(molecule);
        float queryBitCount = queryFp.cardinality();
        byte[] queryBytes = Utils.toByteArray(queryFp, extFpSize);

        //Database comound
        conn = (OracleConnection) new OracleDriver().defaultConnection();
        String compoundQuery = "select bit_count, fp from orchem_fingprint_simsearch s where id=?";
        pstmtFp = conn.prepareStatement(compoundQuery);
        pstmtFp.setFetchSize(1);
        pstmtFp.setString(1, compoundId);
        resFp = pstmtFp.executeQuery();

        if (resFp.next()) {
            byte[] dbByteArray = resFp.getBytes("fp");
            float compoundBitCount = resFp.getFloat("bit_count");
            tanimotoCoeff = calcTanimoto(queryBytes, queryBytes.length, dbByteArray, queryBitCount,
                    compoundBitCount);
        } else
            throw new RuntimeException("Compound " + compoundId + " not found in similarity table");

    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        if (resFp != null)
            resFp.close();
        if (pstmtFp != null)
            pstmtFp.close();
        if (conn != null)
            conn.close();
    }
    return tanimotoCoeff;
}