Mercurial > repos > bimib > cobraxy
comparison COBRAxy/flux_to_map.py @ 240:63f5078627a9 draft
Uploaded
author | francesco_lapi |
---|---|
date | Mon, 13 Jan 2025 10:01:40 +0000 |
parents | 5c70439f2907 |
children | 049aa0f4844f |
comparison
equal
deleted
inserted
replaced
239:7bd33d296319 | 240:63f5078627a9 |
---|---|
829 | 829 |
830 Returns: | 830 Returns: |
831 Tuple[ClassPat, List[str]]: values and IDs extracted from the dataset | 831 Tuple[ClassPat, List[str]]: values and IDs extracted from the dataset |
832 """ | 832 """ |
833 dataset = read_dataset(datasetPath, datasetName) | 833 dataset = read_dataset(datasetPath, datasetName) |
834 | |
835 # Ensure the first column is treated as the reaction name | |
836 dataset = dataset.set_index(dataset.columns[0]) | |
837 | |
838 # Check if required reactions exist in the dataset | |
839 required_reactions = ['EX_lac__L_e', 'EX_glc__D_e', 'EX_gln__L_e', 'EX_glu__L_e'] | |
840 missing_reactions = [reaction for reaction in required_reactions if reaction not in dataset.index] | |
841 | |
842 if missing_reactions: | |
843 sys.exit(f'Execution aborted: Missing required reactions {missing_reactions} in {datasetName}\n') | |
844 | |
845 # Calculate new rows using safe division | |
846 lact_glc = np.divide( | |
847 dataset.loc['EX_lac__L_e'], -dataset.loc['EX_glc__D_e'], | |
848 out=np.zeros_like(dataset.loc['EX_lac__L_e']), where=dataset.loc['EX_glc__D_e'] != 0 | |
849 ) | |
850 lact_gln = np.divide( | |
851 dataset.loc['EX_lac__L_e'], -dataset.loc['EX_gln__L_e'], | |
852 out=np.zeros_like(dataset.loc['EX_lac__L_e']), where=dataset.loc['EX_gln__L_e'] != 0 | |
853 ) | |
854 glu_gln = np.divide( | |
855 dataset.loc['EX_glu__L_e'], -dataset.loc['EX_gln__L_e'], | |
856 out=np.zeros_like(dataset.loc['EX_glu__L_e']), where=dataset.loc['EX_gln__L_e'] != 0 | |
857 ) | |
858 | |
859 # Create a DataFrame for the new rows | |
860 new_rows = pd.DataFrame({ | |
861 dataset.index.name: ['LactGlc', 'LactGln', 'GluGln'], | |
862 **{col: [lact_glc[i], lact_gln[i], glu_gln[i]] for i, col in enumerate(dataset.columns)} | |
863 }) | |
864 | |
865 # Reset the index of the original dataset and append new rows | |
866 dataset.reset_index(inplace=True) | |
867 dataset = pd.concat([dataset, new_rows], ignore_index=True) | |
868 | |
834 IDs = pd.Series.tolist(dataset.iloc[:, 0].astype(str)) | 869 IDs = pd.Series.tolist(dataset.iloc[:, 0].astype(str)) |
835 | 870 |
836 dataset = dataset.drop(dataset.columns[0], axis = "columns").to_dict("list") | 871 dataset = dataset.drop(dataset.columns[0], axis = "columns").to_dict("list") |
837 return { id : list(map(utils.Float("Dataset values, not an argument"), values)) for id, values in dataset.items() }, IDs | 872 return { id : list(map(utils.Float("Dataset values, not an argument"), values)) for id, values in dataset.items() }, IDs |
838 | 873 |
925 | 960 |
926 save_colormap_image(min_flux_medians, max_flux_medians, utils.FilePath("Color map median", ext=utils.FileFormat.PNG, prefix=ARGS.output_path), colormap) | 961 save_colormap_image(min_flux_medians, max_flux_medians, utils.FilePath("Color map median", ext=utils.FileFormat.PNG, prefix=ARGS.output_path), colormap) |
927 save_colormap_image(min_flux_means, max_flux_means, utils.FilePath("Color map mean", ext=utils.FileFormat.PNG, prefix=ARGS.output_path), colormap) | 962 save_colormap_image(min_flux_means, max_flux_means, utils.FilePath("Color map mean", ext=utils.FileFormat.PNG, prefix=ARGS.output_path), colormap) |
928 | 963 |
929 cmap = plt.get_cmap(colormap) | 964 cmap = plt.get_cmap(colormap) |
965 | |
966 min_width = 2.0 # Minimum arrow width | |
967 max_width = 15.0 # Maximum arrow width | |
930 | 968 |
931 for key in class_pat: | 969 for key in class_pat: |
932 # Create color mappings for median and mean | 970 # Create color mappings for median and mean |
933 colors_median = { | 971 colors_median = { |
934 rxn_id: rgb_to_hex(cmap(abs(medians[key][i]))) if medians[key][i] != 0 else '#bebebe' #grey blocked | 972 rxn_id: rgb_to_hex(cmap(abs(medians[key][i]))) if medians[key][i] != 0 else '#bebebe' #grey blocked |
939 rxn_id: rgb_to_hex(cmap(abs(means[key][i]))) if means[key][i] != 0 else '#bebebe' #grey blocked | 977 rxn_id: rgb_to_hex(cmap(abs(means[key][i]))) if means[key][i] != 0 else '#bebebe' #grey blocked |
940 for i, rxn_id in enumerate(ids) | 978 for i, rxn_id in enumerate(ids) |
941 } | 979 } |
942 | 980 |
943 for i, rxn_id in enumerate(ids): | 981 for i, rxn_id in enumerate(ids): |
982 # Calculate arrow width for median | |
983 width_median = np.interp(abs(medians[key][i]), [0, 1], [min_width, max_width]) | |
944 isNegative = medians[key][i] < 0 | 984 isNegative = medians[key][i] < 0 |
945 | 985 apply_arrow(metabMap_median, rxn_id, colors_median[rxn_id], isNegative, width_median) |
946 # Apply median arrows | 986 |
947 apply_arrow(metabMap_median, rxn_id, colors_median[rxn_id], isNegative) | 987 # Calculate arrow width for mean |
948 | 988 width_mean = np.interp(abs(means[key][i]), [0, 1], [min_width, max_width]) |
949 isNegative = means[key][i] < 0 | 989 isNegative = means[key][i] < 0 |
950 # Apply mean arrows | 990 apply_arrow(metabMap_mean, rxn_id, colors_mean[rxn_id], isNegative, width_mean) |
951 apply_arrow(metabMap_mean, rxn_id, colors_mean[rxn_id], isNegative) | |
952 | 991 |
953 # Save and convert the SVG files | 992 # Save and convert the SVG files |
954 save_and_convert(metabMap_mean, "mean", key) | 993 save_and_convert(metabMap_mean, "mean", key) |
955 save_and_convert(metabMap_median, "median", key) | 994 save_and_convert(metabMap_median, "median", key) |
956 | 995 |
957 def apply_arrow(metabMap, rxn_id, color, isNegative): | 996 def apply_arrow(metabMap, rxn_id, color, isNegative, width=5): |
958 """ | 997 """ |
959 Apply an arrow to a specific reaction in the metabolic map with a given color. | 998 Apply an arrow to a specific reaction in the metabolic map with a given color. |
960 | 999 |
961 Args: | 1000 Args: |
962 metabMap (ET.ElementTree): An XML tree representing the metabolic map. | 1001 metabMap (ET.ElementTree): An XML tree representing the metabolic map. |
963 rxn_id (str): The ID of the reaction to which the arrow will be applied. | 1002 rxn_id (str): The ID of the reaction to which the arrow will be applied. |
964 color (str): The color of the arrow in hexadecimal format. | 1003 color (str): The color of the arrow in hexadecimal format. |
1004 isNegative (bool): A boolean indicating if the arrow represents a negative value. | |
1005 width (int): The width of the arrow. | |
965 | 1006 |
966 Returns: | 1007 Returns: |
967 None | 1008 None |
968 """ | 1009 """ |
969 arrow = Arrow(width=5, col=color) | 1010 arrow = Arrow(width=width, col=color) |
970 arrow.styleReactionElementsMeanMedian(metabMap, rxn_id, isNegative) | 1011 arrow.styleReactionElementsMeanMedian(metabMap, rxn_id, isNegative) |
971 pass | 1012 pass |
972 | 1013 |
973 def save_and_convert(metabMap, map_type, key): | 1014 def save_and_convert(metabMap, map_type, key): |
974 """ | 1015 """ |
1005 """ | 1046 """ |
1006 | 1047 |
1007 global ARGS | 1048 global ARGS |
1008 ARGS = process_args(args) | 1049 ARGS = process_args(args) |
1009 | 1050 |
1051 if ARGS.custom_map == 'None': | |
1052 ARGS.custom_map = None | |
1053 | |
1010 if os.path.isdir(ARGS.output_path) == False: os.makedirs(ARGS.output_path) | 1054 if os.path.isdir(ARGS.output_path) == False: os.makedirs(ARGS.output_path) |
1011 | 1055 |
1012 core_map :ET.ElementTree = ARGS.choice_map.getMap( | 1056 core_map :ET.ElementTree = ARGS.choice_map.getMap( |
1013 ARGS.tool_dir, | 1057 ARGS.tool_dir, |
1014 utils.FilePath.fromStrPath(ARGS.custom_map) if ARGS.custom_map else None) | 1058 utils.FilePath.fromStrPath(ARGS.custom_map) if ARGS.custom_map else None) |