Python API
lanctools exports two classes for working with local ancestry data. LancData contains the genotype and local ancestry data for a set of plink2 .pgen and .lanc files, together with efficient methods for querying this data. FlatLanc is the core data structure which stores local ancestry data in a flattened structure.
LancData owns open PLINK readers. Use it as a context manager, or call
close() when finished, especially when processing many datasets:
with LancData(plink_prefix="chr1", lanc_file="chr1.lanc") as data:
lanc = data.get_lanc(indices)
lanctools.LancData
The genotype and local ancestry data for a single chromosome/dataset.
Attributes:
| Name | Type | Description |
|---|---|---|
pgen |
PgenReader
|
A pgenlib PgenReader object. |
pvar |
PvarReader
|
A pgenlib PVarReader object. |
lanc |
FlatLanc
|
A FlatLanc object with local ancestry data. |
ancestries |
list[str]
|
An ordered list of ancestry names. The integer codes in
the .lanc file and |
plink_prefix |
str
|
The prefix for the corresponding plink2 fileset. |
Source code in src/lanctools/core.py
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 | |
__init__(plink_prefix, lanc_file, ancestries=None)
Constructs a LancData from plink2 files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plink_prefix
|
str
|
The prefix for a plink2 fileset. |
required |
lanc_file
|
str
|
The path to a .lanc file. |
required |
ancestries
|
Optional[list[str]
|
An optional list of ordered ancestry names corresponding to the .lanc file. |
None
|
Source code in src/lanctools/core.py
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 | |
close()
Release the underlying PLINK readers.
Calling close more than once is safe. Query methods cannot be used
after the readers have been closed.
Source code in src/lanctools/core.py
513 514 515 516 517 518 519 520 521 522 | |
get_geno(indices)
Query phased genotypes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
NDArray[uint32]
|
An array of variant indices (0-based) |
required |
Returns:
| Type | Description |
|---|---|
NDArray[int32]
|
An array of phased genotypes, shape (N, V, 2) |
Source code in src/lanctools/core.py
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 | |
get_info(indices)
Query info for a set of variants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
NDArray[uint32]
|
The variant indices in pvar order (0-based), shape (V,) |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: One row per variant with the following columns:
|
Source code in src/lanctools/core.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | |
get_lanc(indices)
Query phased local ancestry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
NDArray[unsignedinteger]
|
The variant indices in pvar order (0-based), shape (V,) |
required |
Returns:
| Type | Description |
|---|---|
NDArray[uint8]
|
An array of ancestries, shape (N, V, 2) |
Source code in src/lanctools/core.py
555 556 557 558 559 560 561 562 563 564 565 566 | |
get_lanc_dosage(indices)
Query local ancestry dosage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
NDArray[uint32]
|
An array of variant indices in pvar order (0-based), shape (V,) |
required |
Returns:
| Type | Description |
|---|---|
NDArray[int32]
|
An array of local ancestry dosages, shape (N, V, K) (where K is the number of ancestries) |
Source code in src/lanctools/core.py
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
get_lanc_geno(indices)
Query genotypes deconvoluted/masked by ancestry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
NDArray[unsignedinteger]
|
An array of variant indices (0-based) |
required |
Returns:
| Type | Description |
|---|---|
NDArray[int32]
|
An array of genotypes masked by ancestry, shape (N, V, 2) |
Source code in src/lanctools/core.py
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
lanctools.FlatLanc
Stores .lanc file ancestry data in a flattened structure for fast querying.
Attributes:
| Name | Type | Description |
|---|---|---|
right_haps |
NDArray[uint8]
|
Concatenated right haplotypes for all samples, shape (H,) |
left_haps |
NDArray[uint8]
|
Concatenated left haplotypes for all samples, shape (H,) |
breakpoints |
NDArray[uint32]
|
Concatenated breakpoints for all samples, shape (H,) |
offsets |
NDArray[uint32]
|
Cumulative end indices separating samples, shape (N,) |
Source code in src/lanctools/core.py
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 | |
get_lanc(indices)
Query phased local ancestry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
NDArray[unsignedinteger]
|
The variant indices in (0-based) |
required |
Returns:
| Type | Description |
|---|---|
NDArray[uint8]
|
An array of ancestries, shape (N, V, 2) |
Source code in src/lanctools/core.py
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 | |