Skip to main content

ML analysis

Project description

Python License

In January 2024, I defended my doctoral thesis in computational toxicology, and since then, I have been contemplating how to contribute to the community with code that can simplify calculations needed according to the specific problem. Over these years, among many other things, I have worked on topics related to interpretability, visualization of chemical space, and combining the outputs of different models. In all cases, the problem was common: I often needed a lot of code for tasks that were part of my daily routine. That's why I decided to create comptox_analysis.

Installation

Dependencies

comptox_analysis requires:

  • rdkit=2023.09.6
  • bokeh=3.1.1
  • scikit-learn
  • matplotlib
  • numpy
  • pandas

User installation

The easiest way to install comptox_analysis is using pip:

pip install comptox_analysis

Example of Usage for Interpretability using highlighter

Imagine you obtain the feature importance, which in this case are fingerprints, using a ML or DL model, and you want to highlight the most important fingerprints for each molecule. To carry out this task, we first load the necessary libraries and the dataset, build a Random Forest model as an example, and then extract the feature importance.

# Import library
from comptox_analysis.highlighting.highlighting_atoms import highlighter
from rdkit import Chem
from rdkit.Chem import PandasTools
from rdkit.Chem.AllChem import GetMorganFingerprintAsBitVect
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor

df=PandasTools.LoadSDF('caco2.sdf',smilesName='Smiles')

def fingerprints_inputs2(dataframe):
        X=np.array([GetMorganFingerprintAsBitVect(mol,radius=2,nBits=2048,useFeatures=True) for mol in [Chem.MolFromSmiles(m) for m in list(dataframe.Smiles)]])
        y=dataframe.activity.astype('float')
        return X,y

X,y=fingerprints_inputs2(df)

model=RandomForestRegressor(random_state=46).fit(X,y)

columns=[f'fp_{i}' for i in range(2048)]

imp=pd.Series(data=model.feature_importances_,index=columns).sort_values(ascending=False)
imp[:10].plot.bar();

Cover Page

We are going to select compounds with two of the ten most important variables (in this case, we obtain a list with three compounds). Now, we render the image of the first compound using render_image function:

df_fp=pd.DataFrame(X,columns=columns)

indexes=df_fp[(df_fp.fp_576==1)&(df_fp.fp_779==1)].index.values

mols=df.loc[indexes,'Smiles'].tolist()

highlighter_instance=highlighter(mols)

highlighter_instance.render_image(number=0,indexes=False)

Cover Page

Also, you can visualize molecular fragments using fragmentation function:

highlighter_instance.fragmentation(n=26,number=0)[0]

Cover Page

And now, using highlighting function, you will be able to observe the fragments that you want to highlight (the package supports both, rdkit and morgan fingerprints):

highlighter_instance = highlighter([mols[0]])

highlighter_instance.highlighting(type='morgan',fingerprint_numbers=[[1300]])

Cover Page

Also, it is possible to highlight multiple compounds (in this case we are highlighting the selected fingerprints as an example9:

#for multiple compounds

highlighter_instance = highlighter(mols)

highlighter_instance.highlighting(fingerprint_numbers=[[576,779],[576,779],[576,779]])

Cover Page

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

comptox_analysis-0.9.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

comptox_analysis-0.9-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file comptox_analysis-0.9.tar.gz.

File metadata

  • Download URL: comptox_analysis-0.9.tar.gz
  • Upload date:
  • Size: 25.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.19

File hashes

Hashes for comptox_analysis-0.9.tar.gz
Algorithm Hash digest
SHA256 4f95b02692f9c917893a8678c71f2c3905476852dbdf31be9750965b27b030d2
MD5 8959d995ef77c58ac189d0fc2a7b9d70
BLAKE2b-256 4c56f4b7ff07b64bcb71397fddae9f174b2dfb5827d76d1a9efb16807e1b8338

See more details on using hashes here.

File details

Details for the file comptox_analysis-0.9-py3-none-any.whl.

File metadata

  • Download URL: comptox_analysis-0.9-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.19

File hashes

Hashes for comptox_analysis-0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 eb66634e125b95ffb84b94e5300a10674ce4bd8e10cd65766c61344e369ce90d
MD5 18bb107a06073898688f15acec92cfff
BLAKE2b-256 783009a4fa46a4ac92c9f0a25b4bda8344754ee3a75a85c0010d1c8bc040f1de

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page