Wednesday, July 13, 2016

Using ipywidgets and py3Dmol to browse conformations in jupyter

Disclaimer: This is unfortunately one where you need a backend server to do have a live widget, so I'm using standard blogger and screenshots.

I've been looking at a lot of conformational manifolds lately. I used to do this with the RDKit's PyMol integration, but since that doesn't run on either of my laptops and since py3DMol looks awesome, I invested a bit of time in figuring out how to make it work with ipywidgets in jupyter. I figure this might be useful to others (and having the code online will be useful to me as well!), I'm doing another short post.

What I want to do is generate a set of conformers for a molecule and scroll through them interactively. Here's some code for doing that:
    def drawit(m,p,confId=-1):
        mb = Chem.MolToMolBlock(m,confId=confId)
        p.removeAllModels()
        p.addModel(mb,'sdf')
        p.setStyle({'stick':{}})
        p.setBackgroundColor('0xeeeeee')
        p.zoomTo()
        return p.show()

You use it like this:
    import py3Dmol
    from rdkit import Chem
    from rdkit.Chem import AllChem
    from ipywidgets import interact, interactive, fixed

    m = Chem.MolFromSmiles(r'COc1ccc2[nH]c([S@@+]([O-])Cc3ncc(C)c(OC)c3C)nc2c1') # esomeprazole
    m = Chem.AddHs(m)
    AllChem.EmbedMultipleConfs(m,numConfs=10,randomSeed=0xf00d,useExpTorsionAnglePrefs=True,\
                           useBasicKnowledge=True)
    # align to one of the ring systems:
    AllChem.AlignMolConformers(m,m.GetSubstructMatch(Chem.MolFromSmarts('c1[nH]c2ccccc2n1')))

    # now construct the view and interactive widget:
    p = py3Dmol.view(width=400,height=400)
    interact(drawit, m=fixed(m),p=fixed(p),confId=(0,m.GetNumConformers()-1));

And here's what the output looks like in the notebook:





Simple and, I think, quite useful.

You can also display the whole bundle at once:
    def drawit2(m,p,confId=-1):
        mb = Chem.MolToMolBlock(m,confId=confId)
        p.addModel(mb,'sdf')
        p.setStyle({'stick':{}})
        p.setBackgroundColor('0xeeeeee')
        p.zoomTo()
    
    p = py3Dmol.view(width=400,height=400)
    for confId in range(10):
        drawit2(m,p,confId)
    p.show()

This yields:



4 comments:

Brian Cole said...

Greg, unfortunately, the interactive slider bar example here doesn't seem to work in 2017.09 anymore. :-(

Some internal miscommunication due to the inclusion of py3Dmol being used internally in RDKit iPython now?

Georgina Alexis said...

We are best and trusted bangladeshi online news portal website. Everyday we publish the national and international latest news. If you want to get regular newslatter from our newspaper, please go home page BANGLA NEWS and click the below subscribe button.

বাংলাদেশ :
Read Bangla Breaking বাংলাদেশ News on Parliament, Govt, Justice, Road Accident, Cricket, Sports, Education, Business, Agriculture, Population, Dhaka at todaybanglenews.com
রাজধানী :
রাজধানী ঢাকার সরকার/রাজনীতি, শিক্ষা, বিচার/আদালত, পুলিশ/অপরাধ, বিক্ষোভ/ধর্মঘট/প্রতিবাদ সমাবেশ, গোলাগুলি/আগুন/আহত, প্রেসক্লাব বিষয়ক খবর ও সংবাদ Dhaka news at todaybanglenews.com
আন্তর্জাতিক :
আন্তর্জাতিক,Read Bangla Breaking World News on USA, UK, India, Pakistan, Asia, China, Europe, Middle East, Latin America, Africa, Australia, America & UN at todaybanglenews.com
সম্পাদকীয় :
সম্পাদকীয় - সংবাদ গবেষণা, বিশ্লেষণ, অভিমত, মতামত, মন্তব্য বিশয়ক ফিচার। প্রথম আলো, বাংলাদেশের সর্বাধিক পঠিত অনলাইন বাংলা সংবাদপত্র/পত্রিকা at todaybanglenews.com
বিজ্ঞান ও প্রযুক্তি :
বিজ্ঞান ও প্রযুক্তি বিষয়ক খবর/সংবাদ - Bangla Technology News। কম্পিউটার সফটওয়্যার, ইন্টারনেট, মোবাইল, আইফোন/আইপ্যাড, উইন্ডোজ/মাইক্রোসফট, এনড্রয়েড/গুগল, নকিয়া, আপডেট/আপগ্রেড, গেমস, ফ্রিল্যান্সার বিষয়ে রিভিউ, গবেষণা, ও ফিচার at todaybanglenews.com
টিপস্ :
টিপস্, health tips bangla, health tips bangladesh, health tips bangla version, health tips bangla diabetes, health tips bangla language, health tips bangla pregnancy, health tips bangla women, health tips bangla health tips, healthy jibon, পুরুষের স্বাস্থ্য, health tips for human, health tips in bangla at todaybanglenews.com
বিনোদন খবর :
বিনোদন খবর ও সংবাদ - Bangla Movie and Entertainment News। ছবি, শুটিং, মঞ্চ, শিল্পী, নাচ বিষয়ক ফিচার। প্রথম আলো, বাংলাদেশের সর্বাধিক পঠিত অনলাইন বাংলা সংবাদপত্র/পত্রিকা at todaybanglenews.com
খেলাধুলা :
খেলার খবর/সংবাদ - খেলাধুলা, ক্রীড়া ও ক্রিকেট - Bangla Sports News। ফুটবল, ক্রিকেট, টেনিস, বিশ্বকাপ, শিরোপা, টি-টোয়েন্টি, বল, সেমি-ফাইনাল, বোলার বিষয়ক ফিচার at todaybanglenews.com


Georgina Alexis said...

We are the world best leading online newspaper portal. You all are the most welcome in our newspaper.If you want to get regular newslatter from our newspaper, please go home page sports news and click the below subscribe button.

World News :
View FOX world news today for international news and videos from Europe, Asia, Africa, the Middle East and the Americas. Visit world news reuters for up-to-the-minute news, breaking news, video, audio and feature stories.

Politics News :
Politics at FOX has news, opinion and analysis of American and global politics Find news and video about elections, the White House, the U.N and much more. What You Need To Know About politics news latest.

Sports News :
Latest sports news nba from around the world with in-depth analysis, features, photos and videos covering football, tennis, motorsport, golf, rugby, sailing, skiing, horse racing and equestrian.

Entertainment News :
View entertainment news of today and videos for the latest movie, music, TV and celebrity headlines on worldfoxnews.com

Technology News :
Get the latest FOX technology news google : breaking news and analysis on computing, the web, blogs, games, gadgets, social media, broadband and more.

Science News :
Get the latest FOX science news for today and Environment News: breaking news, analysis and debate on science and nature in the UK and around the world.

Health News :
Get the latest FOX health news science: breaking health and medical news from the UK and around the world, with in-depth features on well-being and lifestyle.

Daily Life :
What You Need To Know About The daily life yoga? Get the latest lifestyle news with articles and videos on pets, parenting, fashion, beauty, food, travel, relationships and more on Fox news.

Georgina Alexis said...

We are the world best leading online newspaper portal. You all are the most welcome in our newspaper.If you want to get regular newslatter from our newspaper, please go home page science news and click the below subscribe button.

World News :
View CNN world news tonight for international news and videos from Europe, Asia, Africa, the Middle East and the Americas. Visit CNN News for up-to-the-minute news, breaking news, video, audio and feature stories.

Politics News :
Politics at CNN has news, opinion and analysis of American and global politics Find news and video about elections, the White House, the U.N and much more. What You Need To Know About politics news article .

Sports News :
Latest sports news nba from around the world with in-depth analysis, features, photos and videos covering football, tennis, motorsport, golf, rugby, sailing, skiing, horse racing and equestrian.

Entertainment News :
View eentertainment news today and videos for the latest movie, music, TV and celebrity headlines on usacnnnews.com
Technology News :
Get the latest CNN technology news google : breaking news and analysis on computing, the web, blogs, games, gadgets, social media, broadband and more.

Science News :
Get the latest CNN Science and Environment News: breaking news, analysis and debate on science news of today and nature in the UK and around the world.

Health News :
Get the latest CNN health news science : breaking health and medical news from the UK and around the world, with in-depth features on well-being and lifestyle.

Daily Life :
What You Need To Know About daily life quotes ? Get the latest lifestyle news with articles and videos on pets, parenting, fashion, beauty, food, travel, relationships and more on CNN news.