annotate Marea/marea.py @ 12:3d77287caf22 draft

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