import sqlite3
import csv
import pandas as pd
from rdkit import Chem
from rdkit.Chem import PandasTools
#Install Chemicalite extension
connection = sqlite3.connect("Pubchem2.sqlite")
connection.enable_load_extension(True)
connection.load_extension('/Users/chrisswain/miniconda3/envs/rdkitenv/lib/chemicalite.dylib')
#now disable load because of potential security risks
connection.enable_load_extension(False)
connection.execute(
"CREATE TABLE ID_DATA (SMILES TEXT NOT NULL,ID TEXT PRIMARY KEY,INCHIKEY TEXT NOT NULL, molecule MOL)")
<sqlite3.Cursor at 0x33b0d4840>
def pubchem(path):
with open(path, 'rt') as inputfile:
reader = csv.reader(inputfile, delimiter = '\t')
next(reader) #skip header
for SMILES, ID, INCHIKEY, *_ in reader:
yield SMILES, ID, INCHIKEY
with connection:
connection.executemany(
"INSERT INTO ID_DATA(SMILES, ID, INCHIKEY, molecule)"
"VALUES(?1, ?2, ?3, mol_from_smiles(?1))", pubchem('/Users/chrisswain/Projects/Pubchem/InChiKey/AllInChiKey.tsv'))
cursor = connection.cursor()
targetinchikey = "WPQAOGZTDKTBHI-UHFFFAOYSA-N"
rows = cursor.execute("SELECT * FROM ID_DATA WHERE INCHIKEY == ?", (targetinchikey,),).fetchall()
rows
# This also returns the molecule as a binary object
[('OC1(C(=CCC(C1)C(=C)C)C)C1(O)C(=CCC(C1)C(=C)C)C', '500061', 'WPQAOGZTDKTBHI-UHFFFAOYSA-N', b'MOL\x00\xef\xbe\xad\xde\x00\x00\x00\x00\x10\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00\x00\x17\x00\x00\x00\x80\x01\x08\x00`\x00\x00\x00\x01\x01\x06\x00 \x00\x00\x00\x04\x06\x00(\x00\x00\x00\x03\x04\x06\x00h\x00\x00\x00\x03\x03\x01\x06\x00`\x00\x00\x00\x02\x02\x06\x00`\x00\x00\x00\x03\x01\x06\x00`\x00\x00\x00\x02\x02\x06\x00(\x00\x00\x00\x03\x04\x06\x00h\x00\x00\x00\x03\x02\x02\x06\x00`\x00\x00\x00\x01\x03\x06\x00`\x00\x00\x00\x01\x03\x06\x00 \x00\x00\x00\x04\x08\x00`\x00\x00\x00\x01\x01\x06\x00(\x00\x00\x00\x03\x04\x06\x00h\x00\x00\x00\x03\x03\x01\x06\x00`\x00\x00\x00\x02\x02\x06\x00`\x00\x00\x00\x03\x01\x06\x00`\x00\x00\x00\x02\x02\x06\x00(\x00\x00\x00\x03\x04\x06\x00h\x00\x00\x00\x03\x02\x02\x06\x00`\x00\x00\x00\x01\x03\x06\x00`\x00\x00\x00\x01\x03\x0b\x00\x01\x00\x01\x02\x00\x02\x03\x08\x02\x03\x04\x00\x04\x05\x00\x05\x06\x00\x05\x07\x00\x07\x08\x08\x02\x07\t\x00\x02\n\x00\x01\x0b\x00\x0b\x0c\x00\x0b\r\x00\r\x0e\x08\x02\x0e\x0f\x00\x0f\x10\x00\x10\x11\x00\x10\x12\x00\x12\x13\x08\x02\x12\x14\x00\r\x15\x00\x06\x01\x00\x11\x0b\x00B\x02\x00\x00\x00\x06\x02\x03\x04\x05\x06\x01\x06\r\x0e\x0f\x10\x11\x0b\x17\t\x00\x00\x00\x00\x00\x00\x00?\x00\x00\x00\x00\x12d\x00\x00\x00\x03\x00\x0f\x00\x00\x00__computedProps\x06\x02\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00numArom\x0f\x00\x00\x00_StereochemDone\x07\x00\x00\x00numArom\x01\x00\x00\x00\x00\x0f\x00\x00\x00_StereochemDone\x01\x01\x00\x00\x00\x13:0\x05\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\n\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\t\x00\x00\x00\x08\x01\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x08\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x06\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x02\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x05\x00\x00\x00\x08\x01\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x04\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x07\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x03\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x00\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x01\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\t\x00\x00\x00\x08\x01\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\n\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x08\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x06\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x02\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x05\x00\x00\x00\x08\x01\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x04\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x07\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x03\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x00\x00\x00\x00\x00\x02\x00\x0f\x00\x00\x00__computedProps\x06\x01\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00_CIPRank\x08\x00\x00\x00_CIPRank\x02\x01\x00\x00\x00\x00\x13\x16')]
#To do a structure search
resID=cursor.execute("SELECT ID FROM ID_DATA WHERE mol_is_substruct(molecule, mol_from_smiles('Nc1nc2[nH]c(CN3CCN(Cc4cccc(F)c4)CC3)c(CN3CCN(Cc4cccc(F)c4)CC3)c2c(=O)[nH]1'))").fetchall()
resID
[('149878534',), ('153408163',)]
#Toreturn the SMILES
resSMILES=cursor.execute("SELECT smiles FROM ID_DATA WHERE mol_is_substruct(molecule, mol_from_smiles('O=S(=O)(CCO)c1no[n+]([O-])c1-c1ccccc1'))").fetchall()
resSMILES
[('OCCS(=O)(=O)c1no[n+](c1c1ccccc1)[O-]',), ('c1ccc(cc1)c1[n+]([O-])onc1S(=O)(=O)CCOCC',), ('c1ccc(cc1)c1[n+]([O-])onc1S(=O)(=O)CCOC(C)C',), ('O=C(C(S(=O)(=O)c1no[n+](c1c1ccccc1)[O-])C)OCC',)]
#To get the mol object you need to use the mol_to_binary_mol
mols=cursor.execute("SELECT mol_to_binary_mol(molecule) FROM ID_DATA WHERE mol_is_substruct(molecule, mol_from_smiles('Nc1nc2[nH]c(CN3CCN(Cc4cccc(F)c4)CC3)c(CN3CCN(Cc4cccc(F)c4)CC3)c2c(=O)[nH]1'))").fetchall()
rdmols = [Chem.Mol(b[0]) for b in mols]
mol = rdmols[0]
mol
# ChemicaLite uses a virtual table mechanism to support indexing binary fingerprints in an RD-tree data structure, and this way improve the performances of substructure and similarity queries.
%%time
res=cursor.execute("SELECT COUNT(*) FROM ID_DATA WHERE mol_is_substruct(molecule, mol_from_smiles('Nc1nc2[nH]c(CN3CCN(Cc4cccc(F)c4)CC3)c(CN3CCN(Cc4cccc(F)c4)CC3)c2c(=O)[nH]1'))")
CPU times: user 24min 39s, sys: 1min 22s, total: 26min 2s Wall time: 27min 29s
# create a virtual table to be filled with morgan bfp data
connection.execute("CREATE VIRTUAL TABLE str_idx_pubchem_molecule " + "USING rdtree(id, fp bits(2048))")
#calculate and insert fingerprints, this will take a while
with connection:
connection.execute(
"INSERT INTO str_idx_pubchem_molecule(id, fp) " +
"SELECT ID, mol_pattern_bfp(molecule, 2048) FROM ID_DATA " +
"WHERE molecule IS NOT NULL")
%%time
res=cursor.execute("SELECT COUNT(*) FROM ID_DATA, str_idx_pubchem_molecule AS idx WHERE ID_DATA.ID = idx.id AND mol_is_substruct(ID_DATA.molecule, mol_from_smiles('Nc1nc2[nH]c(CN3CCN(Cc4cccc(F)c4)CC3)c(CN3CCN(Cc4cccc(F)c4)CC3)c2c(=O)[nH]1')) AND idx.id MATCH rdtree_subset(mol_pattern_bfp(mol_from_smiles('Nc1nc2[nH]c(CN3CCN(Cc4cccc(F)c4)CC3)c(CN3CCN(Cc4cccc(F)c4)CC3)c2c(=O)[nH]1'), 2048))").fetchall()
CPU times: user 16.9 ms, sys: 37.4 ms, total: 54.3 ms Wall time: 223 ms