annotate Marea/marea_cluster.py @ 13:e96f3b85e5a0 draft

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