annotate Marea/ras_generator.py @ 76:3f144565551f draft

Uploaded
author bimib
date Wed, 03 Jun 2020 12:00:40 -0400
parents 3af9d394367c
children 3b0e71e28c0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
1 from __future__ import division
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
2 import sys
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
3 import pandas as pd
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
4 import collections
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
5 import pickle as pk
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
6 import math
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
7 import argparse
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
8
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
9 ########################## argparse ##########################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
10
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
11 def process_args(args):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
12 parser = argparse.ArgumentParser(usage = '%(prog)s [options]',
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
13 description = 'process some value\'s'+
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
14 ' genes to create a comparison\'s map.')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
15 parser.add_argument('-rs', '--rules_selector',
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
16 type = str,
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
17 default = 'HMRcore',
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
18 choices = ['HMRcore', 'Recon', 'Custom'],
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
19 help = 'chose which type of dataset you want use')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
20 parser.add_argument('-cr', '--custom',
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
21 type = str,
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
22 help='your dataset if you want custom rules')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
23 parser.add_argument('-n', '--none',
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
24 type = str,
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
25 default = 'true',
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
26 choices = ['true', 'false'],
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
27 help = 'compute Nan values')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
28 parser.add_argument('-td', '--tool_dir',
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
29 type = str,
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
30 required = True,
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
31 help = 'your tool directory')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
32 parser.add_argument('-ol', '--out_log',
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
33 help = "Output log")
47
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
34 parser.add_argument('-id', '--input',
46
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
35 type = str,
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
36 help = 'input dataset')
47
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
37 parser.add_argument('-ra', '--ras_output',
46
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
38 type = str,
47
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
39 required = True,
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
40 help = 'ras output')
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
41
46
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
42 args = parser.parse_args()
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
43 return args
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
44
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
45 ########################### warning ###########################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
46
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
47 def warning(s):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
48 args = process_args(sys.argv)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
49 with open(args.out_log, 'a') as log:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
50 log.write(s)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
51
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
52 ############################ dataset input ####################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
53
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
54 def read_dataset(data, name):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
55 try:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
56 dataset = pd.read_csv(data, sep = '\t', header = 0, engine='python')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
57 except pd.errors.EmptyDataError:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
58 sys.exit('Execution aborted: wrong format of ' + name + '\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
59 if len(dataset.columns) < 2:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
60 sys.exit('Execution aborted: wrong format of ' + name + '\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
61 return dataset
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
62
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
63 ############################ dataset name #####################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
64
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
65 def name_dataset(name_data, count):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
66 if str(name_data) == 'Dataset':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
67 return str(name_data) + '_' + str(count)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
68 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
69 return str(name_data)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
70
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
71 ############################ load id e rules ##################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
72
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
73 def load_id_rules(reactions):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
74 ids, rules = [], []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
75 for key, value in reactions.items():
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
76 ids.append(key)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
77 rules.append(value)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
78 return (ids, rules)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
79
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
80 ############################ check_methods ####################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
81
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
82 def gene_type(l, name):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
83 if check_hgnc(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
84 return 'hugo_id'
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
85 elif check_ensembl(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
86 return 'ensembl_gene_id'
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
87 elif check_symbol(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
88 return 'symbol'
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
89 elif check_entrez(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
90 return 'entrez_id'
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
91 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
92 sys.exit('Execution aborted:\n' +
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
93 'gene ID type in ' + name + ' not supported. Supported ID'+
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
94 'types are: HUGO ID, Ensemble ID, HUGO symbol, Entrez ID\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
95
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
96 def check_hgnc(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
97 if len(l) > 5:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
98 if (l.upper()).startswith('HGNC:'):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
99 return l[5:].isdigit()
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
100 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
101 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
102 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
103 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
104
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
105 def check_ensembl(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
106 if len(l) == 15:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
107 if (l.upper()).startswith('ENS'):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
108 return l[4:].isdigit()
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
109 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
110 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
111 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
112 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
113
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
114 def check_symbol(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
115 if len(l) > 0:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
116 if l[0].isalpha() and l[1:].isalnum():
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
117 return True
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
118 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
119 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
120 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
121 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
122
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
123 def check_entrez(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
124 if len(l) > 0:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
125 return l.isdigit()
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
126 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
127 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
128
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
129 def check_bool(b):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
130 if b == 'true':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
131 return True
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
132 elif b == 'false':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
133 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
134
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
135 ############################ resolve_methods ##################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
136
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
137 def replace_gene_value(l, d):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
138 tmp = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
139 err = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
140 while l:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
141 if isinstance(l[0], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
142 tmp_rules, tmp_err = replace_gene_value(l[0], d)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
143 tmp.append(tmp_rules)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
144 err.extend(tmp_err)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
145 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
146 value = replace_gene(l[0], d)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
147 tmp.append(value)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
148 if value == None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
149 err.append(l[0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
150 l = l[1:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
151 return (tmp, err)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
152
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
153
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
154 def replace_gene(l, d):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
155 if l =='and' or l == 'or':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
156 return l
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
157 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
158 value = d.get(l, None)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
159 if not(value == None or isinstance(value, (int, float))):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
160 sys.exit('Execution aborted: ' + value + ' value not valid\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
161 return value
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
162
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
163 def computes(val1, op, val2, cn):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
164 if val1 != None and val2 != None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
165 if op == 'and':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
166 return min(val1, val2)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
167 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
168 return val1 + val2
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
169 elif op == 'and':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
170 if cn is True:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
171 if val1 != None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
172 return val1
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
173 elif val2 != None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
174 return val2
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
175 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
176 return None
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
177 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
178 return None
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
179 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
180 if val1 != None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
181 return val1
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
182 elif val2 != None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
183 return val2
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
184 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
185 return None
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
186
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
187 def control(ris, l, cn):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
188 if len(l) == 1:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
189 if isinstance(l[0], (float, int)) or l[0] == None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
190 return l[0]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
191 elif isinstance(l[0], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
192 return control(None, l[0], cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
193 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
194 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
195 elif len(l) > 2:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
196 return control_list(ris, l, cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
197 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
198 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
199
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
200 def control_list(ris, l, cn):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
201 while l:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
202 if len(l) == 1:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
203 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
204 elif (isinstance(l[0], (float, int)) or
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
205 l[0] == None) and l[1] in ['and', 'or']:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
206 if isinstance(l[2], (float, int)) or l[2] == None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
207 ris = computes(l[0], l[1], l[2], cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
208 elif isinstance(l[2], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
209 tmp = control(None, l[2], cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
210 if tmp is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
211 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
212 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
213 ris = computes(l[0], l[1], tmp, cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
214 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
215 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
216 l = l[3:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
217 elif l[0] in ['and', 'or']:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
218 if isinstance(l[1], (float, int)) or l[1] == None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
219 ris = computes(ris, l[0], l[1], cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
220 elif isinstance(l[1], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
221 tmp = control(None,l[1], cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
222 if tmp is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
223 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
224 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
225 ris = computes(ris, l[0], tmp, cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
226 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
227 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
228 l = l[2:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
229 elif isinstance(l[0], list) and l[1] in ['and', 'or']:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
230 if isinstance(l[2], (float, int)) or l[2] == None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
231 tmp = control(None, l[0], cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
232 if tmp is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
233 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
234 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
235 ris = computes(tmp, l[1], l[2], cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
236 elif isinstance(l[2], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
237 tmp = control(None, l[0], cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
238 tmp2 = control(None, l[2], cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
239 if tmp is False or tmp2 is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
240 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
241 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
242 ris = computes(tmp, l[1], tmp2, cn)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
243 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
244 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
245 l = l[3:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
246 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
247 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
248 return ris
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
249
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
250 ############################ make recon #######################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
251
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
252 def check_and_doWord(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
253 tmp = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
254 tmp_genes = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
255 count = 0
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
256 while l:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
257 if count >= 0:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
258 if l[0] == '(':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
259 count += 1
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
260 tmp.append(l[0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
261 l.pop(0)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
262 elif l[0] == ')':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
263 count -= 1
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
264 tmp.append(l[0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
265 l.pop(0)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
266 elif l[0] == ' ':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
267 l.pop(0)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
268 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
269 word = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
270 while l:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
271 if l[0] in [' ', '(', ')']:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
272 break
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
273 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
274 word.append(l[0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
275 l.pop(0)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
276 word = ''.join(word)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
277 tmp.append(word)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
278 if not(word in ['or', 'and']):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
279 tmp_genes.append(word)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
280 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
281 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
282 if count == 0:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
283 return (tmp, tmp_genes)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
284 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
285 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
286
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
287 def brackets_to_list(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
288 tmp = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
289 while l:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
290 if l[0] == '(':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
291 l.pop(0)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
292 tmp.append(resolve_brackets(l))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
293 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
294 tmp.append(l[0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
295 l.pop(0)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
296 return tmp
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
297
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
298 def resolve_brackets(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
299 tmp = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
300 while l[0] != ')':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
301 if l[0] == '(':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
302 l.pop(0)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
303 tmp.append(resolve_brackets(l))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
304 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
305 tmp.append(l[0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
306 l.pop(0)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
307 l.pop(0)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
308 return tmp
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
309
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
310 def priorityAND(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
311 tmp = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
312 flag = True
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
313 while l:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
314 if len(l) == 1:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
315 if isinstance(l[0], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
316 tmp.append(priorityAND(l[0]))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
317 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
318 tmp.append(l[0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
319 l = l[1:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
320 elif l[0] == 'or':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
321 tmp.append(l[0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
322 flag = False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
323 l = l[1:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
324 elif l[1] == 'or':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
325 if isinstance(l[0], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
326 tmp.append(priorityAND(l[0]))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
327 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
328 tmp.append(l[0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
329 tmp.append(l[1])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
330 flag = False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
331 l = l[2:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
332 elif l[1] == 'and':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
333 tmpAnd = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
334 if isinstance(l[0], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
335 tmpAnd.append(priorityAND(l[0]))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
336 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
337 tmpAnd.append(l[0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
338 tmpAnd.append(l[1])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
339 if isinstance(l[2], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
340 tmpAnd.append(priorityAND(l[2]))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
341 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
342 tmpAnd.append(l[2])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
343 l = l[3:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
344 while l:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
345 if l[0] == 'and':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
346 tmpAnd.append(l[0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
347 if isinstance(l[1], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
348 tmpAnd.append(priorityAND(l[1]))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
349 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
350 tmpAnd.append(l[1])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
351 l = l[2:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
352 elif l[0] == 'or':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
353 flag = False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
354 break
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
355 if flag == True: #when there are only AND in list
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
356 tmp.extend(tmpAnd)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
357 elif flag == False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
358 tmp.append(tmpAnd)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
359 return tmp
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
360
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
361 def checkRule(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
362 if len(l) == 1:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
363 if isinstance(l[0], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
364 if checkRule(l[0]) is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
365 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
366 elif len(l) > 2:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
367 if checkRule2(l) is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
368 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
369 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
370 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
371 return True
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
372
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
373 def checkRule2(l):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
374 while l:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
375 if len(l) == 1:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
376 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
377 elif isinstance(l[0], list) and l[1] in ['and', 'or']:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
378 if checkRule(l[0]) is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
379 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
380 if isinstance(l[2], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
381 if checkRule(l[2]) is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
382 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
383 l = l[3:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
384 elif l[1] in ['and', 'or']:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
385 if isinstance(l[2], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
386 if checkRule(l[2]) is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
387 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
388 l = l[3:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
389 elif l[0] in ['and', 'or']:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
390 if isinstance(l[1], list):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
391 if checkRule(l[1]) is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
392 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
393 l = l[2:]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
394 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
395 return False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
396 return True
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
397
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
398 def do_rules(rules):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
399 split_rules = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
400 err_rules = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
401 tmp_gene_in_rule = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
402 for i in range(len(rules)):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
403 tmp = list(rules[i])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
404 if tmp:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
405 tmp, tmp_genes = check_and_doWord(tmp)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
406 tmp_gene_in_rule.extend(tmp_genes)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
407 if tmp is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
408 split_rules.append([])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
409 err_rules.append(rules[i])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
410 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
411 tmp = brackets_to_list(tmp)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
412 if checkRule(tmp):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
413 split_rules.append(priorityAND(tmp))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
414 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
415 split_rules.append([])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
416 err_rules.append(rules[i])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
417 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
418 split_rules.append([])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
419 if err_rules:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
420 warning('Warning: wrong format rule in ' + str(err_rules) + '\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
421 return (split_rules, list(set(tmp_gene_in_rule)))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
422
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
423 def make_recon(data):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
424 try:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
425 import cobra as cb
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
426 import warnings
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
427 with warnings.catch_warnings():
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
428 warnings.simplefilter('ignore')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
429 recon = cb.io.read_sbml_model(data)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
430 react = recon.reactions
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
431 rules = [react[i].gene_reaction_rule for i in range(len(react))]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
432 ids = [react[i].id for i in range(len(react))]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
433 except cb.io.sbml3.CobraSBMLError:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
434 try:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
435 data = (pd.read_csv(data, sep = '\t', dtype = str, engine='python')).fillna('')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
436 if len(data.columns) < 2:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
437 sys.exit('Execution aborted: wrong format of '+
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
438 'custom datarules\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
439 if not len(data.columns) == 2:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
440 warning('Warning: more than 2 columns in custom datarules.\n' +
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
441 'Extra columns have been disregarded\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
442 ids = list(data.iloc[:, 0])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
443 rules = list(data.iloc[:, 1])
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
444 except pd.errors.EmptyDataError:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
445 sys.exit('Execution aborted: wrong format of custom datarules\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
446 except pd.errors.ParserError:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
447 sys.exit('Execution aborted: wrong format of custom datarules\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
448 split_rules, tmp_genes = do_rules(rules)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
449 gene_in_rule = {}
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
450 for i in tmp_genes:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
451 gene_in_rule[i] = 'ok'
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
452 return (ids, split_rules, gene_in_rule)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
453
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
454 ############################ gene #############################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
455
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
456 def data_gene(gene, type_gene, name, gene_custom):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
457 args = process_args(sys.argv)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
458 for i in range(len(gene)):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
459 tmp = gene.iloc[i, 0]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
460 if tmp.startswith(' ') or tmp.endswith(' '):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
461 gene.iloc[i, 0] = (tmp.lstrip()).rstrip()
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
462 gene_dup = [item for item, count in
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
463 collections.Counter(gene[gene.columns[0]]).items() if count > 1]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
464 pat_dup = [item for item, count in
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
465 collections.Counter(list(gene.columns)).items() if count > 1]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
466
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
467 if gene_dup:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
468 if gene_custom == None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
469 if args.rules_selector == 'HMRcore':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
470 gene_in_rule = pk.load(open(args.tool_dir +
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
471 '/local/HMRcore_genes.p', 'rb'))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
472 elif args.rules_selector == 'Recon':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
473 gene_in_rule = pk.load(open(args.tool_dir +
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
474 '/local/Recon_genes.p', 'rb'))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
475 gene_in_rule = gene_in_rule.get(type_gene)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
476 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
477 gene_in_rule = gene_custom
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
478 tmp = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
479 for i in gene_dup:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
480 if gene_in_rule.get(i) == 'ok':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
481 tmp.append(i)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
482 if tmp:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
483 sys.exit('Execution aborted because gene ID '
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
484 +str(tmp)+' in '+name+' is duplicated\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
485 if pat_dup:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
486 warning('Warning: duplicated label\n' + str(pat_dup) + 'in ' + name +
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
487 '\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
488
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
489 return (gene.set_index(gene.columns[0])).to_dict()
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
490
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
491 ############################ resolve ##########################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
492
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
493 def resolve(genes, rules, ids, resolve_none, name):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
494 resolve_rules = {}
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
495 not_found = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
496 flag = False
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
497 for key, value in genes.items():
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
498 tmp_resolve = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
499 for i in range(len(rules)):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
500 tmp = rules[i]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
501 if tmp:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
502 tmp, err = replace_gene_value(tmp, value)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
503 if err:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
504 not_found.extend(err)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
505 ris = control(None, tmp, resolve_none)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
506 if ris is False or ris == None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
507 tmp_resolve.append(None)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
508 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
509 tmp_resolve.append(ris)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
510 flag = True
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
511 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
512 tmp_resolve.append(None)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
513 resolve_rules[key] = tmp_resolve
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
514 if flag is False:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
515 warning('Warning: no computable score (due to missing gene values)' +
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
516 'for class ' + name + ', the class has been disregarded\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
517 return (None, None)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
518 return (resolve_rules, list(set(not_found)))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
519
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
520 ############################ split class ######################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
521
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
522 def split_class(classes, resolve_rules):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
523 class_pat = {}
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
524 for i in range(len(classes)):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
525 classe = classes.iloc[i, 1]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
526 if not pd.isnull(classe):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
527 l = []
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
528 for j in range(i, len(classes)):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
529 if classes.iloc[j, 1] == classe:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
530 pat_id = classes.iloc[j, 0]
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
531 if tmp != None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
532 l.append(tmp)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
533 classes.iloc[j, 1] = None
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
534 if l:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
535 class_pat[classe] = list(map(list, zip(*l)))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
536 else:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
537 warning('Warning: no sample found in class ' + classe +
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
538 ', the class has been disregarded\n')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
539 return class_pat
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
540
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
541 ############################ create_ras #######################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
542
47
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
543 def create_ras (resolve_rules, dataset_name, rules, ids, file):
46
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
544
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
545 if resolve_rules == None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
546 warning("Couldn't generate RAS for current dataset: " + dataset_name)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
547
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
548 for geni in resolve_rules.values():
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
549 for i, valori in enumerate(geni):
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
550 if valori == None:
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
551 geni[i] = 'None'
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
552
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
553 output_ras = pd.DataFrame.from_dict(resolve_rules)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
554
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
555 output_ras.insert(0, 'Reactions', ids)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
556 output_to_csv = pd.DataFrame.to_csv(output_ras, sep = '\t', index = False)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
557
47
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
558 text_file = open(file, "w")
46
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
559
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
560 text_file.write(output_to_csv)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
561 text_file.close()
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
562
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
563 ############################ MAIN #############################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
564
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
565 def main():
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
566 args = process_args(sys.argv)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
567
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
568 if args.rules_selector == 'HMRcore':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
569 recon = pk.load(open(args.tool_dir + '/local/HMRcore_rules.p', 'rb'))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
570 elif args.rules_selector == 'Recon':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
571 recon = pk.load(open(args.tool_dir + '/local/Recon_rules.p', 'rb'))
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
572 elif args.rules_selector == 'Custom':
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
573 ids, rules, gene_in_rule = make_recon(args.custom)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
574
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
575 resolve_none = check_bool(args.none)
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
576
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
577
47
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
578 name = "RAS Dataset"
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
579 dataset = read_dataset(args.input, "dataset")
46
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
580
47
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
581 dataset.iloc[:, 0] = (dataset.iloc[:, 0]).astype(str)
46
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
582
47
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
583 type_gene = gene_type(dataset.iloc[0, 0], name)
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
584
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
585 if args.rules_selector != 'Custom':
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
586 genes = data_gene(dataset, type_gene, name, None)
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
587 ids, rules = load_id_rules(recon.get(type_gene))
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
588 elif args.rules_selector == 'Custom':
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
589 genes = data_gene(dataset, type_gene, name, gene_in_rule)
46
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
590
47
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
591 resolve_rules, err = resolve(genes, rules, ids, resolve_none, name)
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
592
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
593 create_ras(resolve_rules, name, rules, ids, args.ras_output)
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
594
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
595 if err != None and err:
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
596 warning('Warning: gene\n' + str(err) + '\nnot found in class '
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
597 + name + ', the expression level for this gene ' +
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
598 'will be considered NaN\n')
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
599
3af9d394367c Uploaded
bimib
parents: 46
diff changeset
600
46
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
601 print('Execution succeded')
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
602
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
603 return None
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
604
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
605 ###############################################################################
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
606
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
607 if __name__ == "__main__":
5d5d01ef1d68 Uploaded
bimib
parents:
diff changeset
608 main()