credibleset
Credible Set functions.
CredibleSet
¶
Class representing credible sets from one fine-mapping tool.
Parameters¶
tool : str The name of the fine-mapping tool. parameters : Dict[str, Any] Additional parameters used by the fine-mapping tool. coverage : float The coverage of the credible sets. n_cs : int The number of credible sets. cs_sizes : List[int] Sizes of each credible set. lead_snps : List[str] List of lead SNPs. snps : List[List[str]] List of SNPs for each credible set. pips : pd.Series Posterior inclusion probabilities.
Attributes¶
tool : str The name of the fine-mapping tool. n_cs : int The number of credible sets. coverage : float The coverage of the credible sets. lead_snps : List[str] List of lead SNPs. snps : List[List[str]] List of SNPs for each credible set. cs_sizes : List[int] Sizes of each credible set. pips : pd.Series Posterior inclusion probabilities. parameters : Dict[str, Any] Additional parameters used by the fine-mapping tool.
Source code in credtools/credibleset.py
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 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 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 340 341 342 343 344 345 346 347 348 349 350 351 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 | |
coverage
property
¶
Get the coverage.
cs_sizes
property
¶
Get the sizes of each credible set.
lead_snps
property
¶
Get the lead SNPs.
n_cs
property
¶
Get the number of credible sets.
parameters
property
¶
Get the parameters.
per_locus_results
property
¶
Get per-locus credible set results.
pips
property
¶
Get the PIPs.
purity
property
¶
Get the purity values for each credible set.
snps
property
¶
Get the SNPs.
tool
property
¶
Get the tool name.
__init__(tool, parameters, coverage, n_cs, cs_sizes, lead_snps, snps, pips, per_locus_results=None, purity=None)
¶
Initialize CredibleSet object.
Parameters¶
tool : str The name of the fine-mapping tool. parameters : Dict[str, Any] Additional parameters used by the fine-mapping tool. coverage : float The coverage of the credible sets. n_cs : int The number of credible sets. cs_sizes : List[int] Sizes of each credible set. lead_snps : List[str] List of lead SNPs. snps : List[List[str]] List of SNPs for each credible set. pips : pd.Series Posterior inclusion probabilities. per_locus_results : Optional[Dict[str, "CredibleSet"]], optional Mapping of locus identifiers to their individual credible set results. purity : Optional[List[Optional[float]]], optional List of purity values for each credible set. Purity is the minimum absolute LD R value between all SNP pairs in a credible set. None if LD matrix is not available.
Source code in credtools/credibleset.py
__repr__()
¶
Return a string representation of the CredibleSet object.
Returns¶
str String representation of the CredibleSet object.
Source code in credtools/credibleset.py
copy()
¶
Copy the CredibleSet object.
Returns¶
CredibleSet A copy of the CredibleSet object.
Source code in credtools/credibleset.py
create_enhanced_pips_df(locus_set)
¶
Create DataFrame with PIPs and full sumstats information.
Parameters¶
locus_set : LocusSet The locus set containing locus data.
Returns¶
pd.DataFrame DataFrame containing full sumstats, PIPs, R2, and credible set assignments.
Source code in credtools/credibleset.py
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 340 341 342 343 344 345 346 347 348 349 350 351 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 | |
from_dict(data, pips)
classmethod
¶
Create CredibleSet from dictionary and pips.
Parameters¶
data : Dict[str, Any] A dictionary containing the data to initialize the CredibleSet. pips : pd.Series Posterior inclusion probabilities.
Returns¶
CredibleSet An instance of CredibleSet initialized with the provided data and pips.
Source code in credtools/credibleset.py
set_per_locus_results(per_locus_results)
¶
to_dict()
¶
Convert to dictionary for TOML storage (excluding pips).
Returns¶
Dict[str, Any] A dictionary representation of the CredibleSet excluding pips.
Source code in credtools/credibleset.py
calculate_cs_purity(ld, cs_snp_ids)
¶
Calculate purity for a single credible set.
Purity is defined as the minimum absolute LD R value between all pairs of SNPs in the credible set.
For multiple LD matrices (multi-ancestry case), purity is calculated as: 1. Extract CS submatrix from each LD matrix 2. Take element-wise maximum of absolute values across all matrices 3. Return the minimum value from the resulting meta-LD matrix
This approach (similar to MultiSuSiE) ensures the credible set has high purity across all populations.
Parameters¶
ld : LDMatrix or List[LDMatrix] LDMatrix object(s) containing both r matrix and map with SNPIDs. If a list is provided, meta-purity across all matrices is calculated. cs_snp_ids : List[str] List of SNPID strings in the credible set.
Returns¶
Optional[float] - If CS has only 1 SNP, returns 1.0 - If CS has multiple SNPs, returns min(|R|) for all SNP pairs - For multiple LD matrices, returns min of element-wise max across matrices - If unable to calculate (e.g., SNPs not in LD matrix), returns None
Examples¶
Single LD matrix: CS with 3 SNPs having LD R values: 0.8, 0.9, 0.7¶
Purity = min(|0.8|, |0.9|, |0.7|) = 0.7¶
Multiple LD matrices: same CS in EUR and AFR¶
EUR: |R| values = [0.8, 0.9, 0.7]¶
AFR: |R| values = [0.6, 0.85, 0.75]¶
Meta |R| = max([0.8, 0.9, 0.7], [0.6, 0.85, 0.75]) = [0.8, 0.9, 0.75]¶
Purity = min([0.8, 0.9, 0.75]) = 0.75¶
Source code in credtools/credibleset.py
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 | |
cluster_cs(dict_sets, threshold=0.9)
¶
Cluster dictionaries from different sets based on continuous Jaccard similarity.
Parameters¶
dict_sets : List[List[Dict[str, float]]] List of m sets, where each set contains dictionaries with PIP values. threshold : float, optional Clustering threshold, by default 0.9.
Returns¶
List[List[str]] List of merged clusters, where each cluster contains a list of unique SNP IDs from the dictionaries in that cluster.
Raises¶
ValueError If less than two sets of dictionaries are provided or if any set is empty.
Examples¶
sets = [ ... [{'a': 0.8, 'b': 0.5}], ... [{'b': 0.6, 'c': 0.3}] ... ] clusters = cluster_cs(sets)
Source code in credtools/credibleset.py
combine_creds(creds, combine_cred='union', combine_pip='max', jaccard_threshold=0.1, ld_matrices=None, min_purity=0.0)
¶
Combine credible sets from multiple tools.
Parameters¶
creds : List[CredibleSet] List of credible sets from multiple tools. combine_cred : str, optional Method to combine credible sets, by default "union". Options: "union", "intersection", "cluster".
- "union": Union of all credible sets to form a merged credible set.
- "intersection": First merge the credible sets from the same tool,
then take the intersection of all merged credible sets.
No credible set will be returned if no common SNPs found.
- "cluster": Merge credible sets with Jaccard index > jaccard_threshold.
combine_pip : str, optional Method to combine PIPs, by default "max". Options: "max", "min", "mean", "meta".
- "meta": PIP_meta = 1 - prod(1 - PIP_i), where i is the index of tools,
PIP_i = 0 when the SNP is not in the credible set of the tool.
- "max": Maximum PIP value for each SNP across all tools.
- "min": Minimum PIP value for each SNP across all tools.
- "mean": Mean PIP value for each SNP across all tools.
jaccard_threshold : float, optional Jaccard index threshold for the "cluster" method, by default 0.1. ld_matrices : Optional[List[LDMatrix]], optional List of LD matrices for purity calculation, by default None. If provided, purity will be calculated for merged credible sets using multi-ancestry approach (element-wise max across populations). If None, purity will not be calculated for the merged credible sets. min_purity : float, optional Minimum purity threshold for filtering credible sets, by default 0.0. After combining credible sets, only those with purity >= min_purity will be kept. Purity is the minimum absolute LD R value between all SNP pairs in a credible set. Set to 0.0 (default) for no filtering.
Returns¶
CredibleSet Combined credible set.
Raises¶
ValueError If the method is not supported.
Notes¶
'union' and 'intersection' methods will merge all credible sets into one.
Source code in credtools/credibleset.py
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 | |
combine_pips(pips, method='max')
¶
Combine PIPs from multiple tools.
Parameters¶
pips : List[pd.Series] List of PIPs from multiple tools. method : str, optional Method to combine PIPs, by default "max". Options: "max", "min", "mean", "meta". When "meta" is selected, the method will use the formula: PIP_meta = 1 - prod(1 - PIP_i), where i is the index of tools, PIP_i = 0 when the SNP is not in the credible set of the tool. When "max", "min", "mean" is selected, the SNP not in the credible set will be excluded from the calculation.
Returns¶
pd.Series Combined PIPs.
Raises¶
ValueError If the method is not supported.
Source code in credtools/credibleset.py
continuous_jaccard(dict1, dict2)
¶
Calculate modified Jaccard similarity for continuous values (PIP values).
Formula: ∑min(xi,yi)/∑max(xi,yi) where xi, yi are PIP values or 0 if missing
Citation: Yuan, K. et al. (2024) Nature Genetics https://doi.org/10.1038/s41588-024-01870-z.
Parameters¶
dict1 : Dict[str, float] First dictionary with keys and PIP values (0-1). dict2 : Dict[str, float] Second dictionary with keys and PIP values (0-1).
Returns¶
float Modified Jaccard similarity index between 0 and 1.
Raises¶
ValueError If any values are not between 0 and 1.
Examples¶
d1 = {'a': 0.8, 'b': 0.5} d2 = {'b': 0.6, 'c': 0.3} continuous_jaccard(d1, d2) 0.5
Source code in credtools/credibleset.py
create_similarity_matrix(dict_sets)
¶
Create a similarity matrix for all pairs of dictionaries across different sets.
Parameters¶
dict_sets : List[List[Dict[str, float]]] List of m sets, where each set contains dictionaries with PIP values.
Returns¶
Tuple[np.ndarray, List[Dict[str, float]]] A tuple containing: - Similarity matrix (n_dicts x n_dicts) - Flattened list of dictionaries
Examples¶
sets = [[{'a': 0.8, 'b': 0.5}], [{'b': 0.6, 'c': 0.3}]] matrix, dicts = create_similarity_matrix(sets)
Source code in credtools/credibleset.py
filter_credset_by_purity(credset, min_purity=0.0)
¶
Filter credible sets by purity threshold.
Removes credible sets that do not meet the minimum purity requirement. Purity is defined as the minimum absolute LD R value between all pairs of SNPs in the credible set.
Parameters¶
credset : CredibleSet CredibleSet object containing credible sets and their purity values. min_purity : float, optional Minimum purity threshold for filtering, by default 0.0. Credible sets with purity < min_purity will be removed. Set to 0.0 (default) for no filtering.
Returns¶
CredibleSet New CredibleSet object with only credible sets meeting purity threshold. If no credible sets pass filtering, returns empty CredibleSet (n_cs=0).
Notes¶
- If credset.purity is None or empty, no filtering is applied (returns original credset)
- If min_purity <= 0, no filtering is applied (returns original credset)
- Filtered credible sets maintain their original ordering
- PIPs are preserved for all variants (not filtered)
Examples¶
Filter credible sets to keep only high-purity sets (purity >= 0.5)¶
filtered_cs = filter_credset_by_purity(credset, min_purity=0.5) print(f"Original: {credset.n_cs} CS, Filtered: {filtered_cs.n_cs} CS") Original: 5 CS, Filtered: 3 CS
No filtering (default)¶
same_cs = filter_credset_by_purity(credset, min_purity=0.0) assert same_cs.n_cs == credset.n_cs
Source code in credtools/credibleset.py
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 | |
generate_cs_summary(causal_variants, locus_id, locus_set)
¶
Generate credible set summary from causal variants.
Creates one summary row per credible set, including lead SNP, size, PIP thresholds, and purity metrics.
Parameters¶
causal_variants : pd.DataFrame DataFrame of causal variants (rows where CRED != 0), must have columns: CRED, PIP, SNPID. locus_id : str Locus identifier string. locus_set : LocusSet LocusSet object for LD-based purity calculation.
Returns¶
List[Dict] List of summary dictionaries, one per credible set.