annotate marea_2/flux_to_map.py @ 253:3a6d10063f06 draft

Uploaded
author luca_milaz
date Sun, 04 Aug 2024 14:08:57 +0000
parents a2adb47e630d
children 3e16bced8062
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
1 from __future__ import division
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
2 import csv
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
3 from enum import Enum
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
4 import re
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
5 import sys
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
6 import numpy as np
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
7 import pandas as pd
253
3a6d10063f06 Uploaded
luca_milaz
parents: 249
diff changeset
8 import matplotlib.pyplot as plt
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
9 import itertools as it
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
10 import scipy.stats as st
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
11 import lxml.etree as ET
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
12 import math
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
13 import utils.general_utils as utils
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
14 from PIL import Image
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
15 import os
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
16 import argparse
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
17 import pyvips
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
18 from typing import Tuple, Union, Optional, List, Dict
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
19
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
20 ERRORS = []
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
21 ########################## argparse ##########################################
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
22 ARGS :argparse.Namespace
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
23 def process_args() -> argparse.Namespace:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
24 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
25 Interfaces the script of a module with its frontend, making the user's choices for various parameters available as values in code.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
26
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
27 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
28 args : Always obtained (in file) from sys.argv
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
29
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
30 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
31 Namespace : An object containing the parsed arguments
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
32 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
33 parser = argparse.ArgumentParser(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
34 usage = "%(prog)s [options]",
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
35 description = "process some value's genes to create a comparison's map.")
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
36
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
37 #General:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
38 parser.add_argument(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
39 '-td', '--tool_dir',
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
40 type = str,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
41 required = True,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
42 help = 'your tool directory')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
43
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
44 parser.add_argument('-on', '--control', type = str)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
45 parser.add_argument('-ol', '--out_log', help = "Output log")
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
46
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
47 #Computation details:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
48 parser.add_argument(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
49 '-co', '--comparison',
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
50 type = str,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
51 default = '1vs1',
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
52 choices = ['manyvsmany', 'onevsrest', 'onevsmany'])
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
53
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
54 parser.add_argument(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
55 '-pv' ,'--pValue',
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
56 type = float,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
57 default = 0.1,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
58 help = 'P-Value threshold (default: %(default)s)')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
59
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
60 parser.add_argument(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
61 '-fc', '--fChange',
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
62 type = float,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
63 default = 1.5,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
64 help = 'Fold-Change threshold (default: %(default)s)')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
65
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
66
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
67 parser.add_argument(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
68 '-op', '--option',
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
69 type = str,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
70 choices = ['datasets', 'dataset_class'],
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
71 help='dataset or dataset and class')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
72
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
73 parser.add_argument(
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
74 '-idf', '--input_data_fluxes',
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
75 type = str,
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
76 help = 'input dataset fluxes')
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
77
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
78 parser.add_argument(
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
79 '-icf', '--input_class_fluxes',
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
80 type = str,
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
81 help = 'sample group specification fluxes')
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
82
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
83 parser.add_argument(
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
84 '-idsf', '--input_datas_fluxes',
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
85 type = str,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
86 nargs = '+',
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
87 help = 'input datasets fluxes')
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
88
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
89 parser.add_argument(
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
90 '-naf', '--names_fluxes',
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
91 type = str,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
92 nargs = '+',
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
93 help = 'input names fluxes')
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
94
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
95 #Output:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
96 parser.add_argument(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
97 "-gs", "--generate_svg",
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
98 type = utils.Bool("generate_svg"), default = True,
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
99 help = "choose whether to generate svg")
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
100
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
101 parser.add_argument(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
102 "-gp", "--generate_pdf",
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
103 type = utils.Bool("generate_pdf"), default = True,
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
104 help = "choose whether to generate pdf")
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
105
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
106 parser.add_argument(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
107 '-cm', '--custom_map',
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
108 type = str,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
109 help='custom map to use')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
110
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
111 parser.add_argument(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
112 '-mc', '--choice_map',
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
113 type = utils.Model, default = utils.Model.HMRcore,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
114 choices = [utils.Model.HMRcore, utils.Model.ENGRO2, utils.Model.Custom])
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
115
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
116 args :argparse.Namespace = parser.parse_args()
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
117 args.net = True
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
118
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
119 return args
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
120
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
121 ############################ dataset input ####################################
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
122 def read_dataset(data :str, name :str) -> pd.DataFrame:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
123 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
124 Tries to read the dataset from its path (data) as a tsv and turns it into a DataFrame.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
125
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
126 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
127 data : filepath of a dataset (from frontend input params or literals upon calling)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
128 name : name associated with the dataset (from frontend input params or literals upon calling)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
129
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
130 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
131 pd.DataFrame : dataset in a runtime operable shape
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
132
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
133 Raises:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
134 sys.exit : if there's no data (pd.errors.EmptyDataError) or if the dataset has less than 2 columns
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
135 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
136 try:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
137 dataset = pd.read_csv(data, sep = '\t', header = 0, engine='python')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
138 except pd.errors.EmptyDataError:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
139 sys.exit('Execution aborted: wrong format of ' + name + '\n')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
140 if len(dataset.columns) < 2:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
141 sys.exit('Execution aborted: wrong format of ' + name + '\n')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
142 return dataset
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
143
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
144 ############################ dataset name #####################################
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
145 def name_dataset(name_data :str, count :int) -> str:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
146 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
147 Produces a unique name for a dataset based on what was provided by the user. The default name for any dataset is "Dataset", thus if the user didn't change it this function appends f"_{count}" to make it unique.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
148
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
149 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
150 name_data : name associated with the dataset (from frontend input params)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
151 count : counter from 1 to make these names unique (external)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
152
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
153 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
154 str : the name made unique
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
155 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
156 if str(name_data) == 'Dataset':
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
157 return str(name_data) + '_' + str(count)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
158 else:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
159 return str(name_data)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
160
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
161 ############################ map_methods ######################################
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
162 FoldChange = Union[float, int, str] # Union[float, Literal[0, "-INF", "INF"]]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
163 def fold_change(avg1 :float, avg2 :float) -> FoldChange:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
164 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
165 Calculates the fold change between two gene expression values.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
166
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
167 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
168 avg1 : average expression value from one dataset avg2 : average expression value from the other dataset
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
169
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
170 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
171 FoldChange :
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
172 0 : when both input values are 0
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
173 "-INF" : when avg1 is 0
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
174 "INF" : when avg2 is 0
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
175 float : for any other combination of values
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
176 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
177 if avg1 == 0 and avg2 == 0:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
178 return 0
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
179 elif avg1 == 0:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
180 return '-INF'
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
181 elif avg2 == 0:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
182 return 'INF'
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
183 else: # (threshold_F_C - 1) / (abs(threshold_F_C) + 1) con threshold_F_C > 1
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
184 return (avg1 - avg2) / (abs(avg1) + abs(avg2))
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
185
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
186 def fix_style(l :str, col :Optional[str], width :str, dash :str) -> str:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
187 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
188 Produces a "fixed" style string to assign to a reaction arrow in the SVG map, assigning style properties to the corresponding values passed as input params.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
189
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
190 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
191 l : current style string of an SVG element
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
192 col : new value for the "stroke" style property
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
193 width : new value for the "stroke-width" style property
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
194 dash : new value for the "stroke-dasharray" style property
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
195
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
196 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
197 str : the fixed style string
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
198 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
199 tmp = l.split(';')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
200 flag_col = False
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
201 flag_width = False
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
202 flag_dash = False
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
203 for i in range(len(tmp)):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
204 if tmp[i].startswith('stroke:'):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
205 tmp[i] = 'stroke:' + col
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
206 flag_col = True
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
207 if tmp[i].startswith('stroke-width:'):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
208 tmp[i] = 'stroke-width:' + width
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
209 flag_width = True
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
210 if tmp[i].startswith('stroke-dasharray:'):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
211 tmp[i] = 'stroke-dasharray:' + dash
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
212 flag_dash = True
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
213 if not flag_col:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
214 tmp.append('stroke:' + col)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
215 if not flag_width:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
216 tmp.append('stroke-width:' + width)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
217 if not flag_dash:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
218 tmp.append('stroke-dasharray:' + dash)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
219 return ';'.join(tmp)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
220
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
221 # The type of d values is collapsed, losing precision, because the dict containst lists instead of tuples, please fix!
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
222 def fix_map(d :Dict[str, List[Union[float, FoldChange]]], core_map :ET.ElementTree, threshold_P_V :float, threshold_F_C :float, max_z_score :float) -> ET.ElementTree:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
223 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
224 Edits the selected SVG map based on the p-value and fold change data (d) and some significance thresholds also passed as inputs.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
225
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
226 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
227 d : dictionary mapping a p-value and a fold-change value (values) to each reaction ID as encoded in the SVG map (keys)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
228 core_map : SVG map to modify
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
229 threshold_P_V : threshold for a p-value to be considered significant
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
230 threshold_F_C : threshold for a fold change value to be considered significant
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
231 max_z_score : highest z-score (absolute value)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
232
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
233 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
234 ET.ElementTree : the modified core_map
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
235
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
236 Side effects:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
237 core_map : mut
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
238 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
239 maxT = 12
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
240 minT = 2
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
241 grey = '#BEBEBE'
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
242 blue = '#6495ed'
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
243 red = '#ecac68'
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
244 for el in core_map.iter():
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
245 el_id = str(el.get('id'))
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
246 if el_id.startswith('R_'):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
247 tmp = d.get(el_id[2:])
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
248 if tmp != None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
249 p_val :float = tmp[0]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
250 f_c = tmp[1]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
251 z_score = tmp[2]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
252 if p_val < threshold_P_V:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
253 if not isinstance(f_c, str):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
254 if abs(f_c) < ((threshold_F_C - 1) / (abs(threshold_F_C) + 1)): #
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
255 col = grey
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
256 width = str(minT)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
257 else:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
258 if f_c < 0:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
259 col = blue
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
260 elif f_c > 0:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
261 col = red
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
262 width = str(max((abs(z_score) * maxT) / max_z_score, minT))
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
263 else:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
264 if f_c == '-INF':
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
265 col = blue
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
266 elif f_c == 'INF':
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
267 col = red
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
268 width = str(maxT)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
269 dash = 'none'
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
270 else:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
271 dash = '5,5'
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
272 col = grey
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
273 width = str(minT)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
274 el.set('style', fix_style(el.get('style', ""), col, width, dash))
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
275 return core_map
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
276
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
277 def getElementById(reactionId :str, metabMap :ET.ElementTree) -> utils.Result[ET.Element, utils.Result.ResultErr]:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
278 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
279 Finds any element in the given map with the given ID. ID uniqueness in an svg file is recommended but
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
280 not enforced, if more than one element with the exact ID is found only the first will be returned.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
281
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
282 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
283 reactionId (str): exact ID of the requested element.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
284 metabMap (ET.ElementTree): metabolic map containing the element.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
285
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
286 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
287 utils.Result[ET.Element, ResultErr]: result of the search, either the first match found or a ResultErr.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
288 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
289 return utils.Result.Ok(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
290 f"//*[@id=\"{reactionId}\"]").map(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
291 lambda xPath : metabMap.xpath(xPath)[0]).mapErr(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
292 lambda _ : utils.Result.ResultErr(f"No elements with ID \"{reactionId}\" found in map"))
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
293 # ^^^ we shamelessly ignore the contents of the IndexError, it offers nothing to the user.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
294
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
295 def styleMapElement(element :ET.Element, styleStr :str) -> None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
296 currentStyles :str = element.get("style", "")
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
297 if re.search(r";stroke:[^;]+;stroke-width:[^;]+;stroke-dasharray:[^;]+$", currentStyles):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
298 currentStyles = ';'.join(currentStyles.split(';')[:-3])
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
299
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
300 element.set("style", currentStyles + styleStr)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
301
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
302 class ReactionDirection(Enum):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
303 Unknown = ""
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
304 Direct = "_F"
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
305 Inverse = "_B"
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
306
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
307 @classmethod
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
308 def fromDir(cls, s :str) -> "ReactionDirection":
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
309 # vvv as long as there's so few variants I actually condone the if spam:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
310 if s == ReactionDirection.Direct.value: return ReactionDirection.Direct
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
311 if s == ReactionDirection.Inverse.value: return ReactionDirection.Inverse
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
312 return ReactionDirection.Unknown
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
313
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
314 @classmethod
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
315 def fromReactionId(cls, reactionId :str) -> "ReactionDirection":
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
316 return ReactionDirection.fromDir(reactionId[-2:])
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
317
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
318 def getArrowBodyElementId(reactionId :str) -> str:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
319 if reactionId.endswith("_RV"): reactionId = reactionId[:-3] #TODO: standardize _RV
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
320 elif ReactionDirection.fromReactionId(reactionId) is not ReactionDirection.Unknown: reactionId = reactionId[:-2]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
321 return f"R_{reactionId}"
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
322
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
323 def getArrowHeadElementId(reactionId :str) -> Tuple[str, str]:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
324 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
325 We attempt extracting the direction information from the provided reaction ID, if unsuccessful we provide the IDs of both directions.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
326
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
327 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
328 reactionId : the provided reaction ID.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
329
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
330 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
331 Tuple[str, str]: either a single str ID for the correct arrow head followed by an empty string or both options to try.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
332 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
333 if reactionId.endswith("_RV"): reactionId = reactionId[:-3] #TODO: standardize _RV
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
334 elif ReactionDirection.fromReactionId(reactionId) is not ReactionDirection.Unknown: return reactionId[:-3:-1] + reactionId[:-2], ""
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
335 return f"F_{reactionId}", f"B_{reactionId}"
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
336
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
337 class ArrowColor(Enum):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
338 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
339 Encodes possible arrow colors based on their meaning in the enrichment process.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
340 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
341 Invalid = "#BEBEBE" # gray, fold-change under treshold
222
a15f5ef34d18 Uploaded
francesco_lapi
parents: 219
diff changeset
342 Transparent = "#ffffff00" # white, not significant p-value
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
343 UpRegulated = "#ecac68" # red, up-regulated reaction
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
344 DownRegulated = "#6495ed" # blue, down-regulated reaction
207
2c5fe2bf1bd3 Uploaded
francesco_lapi
parents: 206
diff changeset
345
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
346 UpRegulatedInv = "#FF0000"
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
347 # ^^^ different shade of red (actually orange), up-regulated net value for a reversible reaction with
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
348 # conflicting enrichment in the two directions.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
349
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
350 DownRegulatedInv = "#0000FF"
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
351 # ^^^ different shade of blue (actually purple), down-regulated net value for a reversible reaction with
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
352 # conflicting enrichment in the two directions.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
353
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
354 @classmethod
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
355 def fromFoldChangeSign(cls, foldChange :float, *, useAltColor = False) -> "ArrowColor":
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
356 colors = (cls.DownRegulated, cls.DownRegulatedInv) if foldChange < 0 else (cls.UpRegulated, cls.UpRegulatedInv)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
357 return colors[useAltColor]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
358
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
359 def __str__(self) -> str: return self.value
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
360
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
361 class Arrow:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
362 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
363 Models the properties of a reaction arrow that change based on enrichment.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
364 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
365 MIN_W = 2
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
366 MAX_W = 12
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
367
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
368 def __init__(self, width :int, col: ArrowColor, *, isDashed = False) -> None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
369 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
370 (Private) Initializes an instance of Arrow.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
371
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
372 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
373 width : width of the arrow, ideally to be kept within Arrow.MIN_W and Arrow.MAX_W (not enforced).
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
374 col : color of the arrow.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
375 isDashed : whether the arrow should be dashed, meaning the associated pValue resulted not significant.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
376
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
377 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
378 None : practically, a Arrow instance.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
379 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
380 self.w = width
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
381 self.col = col
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
382 self.dash = isDashed
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
383
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
384 def applyTo(self, reactionId :str, metabMap :ET.ElementTree, styleStr :str) -> None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
385 if getElementById(reactionId, metabMap).map(lambda el : styleMapElement(el, styleStr)).isErr:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
386 ERRORS.append(reactionId)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
387
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
388 def styleReactionElements(self, metabMap :ET.ElementTree, reactionId :str, *, mindReactionDir = True) -> None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
389 if not mindReactionDir:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
390 return self.applyTo(getArrowBodyElementId(reactionId), metabMap, self.toStyleStr())
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
391
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
392 # Now we style the arrow head(s):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
393 idOpt1, idOpt2 = getArrowHeadElementId(reactionId)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
394 self.applyTo(idOpt1, metabMap, self.toStyleStr(downSizedForTips = True))
211
5081d0bb2a36 Uploaded
francesco_lapi
parents: 210
diff changeset
395 if idOpt2: self.applyTo(idOpt2, metabMap, self.toStyleStr(downSizedForTips = True))
214
27d2d35ddbe8 Uploaded
francesco_lapi
parents: 211
diff changeset
396
27d2d35ddbe8 Uploaded
francesco_lapi
parents: 211
diff changeset
397
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
398
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
399 def getMapReactionId(self, reactionId :str, mindReactionDir :bool) -> str:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
400 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
401 Computes the reaction ID as encoded in the map for a given reaction ID from the dataset.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
402
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
403 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
404 reactionId: the reaction ID, as encoded in the dataset.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
405 mindReactionDir: if True forward (F_) and backward (B_) directions will be encoded in the result.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
406
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
407 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
408 str : the ID of an arrow's body or tips in the map.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
409 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
410 # we assume the reactionIds also don't encode reaction dir if they don't mind it when styling the map.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
411 if not mindReactionDir: return "R_" + reactionId
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
412
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
413 #TODO: this is clearly something we need to make consistent in fluxes
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
414 return (reactionId[:-3:-1] + reactionId[:-2]) if reactionId[:-2] in ["_F", "_B"] else f"F_{reactionId}" # "Pyr_F" --> "F_Pyr"
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
415
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
416 def toStyleStr(self, *, downSizedForTips = False) -> str:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
417 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
418 Collapses the styles of this Arrow into a str, ready to be applied as part of the "style" property on an svg element.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
419
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
420 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
421 str : the styles string.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
422 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
423 width = self.w
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
424 if downSizedForTips: width *= 0.8
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
425 return f";stroke:{self.col};stroke-width:{width};stroke-dasharray:{'5,5' if self.dash else 'none'}"
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
426
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
427 # vvv These constants could be inside the class itself a static properties, but python
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
428 # was built by brainless organisms so here we are!
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
429 INVALID_ARROW = Arrow(Arrow.MIN_W, ArrowColor.Invalid)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
430 INSIGNIFICANT_ARROW = Arrow(Arrow.MIN_W, ArrowColor.Invalid, isDashed = True)
207
2c5fe2bf1bd3 Uploaded
francesco_lapi
parents: 206
diff changeset
431
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
432 def applyFluxesEnrichmentToMap(fluxesEnrichmentRes :Dict[str, Union[Tuple[float, FoldChange], Tuple[float, FoldChange, float, float]]], metabMap :ET.ElementTree, maxNumericZScore :float) -> None:
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
433 """
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
434 Applies fluxes enrichment results to the provided metabolic map.
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
435
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
436 Args:
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
437 fluxesEnrichmentRes : fluxes enrichment results.
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
438 metabMap : the metabolic map to edit.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
439 maxNumericZScore : biggest finite z-score value found.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
440
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
441 Side effects:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
442 metabMap : mut
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
443
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
444 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
445 None
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
446 """
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
447 for reactionId, values in fluxesEnrichmentRes.items():
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
448 pValue = values[0]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
449 foldChange = values[1]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
450 z_score = values[2]
222
a15f5ef34d18 Uploaded
francesco_lapi
parents: 219
diff changeset
451
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
452 if isinstance(foldChange, str): foldChange = float(foldChange)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
453 if pValue >= ARGS.pValue: # pValue above tresh: dashed arrow
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
454 INSIGNIFICANT_ARROW.styleReactionElements(metabMap, reactionId)
211
5081d0bb2a36 Uploaded
francesco_lapi
parents: 210
diff changeset
455 INSIGNIFICANT_ARROW.styleReactionElements(metabMap, reactionId, mindReactionDir = False)
222
a15f5ef34d18 Uploaded
francesco_lapi
parents: 219
diff changeset
456
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
457 continue
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
458
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
459 if abs(foldChange) < (ARGS.fChange - 1) / (abs(ARGS.fChange) + 1):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
460 INVALID_ARROW.styleReactionElements(metabMap, reactionId)
211
5081d0bb2a36 Uploaded
francesco_lapi
parents: 210
diff changeset
461 INVALID_ARROW.styleReactionElements(metabMap, reactionId, mindReactionDir = False)
222
a15f5ef34d18 Uploaded
francesco_lapi
parents: 219
diff changeset
462
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
463 continue
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
464
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
465 width = Arrow.MAX_W
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
466 if not math.isinf(foldChange):
196
4aca24a98a4c Uploaded
francesco_lapi
parents: 195
diff changeset
467 try:
4aca24a98a4c Uploaded
francesco_lapi
parents: 195
diff changeset
468 width = max(abs(z_score * Arrow.MAX_W) / maxNumericZScore, Arrow.MIN_W)
222
a15f5ef34d18 Uploaded
francesco_lapi
parents: 219
diff changeset
469
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
470 except ZeroDivisionError: pass
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
471
203
f2be75f8ef60 Uploaded
francesco_lapi
parents: 199
diff changeset
472 #if not reactionId.endswith("_RV"): # RV stands for reversible reactions
f2be75f8ef60 Uploaded
francesco_lapi
parents: 199
diff changeset
473 # Arrow(width, ArrowColor.fromFoldChangeSign(foldChange)).styleReactionElements(metabMap, reactionId)
f2be75f8ef60 Uploaded
francesco_lapi
parents: 199
diff changeset
474 # continue
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
475
195
1579b38b9c90 Uploaded
francesco_lapi
parents: 193
diff changeset
476 #reactionId = reactionId[:-3] # Remove "_RV"
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
477
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
478 inversionScore = (values[3] < 0) + (values[4] < 0) # Compacts the signs of averages into 1 easy to check score
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
479 if inversionScore == 2: foldChange *= -1
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
480 # ^^^ Style the inverse direction with the opposite sign netValue
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
481
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
482 # If the score is 1 (opposite signs) we use alternative colors vvv
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
483 arrow = Arrow(width, ArrowColor.fromFoldChangeSign(foldChange, useAltColor = inversionScore == 1))
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
484
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
485 # vvv These 2 if statements can both be true and can both happen
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
486 if ARGS.net: # style arrow head(s):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
487 arrow.styleReactionElements(metabMap, reactionId + ("_B" if inversionScore == 2 else "_F"))
222
a15f5ef34d18 Uploaded
francesco_lapi
parents: 219
diff changeset
488 arrow.applyTo(("F_" if inversionScore == 2 else "B_") + reactionId, metabMap, f";stroke:{ArrowColor.Transparent};stroke-width:0;stroke-dasharray:None")
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
489
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
490 arrow.styleReactionElements(metabMap, reactionId, mindReactionDir = False)
222
a15f5ef34d18 Uploaded
francesco_lapi
parents: 219
diff changeset
491
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
492
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
493 ############################ split class ######################################
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
494 def split_class(classes :pd.DataFrame, resolve_rules :Dict[str, List[float]]) -> Dict[str, List[List[float]]]:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
495 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
496 Generates a :dict that groups together data from a :DataFrame based on classes the data is related to.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
497
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
498 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
499 classes : a :DataFrame of only string values, containing class information (rows) and keys to query the resolve_rules :dict
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
500 resolve_rules : a :dict containing :float data
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
501
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
502 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
503 dict : the dict with data grouped by class
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
504
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
505 Side effects:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
506 classes : mut
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
507 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
508 class_pat :Dict[str, List[List[float]]] = {}
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
509 for i in range(len(classes)):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
510 classe :str = classes.iloc[i, 1]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
511 if pd.isnull(classe): continue
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
512
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
513 l :List[List[float]] = []
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
514 for j in range(i, len(classes)):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
515 if classes.iloc[j, 1] == classe:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
516 pat_id :str = classes.iloc[j, 0]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
517 tmp = resolve_rules.get(pat_id, None)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
518 if tmp != None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
519 l.append(tmp)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
520 classes.iloc[j, 1] = None
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
521
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
522 if l:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
523 class_pat[classe] = list(map(list, zip(*l)))
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
524 continue
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
525
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
526 utils.logWarning(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
527 f"Warning: no sample found in class \"{classe}\", the class has been disregarded", ARGS.out_log)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
528
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
529 return class_pat
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
530
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
531 ############################ conversion ##############################################
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
532 #conversion from svg to png
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
533 def svg_to_png_with_background(svg_path :utils.FilePath, png_path :utils.FilePath, dpi :int = 72, scale :int = 1, size :Optional[float] = None) -> None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
534 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
535 Internal utility to convert an SVG to PNG (forced opaque) to aid in PDF conversion.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
536
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
537 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
538 svg_path : path to SVG file
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
539 png_path : path for new PNG file
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
540 dpi : dots per inch of the generated PNG
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
541 scale : scaling factor for the generated PNG, computed internally when a size is provided
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
542 size : final effective width of the generated PNG
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
543
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
544 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
545 None
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
546 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
547 if size:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
548 image = pyvips.Image.new_from_file(svg_path.show(), dpi=dpi, scale=1)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
549 scale = size / image.width
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
550 image = image.resize(scale)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
551 else:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
552 image = pyvips.Image.new_from_file(svg_path.show(), dpi=dpi, scale=scale)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
553
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
554 white_background = pyvips.Image.black(image.width, image.height).new_from_image([255, 255, 255])
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
555 white_background = white_background.affine([scale, 0, 0, scale])
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
556
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
557 if white_background.bands != image.bands:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
558 white_background = white_background.extract_band(0)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
559
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
560 composite_image = white_background.composite2(image, 'over')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
561 composite_image.write_to_file(png_path.show())
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
562
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
563 #funzione unica, lascio fuori i file e li passo in input
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
564 #conversion from png to pdf
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
565 def convert_png_to_pdf(png_file :utils.FilePath, pdf_file :utils.FilePath) -> None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
566 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
567 Internal utility to convert a PNG to PDF to aid from SVG conversion.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
568
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
569 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
570 png_file : path to PNG file
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
571 pdf_file : path to new PDF file
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
572
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
573 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
574 None
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
575 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
576 image = Image.open(png_file.show())
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
577 image = image.convert("RGB")
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
578 image.save(pdf_file.show(), "PDF", resolution=100.0)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
579
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
580 #function called to reduce redundancy in the code
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
581 def convert_to_pdf(file_svg :utils.FilePath, file_png :utils.FilePath, file_pdf :utils.FilePath) -> None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
582 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
583 Converts the SVG map at the provided path to PDF.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
584
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
585 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
586 file_svg : path to SVG file
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
587 file_png : path to PNG file
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
588 file_pdf : path to new PDF file
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
589
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
590 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
591 None
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
592 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
593 svg_to_png_with_background(file_svg, file_png)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
594 try:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
595 convert_png_to_pdf(file_png, file_pdf)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
596 print(f'PDF file {file_pdf.filePath} successfully generated.')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
597
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
598 except Exception as e:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
599 raise utils.DataErr(file_pdf.show(), f'Error generating PDF file: {e}')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
600
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
601 ############################ map ##############################################
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
602 def buildOutputPath(dataset1Name :str, dataset2Name = "rest", *, details = "", ext :utils.FileFormat) -> utils.FilePath:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
603 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
604 Builds a FilePath instance from the names of confronted datasets ready to point to a location in the
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
605 "result/" folder, used by this tool for output files in collections.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
606
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
607 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
608 dataset1Name : _description_
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
609 dataset2Name : _description_. Defaults to "rest".
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
610 details : _description_
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
611 ext : _description_
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
612
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
613 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
614 utils.FilePath : _description_
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
615 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
616 # This function returns a util data structure but is extremely specific to this module.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
617 # RAS also uses collections as output and as such might benefit from a method like this, but I'd wait
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
618 # TODO: until a third tool with multiple outputs appears before porting this to utils.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
619 return utils.FilePath(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
620 f"{dataset1Name}_vs_{dataset2Name}" + (f" ({details})" if details else ""),
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
621 # ^^^ yes this string is built every time even if the form is the same for the same 2 datasets in
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
622 # all output files: I don't care, this was never the performance bottleneck of the tool and
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
623 # there is no other net gain in saving and re-using the built string.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
624 ext,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
625 prefix = "result")
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
626
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
627 FIELD_NOT_AVAILABLE = '/'
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
628 def writeToCsv(rows: List[list], fieldNames :List[str], outPath :utils.FilePath) -> None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
629 fieldsAmt = len(fieldNames)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
630 with open(outPath.show(), "w", newline = "") as fd:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
631 writer = csv.DictWriter(fd, fieldnames = fieldNames, delimiter = '\t')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
632 writer.writeheader()
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
633
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
634 for row in rows:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
635 sizeMismatch = fieldsAmt - len(row)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
636 if sizeMismatch > 0: row.extend([FIELD_NOT_AVAILABLE] * sizeMismatch)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
637 writer.writerow({ field : data for field, data in zip(fieldNames, row) })
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
638
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
639 OldEnrichedScores = Dict[str, List[Union[float, FoldChange]]] #TODO: try to use Tuple whenever possible
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
640 def writeTabularResult(enrichedScores : OldEnrichedScores, outPath :utils.FilePath) -> None:
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
641 fieldNames = ["ids", "P_Value", "fold change"]
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
642 fieldNames.extend(["average_1", "average_2"])
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
643
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
644 writeToCsv([ [reactId] + values for reactId, values in enrichedScores.items() ], fieldNames, outPath)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
645
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
646 def temp_thingsInCommon(tmp :Dict[str, List[Union[float, FoldChange]]], core_map :ET.ElementTree, max_z_score :float, dataset1Name :str, dataset2Name = "rest") -> None:
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
647 # this function compiles the things always in common between comparison modes after enrichment.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
648 # TODO: organize, name better.
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
649 writeTabularResult(tmp, buildOutputPath(dataset1Name, dataset2Name, details = "Tabular Result", ext = utils.FileFormat.TSV))
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
650 for reactId, enrichData in tmp.items(): tmp[reactId] = tuple(enrichData)
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
651 applyFluxesEnrichmentToMap(tmp, core_map, max_z_score)
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
652
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
653 def computePValue(dataset1Data: List[float], dataset2Data: List[float]) -> Tuple[float, float]:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
654 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
655 Computes the statistical significance score (P-value) of the comparison between coherent data
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
656 from two datasets. The data is supposed to, in both datasets:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
657 - be related to the same reaction ID;
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
658 - be ordered by sample, such that the item at position i in both lists is related to the
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
659 same sample or cell line.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
660
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
661 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
662 dataset1Data : data from the 1st dataset.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
663 dataset2Data : data from the 2nd dataset.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
664
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
665 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
666 tuple: (P-value, Z-score)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
667 - P-value from a Kolmogorov-Smirnov test on the provided data.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
668 - Z-score of the difference between means of the two datasets.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
669 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
670 # Perform Kolmogorov-Smirnov test
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
671 ks_statistic, p_value = st.ks_2samp(dataset1Data, dataset2Data)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
672
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
673 # Calculate means and standard deviations
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
674 mean1 = np.mean(dataset1Data)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
675 mean2 = np.mean(dataset2Data)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
676 std1 = np.std(dataset1Data, ddof=1)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
677 std2 = np.std(dataset2Data, ddof=1)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
678
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
679 n1 = len(dataset1Data)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
680 n2 = len(dataset2Data)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
681
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
682 # Calculate Z-score
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
683 z_score = (mean1 - mean2) / np.sqrt((std1**2 / n1) + (std2**2 / n2))
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
684
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
685 return p_value, z_score
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
686
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
687 def compareDatasetPair(dataset1Data :List[List[float]], dataset2Data :List[List[float]], ids :List[str]) -> Tuple[Dict[str, List[Union[float, FoldChange]]], float]:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
688 #TODO: the following code still suffers from "dumbvarnames-osis"
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
689 tmp :Dict[str, List[Union[float, FoldChange]]] = {}
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
690 count = 0
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
691 max_z_score = 0
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
692
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
693 for l1, l2 in zip(dataset1Data, dataset2Data):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
694 reactId = ids[count]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
695 count += 1
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
696 if not reactId: continue # we skip ids that have already been processed
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
697
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
698 try:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
699 p_value, z_score = computePValue(l1, l2)
204
4f40b848d55a Uploaded
francesco_lapi
parents: 203
diff changeset
700 avg1 = sum(l1) / len(l1)
4f40b848d55a Uploaded
francesco_lapi
parents: 203
diff changeset
701 avg2 = sum(l2) / len(l2)
4f40b848d55a Uploaded
francesco_lapi
parents: 203
diff changeset
702 avg = fold_change(avg1, avg2)
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
703 if not isinstance(z_score, str) and max_z_score < abs(z_score): max_z_score = abs(z_score)
204
4f40b848d55a Uploaded
francesco_lapi
parents: 203
diff changeset
704 tmp[reactId] = [float(p_value), avg, z_score, avg1, avg2]
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
705 except (TypeError, ZeroDivisionError): continue
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
706
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
707 return tmp, max_z_score
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
708
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
709 def computeEnrichment(metabMap :ET.ElementTree, class_pat :Dict[str, List[List[float]]], ids :List[str]) -> None:
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
710 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
711 Compares clustered data based on a given comparison mode and applies enrichment-based styling on the
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
712 provided metabolic map.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
713
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
714 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
715 metabMap : SVG map to modify.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
716 class_pat : the clustered data.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
717 ids : ids for data association.
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
718
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
719
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
720 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
721 None
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
722
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
723 Raises:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
724 sys.exit : if there are less than 2 classes for comparison
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
725
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
726 Side effects:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
727 metabMap : mut
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
728 ids : mut
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
729 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
730 class_pat = { k.strip() : v for k, v in class_pat.items() }
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
731 #TODO: simplfy this stuff vvv and stop using sys.exit (raise the correct utils error)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
732 if (not class_pat) or (len(class_pat.keys()) < 2): sys.exit('Execution aborted: classes provided for comparisons are less than two\n')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
733
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
734 if ARGS.comparison == "manyvsmany":
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
735 for i, j in it.combinations(class_pat.keys(), 2):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
736 #TODO: these 2 functions are always called in pair and in this order and need common data,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
737 # some clever refactoring would be appreciated.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
738 comparisonDict, max_z_score = compareDatasetPair(class_pat.get(i), class_pat.get(j), ids)
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
739 temp_thingsInCommon(comparisonDict, metabMap, max_z_score, i, j)
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
740
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
741 elif ARGS.comparison == "onevsrest":
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
742 for single_cluster in class_pat.keys():
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
743 t :List[List[List[float]]] = []
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
744 for k in class_pat.keys():
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
745 if k != single_cluster:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
746 t.append(class_pat.get(k))
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
747
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
748 rest :List[List[float]] = []
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
749 for i in t:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
750 rest = rest + i
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
751
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
752 comparisonDict, max_z_score = compareDatasetPair(class_pat.get(single_cluster), rest, ids)
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
753 temp_thingsInCommon(comparisonDict, metabMap, max_z_score, single_cluster)
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
754
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
755 elif ARGS.comparison == "onevsmany":
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
756 controlItems = class_pat.get(ARGS.control)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
757 for otherDataset in class_pat.keys():
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
758 if otherDataset == ARGS.control: continue
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
759
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
760 comparisonDict, max_z_score = compareDatasetPair(controlItems, class_pat.get(otherDataset), ids)
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
761 temp_thingsInCommon(comparisonDict, metabMap, max_z_score, ARGS.control, otherDataset)
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
762
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
763 def createOutputMaps(dataset1Name :str, dataset2Name :str, core_map :ET.ElementTree) -> None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
764 svgFilePath = buildOutputPath(dataset1Name, dataset2Name, details = "SVG Map", ext = utils.FileFormat.SVG)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
765 utils.writeSvg(svgFilePath, core_map)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
766
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
767 if ARGS.generate_pdf:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
768 pngPath = buildOutputPath(dataset1Name, dataset2Name, details = "PNG Map", ext = utils.FileFormat.PNG)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
769 pdfPath = buildOutputPath(dataset1Name, dataset2Name, details = "PDF Map", ext = utils.FileFormat.PDF)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
770 convert_to_pdf(svgFilePath, pngPath, pdfPath)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
771
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
772 if not ARGS.generate_svg: os.remove(svgFilePath.show())
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
773
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
774 ClassPat = Dict[str, List[List[float]]]
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
775 def getClassesAndIdsFromDatasets(datasetsPaths :List[str], datasetPath :str, classPath :str, names :List[str]) -> Tuple[List[str], ClassPat]:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
776 # TODO: I suggest creating dicts with ids as keys instead of keeping class_pat and ids separate,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
777 # for the sake of everyone's sanity.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
778 class_pat :ClassPat = {}
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
779 if ARGS.option == 'datasets':
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
780 num = 1 #TODO: the dataset naming function could be a generator
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
781 for path, name in zip(datasetsPaths, names):
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
782 name = name_dataset(name, num)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
783 resolve_rules_float, ids = getDatasetValues(path, name)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
784 if resolve_rules_float != None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
785 class_pat[name] = list(map(list, zip(*resolve_rules_float.values())))
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
786
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
787 num += 1
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
788
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
789 elif ARGS.option == "dataset_class":
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
790 classes = read_dataset(classPath, "class")
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
791 classes = classes.astype(str)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
792
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
793 resolve_rules_float, ids = getDatasetValues(datasetPath, "Dataset Class (not actual name)")
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
794 if resolve_rules_float != None: class_pat = split_class(classes, resolve_rules_float)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
795
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
796 return ids, class_pat
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
797 #^^^ TODO: this could be a match statement over an enum, make it happen future marea dev with python 3.12! (it's why I kept the ifs)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
798
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
799 #TODO: create these damn args as FilePath objects
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
800 def getDatasetValues(datasetPath :str, datasetName :str) -> Tuple[ClassPat, List[str]]:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
801 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
802 Opens the dataset at the given path and extracts the values (expected nullable numerics) and the IDs.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
803
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
804 Args:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
805 datasetPath : path to the dataset
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
806 datasetName (str): dataset name, used in error reporting
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
807
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
808 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
809 Tuple[ClassPat, List[str]]: values and IDs extracted from the dataset
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
810 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
811 dataset = read_dataset(datasetPath, datasetName)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
812 IDs = pd.Series.tolist(dataset.iloc[:, 0].astype(str))
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
813
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
814 dataset = dataset.drop(dataset.columns[0], axis = "columns").to_dict("list")
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
815 return { id : list(map(utils.Float("Dataset values, not an argument"), values)) for id, values in dataset.items() }, IDs
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
816
249
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
817 def rgba_to_hex(rgba):
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
818 """Convert an RGBA color to HEX format."""
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
819 return '#{:02x}{:02x}{:02x}'.format(int(rgba[0] * 255), int(rgba[1] * 255), int(rgba[2] * 255))
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
820
246
caf79d752eb4 Uploaded
luca_milaz
parents: 244
diff changeset
821 def computeEnrichmentMedoids(metabMap :ET.ElementTree, class_pat :Dict[str, List[List[float]]], ids :List[str]) -> None:
249
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
822
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
823 metabMap_mean = metabMap.copy()
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
824 metabMap_median = metabMap.copy()
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
825
248
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
826 #class_pat 462 * cellule
246
caf79d752eb4 Uploaded
luca_milaz
parents: 244
diff changeset
827 utils.logWarning(
248
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
828 ids,
246
caf79d752eb4 Uploaded
luca_milaz
parents: 244
diff changeset
829 ARGS.out_log)
248
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
830 medians = {}
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
831 means = {}
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
832 for key, value in class_pat.items():
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
833 values = np.array(class_pat[key])
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
834 median = np.median(values, axis=1)
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
835 mean = np.mean(values, axis=1)
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
836 medians[key] = median
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
837 means[key] = means
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
838
249
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
839 max_flux_medians = max(np.max(np.abs(arr)) for arr in medians.values())
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
840 max_flux_means = max(np.max(np.abs(arr)) for arr in means.values())
248
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
841
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
842 for key, value in medians.items():
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
843 medians[key] = medians[key] / max_flux_medians
247
6c3e7c338b93 Uploaded
luca_milaz
parents: 246
diff changeset
844
248
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
845 for key, value in means.items():
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
846 means[key] = means[key] / max_flux_means
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
847
249
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
848 cmap = plt.cm.Reds
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
849 norm = plt.Normalize(vmin=0, vmax=1)
248
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
850
249
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
851 colors_median_rgb = {k: cmap(norm(v)) for k, v in medians.items()}
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
852 colors_median = {k: rgba_to_hex(c) for k, c in colors_median_rgb.items()}
248
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
853
249
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
854 colors_mean_rgb = {k: cmap(norm(v)) for k, v in means.items()}
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
855 colors_mean = {k: rgba_to_hex(c) for k, c in colors_mean_rgb.items()}
248
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
856
249
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
857 for rxn_id in ids:
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
858 arrow = Arrow(width=5, col=colors_median[rxn_id])
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
859 arrow.applyTo(rxn_id, metabMap_median, arrow.toStyleStr())
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
860
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
861 svgFilePath = utils.FilePath(details = "SVG Map median", ext = utils.FileFormat.SVG, prefix="result")
a2adb47e630d Uploaded
luca_milaz
parents: 248
diff changeset
862 utils.writeSvg(svgFilePath, metabMap_median)
247
6c3e7c338b93 Uploaded
luca_milaz
parents: 246
diff changeset
863 pass
6c3e7c338b93 Uploaded
luca_milaz
parents: 246
diff changeset
864
248
419c6bfad590 Uploaded
luca_milaz
parents: 247
diff changeset
865
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
866 ############################ MAIN #############################################
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
867 def main() -> None:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
868 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
869 Initializes everything and sets the program in motion based on the fronted input arguments.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
870
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
871 Returns:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
872 None
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
873
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
874 Raises:
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
875 sys.exit : if a user-provided custom map is in the wrong format (ET.XMLSyntaxError, ET.XMLSchemaParseError)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
876 """
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
877
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
878 global ARGS
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
879 ARGS = process_args()
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
880
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
881 if os.path.isdir('result') == False: os.makedirs('result')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
882
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
883 core_map :ET.ElementTree = ARGS.choice_map.getMap(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
884 ARGS.tool_dir,
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
885 utils.FilePath.fromStrPath(ARGS.custom_map) if ARGS.custom_map else None)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
886 # TODO: ^^^ ugly but fine for now, the argument is None if the model isn't custom because no file was given.
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
887 # getMap will None-check the customPath and panic when the model IS custom but there's no file (good). A cleaner
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
888 # solution can be derived from my comment in FilePath.fromStrPath
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
889
241
1058a2803472 Uploaded
luca_milaz
parents: 222
diff changeset
890 ids, class_pat = getClassesAndIdsFromDatasets(ARGS.input_datas_fluxes, ARGS.input_data_fluxes, ARGS.input_class_fluxes, ARGS.names_fluxes)
246
caf79d752eb4 Uploaded
luca_milaz
parents: 244
diff changeset
891
caf79d752eb4 Uploaded
luca_milaz
parents: 244
diff changeset
892 computeEnrichmentMedoids(core_map, class_pat, ids)
caf79d752eb4 Uploaded
luca_milaz
parents: 244
diff changeset
893
caf79d752eb4 Uploaded
luca_milaz
parents: 244
diff changeset
894 pass
caf79d752eb4 Uploaded
luca_milaz
parents: 244
diff changeset
895
244
f1754122f37d Uploaded
luca_milaz
parents: 241
diff changeset
896 computeEnrichment(core_map, class_pat, ids)
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
897
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
898 # create output files: TODO: this is the same comparison happening in "maps", find a better way to organize this
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
899 if ARGS.comparison == "manyvsmany":
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
900 for i, j in it.combinations(class_pat.keys(), 2): createOutputMaps(i, j, core_map)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
901 return
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
902
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
903 if ARGS.comparison == "onevsrest":
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
904 for single_cluster in class_pat.keys(): createOutputMaps(single_cluster, "rest", core_map)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
905 return
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
906
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
907 for otherDataset in class_pat.keys():
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
908 if otherDataset != ARGS.control: createOutputMaps(i, j, core_map)
199
2dbc0842124b Uploaded
francesco_lapi
parents: 198
diff changeset
909
193
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
910 if not ERRORS: return
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
911 utils.logWarning(
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
912 f"The following reaction IDs were mentioned in the dataset but weren't found in the map: {ERRORS}",
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
913 ARGS.out_log)
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
914
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
915 print('Execution succeded')
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
916
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
917 ###############################################################################
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
918 if __name__ == "__main__":
560aede85627 Uploaded
francesco_lapi
parents:
diff changeset
919 main()