immunum.polars
immunum.polars.number(expr, *, chains, scheme, min_confidence=None)
Segment a polars expr with immunum.
Annotator object will be initialized with chains, scheme, min_confidence.
Results are returned as Struct({'chain': String, 'scheme': String, 'positions': List(String), 'residues': List(String)})
Example:
import polars as pl
import immunum.polars as imp
df = pl.DataFrame(
{
"sequence": [
"QVQLVQSGAEVKRPGSSVTVSCKASGGSFSTYALSWVRQAPGRGLEWMGGVIPLLTITNYAPRFQGRITITADRSTSTAYLELNSLRPEDTAVYYCAREGTTGKPIGAFAHWGQGTLVTVSS",
"DIQMTQSPSSLSASVGDRVTITCRASQDVNTAVAWYQQKPGKAPKLLIYSASFLYSGVPSRFSGSRSGTDFTLTISSLQPEDFATYYCQQHYTTPPTFGQGTKVEIK",
]
}
).select(
imp.number(
"sequence",
chains=["h"],
scheme="imgt",
min_confidence=0.0,
).alias("numbering")
)
assert df.dtypes == [
pl.Struct(
{
"chain": pl.String,
"scheme": pl.String,
"positions": pl.List(
pl.String
),
"residues": pl.List(
pl.String
),
}
)
]
print(
df.select(
pl.col(
"numbering"
).struct.unnest()
)
)
# shape: (2, 4)
# ┌───────┬────────┬─────────────────────┬───────────────────┐
# │ chain ┆ scheme ┆ positions ┆ residues │
# │ --- ┆ --- ┆ --- ┆ --- │
# │ str ┆ str ┆ list[str] ┆ list[str] │
# ╞═══════╪════════╪═════════════════════╪═══════════════════╡
# │ H ┆ IMGT ┆ ["1", "2", … "128"] ┆ ["Q", "V", … "S"] │
# │ H ┆ IMGT ┆ ["1", "2", … "127"] ┆ ["D", "I", … "K"] │
# └───────┴────────┴─────────────────────┴───────────────────┘
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
IntoExprColumn
|
input polars expression (e.g. |
required |
chains
|
list[str]
|
list of chains to use for initialized |
required |
scheme
|
str
|
scheme to use for initialized |
required |
min_confidence
|
float | None
|
confidence to use for initialized |
None
|
Returns:
| Type | Description |
|---|---|
Expr
|
pl.Expr: numbering expression |
Source code in immunum/polars.py
immunum.polars.segment(expr, *, chains, scheme, min_confidence=None)
Split sequences into FR/CDR regions as a Polars expression.
Annotator object will be initialized with chains, scheme, min_confidence.
Results are returned as Struct({'fr1': String, 'cdr1': String, 'fr2': String, 'cdr2': String, 'fr3': String, 'cdr3': String, 'fr4': String, 'prefix': String, 'postfix': String}).
Example:
import polars as pl
import immunum.polars as imp
df = pl.DataFrame(
{
"sequence": [
"QVQLVQSGAEVKRPGSSVTVSCKASGGSFSTYALSWVRQAPGRGLEWMGGVIPLLTITNYAPRFQGRITITADRSTSTAYLELNSLRPEDTAVYYCAREGTTGKPIGAFAHWGQGTLVTVSS",
"DIQMTQSPSSLSASVGDRVTITCRASQDVNTAVAWYQQKPGKAPKLLIYSASFLYSGVPSRFSGSRSGTDFTLTISSLQPEDFATYYCQQHYTTPPTFGQGTKVEIK",
]
}
).select(
imp.segment(
"sequence",
chains=["h", "k", "l"],
scheme="imgt",
min_confidence=0.0,
).alias("segmentation")
)
assert df[
"segmentation"
].dtype == pl.Struct(
{
"prefix": pl.String,
"fr1": pl.String,
"cdr1": pl.String,
"fr2": pl.String,
"cdr2": pl.String,
"fr3": pl.String,
"cdr3": pl.String,
"fr4": pl.String,
"postfix": pl.String,
}
)
print(
df.select(
pl.col(
"segmentation"
).struct.unnest()
)
)
# shape: (2, 9)
# ┌────────────────────────┬──────────┬───┬─────────────┬────────┬─────────┐
# │ fr1 ┆ cdr1 ┆ … ┆ fr4 ┆ prefix ┆ postfix │
# │ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- │
# │ str ┆ str ┆ ┆ str ┆ str ┆ str │
# ╞════════════════════════╪══════════╪═══╪═════════════╪════════╪═════════╡
# │ QVQLVQSGAEVKRPGSSVTVS… ┆ GGSFSTYA ┆ … ┆ WGQGTLVTVSS ┆ ┆ │
# │ DIQMTQSPSSLSASVGDRVTI… ┆ RASQDVNT ┆ … ┆ FGQGTKVEIK ┆ ┆ │
# └────────────────────────┴──────────┴───┴─────────────┴────────┴─────────┘
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
IntoExprColumn
|
input polars expression (e.g. |
required |
chains
|
list[str]
|
list of chains to use for initialized |
required |
scheme
|
str
|
scheme to use for initialized |
required |
min_confidence
|
float | None
|
confidence to use for initialized |
None
|
Returns:
| Type | Description |
|---|---|
Expr
|
pl.Expr: segmentation expression |
Source code in immunum/polars.py
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 | |
immunum.polars.numbering_method(expr, *, annotator)
Number sequences using a pre-built Annotator instance.
Prefer this over number when reusing the same annotator across multiple expressions,
as it avoids re-initializing the annotator on each call. Returns the same struct shape
as number.
Example:
import polars as pl
import immunum
import immunum.polars as imp
annotator = immunum.Annotator(
chains=["H", "K", "L"],
scheme="imgt",
)
df = pl.DataFrame(
{
"sequence": [
"QVQLVQSGAEVKRPGSSVTVSCKASGGSFSTYALSWVRQAPGRGLEWMGGVIPLLTITNYAPRFQGRITITADRSTSTAYLELNSLRPEDTAVYYCAREGTTGKPIGAFAHWGQGTLVTVSS",
]
}
).select(
imp.numbering_method(
pl.col("sequence"),
annotator=annotator,
).alias("numbering")
)
assert set(
df["numbering"].struct.fields
) == {
"chain",
"scheme",
"confidence",
"numbering",
}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
IntoExprColumn
|
input polars expression (e.g. |
required |
annotator
|
Annotator
|
pre-built |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
pl.Expr: numbering expression |
Source code in immunum/polars.py
immunum.polars.segmentation_method(expr, *, annotator)
Segment sequences using a pre-built Annotator instance.
Prefer this over segment when reusing the same annotator across multiple expressions,
as it avoids re-initializing the annotator on each call. Returns the same struct shape
as segment.
Example:
import polars as pl
import immunum
import immunum.polars as imp
annotator = immunum.Annotator(
chains=["H", "K", "L"],
scheme="imgt",
)
df = pl.DataFrame(
{
"sequence": [
"QVQLVQSGAEVKRPGSSVTVSCKASGGSFSTYALSWVRQAPGRGLEWMGGVIPLLTITNYAPRFQGRITITADRSTSTAYLELNSLRPEDTAVYYCAREGTTGKPIGAFAHWGQGTLVTVSS",
"DIQMTQSPSSLSASVGDRVTITCRASQDVNTAVAWYQQKPGKAPKLLIYSASFLYSGVPSRFSGSRSGTDFTLTISSLQPEDFATYYCQQHYTTPPTFGQGTKVEIK",
]
}
).select(
imp.segmentation_method(
"sequence", annotator=annotator
).alias("segmentation")
)
assert df[
"segmentation"
].dtype == pl.Struct(
{
"prefix": pl.String,
"fr1": pl.String,
"cdr1": pl.String,
"fr2": pl.String,
"cdr2": pl.String,
"fr3": pl.String,
"cdr3": pl.String,
"fr4": pl.String,
"postfix": pl.String,
}
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
IntoExprColumn
|
input polars expression (e.g. |
required |
annotator
|
Annotator
|
pre-built |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
pl.Expr: segmentation expression |