annotate cobraxy-9688ad27287b/COBRAxy/ras_generator.py @ 91:f4f93df8c221 draft

Uploaded
author luca_milaz
date Sun, 13 Oct 2024 11:38:28 +0000
parents a48b2e06ebe7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
90
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
1 from __future__ import division
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
2 # galaxy complains this ^^^ needs to be at the very beginning of the file, for some reason.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
3 import sys
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
4 import argparse
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
5 import collections
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
6 import pandas as pd
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
7 import pickle as pk
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
8 import utils.general_utils as utils
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
9 import utils.rule_parsing as ruleUtils
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
10 from typing import Union, Optional, List, Dict, Tuple, TypeVar
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
11
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
12 ERRORS = []
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
13 ########################## argparse ##########################################
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
14 ARGS :argparse.Namespace
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
15 def process_args() -> argparse.Namespace:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
16 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
17 Processes command-line arguments.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
18
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
19 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
20 args (list): List of command-line arguments.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
21
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
22 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
23 Namespace: An object containing parsed arguments.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
24 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
25 parser = argparse.ArgumentParser(
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
26 usage = '%(prog)s [options]',
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
27 description = "process some value's genes to create a comparison's map.")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
28
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
29 parser.add_argument(
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
30 '-rs', '--rules_selector',
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
31 type = utils.Model, default = utils.Model.HMRcore, choices = list(utils.Model),
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
32 help = 'chose which type of dataset you want use')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
33
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
34 parser.add_argument("-rl", "--rule_list", type = str,
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
35 help = "path to input file with custom rules, if provided")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
36
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
37 parser.add_argument("-rn", "--rules_name", type = str, help = "custom rules name")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
38 # ^ I need this because galaxy converts my files into .dat but I need to know what extension they were in
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
39
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
40 parser.add_argument(
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
41 '-n', '--none',
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
42 type = utils.Bool("none"), default = True,
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
43 help = 'compute Nan values')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
44
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
45 parser.add_argument(
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
46 '-td', '--tool_dir',
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
47 type = str,
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
48 required = True, help = 'your tool directory')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
49
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
50 parser.add_argument(
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
51 '-ol', '--out_log',
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
52 type = str,
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
53 help = "Output log")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
54
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
55 parser.add_argument(
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
56 '-in', '--input', #id รจ diventato in
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
57 type = str,
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
58 help = 'input dataset')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
59
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
60 parser.add_argument(
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
61 '-ra', '--ras_output',
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
62 type = str,
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
63 required = True, help = 'ras output')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
64
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
65 return parser.parse_args()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
66
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
67 ############################ dataset input ####################################
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
68 def read_dataset(data :str, name :str) -> pd.DataFrame:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
69 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
70 Read a dataset from a CSV file and return it as a pandas DataFrame.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
71
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
72 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
73 data (str): Path to the CSV file containing the dataset.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
74 name (str): Name of the dataset, used in error messages.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
75
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
76 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
77 pandas.DataFrame: DataFrame containing the dataset.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
78
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
79 Raises:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
80 pd.errors.EmptyDataError: If the CSV file is empty.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
81 sys.exit: If the CSV file has the wrong format, the execution is aborted.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
82 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
83 try:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
84 dataset = pd.read_csv(data, sep = '\t', header = 0, engine='python')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
85 except pd.errors.EmptyDataError:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
86 sys.exit('Execution aborted: wrong format of ' + name + '\n')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
87 if len(dataset.columns) < 2:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
88 sys.exit('Execution aborted: wrong format of ' + name + '\n')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
89 return dataset
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
90
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
91 ############################ load id e rules ##################################
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
92 def load_id_rules(reactions :Dict[str, Dict[str, List[str]]]) -> Tuple[List[str], List[Dict[str, List[str]]]]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
93 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
94 Load IDs and rules from a dictionary of reactions.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
95
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
96 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
97 reactions (dict): A dictionary where keys are IDs and values are rules.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
98
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
99 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
100 tuple: A tuple containing two lists, the first list containing IDs and the second list containing rules.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
101 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
102 ids, rules = [], []
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
103 for key, value in reactions.items():
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
104 ids.append(key)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
105 rules.append(value)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
106 return (ids, rules)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
107
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
108 ############################ check_methods ####################################
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
109 def gene_type(l :str, name :str) -> str:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
110 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
111 Determine the type of gene ID.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
112
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
113 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
114 l (str): The gene identifier to check.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
115 name (str): The name of the dataset, used in error messages.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
116
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
117 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
118 str: The type of gene ID ('hugo_id', 'ensembl_gene_id', 'symbol', or 'entrez_id').
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
119
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
120 Raises:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
121 sys.exit: If the gene ID type is not supported, the execution is aborted.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
122 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
123 if check_hgnc(l):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
124 return 'hugo_id'
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
125 elif check_ensembl(l):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
126 return 'ensembl_gene_id'
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
127 elif check_symbol(l):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
128 return 'symbol'
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
129 elif check_entrez(l):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
130 return 'entrez_id'
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
131 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
132 sys.exit('Execution aborted:\n' +
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
133 'gene ID type in ' + name + ' not supported. Supported ID'+
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
134 'types are: HUGO ID, Ensemble ID, HUGO symbol, Entrez ID\n')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
135
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
136 def check_hgnc(l :str) -> bool:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
137 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
138 Check if a gene identifier follows the HGNC format.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
139
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
140 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
141 l (str): The gene identifier to check.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
142
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
143 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
144 bool: True if the gene identifier follows the HGNC format, False otherwise.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
145 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
146 if len(l) > 5:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
147 if (l.upper()).startswith('HGNC:'):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
148 return l[5:].isdigit()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
149 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
150 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
151 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
152 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
153
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
154 def check_ensembl(l :str) -> bool:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
155 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
156 Check if a gene identifier follows the Ensembl format.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
157
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
158 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
159 l (str): The gene identifier to check.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
160
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
161 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
162 bool: True if the gene identifier follows the Ensembl format, False otherwise.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
163 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
164 return l.upper().startswith('ENS')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
165
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
166
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
167 def check_symbol(l :str) -> bool:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
168 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
169 Check if a gene identifier follows the symbol format.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
170
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
171 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
172 l (str): The gene identifier to check.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
173
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
174 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
175 bool: True if the gene identifier follows the symbol format, False otherwise.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
176 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
177 if len(l) > 0:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
178 if l[0].isalpha() and l[1:].isalnum():
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
179 return True
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
180 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
181 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
182 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
183 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
184
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
185 def check_entrez(l :str) -> bool:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
186 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
187 Check if a gene identifier follows the Entrez ID format.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
188
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
189 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
190 l (str): The gene identifier to check.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
191
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
192 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
193 bool: True if the gene identifier follows the Entrez ID format, False otherwise.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
194 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
195 if len(l) > 0:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
196 return l.isdigit()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
197 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
198 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
199
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
200 ############################ gene #############################################
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
201 def data_gene(gene: pd.DataFrame, type_gene: str, name: str, gene_custom: Optional[Dict[str, str]]) -> Dict[str, str]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
202 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
203 Process gene data to ensure correct formatting and handle duplicates.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
204
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
205 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
206 gene (DataFrame): DataFrame containing gene data.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
207 type_gene (str): Type of gene data (e.g., 'hugo_id', 'ensembl_gene_id', 'symbol', 'entrez_id').
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
208 name (str): Name of the dataset.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
209 gene_custom (dict or None): Custom gene data dictionary if provided.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
210
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
211 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
212 dict: A dictionary containing gene data with gene IDs as keys and corresponding values.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
213 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
214 args = process_args()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
215 for i in range(len(gene)):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
216 tmp = gene.iloc[i, 0]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
217 gene.iloc[i, 0] = tmp.strip().split('.')[0]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
218
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
219 gene_dup = [item for item, count in
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
220 collections.Counter(gene[gene.columns[0]]).items() if count > 1]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
221 pat_dup = [item for item, count in
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
222 collections.Counter(list(gene.columns)).items() if count > 1]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
223
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
224 if gene_dup:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
225 if gene_custom == None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
226 if args.rules_selector == 'HMRcore':
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
227 gene_in_rule = pk.load(open(args.tool_dir + '/local/pickle files/HMRcore_genes.p', 'rb'))
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
228
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
229 elif args.rules_selector == 'Recon':
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
230 gene_in_rule = pk.load(open(args.tool_dir + '/local/pickle files/Recon_genes.p', 'rb'))
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
231
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
232 elif args.rules_selector == 'ENGRO2':
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
233 gene_in_rule = pk.load(open(args.tool_dir + '/local/pickle files/ENGRO2_genes.p', 'rb'))
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
234 print(f"{args.tool_dir}'/local/pickle files/ENGRO2_genes.p'")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
235 utils.logWarning(f"{args.tool_dir}'/local/pickle files/ENGRO2_genes.p'", ARGS.out_log)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
236 print(args.rules_selector)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
237 gene_in_rule = gene_in_rule.get(type_gene)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
238
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
239 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
240 gene_in_rule = gene_custom
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
241 tmp = []
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
242 for i in gene_dup:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
243 if gene_in_rule.get(i) == 'ok':
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
244 tmp.append(i)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
245 if tmp:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
246 sys.exit('Execution aborted because gene ID '
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
247 +str(tmp)+' in '+name+' is duplicated\n')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
248
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
249 if pat_dup: utils.logWarning(f"Warning: duplicated label\n{pat_dup} in {name}", ARGS.out_log)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
250 return (gene.set_index(gene.columns[0])).to_dict()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
251
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
252 ############################ resolve ##########################################
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
253 def replace_gene_value(l :str, d :str) -> Tuple[Union[int, float], list]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
254 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
255 Replace gene identifiers with corresponding values from a dictionary.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
256
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
257 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
258 l (str): String of gene identifier.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
259 d (str): String corresponding to its value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
260
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
261 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
262 tuple: A tuple containing two lists: the first list contains replaced values, and the second list contains any errors encountered during replacement.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
263 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
264 tmp = []
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
265 err = []
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
266 while l:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
267 if isinstance(l[0], list):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
268 tmp_rules, tmp_err = replace_gene_value(l[0], d)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
269 tmp.append(tmp_rules)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
270 err.extend(tmp_err)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
271 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
272 value = replace_gene(l[0], d)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
273 tmp.append(value)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
274 if value == None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
275 err.append(l[0])
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
276 l = l[1:]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
277 return (tmp, err)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
278
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
279 def replace_gene(l :str, d :str) -> Union[int, float]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
280 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
281 Replace a single gene identifier with its corresponding value from a dictionary.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
282
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
283 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
284 l (str): Gene identifier to replace.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
285 d (str): String corresponding to its value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
286
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
287 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
288 float/int: Corresponding value from the dictionary if found, None otherwise.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
289
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
290 Raises:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
291 sys.exit: If the value associated with the gene identifier is not valid.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
292 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
293 if l =='and' or l == 'or':
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
294 return l
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
295 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
296 value = d.get(l, None)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
297 if not(value == None or isinstance(value, (int, float))):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
298 sys.exit('Execution aborted: ' + value + ' value not valid\n')
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
299 return value
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
300
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
301 T = TypeVar("T", bound = Optional[Union[int, float]])
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
302 def computes(val1 :T, op :str, val2 :T, cn :bool) -> T:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
303 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
304 Compute the RAS value between two value and an operator ('and' or 'or').
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
305
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
306 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
307 val1(Optional(Union[float, int])): First value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
308 op (str): Operator ('and' or 'or').
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
309 val2(Optional(Union[float, int])): Second value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
310 cn (bool): Control boolean value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
311
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
312 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
313 Optional(Union[float, int]): Result of the computation.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
314 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
315 if val1 != None and val2 != None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
316 if op == 'and':
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
317 return min(val1, val2)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
318 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
319 return val1 + val2
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
320 elif op == 'and':
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
321 if cn is True:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
322 if val1 != None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
323 return val1
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
324 elif val2 != None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
325 return val2
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
326 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
327 return None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
328 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
329 return None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
330 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
331 if val1 != None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
332 return val1
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
333 elif val2 != None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
334 return val2
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
335 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
336 return None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
337
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
338 # ris should be Literal[None] but Literal is not supported in Python 3.7
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
339 def control(ris, l :List[Union[int, float, list]], cn :bool) -> Union[bool, int, float]: #Union[Literal[False], int, float]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
340 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
341 Control the format of the expression.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
342
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
343 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
344 ris: Intermediate result.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
345 l (list): Expression to control.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
346 cn (bool): Control boolean value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
347
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
348 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
349 Union[Literal[False], int, float]: Result of the control.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
350 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
351 if len(l) == 1:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
352 if isinstance(l[0], (float, int)) or l[0] == None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
353 return l[0]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
354 elif isinstance(l[0], list):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
355 return control(None, l[0], cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
356 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
357 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
358 elif len(l) > 2:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
359 return control_list(ris, l, cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
360 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
361 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
362
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
363 def control_list(ris, l :List[Optional[Union[float, int, list]]], cn :bool) -> Optional[bool]: #Optional[Literal[False]]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
364 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
365 Control the format of a list of expressions.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
366
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
367 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
368 ris: Intermediate result.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
369 l (list): List of expressions to control.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
370 cn (bool): Control boolean value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
371
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
372 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
373 Optional[Literal[False]]: Result of the control.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
374 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
375 while l:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
376 if len(l) == 1:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
377 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
378 elif (isinstance(l[0], (float, int)) or
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
379 l[0] == None) and l[1] in ['and', 'or']:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
380 if isinstance(l[2], (float, int)) or l[2] == None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
381 ris = computes(l[0], l[1], l[2], cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
382 elif isinstance(l[2], list):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
383 tmp = control(None, l[2], cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
384 if tmp is False:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
385 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
386 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
387 ris = computes(l[0], l[1], tmp, cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
388 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
389 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
390 l = l[3:]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
391 elif l[0] in ['and', 'or']:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
392 if isinstance(l[1], (float, int)) or l[1] == None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
393 ris = computes(ris, l[0], l[1], cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
394 elif isinstance(l[1], list):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
395 tmp = control(None,l[1], cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
396 if tmp is False:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
397 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
398 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
399 ris = computes(ris, l[0], tmp, cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
400 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
401 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
402 l = l[2:]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
403 elif isinstance(l[0], list) and l[1] in ['and', 'or']:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
404 if isinstance(l[2], (float, int)) or l[2] == None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
405 tmp = control(None, l[0], cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
406 if tmp is False:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
407 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
408 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
409 ris = computes(tmp, l[1], l[2], cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
410 elif isinstance(l[2], list):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
411 tmp = control(None, l[0], cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
412 tmp2 = control(None, l[2], cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
413 if tmp is False or tmp2 is False:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
414 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
415 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
416 ris = computes(tmp, l[1], tmp2, cn)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
417 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
418 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
419 l = l[3:]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
420 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
421 return False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
422 return ris
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
423
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
424 ResolvedRules = Dict[str, List[Optional[Union[float, int]]]]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
425 def resolve(genes: Dict[str, str], rules: List[str], ids: List[str], resolve_none: bool, name: str) -> Tuple[Optional[ResolvedRules], Optional[list]]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
426 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
427 Resolve rules using gene data to compute scores for each rule.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
428
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
429 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
430 genes (dict): Dictionary containing gene data with gene IDs as keys and corresponding values.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
431 rules (list): List of rules to resolve.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
432 ids (list): List of IDs corresponding to the rules.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
433 resolve_none (bool): Flag indicating whether to resolve None values in the rules.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
434 name (str): Name of the dataset.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
435
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
436 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
437 tuple: A tuple containing resolved rules as a dictionary and a list of gene IDs not found in the data.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
438 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
439 resolve_rules = {}
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
440 not_found = []
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
441 flag = False
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
442 for key, value in genes.items():
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
443 tmp_resolve = []
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
444 for i in range(len(rules)):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
445 tmp = rules[i]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
446 if tmp:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
447 tmp, err = replace_gene_value(tmp, value)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
448 if err:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
449 not_found.extend(err)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
450 ris = control(None, tmp, resolve_none)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
451 if ris is False or ris == None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
452 tmp_resolve.append(None)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
453 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
454 tmp_resolve.append(ris)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
455 flag = True
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
456 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
457 tmp_resolve.append(None)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
458 resolve_rules[key] = tmp_resolve
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
459
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
460 if flag is False:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
461 utils.logWarning(
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
462 f"Warning: no computable score (due to missing gene values) for class {name}, the class has been disregarded",
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
463 ARGS.out_log)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
464
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
465 return (None, None)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
466
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
467 return (resolve_rules, list(set(not_found)))
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
468 ############################ create_ras #######################################
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
469 def create_ras(resolve_rules: Optional[ResolvedRules], dataset_name: str, rules: List[str], ids: List[str], file: str) -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
470 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
471 Create a RAS (Reaction Activity Score) file from resolved rules.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
472
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
473 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
474 resolve_rules (dict): Dictionary containing resolved rules.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
475 dataset_name (str): Name of the dataset.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
476 rules (list): List of rules.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
477 file (str): Path to the output RAS file.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
478
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
479 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
480 None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
481 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
482 if resolve_rules is None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
483 utils.logWarning(f"Couldn't generate RAS for current dataset: {dataset_name}", ARGS.out_log)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
484
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
485 for geni in resolve_rules.values():
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
486 for i, valori in enumerate(geni):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
487 if valori == None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
488 geni[i] = 'None'
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
489
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
490 output_ras = pd.DataFrame.from_dict(resolve_rules)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
491
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
492 output_ras.insert(0, 'Reactions', ids)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
493 output_to_csv = pd.DataFrame.to_csv(output_ras, sep = '\t', index = False)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
494
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
495 text_file = open(file, "w")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
496
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
497 text_file.write(output_to_csv)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
498 text_file.close()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
499
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
500 ################################- NEW RAS COMPUTATION -################################
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
501 Expr = Optional[Union[int, float]]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
502 Ras = Expr
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
503 def ras_for_cell_lines(dataset: pd.DataFrame, rules: Dict[str, ruleUtils.OpList]) -> Dict[str, Dict[str, Ras]]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
504 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
505 Generates the RAS scores for each cell line found in the dataset.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
506
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
507 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
508 dataset (pd.DataFrame): Dataset containing gene values.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
509 rules (dict): The dict containing reaction ids as keys and rules as values.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
510
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
511 Side effects:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
512 dataset : mut
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
513
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
514 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
515 dict: A dictionary where each key corresponds to a cell line name and each value is a dictionary
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
516 where each key corresponds to a reaction ID and each value is its computed RAS score.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
517 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
518 ras_values_by_cell_line = {}
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
519 dataset.set_index(dataset.columns[0], inplace=True)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
520 # Considera tutte le colonne tranne la prima in cui ci sono gli hugo quindi va scartata
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
521 for cell_line_name in dataset.columns[1:]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
522 cell_line = dataset[cell_line_name].to_dict()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
523 ras_values_by_cell_line[cell_line_name]= get_ras_values(rules, cell_line)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
524 return ras_values_by_cell_line
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
525
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
526 def get_ras_values(value_rules: Dict[str, ruleUtils.OpList], dataset: Dict[str, Expr]) -> Dict[str, Ras]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
527 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
528 Computes the RAS (Reaction Activity Score) values for each rule in the given dict.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
529
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
530 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
531 value_rules (dict): A dictionary where keys are reaction ids and values are OpLists.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
532 dataset : gene expression data of one cell line.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
533
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
534 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
535 dict: A dictionary where keys are reaction ids and values are the computed RAS values for each rule.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
536 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
537 return {key: ras_op_list(op_list, dataset) for key, op_list in value_rules.items()}
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
538
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
539 def get_gene_expr(dataset :Dict[str, Expr], name :str) -> Expr:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
540 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
541 Extracts the gene expression of the given gene from a cell line dataset.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
542
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
543 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
544 dataset : gene expression data of one cell line.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
545 name : gene name.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
546
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
547 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
548 Expr : the gene's expression value.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
549 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
550 expr = dataset.get(name, None)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
551 if expr is None: ERRORS.append(name)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
552
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
553 return expr
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
554
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
555 def ras_op_list(op_list: ruleUtils.OpList, dataset: Dict[str, Expr]) -> Ras:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
556 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
557 Computes recursively the RAS (Reaction Activity Score) value for the given OpList, considering the specified flag to control None behavior.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
558
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
559 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
560 op_list (OpList): The OpList representing a rule with gene values.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
561 dataset : gene expression data of one cell line.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
562
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
563 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
564 Ras: The computed RAS value for the given OpList.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
565 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
566 op = op_list.op
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
567 ras_value :Ras = None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
568 if not op: return get_gene_expr(dataset, op_list[0])
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
569 if op is ruleUtils.RuleOp.AND and not ARGS.none and None in op_list: return None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
570
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
571 for i in range(len(op_list)):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
572 item = op_list[i]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
573 if isinstance(item, ruleUtils.OpList):
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
574 item = ras_op_list(item, dataset)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
575
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
576 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
577 item = get_gene_expr(dataset, item)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
578
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
579 if item is None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
580 if op is ruleUtils.RuleOp.AND and not ARGS.none: return None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
581 continue
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
582
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
583 if ras_value is None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
584 ras_value = item
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
585 else:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
586 ras_value = ras_value + item if op is ruleUtils.RuleOp.OR else min(ras_value, item)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
587
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
588 return ras_value
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
589
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
590 def save_as_tsv(rasScores: Dict[str, Dict[str, Ras]], reactions :List[str]) -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
591 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
592 Save computed ras scores to the given path, as a tsv file.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
593
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
594 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
595 rasScores : the computed ras scores.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
596 path : the output tsv file's path.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
597
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
598 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
599 None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
600 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
601 for scores in rasScores.values(): # this is actually a lot faster than using the ootb dataframe metod, sadly
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
602 for reactId, score in scores.items():
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
603 if score is None: scores[reactId] = "None"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
604
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
605 output_ras = pd.DataFrame.from_dict(rasScores)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
606 output_ras.insert(0, 'Reactions', reactions)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
607 output_ras.to_csv(ARGS.ras_output, sep = '\t', index = False)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
608
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
609 ############################ MAIN #############################################
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
610 #TODO: not used but keep, it will be when the new translator dicts will be used.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
611 def translateGene(geneName :str, encoding :str, geneTranslator :Dict[str, Dict[str, str]]) -> str:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
612 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
613 Translate gene from any supported encoding to HugoID.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
614
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
615 Args:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
616 geneName (str): the name of the gene in its current encoding.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
617 encoding (str): the encoding.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
618 geneTranslator (Dict[str, Dict[str, str]]): the dict containing all supported gene names
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
619 and encodings in the current model, mapping each to the corresponding HugoID encoding.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
620
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
621 Raises:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
622 ValueError: When the gene isn't supported in the model.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
623
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
624 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
625 str: the gene in HugoID encoding.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
626 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
627 supportedGenesInEncoding = geneTranslator[encoding]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
628 if geneName in supportedGenesInEncoding: return supportedGenesInEncoding[geneName]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
629 raise ValueError(f"Gene \"{geneName}\" non trovato, verifica di star utilizzando il modello corretto!")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
630
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
631 def load_custom_rules() -> Dict[str, ruleUtils.OpList]:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
632 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
633 Opens custom rules file and extracts the rules. If the file is in .csv format an additional parsing step will be
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
634 performed, significantly impacting the runtime.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
635
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
636 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
637 Dict[str, ruleUtils.OpList] : dict mapping reaction IDs to rules.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
638 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
639 datFilePath = utils.FilePath.fromStrPath(ARGS.rule_list) # actual file, stored in galaxy as a .dat
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
640
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
641 try: filenamePath = utils.FilePath.fromStrPath(ARGS.rules_name) # file's name in input, to determine its original ext
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
642 except utils.PathErr as err:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
643 raise utils.PathErr(filenamePath, f"Please make sure your file's name is a valid file path, {err.msg}")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
644
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
645 if filenamePath.ext is utils.FileFormat.PICKLE: return utils.readPickle(datFilePath)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
646
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
647 # csv rules need to be parsed, those in a pickle format are taken to be pre-parsed.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
648 return { line[0] : ruleUtils.parseRuleToNestedList(line[1]) for line in utils.readCsv(datFilePath) }
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
649
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
650 def main() -> None:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
651 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
652 Initializes everything and sets the program in motion based on the fronted input arguments.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
653
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
654 Returns:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
655 None
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
656 """
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
657 # get args from frontend (related xml)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
658 global ARGS
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
659 ARGS = process_args()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
660 print(ARGS.rules_selector)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
661 # read dataset
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
662 dataset = read_dataset(ARGS.input, "dataset")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
663 dataset.iloc[:, 0] = (dataset.iloc[:, 0]).astype(str)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
664
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
665 # remove versioning from gene names
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
666 dataset.iloc[:, 0] = dataset.iloc[:, 0].str.split('.').str[0]
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
667
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
668 # handle custom models
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
669 model :utils.Model = ARGS.rules_selector
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
670 if model is utils.Model.Custom:
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
671 rules = load_custom_rules()
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
672 reactions = list(rules.keys())
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
673
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
674 save_as_tsv(ras_for_cell_lines(dataset, rules), reactions)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
675 if ERRORS: utils.logWarning(
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
676 f"The following genes are mentioned in the rules but don't appear in the dataset: {ERRORS}",
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
677 ARGS.out_log)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
678
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
679 return
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
680
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
681 # This is the standard flow of the ras_generator program, for non-custom models.
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
682 name = "RAS Dataset"
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
683 type_gene = gene_type(dataset.iloc[0, 0], name)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
684
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
685 rules = model.getRules(ARGS.tool_dir)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
686 genes = data_gene(dataset, type_gene, name, None)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
687 ids, rules = load_id_rules(rules.get(type_gene))
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
688
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
689 resolve_rules, err = resolve(genes, rules, ids, ARGS.none, name)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
690 create_ras(resolve_rules, name, rules, ids, ARGS.ras_output)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
691
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
692 if err: utils.logWarning(
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
693 f"Warning: gene(s) {err} not found in class \"{name}\", " +
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
694 "the expression level for this gene will be considered NaN",
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
695 ARGS.out_log)
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
696
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
697 print("Execution succeded")
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
698
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
699 ###############################################################################
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
700 if __name__ == "__main__":
a48b2e06ebe7 Uploaded
luca_milaz
parents:
diff changeset
701 main()