Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Fratello committed Jun 15, 2023
0 parents commit b244d26
Show file tree
Hide file tree
Showing 69 changed files with 5,738 additions and 0 deletions.
211 changes: 211 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# History files
.Rhistory
.Rapp.history

# Session Data files
.RData
.RDataTmp

# User-specific files
.Ruserdata

# Example code in package build process
*-Ex.R

# Output files from R CMD build
/*.tar.gz

# Output files from R CMD check
/*.Rcheck/

# RStudio files
.Rproj.user/

# produced vignettes
vignettes/*.html
vignettes/*.pdf

# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3
.httr-oauth

# knitr and R markdown default cache directories
*_cache/
/cache/

# Temporary files created by R markdown
*.utf8.md
*.knit.md

# R Environment Variables
.Renviron

# pkgdown site
docs/

# translation temp files
po/*~

# RStudio Connect folder
rsconnect/

61 changes: 61 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
FROM ubuntu:18.04

ARG UNAME=dream
ARG UID=1000
ARG GID=1000
ENV CONDA_DIR=/opt/mambaforge
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
ENV PATH=${CONDA_DIR}/bin:${PATH}

RUN rm -rf /tmp/*
RUN apt-get clean && apt-get update && \
apt-get install --no-install-recommends --yes \
gosu wget bzip2 ca-certificates \
git libcairo2 \
libxtst6 libxt6 \
fonts-texgyre \
gsfonts \
libblas-dev \
libbz2-* \
libcurl4 \
liblapack-dev \
libpcre2* \
libjpeg-turbo* \
libpangocairo-* \
libpng16* \
libtiff* \
liblzma* \
unzip \
zip \
zlib1g \
&& apt-get clean && \
rm -rf /var/lib/apt/lists/*

RUN wget --no-hsts --quiet https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh -O /tmp/mambaforge.sh && \
/bin/bash /tmp/mambaforge.sh -b -p ${CONDA_DIR} && \
rm /tmp/mambaforge.sh && \
conda clean --tarballs --index-cache --packages --yes && \
find ${CONDA_DIR} -follow -type f -name '*.a' -delete && \
find ${CONDA_DIR} -follow -type f -name '*.pyc' -delete && \
conda clean --force-pkgs-dirs --all --yes && \
echo ". ${CONDA_DIR}/etc/profile.d/conda.sh && conda activate base" >> /etc/skel/.bashrc && \
echo ". ${CONDA_DIR}/etc/profile.d/conda.sh && conda activate base" >> ~/.bashrc

COPY environment.yml /tmp/environment.yml
COPY Python_package/dist/dream-0.1.dev0-py3-none-any.whl /tmp/dream-0.1.dev0-py3-none-any.whl
COPY R_package/DREAM_0.1.0.tar.gz /tmp/DREAM_0.1.0.tar.gz

SHELL ["/bin/bash", "--login", "-c"]
RUN mamba env update -n base -f /tmp/environment.yml
RUN mamba run -n base R -e "install.packages('TopKLists', dependencies=FALSE, repos='http://cran.us.r-project.org')"
RUN mamba run -n base R -e "devtools::install_github('filosi/nettools', dependencies=FALSE, upgrade_dependencies=FALSE)"
RUN mamba run -n base R -e "install.packages('IRkernel', dependencies=FALSE, repos='http://cran.us.r-project.org'); IRkernel::installspec(user=FALSE)"
RUN mamba run -n base pip install /tmp/dream-0.1.dev0-py3-none-any.whl
RUN mamba run -n base R CMD INSTALL /tmp/DREAM_0.1.0.tar.gz

# make sure entrypoint.sh has +x flag
COPY entrypoint.sh /entrypoint.sh
RUN mkdir -p /workspace && chmod -R 777 /workspace
WORKDIR /workspace
ENTRYPOINT [ "/entrypoint.sh" ]
CMD [ "mamba", "run", "--no-capture-output", "-n", "base", "jupyter", "lab", "--ip='*'", "--NotebookApp.token=''", "--NotebookApp.password=''"]
Empty file added Python_package/.gitkeep
Empty file.
Empty file.
109 changes: 109 additions & 0 deletions Python_package/dream/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import io
import json
import pandas as pd
import numpy as np
from flask import Flask, jsonify, request
from dream.chemicals import pairwise_fingerprint_similarity
from dream.chemicals import pairwise_mcs_similarity


from dream.genetic_algorithm import genetic_algorithm

app = Flask(__name__)

@app.route("/chemicals_distance", methods=["GET", "POST"])
def chemicals_distance():
parameters = request.args.to_dict(flat=True)
method = parameters.pop("method", "fingerprint")
data = request.get_json()
smiles_dict = data["smiles"]
rev_smiles_dict = {v:k for k,v in smiles_dict.items()}
smiles = pd.Series(smiles_dict)

try:
if method == "mcs":
only_heavy_atoms = (parameters.pop("only_heavy_atoms", "TRUE") == "TRUE")
similarities = pairwise_mcs_similarity(
smiles, only_heavy_atoms=only_heavy_atoms
)
elif method == "fingerprint":
radius = int(parameters.pop("radius", 4))
n_bits = int(parameters.pop("n_bits", 2048))
similarities = pairwise_fingerprint_similarity(
smiles, radius=radius, n_bits=n_bits
)
else:
# http 400 error code: bad request: https://http.cat/400
return jsonify({"error": "Unrecognized method: " + method}), 400
except Exception as err:
return jsonify({"error": str(err)}), 400
# negative log transformation converts the similarity to a distance
distances = [(rev_smiles_dict[mol1], rev_smiles_dict[mol2], -np.log(max(1e-5, sim))) for (mol1, mol2, sim) in similarities]
return jsonify({"distances": distances})

@app.route("/genetic_algorithm", methods=["GET", "POST"])
def genetic_algorithm_api():
parameters = request.args.to_dict(flat=True)

population_size = int(parameters.pop("population_size", 100))
n_offsprings = int(parameters.pop("n_offsprings", 20))
attribute_init_prob = float(parameters.pop("attribute_init_prob", 0.3))
attribute_mutation_prob = float(parameters.pop("attribute_mutation_prob", 0.1))
crossover_prob = float(parameters.pop("crossover_prob", 0.7))
mutation_prob = float(parameters.pop("mutation_prob", 0.3))
n_generations = int(parameters.pop("n_generations", 2500))

data = request.get_json()
smiles_distances = data.pop("smiles_distances", None)
moa_distances = data.pop("moa_distances", None)
graph_distances = data.pop("graph_distances", None)
ppi_network = data.pop("ppi_network", None)
graph_rank = data.pop("graph_rank", None)
drug_targets = data.pop("drug_targets", None)

try:
check_not_none(
smiles_distances=smiles_distances,
moa_distances=moa_distances,
graph_distances=graph_distances,
ppi_network=ppi_network,
graph_rank=graph_rank,
drug_targets=drug_targets
)
except ValueError as e:
return jsonify(
{"error": "{} is missing or not well formatted".format(str(e))}
)

drug_names, population, logbook, hall_of_fame = genetic_algorithm(
smiles_distances=smiles_distances,
moa_distances=moa_distances,
graph_distances=graph_distances,
ppi_network=ppi_network,
graph_rank=graph_rank,
drug_targets=drug_targets,
population_size=population_size,
n_offsprings=n_offsprings,
attribute_init_prob=attribute_init_prob,
attribute_mutation_prob=attribute_mutation_prob,
crossover_prob=crossover_prob,
mutation_prob=mutation_prob,
n_generations=n_generations,
verbose=False,
)

return jsonify({
"drug_names": drug_names,
"population": population,
"logbook": logbook,
"hall_of_fame":hall_of_fame,
})

def check_not_none(**kwargs):
for (k, v) in kwargs.items():
if v is None:
raise ValueError(k)

if __name__ == '__main__':
app.debug = True
app.run()
Loading

0 comments on commit b244d26

Please sign in to comment.