Christina Theodoris commited on
Commit
4fdb850
1 Parent(s): 9c62f4c

Rename loom tokenizer function and modify example notebook for adata.

Browse files
examples/tokenizing_scRNAseq_data.ipynb CHANGED
@@ -1,31 +1,31 @@
1
  {
2
  "cells": [
3
  {
4
- "attachments": {},
5
  "cell_type": "markdown",
6
  "id": "a91bca46-c056-4784-8c6c-b0f5d3f33496",
7
  "metadata": {
8
  "tags": []
9
  },
10
  "source": [
11
- "## Tokenizing .loom single cell RNA-seq data to rank value encoding .dataset format"
12
  ]
13
  },
14
  {
15
- "attachments": {},
16
  "cell_type": "markdown",
17
  "id": "350e6252-b783-494b-9767-f087eb868a15",
18
  "metadata": {},
19
  "source": [
20
- "#### Input data is a directory with .loom files containing raw counts from single cell RNAseq data, including all genes detected in the transcriptome without feature selection. \n",
21
  "\n",
22
- "#### Genes should be labeled with Ensembl IDs (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 (column attribute \"n_counts\") to be used for normalization.\n",
 
 
23
  "\n",
24
  "#### 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\"}. \n",
25
  "\n",
26
  "#### 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.\n",
27
  "\n",
28
- "#### If one's data is in other formats besides .loom, one can use the relevant tools (such as Anndata tools) to convert the file to a .loom format prior to running the transcriptome tokenizer."
29
  ]
30
  },
31
  {
@@ -45,8 +45,11 @@
45
  "metadata": {},
46
  "outputs": [],
47
  "source": [
48
- "tk = TranscriptomeTokenizer({\"cell_type\": \"cell_type\", \"organ_major\": \"organ_major\"}, nproc=4)\n",
49
- "tk.tokenize_data(\"loom_data_directory\", \"output_directory\", \"output_prefix\", file_format=\"loom\")"
 
 
 
50
  ]
51
  }
52
  ],
 
1
  {
2
  "cells": [
3
  {
 
4
  "cell_type": "markdown",
5
  "id": "a91bca46-c056-4784-8c6c-b0f5d3f33496",
6
  "metadata": {
7
  "tags": []
8
  },
9
  "source": [
10
+ "## Tokenizing .loom or .h5ad single cell RNA-seq data to rank value encoding .dataset format"
11
  ]
12
  },
13
  {
 
14
  "cell_type": "markdown",
15
  "id": "350e6252-b783-494b-9767-f087eb868a15",
16
  "metadata": {},
17
  "source": [
18
+ "#### 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.\n",
19
  "\n",
20
+ "#### 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.\n",
21
+ "\n",
22
+ "#### 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.\n",
23
  "\n",
24
  "#### 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\"}. \n",
25
  "\n",
26
  "#### 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.\n",
27
  "\n",
28
+ "#### 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."
29
  ]
30
  },
31
  {
 
45
  "metadata": {},
46
  "outputs": [],
47
  "source": [
48
+ "tk = TranscriptomeTokenizer({\"cell_type\": \"cell_type\", \"organ_major\": \"organ\"}, nproc=16)\n",
49
+ "tk.tokenize_data(\"loom_data_directory\", \n",
50
+ " \"output_directory\", \n",
51
+ " \"output_prefix\", \n",
52
+ " file_format=\"loom\")"
53
  ]
54
  }
55
  ],
geneformer/tokenizer.py CHANGED
@@ -146,7 +146,7 @@ class TranscriptomeTokenizer:
146
  file_found = 0
147
  # loops through directories to tokenize .loom or .h5ad files
148
  tokenize_file_fn = (
149
- self.tokenize_file if file_format == "loom" else self.tokenize_anndata
150
  )
151
  for file_path in data_directory.glob("*.{}".format(file_format)):
152
  file_found = 1
@@ -209,8 +209,8 @@ class TranscriptomeTokenizer:
209
  for i in range(0, len(filter_pass_loc), chunk_size):
210
  idx = filter_pass_loc[i:i+chunk_size]
211
 
212
- X_view = adata[idx, coding_miRNA_loc].X
213
  n_counts = adata[idx].obs['n_counts'].values[:, None]
 
214
  X_norm = (X_view / n_counts * target_sum / norm_factor_vector)
215
  X_norm = sp.csr_matrix(X_norm)
216
 
@@ -228,7 +228,7 @@ class TranscriptomeTokenizer:
228
 
229
  return tokenized_cells, file_cell_metadata
230
 
231
- def tokenize_file(self, loom_file_path, target_sum=10_000):
232
  if self.custom_attr_name_dict is not None:
233
  file_cell_metadata = {
234
  attr_key: [] for attr_key in self.custom_attr_name_dict.keys()
@@ -298,7 +298,7 @@ class TranscriptomeTokenizer:
298
  return tokenized_cells, file_cell_metadata
299
 
300
  def create_dataset(self, tokenized_cells, cell_metadata, use_generator=False):
301
- print("Creating dataset...")
302
  # create dict for dataset creation
303
  dataset_dict = {"input_ids": tokenized_cells}
304
  if self.custom_attr_name_dict is not None:
@@ -329,4 +329,4 @@ class TranscriptomeTokenizer:
329
  measure_length, num_proc=self.nproc
330
  )
331
 
332
- return output_dataset_truncated_w_length
 
146
  file_found = 0
147
  # loops through directories to tokenize .loom or .h5ad files
148
  tokenize_file_fn = (
149
+ self.tokenize_loom if file_format == "loom" else self.tokenize_anndata
150
  )
151
  for file_path in data_directory.glob("*.{}".format(file_format)):
152
  file_found = 1
 
209
  for i in range(0, len(filter_pass_loc), chunk_size):
210
  idx = filter_pass_loc[i:i+chunk_size]
211
 
 
212
  n_counts = adata[idx].obs['n_counts'].values[:, None]
213
+ X_view = adata[idx, coding_miRNA_loc].X
214
  X_norm = (X_view / n_counts * target_sum / norm_factor_vector)
215
  X_norm = sp.csr_matrix(X_norm)
216
 
 
228
 
229
  return tokenized_cells, file_cell_metadata
230
 
231
+ def tokenize_loom(self, loom_file_path, target_sum=10_000):
232
  if self.custom_attr_name_dict is not None:
233
  file_cell_metadata = {
234
  attr_key: [] for attr_key in self.custom_attr_name_dict.keys()
 
298
  return tokenized_cells, file_cell_metadata
299
 
300
  def create_dataset(self, tokenized_cells, cell_metadata, use_generator=False):
301
+ print("Creating dataset.")
302
  # create dict for dataset creation
303
  dataset_dict = {"input_ids": tokenized_cells}
304
  if self.custom_attr_name_dict is not None:
 
329
  measure_length, num_proc=self.nproc
330
  )
331
 
332
+ return output_dataset_truncated_w_length