Auxiliary
Metadata
get_metadata(df)
Get all metadata attached to a DataFrame or LazyFrame.
Returns all metadata including: - Source file information (format, path) - Format-specific metadata (VCF INFO/FORMAT fields, FASTQ quality encoding, etc.) - Comprehensive Arrow schema metadata (if available)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
Polars DataFrame or LazyFrame (or Pandas DataFrame) |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict with keys: |
dict
|
|
dict
|
|
dict
|
|
dict
|
|
Examples:
Get all metadata from a VCF file:
Access basic metadata:
meta["format"] # Returns: 'vcf'
meta["path"] # Returns: 'file.vcf'
meta["coordinate_system_zero_based"] # Returns: False (1-based for VCF)
Access VCF-specific metadata:
info_fields = meta["header"]["info_fields"]
format_fields = meta["header"]["format_fields"]
sample_names = meta["header"]["sample_names"]
version = meta["header"]["version"]
contigs = meta["header"]["contigs"]
Source code in polars_bio/_metadata.py
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 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | |
print_metadata_json(df, indent=2)
Print metadata as pretty-formatted JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
Union[DataFrame, LazyFrame]
|
Polars DataFrame or LazyFrame |
required |
indent
|
int
|
Number of spaces for indentation (default: 2) |
2
|
Source code in polars_bio/_metadata.py
print_metadata_summary(df)
Print a human-readable summary of all metadata.
Displays a formatted summary of all metadata attached to a DataFrame or LazyFrame, including format, path, coordinate system, and format-specific information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
Union[DataFrame, LazyFrame]
|
Polars DataFrame or LazyFrame |
required |
Source code in polars_bio/_metadata.py
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | |
set_source_metadata(df, format, path='', header=None)
Set standardized source file metadata.
Stores metadata about the source file format, path, and format-specific header information. This standardized approach works across all file formats (VCF, FASTQ, BAM, GFF, BED, FASTA, CRAM).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
Polars DataFrame or LazyFrame (or Pandas DataFrame) |
required | |
format
|
str
|
File format identifier (e.g., "vcf", "fastq", "bam") |
required |
path
|
str
|
Original file path (default: "") |
''
|
header
|
dict
|
Format-specific header data as dict (default: None) For VCF: {"info_fields": {...}, "format_fields": {...}, "sample_names": [...], ...} For other formats: format-specific metadata |
None
|
Example
Source code in polars_bio/_metadata.py
Session options & logging
set_option(key, value)
Set a configuration option.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
The configuration key. |
required | |
value
|
The value to set (bool values are converted to "true"/"false"). |
required |
Source code in polars_bio/context.py
get_option(key)
Get the value of a configuration option.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
The configuration key. |
required |
Returns:
| Type | Description |
|---|---|
|
The current value of the option as a string, or None if not set. |
Source code in polars_bio/context.py
set_loglevel(level)
Set the log level for the logger and root logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
The log level to set. Can be "debug", "info", "warn", or "warning". |
required |
Note
The log level should be set as a first step after importing the library. Once set it can be only decreased, not increased. In order to increase the log level, you need to restart the Python session.
Example:
Source code in polars_bio/logging.py
Errors
MissingCoordinateSystemError
Bases: Exception
Raised when a DataFrame lacks coordinate system metadata.
Range operations require coordinate system metadata to determine the correct interval semantics. This error is raised when:
- A Polars LazyFrame/DataFrame lacks polars-config-meta metadata
- A Pandas DataFrame lacks df.attrs["coordinate_system_zero_based"]
- A file path registers a table without Arrow schema metadata
For Polars DataFrames, use polars-bio I/O functions (scan_, read_) which automatically set the metadata.
For Pandas DataFrames, set the attribute before passing to range operations:
Example
import pandas as pd
import polars_bio as pb
pdf = pd.read_csv("intervals.bed", sep=" ", names=["chrom", "start", "end"])
pb.overlap(pdf, pdf) # Raises MissingCoordinateSystemError
# Fix: set the coordinate system metadata
pdf.attrs["coordinate_system_zero_based"] = True
pb.overlap(pdf, pdf) # Works correctly
Source code in polars_bio/exceptions.py
CoordinateSystemMismatchError
Bases: Exception
Raised when two DataFrames have different coordinate systems.
This error occurs when attempting range operations (overlap, nearest, etc.) on DataFrames where one uses 0-based coordinates and the other uses 1-based coordinates.