Model
helical.models.geneformer.Geneformer
Bases: HelicalRNAModel
Geneformer Model. The Geneformer Model is a transformer-based model that can be used to extract gene embeddings from single-cell RNA-seq data. Both versions are made available through this interface. Both versions of Geneformer (v1 and v2) have different sub-models with varying numbers of layers, context size and pretraining set. The available models are the following:
Version 1.0: - gf-12L-30M-i2048 - gf-6L-30M-i2048
Version 2.0: - gf-12L-95M-i4096 - gf-12L-95M-i4096-CLcancer - gf-20L-95M-i4096
For a detailed explanation of the differences between these models and versions, please refer to the Geneformer model card: https://helical.readthedocs.io/en/latest/model_cards/geneformer/
Example
from helical.models.geneformer import Geneformer, GeneformerConfig
import anndata as ad
# Example configuration
model_config = GeneformerConfig(model_name="gf-12L-95M-i4096", batch_size=10)
geneformer_v2 = Geneformer(model_config)
# Example usage for base pretrained model
ann_data = ad.read_h5ad("anndata_file.h5ad")
dataset = geneformer_v2.process_data(ann_data)
embeddings = geneformer_v2.get_embeddings(dataset)
print("Base model embeddings shape:", embeddings.shape)
# Example usage for cancer-tuned model
model_config_cancer = GeneformerConfig(model_name="gf-12L-95M-i4096-CLcancer", batch_size=10)
geneformer_v2_cancer = Geneformer(model_config)
cancer_ann_data = ad.read_h5ad("anndata_file.h5ad")
cancer_dataset = geneformer_v2_cancer.process_data(cancer_ann_data)
cancer_embeddings = geneformer_v2_cancer.get_embeddings(cancer_dataset)
print("Cancer-tuned model embeddings shape:", cancer_embeddings.shape)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
configurer
|
GeneformerConfig
|
The model configration |
= default_configurer
|
Notes
The first version of the model is published in this Nature Paper. The second version of the model is available at NIH. We use the implementation from the Geneformer repository.
Source code in helical/models/geneformer/model.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
process_data(adata, gene_names='index', output_path=None, use_raw_counts=True)
Processes the data for the Geneformer model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adata
|
AnnData
|
The AnnData object containing the data to be processed. Geneformer uses Ensembl IDs to identify genes and currently supports only human genes. If the AnnData object already has an 'ensembl_id' column, the mapping step can be skipped. |
required |
gene_names
|
str
|
The column in |
"index"
|
output_path
|
str
|
If specified, saves the tokenized dataset to the given output path. |
None
|
use_raw_counts
|
bool
|
Determines whether raw counts should be used. |
True
|
Returns:
Type | Description |
---|---|
Dataset
|
The tokenized dataset in the form of a Huggingface Dataset object. |
Source code in helical/models/geneformer/model.py
get_embeddings(dataset)
Gets the gene embeddings from the Geneformer model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
Dataset
|
The tokenized dataset containing the processed data |
required |
Returns:
Type | Description |
---|---|
array
|
The gene embeddings in the form of a numpy array |