macwiatrak commited on
Commit
7258529
·
verified ·
1 Parent(s): 9fd296f

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +115 -3
README.md CHANGED
@@ -1,3 +1,115 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ tags:
4
+ - bacformer
5
+ - genomics
6
+ - genome
7
+ - bacteria
8
+ - protein
9
+ - phenotype
10
+ - prokaryotes
11
+ pretty_name: Bacformer genome embeddings with phenotypic traits labels dataset
12
+ size_categories:
13
+ - 10K<n<100K
14
+ ---
15
+
16
+ # Dataset for predicting phenotypic traits labels using Bacformer embeddings
17
+
18
+ A dataset containing Bacformer embeddings for a set of almost `25k` unique genomes downloaded from [NCBI GenBank](https://www.ncbi.nlm.nih.gov/genbank/)
19
+ with associated phenotypic trait labels.
20
+
21
+ The phenotypic traits have been extracted from a number of sources [1, 2, 3] and include a diversity of categorical phenotypes. We exclude phenotypic
22
+ traits with a low nr of samples, giving us 139 uniqe phenotypic traits.
23
+ 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.
24
+
25
+ The Bacformer embeddings have been computed by running [macwiatrak/bacformer-masked-complete-genomes](https://huggingface.co/macwiatrak/bacformer-masked-complete-genomes/edit/main/README.md)
26
+ in inference mode and averaging the contextual protein embeddings to get a genome embedding.
27
+
28
+ For more info on how to embed genomes with Bacformer see github - https://github.com/macwiatrak/Bacformer.
29
+
30
+ ## Usage
31
+
32
+ See the tutorial on predicting the phenotypic traits with Bacformer - [tutorial link](https://github.com/macwiatrak/Bacformer/blob/main/tutorials/phenotypic_traits_prediction_tutorial.ipynb).
33
+
34
+ ### Code snippet on how to train a linear regression model for a phenotype
35
+
36
+ ```python
37
+ import numpy as np
38
+ import pandas as pd
39
+ from datasets import load_dataset
40
+
41
+ from sklearn.model_selection import train_test_split
42
+ from sklearn.metrics import roc_auc_score
43
+ from sklearn.linear_model import LogisticRegression
44
+ from sklearn.preprocessing import StandardScaler
45
+ from sklearn.pipeline import Pipeline
46
+
47
+
48
+ # load the dataset and convert to pandas DF
49
+ df = load_dataset("macwiatrak/bacformer-genome-embeddings-with-phenotypic-traits-labels", split="train").to_pandas()
50
+
51
+ # select the phenotype
52
+ phenotype = "gideon_Catalase"
53
+ # remove the genomes with NaN values for the phenotype of interest
54
+ phenotype_df = df[df[phenotype].notna()].copy()
55
+
56
+
57
+ # ------------------------------------------------------------------
58
+ # 2 60 / 20 / 20 stratified split (train → 0.6, val → 0.2, test → 0.2)
59
+ # ------------------------------------------------------------------
60
+ X_train_val, X_test, y_train_val, y_test = train_test_split(
61
+ X, y, test_size=0.20, random_state=42, stratify=y
62
+ )
63
+ X_train, X_val, y_train, y_val = train_test_split(
64
+ X_train_val, y_train_val,
65
+ test_size=0.25, # 0.25 × 0.80 = 0.20
66
+ random_state=42,
67
+ stratify=y_train_val
68
+ )
69
+
70
+
71
+ # ------------------------------------------------------------------
72
+ # 3 Hyper-parameter search on validation set
73
+ # ------------------------------------------------------------------
74
+ param_grid = np.logspace(-4, 4, 9) # 1e-4 … 1e4
75
+ best_auc, best_C, best_model = -np.inf, None, None
76
+
77
+ for C in param_grid:
78
+ model = Pipeline(
79
+ steps=[
80
+ ("scale", StandardScaler()),
81
+ ("clf", LogisticRegression(
82
+ C=C, solver="liblinear", max_iter=2000, penalty="l2"
83
+ ))
84
+ ]
85
+ )
86
+ model.fit(X_train, y_train)
87
+
88
+ val_probs = model.predict_proba(X_val)[:, 1]
89
+ auc = roc_auc_score(y_val, val_probs)
90
+
91
+ if auc > best_auc:
92
+ best_auc, best_C, best_model = auc, C, model
93
+
94
+ print(f"Best C on validation: {best_C} | AUROC_val = {best_auc:.4f}")
95
+
96
+
97
+ # ------------------------------------------------------------------
98
+ # 4 Final evaluation on the held-out test set
99
+ # ------------------------------------------------------------------
100
+ test_probs = best_model.predict_proba(X_test)[:, 1]
101
+ test_auc = roc_auc_score(y_test, test_probs)
102
+
103
+ print(f"AUROC_test = {test_auc:.4f}")
104
+ ```
105
+
106
+ ## Contact
107
+ In case of questions/issues or feature requests please raise an issue on github - https://github.com/macwiatrak/Bacformer.
108
+
109
+ ## References
110
+
111
+ [1] Madin, Joshua S., et al. "A synthesis of bacterial and archaeal phenotypic trait data." Scientific data 7.1 (2020): 170.
112
+
113
+ [2] Weimann, Aaron, et al. "From genomes to phenotypes: Traitar, the microbial trait analyzer." MSystems 1.6 (2016): 10-1128.
114
+
115
+ [3] Brbić, Maria, et al. "The landscape of microbial phenotypic traits and associated genes." Nucleic acids research (2016): gkw964.