-
Notifications
You must be signed in to change notification settings - Fork 22
/
pathway_parser.py
164 lines (101 loc) · 4.06 KB
/
pathway_parser.py
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
# -*- coding: utf-8 -*-
### This file is a part of iPANDA package
### (in silico Pathway Activation Network Decomposition Analysis)
### The package is distributed under iPANDA license
### Version 0.0.1
###
### Copyright © 2016 Insilico Medicine Inc.
###
### USA, Johns Hopkins University, ETC B301,
### 1101 East 33rd St. Baltimore, MD 21218
import numpy as np
import os
from my_loadtxt import loadtxt
import warnings
class pathway():
'''class representing pathway as adjacency matrix'''
def __init__(self, pathway_contents,name):
'''Read pathway file to matrix'''
self.ARR_algorithms_dict = {\
'original':self.get_original_ARRs\
}
self.gene_vect = np.array([],dtype=np.str)
self.expand_gene_list(pathway_contents)
self.name = name
def expand_gene_list(self,new_gene_vect):
'''Expand gene list using pathway content file'''
gene_vect = list(self.gene_vect)
new_genes = list(new_gene_vect)
gene_vect.extend(new_genes)
genes = np.unique(gene_vect)
genes = np.array([gene for gene in genes if gene != ''])
self.gene_vect = genes
def get_ARRs(self, algorithm = 'dist_sum', args = {}):
'''Calculate ARR koefficients using given algorithm
List of availible algorithms:
dist_sum - simple sum of pairwize distances between nodes
'''
alg_call = self.ARR_algorithms_dict[algorithm]
return alg_call(**args)
def clean_gene_vect(self,gene_dict):
'''Remove genes which are not in ARR_list'''
gene_vect = list(self.gene_vect)
gene_array = list(gene_dict[self.name])
genes_to_remove = []
for gene in gene_vect:
if gene_array.count(gene) == 0:
genes_to_remove.append(gene)
for gene in genes_to_remove:
gene_vect.remove(gene)
self.gene_vect = np.array(gene_vect,dtype=np.str)
def get_original_ARRs(self,gene_dict,arr_dict):
'''Get ARRs from reference files'''
return self.extract_ARRs_from_text_files(gene_dict,arr_dict,False)
def extract_ARRs_from_text_files(self, gene_dict, arr_dict, \
sign_only=True):
'''Get array of signs from list of ARRs for all pathways'''
gene_array = gene_dict[self.name]
fig_array = arr_dict[self.name]
gene_array = [x for x in gene_array if x!='']
fig_array = [float(x) for x in fig_array if x!='']
gene_vect = self.gene_vect
fin_list = []
if sign_only:
sign_dict = {}
sign_list = []
for fig in fig_array:
if fig == 0: sign_list.append( 0.)
if fig > 0: sign_list.append( 1.)
if fig < 0: sign_list.append(-1.)
for i in range(len(gene_array)):
sign_dict[gene_array[i]] = sign_list[i]
for gene in gene_vect:
if sign_dict.has_key(gene):
fin_list.append(sign_dict[gene])
else:
fin_list.append(0.)
else:
val_dict = {}
for i in range(len(gene_array)):
val_dict[gene_array[i]] = fig_array[i]
for gene in gene_vect:
if val_dict.has_key(gene):
fin_list.append(val_dict[gene])
else:
fin_list.append(0.)
return fin_list
def get_sign_array_from_text(self, ref_file):
'''Get sign array from text file with ARRs'''
sign_dict = {}
with open(ref_file, 'r') as sign_file:
for line in sign_file:
if not line.startswith('name'):
l_key,l_value = line.split(',')
l_value = float(l_value)
if l_value == 0:
l_value = 0
else:
l_value = l_value/np.abs(l_value)
sign_dict[l_key] = l_value
gene_vect = self.gene_vect
return [sign_dict[gene] for gene in gene_vect]