annotate Marea/ras_generator.py @ 46:5d5d01ef1d68 draft

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