annotate Marea/marea.py @ 48:e4235b5231e4 draft

Uploaded
author bimib
date Sun, 23 Feb 2020 09:41:14 -0500
parents 3af9d394367c
children 7b57a93ac594
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 sys
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
3 import pandas as pd
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
4 import itertools as it
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
5 import scipy.stats as st
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
6 import collections
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
7 import lxml.etree as ET
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
8 import pickle as pk
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
9 import math
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
10 import os
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
11 import argparse
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
12 from svglib.svglib import svg2rlg
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
13 from reportlab.graphics import renderPDF
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
14
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
15 ########################## argparse ##########################################
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
16
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
17 def process_args(args):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
18 parser = argparse.ArgumentParser(usage = '%(prog)s [options]',
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
19 description = 'process some value\'s'+
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
20 ' genes to create a comparison\'s map.')
48
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
21 parser.add_argument('-cr', '--custom_rules',
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
22 type = str,
48
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
23 default = 'false',
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
24 choices = ['true', 'false'],
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
25 help = 'choose whether to use custom rules')
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
26 parser.add_argument('-cc', '--custom_rule',
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
27 type = str,
48
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
28 help='custom rules to use')
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
29 parser.add_argument('-cm', '--custom_map',
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
30 type = str,
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
31 help='custom map to use')
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
32 parser.add_argument('-n', '--none',
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
33 type = str,
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
34 default = 'true',
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
35 choices = ['true', 'false'],
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
36 help = 'compute Nan values')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
37 parser.add_argument('-pv' ,'--pValue',
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
38 type = float,
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
39 default = 0.1,
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
40 help = 'P-Value threshold (default: %(default)s)')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
41 parser.add_argument('-fc', '--fChange',
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
42 type = float,
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
43 default = 1.5,
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
44 help = 'Fold-Change threshold (default: %(default)s)')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
45 parser.add_argument('-td', '--tool_dir',
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
46 type = str,
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
47 required = True,
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
48 help = 'your tool directory')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
49 parser.add_argument('-op', '--option',
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
50 type = str,
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
51 choices = ['datasets', 'dataset_class'],
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
52 help='dataset or dataset and class')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
53 parser.add_argument('-ol', '--out_log',
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
54 help = "Output log")
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
55 parser.add_argument('-id', '--input_data',
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
56 type = str,
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
57 help = 'input dataset')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
58 parser.add_argument('-ic', '--input_class',
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
59 type = str,
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
60 help = 'sample group specification')
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
61 parser.add_argument('-gs', '--generate_svg',
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
62 type = str,
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
63 default = 'true',
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
64 choices = ['true', 'false'],
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
65 help = 'generate svg map')
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
66 parser.add_argument('-gp', '--generate_pdf',
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
67 type = str,
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
68 default = 'true',
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
69 choices = ['true', 'false'],
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
70 help = 'generate pdf map')
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
71 parser.add_argument('-on', '--control',
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
72 type = str)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
73 parser.add_argument('-co', '--comparison',
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
74 type = str,
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
75 default = '1vs1',
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
76 choices = ['manyvsmany', 'onevsrest', 'onevsmany'])
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
77 parser.add_argument('-ids', '--input_datas',
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
78 type = str,
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
79 nargs = '+',
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
80 help = 'input datasets')
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
81 parser.add_argument('-na', '--names',
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
82 type = str,
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
83 nargs = '+',
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
84 help = 'input names')
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
85
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
86 args = parser.parse_args()
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
87 return args
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
88
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
89 ########################### warning ###########################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
90
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
91 def warning(s):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
92 args = process_args(sys.argv)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
93 with open(args.out_log, 'a') as log:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
94 log.write(s)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
95
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
96 ############################ dataset input ####################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
97
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
98 def read_dataset(data, name):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
99 try:
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
100 dataset = pd.read_csv(data, sep = '\t', header = 0, engine='python')
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
101 except pd.errors.EmptyDataError:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
102 sys.exit('Execution aborted: wrong format of ' + name + '\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
103 if len(dataset.columns) < 2:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
104 sys.exit('Execution aborted: wrong format of ' + name + '\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
105 return dataset
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
106
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
107 ############################ dataset name #####################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
108
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
109 def name_dataset(name_data, count):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
110 if str(name_data) == 'Dataset':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
111 return str(name_data) + '_' + str(count)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
112 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
113 return str(name_data)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
114
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
115 ############################ load id e rules ##################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
116
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
117 def load_id_rules(reactions):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
118 ids, rules = [], []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
119 for key, value in reactions.items():
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
120 ids.append(key)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
121 rules.append(value)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
122 return (ids, rules)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
123
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
124 ############################ check_methods ####################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
125
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
126 def gene_type(l, name):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
127 if check_hgnc(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
128 return 'hugo_id'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
129 elif check_ensembl(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
130 return 'ensembl_gene_id'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
131 elif check_symbol(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
132 return 'symbol'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
133 elif check_entrez(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
134 return 'entrez_id'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
135 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
136 sys.exit('Execution aborted:\n' +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
137 'gene ID type in ' + name + ' not supported. Supported ID'+
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
138 'types are: HUGO ID, Ensemble ID, HUGO symbol, Entrez ID\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
139
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
140 def check_hgnc(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
141 if len(l) > 5:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
142 if (l.upper()).startswith('HGNC:'):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
143 return l[5:].isdigit()
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
144 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
145 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
146 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
147 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
148
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
149 def check_ensembl(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
150 if len(l) == 15:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
151 if (l.upper()).startswith('ENS'):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
152 return l[4:].isdigit()
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
153 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
154 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
155 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
156 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
157
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
158 def check_symbol(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
159 if len(l) > 0:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
160 if l[0].isalpha() and l[1:].isalnum():
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
161 return True
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
162 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
163 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
164 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
165 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
166
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
167 def check_entrez(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
168 if len(l) > 0:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
169 return l.isdigit()
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
170 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
171 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
172
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
173 def check_bool(b):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
174 if b == 'true':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
175 return True
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
176 elif b == 'false':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
177 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
178
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
179 ############################ resolve_methods ##################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
180
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
181 def replace_gene_value(l, d):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
182 tmp = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
183 err = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
184 while l:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
185 if isinstance(l[0], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
186 tmp_rules, tmp_err = replace_gene_value(l[0], d)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
187 tmp.append(tmp_rules)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
188 err.extend(tmp_err)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
189 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
190 value = replace_gene(l[0], d)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
191 tmp.append(value)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
192 if value == None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
193 err.append(l[0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
194 l = l[1:]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
195 return (tmp, err)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
196
35
7b1971251c63 Uploaded
bimib
parents: 34
diff changeset
197
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
198 def replace_gene(l, d):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
199 if l =='and' or l == 'or':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
200 return l
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
201 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
202 value = d.get(l, None)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
203 if not(value == None or isinstance(value, (int, float))):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
204 sys.exit('Execution aborted: ' + value + ' value not valid\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
205 return value
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
206
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
207 def computes(val1, op, val2, cn):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
208 if val1 != None and val2 != None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
209 if op == 'and':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
210 return min(val1, val2)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
211 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
212 return val1 + val2
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
213 elif op == 'and':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
214 if cn is True:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
215 if val1 != None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
216 return val1
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
217 elif val2 != None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
218 return val2
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
219 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
220 return None
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
221 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
222 return None
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
223 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
224 if val1 != None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
225 return val1
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
226 elif val2 != None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
227 return val2
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
228 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
229 return None
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
230
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
231 def control(ris, l, cn):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
232 if len(l) == 1:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
233 if isinstance(l[0], (float, int)) or l[0] == None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
234 return l[0]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
235 elif isinstance(l[0], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
236 return control(None, l[0], cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
237 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
238 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
239 elif len(l) > 2:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
240 return control_list(ris, l, cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
241 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
242 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
243
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
244 def control_list(ris, l, cn):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
245 while l:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
246 if len(l) == 1:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
247 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
248 elif (isinstance(l[0], (float, int)) or
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
249 l[0] == None) and l[1] in ['and', 'or']:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
250 if isinstance(l[2], (float, int)) or l[2] == None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
251 ris = computes(l[0], l[1], l[2], cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
252 elif isinstance(l[2], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
253 tmp = control(None, l[2], cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
254 if tmp is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
255 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
256 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
257 ris = computes(l[0], l[1], tmp, cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
258 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
259 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
260 l = l[3:]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
261 elif l[0] in ['and', 'or']:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
262 if isinstance(l[1], (float, int)) or l[1] == None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
263 ris = computes(ris, l[0], l[1], cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
264 elif isinstance(l[1], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
265 tmp = control(None,l[1], cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
266 if tmp is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
267 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
268 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
269 ris = computes(ris, l[0], tmp, cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
270 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
271 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
272 l = l[2:]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
273 elif isinstance(l[0], list) and l[1] in ['and', 'or']:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
274 if isinstance(l[2], (float, int)) or l[2] == None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
275 tmp = control(None, l[0], cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
276 if tmp is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
277 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
278 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
279 ris = computes(tmp, l[1], l[2], cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
280 elif isinstance(l[2], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
281 tmp = control(None, l[0], cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
282 tmp2 = control(None, l[2], cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
283 if tmp is False or tmp2 is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
284 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
285 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
286 ris = computes(tmp, l[1], tmp2, cn)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
287 else:
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 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
291 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
292 return ris
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
293
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
294 ############################ map_methods ######################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
295
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
296 def fold_change(avg1, avg2):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
297 if avg1 == 0 and avg2 == 0:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
298 return 0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
299 elif avg1 == 0:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
300 return '-INF'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
301 elif avg2 == 0:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
302 return 'INF'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
303 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
304 return math.log(avg1 / avg2, 2)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
305
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
306 def fix_style(l, col, width, dash):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
307 tmp = l.split(';')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
308 flag_col = False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
309 flag_width = False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
310 flag_dash = False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
311 for i in range(len(tmp)):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
312 if tmp[i].startswith('stroke:'):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
313 tmp[i] = 'stroke:' + col
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
314 flag_col = True
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
315 if tmp[i].startswith('stroke-width:'):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
316 tmp[i] = 'stroke-width:' + width
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
317 flag_width = True
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
318 if tmp[i].startswith('stroke-dasharray:'):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
319 tmp[i] = 'stroke-dasharray:' + dash
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
320 flag_dash = True
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
321 if not flag_col:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
322 tmp.append('stroke:' + col)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
323 if not flag_width:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
324 tmp.append('stroke-width:' + width)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
325 if not flag_dash:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
326 tmp.append('stroke-dasharray:' + dash)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
327 return ';'.join(tmp)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
328
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
329 def fix_map(d, core_map, threshold_P_V, threshold_F_C, max_F_C):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
330 maxT = 12
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
331 minT = 2
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
332 grey = '#BEBEBE'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
333 blue = '#0000FF'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
334 red = '#E41A1C'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
335 for el in core_map.iter():
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
336 el_id = str(el.get('id'))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
337 if el_id.startswith('R_'):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
338 tmp = d.get(el_id[2:])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
339 if tmp != None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
340 p_val = tmp[0]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
341 f_c = tmp[1]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
342 if p_val < threshold_P_V:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
343 if not isinstance(f_c, str):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
344 if abs(f_c) < math.log(threshold_F_C, 2):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
345 col = grey
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
346 width = str(minT)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
347 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
348 if f_c < 0:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
349 col = blue
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
350 elif f_c > 0:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
351 col = red
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
352 width = str(max((abs(f_c) * maxT) / max_F_C, minT))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
353 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
354 if f_c == '-INF':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
355 col = blue
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
356 elif f_c == 'INF':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
357 col = red
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
358 width = str(maxT)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
359 dash = 'none'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
360 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
361 dash = '5,5'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
362 col = grey
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
363 width = str(minT)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
364 el.set('style', fix_style(el.get('style'), col, width, dash))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
365 return core_map
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
366
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
367 ############################ make recon #######################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
368
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
369 def check_and_doWord(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
370 tmp = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
371 tmp_genes = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
372 count = 0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
373 while l:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
374 if count >= 0:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
375 if l[0] == '(':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
376 count += 1
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
377 tmp.append(l[0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
378 l.pop(0)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
379 elif l[0] == ')':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
380 count -= 1
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
381 tmp.append(l[0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
382 l.pop(0)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
383 elif l[0] == ' ':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
384 l.pop(0)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
385 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
386 word = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
387 while l:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
388 if l[0] in [' ', '(', ')']:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
389 break
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
390 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
391 word.append(l[0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
392 l.pop(0)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
393 word = ''.join(word)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
394 tmp.append(word)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
395 if not(word in ['or', 'and']):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
396 tmp_genes.append(word)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
397 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
398 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
399 if count == 0:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
400 return (tmp, tmp_genes)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
401 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
402 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
403
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
404 def brackets_to_list(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
405 tmp = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
406 while l:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
407 if l[0] == '(':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
408 l.pop(0)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
409 tmp.append(resolve_brackets(l))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
410 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
411 tmp.append(l[0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
412 l.pop(0)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
413 return tmp
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
414
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
415 def resolve_brackets(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
416 tmp = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
417 while l[0] != ')':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
418 if l[0] == '(':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
419 l.pop(0)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
420 tmp.append(resolve_brackets(l))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
421 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
422 tmp.append(l[0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
423 l.pop(0)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
424 l.pop(0)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
425 return tmp
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
426
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
427 def priorityAND(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
428 tmp = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
429 flag = True
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
430 while l:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
431 if len(l) == 1:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
432 if isinstance(l[0], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
433 tmp.append(priorityAND(l[0]))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
434 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
435 tmp.append(l[0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
436 l = l[1:]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
437 elif l[0] == 'or':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
438 tmp.append(l[0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
439 flag = False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
440 l = l[1:]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
441 elif l[1] == 'or':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
442 if isinstance(l[0], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
443 tmp.append(priorityAND(l[0]))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
444 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
445 tmp.append(l[0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
446 tmp.append(l[1])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
447 flag = False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
448 l = l[2:]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
449 elif l[1] == 'and':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
450 tmpAnd = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
451 if isinstance(l[0], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
452 tmpAnd.append(priorityAND(l[0]))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
453 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
454 tmpAnd.append(l[0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
455 tmpAnd.append(l[1])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
456 if isinstance(l[2], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
457 tmpAnd.append(priorityAND(l[2]))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
458 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
459 tmpAnd.append(l[2])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
460 l = l[3:]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
461 while l:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
462 if l[0] == 'and':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
463 tmpAnd.append(l[0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
464 if isinstance(l[1], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
465 tmpAnd.append(priorityAND(l[1]))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
466 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
467 tmpAnd.append(l[1])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
468 l = l[2:]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
469 elif l[0] == 'or':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
470 flag = False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
471 break
13
e96f3b85e5a0 Uploaded
bimib
parents: 12
diff changeset
472 if flag == True: #when there are only AND in list
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
473 tmp.extend(tmpAnd)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
474 elif flag == False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
475 tmp.append(tmpAnd)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
476 return tmp
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
477
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
478 def checkRule(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
479 if len(l) == 1:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
480 if isinstance(l[0], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
481 if checkRule(l[0]) is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
482 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
483 elif len(l) > 2:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
484 if checkRule2(l) is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
485 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
486 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
487 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
488 return True
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
489
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
490 def checkRule2(l):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
491 while l:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
492 if len(l) == 1:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
493 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
494 elif isinstance(l[0], list) and l[1] in ['and', 'or']:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
495 if checkRule(l[0]) is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
496 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
497 if isinstance(l[2], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
498 if checkRule(l[2]) is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
499 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
500 l = l[3:]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
501 elif l[1] in ['and', 'or']:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
502 if isinstance(l[2], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
503 if checkRule(l[2]) is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
504 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
505 l = l[3:]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
506 elif l[0] in ['and', 'or']:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
507 if isinstance(l[1], list):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
508 if checkRule(l[1]) is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
509 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
510 l = l[2:]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
511 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
512 return False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
513 return True
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
514
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
515 def do_rules(rules):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
516 split_rules = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
517 err_rules = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
518 tmp_gene_in_rule = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
519 for i in range(len(rules)):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
520 tmp = list(rules[i])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
521 if tmp:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
522 tmp, tmp_genes = check_and_doWord(tmp)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
523 tmp_gene_in_rule.extend(tmp_genes)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
524 if tmp is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
525 split_rules.append([])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
526 err_rules.append(rules[i])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
527 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
528 tmp = brackets_to_list(tmp)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
529 if checkRule(tmp):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
530 split_rules.append(priorityAND(tmp))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
531 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
532 split_rules.append([])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
533 err_rules.append(rules[i])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
534 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
535 split_rules.append([])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
536 if err_rules:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
537 warning('Warning: wrong format rule in ' + str(err_rules) + '\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
538 return (split_rules, list(set(tmp_gene_in_rule)))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
539
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
540 def make_recon(data):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
541 try:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
542 import cobra as cb
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
543 import warnings
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
544 with warnings.catch_warnings():
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
545 warnings.simplefilter('ignore')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
546 recon = cb.io.read_sbml_model(data)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
547 react = recon.reactions
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
548 rules = [react[i].gene_reaction_rule for i in range(len(react))]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
549 ids = [react[i].id for i in range(len(react))]
48
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
550 except cb.io.sbml.CobraSBMLError:
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
551 try:
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
552 data = (pd.read_csv(data, sep = '\t', dtype = str, engine='python')).fillna('')
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
553 if len(data.columns) < 2:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
554 sys.exit('Execution aborted: wrong format of '+
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
555 'custom datarules\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
556 if not len(data.columns) == 2:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
557 warning('Warning: more than 2 columns in custom datarules.\n' +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
558 'Extra columns have been disregarded\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
559 ids = list(data.iloc[:, 0])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
560 rules = list(data.iloc[:, 1])
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
561 except pd.errors.EmptyDataError:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
562 sys.exit('Execution aborted: wrong format of custom datarules\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
563 except pd.errors.ParserError:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
564 sys.exit('Execution aborted: wrong format of custom datarules\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
565 split_rules, tmp_genes = do_rules(rules)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
566 gene_in_rule = {}
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
567 for i in tmp_genes:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
568 gene_in_rule[i] = 'ok'
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
569 return (ids, split_rules, gene_in_rule)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
570
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
571 ############################ gene #############################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
572
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
573 def data_gene(gene, type_gene, name, gene_custom):
35
7b1971251c63 Uploaded
bimib
parents: 34
diff changeset
574 args = process_args(sys.argv)
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
575 for i in range(len(gene)):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
576 tmp = gene.iloc[i, 0]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
577 if tmp.startswith(' ') or tmp.endswith(' '):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
578 gene.iloc[i, 0] = (tmp.lstrip()).rstrip()
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
579 gene_dup = [item for item, count in
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
580 collections.Counter(gene[gene.columns[0]]).items() if count > 1]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
581 pat_dup = [item for item, count in
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
582 collections.Counter(list(gene.columns)).items() if count > 1]
35
7b1971251c63 Uploaded
bimib
parents: 34
diff changeset
583
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
584 if gene_dup:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
585 if gene_custom == None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
586 if args.rules_selector == 'HMRcore':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
587 gene_in_rule = pk.load(open(args.tool_dir +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
588 '/local/HMRcore_genes.p', 'rb'))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
589 elif args.rules_selector == 'Recon':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
590 gene_in_rule = pk.load(open(args.tool_dir +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
591 '/local/Recon_genes.p', 'rb'))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
592 gene_in_rule = gene_in_rule.get(type_gene)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
593 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
594 gene_in_rule = gene_custom
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
595 tmp = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
596 for i in gene_dup:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
597 if gene_in_rule.get(i) == 'ok':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
598 tmp.append(i)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
599 if tmp:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
600 sys.exit('Execution aborted because gene ID '
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
601 +str(tmp)+' in '+name+' is duplicated\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
602 if pat_dup:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
603 warning('Warning: duplicated label\n' + str(pat_dup) + 'in ' + name +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
604 '\n')
35
7b1971251c63 Uploaded
bimib
parents: 34
diff changeset
605
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
606 return (gene.set_index(gene.columns[0])).to_dict()
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
607
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
608 ############################ resolve ##########################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
609
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
610 def resolve(genes, rules, ids, resolve_none, name):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
611 resolve_rules = {}
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
612 not_found = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
613 flag = False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
614 for key, value in genes.items():
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
615 tmp_resolve = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
616 for i in range(len(rules)):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
617 tmp = rules[i]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
618 if tmp:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
619 tmp, err = replace_gene_value(tmp, value)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
620 if err:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
621 not_found.extend(err)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
622 ris = control(None, tmp, resolve_none)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
623 if ris is False or ris == None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
624 tmp_resolve.append(None)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
625 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
626 tmp_resolve.append(ris)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
627 flag = True
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
628 else:
35
7b1971251c63 Uploaded
bimib
parents: 34
diff changeset
629 tmp_resolve.append(None)
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
630 resolve_rules[key] = tmp_resolve
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
631 if flag is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
632 warning('Warning: no computable score (due to missing gene values)' +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
633 'for class ' + name + ', the class has been disregarded\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
634 return (None, None)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
635 return (resolve_rules, list(set(not_found)))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
636
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
637 ############################ split class ######################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
638
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
639 def split_class(classes, resolve_rules):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
640 class_pat = {}
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
641 for i in range(len(classes)):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
642 classe = classes.iloc[i, 1]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
643 if not pd.isnull(classe):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
644 l = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
645 for j in range(i, len(classes)):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
646 if classes.iloc[j, 1] == classe:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
647 pat_id = classes.iloc[j, 0]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
648 tmp = resolve_rules.get(pat_id, None)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
649 if tmp != None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
650 l.append(tmp)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
651 classes.iloc[j, 1] = None
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
652 if l:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
653 class_pat[classe] = list(map(list, zip(*l)))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
654 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
655 warning('Warning: no sample found in class ' + classe +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
656 ', the class has been disregarded\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
657 return class_pat
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
658
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
659 ############################ map ##############################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
660
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
661 def maps(core_map, class_pat, ids, threshold_P_V, threshold_F_C, create_svg, create_pdf, comparison, control):
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
662 args = process_args(sys.argv)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
663 if (not class_pat) or (len(class_pat.keys()) < 2):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
664 sys.exit('Execution aborted: classes provided for comparisons are ' +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
665 'less than two\n')
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
666
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
667 if comparison == "manyvsmany":
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
668 for i, j in it.combinations(class_pat.keys(), 2):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
669
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
670 tmp = {}
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
671 count = 0
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
672 max_F_C = 0
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
673 for l1, l2 in zip(class_pat.get(i), class_pat.get(j)):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
674 try:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
675 stat_D, p_value = st.ks_2samp(l1, l2)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
676 #sum(l1) da errore secondo me perchè ha null
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
677 avg = fold_change(sum(l1) / len(l1), sum(l2) / len(l2))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
678 if not isinstance(avg, str):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
679 if max_F_C < abs(avg):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
680 max_F_C = abs(avg)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
681 tmp[ids[count]] = [float(p_value), avg]
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
682 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
683 except (TypeError, ZeroDivisionError):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
684 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
685 tab = 'result/' + i + '_vs_' + j + ' (Tabular Result).tsv'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
686 tmp_csv = pd.DataFrame.from_dict(tmp, orient = "index")
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
687 tmp_csv = tmp_csv.reset_index()
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
688 header = ['ids', 'P_Value', 'Log2(fold change)']
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
689 tmp_csv.to_csv(tab, sep = '\t', index = False, header = header)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
690
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
691 if create_svg or create_pdf:
48
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
692 if args.custom_rules == 'false' or (args.custom_rules == 'true'
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
693 and args.custom_map != ''):
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
694 fix_map(tmp, core_map, threshold_P_V, threshold_F_C, max_F_C)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
695 file_svg = 'result/' + i + '_vs_' + j + ' (SVG Map).svg'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
696 with open(file_svg, 'wb') as new_map:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
697 new_map.write(ET.tostring(core_map))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
698
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
699
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
700 if create_pdf:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
701 file_pdf = 'result/' + i + '_vs_' + j + ' (PDF Map).pdf'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
702 renderPDF.drawToFile(svg2rlg(file_svg), file_pdf)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
703
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
704 if not create_svg:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
705 #Ho utilizzato il file svg per generare il pdf,
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
706 #ma l'utente non ne ha richiesto il ritorno, quindi
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
707 #lo elimino
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
708
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
709 os.remove('result/' + i + '_vs_' + j + ' (SVG Map).svg')
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
710 elif comparison == "onevsrest":
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
711 for single_cluster in class_pat.keys():
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
712 t = []
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
713 for k in class_pat.keys():
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
714 if k != single_cluster:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
715 t.append(class_pat.get(k))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
716 rest = []
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
717 for i in t:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
718 rest = rest + i
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
719
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
720 tmp = {}
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
721 count = 0
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
722 max_F_C = 0
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
723
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
724 for l1, l2 in zip(rest, class_pat.get(single_cluster)):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
725 try:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
726 stat_D, p_value = st.ks_2samp(l1, l2)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
727 avg = fold_change(sum(l1) / len(l1), sum(l2) / len(l2))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
728 if not isinstance(avg, str):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
729 if max_F_C < abs(avg):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
730 max_F_C = abs(avg)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
731 tmp[ids[count]] = [float(p_value), avg]
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
732 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
733 except (TypeError, ZeroDivisionError):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
734 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
735 tab = 'result/' + single_cluster + '_vs_rest (Tabular Result).tsv'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
736 tmp_csv = pd.DataFrame.from_dict(tmp, orient = "index")
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
737 tmp_csv = tmp_csv.reset_index()
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
738 header = ['ids', 'P_Value', 'Log2(fold change)']
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
739 tmp_csv.to_csv(tab, sep = '\t', index = False, header = header)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
740
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
741 if create_svg or create_pdf:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
742 if args.rules_selector == 'HMRcore' or (args.rules_selector == 'Custom'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
743 and args.yes_no == 'yes'):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
744 fix_map(tmp, core_map, threshold_P_V, threshold_F_C, max_F_C)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
745 file_svg = 'result/' + single_cluster + '_vs_ rest (SVG Map).svg'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
746 with open(file_svg, 'wb') as new_map:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
747 new_map.write(ET.tostring(core_map))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
748
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
749
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
750 if create_pdf:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
751 file_pdf = 'result/' + single_cluster + '_vs_ rest (PDF Map).pdf'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
752 renderPDF.drawToFile(svg2rlg(file_svg), file_pdf)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
753
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
754 if not create_svg:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
755 os.remove('result/' + single_cluster + '_vs_ rest (SVG Map).svg')
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
756
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
757 elif comparison == "onevsmany":
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
758 for i, j in it.combinations(class_pat.keys(), 2):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
759 tmp = {}
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
760 count = 0
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
761 max_F_C = 0
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
762 for l1, l2 in zip(class_pat.get(i), class_pat.get(j)):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
763 try:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
764 stat_D, p_value = st.ks_2samp(l1, l2)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
765 #sum(l1) da errore secondo me perchè ha null
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
766 avg = fold_change(sum(l1) / len(l1), sum(l2) / len(l2))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
767 if not isinstance(avg, str):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
768 if max_F_C < abs(avg):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
769 max_F_C = abs(avg)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
770 tmp[ids[count]] = [float(p_value), avg]
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
771 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
772 except (TypeError, ZeroDivisionError):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
773 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
774 tab = 'result/' + i + '_vs_' + j + ' (Tabular Result).tsv'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
775 tmp_csv = pd.DataFrame.from_dict(tmp, orient = "index")
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
776 tmp_csv = tmp_csv.reset_index()
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
777 header = ['ids', 'P_Value', 'Log2(fold change)']
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
778 tmp_csv.to_csv(tab, sep = '\t', index = False, header = header)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
779
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
780 if create_svg or create_pdf:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
781 if args.rules_selector == 'HMRcore' or (args.rules_selector == 'Custom'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
782 and args.yes_no == 'yes'):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
783 fix_map(tmp, core_map, threshold_P_V, threshold_F_C, max_F_C)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
784 file_svg = 'result/' + i + '_vs_' + j + ' (SVG Map).svg'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
785 with open(file_svg, 'wb') as new_map:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
786 new_map.write(ET.tostring(core_map))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
787
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
788
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
789 if create_pdf:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
790 file_pdf = 'result/' + i + '_vs_' + j + ' (PDF Map).pdf'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
791 renderPDF.drawToFile(svg2rlg(file_svg), file_pdf)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
792
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
793 if not create_svg:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
794 #Ho utilizzato il file svg per generare il pdf,
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
795 #ma l'utente non ne ha richiesto il ritorno, quindi
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
796 #lo elimino
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
797
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
798 os.remove('result/' + i + '_vs_' + j + ' (SVG Map).svg')
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
799
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
800
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
801
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
802
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
803 return None
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
804
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
805 ############################ MAIN #############################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
806
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
807 def main():
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
808 args = process_args(sys.argv)
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
809
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
810 create_svg = check_bool(args.generate_svg)
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
811 create_pdf = check_bool(args.generate_pdf)
35
7b1971251c63 Uploaded
bimib
parents: 34
diff changeset
812
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
813 if os.path.isdir('result') == False:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
814 os.makedirs('result')
48
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
815
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
816 if args.custom_rules == 'true':
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
817 ids, rules, gene_in_rule = make_recon(args.custom_rule)
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
818
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
819 class_pat = {}
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
820
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
821 if args.option == 'datasets':
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
822 num = 1
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
823 for i, j in zip(args.input_datas, args.names):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
824 name = name_dataset(j, num)
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
825 resolve_rules = read_dataset(i, name)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
826
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
827 resolve_rules.iloc[:, 0] = (resolve_rules.iloc[:, 0]).astype(str)
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
828
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
829 ids = pd.Series.tolist(resolve_rules.iloc[:, 0])
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
830
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
831 resolve_rules = resolve_rules.drop(resolve_rules.columns[[0]], axis=1)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
832 resolve_rules = resolve_rules.replace({'None': None})
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
833 resolve_rules = resolve_rules.to_dict('list')
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
834
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
835 #Converto i valori da str a float
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
836 to_float = lambda x: float(x) if (x != None) else None
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
837
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
838 resolve_rules_float = {}
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
839
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
840 for k in resolve_rules:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
841 resolve_rules_float[k] = list(map(to_float, resolve_rules[k])); resolve_rules_float
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
842
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
843 if resolve_rules != None:
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
844 class_pat[name] = list(map(list, zip(*resolve_rules_float.values())))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
845
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
846 num += 1
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
847
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
848 if args.option == 'dataset_class':
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
849 name = 'RAS'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
850 resolve_rules = read_dataset(args.input_data, name)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
851 resolve_rules.iloc[:, 0] = (resolve_rules.iloc[:, 0]).astype(str)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
852
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
853 ids = pd.Series.tolist(resolve_rules.iloc[:, 0])
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
854
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
855 resolve_rules = resolve_rules.drop(resolve_rules.columns[[0]], axis=1)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
856 resolve_rules = resolve_rules.replace({'None': None})
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
857 resolve_rules = resolve_rules.to_dict('list')
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
858
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
859 #Converto i valori da str a float
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
860 to_float = lambda x: float(x) if (x != None) else None
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
861
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
862 resolve_rules_float = {}
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
863
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
864 for k in resolve_rules:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
865 resolve_rules_float[k] = list(map(to_float, resolve_rules[k])); resolve_rules_float
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
866
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
867 classes = read_dataset(args.input_class, 'class')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
868 classes = classes.astype(str)
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
869
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
870 if resolve_rules_float != None:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
871 class_pat = split_class(classes, resolve_rules_float)
28
e6831924df01 small fixes (elbow plot and output managment)
bimib
parents: 16
diff changeset
872
48
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
873
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
874 if args.custom_rules == 'true':
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
875 try:
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
876 core_map = ET.parse(args.custom_map)
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
877 except (ET.XMLSyntaxError, ET.XMLSchemaParseError):
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
878 sys.exit('Execution aborted: custom map in wrong format')
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
879 else:
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
880 core_map = ET.parse(args.tool_dir+'/local/HMRcoreMap.svg')
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
881
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
882 maps(core_map, class_pat, ids, args.pValue, args.fChange, create_svg, create_pdf, args.comparison, args.control)
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
883
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
884 print('Execution succeded')
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
885
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
886 return None
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
887
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
888
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
889 ###############################################################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
890
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
891 if __name__ == "__main__":
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
892 main()