|
| 1 | +/* |
| 2 | + * Copyright (C)2014, Novartis Institutes for BioMedical Research Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * - Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * - Redistributions in binary form must reproduce the above |
| 13 | + * copyright notice, this list of conditions and the following |
| 14 | + * disclaimer in the documentation and/or other materials provided |
| 15 | + * with the distribution. |
| 16 | + * |
| 17 | + * - Neither the name of Novartis Institutes for BioMedical Research Inc. |
| 18 | + * nor the names of its contributors may be used to endorse or promote |
| 19 | + * products derived from this software without specific prior written permission. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | + */ |
| 33 | +package org.rdkit.fingerprint; |
| 34 | + |
| 35 | +import java.util.BitSet; |
| 36 | +import lombok.AllArgsConstructor; |
| 37 | +import lombok.Data; |
| 38 | +import lombok.NonNull; |
| 39 | +import lombok.val; |
| 40 | +import org.RDKit.ExplicitBitVect; |
| 41 | +import org.RDKit.RDKFuncs; |
| 42 | +import org.RDKit.ROMol; |
| 43 | +import org.RDKit.RWMol; |
| 44 | +import org.rdkit.neo4j.utils.RWMolCloseable; |
| 45 | +import org.slf4j.Logger; |
| 46 | +import org.slf4j.LoggerFactory; |
| 47 | + |
| 48 | +/** |
| 49 | + * A fingerprint factory is an object that knows how to produce fingerprints for SMILES. It is used to calculate fingerprints for the search index as |
| 50 | + * well as for query structures when the index is searched. As some fingerprints, e.g. Avalon, support different optimizations we have two different |
| 51 | + * methods for the two different purposes. |
| 52 | + * |
| 53 | + * @author Manuel Schwarze |
| 54 | + */ |
| 55 | + |
| 56 | +@Data |
| 57 | +@AllArgsConstructor |
| 58 | +public class DefaultFingerprintFactory implements FingerprintFactory { |
| 59 | + |
| 60 | + // |
| 61 | + // Constants |
| 62 | + // |
| 63 | + |
| 64 | + /** |
| 65 | + * The logger instance. |
| 66 | + */ |
| 67 | + private static final Logger logger = LoggerFactory.getLogger(DefaultFingerprintFactory.class); |
| 68 | + |
| 69 | + // |
| 70 | + // Members |
| 71 | + // |
| 72 | + |
| 73 | + /** |
| 74 | + * The settings to be used for calculating structure fingerprints with this factory. |
| 75 | + */ |
| 76 | + private final FingerprintSettings settingsStructure; |
| 77 | + |
| 78 | + /** |
| 79 | + * The settings to be used for calculating query fingerprints with this factory. |
| 80 | + */ |
| 81 | + private final FingerprintSettings settingsQuery; |
| 82 | + |
| 83 | + // |
| 84 | + // Constructors |
| 85 | + // |
| 86 | + |
| 87 | + /** |
| 88 | + * Creates a new fingerprint factory based on the past in settings. Structure and query fingerprints are handled the same way. There is distinction |
| 89 | + * between them. To handle them differently, use the other constructor. |
| 90 | + * |
| 91 | + * @param settings Fingerprint settings. Must not be null. |
| 92 | + */ |
| 93 | + public DefaultFingerprintFactory(@NonNull final FingerprintSettings settings) { |
| 94 | + settingsStructure = settingsQuery = settings; |
| 95 | + } |
| 96 | + |
| 97 | + // |
| 98 | + // Public Methods |
| 99 | + // |
| 100 | + |
| 101 | + /** |
| 102 | + * Creates a fingerprint based on the passed in SMILES. |
| 103 | + * |
| 104 | + * @param strSmiles SMILES structure, preferably canonicalized by RDKit before. Must not be null. |
| 105 | + * @return Fingerprint as BitSet. |
| 106 | + */ |
| 107 | + @Override |
| 108 | + public BitSet createStructureFingerprint(final String strSmiles) { |
| 109 | + return createFingerprint(strSmiles, settingsStructure); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Creates a fingerprint based on the passed in SMILES. |
| 114 | + * |
| 115 | + * @param strSmiles SMILES structure, preferably canonicalized by RDKit before. Must not be null. |
| 116 | + * @return Fingerprint as BitSet. |
| 117 | + */ |
| 118 | + @Override |
| 119 | + public BitSet createQueryFingerprint(final String strSmiles) { |
| 120 | + return createFingerprint(strSmiles, settingsQuery); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Method for already opened RWMol to build fingerprint from Query settings. |
| 125 | + * |
| 126 | + * @param mol already opened RWMol object |
| 127 | + * @return Fingerprint as BitSet. |
| 128 | + */ |
| 129 | + public BitSet createQueryFingerprint(final RWMol mol) { |
| 130 | + return createFingerprint(mol, settingsQuery); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Method for already opened RWMol to build fingerprint from Structure settings. |
| 135 | + * |
| 136 | + * @param mol already opened RWMol object |
| 137 | + * @return Fingerprint as BitSet. |
| 138 | + */ |
| 139 | + public BitSet createStructureFingerprint(final RWMol mol) { |
| 140 | + return createFingerprint(mol, settingsStructure); |
| 141 | + } |
| 142 | + |
| 143 | + // |
| 144 | + // Private Methods |
| 145 | + // |
| 146 | + |
| 147 | + /** |
| 148 | + * Creates a fingerprint based on the passed in SMILES. |
| 149 | + * |
| 150 | + * @param strSmiles SMILES structure, preferably canonicalized by RDKit before. Must not be null. ! EXPECTED CANONICALIZED SMILES ! |
| 151 | + * @param settings Fingerprint settings to be used. |
| 152 | + * @return Fingerprint as BitSet. |
| 153 | + */ |
| 154 | + private BitSet createFingerprint(@NonNull final String strSmiles, final FingerprintSettings settings) { |
| 155 | + |
| 156 | + // todo: update code if other types are used |
| 157 | + |
| 158 | + // Normally: ROMol objects are needed to calculate fingerprints |
| 159 | + // Create an ROMol object |
| 160 | + |
| 161 | + // Performance trick, if SMILES is already canonicalized |
| 162 | + try (val mol = RWMolCloseable.from(RWMol.MolFromSmiles(strSmiles, 0, false))) { |
| 163 | + return createFingerprint(mol, settings); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Method for already opened RWMol |
| 169 | + * @param mol - canonicalized |
| 170 | + * @param settings to build fingerprint from |
| 171 | + * @return BitSet from rwmol (fingerprint of `settings` type) |
| 172 | + */ |
| 173 | + private BitSet createFingerprint(final RWMol mol, final FingerprintSettings settings) { |
| 174 | + mol.updatePropertyCache(); |
| 175 | + |
| 176 | + // Calculate fingerprint |
| 177 | + return convert(settings.getRdkitFingerprintType().calculate(mol, settings)); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Converts an RDKit bit vector into a Java BitSet object. |
| 182 | + * |
| 183 | + * @param rdkitBitVector RDKit (C++ based) bit vector. Can be null. |
| 184 | + * @return BitSet or null, if null was passed in. |
| 185 | + */ |
| 186 | + private BitSet convert(final ExplicitBitVect rdkitBitVector) { |
| 187 | + BitSet fingerprint = null; |
| 188 | + |
| 189 | + if (rdkitBitVector != null) { |
| 190 | + final int iLength = (int) rdkitBitVector.getNumBits(); |
| 191 | + fingerprint = new BitSet(iLength); |
| 192 | + for (int i = 0; i < iLength; i++) { |
| 193 | + if (rdkitBitVector.getBit(i)) { |
| 194 | + fingerprint.set(i); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return fingerprint; |
| 200 | + } |
| 201 | +} |
0 commit comments