File size: 15,757 Bytes
0161e74 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | import json
import pickle
from pathlib import Path
from collections import Counter, OrderedDict
from typing import Dict, Iterable, List, Optional, Tuple, Union
from typing_extensions import Self
import numpy as np
import pandas as pd
import torch
import torchtext.vocab as torch_vocab
from torchtext.vocab import Vocab
# from transformers.tokenization_utils import PreTrainedTokenizer
# from transformers import AutoTokenizer, BertTokenizer
from .. import logger
class GeneVocab(Vocab):
"""
Vocabulary for genes.
"""
def __init__(
self,
gene_list_or_vocab: Union[List[str], Vocab],
specials: Optional[List[str]] = None,
special_first: bool = True,
default_token: Optional[str] = "<pad>",
) -> None:
"""
Initialize the vocabulary.
Note: add specials only works when init from a gene list.
Args:
gene_list_or_vocab (List[str] or Vocab): List of gene names or a
Vocab object.
specials (List[str]): List of special tokens.
special_first (bool): Whether to add special tokens to the beginning
of the vocabulary.
default_token (str): Default token, by default will set to "<pad>",
if "<pad>" is in the vocabulary.
"""
if isinstance(gene_list_or_vocab, Vocab):
_vocab = gene_list_or_vocab
if specials is not None:
raise ValueError(
"receive non-empty specials when init from a Vocab object."
)
elif isinstance(gene_list_or_vocab, list):
_vocab = self._build_vocab_from_iterator(
gene_list_or_vocab,
specials=specials,
special_first=special_first,
)
else:
raise ValueError(
"gene_list_or_vocab must be a list of gene names or a Vocab object."
)
super().__init__(_vocab.vocab)
if default_token is not None and default_token in self:
self.set_default_token(default_token)
@classmethod
def from_file(cls, file_path: Union[Path, str]) -> Self:
"""
Load the vocabulary from a file. The file should be either a pickle or a
json file of token to index mapping.
"""
if isinstance(file_path, str):
file_path = Path(file_path)
if file_path.suffix == ".pkl":
with file_path.open("rb") as f:
vocab = pickle.load(f)
return cls(vocab)
elif file_path.suffix == ".json":
with file_path.open("r") as f:
token2idx = json.load(f)
return cls.from_dict(token2idx)
else:
raise ValueError(
f"{file_path} is not a valid file type. "
"Only .pkl and .json are supported."
)
@classmethod
def from_dict(
cls,
token2idx: Dict[str, int],
default_token: Optional[str] = "<pad>",
) -> Self:
"""
Load the vocabulary from a dictionary.
Args:
token2idx (Dict[str, int]): Dictionary mapping tokens to indices.
"""
# initiate an empty vocabulary first
_vocab = cls([])
# add the tokens to the vocabulary, GeneVocab requires consecutive indices
for t, i in sorted(token2idx.items(), key=lambda x: x[1]):
_vocab.insert_token(t, i)
if default_token is not None and default_token in _vocab:
_vocab.set_default_token(default_token)
return _vocab
def _build_vocab_from_iterator(
self,
iterator: Iterable,
min_freq: int = 1,
specials: Optional[List[str]] = None,
special_first: bool = True,
) -> Vocab:
"""
Build a Vocab from an iterator. This function is modified from
torchtext.vocab.build_vocab_from_iterator. The original function always
splits tokens into characters, which is not what we want.
Args:
iterator (Iterable): Iterator used to build Vocab. Must yield list
or iterator of tokens.
min_freq (int): The minimum frequency needed to include a token in
the vocabulary.
specials (List[str]): Special symbols to add. The order of supplied
tokens will be preserved.
special_first (bool): Whether to add special tokens to the beginning
Returns:
torchtext.vocab.Vocab: A `Vocab` object
"""
counter = Counter()
counter.update(iterator)
if specials is not None:
for tok in specials:
del counter[tok]
sorted_by_freq_tuples = sorted(counter.items(), key=lambda x: x[0])
sorted_by_freq_tuples.sort(key=lambda x: x[1], reverse=True)
ordered_dict = OrderedDict(sorted_by_freq_tuples)
if specials is not None:
if special_first:
specials = specials[::-1]
for symbol in specials:
ordered_dict.update({symbol: min_freq})
ordered_dict.move_to_end(symbol, last=not special_first)
word_vocab = torch_vocab.vocab(ordered_dict, min_freq=min_freq)
return word_vocab
@property
def pad_token(self) -> Optional[str]:
"""
Get the pad token.
"""
if getattr(self, "_pad_token", None) is None:
self._pad_token = None
return self._pad_token
@pad_token.setter
def pad_token(self, pad_token: str) -> None:
"""
Set the pad token. Will not add the pad token to the vocabulary.
Args:
pad_token (str): Pad token, should be in the vocabulary.
"""
if pad_token not in self:
raise ValueError(f"{pad_token} is not in the vocabulary.")
self._pad_token = pad_token
def save_json(self, file_path: Union[Path, str]) -> None:
"""
Save the vocabulary to a json file.
"""
if isinstance(file_path, str):
file_path = Path(file_path)
with file_path.open("w") as f:
json.dump(self.get_stoi(), f, indent=2)
def set_default_token(self, default_token: str) -> None:
"""
Set the default token.
Args:
default_token (str): Default token.
"""
if default_token not in self:
raise ValueError(f"{default_token} is not in the vocabulary.")
self.set_default_index(self[default_token])
def get_default_gene_vocab() -> GeneVocab:
"""
Get the default gene vocabulary, consisting of gene symbols and ids.
"""
vocab_file = Path(__file__).parent / "default_gene_vocab.json"
if not vocab_file.exists():
logger.info(
f"No existing default vocab, will build one and save to {vocab_file}"
)
return _build_default_gene_vocab(save_vocab_to=vocab_file)
logger.info(f"Loading gene vocabulary from {vocab_file}")
return GeneVocab.from_file(vocab_file)
def _build_default_gene_vocab(
download_source_to: str = "/tmp",
save_vocab_to: Union[Path, str, None] = None,
) -> GeneVocab:
"""
Build the default gene vocabulary from HGNC gene symbols.
Args:
download_source_to (str): Directory to download the source data.
save_vocab_to (Path or str): Path to save the vocabulary. If None,
the vocabulary will not be saved. Default to None.
"""
gene_collection_file = (
Path(download_source_to) / "human.gene_name_symbol.from_genenames.org.tsv"
)
if not gene_collection_file.exists():
# download and save file from url
url = (
"https://www.genenames.org/cgi-bin/download/custom?col=gd_app_sym&"
"col=md_ensembl_id&status=Approved&status=Entry%20Withdrawn&hgnc_dbtag"
"=on&order_by=gd_app_sym_sort&format=text&submit=submit"
)
import requests
r = requests.get(url)
gene_collection_file.write_text(r.text)
logger.info(f"Building gene vocabulary from {gene_collection_file}")
df = pd.read_csv(gene_collection_file, sep="\t")
gene_list = df["Approved symbol"].dropna().unique().tolist()
gene_vocab = GeneVocab(gene_list) # no special tokens set in default vocab
if save_vocab_to is not None:
gene_vocab.save_json(Path(save_vocab_to))
return gene_vocab
def tokenize_batch(
data: np.ndarray,
gene_ids: np.ndarray,
return_pt: bool = True,
append_cls: bool = True,
include_zero_gene: bool = False,
cls_id: int = "<cls>",
mod_type: np.ndarray = None,
cls_id_mod_type: int = None,
) -> List[Tuple[Union[torch.Tensor, np.ndarray]]]:
"""
Tokenize a batch of data. Returns a list of tuple (gene_id, count).
Args:
data (array-like): A batch of data, with shape (batch_size, n_features).
n_features equals the number of all genes.
gene_ids (array-like): A batch of gene ids, with shape (n_features,).
return_pt (bool): Whether to return torch tensors of gene_ids and counts,
default to True.
Returns:
list: A list of tuple (gene_id, count) of non zero gene expressions.
"""
if data.shape[1] != len(gene_ids):
raise ValueError(
f"Number of features in data ({data.shape[1]}) does not match "
f"number of gene_ids ({len(gene_ids)})."
)
if mod_type is not None and data.shape[1] != len(mod_type):
raise ValueError(
f"Number of features in data ({data.shape[1]}) does not match "
f"number of mod_type ({len(mod_type)})."
)
tokenized_data = []
for i in range(len(data)):
row = data[i]
mod_types = None
if include_zero_gene:
values = row
genes = gene_ids
if mod_type is not None:
mod_types = mod_type
else:
idx = np.nonzero(row)[0]
values = row[idx]
genes = gene_ids[idx]
if mod_type is not None:
mod_types = mod_type[idx]
if append_cls:
genes = np.insert(genes, 0, cls_id)
values = np.insert(values, 0, 0)
if mod_type is not None:
mod_types = np.insert(mod_types, 0, cls_id_mod_type)
if return_pt:
genes = torch.from_numpy(genes).long()
values = torch.from_numpy(values).float()
if mod_type is not None:
mod_types = torch.from_numpy(mod_types).long()
tokenized_data.append((genes, values, mod_types))
return tokenized_data
def pad_batch(
batch: List[Tuple],
max_len: int,
vocab: Vocab,
pad_token: str = "<pad>",
pad_value: int = 0,
cls_appended: bool = True,
vocab_mod: Vocab = None,
) -> Dict[str, torch.Tensor]:
"""
Pad a batch of data. Returns a list of Dict[gene_id, count].
Args:
batch (list): A list of tuple (gene_id, count).
max_len (int): The maximum length of the batch.
vocab (Vocab): The vocabulary containing the pad token.
pad_token (str): The token to pad with.
Returns:
Dict[str, torch.Tensor]: A dictionary of gene_id and count.
"""
max_ori_len = max(len(batch[i][0]) for i in range(len(batch)))
max_len = min(max_ori_len, max_len)
pad_id = vocab[pad_token]
if vocab_mod is not None:
mod_pad_id = vocab_mod[pad_token]
gene_ids_list = []
values_list = []
mod_types_list = []
for i in range(len(batch)):
gene_ids, values, mod_types = batch[i]
if len(gene_ids) > max_len:
# sample max_len genes
if not cls_appended:
idx = np.random.choice(len(gene_ids), max_len, replace=False)
else:
idx = np.random.choice(len(gene_ids) - 1, max_len - 1, replace=False)
idx = idx + 1
idx = np.insert(idx, 0, 0)
gene_ids = gene_ids[idx]
values = values[idx]
if mod_types is not None:
mod_types = mod_types[idx]
if len(gene_ids) < max_len:
gene_ids = torch.cat(
[
gene_ids,
torch.full(
(max_len - len(gene_ids),), pad_id, dtype=gene_ids.dtype
),
]
)
values = torch.cat(
[
values,
torch.full((max_len - len(values),), pad_value, dtype=values.dtype),
]
)
if mod_types is not None:
mod_types = torch.cat(
[
mod_types,
torch.full(
(max_len - len(mod_types),),
mod_pad_id,
dtype=mod_types.dtype,
),
]
)
gene_ids_list.append(gene_ids)
values_list.append(values)
if mod_types is not None:
mod_types_list.append(mod_types)
batch_padded = {
"genes": torch.stack(gene_ids_list, dim=0),
"values": torch.stack(values_list, dim=0),
}
if mod_types is not None:
batch_padded["mod_types"] = torch.stack(mod_types_list, dim=0)
return batch_padded
def tokenize_and_pad_batch(
data: np.ndarray,
gene_ids: np.ndarray,
max_len: int,
vocab: Vocab,
pad_token: str,
pad_value: int,
append_cls: bool = True,
include_zero_gene: bool = False,
cls_token: str = "<cls>",
return_pt: bool = True,
mod_type: np.ndarray = None,
vocab_mod: Vocab = None,
) -> Dict[str, torch.Tensor]:
"""
Tokenize and pad a batch of data. Returns a list of tuple (gene_id, count).
"""
cls_id = vocab[cls_token]
if mod_type is not None:
cls_id_mod_type = vocab_mod[cls_token]
tokenized_data = tokenize_batch(
data,
gene_ids,
return_pt=return_pt,
append_cls=append_cls,
include_zero_gene=include_zero_gene,
cls_id=cls_id,
mod_type=mod_type,
cls_id_mod_type=cls_id_mod_type if mod_type is not None else None,
)
batch_padded = pad_batch(
tokenized_data,
max_len,
vocab,
pad_token,
pad_value,
cls_appended=append_cls,
vocab_mod=vocab_mod,
)
return batch_padded
def random_mask_value(
values: Union[torch.Tensor, np.ndarray],
mask_ratio: float = 0.15,
mask_value: int = -1,
pad_value: int = 0,
) -> torch.Tensor:
"""
Randomly mask a batch of data.
Args:
values (array-like):
A batch of tokenized data, with shape (batch_size, n_features).
mask_ratio (float): The ratio of genes to mask, default to 0.15.
mask_value (int): The value to mask with, default to -1.
pad_value (int): The value of padding in the values, will be kept unchanged.
Returns:
torch.Tensor: A tensor of masked data.
"""
if isinstance(values, torch.Tensor):
# it is crutial to clone the tensor, otherwise it changes the original tensor
values = values.clone().detach().numpy()
else:
values = values.copy()
for i in range(len(values)):
row = values[i]
non_padding_idx = np.nonzero(row - pad_value)[0]
n_mask = int(len(non_padding_idx) * mask_ratio)
mask_idx = np.random.choice(non_padding_idx, n_mask, replace=False)
row[mask_idx] = mask_value
return torch.from_numpy(values).float()
|