From 22a72417684400b6d4754d2eb60be7f5b9a0320c Mon Sep 17 00:00:00 2001
From: local_comparaison <mathieu.umec@inrae.fr>
Date: Mon, 15 Jan 2024 14:53:24 +0100
Subject: [PATCH] adjust for CPDB visu

---
 Visualisation_des_donnes_de_mapping.py    |  9 +--
 complete_processing_of_mapping_results.py | 71 ++++++++++++++++-------
 2 files changed, 55 insertions(+), 25 deletions(-)

diff --git a/Visualisation_des_donnes_de_mapping.py b/Visualisation_des_donnes_de_mapping.py
index 769b37f..cd041bd 100644
--- a/Visualisation_des_donnes_de_mapping.py
+++ b/Visualisation_des_donnes_de_mapping.py
@@ -138,7 +138,7 @@ def up_down_path_plot(l_path, up, down, log_p):
 
     return : 1 plot of regulation pathways
     """
-    fig, ax1 = plt.subplots()
+    fig, ax1 = plt.subplots(figsize=(22, 14))
     plt.subplots_adjust(top=0.95, bottom=0.30)
     l_bar = 0.8
     x = range(len(down))
@@ -152,10 +152,10 @@ def up_down_path_plot(l_path, up, down, log_p):
     ax2.set_xlabel("Pathways")
     ax2.set_ylabel("-log(pvalue)")
     plt.ylim(bottom = 0, top=max(log_p)+0.5)
-    fig.legend(loc='upper right', bbox_to_anchor=(0.9, 0.88))
+    fig.legend(loc='upper right', bbox_to_anchor=(0.9, 0.95))
     ax1.set_xticks(l_path)
     ax1.set_xticklabels(l_path, size=6.5, rotation=-90, ha='center')
-    plt.show()
+    return(fig)
 
 
 if __name__ == "__main__":
@@ -163,7 +163,8 @@ if __name__ == "__main__":
     UP = [5, 8, 21]
     DOWN = [41, 13, 9]
     LOG_P = [13.61, 7.96, 4.64]
-    up_down_path_plot(L_PATH, UP, DOWN, LOG_P)
+    figg = up_down_path_plot(L_PATH, UP, DOWN, LOG_P)
+    plt.show()
 
 
 
diff --git a/complete_processing_of_mapping_results.py b/complete_processing_of_mapping_results.py
index 2c45122..8c30fde 100644
--- a/complete_processing_of_mapping_results.py
+++ b/complete_processing_of_mapping_results.py
@@ -8,7 +8,7 @@ import matplotlib.pyplot as plt
 import seaborn as sns
 import numpy as np
 import pandas as pd
-from math import log
+from math import log, floor
 import sys
 sys.path.append('C:\\Users\\mumec\\Desktop\\Dossier_gitlab_local\\traitement_des_données')
 from Visualisation_des_donnes_de_mapping import up_down_path_plot
@@ -91,6 +91,9 @@ def recup_ramp_pathways_list(ramp_mapping_result, correspondence_file):
     return l_to_return
 
 
+def recup_ramp_enrichment_pathways_list(ramp_mapping_result, correspondence_file):
+
+
 def recup_cpdb_pathways_list(cpdb_mapping_result, correspondence_file):
     """
     Give a list of pathways with the correspondent metabolites names
@@ -108,6 +111,8 @@ def recup_cpdb_pathways_list(cpdb_mapping_result, correspondence_file):
     l_pathways = l_pathways[1:]
     l_path_metabo_whith_top = column_recovery(cpdb_mapping_result, 5)
     l_path_metabo = l_path_metabo_whith_top[1:]
+    p_value = column_recovery(cpdb_mapping_result, 0)
+    m_inp_ol = column_recovery(cpdb_mapping_result, 8)
     l_to_return = []
     for num_path_t, l_p_m in enumerate(l_path_metabo):
         path_cont = []
@@ -130,6 +135,8 @@ def recup_cpdb_pathways_list(cpdb_mapping_result, correspondence_file):
             path_cont. append(str(l_p_m))
         paths_to_rec = cor_index(path_cont, associated_chebi, associated_name)
         paths_to_rec.insert(0, l_pathways[num_path_t])
+        paths_to_rec.insert(0, m_inp_ol[num_path_t + 1])
+        paths_to_rec.insert(0, p_value[num_path_t + 1])
         l_to_return.append(paths_to_rec)
     return l_to_return
 
@@ -398,25 +405,14 @@ def c_p_o_m_r(file, outf, mapper, type_of_view="all", save_plot="all",
     else:
         n_c = int(input('how many columns are there in the file?'))
         l_of_pathways_list = recup_ma_pathways_list(file, n_c)
-
-
-
-
-
-
-
-
-
-
-
-
-    if modul != None:
+    if modul == True:
         list_path = []
         up = []
         down = []
         log_p = []
         metabo = column_recovery(f_modul, 0)
         value_modul = column_recovery(f_modul, 1)
+    #print(l_of_pathways_list)
     for i_p_l, path_l in enumerate(l_of_pathways_list):
         l_of_pathways_list[i_p_l][2] = comma_cleaning(path_l[2])
         if modul == True:
@@ -425,15 +421,43 @@ def c_p_o_m_r(file, outf, mapper, type_of_view="all", save_plot="all",
             list_path.append(l_of_pathways_list[i_p_l][2])
             for path_meta in path_l[3:]:
                 # print(comma_cleaning(path_meta)) Probable probléme de version entre ME et les autres (a vériifer)
-                if float(value_modul[metabo.index(comma_cleaning(path_meta))]) >= 0:
-                    actu_up += 1
+                #print(path_meta)
+                if mapper == "ME":
+                    if float(value_modul[metabo.index(comma_cleaning(path_meta))]) >= 0:
+                        actu_up += 1
+                    else:
+                        actu_down += 1
                 else:
-                    actu_down += 1
-            up.append((actu_up/path_l[1])*100)
-            down.append((actu_down/path_l[1])*100)
-            log_p.append(-log(path_l[0]))
+                    if float(value_modul[metabo.index(path_meta)]) >= 0:
+                        actu_up += 1
+                    else:
+                        actu_down += 1
+            up.append((actu_up/int(path_l[1]))*100)
+            down.append((actu_down/int(path_l[1]))*100)
+            log_p.append(-log(float(path_l[0])))
     if modul == True:
-        up_down_path_plot(list_path, up, down, log_p)
+        n_m_i_p = 200
+        if len(log_p) > n_m_i_p:
+            print(len(log_p))
+            under_plot = floor(len(log_p)/n_m_i_p)
+            print(floor(len(log_p)/n_m_i_p))
+            for plot in range(under_plot):
+                up_u_p = up[n_m_i_p*plot:n_m_i_p*plot+n_m_i_p]
+                down_u_p = down[n_m_i_p*plot:n_m_i_p*plot+n_m_i_p]
+                log_p_u_p = log_p[n_m_i_p*plot:n_m_i_p*plot+n_m_i_p]
+                list_path_u_p = list_path[n_m_i_p*plot:n_m_i_p*plot+n_m_i_p]
+                plot_u = up_down_path_plot(list_path_u_p, up_u_p, down_u_p, log_p_u_p)
+                plt.savefig(fold_of_visu_sav+"up_down_path_plot"+str(plot)+".png")
+                #plt.show()
+            up_u_p = up[n_m_i_p*plot+n_m_i_p:]
+            down_u_p = down[n_m_i_p*plot+n_m_i_p:]
+            log_p_u_p = log_p[n_m_i_p*plot+n_m_i_p:]
+            list_path_u_p = list_path[n_m_i_p*plot+n_m_i_p:]
+            plot_u = up_down_path_plot(list_path_u_p, up_u_p, down_u_p, log_p_u_p)
+            plt.savefig(fold_of_visu_sav+"up_down_path_plot"+str(under_plot + 1)+".png")
+        else :
+            plot = up_down_path_plot(list_path, up, down, log_p)
+            plt.savefig(fold_of_visu_sav+"up_down_path_plot.png")
     if midfile == "Yes":
         mid_data = pd.DataFrame(l_of_pathways_list, dtype=object)
         excel_file_writer(mid_data, midfile_name, sheetname="Resultats")
@@ -591,10 +615,15 @@ def c_p_o_m_r(file, outf, mapper, type_of_view="all", save_plot="all",
 
 
 if __name__ == "__main__":
+    #MAP = 'RAMP'
+    #MAP = "CPDB"
     MAP = "ME"
     VIEW = "all"
     SAVE = "no"
+    #INFILE = LOCAL + "CPDB\\Resultats_mapping_Chebi_ID_L100_CPDB.csv"
     INFILE = "ExportExcel_6843"
+    #INFILE = LOCAL + "RAMP\\sortie_Mapping_RAMP_L100_CheEBI.csv"
     FINISHFILE = LOCAL + "test.xlsx"
     FILE_MODUL = LOCAL + "L100_modulation_artificielle_HumanCyc.csv"
+    #FILE_MODUL = LOCAL + "CPDB\\liste_Chebi_des_100_chebi_ConsensusPAthDB_modul.csv"
     c_p_o_m_r(INFILE, FINISHFILE, MAP, type_of_view=VIEW, save_plot=SAVE, modul=True, f_modul=FILE_MODUL)
-- 
GitLab