Genomic operations
Range operations
Source code in polars_bio/range_op.py
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 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 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 | |
cluster(df, min_dist=0, cols=['chrom', 'start', 'end'], output_type='polars.LazyFrame', projection_pushdown=True)
staticmethod
Assign cluster IDs to overlapping or nearby genomic intervals.
Groups intervals that overlap or are within min_dist of each other
into clusters. Each row is annotated with a cluster ID and the
cluster's merged start/end boundaries.
Bioframe inspired API.
The coordinate system (0-based or 1-based) is automatically detected from DataFrame metadata set at I/O time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame. CSV with a header, BED and Parquet are supported. |
required |
min_dist
|
int
|
Minimum distance (integer) between intervals to cluster. Default is 0. |
0
|
cols
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals. |
['chrom', 'start', 'end']
|
output_type
|
str
|
Type of the output. default is "polars.LazyFrame", "polars.DataFrame", or "pandas.DataFrame" or "datafusion.DataFrame" are also supported. |
'polars.LazyFrame'
|
projection_pushdown
|
bool
|
Enable column projection pushdown. |
True
|
Returns:
| Type | Description |
|---|---|
Union[LazyFrame, DataFrame, 'pd.DataFrame', DataFrame]
|
polars.LazyFrame or polars.DataFrame or pandas.DataFrame with original |
Union[LazyFrame, DataFrame, 'pd.DataFrame', DataFrame]
|
interval columns plus |
Raises:
| Type | Description |
|---|---|
MissingCoordinateSystemError
|
If input lacks coordinate system metadata
and |
Source code in polars_bio/range_op.py
complement(df, view_df=None, cols=['chrom', 'start', 'end'], view_cols=None, output_type='polars.LazyFrame', projection_pushdown=True)
staticmethod
Compute the complement of genomic intervals — the gaps between them.
Returns intervals that represent the genomic regions not covered
by the input intervals. If view_df is provided, gaps are computed
within the boundaries of the view (e.g., chromosome sizes); otherwise
each contig spans [0, i64::MAX).
Bioframe inspired API.
The coordinate system (0-based or 1-based) is automatically detected from DataFrame metadata set at I/O time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame. CSV with a header, BED and Parquet are supported. |
required |
view_df
|
Union[DataFrame, LazyFrame, 'pd.DataFrame', None]
|
Optional DataFrame defining contig boundaries (e.g., chromosome sizes). Each row should have contig, start, end columns. |
None
|
cols
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals. |
['chrom', 'start', 'end']
|
view_cols
|
Union[list[str], None]
|
Column names for the view table. Defaults to |
None
|
output_type
|
str
|
Type of the output. default is "polars.LazyFrame", "polars.DataFrame", or "pandas.DataFrame" or "datafusion.DataFrame" are also supported. |
'polars.LazyFrame'
|
projection_pushdown
|
bool
|
Enable column projection pushdown. |
True
|
Returns:
| Type | Description |
|---|---|
Union[LazyFrame, DataFrame, 'pd.DataFrame', DataFrame]
|
polars.LazyFrame or polars.DataFrame or pandas.DataFrame of complement |
Union[LazyFrame, DataFrame, 'pd.DataFrame', DataFrame]
|
intervals (contig, start, end). |
Raises:
| Type | Description |
|---|---|
MissingCoordinateSystemError
|
If input lacks coordinate system metadata
and |
Source code in polars_bio/range_op.py
count_overlaps(df1, df2, suffixes=('', '_'), cols1=['chrom', 'start', 'end'], cols2=['chrom', 'start', 'end'], on_cols=None, output_type='polars.LazyFrame', naive_query=True, projection_pushdown=True)
staticmethod
Count pairs of overlapping genomic intervals. Bioframe inspired API.
The coordinate system (0-based or 1-based) is automatically detected from DataFrame metadata set at I/O time. Both inputs must have the same coordinate system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df1
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame or a registered table (see register_vcf). CSV with a header, BED and Parquet are supported. |
required |
df2
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame or a registered table. CSV with a header, BED and Parquet are supported. |
required |
suffixes
|
tuple[str, str]
|
Suffixes for the columns of the two overlapped sets. |
('', '_')
|
cols1
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals, provided separately for each set. |
['chrom', 'start', 'end']
|
cols2
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals, provided separately for each set. |
['chrom', 'start', 'end']
|
on_cols
|
Union[list[str], None]
|
List of additional column names to join on. default is None. |
None
|
output_type
|
str
|
Type of the output. default is "polars.LazyFrame", "polars.DataFrame", or "pandas.DataFrame" or "datafusion.DataFrame" are also supported. |
'polars.LazyFrame'
|
naive_query
|
bool
|
If True, use naive query for counting overlaps based on overlaps. |
True
|
projection_pushdown
|
bool
|
Enable column projection pushdown to optimize query performance by only reading the necessary columns at the DataFusion level. |
True
|
Returns:
| Type | Description |
|---|---|
Union[LazyFrame, DataFrame, 'pd.DataFrame', DataFrame]
|
polars.LazyFrame or polars.DataFrame or pandas.DataFrame of the overlapping intervals. |
Raises:
| Type | Description |
|---|---|
MissingCoordinateSystemError
|
If either input lacks coordinate system metadata
and |
CoordinateSystemMismatchError
|
If inputs have different coordinate systems. |
Example
import polars_bio as pb
import pandas as pd
df1 = pd.DataFrame([
['chr1', 1, 5],
['chr1', 3, 8],
['chr1', 8, 10],
['chr1', 12, 14]],
columns=['chrom', 'start', 'end']
)
df1.attrs["coordinate_system_zero_based"] = False # 1-based coordinates
df2 = pd.DataFrame(
[['chr1', 4, 8],
['chr1', 10, 11]],
columns=['chrom', 'start', 'end' ]
)
df2.attrs["coordinate_system_zero_based"] = False # 1-based coordinates
counts = pb.count_overlaps(df1, df2, output_type="pandas.DataFrame")
counts
chrom start end count
0 chr1 1 5 1
1 chr1 3 8 1
2 chr1 8 10 0
3 chr1 12 14 0
Todo
Support return_input.
Source code in polars_bio/range_op.py
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 | |
coverage(df1, df2, suffixes=('_1', '_2'), on_cols=None, cols1=['chrom', 'start', 'end'], cols2=['chrom', 'start', 'end'], output_type='polars.LazyFrame', read_options=None, projection_pushdown=True)
staticmethod
Calculate intervals coverage. Bioframe inspired API.
The coordinate system (0-based or 1-based) is automatically detected from DataFrame metadata set at I/O time. Both inputs must have the same coordinate system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df1
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame or a registered table (see register_vcf). CSV with a header, BED and Parquet are supported. |
required |
df2
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame or a registered table. CSV with a header, BED and Parquet are supported. |
required |
cols1
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals, provided separately for each set. |
['chrom', 'start', 'end']
|
cols2
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals, provided separately for each set. |
['chrom', 'start', 'end']
|
suffixes
|
tuple[str, str]
|
Suffixes for the columns of the two overlapped sets. |
('_1', '_2')
|
on_cols
|
Union[list[str], None]
|
List of additional column names to join on. default is None. |
None
|
output_type
|
str
|
Type of the output. default is "polars.LazyFrame", "polars.DataFrame", or "pandas.DataFrame" or "datafusion.DataFrame" are also supported. |
'polars.LazyFrame'
|
read_options
|
Union[ReadOptions, None]
|
Additional options for reading the input files. |
None
|
projection_pushdown
|
bool
|
Enable column projection pushdown to optimize query performance by only reading the necessary columns at the DataFusion level. |
True
|
Returns:
| Type | Description |
|---|---|
Union[LazyFrame, DataFrame, 'pd.DataFrame', DataFrame]
|
polars.LazyFrame or polars.DataFrame or pandas.DataFrame of the overlapping intervals. |
Raises:
| Type | Description |
|---|---|
MissingCoordinateSystemError
|
If either input lacks coordinate system metadata
and |
CoordinateSystemMismatchError
|
If inputs have different coordinate systems. |
Note
The default output format, i.e. LazyFrame, is recommended for large datasets as it supports output streaming and lazy evaluation. This enables efficient processing of large datasets without loading the entire output dataset into memory.
Example:
Todo
Support for on_cols.
Source code in polars_bio/range_op.py
merge(df, min_dist=0, cols=['chrom', 'start', 'end'], on_cols=None, output_type='polars.LazyFrame', projection_pushdown=True)
staticmethod
Merge overlapping intervals. It is assumed that start < end.
The coordinate system (0-based or 1-based) is automatically detected from DataFrame metadata set at I/O time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame. CSV with a header, BED and Parquet are supported. |
required |
min_dist
|
int
|
Minimum distance (integer) between intervals to merge. Default is 0. |
0
|
cols
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals, provided separately for each set. |
['chrom', 'start', 'end']
|
on_cols
|
Union[list[str], None]
|
List of additional column names for clustering. default is None. |
None
|
output_type
|
str
|
Type of the output. default is "polars.LazyFrame", "polars.DataFrame", or "pandas.DataFrame" or "datafusion.DataFrame" are also supported. |
'polars.LazyFrame'
|
projection_pushdown
|
bool
|
Enable column projection pushdown to optimize query performance by only reading the necessary columns at the DataFusion level. |
True
|
Returns:
| Type | Description |
|---|---|
Union[LazyFrame, DataFrame, 'pd.DataFrame', DataFrame]
|
polars.LazyFrame or polars.DataFrame or pandas.DataFrame of the overlapping intervals. |
Raises:
| Type | Description |
|---|---|
MissingCoordinateSystemError
|
If input lacks coordinate system metadata
and |
Example:
Todo
Support for on_cols.
Source code in polars_bio/range_op.py
nearest(df1, df2, suffixes=('_1', '_2'), on_cols=None, cols1=['chrom', 'start', 'end'], cols2=['chrom', 'start', 'end'], k=1, overlap=True, distance=True, output_type='polars.LazyFrame', read_options=None, projection_pushdown=True)
staticmethod
Find pairs of closest genomic intervals. Bioframe inspired API.
The coordinate system (0-based or 1-based) is automatically detected from DataFrame metadata set at I/O time. Both inputs must have the same coordinate system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df1
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame or a registered table (see register_vcf). CSV with a header, BED and Parquet are supported. |
required |
df2
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame or a registered table. CSV with a header, BED and Parquet are supported. |
required |
cols1
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals, provided separately for each set. |
['chrom', 'start', 'end']
|
cols2
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals, provided separately for each set. |
['chrom', 'start', 'end']
|
suffixes
|
tuple[str, str]
|
Suffixes for the columns of the two overlapped sets. |
('_1', '_2')
|
on_cols
|
Union[list[str], None]
|
List of additional column names to join on. default is None. |
None
|
k
|
int
|
Number of nearest neighbors to return per query interval. Default is 1. |
1
|
overlap
|
bool
|
If True (default), include overlapping intervals in results. If False, only return non-overlapping nearest neighbors. |
True
|
distance
|
bool
|
If True (default), include a |
True
|
output_type
|
str
|
Type of the output. default is "polars.LazyFrame", "polars.DataFrame", or "pandas.DataFrame" or "datafusion.DataFrame" are also supported. |
'polars.LazyFrame'
|
read_options
|
Union[ReadOptions, None]
|
Additional options for reading the input files. |
None
|
projection_pushdown
|
bool
|
Enable column projection pushdown to optimize query performance by only reading the necessary columns at the DataFusion level. |
True
|
Returns:
| Type | Description |
|---|---|
Union[LazyFrame, DataFrame, 'pd.DataFrame', DataFrame]
|
polars.LazyFrame or polars.DataFrame or pandas.DataFrame of the overlapping intervals. |
Raises:
| Type | Description |
|---|---|
MissingCoordinateSystemError
|
If either input lacks coordinate system metadata
and |
CoordinateSystemMismatchError
|
If inputs have different coordinate systems. |
Note
The default output format, i.e. LazyFrame, is recommended for large datasets as it supports output streaming and lazy evaluation. This enables efficient processing of large datasets without loading the entire output dataset into memory.
Example:
Todo
Support for on_cols.
Source code in polars_bio/range_op.py
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 | |
overlap(df1, df2, suffixes=('_1', '_2'), on_cols=None, cols1=['chrom', 'start', 'end'], cols2=['chrom', 'start', 'end'], algorithm='Coitrees', low_memory=False, overlap_output='join', distinct_output=False, output_type='polars.LazyFrame', read_options1=None, read_options2=None, projection_pushdown=True)
staticmethod
Find pairs of overlapping genomic intervals. Bioframe inspired API.
The coordinate system (0-based or 1-based) is automatically detected from DataFrame metadata set at I/O time. Both inputs must have the same coordinate system.
Output modes (overlap_output, distinct_output):
overlap_output="join"(default): returns joined pairs, suffixing columns from both inputs. This is the historical behavior.overlap_output="left": returns only rows fromdf1that overlap at least one row indf2, keepingdf1columns with their original names. By default it preserves overlap multiplicity — onedf1row for each matchingdf2row.overlap_output="left", distinct_output=True: returns each overlappingdf1row once by row identity. Duplicatedf1rows are preserved by identity; this does not applyDISTINCTover projected values.
pb.overlap(df1, df2, overlap_output="join") # joined pairs (default)
pb.overlap(df1, df2, overlap_output="left") # df1 rows overlapping df2
pb.overlap(df1, df2, overlap_output="left", distinct_output=True) # each df1 row once
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df1
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame or a registered table (see register_vcf). CSV with a header, BED and Parquet are supported. |
required |
df2
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame or a registered table. CSV with a header, BED and Parquet are supported. |
required |
cols1
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals, provided separately for each set. |
['chrom', 'start', 'end']
|
cols2
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals, provided separately for each set. |
['chrom', 'start', 'end']
|
suffixes
|
tuple[str, str]
|
Suffixes for the columns of the two overlapped sets. |
('_1', '_2')
|
on_cols
|
Union[list[str], None]
|
List of additional column names to join on. default is None. |
None
|
algorithm
|
str
|
The algorithm to use for the overlap operation. Available options: Coitrees, IntervalTree, ArrayIntervalTree, Lapper, SuperIntervals |
'Coitrees'
|
low_memory
|
bool
|
If True, use low memory method for output generation. This caps the output batch size, trading some performance for significantly lower peak memory consumption. Recommended for operations that produce very large result sets. |
False
|
overlap_output
|
Literal['join', 'left']
|
Output shape for overlap. "join" returns the current joined df1/df2 rows with suffixes. "left" returns only df1 rows that overlap at least one df2 row, preserving duplicate df1 rows and original column names. |
'join'
|
distinct_output
|
bool
|
When overlap_output="left", True returns each overlapping df1 row once by row identity. False returns one df1 row per matching df2 row. |
False
|
output_type
|
str
|
Type of the output. default is "polars.LazyFrame", "polars.DataFrame", or "pandas.DataFrame" or "datafusion.DataFrame" are also supported. |
'polars.LazyFrame'
|
read_options1
|
Union[ReadOptions, None]
|
Additional options for reading the input files. |
None
|
read_options2
|
Union[ReadOptions, None]
|
Additional options for reading the input files. |
None
|
projection_pushdown
|
bool
|
Enable column projection pushdown to optimize query performance by only reading the necessary columns at the DataFusion level. |
True
|
Returns:
| Type | Description |
|---|---|
Union[LazyFrame, DataFrame, 'pd.DataFrame', DataFrame]
|
polars.LazyFrame or polars.DataFrame or pandas.DataFrame of the overlapping intervals. |
Raises:
| Type | Description |
|---|---|
MissingCoordinateSystemError
|
If either input lacks coordinate system metadata
and |
CoordinateSystemMismatchError
|
If inputs have different coordinate systems. |
Note
- The default output format, i.e. LazyFrame, is recommended for large datasets as it supports output streaming and lazy evaluation. This enables efficient processing of large datasets without loading the entire output dataset into memory.
- Streaming is only supported for polars.LazyFrame output.
Example
import polars_bio as pb
import pandas as pd
df1 = pd.DataFrame([
['chr1', 1, 5],
['chr1', 3, 8],
['chr1', 8, 10],
['chr1', 12, 14]],
columns=['chrom', 'start', 'end']
)
df1.attrs["coordinate_system_zero_based"] = False # 1-based coordinates
df2 = pd.DataFrame(
[['chr1', 4, 8],
['chr1', 10, 11]],
columns=['chrom', 'start', 'end' ]
)
df2.attrs["coordinate_system_zero_based"] = False # 1-based coordinates
overlapping_intervals = pb.overlap(df1, df2, output_type="pandas.DataFrame")
overlapping_intervals
chrom_1 start_1 end_1 chrom_2 start_2 end_2
0 chr1 1 5 chr1 4 8
1 chr1 3 8 chr1 4 8
Todo
Support for on_cols.
Source code in polars_bio/range_op.py
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 | |
subtract(df1, df2, cols1=['chrom', 'start', 'end'], cols2=['chrom', 'start', 'end'], output_type='polars.LazyFrame', projection_pushdown=True)
staticmethod
Subtract the second set of intervals from the first.
For each interval in df1, removes any portion that overlaps with
intervals in df2. The result contains the remaining fragments.
Bioframe inspired API.
The coordinate system (0-based or 1-based) is automatically detected from DataFrame metadata set at I/O time. Both inputs must have the same coordinate system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df1
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame. CSV with a header, BED and Parquet are supported. |
required |
df2
|
Union[str, DataFrame, LazyFrame, 'pd.DataFrame']
|
Can be a path to a file, a polars DataFrame, or a pandas DataFrame. CSV with a header, BED and Parquet are supported. |
required |
cols1
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals for the first set. |
['chrom', 'start', 'end']
|
cols2
|
Union[list[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals for the second set. |
['chrom', 'start', 'end']
|
output_type
|
str
|
Type of the output. default is "polars.LazyFrame", "polars.DataFrame", or "pandas.DataFrame" or "datafusion.DataFrame" are also supported. |
'polars.LazyFrame'
|
projection_pushdown
|
bool
|
Enable column projection pushdown. |
True
|
Returns:
| Type | Description |
|---|---|
Union[LazyFrame, DataFrame, 'pd.DataFrame', DataFrame]
|
polars.LazyFrame or polars.DataFrame or pandas.DataFrame of the |
Union[LazyFrame, DataFrame, 'pd.DataFrame', DataFrame]
|
remaining interval fragments (contig, start, end). |
Raises:
| Type | Description |
|---|---|
MissingCoordinateSystemError
|
If either input lacks coordinate system metadata
and |
CoordinateSystemMismatchError
|
If inputs have different coordinate systems. |
Source code in polars_bio/range_op.py
Pileup
Per-base read depth (pileup) operations on alignment files.
Computes per-position depth from BAM/SAM/CRAM files by walking CIGAR operations, producing mosdepth-compatible coverage blocks.
Source code in polars_bio/pileup_op.py
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 | |
depth(path, filter_flag=1796, min_mapping_quality=0, binary_cigar=True, dense_mode='auto', use_zero_based=None, per_base=False, output_type='polars.LazyFrame')
staticmethod
Compute per-base read depth (pileup) from a BAM/SAM/CRAM file.
Walks CIGAR operations to produce coverage blocks -- similar to mosdepth / samtools depth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to alignment file (.bam, .sam, or .cram). Index files (BAI/CSI/CRAI) are auto-discovered. |
required |
filter_flag
|
int
|
SAM flag mask -- reads with any of these flags are excluded. Default 1796 (unmapped, secondary, failed QC, duplicate). |
1796
|
min_mapping_quality
|
int
|
Minimum MAPQ threshold. Default 0 (no filter). |
0
|
binary_cigar
|
bool
|
Use binary CIGAR parsing (faster). Default True. |
True
|
dense_mode
|
str
|
Accumulation strategy:
|
'auto'
|
use_zero_based
|
Optional[bool]
|
Coordinate system for output positions.
|
None
|
per_base
|
bool
|
If True, emit one row per genomic position (like
|
False
|
output_type
|
str
|
One of |
'polars.LazyFrame'
|
Returns:
| Type | Description |
|---|---|
Union[LazyFrame, DataFrame, DataFrame]
|
DataFrame with columns depending on |
Union[LazyFrame, DataFrame, DataFrame]
|
|
Union[LazyFrame, DataFrame, DataFrame]
|
|
Example
import polars_bio as pb
# Basic depth computation (RLE blocks)
df = pb.depth("alignments.bam").collect()
# Per-base output (one row per position)
df = pb.depth("alignments.bam", per_base=True).collect()
# With MAPQ filter
df = pb.depth("alignments.bam", min_mapping_quality=20).collect()
# As pandas DataFrame
pdf = pb.depth("alignments.bam", output_type="pandas.DataFrame")
Source code in polars_bio/pileup_op.py
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 | |
FastQC
FastQC quality-control operations on FASTQ files.
Source code in polars_bio/fastqc_op.py
fastqc(path, modules=None, group=True)
staticmethod
Compute FastQC modules over a FASTQ file in one streaming pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to a FASTQ file (plain, .gz, or .bgz). |
required |
modules
|
Optional[List[str]]
|
Module names to compute; |
None
|
group
|
bool
|
Reserved for FastQC-style position binning of long reads
( |
True
|
Returns:
| Type | Description |
|---|---|
FastQCResult
|
FastQCResult with |
FastQCResult
|
|