-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrnaseq_subgraphs.py
More file actions
executable file
·257 lines (234 loc) · 11.2 KB
/
Copy pathrnaseq_subgraphs.py
File metadata and controls
executable file
·257 lines (234 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
'''
Finds deregulated subgraphs based on RNASeq gene expression in TCGA
'''
import os
import argparse
import igraph as ig
from biomap import BioMap
from deregnet.core import SubgraphFinder
from deregnet_tcga.scores import (get_rnaseq_score,
get_global_rnaseq_score)
from deregnet_tcga.layers import Layers
from deregnet_tcga.graph import (get_expression_induced_subgraph,
prepare_expression_indicator)
__FILEDIR__ = os.path.dirname(os.path.abspath(__file__))
GRAPH_PATH = os.path.join(__FILEDIR__, 'graph', 'kegg_hsa.graphml')
PATIENT_SPECIFIC_LAYERS = ['snv_as_roots', 'snv_as_terminals']
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--layer', type=str, default='null',
help='Which predefined layer to use')
parser.add_argument('-d', '--dataset', type=str, default='uvm',
help='Which TCGA dataset to use')
parser.add_argument('-m', '--mode', type=str, default='deregulated',
help='Which mode to choose: deregulated, upregulated,'
' downregulated')
parser.add_argument('-t', '--time-limit', type=int, default=600,
help='Maximal time to search for a subgraph')
parser.add_argument('-g', '--gap-cut', type=float, default=None,
help='Gap cut to stop optimization prematurely')
parser.add_argument('--min-size', type=int, default=10,
help='Minimal subgraph size.')
parser.add_argument('--min-num-terminals', type=int, default=0,
help='Minimal number of terminals in subgraph.')
parser.add_argument('--exclude', type=str, default='',
help='Which nodes to exclude from subgraphs.')
parser.add_argument('-n', '--normalize-wrt', type=str, default='control',
help='Normalization strategy for RNASeq data')
parser.add_argument('--suboptimal', action='store_true',
help='Whether to find next best suboptimal subgraph.')
parser.add_argument('--max-suboptimal', type=int, default=0,
help='Whether to find next best suboptimal subgraph.')
parser.add_argument('--global', dest='_global', action='store_true',
help='Find global subgraphs.')
parser.add_argument('--global-score', dest='global_score', type=str, default='log2fold',
help='Log2Fold or Binary.')
return parser.parse_args()
def prepare_rnaseq_score(dataset, compare_to):
id_mapper = BioMap().get_mapper('hgnc')
rnaseq_score = get_rnaseq_score(dataset, compare_to=compare_to)
rnaseq_score.index = [gene.split('.')[0] for gene in rnaseq_score.index]
rnaseq_score.index = list(id_mapper.map(list(rnaseq_score.index),
FROM='ensembl',
TO='entrez'))
return rnaseq_score
def get_mode_args(mode):
if mode == 'deregulated':
abs_vals = True
minmax = 'max'
else:
abs_vals = False
if mode == 'upregulated':
minmax = 'max'
elif mode == 'downregulated':
minmax = 'min'
else:
raise ValueError
return abs_vals, minmax
def write_fails(failed, base_path):
with open(os.path.join(base_path, 'failed.txt'), 'w') as fp:
for fail in failed:
fp.write(fail+'\n')
def read_fails(base_path):
try:
open(os.path.join(base_path, 'failed.txt'), 'x')
except:
pass
with open(os.path.join(base_path, 'failed.txt'), 'r') as fp:
fails = [fail.strip() for fail in fp.read().split('\n')]
return [fail for fail in fails if fail]
return []
def global_rnaseq_subgraphs(args):
graph = ig.Graph.Read_GraphML(GRAPH_PATH)
mode, orientation = args.mode.split('/')
flip = True if orientation == 'upstream' else False
base_path = os.path.join('rnaseq/global',
args.dataset,
args.global_score,
mode,
orientation,
args.layer)
receptors, terminals = Layers.get(args.layer)
if not os.path.isdir(base_path):
os.makedirs(base_path)
score = get_global_rnaseq_score(args.dataset, args.global_score)
abs_vals, minmax = get_mode_args(mode)
finder = SubgraphFinder(graph)
try:
result = finder.run_average_deregnet(score,
min_size=args.min_size,
max_size=50,
time_limit=args.time_limit,
gap_cut=args.gap_cut,
abs_values=abs_vals,
model_sense=minmax,
flip_orientation=flip,
receptors=receptors,
terminals=terminals,
num_suboptimal=args.max_suboptimal,
max_overlap=0.2,
min_num_terminals=args.min_num_terminals)
except:
print('could not find subgraphs')
return
try:
result.to_graphml(path=base_path)
except:
print('could not write subgraphs.')
return
def local_rnaseq_subgraphs(args):
hgnc = BioMap().get_mapper('hgnc')
graph = ig.Graph.Read_GraphML(GRAPH_PATH)
#
rnaseq_score = prepare_rnaseq_score(args.dataset,
compare_to=args.normalize_wrt)
expression_ind_tumor = prepare_expression_indicator(args.dataset, threshold=100, tumor=True)
expression_ind_control = prepare_expression_indicator(args.dataset, threshold=100, tumor=False)
patients = list(rnaseq_score.columns)
abs_vals, minmax = get_mode_args(args.mode)
base_path = os.path.join('rnaseq', args.layer, args.mode, args.dataset)
if not os.path.isdir(base_path):
os.makedirs(base_path)
failed = set(read_fails(base_path))
for patient_id in patients:
path = os.path.join(base_path, patient_id)
if not os.path.isdir(path):
os.makedirs(path)
if patient_id in failed:
print('Ignoring previously failed patient %s.' % patient_id)
continue
suboptimality_index = 0
suboptimality_excludes = set()
print(path)
print(os.listdir(path))
subgraphs = os.listdir(path)
suboptimal_subgraphs = set(subgraphs) - {'optimal.graphml'}
if not os.path.isfile(os.path.join(path, 'optimal.graphml')):
suboptimality_index = 0
else:
print({f.split('_')[1].split('.')[0] for f in suboptimal_subgraphs})
suboptimality_index = max({int(f.split('_')[1].split('.')[0]) for f in suboptimal_subgraphs} | {0}) + 1
print('Suboptimality index: %d' % suboptimality_index)
for f in subgraphs:
fpath = os.path.join(path, f)
nodes = set(ig.Graph.Read_GraphML(fpath).vs['entrez'])
suboptimality_excludes = suboptimality_excludes.union(nodes)
if (suboptimality_index == 1 and not args.suboptimal):
print('optimal found. Not going for suboptimal_1.')
continue
if (suboptimality_index > 1 and not args.suboptimal):
print('suboptimal_%d found. Not going for suboptimal_%d.' %(suboptimality_index-1, suboptimality_index))
continue
if (suboptimality_index > args.max_suboptimal):
print('Suboptimality index exceeding maximum suboptimality index.')
continue
try:
expression_induced_graph = \
get_expression_induced_subgraph(graph,
patient_id,
expression_ind_tumor,
expression_ind_control)
print(patient_id,
': #nodes ',
str(len(expression_induced_graph.vs)),
' #edges ',
str(len(expression_induced_graph.es)))
except Exception:
failed.add(patient_id)
print('No mRNA expression for ', patient_id)
continue
finder = SubgraphFinder(expression_induced_graph)
score = rnaseq_score[patient_id].to_dict()
if args.layer in PATIENT_SPECIFIC_LAYERS:
receptors, terminals = Layers.get(args.layer,
cancer_type=args.dataset.upper(),
patient=patient_id)
else:
receptors, terminals = Layers.get(args.layer)
flip = True if args.layer.startswith('terminal') else False
if args.layer == 'snv_as_terminals':
flip = True
if args.layer.startswith('terminal') or args.layer.startswith('rooted'):
if receptors and len([g for g in receptors if expression_induced_graph.vs.select(entrez_eq=g)]) == 0:
continue
if terminals and len([g for g in terminals if expression_induced_graph.vs.select(entrez_eq=g)]) == 0:
continue
result = None
exclude = hgnc.map(args.exclude.split(','), TO='entrez')
exclude = set(exclude).union(suboptimality_excludes)
if args.layer in {'snv_as_terminals', 'snv_as_roots'}:
receptor_excludes = set(receptors) if receptors else set()
terminal_excludes = set(terminals) if terminals else set()
exclude = exclude.difference(set(receptor_excludes), set(terminal_excludes))
exclude = [gene for gene in exclude if gene]
print(exclude)
try:
result = finder.run_average_deregnet(score,
min_size=args.min_size,
max_size=50,
time_limit=args.time_limit,
gap_cut=args.gap_cut,
abs_values=abs_vals,
model_sense=minmax,
receptors=receptors,
terminals=terminals,
flip_orientation=flip,
min_num_terminals=args.min_num_terminals,
excluded_nodes=exclude)
except:
failed.add(patient_id)
try:
name = 'optimal.graphml'
if suboptimality_index > 0:
name = 'suboptimal_'+str(suboptimality_index)+'.graphml'
result.optimal.write_graphml(os.path.join(path, name))
except:
failed.add(patient_id)
write_fails(failed, base_path)
def main(args):
if args._global:
global_rnaseq_subgraphs(args)
else:
local_rnaseq_subgraphs(args)
if __name__ == '__main__':
main(parse_args())