annotate COBRAxy/rps_generator.py @ 4:41f35c2f0c7b draft

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