1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
/*
* $Id: AtomPairsTests.java 4395 2011-01-28 16:54:48Z landrgr1 $
*
* Copyright (c) 2010, Novartis Institutes for BioMedical Research Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Novartis Institutes for BioMedical Research Inc.
* nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.RDKit;
import static org.junit.Assert.*;
import java.io.File;
import org.junit.*;
public class AtomPairsTests extends GraphMolTest {
@Test
public void testAtomCodes_1() {
ROMol mol = RWMol.MolFromSmiles("C=C");
Atom atom0 = mol.getAtomWithIdx(0);
Atom atom1 = mol.getAtomWithIdx(1);
assertNotNull(atom0);
assertNotNull(atom1);
assertEquals(RDKFuncs.getAtomCode(atom0), RDKFuncs.getAtomCode(atom1));
long tgt = 1 | (1 | 1 << RDKFuncs.getNumPiBits()) << RDKFuncs.getNumBranchBits();
assertEquals(tgt, RDKFuncs.getAtomCode(atom0));
tgt = 1 << RDKFuncs.getNumBranchBits() | 1 << (RDKFuncs.getNumBranchBits() + RDKFuncs.getNumPiBits());
assertEquals(tgt, RDKFuncs.getAtomCode(atom0, 1));
}
@Test
public void testAtomCodes_2() {
ROMol mol = RWMol.MolFromSmiles("C#CO");
long tgt = 1 | 2 << RDKFuncs.getNumBranchBits() | 1 << (RDKFuncs.getNumBranchBits() + RDKFuncs.getNumPiBits());
assertEquals(tgt, RDKFuncs.getAtomCode(mol.getAtomWithIdx(0)));
tgt = 2 | 2 << RDKFuncs.getNumBranchBits() | 1 << (RDKFuncs.getNumBranchBits() + RDKFuncs.getNumPiBits());
assertEquals(tgt, RDKFuncs.getAtomCode(mol.getAtomWithIdx(1)));
tgt = 1 | 0 << RDKFuncs.getNumBranchBits() | 3 << (RDKFuncs.getNumBranchBits() + RDKFuncs.getNumPiBits());
assertEquals(tgt, RDKFuncs.getAtomCode(mol.getAtomWithIdx(2)));
}
@Test
public void testAtomCodes_3() {
ROMol mol = RWMol.MolFromSmiles("CC(O)C(O)(O)C");
long tgt = 1 | 0 << RDKFuncs.getNumBranchBits() | 1 << (RDKFuncs.getNumBranchBits() + RDKFuncs.getNumPiBits());
assertEquals(tgt, RDKFuncs.getAtomCode(mol.getAtomWithIdx(1), 2));
tgt = 2 | 0 << RDKFuncs.getNumBranchBits() | 1 << (RDKFuncs.getNumBranchBits() + RDKFuncs.getNumPiBits());
assertEquals(tgt, RDKFuncs.getAtomCode(mol.getAtomWithIdx(3), 2));
}
@Test
public void testAtomCodes_4() {
ROMol mol = RWMol.MolFromSmiles("C=CC(=O)O");
long tgt = 0 | 0 << RDKFuncs.getNumBranchBits() | 3 << (RDKFuncs.getNumBranchBits() + RDKFuncs.getNumPiBits());
assertEquals(tgt, RDKFuncs.getAtomCode(mol.getAtomWithIdx(4), 1));
tgt = 3 | 1 << RDKFuncs.getNumBranchBits() | 1 << (RDKFuncs.getNumBranchBits() + RDKFuncs.getNumPiBits());
assertEquals(tgt, RDKFuncs.getAtomCode(mol.getAtomWithIdx(2)));
}
@Test
public void testAtomPairs() {
ROMol mol = RWMol.MolFromSmiles("CCCCC");
SparseIntVect32 fp;
long tgt, c1, c2, c3;
c1 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(0));
c2 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(1));
c3 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(2));
tgt = 1 | (Math.min(c1, c2) | Math.max(c1, c2) << RDKFuncs.getCodeSize()) << RDKFuncs.getNumPathBits();
assertEquals(tgt, RDKFuncs.getAtomPairCode(c1, c2, 1));
assertEquals(tgt, RDKFuncs.getAtomPairCode(c2, c1, 1));
tgt = 2 | (Math.min(c1, c3) | Math.max(c1, c3) << RDKFuncs.getCodeSize()) << RDKFuncs.getNumPathBits();
assertEquals(tgt, RDKFuncs.getAtomPairCode(c1, c3, 2));
assertEquals(tgt, RDKFuncs.getAtomPairCode(c3, c1, 2));
mol = RWMol.MolFromSmiles("CCC");
fp = RDKFuncs.getAtomPairFingerprint(mol);
assertEquals(3, fp.getTotalVal());
assertEquals(2, fp.getNonzero().size());
c1 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(0));
c2 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(1));
c3 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(2));
assertEquals(2, fp.getVal((int) RDKFuncs.getAtomPairCode(c1, c2, 1)));
assertEquals(1, fp.getVal((int) RDKFuncs.getAtomPairCode(c1, c3, 2)));
mol = RWMol.MolFromSmiles("CC=O.Cl");
fp = RDKFuncs.getAtomPairFingerprint(mol);
assertEquals(3, fp.getTotalVal());
assertEquals(3, fp.getNonzero().size());
c1 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(0));
c2 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(1));
c3 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(2));
assertEquals(1, fp.getVal((int) RDKFuncs.getAtomPairCode(c1, c2, 1)));
assertEquals(1, fp.getVal((int) RDKFuncs.getAtomPairCode(c1, c2, 1)));
assertEquals(1, fp.getVal((int) RDKFuncs.getAtomPairCode(c2, c3, 1)));
}
@Test
public void testAtomPairs2() {
ROMol mol;
SparseIntVect32 fp;
mol = RWMol.MolFromSmiles("CCC");
fp = RDKFuncs.getAtomPairFingerprint(mol, 1, 2);
assertEquals(3, fp.getTotalVal());
assertEquals(2, fp.getNonzero().size());
fp = RDKFuncs.getAtomPairFingerprint(mol, 2, 2);
assertEquals(1, fp.getTotalVal());
assertEquals(1, fp.getNonzero().size());
}
@Test
public void testHashedAtomPairs() {
ROMol mol = RWMol.MolFromSmiles("c1ccccc1");
SparseIntVect32 fp1, fp2;
fp1 = RDKFuncs.getHashedAtomPairFingerprint(mol);
fp2 = RDKFuncs.getHashedAtomPairFingerprint(mol);
assertEquals(1.0, RDKFuncs.DiceSimilarity(fp1, fp2), 0.0);
assertTrue( fp1.eq(fp2) );
mol = RWMol.MolFromSmiles("c1ccccn1");
fp2 = RDKFuncs.getHashedAtomPairFingerprint(mol);
assertEquals(0.0, RDKFuncs.DiceSimilarity(fp1, fp2), 1.0);
mol = RWMol.MolFromSmiles("c1ccccc1");
fp1 = RDKFuncs.getHashedAtomPairFingerprint(mol, 2048);
fp2 = RDKFuncs.getHashedAtomPairFingerprint(mol, 2048, 1, 3);
assertEquals(1.0, RDKFuncs.DiceSimilarity(fp1, fp2), 0.0);
assertTrue( fp1.eq(fp2) );
fp2 = RDKFuncs.getHashedAtomPairFingerprint(mol, 2048, 1, 2);
assertEquals(0.0, RDKFuncs.DiceSimilarity(fp1, fp2), 1.0);
}
@Test
public void testTorsions() {
ROMol mol = RWMol.MolFromSmiles("CCCC");
SparseIntVect64 fp;
double tgt;
long c1, c2, c3, c4;
UInt_Vect codes = new UInt_Vect();
mol = RWMol.MolFromSmiles("CCCC");
c1 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(0)) - 1;
c2 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(1)) - 2;
c3 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(2)) - 2;
c4 = RDKFuncs.getAtomCode(mol.getAtomWithIdx(3)) - 1;
tgt = c1 | (c2 | (c3 | c4 << RDKFuncs.getCodeSize()) << RDKFuncs.getCodeSize()) << RDKFuncs.getCodeSize();
codes.clear();
codes.add(c1);
codes.add(c2);
codes.add(c3);
codes.add(c4);
assertEquals(tgt, RDKFuncs.getTopologicalTorsionCode(codes).doubleValue(), 0.0);
fp = RDKFuncs.getTopologicalTorsionFingerprint(mol);
assertEquals(1, fp.getTotalVal());
assertEquals(1, fp.getNonzero().size());
mol = RWMol.MolFromSmiles("CCCCO.Cl");
fp = RDKFuncs.getTopologicalTorsionFingerprint(mol);
assertEquals(2, fp.getTotalVal());
assertEquals(2, fp.getNonzero().size());
fp = RDKFuncs.getTopologicalTorsionFingerprint(mol, 3);
assertEquals(3, fp.getTotalVal());
assertEquals(3, fp.getNonzero().size());
}
@Test
public void testHashedTorsions() {
ROMol mol = RWMol.MolFromSmiles("c1ccccc1");
SparseIntVect64 fp1, fp2;
fp1 = RDKFuncs.getHashedTopologicalTorsionFingerprint(mol);
fp2 = RDKFuncs.getHashedTopologicalTorsionFingerprint(mol);
assertEquals(1.0, RDKFuncs.DiceSimilarity(fp1, fp2), 0.0);
assertTrue( fp1.eq(fp2) );
mol = RWMol.MolFromSmiles("c1ccccn1");
fp2 = RDKFuncs.getHashedTopologicalTorsionFingerprint(mol);
assertEquals(0.0, RDKFuncs.DiceSimilarity(fp1, fp2), 1.0);
mol = RWMol.MolFromSmiles("c1ccccc1");
fp1 = RDKFuncs.getHashedTopologicalTorsionFingerprint(mol, 2048, 6);
fp2 = RDKFuncs.getHashedTopologicalTorsionFingerprint(mol, 2048, 6);
assertEquals(1.0, RDKFuncs.DiceSimilarity(fp1, fp2), 0.0);
assertTrue( fp1.eq(fp2) );
mol = RWMol.MolFromSmiles("c1ccccn1");
fp2 = RDKFuncs.getHashedTopologicalTorsionFingerprint(mol, 2048, 6);
assertEquals(0.0, RDKFuncs.DiceSimilarity(fp1, fp2), 1.0);
}
@Test
public void testBulkTorsions() {
String fName = new File(getRdBase(), "/Projects/DbCLI/testData/pubchem.200.sdf").getPath();
SDMolSupplier suppl = new SDMolSupplier(fName);
while (!suppl.atEnd()) {
ROMol mol = suppl.next();
SparseIntVect64 fp;
fp = RDKFuncs.getTopologicalTorsionFingerprint(mol);
assertTrue(fp.getTotalVal() > 1);
}
}
@Test
public void testRootedAtomPairs() {
ROMol mol = RWMol.MolFromSmiles("OCCCCC");
SparseIntVect32 fp1, fp2;
UInt_Vect roots = new UInt_Vect();
fp1 = RDKFuncs.getAtomPairFingerprint(mol);
Match_Vect nz1 = fp1.getNonzero();
assertTrue(nz1.size() > 0);
roots.add(0);
fp2 = RDKFuncs.getAtomPairFingerprint(mol, roots);
Match_Vect nz2 = fp2.getNonzero();
assertTrue(nz2.size() > 0);
assertTrue(nz2.size() < nz1.size());
for (int i = 0; i < nz2.size(); i++) {
Int_Pair pair = nz2.get(i);
assertEquals(pair.getSecond() , fp2.getVal(pair.getFirst()));
}
}
@Test
public void testRootedTorsions() {
SparseIntVect64 fp1, fp2;
ROMol mol = RWMol.MolFromSmiles("OCCCC");
UInt_Vect roots = new UInt_Vect();
roots.add(0);
fp1 = RDKFuncs.getTopologicalTorsionFingerprint(mol);
Long_Pair_Vect nz1 = fp1.getNonzero();
assertTrue(nz1.size() > 0);
fp2 = RDKFuncs.getTopologicalTorsionFingerprint(mol, 4, roots);
Long_Pair_Vect nz2 = fp2.getNonzero();
assertTrue(nz2.size() > 0);
assertTrue(nz2.size() < nz1.size());
for (int i = 0; i < nz2.size(); ++i) {
Long_Pair pair = nz2.get(i);
assertTrue(pair.getSecond() <= fp2.getVal(pair.getFirst()));
}
}
public static void main(String args[]) {
org.junit.runner.JUnitCore.main(AtomPairsTests.class.getName());
}
}
|