RECAPとか

RDKitのRECAPルールを使うと
分子をある一定の結合で切断できます。
ライブラリーにアプライして得られる頻度を見ると
これよく使ってるよね。みたいなのが分かるかもと思います。
こちらのブログを拝見させていただき、pytagcloudなるものを知りました。
使うのにSDFとかpygamesとか入れないとなんなくて面倒でした。
が一応環境ができたのでRECAPで分子を切断した後
HTMLに出力するようなスクリプトを書いてみました。
ちなみにほとんどは、githubにあがっているpytagcloudのunittestのコードを
ベースにしています。

from pytagcloud import create_tag_image, create_html_data, make_tags
from pytagcloud.colors import COLOR_SCHEMES
from pytagcloud.lang.counter import get_tag_counts
from string import Template
from rdkit import Chem
from rdkit.Chem import Recap

import os, sys
import time

def get_leaves(mols):
    leaves = [ Recap.RecapDecompose(mol).GetLeaves() for mol in mols ]
    flat_leaves = []
    for frags in leaves:
        for frag in frags:
            flat_leaves.append( frag )
    return flat_leaves

def make_smi_tag( flat_leaves ):
    frag_dict = {}
    frag_list = []
    for frag in flat_leaves:
        if frag_dict.has_key( frag ):
            frag_dict[ frag ] += 1
        else:
            frag_dict[ frag ] = 1
    for k, v in frag_dict.items():
        frag_list.append( (k,v) )
    
    tags = make_tags( frag_list )
    htmlobj = create_html_data( tags, size = ( 1000,800 ), layout="LAYOUT_HORIZONTAL" )
    return htmlobj


def create_html( htmlobj ):
     template_file = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'web/template.html'), 'r')
     html_template = Template(template_file.read())
     context = {}
     tags_template = '<li class="cnt" style="top: %(top)dpx; left: %(left)dpx; height: %(height)dpx;"><a class="tag %(cls)s" href="#%(tag)s" style="top: %(top)dpx;\
     left: %(left)dpx; font-size: %(size)dpx; height: %(height)dpx; line-height:%(lh)dpx;">%(tag)s</a></li>'
     context['tags'] = ''.join([tags_template % link for link in htmlobj['links']])
     context['width'] = htmlobj['size'][0]
     context['height'] = htmlobj['size'][1]
     context['css'] = "".join("a.%(cname)s{color:%(normal)s;}\
        a.%(cname)s:hover{color:%(hover)s;}" % 
                                  {'cname':k,
                                   'normal': v[0],
                                   'hover': v[1]} 
                                 for k,v in htmlobj['css'].items())
     html_text = html_template.substitute(context)
        
     html_file = open(('./out/smilescloud.html'), 'w')
     html_file.write(html_text)
     html_file.close()       


if __name__=="__main__":
     mols = [mol for mol in Chem.SDMolSupplier(sys.argv[1])]
     flat_leaves = get_leaves(mols)
     htmlobj = make_smi_tag( flat_leaves )
     create_html( htmlobj )
     
     print "Done!"
lion:~ pen$ python tag.py hoge.sdf

で実行します。

htmlのテンプレートは
https://github.com/atizo/PyTagCloud/tree/master/pytagcloud/test/web
のtemplate.htmlです。
で、
カレントディレクトリにwebというフォルダを作ってそこに入れておきます。
outというフォルダにtagcloudなsmilesが作成されます。多分。

分子数が多いとRECAPで結構時間を食うし、fragmentは1っこでもカウントしてるので
適当な数以上になったらタグにするとかしないと重たい感じ。
リンクになってるからマウスオーバーしたら親化合物が見えるとか、
smi2jpgみたいになると素敵ですが、、、ちょっと技術力不足、、、

JSのらいぶらりだけどTagCloudjsというの今日教えてもらったが便利そう。

Advertisement

Published by iwatobipen

I'm medicinal chemist in mid size of pharmaceutical company. I love chemoinfo, cording, organic synthesis, my family.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: