annotate COBRAxy/ras_to_bounds.py @ 63:5302258f8262 draft

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