-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
150 lines (115 loc) · 4.33 KB
/
core.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
from typing import List, Tuple
import pandas as pd
import seaborn as sns
class PathwayDatabase:
DATABASES = {
'hallmark': 'h.all.v2023.2.Hs.symbols.gmt',
'canonical pathways': 'c2.cp.v2023.2.Hs.symbols.gmt',
'reactome': 'c2.cp.reactome.v2023.2.Hs.symbols.gmt',
'transcription factor targets': 'c3.tft.v2023.2.Hs.symbols.gmt'
}
def __init__(self, pathways: dict) -> None:
self.pathways = pathways
def __getitem__(self, pathway: str) -> List[str] | None:
if pathway in self.pathways:
return self.pathways[pathway]
def search(self, query: str, by_gene: bool = False) -> List[str]:
if by_gene:
return [pathway for pathway in self.pathways if query in self.pathways[pathway]]
else:
return [pathway for pathway in self.pathways if query in pathway]
@classmethod
def load(cls, path: str, database: str):
pathways = {}
with open(f'{path}/{cls.DATABASES[database]}') as file:
for line in file:
pathway, _, *genes = line.removesuffix('\n').split('\t')
pathways[pathway] = genes
return cls(pathways)
@classmethod
@property
def collection(cls) -> List:
return list(cls.DATABASES)
def read(filepath: str) -> pd.DataFrame:
match filepath.split('.')[-1]:
case 'csv':
sep = ','
case 'tsv':
sep = '\t'
case _:
raise Exception
return pd.read_csv(filepath, sep=sep, index_col=0)
def significance(p_value: float) -> str:
if p_value < 0.001:
return '***'
elif p_value < 0.01:
return '**'
elif p_value < 0.05:
return '*'
else:
return ''
from typing import List, Tuple
import pandas as pd
import seaborn as sns
class PathwayDatabase:
DATABASES = {
'hallmark': 'h.all.v2023.2.Hs.symbols.gmt',
'canonical pathways': 'c2.cp.v2023.2.Hs.symbols.gmt',
'reactome': 'c2.cp.reactome.v2023.2.Hs.symbols.gmt',
'transcription factor targets': 'c3.tft.v2023.2.Hs.symbols.gmt'
}
def __init__(self, pathways: dict) -> None:
self.pathways = pathways
def __getitem__(self, pathway: str) -> List[str] | None:
if pathway in self.pathways:
return self.pathways[pathway]
def search(self, query: str, by_gene: bool = False) -> List[str]:
if by_gene:
return [pathway for pathway in self.pathways if query in self.pathways[pathway]]
else:
return [pathway for pathway in self.pathways if query in pathway]
@classmethod
def load(cls, path: str, database: str):
pathways = {}
with open(f'{path}/{cls.DATABASES[database]}') as file:
for line in file:
pathway, _, *genes = line.removesuffix('\n').split('\t')
pathways[pathway] = genes
return cls(pathways)
@classmethod
@property
def collection(cls) -> List:
return list(cls.DATABASES)
def read(filepath: str) -> pd.DataFrame:
match filepath.split('.')[-1]:
case 'csv':
sep = ','
case 'tsv':
sep = '\t'
case _:
raise Exception
return pd.read_csv(filepath, sep=sep, index_col=0)
def significance(p_value: float) -> str:
if p_value < 0.001:
return '***'
elif p_value < 0.01:
return '**'
elif p_value < 0.05:
return '*'
else:
return ''
def heatmap(data: pd.DataFrame, annot: pd.DataFrame | None, method: str, metric: str, vertical: bool, cbar_pos: Tuple[float] = None, figsize: Tuple[float] = None):
n_rows, n_cols = data.shape
data.index.name = ''
return sns.clustermap(data=data,
method=method,
metric=metric,
cbar_kws={'label': 'log2FoldChange'},
figsize=figsize or (n_cols * 2, n_rows * 0.5),
cmap='bwr',
center=0 if annot is not None else None,
cbar_pos=cbar_pos,
annot=annot.map(significance) if annot is not None else None,
fmt='',
z_score=0 if annot is None else None,
col_cluster=vertical)