RDKit支持PostgreSQL配置

2021-01-28 18:00:56 浏览数 (1)

安装RDKit的PostgreSQL支持

conda install -c rdkit rdkit-postgresql

安装psycopg2,是Python语言的PostgreSQL数据库接口

pip install psycopg2

为drugbank数据库创建扩展:

psql -c 'create extension rdkit' drugbank

代码示例

  1. #!/usr/bin/python3
  2. from rdkit import Chem
  3. import psycopg2 as ps
  4. c = ps.connect(dbname='drugbank', user='aspirin', host='127.0.0.1', password='aspirin12369', port="5432")
  5. cursor = c.cursor()
  6. cursor.execute("select version()")
  7. reply = cursor.fetchall()
  8. print(reply)
  9. cursor.execute("drop schema if exists drugbank cascade")
  10. cursor.execute("create schema drugbank")
  11. cursor.execute("""
  12. create table drugbank.drugbank(
  13. id serial primary key,
  14. drugbank_id text,
  15. smiles text,
  16. hba integer,
  17. hbd integer,
  18. mw float,
  19. logp float
  20. );
  21. """)
  22. cursor.execute("select * from drugbank.drugbank")
  23. print(cursor.fetchall())
  24. supp = Chem.SDMolSupplier("drugbank.sdf")
  25. drugbank = [mol for mol in supp if mol]
  26. query = "insert into drugbank.drugbank (smiles, drugbank_id) values %s" % ((', '.join(["('%s','%s')" % (Chem.MolToSmiles(mol), mol.GetProp('DRUGBANK_ID'))
  27. for mol in drugbank])))
  28. cursor.execute(query)
  29. print(query[:1000])
  30. cursor.execute("select count(*) from drugbank.drugbank")
  31. print(cursor.fetchall(), len(drugbank))
  32. cursor.execute("select * from drugbank.drugbank limit 10")
  33. print(cursor.fetchall())
  34. cursor.execute("select * from drugbank.drugbank where id > 500 and id < 600 limit 10;")
  35. print(cursor.fetchall())
  36. c.commit()

0 人点赞