annotate Desktop/Marea/marea.py @ 30:e88efefbd015 draft

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