macwiatrak's picture
Update README.md
7258529 verified
metadata
license: apache-2.0
tags:
  - bacformer
  - genomics
  - genome
  - bacteria
  - protein
  - phenotype
  - prokaryotes
pretty_name: Bacformer genome embeddings with phenotypic traits labels dataset
size_categories:
  - 10K<n<100K

Dataset for predicting phenotypic traits labels using Bacformer embeddings

A dataset containing Bacformer embeddings for a set of almost 25k unique genomes downloaded from NCBI GenBank with associated phenotypic trait labels.

The phenotypic traits have been extracted from a number of sources [1, 2, 3] and include a diversity of categorical phenotypes. We exclude phenotypic traits with a low nr of samples, giving us 139 uniqe phenotypic traits. If the same or similar label appeared in two different sources, we kept it as separate labels as the label collection setup may differ for the labels.

The Bacformer embeddings have been computed by running macwiatrak/bacformer-masked-complete-genomes in inference mode and averaging the contextual protein embeddings to get a genome embedding.

For more info on how to embed genomes with Bacformer see github - https://github.com/macwiatrak/Bacformer.

Usage

See the tutorial on predicting the phenotypic traits with Bacformer - tutorial link.

Code snippet on how to train a linear regression model for a phenotype

import numpy as np
import pandas as pd
from datasets import load_dataset

from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline


# load the dataset and convert to pandas DF
df = load_dataset("macwiatrak/bacformer-genome-embeddings-with-phenotypic-traits-labels", split="train").to_pandas()

# select the phenotype
phenotype = "gideon_Catalase"
# remove the genomes with NaN values for the phenotype of interest
phenotype_df = df[df[phenotype].notna()].copy()


# ------------------------------------------------------------------
# 2  60 / 20 / 20 stratified split (train → 0.6, val → 0.2, test → 0.2)
# ------------------------------------------------------------------
X_train_val, X_test, y_train_val, y_test = train_test_split(
    X, y, test_size=0.20, random_state=42, stratify=y
)
X_train, X_val, y_train, y_val = train_test_split(
    X_train_val, y_train_val,
    test_size=0.25,  # 0.25 × 0.80 = 0.20
    random_state=42,
    stratify=y_train_val
)


# ------------------------------------------------------------------
# 3  Hyper-parameter search on validation set
# ------------------------------------------------------------------
param_grid = np.logspace(-4, 4, 9)      # 1e-4 … 1e4
best_auc, best_C, best_model = -np.inf, None, None

for C in param_grid:
    model = Pipeline(
        steps=[
            ("scale", StandardScaler()),
            ("clf", LogisticRegression(
                C=C, solver="liblinear", max_iter=2000, penalty="l2"
            ))
        ]
    )
    model.fit(X_train, y_train)

    val_probs = model.predict_proba(X_val)[:, 1]
    auc = roc_auc_score(y_val, val_probs)

    if auc > best_auc:
        best_auc, best_C, best_model = auc, C, model

print(f"Best C on validation: {best_C}  |  AUROC_val = {best_auc:.4f}")


# ------------------------------------------------------------------
# 4  Final evaluation on the held-out test set
# ------------------------------------------------------------------
test_probs = best_model.predict_proba(X_test)[:, 1]
test_auc  = roc_auc_score(y_test, test_probs)

print(f"AUROC_test = {test_auc:.4f}")

Contact

In case of questions/issues or feature requests please raise an issue on github - https://github.com/macwiatrak/Bacformer.

References

[1] Madin, Joshua S., et al. "A synthesis of bacterial and archaeal phenotypic trait data." Scientific data 7.1 (2020): 170.

[2] Weimann, Aaron, et al. "From genomes to phenotypes: Traitar, the microbial trait analyzer." MSystems 1.6 (2016): 10-1128.

[3] Brbić, Maria, et al. "The landscape of microbial phenotypic traits and associated genes." Nucleic acids research (2016): gkw964.