annotate COBRAxy/utils/ras_to_bounds.py @ 127:d40d5d5435df draft

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