Model incorporation workflow
Last updated
Last updated
The model incorporation workflow is streamlined by a series of GitHub Actions. Each push to Ersilia's codebase triggers a series of events that automatically maintain our platform updated across the different services we use to maintain our infrastructure (GitHub, Airtable, DockerHub and AWS).
There are two sets of workflows. The ones triggered by changes in the Ersilia Model Hub itself, and the ones triggered by each individual model. Here, we will focus on the workflow triggered when a new model is being incorporated:
If you want to test the workflow with a toy example, we have prepared an Ersilia Demo Repository. This repository is focused on illustrating the GitHub Actions workflows to be followed in order to successfully incorporate a model to the Ersilia Model Hub.
Go to the Ersilia main repository issues page.
Click on New issue. Then 🦠 Model Request (Get started)
For the purpose of this demo, you can use the following information:
🦠 Model Request: Demo Malaria Model
Model Name: Demo Malaria Model
Model Description: Prediction of the antimalarial potential of small molecules. This model was originally trained on proprietary data from various sources, up to a total of >7M compounds. The training sets belong to Evotec, Johns Hopkins, MRCT, MMV - St. Jude, AZ, GSK, and St. Jude Vendor Library. In this implementation, we have used a teacher-student approach to train a surrogate model based on ChEMBL data (2M molecules) to provide a lite downloadable version of the original MAIP.
Slug: demo-malaria-model
Tag: Malaria,P.falciparum
License: GPL-3.0
The Ersilia team will revise your model requests and likely start a public discussion around it.
At some point, your model will be approved. You will see an /approve
comment in the GitHub issues.
Approval will trigger some GitHub Actions. Eventually, the ersilia-bot
will post an informative message in your issue. Importantly, this message will contain a link to a new model repository placeholder, named, for example, ersilia-os/eosXabc
. This code is arbitrarily assigned by Ersilia and will change every time. You should not worry about creating one manually and you should never modify it.
Go to the model repository page: https://github.com/ersilia-os/eosXabc, in this case.
Fork the repository to your username.
Clone the forked repository. This should create a eosXabc
folder in your local filesystem.
For this demo, you have to clone the current repository (ersilia-os/eos-demo
). This will create an eos-demo
folder in your local filesystem.
Run the following script to populate your forked model with the demo data: python /path/to/eos-demo/populate.py /path/to/eosXabc
.
Your eosXabc
has been populated with model parameters, dependencies and extra metadata. It is now ready for commit.
Commit changes and push changes to eosXabc
.
Open a PR to the main
branch at ersilia-os/eosXabc
. GitHub Actions workflows will be triggered to ensure that your code works as expected.
The Ersilia team will revise your PR and merge it eventually. More GitHub Actions workflows will be triggered at this point.
Once the model is merged, you should see it in Ersilia's AirTable.
The ersilia-bot
will open a new issue at ersilia-os/eosXabc
. As you will see, someone from the Ersilia community will be assigned as a reviewer of the model.
If you are a member of the Ersilia Slack workspace, then you may also see activity triggered around your model.
Now that we have an idea of the contents of the Ersilia Model Template, we will follow the example of a simple but widely used model to calculate the synthetic accessibility of small molecule compounds. Synthetic accessibility measures the feasibility of synthesizing a molecule in the laboratory. In 2009, Peter Ertl presented the synthetic accessiblity (SA) score, based on measures of molecular complexity and occurrence of certain fragments in the small molecule structure. High (greater than 6) SA scores denote difficult-to-synthesize molecules, and low (lower than 3) SA scores suggest that the molecule will be easy to synthesize.
Please fill in the issue fields as accurately as possible and wait for review and approval by one of the Ersilia maintainers.
It is important that you read the original publication in order to understand the training data, the limitations of the model, the validation performed and the characteristic of the algorithm, among other details. In this case, this is a classic (old) publication from a Novartis team. They analyzed small fragments in the PubChem database and devised a molecular complexity score that takes into account molecule size, presence of non-standard structural features, presence of large rings, etc. The authors validated the model by comparing their SA score with synthetic feasibility as estimated by medicinal chemists for a set of 40 molecules.
Code to calculate the SA score does not seem to be available from the publication. Fortunately, though, the RDKit library, in its contributions module, contains an implementation of the SA score. The code can be found here. This RDKit-based implementation was developed in 2013 by Peter Ertl and Greg Laundrum.
Both the link to the code and to the original publication are accessible from the Ersilia Model Hub AirTable database.
Before incorporating the sa-score
model to the Ersilia Model Hub, we need to make sure that we can actually run the code provided by the third party. In this case, upon quick inspection, two elements seem to be central in the repository:
The sascorer.py
script, containing the main code. We can consider this file to be the model code.
The fpscores.pkl.gz
compressed file, containing pre-calculated fragment scores. In this simple case, we can consider this file to be the model parameters.
No installation instructions are provided for this model. However, the sascorer.py
file import
statements indicate that, at least, rdkit
is necessary. We can create a Conda environment and install the rdkit
library as follows:
We can download code and parameters of the directly from the RDKit contributions repository. Here, we store these data in a folder named SA_Score
located in the ~/Desktop
.
Often, the model will be available as a full GitHub repository. In these cases, you can simply clone the repository.
Inspection of the sascorer.py
file indicates that we can run this script from the terminal:
The Chem.SmilesMolSupplier
takes a file containing SMILES strings and identifiers, separated by a space character. The file expects a header. Let's create a file with a few molecules. We looked for examples in DrugBank:
Now let's see if the model works as expected:
It does work! The output in the terminal looks like this:
Many repositories give a clear description of the expected input format. For the SA scorer, the expected input was not clearly specified, and previous knowledge of the Chem.SmilesMolSupplier
method was necessary.
Now that we know that the code can run in our local machine, we can fork the new repository created by the Model Request Workflow and start working on it.
Always fork the repository to your user, and clone it from there in our local machine. Let's do it in the ~/Desktop
:
Let's now place the code and the parameters in the model
folder (in the framework
and checkpoints
sub folders, respectively):
The checkpoints contains a dummy checkpoints.joblib
that can be deleted.
Note that here we are migrating code and parameters to different folders. This may cause critical errors if code expects to find parameters at a certain relative location. Try to locate the pointers to the model parameters and change the paths. Only, and exceptionally, if the model architecture is too complex we can keep code and parameters in the /framework
folder. Please ask for permission to Ersilia's team before doing it.
Now it is time to write some code. Here we will follow the description of the model
folder given in the Model Template page.
The eos-template
provides a main.py
that will guide us through the deployment steps. We will adapt the main.py
to our specific needs, and run it from the run.sh
file present in the /model directory. The arguments are, respectively, the path to the file, the input and the output. Ersilia takes care of passing the right arguments to the run.sh
file. Please do not modify it.
Going back to our model of interest, we have identified three necessary steps to run the model:
Input adapter
SAScorer (in this case, the sascorer.py
which is already written)
Output adapter
Write the input adapter
By default, for chemical compound inputs, Ersilia uses single-column files with a header (see the service.py
file). However, the sascorer.py
expects a two-column file. Let's write an input adapter:
The script creates an intermediate tmp_input.smi
file that can be used as input for sascorer.py
. We can keep this as a separate file under /code, but since it is a small function, we will write it inside the main.py
file:
Make sure that parameters are read
So far, we haven't pointed to the model parameters. When migrating code and parameters, we separated the sascore.py
file and the fpscores.pkl.gz
file.
Let's inspect sascore.py
to understand how parameters are read. There is a readFragmentScore
function that does this job. We need to modify it to point to the checkpoints
folder:
Run the model inside main.py
We now have the input adapter and the model code and parameters. We simply need to run the model in main.py
by calling the sascorer.py.
We first make sure to import the necessary packages and delete the non-necessary ones (in this case, the MW by RDKit). The sa-scorer uses time
to measure how long did the model take, as well as the functions readFragmentScores
and processMols
.
The input file is no longer passed as a sys.argv
, so we modify it with the temporal input file we just created
The processMols()
function was simply printing the output without saving the results, so we have modified it in the sascorer.py
to get the output:
We need to understand the output of the model in order to collect it correctly. The easiest will be to add a print(R) statement in main.py and run the run.sh
file with a mock test file.
We observe that the output provided by sascorer.py
has three columns (tab-separated), so we need to adapt it.
Likewise, we can subsitute this piece of code in main.py
:
Finally, let's add one more line to main.py to clean up the temporary files we have created:
Let's now check that the scripts run as expected. Eventually, Ersilia will run this code from an arbitrary location, so it is best to test it outside the framework
folder. We can create an input file in the ~/Desktop
.
To test the model, we simply have to execute the run.sh
script.
You should get the output.csv
file in your ~/Desktop
. The output contains five predictions, corresponding to the five molecules in the molecules.csv
file.
service.py
file, if necessaryThe service.py
file provided by default in the template manages chemistry inputs and expects tabular outputs. Therefore, in principle, you do not have to modify this file.
Modifying the service.py
file is intended for advanced users only. Please use the Slack #internships
channel if you think your model of interest requires modification of this file.
install.yml
fileThe install.yml
file should include all the installation steps that you run after creating the working Conda environment. In the case of sa-scorer
, we only installed RDKit:
metadata.yml
fileDon't forget to document the model. Read the instructions to write the metadata
file page. Feel free to ask for help in the Slack #internships
channel.
The metadata.yml for this model should read like:
Before committing our new model to Ersilia, we must check it will work within the Ersilia environment. To do so, we have a very convenient option at model fetch time, --repo_path
that allows us to specify a local path to the model we are fetching (so, instead of looking for it online it will use the local folder). It is crucial to complete this step before committing the model to GitHub.
If this runs without issues, the model is ready to be incorporated. If not, please go back to step 4 and revise the model indeed is running without issues.
We are now ready to commit changes, first to our fork, and then to the main Ersilia repository by opening a pull request. Before doing so, complete the steps below:
Probably, there is a few files, such as mock.csv
, that are no longer needed (this is used solely to initialize Git-LFS). Please remove them before committing and also eliminate the Git-LFS tracking from .gitattributes
(see below for more on Git-LFS)
.gitattributes
fileIf you have large files, you will have to track them with Git LFS. The template already provides a collection of common extensions to track with Git LFS (see .gitattributes
file). However, it is possible that your parameters do not have any of these extensions.
Let's track our parameters, even if the file is not particularly big:
Now commit the bulk of your work:
Once the model is ready, open a pull request to merge your changes back into the main repository. This will trigger a series of GitHub Actions:
Security workflow: makes sure that no private key is released with the model
Json syntax check: controls that the metadata file does not contain Json syntax errors (does not check the content, only syntax)
Model Test on PR: this workflow will first test that the metadata.json has the correct fields (if it doesn't, it will fail. Please look at the Action Run to get more information on why it has failed). If the metadata is correct, it will then go onto installing Ersilia and testing the model.
If the Actions at Pull request fail, please check them and work on debugging them before making a new pull request. Ersilia maintainers will only merge PR's that have passed all the checks. Once the PR has the three green checks, the PR will be merged. This triggers a Model Test on Push action that will:
Test the model once more
Update the README.md
and AirTable metadata
Open a new issue requesting two Ersilia Community members to test the model.
This workflow is also triggered each time there is a push to the repository, to ensure changes to the code do maintain model functionality and metadata is not outdated. If the model testing works, two final actions will be triggered:
Upload model to Dockerhub: the model will be packaged in a docker image and made available via the Ersilia DockerHub page
Upload model to S3: the model is zipped and uploaded to S3, to facilitate upload and download from the CLI and avoid incurring Git-LFS bandwidth problems.
You can now visit the eos9ei3
GitHub repository and check that your work is publicly available.
We are ready to test the model in the context of the Ersilia CLI. To run the model, simply run:
The input output should look like this:
Debugging the fetch
and the api
commands of Ersilia can be very complicated. Please reach out to @Miquel directly if you find problems at this step.
As mentioned, the workflow will also trigger a request for model testing to members of the Ersilia community via a GitHub issue in the same repository. The original model contributor should make sure the modle is working for different users and answer any questions or issues that might arise during model testing. If amends must be made, the original model contributor should work on those
Please, help us keep a healthy environment and avoid duplication of files and consuming of our Git LFS quota. Delete the repository fork after the model has been successfully incorporated.
In summary, the steps to incorporate a model to the Ersilia Model Hub are the following. We are assuming that the model can be installed in a Conda environment.
Download the model from a third party repository to your local machine.
Install the model in a dedicated Conda environment and make sure you can run it.
Open a Model Request issue in the Ersilia Model Hub code repository. Wait for approval to automatically obtain a new model repository from the eos-template
Fork the new repository.
Place model code in model/framework
and model parameters in model/checkpoints
.
Write the necessary code to obtain a run.sh
that simply takes one input file and produces one output file. Be sure to use absolute paths throughout.
Edit the service.py
file, if necessary.
Edit the Dockerfile
file to reflect the installation steps followed in 2.
Update the metadata.json
following the guidelines
Make sure that .gitattributes
tracks your model parameters.
Open a PR to the the model repository, and check that all tests are passed. If not, try to identify the bug to solve it.
Once the PR passes all the tests and is merged, activate the Ersilia CLI and fetch the model.
Serve the model and run the default API.
Check that the model can be run from DockerHub.
Check other users can also run the model.
Delete your fork.
smiles | Name | sa_score |
---|---|---|