βοΈ API reference
API structure
There are 2 ways of using polars-bio API:
- directly on a Polars LazyFrame under a registered
pb
namespace
Example
- using
polars_bio
module
Example
Tip
- Not all are available in both ways.
- You can of course use both ways in the same script.
LazyFrame
Source code in polars_bio/polars_ext.py
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 |
|
count_overlaps(other_df, overlap_filter=FilterOp.Strict, suffixes=('', '_'), cols1=['chrom', 'start', 'end'], cols2=['chrom', 'start', 'end'], on_cols=None, naive_query=True)
Note
Alias for count_overlaps
Source code in polars_bio/polars_ext.py
expand(pad=None, scale=None, side='both', cols=['chrom', 'start', 'end'])
Expand each interval by an amount specified with pad
.
Note
Adapted to Polars API from bioframe.expand
Negative values for pad shrink the interval, up to the midpoint. Multiplicative rescaling of intervals enabled with scale. Only one of pad or scale can be provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pad
|
The amount by which the intervals are additively expanded on each side.
Negative values for pad shrink intervals, but not beyond the interval
midpoint. Either |
None
|
|
scale
|
The factor by which to scale intervals multiplicatively on each side, e.g
|
None
|
|
side
|
Which side to expand, possible values are 'left', 'right' and 'both'. Default 'both'. |
'both'
|
|
cols
|
The names of columns containing the chromosome, start and end of the genomic intervals. Default values are 'chrom', 'start', 'end'. |
['chrom', 'start', 'end']
|
Source code in polars_bio/polars_ext.py
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 |
|
merge(overlap_filter=FilterOp.Strict, min_dist=0, cols=None)
Note
Alias for merge
Source code in polars_bio/polars_ext.py
nearest(other_df, suffixes=('_1', '_2'), overlap_filter=FilterOp.Strict, cols1=['chrom', 'start', 'end'], cols2=['chrom', 'start', 'end'])
Note
Alias for nearest
Source code in polars_bio/polars_ext.py
overlap(other_df, suffixes=('_1', '_2'), how='inner', overlap_filter=FilterOp.Strict, cols1=['chrom', 'start', 'end'], cols2=['chrom', 'start', 'end'])
Note
Alias for overlap
Source code in polars_bio/polars_ext.py
sort(cols=['chrom', 'start', 'end'])
Sort a bedframe.
Note
Adapted to Polars API from bioframe.sort_bedframe
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cols
|
Union[tuple[str], None]
|
The names of columns containing the chromosome, start and end of the genomic intervals. |
['chrom', 'start', 'end']
|
Example
import polars_bio as pb
df = pb.read_table("https://www.encodeproject.org/files/ENCFF001XKR/@@download/ENCFF001XKR.bed.gz",schema="bed9")
df.pb.sort().limit(5).collect()
<class 'builtins.PyExpr'>
shape: (5, 9)
βββββββββ¬ββββββββββ¬ββββββββββ¬βββββββ¬ββββ¬βββββββββ¬βββββββββββββ¬βββββββββββ¬βββββββββββ
β chrom β start β end β name β β¦ β strand β thickStart β thickEnd β itemRgb β
β --- β --- β --- β --- β β --- β --- β --- β --- β
β str β i64 β i64 β str β β str β str β str β str β
βββββββββͺββββββββββͺββββββββββͺβββββββͺββββͺβββββββββͺβββββββββββββͺβββββββββββͺβββββββββββ‘
β chr1 β 193500 β 194500 β . β β¦ β + β . β . β 179,45,0 β
β chr1 β 618500 β 619500 β . β β¦ β + β . β . β 179,45,0 β
β chr1 β 974500 β 975500 β . β β¦ β + β . β . β 179,45,0 β
β chr1 β 1301500 β 1302500 β . β β¦ β + β . β . β 179,45,0 β
β chr1 β 1479500 β 1480500 β . β β¦ β + β . β . β 179,45,0 β
βββββββββ΄ββββββββββ΄ββββββββββ΄βββββββ΄ββββ΄βββββββββ΄βββββββββββββ΄βββββββββββ΄βββββββββββ
Source code in polars_bio/polars_ext.py
count_overlaps(df1, df2, overlap_filter=FilterOp.Strict, suffixes=('', '_'), cols1=['chrom', 'start', 'end'], cols2=['chrom', 'start', 'end'], on_cols=None, output_type='polars.LazyFrame', streaming=False, naive_query=True)
Count pairs of overlapping genomic intervals. Bioframe inspired API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df1
|
Union[str, DataFrame, LazyFrame, 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, 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 |
overlap_filter
|
FilterOp
|
FilterOp, optional. The type of overlap to consider(Weak or Strict). |
Strict
|
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" are also supported. |
'polars.LazyFrame'
|
naive_query
|
bool
|
If True, use naive query for counting overlaps based on overlaps. |
True
|
streaming
|
bool
|
EXPERIMENTAL If True, use Polars streaming engine. |
False
|
Returns: polars.LazyFrame or polars.DataFrame or pandas.DataFrame of the overlapping intervals.
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']
)
df2 = pd.DataFrame(
[['chr1', 4, 8],
['chr1', 10, 11]],
columns=['chrom', 'start', 'end' ]
)
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
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 |
|
merge(df, overlap_filter=FilterOp.Strict, min_dist=0, cols=['chrom', 'start', 'end'], on_cols=None, output_type='polars.LazyFrame', streaming=False)
Merge overlapping intervals. It is assumed that start < end.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
Union[str, DataFrame, LazyFrame, 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 |
overlap_filter
|
FilterOp
|
FilterOp, optional. The type of overlap to consider(Weak or Strict). |
Strict
|
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" are also supported. |
'polars.LazyFrame'
|
streaming
|
bool
|
EXPERIMENTAL If True, use Polars streaming engine. |
False
|
Returns:
Type | Description |
---|---|
Union[LazyFrame, DataFrame, DataFrame]
|
polars.LazyFrame or polars.DataFrame or pandas.DataFrame of the overlapping intervals. |
Example:
Todo
Support for on_cols.
Source code in polars_bio/range_op.py
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 |
|
nearest(df1, df2, overlap_filter=FilterOp.Strict, suffixes=('_1', '_2'), on_cols=None, cols1=['chrom', 'start', 'end'], cols2=['chrom', 'start', 'end'], output_type='polars.LazyFrame', streaming=False, read_options=None)
Find pairs of overlapping genomic intervals. Bioframe inspired API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df1
|
Union[str, DataFrame, LazyFrame, 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, 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 |
overlap_filter
|
FilterOp
|
FilterOp, optional. The type of overlap to consider(Weak or Strict). |
Strict
|
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" are also supported. |
'polars.LazyFrame'
|
streaming
|
bool
|
EXPERIMENTAL If True, use Polars streaming engine. |
False
|
read_options
|
Union[ReadOptions, None]
|
Additional options for reading the input files. |
None
|
Returns:
Type | Description |
---|---|
Union[LazyFrame, DataFrame, DataFrame]
|
polars.LazyFrame or polars.DataFrame or pandas.DataFrame of the overlapping intervals. |
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
overlap(df1, df2, how='inner', overlap_filter=FilterOp.Strict, suffixes=('_1', '_2'), on_cols=None, cols1=['chrom', 'start', 'end'], cols2=['chrom', 'start', 'end'], algorithm='Coitrees', output_type='polars.LazyFrame', streaming=False, read_options1=None, read_options2=None)
Find pairs of overlapping genomic intervals. Bioframe inspired API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df1
|
Union[str, DataFrame, LazyFrame, 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, 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 |
how
|
str
|
How to handle the overlaps on the two dataframes. inner: use intersection of the set of intervals from df1 and df2, optional. |
'inner'
|
overlap_filter
|
FilterOp
|
FilterOp, optional. The type of overlap to consider(Weak or Strict). |
Strict
|
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. |
'Coitrees'
|
output_type
|
str
|
Type of the output. default is "polars.LazyFrame", "polars.DataFrame", or "pandas.DataFrame" are also supported. |
'polars.LazyFrame'
|
streaming
|
bool
|
EXPERIMENTAL If True, use Polars streaming engine. |
False
|
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
|
Returns:
Type | Description |
---|---|
Union[LazyFrame, DataFrame, DataFrame]
|
polars.LazyFrame or polars.DataFrame or pandas.DataFrame of the overlapping intervals. |
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']
)
df2 = pd.DataFrame(
[['chr1', 4, 8],
['chr1', 10, 11]],
columns=['chrom', 'start', 'end' ]
)
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
read_bam(path)
Read a BAM file into a LazyFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the BAM file. |
required |
read_fasta(path)
Read a FASTA file into a LazyFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the FASTA file. |
required |
read_fastq(path)
Read a FASTQ file into a LazyFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the FASTQ file. |
required |
read_table(path, schema=None, **kwargs)
Read a tab-delimited (i.e. BED) file into a Polars LazyFrame. Tries to be compatible with Bioframe's read_table but faster and lazy. Schema should follow the Bioframe's schema format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the file. |
required |
schema
|
Dict
|
Schema should follow the Bioframe's schema format. |
None
|
Source code in polars_bio/io.py
read_vcf(path, info_fields=None, thread_num=1, streaming=False)
Read a VCF file into a LazyFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the VCF file. |
required |
info_fields
|
Union[list[str], None]
|
The fields to read from the INFO column. |
None
|
thread_num
|
int
|
The number of threads to use for reading the VCF file. Used only for parallel decompression of BGZF blocks. Works only for local files. |
1
|
streaming
|
bool
|
Whether to read the VCF file in streaming mode. |
False
|
Source code in polars_bio/io.py
register_vcf(path, name=None, info_fields=None, thread_num=1)
Register a VCF file as a Datafusion table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the VCF file. |
required |
name
|
Union[str, None]
|
The name of the table. If None, the name of the table will be generated automatically based on the path. |
None
|
info_fields
|
Union[list[str], None]
|
The fields to read from the INFO column. |
None
|
thread_num
|
int
|
The number of threads to use for reading the VCF file. Used only for parallel decompression of BGZF blocks. Works only for local files. |
1
|
Example
Source code in polars_bio/io.py
sql(query, streaming=False)
Execute a SQL query on the registered tables.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
str
|
The SQL query. |
required |
streaming
|
bool
|
Whether to execute the query in streaming mode. |
False
|
Example
Source code in polars_bio/io.py
visualize_intervals(df, label='overlapping pair')
Visualize the overlapping intervals.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
Union[DataFrame, DataFrame]
|
Pandas DataFrame or Polars DataFrame. The DataFrame containing the overlapping intervals |
required |
label
|
str
|
TBD |
'overlapping pair'
|