diff options
author | Frederick Muriuki Muriithi | 2021-09-28 10:15:43 +0300 |
---|---|---|
committer | BonfaceKilz | 2021-09-28 11:23:46 +0300 |
commit | 4a55971a9be54b399c45a53e211df3348df1c52b (patch) | |
tree | 56204a994144e01109a1c8d186385966f20f9659 /gn3 | |
parent | 767eb96db12476f741bb5197bda7555c29e79b55 (diff) | |
download | genenetwork3-4a55971a9be54b399c45a53e211df3348df1c52b.tar.gz |
Retrieve loci names ordered by chromosomes
Issue:
https://github.com/genenetwork/gn-gemtext-threads/blob/main/topics/gn1-migration-to-gn2/clustering.gmi
* gn3/heatmaps.py: implement function
* tests/unit/test_heatmaps.py: add test
Add a function to retrieve the loci names from the traits, ordered by
chromosomes, in alphabetical order.
This is useful to provide the user with more information on hovering over
the heatmap cells: each cell will now display the locus name, trait name and
value associated with it.
Diffstat (limited to 'gn3')
-rw-r--r-- | gn3/heatmaps.py | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/gn3/heatmaps.py b/gn3/heatmaps.py index 2ef2d16..9c10ba3 100644 --- a/gn3/heatmaps.py +++ b/gn3/heatmaps.py @@ -4,7 +4,7 @@ generate various kinds of heatmaps. """ from functools import reduce -from typing import Any, Dict, Sequence +from typing import Any, Dict, Union, Sequence import numpy as np import plotly.graph_objects as go # type: ignore @@ -142,6 +142,32 @@ def cluster_traits(traits_data_list: Sequence[Dict]): return tuple(__cluster(tdata_i) for tdata_i in enumerate(traits_data_list)) +def get_loci_names( + organised: dict, + chromosome_names: Sequence[str]) -> Sequence[Sequence[str]]: + """ + Get the loci names organised by the same order as the `chromosome_names`. + """ + def __get_trait_loci(accumulator, trait): + chrs = tuple(trait["chromosomes"].keys()) + trait_loci = { + _chr: tuple( + locus["Locus"] + for locus in trait["chromosomes"][_chr]["loci"] + ) for _chr in chrs + } + return { + **accumulator, + **{ + _chr: tuple(sorted(set( + trait_loci[_chr] + accumulator.get(_chr, tuple())))) + for _chr in trait_loci.keys() + } + } + loci_dict: Dict[Union[str, int], Sequence[str]] = reduce( + __get_trait_loci, [v[1] for v in organised.items()], {}) + return tuple(loci_dict[_chr] for _chr in chromosome_names) + def build_heatmap(traits_names, conn: Any): """ heatmap function |