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

Uploaded
author bimib
date Wed, 03 Jun 2020 12:00:40 -0400
parents 19d704e24977
children 84da46340c39
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
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
541 ############################ gene #############################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
542
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
543 def data_gene(gene, type_gene, name, gene_custom):
35
7b1971251c63 Uploaded
bimib
parents: 34
diff changeset
544 args = process_args(sys.argv)
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
545 for i in range(len(gene)):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
546 tmp = gene.iloc[i, 0]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
547 if tmp.startswith(' ') or tmp.endswith(' '):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
548 gene.iloc[i, 0] = (tmp.lstrip()).rstrip()
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
549 gene_dup = [item for item, count in
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
550 collections.Counter(gene[gene.columns[0]]).items() if count > 1]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
551 pat_dup = [item for item, count in
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
552 collections.Counter(list(gene.columns)).items() if count > 1]
35
7b1971251c63 Uploaded
bimib
parents: 34
diff changeset
553
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
554 if gene_dup:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
555 if gene_custom == None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
556 if args.rules_selector == 'HMRcore':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
557 gene_in_rule = pk.load(open(args.tool_dir +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
558 '/local/HMRcore_genes.p', 'rb'))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
559 elif args.rules_selector == 'Recon':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
560 gene_in_rule = pk.load(open(args.tool_dir +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
561 '/local/Recon_genes.p', 'rb'))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
562 gene_in_rule = gene_in_rule.get(type_gene)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
563 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
564 gene_in_rule = gene_custom
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
565 tmp = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
566 for i in gene_dup:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
567 if gene_in_rule.get(i) == 'ok':
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
568 tmp.append(i)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
569 if tmp:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
570 sys.exit('Execution aborted because gene ID '
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
571 +str(tmp)+' in '+name+' is duplicated\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
572 if pat_dup:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
573 warning('Warning: duplicated label\n' + str(pat_dup) + 'in ' + name +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
574 '\n')
35
7b1971251c63 Uploaded
bimib
parents: 34
diff changeset
575
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
576 return (gene.set_index(gene.columns[0])).to_dict()
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
577
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
578 ############################ resolve ##########################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
579
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
580 def resolve(genes, rules, ids, resolve_none, name):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
581 resolve_rules = {}
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
582 not_found = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
583 flag = False
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
584 for key, value in genes.items():
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
585 tmp_resolve = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
586 for i in range(len(rules)):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
587 tmp = rules[i]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
588 if tmp:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
589 tmp, err = replace_gene_value(tmp, value)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
590 if err:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
591 not_found.extend(err)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
592 ris = control(None, tmp, resolve_none)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
593 if ris is False or ris == None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
594 tmp_resolve.append(None)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
595 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
596 tmp_resolve.append(ris)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
597 flag = True
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
598 else:
35
7b1971251c63 Uploaded
bimib
parents: 34
diff changeset
599 tmp_resolve.append(None)
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
600 resolve_rules[key] = tmp_resolve
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
601 if flag is False:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
602 warning('Warning: no computable score (due to missing gene values)' +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
603 'for class ' + name + ', the class has been disregarded\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
604 return (None, None)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
605 return (resolve_rules, list(set(not_found)))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
606
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
607 ############################ split class ######################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
608
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
609 def split_class(classes, resolve_rules):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
610 class_pat = {}
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
611 for i in range(len(classes)):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
612 classe = classes.iloc[i, 1]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
613 if not pd.isnull(classe):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
614 l = []
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
615 for j in range(i, len(classes)):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
616 if classes.iloc[j, 1] == classe:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
617 pat_id = classes.iloc[j, 0]
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
618 tmp = resolve_rules.get(pat_id, None)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
619 if tmp != None:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
620 l.append(tmp)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
621 classes.iloc[j, 1] = None
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
622 if l:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
623 class_pat[classe] = list(map(list, zip(*l)))
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
624 else:
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
625 warning('Warning: no sample found in class ' + classe +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
626 ', the class has been disregarded\n')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
627 return class_pat
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
628
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
629 ############################ map ##############################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
630
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
631 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
632 args = process_args(sys.argv)
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
633 if (not class_pat) or (len(class_pat.keys()) < 2):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
634 sys.exit('Execution aborted: classes provided for comparisons are ' +
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
635 'less than two\n')
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
636
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
637 if comparison == "manyvsmany":
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
638 for i, j in it.combinations(class_pat.keys(), 2):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
639 tmp = {}
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
640 count = 0
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
641 max_F_C = 0
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
642 for l1, l2 in zip(class_pat.get(i), class_pat.get(j)):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
643 try:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
644 stat_D, p_value = st.ks_2samp(l1, l2)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
645 #sum(l1) da errore secondo me perchè ha null
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
646 avg = fold_change(sum(l1) / len(l1), sum(l2) / len(l2))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
647 if not isinstance(avg, str):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
648 if max_F_C < abs(avg):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
649 max_F_C = abs(avg)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
650 tmp[ids[count]] = [float(p_value), avg]
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
651 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
652 except (TypeError, ZeroDivisionError):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
653 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
654 tab = 'result/' + i + '_vs_' + j + ' (Tabular Result).tsv'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
655 tmp_csv = pd.DataFrame.from_dict(tmp, orient = "index")
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
656 tmp_csv = tmp_csv.reset_index()
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
657 header = ['ids', 'P_Value', 'Log2(fold change)']
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
658 tmp_csv.to_csv(tab, sep = '\t', index = False, header = header)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
659
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
660 if create_svg or create_pdf:
48
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
661 if args.custom_rules == 'false' or (args.custom_rules == 'true'
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
662 and args.custom_map != ''):
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
663 fix_map(tmp, core_map, threshold_P_V, threshold_F_C, max_F_C)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
664 file_svg = 'result/' + i + '_vs_' + j + ' (SVG Map).svg'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
665 with open(file_svg, 'wb') as new_map:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
666 new_map.write(ET.tostring(core_map))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
667
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
668
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
669 if create_pdf:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
670 file_pdf = 'result/' + i + '_vs_' + j + ' (PDF Map).pdf'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
671 renderPDF.drawToFile(svg2rlg(file_svg), file_pdf)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
672
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
673 if not create_svg:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
674 os.remove('result/' + i + '_vs_' + j + ' (SVG Map).svg')
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
675 elif comparison == "onevsrest":
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
676 for single_cluster in class_pat.keys():
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
677 t = []
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
678 for k in class_pat.keys():
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
679 if k != single_cluster:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
680 t.append(class_pat.get(k))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
681 rest = []
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
682 for i in t:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
683 rest = rest + i
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
684
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
685 tmp = {}
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
686 count = 0
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
687 max_F_C = 0
69
19d704e24977 Uploaded
bimib
parents: 65
diff changeset
688
19d704e24977 Uploaded
bimib
parents: 65
diff changeset
689 primo = -1
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
690
69
19d704e24977 Uploaded
bimib
parents: 65
diff changeset
691 for l1, l2 in zip(class_pat.get(single_cluster), rest):
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
692 try:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
693 stat_D, p_value = st.ks_2samp(l1, l2)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
694 avg = fold_change(sum(l1) / len(l1), sum(l2) / len(l2))
69
19d704e24977 Uploaded
bimib
parents: 65
diff changeset
695 if primo == -1:
19d704e24977 Uploaded
bimib
parents: 65
diff changeset
696 primo = 0
19d704e24977 Uploaded
bimib
parents: 65
diff changeset
697 print(avg)
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
698 if not isinstance(avg, str):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
699 if max_F_C < abs(avg):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
700 max_F_C = abs(avg)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
701 tmp[ids[count]] = [float(p_value), avg]
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
702 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
703 except (TypeError, ZeroDivisionError):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
704 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
705 tab = 'result/' + single_cluster + '_vs_rest (Tabular Result).tsv'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
706 tmp_csv = pd.DataFrame.from_dict(tmp, orient = "index")
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
707 tmp_csv = tmp_csv.reset_index()
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
708 header = ['ids', 'P_Value', 'Log2(fold change)']
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
709 tmp_csv.to_csv(tab, sep = '\t', index = False, header = header)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
710
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
711 if create_svg or create_pdf:
65
a61733753aec Uploaded
bimib
parents: 64
diff changeset
712 if args.custom_rules == 'false' or (args.custom_rules == 'true'
a61733753aec Uploaded
bimib
parents: 64
diff changeset
713 and args.custom_map != ''):
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
714 fix_map(tmp, core_map, threshold_P_V, threshold_F_C, max_F_C)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
715 file_svg = 'result/' + single_cluster + '_vs_ rest (SVG Map).svg'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
716 with open(file_svg, 'wb') as new_map:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
717 new_map.write(ET.tostring(core_map))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
718
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
719
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
720 if create_pdf:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
721 file_pdf = 'result/' + single_cluster + '_vs_ rest (PDF Map).pdf'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
722 renderPDF.drawToFile(svg2rlg(file_svg), file_pdf)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
723
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
724 if not create_svg:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
725 os.remove('result/' + single_cluster + '_vs_ rest (SVG Map).svg')
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
726
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
727 elif comparison == "onevsmany":
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
728 for i, j in it.combinations(class_pat.keys(), 2):
65
a61733753aec Uploaded
bimib
parents: 64
diff changeset
729 if i != control and j != control:
a61733753aec Uploaded
bimib
parents: 64
diff changeset
730 continue
a61733753aec Uploaded
bimib
parents: 64
diff changeset
731 if i == control and j == control:
a61733753aec Uploaded
bimib
parents: 64
diff changeset
732 continue
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
733 tmp = {}
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
734 count = 0
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
735 max_F_C = 0
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
736 for l1, l2 in zip(class_pat.get(i), class_pat.get(j)):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
737 try:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
738 stat_D, p_value = st.ks_2samp(l1, l2)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
739 #sum(l1) da errore secondo me perchè ha null
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
740 avg = fold_change(sum(l1) / len(l1), sum(l2) / len(l2))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
741 if not isinstance(avg, str):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
742 if max_F_C < abs(avg):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
743 max_F_C = abs(avg)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
744 tmp[ids[count]] = [float(p_value), avg]
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
745 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
746 except (TypeError, ZeroDivisionError):
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
747 count += 1
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
748 tab = 'result/' + i + '_vs_' + j + ' (Tabular Result).tsv'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
749 tmp_csv = pd.DataFrame.from_dict(tmp, orient = "index")
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
750 tmp_csv = tmp_csv.reset_index()
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
751 header = ['ids', 'P_Value', 'Log2(fold change)']
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
752 tmp_csv.to_csv(tab, sep = '\t', index = False, header = header)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
753
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
754 if create_svg or create_pdf:
65
a61733753aec Uploaded
bimib
parents: 64
diff changeset
755 if args.custom_rules == 'false' or (args.custom_rules == 'true'
a61733753aec Uploaded
bimib
parents: 64
diff changeset
756 and args.custom_map != ''):
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
757 fix_map(tmp, core_map, threshold_P_V, threshold_F_C, max_F_C)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
758 file_svg = 'result/' + i + '_vs_' + j + ' (SVG Map).svg'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
759 with open(file_svg, 'wb') as new_map:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
760 new_map.write(ET.tostring(core_map))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
761
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
762
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
763 if create_pdf:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
764 file_pdf = 'result/' + i + '_vs_' + j + ' (PDF Map).pdf'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
765 renderPDF.drawToFile(svg2rlg(file_svg), file_pdf)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
766
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
767 if not create_svg:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
768 os.remove('result/' + i + '_vs_' + j + ' (SVG Map).svg')
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
769
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
770
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
771
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
772
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
773 return None
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
774
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
775 ############################ MAIN #############################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
776
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
777 def main():
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
778 args = process_args(sys.argv)
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
779
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
780 create_svg = check_bool(args.generate_svg)
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
781 create_pdf = check_bool(args.generate_pdf)
35
7b1971251c63 Uploaded
bimib
parents: 34
diff changeset
782
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
783 if os.path.isdir('result') == False:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
784 os.makedirs('result')
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
785
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
786 class_pat = {}
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
787
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
788 if args.option == 'datasets':
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
789 num = 1
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
790 for i, j in zip(args.input_datas, args.names):
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
791 name = name_dataset(j, num)
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
792 resolve_rules = read_dataset(i, name)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
793
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
794 resolve_rules.iloc[:, 0] = (resolve_rules.iloc[:, 0]).astype(str)
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
795
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
796 ids = pd.Series.tolist(resolve_rules.iloc[:, 0])
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
797
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
798 resolve_rules = resolve_rules.drop(resolve_rules.columns[[0]], axis=1)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
799 resolve_rules = resolve_rules.replace({'None': None})
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
800 resolve_rules = resolve_rules.to_dict('list')
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
801
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
802 #Converto i valori da str a float
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
803 to_float = lambda x: float(x) if (x != None) else None
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
804
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
805 resolve_rules_float = {}
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
806
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
807 for k in resolve_rules:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
808 resolve_rules_float[k] = list(map(to_float, resolve_rules[k])); resolve_rules_float
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
809
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
810 if resolve_rules != None:
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
811 class_pat[name] = list(map(list, zip(*resolve_rules_float.values())))
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
812
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
813 num += 1
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
814
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
815 if args.option == 'dataset_class':
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
816 name = 'RAS'
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
817 resolve_rules = read_dataset(args.input_data, name)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
818 resolve_rules.iloc[:, 0] = (resolve_rules.iloc[:, 0]).astype(str)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
819
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
820 ids = pd.Series.tolist(resolve_rules.iloc[:, 0])
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
821
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
822 resolve_rules = resolve_rules.drop(resolve_rules.columns[[0]], axis=1)
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
823 resolve_rules = resolve_rules.replace({'None': None})
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
824 resolve_rules = resolve_rules.to_dict('list')
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
825
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
826 #Converto i valori da str a float
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
827 to_float = lambda x: float(x) if (x != None) else None
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
828
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
829 resolve_rules_float = {}
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
830
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
831 for k in resolve_rules:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
832 resolve_rules_float[k] = list(map(to_float, resolve_rules[k])); resolve_rules_float
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
833
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
834 classes = read_dataset(args.input_class, 'class')
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
835 classes = classes.astype(str)
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
836
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
837 if resolve_rules_float != None:
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
838 class_pat = split_class(classes, resolve_rules_float)
28
e6831924df01 small fixes (elbow plot and output managment)
bimib
parents: 16
diff changeset
839
48
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
840
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
841 if args.custom_rules == 'true':
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
842 try:
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
843 core_map = ET.parse(args.custom_map)
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
844 except (ET.XMLSyntaxError, ET.XMLSchemaParseError):
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
845 sys.exit('Execution aborted: custom map in wrong format')
e4235b5231e4 Uploaded
bimib
parents: 47
diff changeset
846 else:
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
847 core_map = ET.parse(args.tool_dir+'/local/HMRcoreMap.svg')
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
848
65
a61733753aec Uploaded
bimib
parents: 64
diff changeset
849 class_pat_trim = {}
a61733753aec Uploaded
bimib
parents: 64
diff changeset
850
a61733753aec Uploaded
bimib
parents: 64
diff changeset
851 for key in class_pat.keys():
a61733753aec Uploaded
bimib
parents: 64
diff changeset
852 class_pat_trim[key.strip()] = class_pat[key]
a61733753aec Uploaded
bimib
parents: 64
diff changeset
853
a61733753aec Uploaded
bimib
parents: 64
diff changeset
854
a61733753aec Uploaded
bimib
parents: 64
diff changeset
855 maps(core_map, class_pat_trim, ids, args.pValue, args.fChange, create_svg, create_pdf, args.comparison, args.control)
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
856
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
857 print('Execution succeded')
47
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
858
3af9d394367c Uploaded
bimib
parents: 35
diff changeset
859 return None
16
c71ac0bb12de Uploaded
bimib
parents: 13
diff changeset
860
0
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
861
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
862 ###############################################################################
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
863
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
864 if __name__ == "__main__":
23ac9cf12788 Uploaded
bimib
parents:
diff changeset
865 main()