Skip to content

Commit

Permalink
refmt with black
Browse files Browse the repository at this point in the history
  • Loading branch information
aryarm committed Jul 25, 2024
1 parent f689b29 commit 4b84ac8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
4 changes: 3 additions & 1 deletion happler/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ def run(
corrector = None
log.debug(f"Using alpha threshold of {threshold}")
terminator = tree.terminator.TTestTerminator(
thresh=threshold, corrector=corrector, log=log,
thresh=threshold,
corrector=corrector,
log=log,
)
log.info("Running tree builder")
hap_tree = tree.TreeBuilder(
Expand Down
1 change: 0 additions & 1 deletion happler/tree/corrector.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def correct(


class BH(Corrector):

if false_discovery_control is None:

def correct(
Expand Down
56 changes: 32 additions & 24 deletions happler/tree/tree_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ def prune_tree(self, from_root: bool = True):
else:
self.log.debug(f"Left leaf {leaf_var.id} (with LD {ld}) unpruned")
self.log.info(
f"Pruned {count} leaves with LD > {self.ld_prune_thresh} with their"
" siblings"
f"Pruned {count} leaves with LD > {self.ld_prune_thresh} with their siblings"
)

def maf_mask(
Expand Down Expand Up @@ -309,26 +308,31 @@ def _find_split_flexible(
if parent_res is not None:
allele_gts = (self.gens.data[:, best_var_idx] == allele).sum(axis=1)
cv_data = np.vstack((parent.data.sum(axis=1), allele_gts)).T
hap_indep_effect = AssocTestSimpleCovariates(covars=cv_data).run(
hap_matrix[:, best_res_idx][:, np.newaxis].sum(axis=1),
self.phens.data[:, 0],
).data["pval"][0]
hap_indep_effect = (
AssocTestSimpleCovariates(covars=cv_data)
.run(
hap_matrix[:, best_res_idx][:, np.newaxis].sum(axis=1),
self.phens.data[:, 0],
)
.data["pval"][0]
)
if hap_indep_effect > self.indep_thresh:
self.log.debug(
f"Terminating because the haplotype had a pval of {hap_indep_effect} >"
f" {self.indep_thresh} in an additive model with the allele and parent"
"Terminating because the haplotype had a pval of"
f" {hap_indep_effect} > {self.indep_thresh} in an additive model"
" with the allele and parent"
)
yield None, allele, node_res
continue
self.log.debug(
f"The haplotype had a pval of {hap_indep_effect} < {self.indep_thresh}"
" in an additive model with the allele and parent"
f"The haplotype had a pval of {hap_indep_effect} <"
f" {self.indep_thresh} in an additive model with the allele and"
" parent"
)
# step 7: check if this allele is significant and whether we should terminate the branch
self.log.debug(
"Testing variant {} / allele {} with parent_res {} and node_res {}".format(
best_variant.id, allele, parent_res, node_res
)
"Testing variant {} / allele {} with parent_res {} and node_res {}"
.format(best_variant.id, allele, parent_res, node_res)
)
if self.terminator.check(
parent_res, node_res, results, best_res_idx, num_samps, num_tests
Expand Down Expand Up @@ -415,14 +419,14 @@ def _find_split_rigid(
best_allele: best_var_idx,
int(not best_allele): np.searchsorted(
maf_mask[int(not best_allele)], maf_mask[best_allele][best_var_idx]
)
),
}
num_tests = len(parent.nodes) + 1
# step 4: find the index of the best variant within the genotype matrix
# We need to account for the rare variants that were masked out and indices
# that we removed when running transform()
best_var_idx += maf_mask[best_allele][best_res_idx[best_allele]] - len(
maf_mask[best_allele][:best_res_idx[best_allele]]
maf_mask[best_allele][: best_res_idx[best_allele]]
)
# There might be a faster way of doing this but for now we're just going to
# live with it
Expand All @@ -439,14 +443,19 @@ def _find_split_rigid(
if parent_res is not None:
allele_gts = self.gens.data[:, best_var_idx].sum(axis=1)
cv_data = np.vstack((parent.data.sum(axis=1), allele_gts)).T
hap_indep_effect = AssocTestSimpleCovariates(covars=cv_data).run(
hap_matrix[:, best_res_idx[1]][:, np.newaxis].sum(axis=1),
self.phens.data[:, 0],
).data["pval"][0]
hap_indep_effect = (
AssocTestSimpleCovariates(covars=cv_data)
.run(
hap_matrix[:, best_res_idx[1]][:, np.newaxis].sum(axis=1),
self.phens.data[:, 0],
)
.data["pval"][0]
)
if hap_indep_effect > self.indep_thresh:
self.log.debug(
f"Terminating because the haplotype had a pval of {hap_indep_effect} >"
f" {self.indep_thresh} in an additive model with the allele and parent"
"Terminating because the haplotype had a pval of"
f" {hap_indep_effect} > {self.indep_thresh} in an additive model with"
" the allele and parent"
)
for allele in results:
yield None, allele, None
Expand Down Expand Up @@ -478,9 +487,8 @@ def _find_split_rigid(
node_res = self.results_type.from_np(best_results)
# step 8: check whether we should terminate the branch
self.log.debug(
"Testing variant {} / allele {} with parent_res {} and node_res {}".format(
best_variant.id, allele, parent_res, node_res
)
"Testing variant {} / allele {} with parent_res {} and node_res {}"
.format(best_variant.id, allele, parent_res, node_res)
)
if self.terminator.check(
parent_res,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ def test_tree_dot():
assert (
tree.dot()
== 'strict digraph {\nforcelabels=true;\n0 [label="root"];\n1 [label="SNP0"];\n2'
' [label="SNP1"];\n3 [label="SNP2"];\n4 [label="SNP3"];\n5 [label="SNP3"];\n0 -> 1 '
" [label=0];\n1 -> 2 [label=0];\n2 -> 3 [label=1];\n3 -> 4 [label=0];\n3"
" -> 5 [label=1];\n}\n"
' [label="SNP1"];\n3 [label="SNP2"];\n4 [label="SNP3"];\n5 [label="SNP3"];\n0'
" -> 1 [label=0];\n1 -> 2 [label=0];\n2 -> 3 [label=1];\n3 -> 4 "
" [label=0];\n3 -> 5 [label=1];\n}\n"
)
log.setLevel(log_level)

Expand Down

0 comments on commit 4b84ac8

Please sign in to comment.