annotate marea-1.0.1/marea.py @ 15:d0e7f14b773f draft

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