comparison COBRAxy/flux_to_map.py @ 164:929104dceba8 draft

Uploaded
author luca_milaz
date Mon, 18 Nov 2024 17:17:02 +0000
parents 8e3cbf68cdc4
children fa981714e87d
comparison
equal deleted inserted replaced
163:0a043d85070e 164:929104dceba8
889 return np.min(non_zero_elements) if non_zero_elements.size > 0 else None 889 return np.min(non_zero_elements) if non_zero_elements.size > 0 else None
890 890
891 def computeEnrichmentMeanMedian(metabMap: ET.ElementTree, class_pat: Dict[str, List[List[float]]], ids: List[str], colormap:str) -> None: 891 def computeEnrichmentMeanMedian(metabMap: ET.ElementTree, class_pat: Dict[str, List[List[float]]], ids: List[str], colormap:str) -> None:
892 """ 892 """
893 Compute and visualize the metabolic map based on mean and median of the input fluxes. 893 Compute and visualize the metabolic map based on mean and median of the input fluxes.
894 The fluxes are normalised across classes/datasets and visualised using the given colormap. 894 The fluxes are normalized across classes/datasets and visualized using the given colormap.
895 895
896 Args: 896 Args:
897 metabMap (ET.ElementTree): An XML tree representing the metabolic map. 897 metabMap (ET.ElementTree): An XML tree representing the metabolic map.
898 class_pat (Dict[str, List[List[float]]]): A dictionary where keys are class names and values are lists of enrichment values. 898 class_pat (Dict[str, List[List[float]]]): A dictionary where keys are class names and values are lists of enrichment values.
899 ids (List[str]): A list of reaction IDs to be used for coloring arrows. 899 ids (List[str]): A list of reaction IDs to be used for coloring arrows.
905 metabMap_mean = copy.deepcopy(metabMap) 905 metabMap_mean = copy.deepcopy(metabMap)
906 metabMap_median = copy.deepcopy(metabMap) 906 metabMap_median = copy.deepcopy(metabMap)
907 907
908 # Compute medians and means 908 # Compute medians and means
909 medians = {key: np.round(np.median(np.array(value), axis=1), 6) for key, value in class_pat.items()} 909 medians = {key: np.round(np.median(np.array(value), axis=1), 6) for key, value in class_pat.items()}
910 means = {key: np.round(np.mean(np.array(value), axis=1),6) for key, value in class_pat.items()} 910 means = {key: np.round(np.mean(np.array(value), axis=1), 6) for key, value in class_pat.items()}
911 911
912 # Normalize medians and means 912 # Normalize medians and means
913 max_flux_medians = max(np.max(np.abs(arr)) for arr in medians.values()) 913 max_flux_medians = max(np.max(np.abs(arr)) for arr in medians.values())
914 max_flux_means = max(np.max(np.abs(arr)) for arr in means.values()) 914 max_flux_means = max(np.max(np.abs(arr)) for arr in means.values())
915 915
916 min_flux_medians = min(min_nonzero_abs(arr) for arr in medians.values()) 916 min_flux_medians = min(np.min(arr) for arr in medians.values())
917 min_flux_means = min(min_nonzero_abs(arr) for arr in means.values()) 917 min_flux_means = min(np.min(arr) for arr in means.values())
918 918
919 medians = {key: median/max_flux_medians for key, median in medians.items()} 919 max_flux_medians = max(np.max(arr) for arr in medians.values())
920 means = {key: mean/max_flux_means for key, mean in means.items()} 920 max_flux_means = max(np.max(arr) for arr in means.values())
921 921
922 save_colormap_image(min_flux_medians, max_flux_medians, utils.FilePath("Color map median", ext=utils.FileFormat.PNG, prefix=ARGS.output_path), colormap) 922 save_colormap_image(min_flux_medians, max_flux_medians, utils.FilePath("Color map median", ext=utils.FileFormat.PNG, prefix=ARGS.output_path), colormap)
923 save_colormap_image(min_flux_means, max_flux_means, utils.FilePath("Color map mean", ext=utils.FileFormat.PNG, prefix=ARGS.output_path), colormap) 923 save_colormap_image(min_flux_means, max_flux_means, utils.FilePath("Color map mean", ext=utils.FileFormat.PNG, prefix=ARGS.output_path), colormap)
924 924
925 cmap = plt.get_cmap(colormap) 925 cmap = plt.get_cmap(colormap)
926 926
927 for key in class_pat: 927 for key in class_pat:
928 # Create color mappings for median and mean 928 # Create color mappings for median and mean
929 colors_median = { 929 colors_median = {
930 rxn_id: rgb_to_hex(cmap(abs(medians[key][i]))) if medians[key][i] != 0 else '#bebebe' #grey blocked 930 rxn_id: rgb_to_hex(cmap((medians[key][i] - min_flux_medians) / (max_flux_medians - min_flux_medians))) if medians[key][i] != 0 else '#bebebe' #grey blocked
931 for i, rxn_id in enumerate(ids) 931 for i, rxn_id in enumerate(ids)
932 } 932 }
933 933
934 colors_mean = { 934 colors_mean = {
935 rxn_id: rgb_to_hex(cmap(abs(means[key][i]))) if means[key][i] != 0 else '#bebebe' #grey blocked 935 rxn_id: rgb_to_hex(cmap((means[key][i] - min_flux_means) / (max_flux_means - min_flux_means))) if means[key][i] != 0 else '#bebebe' #grey blocked
936 for i, rxn_id in enumerate(ids) 936 for i, rxn_id in enumerate(ids)
937 } 937 }
938 938
939 for i, rxn_id in enumerate(ids): 939 for i, rxn_id in enumerate(ids):
940 isNegative = medians[key][i] < 0 940 isNegative = medians[key][i] < 0