Model utils
dequantize(ints, float_dtype=np.float32)
¶
Dequantize integers to the specified floating point type. NOTE: Assumes original floats are in the range [-1, 1].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ints
|
A numpy array of integers |
required | |
float_dtype
|
The floating point data type to dequantize the integers to. |
float32
|
Source code in magenpy/utils/model_utils.py
get_shared_distance_matrix(tree, tips=None)
¶
This function takes a Biopython tree and returns the shared distance matrix, i.e. for a pair of clades or populations, time to most recent common ancestor of the pair minus the time of the most recent common ancestor (MRCA).
Source code in magenpy/utils/model_utils.py
identify_mismatched_snps(gdl, chrom=None, n_iter=10, G=100, p_dentist_threshold=5e-08, p_gwas_threshold=0.01, rsq_threshold=0.95, max_removed_per_iter=0.005)
¶
This function implements a simple quality control procedures that checks that the GWAS summary statistics (Z-scores) are consistent with the LD reference panel. This is done using a simplified version of the framework outlined in the DENTIST paper:
Improved analyses of GWAS summary statistics by reducing data heterogeneity and errors Chen et al. 2021
Compared to DENTIST, the simplifications we make are:
- For each SNP, we sample one neighboring SNP at a time and compute the T statistic
using that neighbor's information. The benefit of this is that we don't need to
invert any matrices, so it's a fast operation to run.
- To arrive at a more robust estimate, we sample up to k
neighbors and average
the T-statistic across those k
neighbors.
NOTE: May need to re-implement this to apply some of the constraints genome-wide rather than on a per-chromosome basis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gdl
|
A |
required | |
chrom
|
Perform checking only on chromosome |
None
|
|
n_iter
|
Number of iterations |
10
|
|
G
|
The number of neighboring SNPs to sample (default: 100) |
100
|
|
p_dentist_threshold
|
The Bonferroni-corrected P-value threshold (default: 5e-8) |
5e-08
|
|
p_gwas_threshold
|
The nominal GWAS P-value threshold for partitioning variants (default: 1e-2) |
0.01
|
|
rsq_threshold
|
The R^2 threshold to select neighbors (neighbor's squared correlation coefficient must be less than specified threshold). |
0.95
|
|
max_removed_per_iter
|
The maximum proportion of variants removed in each iteration |
0.005
|
Source code in magenpy/utils/model_utils.py
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 |
|
match_chromosomes(chrom_1, chrom_2, check_patterns=('chr_', 'chr:', 'chr'), return_both=False)
¶
Given two lists of chromosome IDs, this function returns the
chromosomes that are common to both lists. By default, the returned chromosomes
follow the data type and order of the first list. If return_both
is set to True,
the function returns the common chromosomes in both lists.
The function also accounts for common ways to encode chromosomes, such as chr18, chr_18, 18, etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chrom_1
|
A list or numpy array of chromosome IDs |
required | |
chrom_2
|
A list or numpy array of chromosome IDs |
required | |
check_patterns
|
A list of patterns to check for and replace in the chromosome IDs |
('chr_', 'chr:', 'chr')
|
|
return_both
|
If True, return the common chromosomes in both lists |
False
|
Source code in magenpy/utils/model_utils.py
merge_snp_tables(ref_table, alt_table, how='inner', on='auto', signed_statistics=('BETA', 'STD_BETA', 'Z'), drop_duplicates=True, correct_flips=True, return_ref_indices=False, return_alt_indices=False)
¶
This function takes a reference SNP table with at least 3 columns ('SNP', 'A1', A2
)
and matches it with an alternative table that also has these 3 columns defined. In the most recent
implementation, we allow users to merge on any set of columns that they wish by specifying the on
parameter. For example, instead of SNP
, the user can join the SNP tables on CHR
and POS
, the
chromosome number and base pair position of the SNP.
The manner in which the join operation takes place depends on the how
argument.
Currently, the function supports inner
and left
joins.
The function removes duplicates if drop_duplicates
parameter is set to True
If correct_flips
is set to True, the function will correct summary statistics in
the alternative table alt_table
(e.g. BETA, MAF) based whether the A1 alleles agree between the two tables.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ref_table
|
The reference table (pandas dataframe). |
required | |
alt_table
|
The alternative table (pandas dataframe) |
required | |
how
|
|
'inner'
|
|
on
|
Which columns to use as anchors when merging. By default, we automatically infer which columns to use, but the user can specify this directly. When |
'auto'
|
|
signed_statistics
|
The columns with signed statistics to flip if |
('BETA', 'STD_BETA', 'Z')
|
|
drop_duplicates
|
Drop duplicate SNPs |
True
|
|
correct_flips
|
Correct SNP summary statistics that depend on status of alternative allele |
True
|
|
return_ref_indices
|
Return the indices of the remaining entries in the reference table before merging. |
False
|
|
return_alt_indices
|
Return the indices of the remaining entries in the alternative table before merging. |
False
|
Source code in magenpy/utils/model_utils.py
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 |
|
multinomial_rvs(n, p)
¶
Copied from Warren Weckesser: https://stackoverflow.com/a/55830796
Sample from the multinomial distribution with multiple p vectors.
- n must be a scalar.
- p must an n-dimensional numpy array, n >= 1. The last axis of p holds the sequence of probabilities for a multinomial distribution.
The return value has the same shape as p.
Source code in magenpy/utils/model_utils.py
quantize(floats, int_dtype=np.int8)
¶
Quantize floating point numbers to the specified integer type. NOTE: Assumes that the floats are in the range [-1, 1].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
floats
|
A numpy array of floats |
required | |
int_dtype
|
The integer type to quantize to. |
int8
|
Source code in magenpy/utils/model_utils.py
tree_to_rho(tree, min_corr)
¶
This function takes a Biopython tree and a minimum correlation parameter and returns the correlation matrix for the effect sizes across populations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree
|
a Biopython Phylo object |
required | |
min_corr
|
minimum correlation |
required |
Returns:
Type | Description |
---|---|
|