annotate COBRAxy/ras_to_bounds.py @ 219:264a10b57481 draft

Uploaded
author luca_milaz
date Sat, 14 Dec 2024 18:56:53 +0000
parents 8d1988935e1f
children fa5483499199
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
1 import argparse
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
2 import utils.general_utils as utils
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
3 from typing import Optional, List
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
4 import os
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
5 import numpy as np
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
6 import pandas as pd
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
7 import cobra
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
8 import sys
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
9 import csv
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
10 from joblib import Parallel, delayed, cpu_count
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
11
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
12 ################################# process args ###############################
147
3fca9b568faf Uploaded
bimib
parents: 141
diff changeset
13 def process_args(args :List[str] = None) -> argparse.Namespace:
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
14 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
15 Processes command-line arguments.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
16
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
17 Args:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
18 args (list): List of command-line arguments.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
19
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
20 Returns:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
21 Namespace: An object containing parsed arguments.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
22 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
23 parser = argparse.ArgumentParser(usage = '%(prog)s [options]',
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
24 description = 'process some value\'s')
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
25
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
26 parser.add_argument(
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
27 '-ms', '--model_selector',
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
28 type = utils.Model, default = utils.Model.ENGRO2, choices = [utils.Model.ENGRO2, utils.Model.Custom],
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
29 help = 'chose which type of model you want use')
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
30
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
31 parser.add_argument("-mo", "--model", type = str,
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
32 help = "path to input file with custom rules, if provided")
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
33
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
34 parser.add_argument("-mn", "--model_name", type = str, help = "custom mode name")
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
35
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
36 parser.add_argument(
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
37 '-mes', '--medium_selector',
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
38 default = "allOpen",
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
39 help = 'chose which type of medium you want use')
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
40
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
41 parser.add_argument("-meo", "--medium", type = str,
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
42 help = "path to input file with custom medium, if provided")
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
43
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
44 parser.add_argument('-ol', '--out_log',
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
45 help = "Output log")
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
46
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
47 parser.add_argument('-td', '--tool_dir',
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
48 type = str,
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
49 required = True,
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
50 help = 'your tool directory')
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
51
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
52 parser.add_argument('-ir', '--input_ras',
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
53 type=str,
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
54 required = False,
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
55 help = 'input ras')
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
56
98
54ded7f28a60 Uploaded
luca_milaz
parents: 94
diff changeset
57 parser.add_argument('-rn', '--name',
94
e844f7dab6fe Uploaded
luca_milaz
parents: 93
diff changeset
58 type=str,
e844f7dab6fe Uploaded
luca_milaz
parents: 93
diff changeset
59 help = 'ras class names')
e844f7dab6fe Uploaded
luca_milaz
parents: 93
diff changeset
60
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
61 parser.add_argument('-rs', '--ras_selector',
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
62 required = True,
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
63 type=utils.Bool("using_RAS"),
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
64 help = 'ras selector')
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
65
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
66 parser.add_argument('-cc', '--cell_class',
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
67 type = str,
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
68 help = 'output of cell class')
147
3fca9b568faf Uploaded
bimib
parents: 141
diff changeset
69 parser.add_argument(
3fca9b568faf Uploaded
bimib
parents: 141
diff changeset
70 '-idop', '--output_path',
3fca9b568faf Uploaded
bimib
parents: 141
diff changeset
71 type = str,
3fca9b568faf Uploaded
bimib
parents: 141
diff changeset
72 default='ras_to_bounds/',
3fca9b568faf Uploaded
bimib
parents: 141
diff changeset
73 help = 'output path for maps')
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
74
94
e844f7dab6fe Uploaded
luca_milaz
parents: 93
diff changeset
75
147
3fca9b568faf Uploaded
bimib
parents: 141
diff changeset
76 ARGS = parser.parse_args(args)
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
77 return ARGS
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
78
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
79 ########################### warning ###########################################
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
80 def warning(s :str) -> None:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
81 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
82 Log a warning message to an output log file and print it to the console.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
83
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
84 Args:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
85 s (str): The warning message to be logged and printed.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
86
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
87 Returns:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
88 None
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
89 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
90 with open(ARGS.out_log, 'a') as log:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
91 log.write(s + "\n\n")
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
92 print(s)
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
94 ############################ dataset input ####################################
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
95 def read_dataset(data :str, name :str) -> pd.DataFrame:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
96 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
97 Read a dataset from a CSV file and return it as a pandas DataFrame.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
98
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
99 Args:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
100 data (str): Path to the CSV file containing the dataset.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
101 name (str): Name of the dataset, used in error messages.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
102
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
103 Returns:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
104 pandas.DataFrame: DataFrame containing the dataset.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
105
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
106 Raises:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
107 pd.errors.EmptyDataError: If the CSV file is empty.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
108 sys.exit: If the CSV file has the wrong format, the execution is aborted.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
109 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
110 try:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
111 dataset = pd.read_csv(data, sep = '\t', header = 0, engine='python')
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
112 except pd.errors.EmptyDataError:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
113 sys.exit('Execution aborted: wrong format of ' + name + '\n')
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
114 if len(dataset.columns) < 2:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
115 sys.exit('Execution aborted: wrong format of ' + name + '\n')
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
116 return dataset
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
117
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
118
216
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
119 def apply_ras_bounds(bounds, ras_row):
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
120 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
121 Adjust the bounds of reactions in the model based on RAS values.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
122
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
123 Args:
216
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
124 bounds (pd.DataFrame): Model bounds.
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
125 ras_row (pd.Series): A row from a RAS DataFrame containing scaling factors for reaction bounds.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
126 Returns:
216
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
127 new_bounds (pd.DataFrame): integrated bounds.
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
128 """
216
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
129 new_bounds = bounds.copy()
122
7e1ce04cf192 Uploaded
luca_milaz
parents: 120
diff changeset
130 for reaction in ras_row.index:
7e1ce04cf192 Uploaded
luca_milaz
parents: 120
diff changeset
131 scaling_factor = ras_row[reaction]
216
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
132 lower_bound=bounds.loc[reaction, "lower_bound"]
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
133 upper_bound=bounds.loc[reaction, "upper_bound"]
123
258ff4208e52 Uploaded
luca_milaz
parents: 122
diff changeset
134 valMax=float((upper_bound)*scaling_factor)
258ff4208e52 Uploaded
luca_milaz
parents: 122
diff changeset
135 valMin=float((lower_bound)*scaling_factor)
219
264a10b57481 Uploaded
luca_milaz
parents: 218
diff changeset
136 if(valMax is None or valMin is None):
264a10b57481 Uploaded
luca_milaz
parents: 218
diff changeset
137 warning(f"RAS values for {reaction}is None")
123
258ff4208e52 Uploaded
luca_milaz
parents: 122
diff changeset
138 if upper_bound!=0 and lower_bound==0:
216
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
139 new_bounds.loc[reaction, "upper_bound"] = valMax
123
258ff4208e52 Uploaded
luca_milaz
parents: 122
diff changeset
140 if upper_bound==0 and lower_bound!=0:
216
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
141 new_bounds.loc[reaction, "lower_bound"] = valMin
123
258ff4208e52 Uploaded
luca_milaz
parents: 122
diff changeset
142 if upper_bound!=0 and lower_bound!=0:
216
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
143 new_bounds.loc[reaction, "lower_bound"] = valMin
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
144 new_bounds.loc[reaction, "upper_bound"] = valMax
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
145 return new_bounds
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
146
127
d40d5d5435df Uploaded
luca_milaz
parents: 125
diff changeset
147 def process_ras_cell(cellName, ras_row, model, rxns_ids, output_folder):
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
148 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
149 Process a single RAS cell, apply bounds, and save the bounds to a CSV file.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
150
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
151 Args:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
152 cellName (str): The name of the RAS cell (used for naming the output file).
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
153 ras_row (pd.Series): A row from a RAS DataFrame containing scaling factors for reaction bounds.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
154 model (cobra.Model): The metabolic model to be modified.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
155 rxns_ids (list of str): List of reaction IDs to which the scaling factors will be applied.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
156 output_folder (str): Folder path where the output CSV file will be saved.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
157
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
158 Returns:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
159 None
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
160 """
216
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
161 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"])
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
162 new_bounds = apply_ras_bounds(bounds, ras_row)
218
8d1988935e1f Uploaded
luca_milaz
parents: 216
diff changeset
163 if new_bounds.isnull().values.any():
8d1988935e1f Uploaded
luca_milaz
parents: 216
diff changeset
164 warning(f"RAS values for {cellName} contain NaN values. Skipping this cell.")
219
264a10b57481 Uploaded
luca_milaz
parents: 218
diff changeset
165 return
216
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
166 new_bounds.to_csv(output_folder + cellName + ".csv", sep='\t', index=True)
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
167 pass
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
168
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
169 def generate_bounds(model: cobra.Model, medium: dict, ras=None, output_folder='output/') -> pd.DataFrame:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
170 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
171 Generate reaction bounds for a metabolic model based on medium conditions and optional RAS adjustments.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
172
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
173 Args:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
174 model (cobra.Model): The metabolic model for which bounds will be generated.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
175 medium (dict): A dictionary where keys are reaction IDs and values are the medium conditions.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
176 ras (pd.DataFrame, optional): RAS pandas dataframe. Defaults to None.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
177 output_folder (str, optional): Folder path where output CSV files will be saved. Defaults to 'output/'.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
178
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
179 Returns:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
180 pd.DataFrame: DataFrame containing the bounds of reactions in the model.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
181 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
182 rxns_ids = [rxn.id for rxn in model.reactions]
124
67a79e4f037a Uploaded
luca_milaz
parents: 123
diff changeset
183
127
d40d5d5435df Uploaded
luca_milaz
parents: 125
diff changeset
184 # Set all reactions to zero in the medium
125
a9825d06cc43 Uploaded
luca_milaz
parents: 124
diff changeset
185 for rxn_id, _ in model.medium.items():
124
67a79e4f037a Uploaded
luca_milaz
parents: 123
diff changeset
186 model.reactions.get_by_id(rxn_id).lower_bound = float(0.0)
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
187
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
188 # Set medium conditions
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
189 for reaction, value in medium.items():
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
190 if value is not None:
127
d40d5d5435df Uploaded
luca_milaz
parents: 125
diff changeset
191 model.reactions.get_by_id(reaction).lower_bound = -float(value)
d40d5d5435df Uploaded
luca_milaz
parents: 125
diff changeset
192
107
58919e82d96e Uploaded
luca_milaz
parents: 106
diff changeset
193
120
567ffd4333d4 Uploaded
luca_milaz
parents: 119
diff changeset
194 # Perform Flux Variability Analysis (FVA) on this medium
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
195 df_FVA = cobra.flux_analysis.flux_variability_analysis(model, fraction_of_optimum=0, processes=1).round(8)
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
196
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
197 # Set FVA bounds
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
198 for reaction in rxns_ids:
102
182c710c1660 Uploaded
luca_milaz
parents: 98
diff changeset
199 model.reactions.get_by_id(reaction).lower_bound = float(df_FVA.loc[reaction, "minimum"])
182c710c1660 Uploaded
luca_milaz
parents: 98
diff changeset
200 model.reactions.get_by_id(reaction).upper_bound = float(df_FVA.loc[reaction, "maximum"])
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
201
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
202 if ras is not None:
129
dfb6a1dc6dbc Uploaded
luca_milaz
parents: 127
diff changeset
203 Parallel(n_jobs=cpu_count())(delayed(process_ras_cell)(cellName, ras_row, model, rxns_ids, output_folder) for cellName, ras_row in ras.iterrows())
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
204 else:
216
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
205 bounds = pd.DataFrame([(rxn.lower_bound, rxn.upper_bound) for rxn in model.reactions], index=rxns_ids, columns=["lower_bound", "upper_bound"])
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
206 newBounds = apply_ras_bounds(bounds, pd.Series([1]*len(rxns_ids), index=rxns_ids))
b162b98f9de5 Uploaded
luca_milaz
parents: 209
diff changeset
207 newBounds.to_csv(output_folder + "bounds.csv", sep='\t', index=True)
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
208 pass
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
209
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
210
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
211
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
212 ############################# main ###########################################
147
3fca9b568faf Uploaded
bimib
parents: 141
diff changeset
213 def main(args:List[str] = None) -> None:
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
214 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
215 Initializes everything and sets the program in motion based on the fronted input arguments.
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
216
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
217 Returns:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
218 None
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
219 """
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
220 if not os.path.exists('ras_to_bounds'):
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
221 os.makedirs('ras_to_bounds')
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
222
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
223
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
224 global ARGS
147
3fca9b568faf Uploaded
bimib
parents: 141
diff changeset
225 ARGS = process_args(args)
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
226
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
227 if(ARGS.ras_selector == True):
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
228 ras_file_list = ARGS.input_ras.split(",")
98
54ded7f28a60 Uploaded
luca_milaz
parents: 94
diff changeset
229 ras_file_names = ARGS.name.split(",")
130
03bbbd9af67d Uploaded
luca_milaz
parents: 129
diff changeset
230 if len(ras_file_names) != len(set(ras_file_names)):
03bbbd9af67d Uploaded
luca_milaz
parents: 129
diff changeset
231 error_message = "Duplicated file names in the uploaded RAS matrices."
03bbbd9af67d Uploaded
luca_milaz
parents: 129
diff changeset
232 warning(error_message)
03bbbd9af67d Uploaded
luca_milaz
parents: 129
diff changeset
233 raise ValueError(error_message)
03bbbd9af67d Uploaded
luca_milaz
parents: 129
diff changeset
234 pass
94
e844f7dab6fe Uploaded
luca_milaz
parents: 93
diff changeset
235 ras_class_names = []
e844f7dab6fe Uploaded
luca_milaz
parents: 93
diff changeset
236 for file in ras_file_names:
209
00a66b9bc29e Uploaded
luca_milaz
parents: 147
diff changeset
237 ras_class_names.append(file.rsplit(".", 1)[0])
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
238 ras_list = []
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
239 class_assignments = pd.DataFrame(columns=["Patient_ID", "Class"])
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
240 for ras_matrix, ras_class_name in zip(ras_file_list, ras_class_names):
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
241 ras = read_dataset(ras_matrix, "ras dataset")
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
242 ras.replace("None", None, inplace=True)
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
243 ras.set_index("Reactions", drop=True, inplace=True)
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
244 ras = ras.T
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
245 ras = ras.astype(float)
130
03bbbd9af67d Uploaded
luca_milaz
parents: 129
diff changeset
246 #append class name to patient id (dataframe index)
03bbbd9af67d Uploaded
luca_milaz
parents: 129
diff changeset
247 ras.index = [f"{idx}_{ras_class_name}" for idx in ras.index]
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
248 ras_list.append(ras)
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
249 for patient_id in ras.index:
94
e844f7dab6fe Uploaded
luca_milaz
parents: 93
diff changeset
250 class_assignments.loc[class_assignments.shape[0]] = [patient_id, ras_class_name]
e844f7dab6fe Uploaded
luca_milaz
parents: 93
diff changeset
251
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
252
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
253 # Concatenate all ras DataFrames into a single DataFrame
94
e844f7dab6fe Uploaded
luca_milaz
parents: 93
diff changeset
254 ras_combined = pd.concat(ras_list, axis=0)
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
255 # Normalize the RAS values by max RAS
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
256 ras_combined = ras_combined.div(ras_combined.max(axis=0))
123
258ff4208e52 Uploaded
luca_milaz
parents: 122
diff changeset
257 ras_combined.dropna(axis=1, how='all', inplace=True)
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
258
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
259
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
260
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
261 model_type :utils.Model = ARGS.model_selector
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
262 if model_type is utils.Model.Custom:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
263 model = model_type.getCOBRAmodel(customPath = utils.FilePath.fromStrPath(ARGS.model), customExtension = utils.FilePath.fromStrPath(ARGS.model_name).ext)
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
264 else:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
265 model = model_type.getCOBRAmodel(toolDir=ARGS.tool_dir)
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
266
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
267 if(ARGS.medium_selector == "Custom"):
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
268 medium = read_dataset(ARGS.medium, "medium dataset")
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
269 medium.set_index(medium.columns[0], inplace=True)
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
270 medium = medium.astype(float)
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
271 medium = medium[medium.columns[0]].to_dict()
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
272 else:
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
273 df_mediums = pd.read_csv(ARGS.tool_dir + "/local/medium/medium.csv", index_col = 0)
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
274 ARGS.medium_selector = ARGS.medium_selector.replace("_", " ")
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
275 medium = df_mediums[[ARGS.medium_selector]]
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
276 medium = medium[ARGS.medium_selector].to_dict()
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
277
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
278 if(ARGS.ras_selector == True):
147
3fca9b568faf Uploaded
bimib
parents: 141
diff changeset
279 generate_bounds(model, medium, ras = ras_combined, output_folder=ARGS.output_path)
94
e844f7dab6fe Uploaded
luca_milaz
parents: 93
diff changeset
280 class_assignments.to_csv(ARGS.cell_class, sep = '\t', index = False)
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
281 else:
147
3fca9b568faf Uploaded
bimib
parents: 141
diff changeset
282 generate_bounds(model, medium, output_folder=ARGS.output_path)
93
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
283
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
284 pass
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
285
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
286 ##############################################################################
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
287 if __name__ == "__main__":
7e703e546998 Uploaded
luca_milaz
parents:
diff changeset
288 main()