immunum.polars
immunum.polars.number(expr, *, chains=None, scheme=None, min_confidence=None, annotator=None)
Number sequences as a Polars expression.
Each row gets the fields Annotator.number returns: chain, scheme, confidence,
numbering (a list of {position, residue} structs), query_start, query_end, error
and error_kind. On failure, error and error_kind are set and every other field is null.
Pass either chains and scheme (and optionally min_confidence), or a prebuilt
annotator; both give the same result and run as fast. Either way the names are checked
when the expression is built, so a bad one raises immunum.Error straight away.
Example:
import polars as pl
import immunum.polars as imp
df = pl.DataFrame(
{
"sequence": [
"QVQLVQSGAEVKRPGSSVTVSCKASGGSFSTYALSWVRQAPGRGLEWMGGVIPLLTITNYAPRFQGRITITADRSTSTAYLELNSLRPEDTAVYYCAREGTTGKPIGAFAHWGQGTLVTVSS",
"DIQMTQSPSSLSASVGDRVTITCRASQDVNTAVAWYQQKPGKAPKLLIYSASFLYSGVPSRFSGSRSGTDFTLTISSLQPEDFATYYCQQHYTTPPTFGQGTKVEIK",
]
}
).select(
imp.number(
"sequence",
chains=["h", "k"],
scheme="imgt",
).alias("numbering")
)
assert df.dtypes == [
pl.Struct(
{
"chain": pl.String,
"scheme": pl.String,
"confidence": pl.Float32,
"numbering": pl.List(
pl.Struct(
{
"position": pl.String,
"residue": pl.String,
}
)
),
"query_start": pl.UInt32,
"query_end": pl.UInt32,
"error": pl.String,
"error_kind": pl.String,
}
)
]
print(
df.select(
pl.col(
"numbering"
).struct.unnest()
)
)
# shape: (2, 8)
# ┌───────┬────────┬────────────┬─────────────────────────────────┬─────────────┬───────────┬───────┬────────────┐
# │ chain ┆ scheme ┆ confidence ┆ numbering ┆ query_start ┆ query_end ┆ error ┆ error_kind │
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
# │ str ┆ str ┆ f32 ┆ list[struct[2]] ┆ u32 ┆ u32 ┆ str ┆ str │
# ╞═══════╪════════╪════════════╪═════════════════════════════════╪═════════════╪═══════════╪═══════╪════════════╡
# │ H ┆ IMGT ┆ 0.784515 ┆ [{"1","Q"}, {"2","V"}, … {"128… ┆ 0 ┆ 121 ┆ null ┆ null │
# │ K ┆ IMGT ┆ 0.878814 ┆ [{"1","D"}, {"2","I"}, … {"127… ┆ 0 ┆ 106 ┆ null ┆ null │
# └───────┴────────┴────────────┴─────────────────────────────────┴─────────────┴───────────┴───────┴────────────┘
# One row per numbered residue
print(
df.select(
pl.col(
"numbering"
).struct.field(
"chain",
"numbering",
)
)
.explode("numbering")
.unnest("numbering")
.head(3)
)
# shape: (3, 3)
# ┌───────┬──────────┬─────────┐
# │ chain ┆ position ┆ residue │
# │ --- ┆ --- ┆ --- │
# │ str ┆ str ┆ str │
# ╞═══════╪══════════╪═════════╡
# │ H ┆ 1 ┆ Q │
# │ H ┆ 2 ┆ V │
# │ H ┆ 3 ┆ Q │
# └───────┴──────────┴─────────┘
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
IntoExprColumn
|
input polars expression (e.g. |
required |
chains
|
list[str] | None
|
chains to consider, as for |
None
|
scheme
|
str | None
|
numbering scheme, as for |
None
|
min_confidence
|
float | None
|
minimum alignment confidence, as for
|
None
|
annotator
|
Annotator | None
|
a prebuilt |
None
|
Returns:
| Type | Description |
|---|---|
Expr
|
A struct expression with the fields above, every one null for a null sequence. |
Raises:
| Type | Description |
|---|---|
Error
|
If |
TypeError
|
If both or neither of |
Source code in immunum/polars.py
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 | |
immunum.polars.number_domains(expr, *, chains=None, scheme=None, min_confidence=None, annotator=None)
Number every variable domain in each sequence, such as both domains of an scFv.
Each row gets a list with one struct per domain, ordered by position, each with the fields
number returns for that domain; the list is never empty. A sequence without a domain gets
a single struct with error and error_kind set and every other field null: low_confidence
when no alignment reaches min_confidence, as number reports it, or domain_too_short when
the best alignment is confident but shorter than a domain must be. So does an invalid sequence
(shorter than 30 or longer than 10,000 residues, or holding a character that isn't a letter),
with invalid_sequence.
A domain that lacks its first IMGT positions (a light chain starting at position 2, say) and directly follows other residues, such as a linker, can have the residue just before it numbered as its first position: IMGT position 1 is so variable that the sequence alone can't tell a linker residue from the domain's own first residue.
Pass either chains and scheme, or a prebuilt annotator, as for number.
Example:
import polars as pl
import immunum.polars as imp
heavy = "QVQLVQSGAEVKRPGSSVTVSCKASGGSFSTYALSWVRQAPGRGLEWMGGVIPLLTITNYAPRFQGRITITADRSTSTAYLELNSLRPEDTAVYYCAREGTTGKPIGAFAHWGQGTLVTVSS"
kappa = "DIQMTQSPSSLSASVGDRVTITCRASQDVNTAVAWYQQKPGKAPKLLIYSASFLYSGVPSRFSGSRSGTDFTLTISSLQPEDFATYYCQQHYTTPPTFGQGTKVEIK"
df = pl.DataFrame(
{
"sequence": [
heavy
+ "GGGGSGGGGSGGGGS"
+ kappa,
heavy,
]
}
).select(
imp.number_domains(
"sequence",
chains=["ig"],
scheme="imgt",
).alias("domains")
)
# One row per domain
print(
df.with_row_index("sequence")
.explode("domains")
.unnest("domains")
.select(
"sequence",
"chain",
"confidence",
"query_start",
"query_end",
)
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
IntoExprColumn
|
input polars expression (e.g. |
required |
chains
|
list[str] | None
|
chains to consider, as for |
None
|
scheme
|
str | None
|
numbering scheme, as for |
None
|
min_confidence
|
float | None
|
minimum alignment confidence, as for
|
None
|
annotator
|
Annotator | None
|
a prebuilt |
None
|
Returns:
| Type | Description |
|---|---|
Expr
|
A list-of-struct expression with one struct per domain; null for a null sequence. |
Raises:
| Type | Description |
|---|---|
Error
|
If |
TypeError
|
If both or neither of |
Source code in immunum/polars.py
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 | |
immunum.polars.segment(expr, *, chains=None, scheme=None, min_confidence=None, annotator=None)
Split sequences into FR/CDR regions as a Polars expression.
Each row gets prefix, fr1, cdr1, fr2, cdr2, fr3, cdr3, fr4, postfix,
error and error_kind. The segments join back into the sequence. On failure, error and
error_kind are set and every segment is null.
Pass either chains and scheme, or a prebuilt annotator, as for number.
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,
"error": pl.String,
"error_kind": pl.String,
}
)
print(
df.select(
pl.col(
"segmentation"
).struct.unnest()
)
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
IntoExprColumn
|
input polars expression (e.g. |
required |
chains
|
list[str] | None
|
chains to consider, as for |
None
|
scheme
|
str | None
|
numbering scheme, as for |
None
|
min_confidence
|
float | None
|
minimum alignment confidence, as for
|
None
|
annotator
|
Annotator | None
|
a prebuilt |
None
|
Returns:
| Type | Description |
|---|---|
Expr
|
A struct expression with the regions above, every field null for a null sequence. |
Raises:
| Type | Description |
|---|---|
Error
|
If |
TypeError
|
If both or neither of |
Source code in immunum/polars.py
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 | |
immunum.polars.segment_domains(expr, *, chains=None, scheme=None, min_confidence=None, annotator=None)
Split every variable domain in each sequence into FR/CDR regions.
Each row gets a list with one struct per domain, ordered by position, each with the fields
segment returns. Every residue lands in exactly one domain's regions: a domain's prefix
holds the residues since the previous domain (or the start of the sequence), and only the
last domain has the residues after it as its postfix, so all domains' regions in order
rebuild the sequence. The list is never empty: a sequence without a domain, or an invalid one,
gets a single struct with error and error_kind set and every segment null, as for
number_domains.
Pass either chains and scheme, or a prebuilt annotator, as for number.
Example:
import polars as pl
import immunum.polars as imp
heavy = "QVQLVQSGAEVKRPGSSVTVSCKASGGSFSTYALSWVRQAPGRGLEWMGGVIPLLTITNYAPRFQGRITITADRSTSTAYLELNSLRPEDTAVYYCAREGTTGKPIGAFAHWGQGTLVTVSS"
kappa = "DIQMTQSPSSLSASVGDRVTITCRASQDVNTAVAWYQQKPGKAPKLLIYSASFLYSGVPSRFSGSRSGTDFTLTISSLQPEDFATYYCQQHYTTPPTFGQGTKVEIK"
df = pl.DataFrame(
{
"sequence": [
heavy
+ "GGGGSGGGGSGGGGS"
+ kappa
]
}
).select(
imp.segment_domains(
"sequence",
chains=["ig"],
scheme="imgt",
).alias("domains")
)
# One row per domain
print(
df.explode("domains")
.unnest("domains")
.select(
"prefix",
"cdr1",
"cdr2",
"cdr3",
)
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
IntoExprColumn
|
input polars expression (e.g. |
required |
chains
|
list[str] | None
|
chains to consider, as for |
None
|
scheme
|
str | None
|
numbering scheme, as for |
None
|
min_confidence
|
float | None
|
minimum alignment confidence, as for
|
None
|
annotator
|
Annotator | None
|
a prebuilt |
None
|
Returns:
| Type | Description |
|---|---|
Expr
|
A list-of-struct expression with one struct per domain; null for a null sequence. |
Raises:
| Type | Description |
|---|---|
Error
|
If |
TypeError
|
If both or neither of |
Source code in immunum/polars.py
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 | |
immunum.polars.numbering_method(expr, *, annotator)
Deprecated: use number(expr, annotator=annotator), which returns the same.
Source code in immunum/polars.py
immunum.polars.segmentation_method(expr, *, annotator)
Deprecated: use segment(expr, annotator=annotator), which returns the same.