diff 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
line wrap: on
line diff
--- a/COBRAxy/flux_to_map.py	Mon Jan 13 09:59:48 2025 +0000
+++ b/COBRAxy/flux_to_map.py	Mon Jan 13 10:01:40 2025 +0000
@@ -831,6 +831,41 @@
         Tuple[ClassPat, List[str]]: values and IDs extracted from the dataset
     """
     dataset = read_dataset(datasetPath, datasetName)
+    
+    # Ensure the first column is treated as the reaction name
+    dataset = dataset.set_index(dataset.columns[0])
+
+    # Check if required reactions exist in the dataset
+    required_reactions = ['EX_lac__L_e', 'EX_glc__D_e', 'EX_gln__L_e', 'EX_glu__L_e']
+    missing_reactions = [reaction for reaction in required_reactions if reaction not in dataset.index]
+
+    if missing_reactions:
+        sys.exit(f'Execution aborted: Missing required reactions {missing_reactions} in {datasetName}\n')
+
+    # Calculate new rows using safe division
+    lact_glc = np.divide(
+        dataset.loc['EX_lac__L_e'], -dataset.loc['EX_glc__D_e'], 
+        out=np.zeros_like(dataset.loc['EX_lac__L_e']), where=dataset.loc['EX_glc__D_e'] != 0
+    )
+    lact_gln = np.divide(
+        dataset.loc['EX_lac__L_e'], -dataset.loc['EX_gln__L_e'], 
+        out=np.zeros_like(dataset.loc['EX_lac__L_e']), where=dataset.loc['EX_gln__L_e'] != 0
+    )
+    glu_gln = np.divide(
+        dataset.loc['EX_glu__L_e'], -dataset.loc['EX_gln__L_e'], 
+        out=np.zeros_like(dataset.loc['EX_glu__L_e']), where=dataset.loc['EX_gln__L_e'] != 0
+    )
+
+    # Create a DataFrame for the new rows
+    new_rows = pd.DataFrame({
+        dataset.index.name: ['LactGlc', 'LactGln', 'GluGln'],
+        **{col: [lact_glc[i], lact_gln[i], glu_gln[i]] for i, col in enumerate(dataset.columns)}
+    })
+
+    # Reset the index of the original dataset and append new rows
+    dataset.reset_index(inplace=True)
+    dataset = pd.concat([dataset, new_rows], ignore_index=True)
+
     IDs = pd.Series.tolist(dataset.iloc[:, 0].astype(str))
 
     dataset = dataset.drop(dataset.columns[0], axis = "columns").to_dict("list")
@@ -928,6 +963,9 @@
 
     cmap = plt.get_cmap(colormap)
 
+    min_width = 2.0  # Minimum arrow width
+    max_width = 15.0  # Maximum arrow width
+
     for key in class_pat:
         # Create color mappings for median and mean
         colors_median = {
@@ -941,20 +979,21 @@
         }
 
         for i, rxn_id in enumerate(ids):
+            # Calculate arrow width for median
+            width_median = np.interp(abs(medians[key][i]), [0, 1], [min_width, max_width])
             isNegative = medians[key][i] < 0
+            apply_arrow(metabMap_median, rxn_id, colors_median[rxn_id], isNegative, width_median)
 
-            # Apply median arrows
-            apply_arrow(metabMap_median, rxn_id, colors_median[rxn_id], isNegative)
-
+            # Calculate arrow width for mean
+            width_mean = np.interp(abs(means[key][i]), [0, 1], [min_width, max_width])
             isNegative = means[key][i] < 0
-            # Apply mean arrows
-            apply_arrow(metabMap_mean, rxn_id, colors_mean[rxn_id], isNegative)
+            apply_arrow(metabMap_mean, rxn_id, colors_mean[rxn_id], isNegative, width_mean)
 
         # Save and convert the SVG files
         save_and_convert(metabMap_mean, "mean", key)
         save_and_convert(metabMap_median, "median", key)
 
-def apply_arrow(metabMap, rxn_id, color, isNegative):
+def apply_arrow(metabMap, rxn_id, color, isNegative, width=5):
     """
     Apply an arrow to a specific reaction in the metabolic map with a given color.
 
@@ -962,11 +1001,13 @@
         metabMap (ET.ElementTree): An XML tree representing the metabolic map.
         rxn_id (str): The ID of the reaction to which the arrow will be applied.
         color (str): The color of the arrow in hexadecimal format.
+        isNegative (bool): A boolean indicating if the arrow represents a negative value.
+        width (int): The width of the arrow.
 
     Returns:
         None
     """
-    arrow = Arrow(width=5, col=color)
+    arrow = Arrow(width=width, col=color)
     arrow.styleReactionElementsMeanMedian(metabMap, rxn_id, isNegative)
     pass
 
@@ -1007,6 +1048,9 @@
     global ARGS
     ARGS = process_args(args)
 
+    if ARGS.custom_map == 'None':
+        ARGS.custom_map = None
+
     if os.path.isdir(ARGS.output_path) == False: os.makedirs(ARGS.output_path)
     
     core_map :ET.ElementTree = ARGS.choice_map.getMap(