datasets/data API¶
DNADataset
¶
Source code in dnallm/datasets/data.py
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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
|
__init__(ds, tokenizer=None, max_length=512)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ds
|
Dataset or DatasetDict
|
A Hugging Face Dataset containing at least 'sequence' and 'label' fields. |
required |
tokenizer
|
PreTrainedTokenizerBase
|
A Hugging Face tokenizer for encoding sequences. |
None
|
max_length
|
int
|
Maximum length for tokenization. |
512
|
Source code in dnallm/datasets/data.py
12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
augment_reverse_complement(reverse=True, complement=True)
¶
Augment the dataset by adding reverse complement sequences. This method doubles the dataset size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reverse
|
bool
|
Whether to do reverse. |
True
|
complement
|
bool
|
Whether to do complement. |
True
|
Source code in dnallm/datasets/data.py
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
|
concat_reverse_complement(reverse=True, complement=True, sep='')
¶
Augment each sample by concatenating the sequence with its reverse complement.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reverse
|
bool
|
Whether to do reverse. |
True
|
complement
|
bool
|
Whether to do complement. |
True
|
sep
|
str
|
Separator between the original and reverse complement sequences. |
''
|
Source code in dnallm/datasets/data.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
|
encode_sequences(padding='max_length', return_tensors='pt', remove_unused_columns=False, uppercase=False, lowercase=False, task='SequenceClassification')
¶
Encode all sequences using the provided tokenizer. The dataset is mapped to include tokenized fields along with the label, making it directly usable with Hugging Face Trainer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
padding
|
str
|
Padding strategy for sequences. this can be 'max_length' or 'longest'. Use 'longest' to pad to the length of the longest sequence in case of memory outage. |
'max_length'
|
return_tensors
|
str | TensorType
|
Returned tensor types, can be 'pt' or 'tf' or 'np'. |
'pt'
|
remove_unused_columns
|
bool
|
Whether to remove the original 'sequence' and 'label' columns |
False
|
uppercase
|
bool
|
Whether to convert sequences to uppercase. |
False
|
lowercase
|
bool
|
Whether to convert sequences to lowercase. |
False
|
task
|
str
|
Task type for the tokenizer. If not provided, defaults to 'SequenceClassification'. |
'SequenceClassification'
|
Source code in dnallm/datasets/data.py
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 |
|
from_huggingface(dataset_name, seq_col='sequence', label_col='labels', data_dir=None, tokenizer=None, max_length=512)
classmethod
¶
Load a dataset from the Hugging Face Hub.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name
|
str
|
Name of the dataset. |
required |
seq_col
|
str
|
Column name for the DNA sequence. |
'sequence'
|
label_col
|
str
|
Column name for the label. |
'labels'
|
data_dir
|
str
|
Data directory in a dataset. |
None
|
tokenizer
|
PreTrainedTokenizerBase
|
Tokenizer. |
None
|
max_length
|
int
|
Max token length. |
512
|
Returns:
Name | Type | Description |
---|---|---|
DNADataset |
any
|
An instance wrapping a datasets.Dataset. |
Source code in dnallm/datasets/data.py
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 |
|
from_modelscope(dataset_name, seq_col='sequence', label_col='labels', data_dir=None, tokenizer=None, max_length=512)
classmethod
¶
Load a dataset from the ModelScope.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name
|
str
|
Name of the dataset. |
required |
seq_col
|
str
|
Column name for the DNA sequence. |
'sequence'
|
label_col
|
str
|
Column name for the label. |
'labels'
|
data_dir
|
str
|
Data directory in a dataset. |
None
|
tokenizer
|
PreTrainedTokenizerBase
|
Tokenizer. |
None
|
max_length
|
int
|
Max token length. |
512
|
Returns:
Name | Type | Description |
---|---|---|
DNADataset |
any
|
An instance wrapping a datasets.Dataset. |
Source code in dnallm/datasets/data.py
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 |
|
head(head=10, show=False)
¶
Fetch the head n data from the dataset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
head
|
int
|
Number of samples to fetch. |
10
|
show
|
bool
|
Whether to print the data or return it. |
False
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary containing the first n samples. |
Source code in dnallm/datasets/data.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
|
iter_batches(batch_size)
¶
Generator that yields batches of examples from the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_size
|
int
|
Size of each batch. |
required |
Yields:
Type | Description |
---|---|
Dataset
|
A batch of examples. |
Source code in dnallm/datasets/data.py
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
|
load_local_data(file_paths, seq_col='sequence', label_col='labels', sep=None, fasta_sep='|', multi_label_sep=None, tokenizer=None, max_length=512)
classmethod
¶
Load DNA sequence datasets from one or multiple local files.
Supports input formats: csv, tsv, json, parquet, arrow, dict, fasta, txt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_paths
|
str, list, or dict
|
|
required |
seq_col
|
str
|
Column name for DNA sequences. |
'sequence'
|
label_col
|
str
|
Column name for labels. |
'labels'
|
sep
|
str
|
Delimiter for CSV, TSV, or TXT. |
None
|
fasta_sep
|
str
|
Delimiter for FASTA files. |
'|'
|
multi_label_sep
|
str
|
Delimiter for multi-label sequences. |
None
|
tokenizer
|
PreTrainedTokenizerBase
|
A tokenizer. |
None
|
max_length
|
int
|
Max token length. |
512
|
Returns:
Name | Type | Description |
---|---|---|
DNADataset |
any
|
An instance wrapping a Dataset or DatasetDict. |
Source code in dnallm/datasets/data.py
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 |
|
process_missing_data()
¶
Filter out samples with missing or empty sequences or labels.
Source code in dnallm/datasets/data.py
452 453 454 455 456 457 458 |
|
random_generate(minl, maxl=0, samples=1, gc=(0, 1), N_ratio=0.0, padding_size=0, seed=None, label_func=None, append=False)
¶
Replace the current dataset with randomly generated DNA sequences.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
minl
|
int
|
int, minimum length of the sequences |
required |
maxl
|
int
|
int, maximum length of the sequences, default is the same as minl |
0
|
samples
|
int
|
int, number of sequences to generate, default 1 |
1
|
gc
|
tuple
|
tuple, GC content range, default (0,1) |
(0, 1)
|
N_ratio
|
float
|
float, include N base in the generated sequence, default 0.0 |
0.0
|
padding_size
|
int
|
int, padding size for sequence length, default 0 |
0
|
seed
|
int
|
int, random seed, default None |
None
|
label_func
|
callable
|
A function that generates a label from a sequence. |
None
|
append
|
bool
|
bool, append the random generated data to the existed dataset or use the data as a dataset |
False
|
Source code in dnallm/datasets/data.py
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 |
|
raw_reverse_complement(ratio=0.5, seed=None)
¶
Do reverse complement of sequences in the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ratio
|
float
|
Ratio of sequences to reverse complement. |
0.5
|
seed
|
int
|
Random seed for reproducibility. |
None
|
Source code in dnallm/datasets/data.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
|
sampling(ratio=1.0, seed=None, overwrite=False)
¶
Randomly sample a fraction of the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ratio
|
float
|
Fraction of the dataset to sample. Default is 1.0 (no sampling). |
1.0
|
seed
|
int
|
Random seed for reproducibility. |
None
|
overwrite
|
bool
|
Whether to overwrite the original dataset with the sampled one. |
False
|
Returns:
Type | Description |
---|---|
any
|
A sampled dataset. |
Source code in dnallm/datasets/data.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
|
show(head=10)
¶
Display the dataset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
head
|
int
|
Number of samples to display. |
10
|
Source code in dnallm/datasets/data.py
600 601 602 603 604 605 606 607 |
|
shuffle(seed=None)
¶
Shuffle the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
int
|
Random seed for reproducibility. |
None
|
Source code in dnallm/datasets/data.py
390 391 392 393 394 395 396 397 |
|
split_data(test_size=0.2, val_size=0.1, seed=None)
¶
Split the dataset into train, test, and validation sets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
test_size
|
float
|
Proportion of the dataset to include in the test split. |
0.2
|
val_size
|
float
|
Proportion of the dataset to include in the validation split. |
0.1
|
seed
|
int
|
Random seed for reproducibility. |
None
|
Source code in dnallm/datasets/data.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
|
validate_sequences(minl=20, maxl=6000, gc=(0, 1), valid_chars='ACGTN')
¶
Filter the dataset to keep sequences containing valid DNA bases or allowed length.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
minl
|
int
|
Minimum length of the sequences. |
20
|
maxl
|
int
|
Maximum length of the sequences. |
6000
|
gc
|
tuple
|
GC content range between 0 and 1. |
(0, 1)
|
valid_chars
|
str
|
Allowed characters in the sequences. |
'ACGTN'
|
Source code in dnallm/datasets/data.py
399 400 401 402 403 404 405 406 407 408 409 410 411 |
|