安装RDKit的PostgreSQL支持
conda install -c rdkit rdkit-postgresql
安装psycopg2,是Python语言的PostgreSQL数据库接口
pip install psycopg2
为drugbank数据库创建扩展:
psql -c 'create extension rdkit' drugbank
代码示例
#!/usr/bin/python3
from rdkit import
Chem
import psycopg2 as ps
c = ps.connect(dbname='drugbank', user='aspirin', host='127.0.0.1', password='aspirin12369', port="5432")
cursor = c.cursor()
cursor.execute("select version()")
reply = cursor.fetchall()
print(reply)
cursor.execute("drop schema if exists drugbank cascade")
cursor.execute("create schema drugbank")
cursor.execute("""
create table drugbank.drugbank(
id serial primary key,
drugbank_id text,
smiles text,
hba integer,
hbd integer,
mw float,
logp float
);
""")
cursor.execute("select * from drugbank.drugbank")
print(cursor.fetchall())
supp =
Chem.SDMolSupplier("drugbank.sdf")
drugbank =
[mol for mol in supp if mol]
query =
"insert into drugbank.drugbank (smiles, drugbank_id) values %s"
%
((', '.join(["('%s','%s')"
%
(Chem.MolToSmiles(mol), mol.GetProp('DRUGBANK_ID'))
-
for mol in drugbank])))
cursor.execute(query)
print(query[:1000])
cursor.execute("select count(*) from drugbank.drugbank")
print(cursor.fetchall(), len(drugbank))
cursor.execute("select * from drugbank.drugbank limit 10")
print(cursor.fetchall())
cursor.execute("select * from drugbank.drugbank where id > 500 and id < 600 limit 10;")
print(cursor.fetchall())
c.commit()