-
Notifications
You must be signed in to change notification settings - Fork 1
/
BIgMAG_functions.py
223 lines (204 loc) · 7.14 KB
/
BIgMAG_functions.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 21 18:38:18 2024
@author: yepesgar
"""
import pandas as pd
from html.parser import HTMLParser
def labels_gunc():
labels = ['n_genes_called',
'n_genes_mapped',
'n_contigs',
'proportion_genes_retained_in_major_clades',
'genes_retained_index',
'clade_separation_score',
'contamination_portion',
'n_effective_surplus_clades',
'mean_hit_identity',
'reference_representation_score',
]
return labels
def labels_quast():
labels = ["# contigs (>= 0 bp)",
"# contigs (>= 1000 bp)",
"# contigs (>= 5000 bp)",
"# contigs (>= 10000 bp)",
"# contigs (>= 25000 bp)",
"# contigs (>= 50000 bp)",
"Total length (>= 0 bp)",
"Total length (>= 1000 bp)",
"Total length (>= 5000 bp)",
"Total length (>= 10000 bp)",
"Total length (>= 25000 bp)",
"Total length (>= 50000 bp)",
"# contigs",
"Largest contig",
"Total length",
"GC (%)",
"N50",
"N90",
"auN",
"L50",
"L90",
"# N's per 100 kbp"
]
return labels
def labels_summary():
labels = [ "N_annotated_bins",
"N_unique_annotated_bins",
"N_mid_quality_MAGs",
"N_high_quality_MAGs",
"N_bins_passing_GUNC",
"N_bins"
]
return labels
def params_heatmap():
parameters = ['Completeness',
'Contamination',
'Complete',
'Single',
'Duplicated',
'Fragmented',
'Missing',
'proportion_genes_retained_in_major_clades',
'genes_retained_index',
'clade_separation_score',
'contamination_portion',
'n_effective_surplus_clades',
'mean_hit_identity',
'reference_representation_score',
]
return parameters
def params_to_normalize():
parameters = ['Completeness',
'Contamination',
'Complete',
'Single',
'Duplicated',
'Fragmented',
'Missing']
return parameters
def names_heatmap():
names = [ 'Completeness (CheckM2)',
'Contamination (CheckM2)',
'Complete SCO (BUSCO)',
'Single SCO (BUSCO)',
'Duplicated SCO (BUSCO)',
'Fragmented SCO (BUSCO)',
'Missing SCO (BUSCO)',
'proportion_genes_retained_in_major_clades (GUNC)',
'genes_retained_index (GUNC)',
'clade_separation_score (GUNC)',
'contamination_portion (GUNC)',
'n_effective_surplus_clades (GUNC)',
'mean_hit_identity (GUNC)',
'reference_representation_score (GUNC)',
'Proportion of bins passing the filter (GUNC)',
]
return names
def labels_GTDB_Tk2 ():
labels = [ 'Domain',
'Phylum',
'Class',
'Order',
'Family',
'Genus',
'Species'
]
return labels
def extract_genus(pd_series, tax_level):
data = pd.Series()
extract = pd_series
for i in range(len(extract)):
if pd.notna(extract[i]):
data = pd.concat([data, pd.Series(';' + extract[i])])
else:
data = pd.concat([data, pd.Series('NaN')])
data = data.reset_index(drop=True)
string = ''
my_list = []
column_names = ['Domain',
'Phylum',
'Class',
'Order',
'Family',
'Genus',
'Species'
]
df = pd.DataFrame(columns=column_names)
for i in data:
if i != ';Unclassified Bacteria' and i != ';Unclassified' and i != ';Unclassified Archaea' and i != 'NaN':
for j in reversed(range(len(i))):
if i[j] != ';':
string += i[j]
else:
my_list.append(string)
string = ''
if len(my_list) == 7:
for i in range(len(my_list)):
my_list[i] = my_list[i][::-1]
for i in range(len(my_list)):
my_list[i] = my_list[i][3:]
my_list.reverse()
df.loc[len(df)] = my_list
my_list = []
else:
my_list = ['Unclassified'] * 7
df.loc[len(df)] = my_list
my_list = []
df = df.replace('','Unclassified')
df = df.fillna('Unclassified')
return pd.Series(df[tax_level])
def patch_file(file_path: str, content: bytes, extra: dict = None) -> bytes:
if file_path == 'bigmag.html':
index_html_content = content.decode('utf8')
extra_jsons = f'''
var patched_jsons_content={{
{','.join(["'/" + k + "':" + v.decode("utf8") + "" for k, v in extra.items()])}
}};
'''
patched_content = index_html_content.replace(
'<footer>',
f'''
<footer>
<script>
''' + extra_jsons + '''
const origFetch = window.fetch;
window.fetch = function () {
const e = arguments[0]
if (patched_jsons_content.hasOwnProperty(e)) {
return Promise.resolve({
json: () => Promise.resolve(patched_jsons_content[e]),
headers: new Headers({'content-type': 'application/json'}),
status: 200,
});
} else {
return origFetch.apply(this, arguments)
}
}
</script>
'''
).replace(
'href="/',
'href="'
).replace(
'src="/',
'src="'
)
return patched_content.encode('utf8')
else:
return content
class ExternalResourceParser(HTMLParser):
def __init__(self):
super().__init__()
self.resources = []
def handle_starttag(self, tag, attrs):
if tag == 'link':
for k, v in attrs:
if k == 'href':
self.resources.append(v)
if tag == 'script':
for k, v in attrs:
if k == 'src':
self.resources.append(v)