-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAFE_enrichment.py
275 lines (206 loc) · 11.3 KB
/
SAFE_enrichment.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3.10
#@uthor Giorgia Del Missier
import argparse, sys, os
import pandas as pd
import numpy as np
import multiprocessing
import altair as alt
from altair_saver import save
np.random.seed(0)
sys.path.append(f'{os.getcwd()}/safepy')
import safe
parser = argparse.ArgumentParser()
parser.add_argument('--input', required=True,
help='input: file containing complete annotation for each cluster (.txt file)')
parser.add_argument('--radius', required=True,
help='neighborhood radius for SAFE analysis')
parser.add_argument('--edgelist', required=True,
help='input: file containing network edge list with respective weights (.txt file)')
parser.add_argument('--wd', required=True,
help='working directory')
parser.add_argument('--output', required=True,
help='output: file name containing improved annotation')
parser.add_argument('--outfig', required=True,
help='output: figure name for summary statistics of improved annotation')
parser.add_argument('--nan', required=False,
help='output: file name containing IDs of uncharacterised proteins')
parser.add_argument('--identifiers', required=True,
help="foldseek file to retrieve UniProt identifiers from target organism")
parser.add_argument('--iteration', required=True,
help="iteration number")
args = parser.parse_args()
def process_identifier(args_list):
i, df = args_list
subdf = df[['#Cluster', 'UniProt ID', i]]
flattened_ids = subdf[i].dropna().astype(str).str.split(';').explode()
final_matrix = pd.DataFrame()
if not flattened_ids.empty:
unique_ids = flattened_ids.dropna().unique()
final_matrix = pd.DataFrame(columns=unique_ids)
for index, row in subdf.iterrows():
protein = row['UniProt ID']
ids = row[i]
if pd.isna(ids):
ids = list()
final_matrix.loc[protein] = [1 if j in ids else 0 for j in unique_ids]
output_file = f"{args.wd}/SAFE/{i}_matrix.txt"
final_matrix.to_csv(output_file, sep='\t')
def perform_safe_analysis(i, wd, radius, sf, enrichment_thr = 0.05):
sf.load_attributes(attribute_file=f'SAFE/{i}_matrix.txt')
sf.define_neighborhoods(neighborhood_radius=float(radius))
sf.compute_pvalues(background='network', num_permutations=1000, processes=64)
sf.print_output_files(output_dir=f'{wd}/SAFE/{i}_')
df_tmp = pd.read_csv(f"{wd}/SAFE/{i}_node_properties_annotation.txt", sep="\t")
significant = dict()
for index, row in df_tmp.iterrows():
key = row['key']
significant[key] = [(col, round(row[col],3)) for col in df_tmp.columns[3:] if row[col] > -np.log10(enrichment_thr) and not col.startswith("Unnamed")]
return i, significant
def prepare_df(d):
df = pd.DataFrame()
for i in d:
tmp_df = pd.DataFrame(d[i]).transpose()
tmp_df = tmp_df.stack().reset_index()
tmp_df.columns = ['id', 'group', 'count']
tmp_df['category'] = i
df = pd.concat([df, tmp_df])
return df
def make_barcharts(txid, coll):
df_taxid = prepare_df(txid)
df_collateral = prepare_df(coll)
chart_taxid = alt.Chart(df_taxid).mark_bar().encode(
# tell Altair which field to group columns on
x=alt.X('group:N', title=None, sort=None), # Add sort argument
# tell Altair which field to use as Y values and how to calculate
y=alt.Y('sum(count):Q',
axis=alt.Axis(
grid=False,
title=None)),
# tell Altair which field to use to use as the set of columns to be represented in each group
column=alt.Column('id:N', sort=alt.SortField(field='id', order='ascending'), title=None), # Add sort argument
# tell Altair which field to use for color segmentation
color=alt.Color('category:N', sort=None,
scale=alt.Scale(
# make it look pretty with an enjoyable color pallet
range=['#001219','#005F73','#94D2BD','#EE9B00','#BB3E03','#9B2226'],
)),
order=alt.Order(
# Sort the segments of the bars by this field
'category',
sort='ascending')
)\
.configure_view(
# remove grid lines around column clusters
strokeOpacity=0
).properties(
width=150)
chart_taxid = chart_taxid.configure_legend(labelFontSize=8)
chart_taxid.save(f"{args.outfig}_taxid_iter{args.iteration}.png", ppi=200)
chart_collateral = alt.Chart(df_collateral).mark_bar().encode(
# tell Altair which field to group columns on
x=alt.X('group:N', title=None, sort=None), # Add sort argument
# tell Altair which field to use as Y values and how to calculate
y=alt.Y('sum(count):Q',
axis=alt.Axis(
grid=False,
title=None)),
# tell Altair which field to use to use as the set of columns to be represented in each group
column=alt.Column('id:N', sort=alt.SortField(field='id', order='ascending'), title=None), # Add sort argument
# tell Altair which field to use for color segmentation
color=alt.Color('category:N', sort=None,
scale=alt.Scale(
# make it look pretty with an enjoyable color pallet
range=['#001219','#005F73','#94D2BD','#EE9B00','#BB3E03','#9B2226'],
)),
order=alt.Order(
# Sort the segments of the bars by this field
'category',
sort='ascending')
)\
.configure_view(
# remove grid lines around column clusters
strokeOpacity=0
).properties(
width=150)
chart_collateral = chart_collateral.configure_legend(labelFontSize=8)
chart_collateral.save(f"{args.outfig}_collateral_iter{args.iteration}.png", ppi=200)
def analyze_safe_results(df_anno, df_safe, ids, allp):
taxid, collateral = {}, {}
for key in ["total", "previously annotated", "not annotated", "nan2nan", "new annotation by WASP", "nan2nan in dark clusters"]:
taxid[key], collateral[key] = {}, {}
for identifier in ids:
taxid[key][f'{ids.index(identifier) + 1}.{identifier}'] = {'total': 0, 'annotation': 0, 'nan2nan / new': 0, 'nan2nan (dark)': 0}
collateral[key][f'{ids.index(identifier) + 1}.{identifier}'] = {'total': 0, 'annotation': 0, 'nan2nan / new': 0, 'nan2nan (dark)': 0}
list_nan2nan = list()
cols = ["UniProt ID"] + ids + ["Organism"]
taxid_outdf, collateral_outdf = pd.DataFrame(columns=cols), pd.DataFrame(columns=cols)
nclusters = df_anno["#Cluster"].unique()
for c in nclusters:
subdf_anno = df_anno[df_anno["#Cluster"] == c]
subdf_safe = df_safe[df_safe["UniProt ID"].isin(subdf_anno["UniProt ID"])]
dark_anno = subdf_anno[ids].apply(lambda col: col.isna().all()).to_dict()
dark_safe = subdf_safe[ids].apply(lambda col: col.isna().all()).to_dict()
tmp_taxid, tmp_collateral = pd.DataFrame(columns=cols), pd.DataFrame(columns=cols)
for i in ids:
taxid['total'][f'{ids.index(i) + 1}.{i}']['total'] += len(subdf_anno[subdf_anno['UniProt ID'].isin(allp['UniProt ID'])])
collateral['total'][f'{ids.index(i) + 1}.{i}']['total'] += len(subdf_anno[~subdf_anno['UniProt ID'].isin(allp['UniProt ID'])])
if dark_anno[i] and dark_safe[i]:
proteins = subdf_anno[["UniProt ID", "Organism"]]
proteins_taxid = proteins[proteins['UniProt ID'].isin(allp['UniProt ID'])]
list_nan2nan.extend(proteins_taxid["UniProt ID"].tolist())
taxid['nan2nan'][f'{ids.index(i) + 1}.{i}']['nan2nan / new'] += len(proteins_taxid)
taxid['not annotated'][f'{ids.index(i) + 1}.{i}']['annotation'] += len(proteins_taxid)
taxid['nan2nan in dark clusters'][f'{ids.index(i) + 1}.{i}']['nan2nan (dark)'] += len(proteins_taxid)
collateral['nan2nan'][f'{ids.index(i) + 1}.{i}']['nan2nan / new'] += len(proteins[~proteins['UniProt ID'].isin(allp['UniProt ID'])])
collateral['not annotated'][f'{ids.index(i) + 1}.{i}']['annotation'] += len(proteins[~proteins['UniProt ID'].isin(allp['UniProt ID'])])
collateral['nan2nan in dark clusters'][f'{ids.index(i) + 1}.{i}']['nan2nan (dark)'] += len(proteins[~proteins['UniProt ID'].isin(allp['UniProt ID'])])
else:
for index, row in subdf_safe.iterrows():
if row[i] == row[i]:
if pd.isna(subdf_anno[subdf_anno["UniProt ID"] == row["UniProt ID"]][i].values[0]):
if row['UniProt ID'] in allp['UniProt ID'].tolist():
taxid['new annotation by WASP'][f'{ids.index(i) + 1}.{i}']['nan2nan / new'] += 1
else:
collateral['new annotation by WASP'][f'{ids.index(i) + 1}.{i}']['nan2nan / new'] += 1
else:
if row['UniProt ID'] in allp['UniProt ID'].tolist():
taxid['previously annotated'][f'{ids.index(i) + 1}.{i}']['annotation'] += 1
else:
collateral['previously annotated'][f'{ids.index(i) + 1}.{i}']['annotation'] += 1
if row['UniProt ID'] in allp['UniProt ID'].tolist():
tmp_taxid = pd.concat([tmp_taxid, subdf_safe[subdf_safe["UniProt ID"] == row["UniProt ID"]]])
else:
tmp_collateral = pd.concat([tmp_collateral, subdf_safe[subdf_safe["UniProt ID"] == row["UniProt ID"]]])
taxid_outdf = pd.concat([taxid_outdf, tmp_taxid])
collateral_outdf = pd.concat([collateral_outdf, tmp_collateral])
taxid_outdf.to_csv(f"{args.wd}/taxid_{args.output}_iter{args.iteration}.txt", sep="\t", index=False)
collateral_outdf.to_csv(f"{args.wd}/collateral_{args.output}_iter{args.iteration}.txt", sep="\t", index=False)
if len(list_nan2nan) > 0:
nan_list_df = pd.DataFrame(list_nan2nan, columns=["nan2nan_list"])
nan_list_df.to_csv(f"{args.wd}/{args.nan}_iter{args.iteration}.txt", sep="\t", index=False)
return taxid, collateral
if __name__ == "__main__":
if not os.path.exists(f"{args.wd}/SAFE"):
os.makedirs(f"{args.wd}/SAFE")
df_anno = pd.read_csv(args.input, sep="\t", header=0)
df_ids = pd.read_csv(args.identifiers, sep="\t", header=0)
all_proteins = df_ids[['UniProt ID']]
safe_file = safe.Safe(
input_network=args.edgelist,
input_node_attribute=None,
neighborhood_radius=float(args.radius),
output_dir=args.wd,
processes=64,
output_prefix=None
)
ids = df_anno.columns[2:]
pool = multiprocessing.Pool(processes=64)
pool.map(process_identifier, [(i, df_anno) for i in ids])
manager = multiprocessing.Manager()
results = manager.dict()
results_list = pool.starmap(perform_safe_analysis, [(i, args.wd, args.radius, safe_file) for i in ids])
for res in results_list:
key, value = res
results[key] = value
taxid, collateral = analyze_safe_results(df_anno, df_anno, ids, all_proteins)
make_barcharts(taxid, collateral)