graphld.io¶
Core I/O functions for loading LDGMs and working with variant data.
For end-to-end loading and merge examples, see the I/O and Merging guide.
io
¶
Input/output operations for LDGM files.
load_ldgm
¶
load_ldgm(filepath: str, snplist_path: Optional[str] = None, population: Optional[str] = 'EUR', snps_only: bool = False) -> Union['PrecisionOperator', List['PrecisionOperator']]
Load an LDGM from a single LD block's edgelist and snplist files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the .edgelist file or directory containing it |
required |
snplist_path
|
Optional[str]
|
Optional path to .snplist file or directory. If None, uses filepath |
None
|
population
|
Optional[str]
|
Optional population name to filter files and set allele frequency column. Defaults to "EUR" |
'EUR'
|
snps_only
|
bool
|
Import snplist data for SNPs only (smaller memory usage) |
False
|
Returns:
| Type | Description |
|---|---|
Union['PrecisionOperator', List['PrecisionOperator']]
|
If filepath is a directory: List of PrecisionOperator instances, one for each edgelist file |
Union['PrecisionOperator', List['PrecisionOperator']]
|
If filepath is a file: Single PrecisionOperator instance with loaded precision matrix and variant info |
Source code in src/graphld/io.py
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 | |
merge_alleles
¶
merge_alleles(anc_alleles: Series, deriv_alleles: Series, ref_alleles: Series, alt_alleles: Series) -> pl.Series
Compare alleles between two sources and return phase information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anc_alleles
|
Series
|
Ancestral alleles from PrecisionOperator |
required |
deriv_alleles
|
Series
|
Derived alleles from PrecisionOperator |
required |
ref_alleles
|
Series
|
Reference alleles from summary statistics |
required |
alt_alleles
|
Series
|
Alternative alleles from summary statistics |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Series of integers indicating phase, where 1 means alleles match exactly, |
Series
|
-1 means alleles match but are swapped, and 0 means alleles do not match. |
Source code in src/graphld/io.py
merge_snplists
¶
merge_snplists(precision_op: 'PrecisionOperator', sumstats: DataFrame, *, variant_id_col: str = 'SNP', ref_allele_col: str = 'REF', alt_allele_col: str = 'ALT', match_by_position: bool = False, pos_col: str = 'POS', table_format: str = '', add_cols: list[str] = None, add_allelic_cols: list[str] = None, representatives_only: bool = False, modify_in_place: bool = False) -> Tuple['PrecisionOperator', np.ndarray]
Merge a PrecisionOperator instance with summary statistics DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
precision_op
|
'PrecisionOperator'
|
PrecisionOperator instance |
required |
sumstats
|
DataFrame
|
Summary statistics DataFrame |
required |
variant_id_col
|
str
|
Column name containing variant IDs |
'SNP'
|
ref_allele_col
|
str
|
Column name containing reference allele |
'REF'
|
alt_allele_col
|
str
|
Column name containing alternative allele |
'ALT'
|
match_by_position
|
bool
|
Whether to match SNPs by position instead of ID |
False
|
pos_col
|
str
|
Column name containing position |
'POS'
|
table_format
|
str
|
Optional file format specification (e.g., 'vcf') |
''
|
add_cols
|
list[str]
|
Optional list of column names from sumstats to append to variant_info |
None
|
add_allelic_cols
|
list[str]
|
Optional list of column names from sumstats to append to variant_info, multiplied by the phase (-1 or 1) to align with ancestral/derived alleles. If no alleles are provided, these are added without sign-flipping. |
None
|
modify_in_place
|
bool
|
Whether to modify the PrecisionOperator in place |
False
|
Returns:
| Type | Description |
|---|---|
'PrecisionOperator'
|
Tuple containing: |
ndarray
|
|
Tuple['PrecisionOperator', ndarray]
|
|
Source code in src/graphld/io.py
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 266 267 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 | |
partition_variants
¶
partition_variants(ldgm_metadata: DataFrame, variant_data: DataFrame, *, chrom_col: Optional[str] = None, pos_col: Optional[str] = None) -> List[pl.DataFrame]
Partition variant data according to LDGM blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ldgm_metadata
|
DataFrame
|
DataFrame from read_ldgm_metadata containing block info |
required |
variant_data
|
DataFrame
|
DataFrame containing variant information |
required |
chrom_col
|
Optional[str]
|
Optional name of chromosome column. If None, tries common names |
None
|
pos_col
|
Optional[str]
|
Optional name of position column. If None, tries common names |
None
|
Returns:
| Type | Description |
|---|---|
List[DataFrame]
|
List of DataFrames, one per row in ldgm_metadata, containing variants |
List[DataFrame]
|
that fall within each block's coordinates. Variants within each |
List[DataFrame]
|
returned DataFrame are sorted by chromosome and position rather than |
List[DataFrame]
|
preserving the input row order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file does not have the expected columns: variant_id, chromosome, position, ... |
Source code in src/graphld/io.py
create_ldgm_metadata
¶
create_ldgm_metadata(directory: Union[str, Path], output_file: Optional[str] = None) -> pl.DataFrame
Create metadata file for LDGM files in a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Union[str, Path]
|
Directory containing .snplist and .edgelist files |
required |
output_file
|
Optional[str]
|
Optional path to write CSV file. If None, only returns DataFrame |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Polars DataFrame containing metadata for each LDGM file |
Source code in src/graphld/io.py
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 458 459 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 | |
read_ldgm_metadata
¶
read_ldgm_metadata(filepath: Union[str, Path], *, populations: Optional[Union[str, List[str]]] = None, chromosomes: Optional[Union[int, List[int]]] = None, max_blocks: Optional[int] = None) -> pl.DataFrame
Read LDGM metadata from CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Union[str, Path]
|
Path to metadata CSV file |
required |
populations
|
Optional[Union[str, List[str]]]
|
Optional population(s) to filter by |
None
|
chromosomes
|
Optional[Union[int, List[int]]]
|
Optional chromosome(s) to filter by |
None
|
max_blocks
|
Optional[int]
|
Optional maximum number of blocks to return |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Polars DataFrame containing LDGM metadata, filtered by population and chromosome |
DataFrame
|
if specified, and limited to max_blocks if specified |
Source code in src/graphld/io.py
load_annotations
¶
load_annotations(annot_path: str, chromosome: Optional[int] = None, infer_schema_length: int = 100000, add_alleles: bool = False, add_positions: bool = True, positions_file: str = POSITIONS_FILE, file_pattern: str = '*.{chrom}.annot', exclude_bed: bool = False) -> pl.DataFrame
Load annotation data for specified chromosome(s) and merge with LDGMs data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annot_path
|
str
|
Path to directory containing annotation files |
required |
chromosome
|
Optional[int]
|
Specific chromosome number, or None for all chromosomes |
None
|
infer_schema_length
|
int
|
Number of rows to infer schema from. Runs faster if this is smaller, but will throw an error if too small because floating-point columns will be cast as integers. |
100000
|
file_pattern
|
str
|
Filename pattern to match, with {chrom} as a placeholder for chromosome number |
'*.{chrom}.annot'
|
exclude_bed
|
bool
|
If True, skip loading .bed files from the annotations directory |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame containing annotations |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no matching annotation files are found |
Source code in src/graphld/io.py
666 667 668 669 670 671 672 673 674 675 676 677 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 756 757 758 759 760 761 762 763 764 765 766 767 | |
read_concat_snplists
¶
Read and concatenate snplists from LDGM metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ldgm_metadata
|
DataFrame
|
DataFrame from read_ldgm_metadata containing block info |
required |
Returns:
| Type | Description |
|---|---|
LazyFrame
|
LazyFrame containing variant information concatenated across blocks. |