From 156e6e4c372f3b4c24d5c07c02603a29d0d95412 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 2 Dec 2022 07:26:13 +0300 Subject: correlations: return `None` rather than zero for missing lit corrs * gn3/computations/correlations.py: Return `None` for missing literature correlations rather than zero. Update the sorting key to avoid issues with comparing floats to NoneType objects --- gn3/computations/correlations.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/gn3/computations/correlations.py b/gn3/computations/correlations.py index 5a9ce62..8e8bea5 100644 --- a/gn3/computations/correlations.py +++ b/gn3/computations/correlations.py @@ -269,9 +269,9 @@ def fetch_lit_correlation_data( *tuple(reversed(query_values)))) lit_corr_results = cursor.fetchone() lit_results = (gene_id, lit_corr_results[0])\ - if lit_corr_results else (gene_id, 0) + if lit_corr_results else (gene_id, None) return lit_results - return (gene_id, 0) + return (gene_id, None) def lit_correlation_for_trait( @@ -339,14 +339,19 @@ def compute_all_lit_correlation(conn, trait_lists: List, """Function that acts as an abstraction for lit_correlation_for_trait""" + def __sorter__(trait_name): + val = list(trait_name.values())[0]["lit_corr"] + try: + return (0, -abs(val)) + except TypeError: + return (1, val) + lit_results = lit_correlation_for_trait( conn=conn, target_trait_lists=trait_lists, species=species, trait_gene_id=gene_id) - sorted_lit_results = sorted( - lit_results, - key=lambda trait_name: -abs(list(trait_name.values())[0]["lit_corr"])) + sorted_lit_results = sorted(lit_results, key=__sorter__) return sorted_lit_results -- cgit v1.2.3