Filtering API¶
Module for clonotype filtering and tier assignment.
filter ¶
Clonotype filtering for TCRsift.
Implements tiered filtering to identify antigen-specific TCR clones.
DEFAULT_THRESHOLD_TIERS
module-attribute
¶
DEFAULT_THRESHOLD_TIERS = {'tier1': {'min_cells': 10, 'min_frequency': 0.01, 'max_conditions': 2}, 'tier2': {'min_cells': 5, 'min_frequency': 0.005, 'max_conditions': 3}, 'tier3': {'min_cells': 3, 'min_frequency': 0.001, 'max_conditions': 5}, 'tier4': {'min_cells': 2, 'min_frequency': 0.0005, 'max_conditions': 10}, 'tier5': {'min_cells': 2, 'min_frequency': 0.0, 'max_conditions': 999}}
filter_clonotypes ¶
filter_clonotypes(clonotypes: DataFrame, method: str = 'threshold', tcell_type: str = 'cd8', min_cells: int = 2, min_frequency: float = 0.0, require_complete: bool = True, exclude_viral: bool = False, fdr_tiers: list | None = None, tier_definitions: dict | None = None, min_donors: int = 0, min_methods_per_donor: int = 0, min_cells_per_method: int = 0, min_frequency_per_method: float = 0.0, min_timepoints: int = 0, min_timepoints_per_donor: int = 0, min_apcs: int = 0, min_apcs_per_donor: int = 0, min_til_cells_per_donor: int = 0, verbose: bool = True, show_progress: bool = True) -> pd.DataFrame
Main filtering function that dispatches to appropriate method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clonotypes
|
DataFrame
|
Clonotype DataFrame |
required |
method
|
str
|
Filtering method: "threshold" or "logistic" |
'threshold'
|
tcell_type
|
str
|
T cell type filter: "cd8", "cd4", or "both" |
'cd8'
|
min_cells
|
int
|
Minimum cells per clone |
2
|
min_frequency
|
float
|
Minimum frequency |
0.0
|
require_complete
|
bool
|
Require complete TCR |
True
|
exclude_viral
|
bool
|
Exclude viral clones |
False
|
fdr_tiers
|
list
|
FDR tiers for logistic method |
None
|
tier_definitions
|
dict
|
Tier definitions for threshold method |
None
|
verbose
|
bool
|
Print progress information |
True
|
show_progress
|
bool
|
Show progress bar |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Filtered and tiered clonotypes |
Source code in tcrsift/filter.py
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 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 | |
filter_clonotypes_threshold ¶
filter_clonotypes_threshold(clonotypes: DataFrame, min_cells: int = 2, min_frequency: float = 0.0, max_conditions: int = 999, require_complete: bool = True, tcell_type: str | None = None, exclude_viral: bool = False, min_donors: int = 0, min_methods_per_donor: int = 0, min_cells_per_method: int = 0, min_frequency_per_method: float = 0.0, min_timepoints: int = 0, min_timepoints_per_donor: int = 0, min_apcs: int = 0, min_apcs_per_donor: int = 0, min_til_cells_per_donor: int = 0, verbose: bool = True) -> pd.DataFrame
Filter clonotypes using simple threshold criteria.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clonotypes
|
DataFrame
|
Clonotype DataFrame |
required |
min_cells
|
int
|
Minimum cell count per clone |
2
|
min_frequency
|
float
|
Minimum frequency in any condition |
0.0
|
max_conditions
|
int
|
Maximum number of conditions clone can appear in. Uses antigen/condition counts when available, otherwise sample count is used as a proxy. |
999
|
require_complete
|
bool
|
Require both alpha and beta chains |
True
|
tcell_type
|
str
|
Filter to specific T cell type ("cd8" or "cd4") |
None
|
exclude_viral
|
bool
|
Exclude clones flagged as viral |
False
|
verbose
|
bool
|
Print progress information |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Filtered clonotypes |
Source code in tcrsift/filter.py
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 | |
filter_clonotypes_logistic ¶
filter_clonotypes_logistic(clonotypes: DataFrame, fdr_tiers: list | None = None, min_freq_threshold: float = 0.09, default_freq_threshold: float = 0.5, only_avoid_viral: bool = True) -> pd.DataFrame
Filter clonotypes using logistic regression model.
This method fits a logistic model to predict clone quality based on frequency and assigns tiers based on FDR thresholds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clonotypes
|
DataFrame
|
Clonotype DataFrame with max_frequency column |
required |
fdr_tiers
|
list
|
FDR values for tier assignment (default: DEFAULT_FDR_TIERS) |
None
|
min_freq_threshold
|
float
|
Minimum frequency to consider for model fitting |
0.09
|
default_freq_threshold
|
float
|
Fallback threshold if model fitting fails |
0.5
|
only_avoid_viral
|
bool
|
If True, model target is non-viral; if False, target is single-culture specific |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Clonotypes with tier assignments and threshold information |
Source code in tcrsift/filter.py
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 | |
assign_tiers_threshold ¶
assign_tiers_threshold(clonotypes: DataFrame, tier_definitions: dict | None = None, tcell_type: str | None = None, exclude_viral: bool = False) -> pd.DataFrame
Assign quality tiers to clonotypes using threshold-based method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clonotypes
|
DataFrame
|
Clonotype DataFrame |
required |
tier_definitions
|
dict
|
Custom tier definitions (default: DEFAULT_THRESHOLD_TIERS) |
None
|
tcell_type
|
str
|
Filter to specific T cell type |
None
|
exclude_viral
|
bool
|
Exclude viral clones from all tiers |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Clonotypes with 'tier' column added |
Source code in tcrsift/filter.py
split_by_tier ¶
Split clonotypes DataFrame by tier.
Returns:
| Type | Description |
|---|---|
dict
|
Mapping from tier name to DataFrame |
Source code in tcrsift/filter.py
get_filter_summary ¶
Get summary of filtering results.
Returns:
| Type | Description |
|---|---|
dict
|
Summary statistics by tier |