In this post we will look at how we can visualize proteins on Hugging Face Spaces.
While the method described below still works, you'll likely want to save some time and use the Molecule3D Gradio Custom Component. This component will allow users to modify the protein visualization on the fly and you can more easily set the default visualization. Simply install it using:. (Related: How to Start a Side Business While Working Full-Time...)
Proteins have a huge impact on our life - from medicines to washing powder. Machine learning on proteins is a rapidly growing field to help us design fresh and interesting proteins. Proteins are complex 3D objects generally composed of a series of building blocks called amino acids that are arranged in 3D space to give the protein its function. For machine learning purposes a protein can for example be represented as coordinates, as graph or as 1D sequence of letters for use in a protein language model.
A famous ML model for proteins is AlphaFold2 which predicts the structure of a protein sequence using a multiple sequence alignment of similar proteins and a structure module.
Since AlphaFold2 made its debut many more such models have come out such as OmegaFold, OpenFold etc. (see this list or this list for more).
According to recent reports, the structure of a protein is an integral part to our understanding of what a protein does. Nowadays, there are a few tools available to visualize proteins directly in the browser such as mol* or 3dmol.js. In this post, you will learn how to integrate structure visualization into your Hugging Face Space using 3Dmol.js and the HTML block.
Make sure you have the gradio Python package already installed and basic knowledge of Javascript/JQuery.
Let's take a look at how to create the minimal working demo of our interface before we dive into how to setup 3Dmol.js.
We will build a simple demo app that can accept either a 4-digit PDB code or a PDB file. Our app will then retrieve the pdb file from the RCSB Protein Databank and display it or use the uploaded file for display.
enhancement: This is the function that does the processing of our proteins and returns an iframe with the viewer.
Our get_pdb function is also simple:.
In a recent development, now, how to visualize the protein since Gradio does not have 3Dmol directly available as a block? We use an iframe for this.
Our molecule function which returns the iframe conceptually looks like this:.
This is a bit clunky to setup but is necessary because of the security rules in modern browsers. (Related: Gift Tax Rules: How Much Can You Give Tax-Free?...)
3Dmol.js setup is pretty easy and the documentation provides a few examples.
The head of our returned document needs to load 3Dmol.js (which in turn also loads JQuery).
The styles for .mol-container can be used to modify the size of the molecule viewer.
We use a template literal (denoted by backticks) to store our pdb file in the html document directly and then output it using 3dmol.js.
And that's it, now you can couple your favorite protein ML model to a fun and easy to use gradio app and directly visualize predicted or redesigned structures. If you are predicting properities of a structure (e.g how likely each amino acid is to bind a ligand), 3Dmol.js also allows to use a custom colorfunc based on a property of each atom.
You can check the source code of the 3Dmol.js space for the full code.
Industry observers note that for a production example, you can check the ProteinMPNN space where a user can upload a backbone, the inverse folding model ProteinMPNN predicts newly introduced optimal sequences and then one can run AlphaFold2 on all predicted sequences to verify whether they adopt the initial input backbone. Successful redesigns that qualitiatively adopt the same structure as predicted by AlphaFold2 with high pLDDT score should be tested in the lab.
If you encounter any complications with the integration of 3Dmol.js in Gradio/HF spaces, please open a discussion in hf.space/simonduerr/3dmol.js.
If you have problems with 3Dmol.js configuration - you need to ask the developers, please, open a 3Dmol.js Glitch instead and describe your problem.