This is the official codebase for predicting antimicrobial resistance (AMR) using standard ML classifiers. This is a joint collaboration between the Chandar Research Lab at Mila and BioMérieux.
The repository is structured as follows:
configdirectory contains the config files for different dataset combinations and ML models from scikit-learn. We usehydrafor running experimentsdatadirectory contains the code for downloading and preprocessing the data. Preprocessing includes reinterpreting the MIC values associated with public phenotypic data according to CLSI-defined breakpoints for a given list of bacterial species.- In addition to random splits, we also create phylogeny-based splits based on intra-species genomic distances.
featuresdirectory contains the code for creating the feature matrix to train classical ML models. In this folder,kmer.pyis the most important script that extracts k-mers input genomic sequences and outputs a feature matrix.trainingdirectory contains utility scripts used during trainingtestingdirectory contains the script used to run inference using saved models.
- Clone the repository
git clone https://github.com/chandar-lab/amr-pred.gitInstalling dependencies with uv
- Install uv
- Run
uv sync. This will install all dependencies and create a virtual environmentamr-predwithin the repository. - Activate the environment using
source <path_to_repo>/.venv/bin/activate - To deactivate the environment, use
deactivate
Installing dependencies without uv
Without uv, you can create a conda environment and install from pyproject.toml using pip install -e .
conda create -n amr-pred python=3.12 -y
conda activate amr-pred
pip install -e .Generating phylogenetic folds
In order to generate phylogenetic folds, you will have to download at least one of the following genomic distance tools:
- dashing
- mash
- Download the isolates database from the NCBI website (https://www.ncbi.nlm.nih.gov/pathogens/isolates/#) in
.tsvformat. - From NCBI's AST browser, download the dataset for each species. Click "Filters" -> "Organism group" (e.g. staphylococcus aureus) -> "Resistance phenotype" (check-box SUSCEPTIBLE, RESISTANT, NOT DEFINED). Download the dataset in
.csvformat.- This step downloads the data for all antibiotics available for the given organism group. We will use this
.csvto reinterpret the data according to MIC values in the next step.
- This step downloads the data for all antibiotics available for the given organism group. We will use this
- Run the
data/reinterpret_data_with_mic.pyscript. This script creates individual specie x drug datasets based on the available MIC breakpoints in the breakpoints file.- This script requires three command line arguments to run, as well as hydra config information from
config/main.yaml. It processes one species per run, so the following arguments must be passed:--path-clsi-bps(the location of the CLSI breakpoints file),--path-data(the path to the .csv file downloaded from the NCBI for the target species), and--path-isolates, the path to the .tsv file downloaded from the NCBI.path_datasets_reinterpretedfrom the hydra config is used as the output of the script.
- This script requires three command line arguments to run, as well as hydra config information from
- Run
data/download_dataset.pyto download the raw genomic sequences from the NCBI database. Before running this script, make sure thatncbi_datasetspython package is installed. See installation instructions here.- Although this script takes a species-antibiotic combination as input via the hydra config, it will download all sequences for the given species.
- To change the target species, use
--run dataset=<difficulty>/<dataset_name>when running the script, using on the files in theconfig/datasetdirectory.
- Models are trained using the
train_model.pyscript in the root directory, and configuration is specified using hydra overrides. - The primary override necessary is
dataset=<difficulty>/<dataset_name>, which is used to specify the species-antibiotic combination that will be used for training. - An example line that will launch a training job for a_baumannii__ciprofloxacin using 1 million features, a k-mer length of 17, random evaluation folds and no sweeps is:
python ./amr-pred/train_model.py --run dataset=easy/a_baumannii__ciprofloxacin matrix.k=17 matrix.max_features=1_000_000 use_genomic_splits=False use_sweeps=False- Additional settings can be found by inspecting the config files in
config/. Hyperparameter tuning ranges are also contained therein. - To train a model on a species-drug combination that is not one of the nine included in our study, simply copy the format used by one of the given combinations into a new YAML file and change the relevant fields.
- Run
testing/test_model.pyfor running model inference. Do NOT change the format of the model name when running inference. This script takes preprocessed csv file (dataset) as input, runs the model and saves: (i) all unique kmers, (ii) feature matrix, (iii) individual predictions and labels (y_trueandy_pred), and (iv) classification report (sklearn). - Additional files
explain_model.pyandkmer_stats.pyare mirror the format oftest_model.py, but are used for interpretability k-mer extraction and dataset analysis, respectively.