perturbgen.pp package
Submodules
perturbgen.pp.GF_tokenisation module
- perturbgen.pp.GF_tokenisation.get_args(args=None)
Parse command line arguments.
- perturbgen.pp.GF_tokenisation.main(cli_args=None)
perturbgen.pp.tokenizer module
Geneformer tokenizer.
Input data:
Required format: raw counts scRNAseq data without feature selection as .loom or anndata file.
Required row (gene) attribute: “ensembl_id”; Ensembl ID for each gene.
Required col (cell) attribute: “n_counts”; total read counts in that cell.
Optional col (cell) attribute: “filter_pass”; binary indicator of whether cell should be tokenized based on user-defined filtering criteria.
Optional col (cell) attributes: any other cell metadata can be passed on to the tokenized dataset as a custom attribute dictionary as shown below.
Usage:
>>> from geneformer import TranscriptomeTokenizer
>>> tk = TranscriptomeTokenizer({"cell_type": "cell_type", "organ_major": "organ"}, nproc=4)
>>> tk.tokenize_data("data_directory", "output_directory", "output_prefix")
Description:
Input data is a directory with .loom or .h5ad files containing raw counts from single cell RNAseq data, including all genes detected in the transcriptome without feature selection. The input file type is specified by the argument file_format in the tokenize_data function.
The discussion below references the .loom file format, but the analagous labels are required for .h5ad files, just that they will be column instead of row attributes and vice versa due to the transposed format of the two file types.
Genes should be labeled with Ensembl IDs (loom row attribute “ensembl_id”), which provide a unique identifer for conversion to tokens. Other forms of gene annotations (e.g. gene names) can be converted to Ensembl IDs via Ensembl Biomart. Cells should be labeled with the total read count in the cell (loom column attribute “n_counts”) to be used for normalization.
No cell metadata is required, but custom cell attributes may be passed onto the tokenized dataset by providing a dictionary of custom attributes to be added, which is formatted as loom_col_attr_name : desired_dataset_col_attr_name. For example, if the original .loom dataset has column attributes “cell_type” and “organ_major” and one would like to retain these attributes as labels in the tokenized dataset with the new names “cell_type” and “organ”, respectively, the following custom attribute dictionary should be provided: {“cell_type”: “cell_type”, “organ_major”: “organ”}.
Additionally, if the original .loom file contains a cell column attribute called “filter_pass”, this column will be used as a binary indicator of whether to include these cells in the tokenized data. All cells with “1” in this attribute will be tokenized, whereas the others will be excluded. One may use this column to indicate QC filtering or other criteria for selection for inclusion in the final tokenized dataset.
If one’s data is in other formats besides .loom or .h5ad, one can use the relevant tools (such as Anndata tools) to convert the file to a .loom or .h5ad format prior to running the transcriptome tokenizer.
OF NOTE: Take care that the correct token dictionary and gene median file is used for the correct model.
OF NOTE: For 95M model series, special_token should be True and model_input_size should be 4096. For 30M model series, special_token should be False and model_input_size should be 2048.
- class perturbgen.pp.tokenizer.TranscriptomeTokenizer(custom_attr_name_dict=None, nproc=1, chunk_size=512, model_input_size=4096, special_token=True, collapse_gene_ids=True, gene_median_file='/builds/ah37/cytomeister-docs/perturbgen/pp/gene_median_dict_gftokens_gc95M.pkl', token_dictionary_file='/builds/ah37/cytomeister-docs/perturbgen/pp/token_dict_gftokens_gc95M.pkl', gene_mapping_file='/builds/ah37/cytomeister-docs/perturbgen/pp/ensembl_mapping_dict_gc95M.pkl')
Bases:
object- create_dataset(tokenized_cells, cell_metadata, use_generator=False, keep_uncropped_input_ids=False)
- tokenize_anndata(adata_file_path, target_sum=10000)
- tokenize_data(data_directory: Path | str, output_directory: Path | str, output_prefix: str, file_format: Literal['loom', 'h5ad'] = 'loom', use_generator: bool = False)
Tokenize .loom files in data_directory and save as tokenized .dataset in output_directory.
Parameters:
- data_directoryPath
- Path to directory containing loom files or anndata files
- output_directoryPath
- Path to directory where tokenized data will be saved as .dataset
- output_prefixstr
- Prefix for output .dataset
- file_formatstr
- Format of input files. Can be “loom” or “h5ad”.
- use_generatorbool
- Whether to use generator or dict for tokenization.
- tokenize_files(data_directory, file_format: Literal['loom', 'h5ad'] = 'loom')
- tokenize_loom(loom_file_path, target_sum=10000)
- perturbgen.pp.tokenizer.rank_genes(gene_vector, gene_tokens)
Rank gene expression vector.
- perturbgen.pp.tokenizer.sum_ensembl_ids(data_directory, collapse_gene_ids, gene_mapping_dict, gene_token_dict, custom_attr_name_dict, file_format='loom', chunk_size=512)
- perturbgen.pp.tokenizer.tokenize_cell(gene_vector, gene_tokens)
Convert normalized gene expression vector to tokenized rank value encoding.