| 3 | import scanpy as sc, pickle as pkl, numpy as np |
| 4 | |
| 5 | class SToFMTranscriptomeTokenizer(TranscriptomeTokenizer): |
| 6 | def __init__(self, *args, **kwargs): |
| 7 | super().__init__(*args, **kwargs) |
| 8 | |
| 9 | def tokenize_anndata(self, data): |
| 10 | if self.custom_attr_name_dict is not None: |
| 11 | file_cell_metadata = { |
| 12 | attr_key: [] for attr_key in self.custom_attr_name_dict.keys() |
| 13 | } |
| 14 | |
| 15 | # with lp.connect(str(loom_file_path)) as data: |
| 16 | # define coordinates of detected protein-coding or miRNA genes and vector of their normalization factors |
| 17 | coding_miRNA_loc = np.where( |
| 18 | [self.genelist_dict.get(i, False) for i in data.var["ensembl_id"]] |
| 19 | )[0] |
| 20 | norm_factor_vector = np.array( |
| 21 | [ |
| 22 | self.gene_median_dict[i] |
| 23 | for i in data.var["ensembl_id"][coding_miRNA_loc] |
| 24 | ] |
| 25 | ) |
| 26 | coding_miRNA_ids = data.var["ensembl_id"][coding_miRNA_loc] |
| 27 | coding_miRNA_tokens = np.array( |
| 28 | [self.gene_token_dict[i] for i in coding_miRNA_ids] |
| 29 | ) |
| 30 | |
| 31 | # define coordinates of cells passing filters for inclusion (e.g. QC) |
| 32 | try: |
| 33 | data.obs["filter_pass"] |
| 34 | except AttributeError: |
| 35 | var_exists = False |
| 36 | else: |
| 37 | var_exists = True |
| 38 | |
| 39 | if var_exists is True: |
| 40 | filter_pass_loc = np.where( |
| 41 | [True if i == 1 else False for i in data.obs["filter_pass"]] |
| 42 | )[0] |
| 43 | elif var_exists is False: |
| 44 | print( |
| 45 | f"data has no column attribute 'filter_pass'; tokenizing all cells." |
| 46 | ) |
| 47 | filter_pass_loc = np.array([i for i in range(data.shape[0])]) |
| 48 | |
| 49 | # scan through .loom files and tokenize cells |
| 50 | tokenized_cells = [] |
| 51 | |
| 52 | # for (_ix, _selection, view) in data.scan(items=filter_pass_loc, axis=1): |
| 53 | # # select subview with protein-coding and miRNA genes |
| 54 | # subview = view.view[coding_miRNA_loc, :] |
| 55 | subview = data[filter_pass_loc, coding_miRNA_loc] |
| 56 | |
| 57 | # normalize by total counts per cell and multiply by 10,000 to allocate bits to precision |
| 58 | # and normalize by gene normalization factors |
| 59 | subview_norm_array = ( |
| 60 | subview.X.toarray().T |
| 61 | / subview.obs.n_counts.to_numpy() |
| 62 | * 10_000 |