annotate Marea/marea_cluster.py @ 7:771a40335876 draft

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