βοΈ 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 |
|
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
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 |
|
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
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)
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. 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. 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
|
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)
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. 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. 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
|
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)
Read a VCF file into a LazyFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the VCF file. |
required |
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'
|