annotate marea_2_0/rps_generator.py @ 14:2f2c9efdd1cd draft

Uploaded
author bimib
date Thu, 23 May 2024 13:47:20 +0000
parents
children cae2dd2c41e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
1 import re
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
2 import sys
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
3 import csv
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
4 import math
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
5 import argparse
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
6
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
7 import numpy as np
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
8 import pickle as pk
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
9 import pandas as pd
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
10
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
11 from enum import Enum
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
12 from typing import Optional, List, Dict, Tuple
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
13
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
14 import utils.general_utils as utils
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
15 import utils.reaction_parsing as reactionUtils
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
16
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
17 ########################## argparse ##########################################
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
18 ARGS :argparse.Namespace
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
19 def process_args() -> argparse.Namespace:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
20 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
21 Processes command-line arguments.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
22
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
23 Args:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
24 args (list): List of command-line arguments.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
25
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
26 Returns:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
27 Namespace: An object containing parsed arguments.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
28 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
29 parser = argparse.ArgumentParser(usage = '%(prog)s [options]',
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
30 description = 'process some value\'s'+
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
31 ' abundances and reactions to create RPS scores.')
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
32 parser.add_argument('-rc', '--reaction_choice',
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
33 type = str,
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
34 default = 'default',
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
35 choices = ['default','custom'],
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
36 help = 'chose which type of reaction dataset you want use')
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
37 parser.add_argument('-cm', '--custom',
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
38 type = str,
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
39 help='your dataset if you want custom reactions')
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
40 parser.add_argument('-td', '--tool_dir',
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
41 type = str,
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
42 required = True,
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
43 help = 'your tool directory')
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
44 parser.add_argument('-ol', '--out_log',
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
45 help = "Output log")
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
46 parser.add_argument('-id', '--input',
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
47 type = str,
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
48 help = 'input dataset')
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
49 parser.add_argument('-rp', '--rps_output',
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
50 type = str,
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
51 required = True,
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
52 help = 'rps output')
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
53
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
54 args = parser.parse_args()
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
55 return args
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
56
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
57 ############################ dataset name #####################################
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
58 def name_dataset(name_data :str, count :int) -> str:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
59 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
60 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.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
61
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
62 Args:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
63 name_data : name associated with the dataset (from frontend input params)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
64 count : counter from 1 to make these names unique (external)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
65
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
66 Returns:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
67 str : the name made unique
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
68 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
69 if str(name_data) == 'Dataset':
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
70 return str(name_data) + '_' + str(count)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
71 else:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
72 return str(name_data)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
73
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
74
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
75 ############################ get_abund_data ####################################
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
76 def get_abund_data(dataset: pd.DataFrame, cell_line_index:int) -> Optional[pd.Series]:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
77 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
78 Extracts abundance data and turns it into a series for a specific cell line from the dataset, which rows are
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
79 metabolites and columns are cell lines.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
80
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
81 Args:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
82 dataset (pandas.DataFrame): The DataFrame containing abundance data for all cell lines and metabolites.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
83 cell_line_index (int): The index of the cell line of interest in the dataset.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
84
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
85 Returns:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
86 pd.Series or None: A series containing abundance values for the specified cell line.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
87 The name of the series is the name of the cell line.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
88 Returns None if the cell index is invalid.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
89 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
90 if cell_line_index < 0 or cell_line_index >= len(dataset.index):
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
91 print(f"Errore: This cell line index: '{cell_line_index}' is not valid.")
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
92 return None
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
93
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
94 cell_line_name = dataset.columns[cell_line_index]
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
95 abundances_series = dataset[cell_line_name][1:]
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
96
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
97 return abundances_series
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
98
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
99
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
100 ############################ clean_metabolite_name ####################################
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
101 def clean_metabolite_name(name :str) -> str:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
102 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
103 Removes some characters from a metabolite's name, provided as input, and makes it lowercase in order to simplify
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
104 the search of a match in the dictionary of synonyms.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
105
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
106 Args:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
107 name : the metabolite's name, as given in the dataset.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
108
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
109 Returns:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
110 str : a new string with the cleaned name.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
111 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
112 return "".join(ch for ch in name if ch not in ",;-_'([{ }])").lower()
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
113
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
114
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
115 ############################ get_metabolite_id ####################################
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
116 def get_metabolite_id(name :str, syn_dict :Dict[str, List[str]]) -> str:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
117 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
118 Looks through a dictionary of synonyms to find a match for a given metabolite's name.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
119
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
120 Args:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
121 name : the metabolite's name, as given in the dataset.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
122 syn_dict : the dictionary of synonyms, using unique identifiers as keys and lists of clean synonyms as values.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
123
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
124 Returns:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
125 str : the internal :str unique identifier of that metabolite, used in all other parts of the model in use.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
126 An empty string is returned if a match isn't found.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
127 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
128 name = clean_metabolite_name(name)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
129 for id, synonyms in syn_dict.items():
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
130 if name in synonyms: return id
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
131
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
132 return ""
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
133
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
134 ############################ check_missing_metab ####################################
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
135 def check_missing_metab(reactions: Dict[str, Dict[str, int]], dataset_by_rows: Dict[str, List[float]], cell_lines_amt :int) -> List[str]:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
136 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
137 Check for missing metabolites in the abundances dictionary compared to the reactions dictionary and update abundances accordingly.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
138
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
139 Parameters:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
140 reactions (dict): A dictionary representing reactions where keys are reaction names and values are dictionaries containing metabolite names as keys and stoichiometric coefficients as values.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
141 dataset_by_rows (dict): A dictionary representing abundances where keys are metabolite names and values are their corresponding abundances for all cell lines.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
142 cell_lines_amt : amount of cell lines, needed to add a new list of abundances for missing metabolites.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
143
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
144 Returns:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
145 list[str] : list of metabolite names that were missing in the original abundances dictionary and thus their aboundances were set to 1.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
146
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
147 Side effects:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
148 dataset_by_rows : mut
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
149 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
150 missing_list = []
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
151 for reaction in reactions.values():
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
152 for metabolite in reaction.keys():
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
153 if metabolite not in dataset_by_rows:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
154 dataset_by_rows[metabolite] = [1] * cell_lines_amt
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
155 missing_list.append(metabolite)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
156
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
157 return missing_list
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
158
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
159 ############################ calculate_rps ####################################
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
160 def calculate_rps(reactions: Dict[str, Dict[str, int]], abundances: Dict[str, float], black_list: List[str], missing_list: List[str]) -> Dict[str, float]:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
161 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
162 Calculate the Reaction Propensity scores (RPS) based on the availability of reaction substrates, for (ideally) each input model reaction and for each sample.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
163 The score is computed as the product of the concentrations of the reacting substances, with each concentration raised to a power equal to its stoichiometric coefficient
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
164 for each reaction using the provided coefficient and abundance values.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
165
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
166 Parameters:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
167 reactions (dict): A dictionary representing reactions where keys are reaction names and values are dictionaries containing metabolite names as keys and stoichiometric coefficients as values.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
168 abundances (dict): A dictionary representing metabolite abundances where keys are metabolite names and values are their corresponding abundances.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
169 black_list (list): A list containing metabolite names that should be excluded from the RPS calculation.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
170 missing_list (list): A list containing metabolite names that were missing in the original abundances dictionary and thus their values were set to 1.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
171
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
172 Returns:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
173 dict: A dictionary containing Reaction Propensity Scores (RPS) where keys are reaction names and values are the corresponding RPS scores.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
174 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
175 rps_scores = {}
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
176
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
177 for reaction_name, substrates in reactions.items():
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
178 total_contribution = 1
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
179 metab_significant = False
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
180 for metabolite, stoichiometry in substrates.items():
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
181 temp = 1 if math.isnan(abundances[metabolite]) else abundances[metabolite]
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
182 if metabolite not in black_list and metabolite not in missing_list:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
183 metab_significant = True
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
184 total_contribution *= temp ** stoichiometry
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
185
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
186 rps_scores[reaction_name] = total_contribution if metab_significant else math.nan
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
187
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
188 return rps_scores
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
189
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
190
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
191 ############################ rps_for_cell_lines ####################################
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
192 def rps_for_cell_lines(dataset: List[List[str]], reactions: Dict[str, Dict[str, int]], black_list: List[str], syn_dict: Dict[str, List[str]]) -> None:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
193 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
194 Calculate Reaction Propensity Scores (RPS) for each cell line represented in the dataframe and creates an output file.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
195
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
196 Parameters:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
197 dataset : the dataset's data, by rows
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
198 reactions (dict): A dictionary representing reactions where keys are reaction names and values are dictionaries containing metabolite names as keys and stoichiometric coefficients as values.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
199 black_list (list): A list containing metabolite names that should be excluded from the RPS calculation.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
200 syn_dict (dict): A dictionary where keys are general metabolite names and values are lists of possible synonyms.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
201
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
202 Returns:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
203 None
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
204 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
205 cell_lines = dataset[0][1:]
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
206 abundances_dict = {}
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
207
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
208 translationIsApplied = ARGS.reaction_choice == "default"
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
209 for row in dataset[1:]:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
210 id = get_metabolite_id(row[0], syn_dict) if translationIsApplied else row[0]
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
211 if id: abundances_dict[id] = list(map(utils.Float(), row[1:]))
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
212
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
213 missing_list = check_missing_metab(reactions, abundances_dict, len((cell_lines)))
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
214
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
215 rps_scores :Dict[Dict[str, float]] = {}
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
216 for pos, cell_line_name in enumerate(cell_lines):
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
217 abundances = { metab : abundances[pos] for metab, abundances in abundances_dict.items() }
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
218 rps_scores[cell_line_name] = calculate_rps(reactions, abundances, black_list, missing_list)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
219
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
220 pd.DataFrame.from_dict(rps_scores) \
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
221 .rename(columns={'Unnamed: 0': 'Reactions'}, inplace=True) \
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
222 .to_csv(ARGS.rps_output, sep = '\t', na_rep = "None", index = False)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
223
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
224 ############################ main ####################################
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
225 def main() -> None:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
226 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
227 Initializes everything and sets the program in motion based on the fronted input arguments.
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
228
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
229 Returns:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
230 None
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
231 """
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
232 global ARGS
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
233 ARGS = process_args()
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
234
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
235 # TODO:use utils functions vvv
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
236 with open(ARGS.tool_dir + '/local/pickle files/black_list.pickle', 'rb') as bl:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
237 black_list = pk.load(bl)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
238
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
239 with open(ARGS.tool_dir + '/local/pickle files/synonyms.pickle', 'rb') as sd:
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
240 syn_dict = pk.load(sd)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
241
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
242 dataset = utils.readCsv(utils.FilePath.fromStrPath(ARGS.input), '\t', skipHeader = False)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
243
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
244 if ARGS.reaction_choice == 'default':
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
245 reactions = pk.load(open(ARGS.tool_dir + '/local/pickle files/reactions.pickle', 'rb'))
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
246
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
247 elif ARGS.reaction_choice == 'custom':
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
248 reactions = reactionUtils.parse_custom_reactions(ARGS.custom)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
249
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
250 rps_for_cell_lines(dataset, reactions, black_list, syn_dict)
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
251 print('Execution succeded')
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
252
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
253 ##############################################################################
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
254 if __name__ == "__main__":
2f2c9efdd1cd Uploaded
bimib
parents:
diff changeset
255 main()