-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaired_sample.py
296 lines (244 loc) · 10.2 KB
/
paired_sample.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from tools.helper import (
read_pro_go_data,
read_specific_columns,
create_ppi_network,
export_graph_to_pickle,
print_progress,
get_neighbors,
read_go_depth_data,
)
from tools.workflow import remove_samples
import pandas as pd
import random
import time
from random import sample
from pathlib import Path
input_directory_path = Path("./output/dataset")
namespace = ["molecular_function", "biological_process", "cellular_component"]
# change the go_term_type variable to include which go term namespace you want
go_term_type = [namespace[0], namespace[1], namespace[2]]
short_name = ""
if namespace[0] in go_term_type:
short_name = short_name + "_mol"
if namespace[1] in go_term_type:
short_name = short_name + "_bio"
if namespace[2] in go_term_type:
short_name = short_name + "_cel"
go_inferred_columns = [0, 2, 3]
go_protein_pairs = read_pro_go_data(
"./network/fly_proGo.csv", go_inferred_columns, go_term_type, ","
)
interactome_columns = [0, 1]
interactome = read_specific_columns("./network/fly_propro.csv", interactome_columns, ",")
protein_list = []
depth_columns = [0,1,2]
go_depth_dict = read_go_depth_data("./network/go_depth.csv", depth_columns, go_term_type, ',')
# if there is no graph.pickle file in the output/dataset directory, uncomment the following lines
graph_file_path = Path(input_directory_path, "graph.pickle")
G, protein_list = create_ppi_network(interactome, go_protein_pairs, go_depth_dict)
export_graph_to_pickle(G, graph_file_path)
sample_size = 100
pair_type = "depth"
reps = 1
#Options for pair_type: "protein_go", "protein_protein", "both", "depth"
def paired_sample_data(go_protein_pairs, sample_size, protein_list, proteins, G, input_directory_path, num, name):
"""
Given a sample size, generate positive nad negative datasets.
Parameters:
go_protein_pairs {list} : a list containing the edge between a protein and a go-term e.g. [[protein1, go_term1], [protein2, go_term2], ...]
sample_size {int} : the size of a positive/negative dataset to be sampled
protein_list {list} : a list of all proteins in the graph
proteins {dict} : a list of all proteins in the graph and the number of annotated go terms
G {nx.Graph} : graph that represents the interactome and go term connections
input_directory_path {Path} : Path to directory of the datasets
num {int} : Number of positive/negative dataset
name {str} : shorthand for all namespaces used to generate datasets, adds shorthand to .csv name
Returns:
positive_dataset, negative_dataset
"""
positive_dataset = {"protein": [], "go": []}
negative_dataset = {"protein": [], "go": []}
# sample the data
for edge in sample(list(go_protein_pairs), sample_size):
positive_dataset["protein"].append(edge[0])
positive_dataset["go"].append(edge[1])
i = 1
for protein, go in zip(positive_dataset["protein"], positive_dataset["go"]):
freq = proteins[protein]
# print(go_num)
sample_edge = random.choice(protein_list)
sample_edge = sample_edge['id']
r = 10
t = .1
start = time.time()
# removes if a protein has a corresponding edge to the GO term in the network
while G.has_edge(sample_edge, go) or not ((freq-r) <= proteins[sample_edge] <= (freq+r)):
sample_edge = random.choice(protein_list)
sample_edge = sample_edge['id']
end = time.time()
if (end-start) > t: #Slightly arbitrary, but the program cycles through all options relatively fast
r += 10
t += .01
negative_dataset["protein"].append(sample_edge)
negative_dataset["go"].append(go)
print_progress(i, sample_size)
i += 1
positive_df = pd.DataFrame(positive_dataset)
negative_df = pd.DataFrame(negative_dataset)
positive_df.to_csv(
Path(input_directory_path, "rep_" + str(num) + "_positive_protein_go_term_pairs" + name + ".csv"),
index=False,
sep="\t",
)
negative_df.to_csv(
Path(input_directory_path, "rep_" + str(num) + "_negative_protein_go_term_pairs" + name + ".csv"),
index=False,
sep="\t",
)
return positive_dataset, negative_dataset
def paired_sample_data_multi_input(go_protein_pairs, sample_size, protein_list, proteins, G, input_directory_path, num, name):
"""
Given a sample size, generate positive nad negative datasets.
Parameters:
go_protein_pairs {list} : a list containing the edge between a protein and a go-term e.g. [[protein1, go_term1], [protein2, go_term2], ...]
sample_size {int} : the size of a positive/negative dataset to be sampled
protein_list {list} : a list of all proteins in the graph
proteins {dict} : a list of all proteins in the graph and the number protein neighbors and annotated go terms
G {nx.Graph} : graph that represents the interactome and go term connections
input_directory_path {Path} : Path to directory of the datasets
num {int} : Number of positive/negative dataset
name {str} : shorthand for all namespaces used to generate datasets, adds shorthand to .csv name
Returns:
positive_dataset, negative_dataset
"""
positive_dataset = {"protein": [], "go": []}
negative_dataset = {"protein": [], "go": []}
# sample the data
for edge in sample(list(go_protein_pairs), sample_size):
positive_dataset["protein"].append(edge[0])
positive_dataset["go"].append(edge[1])
i = 1
for protein, go in zip(positive_dataset["protein"], positive_dataset["go"]):
freq_pro = proteins[protein][0]
freq_go = proteins[protein][1]
sample_edge = random.choice(protein_list)
sample_edge = sample_edge['id']
r = 10
t = .01
start = time.time()
# removes if a protein has a corresponding edge to the GO term in the network
while G.has_edge(sample_edge, go) or not ((freq_pro-r) <= proteins[sample_edge][0] <= (freq_pro+r)) or not ((freq_go-r) <= proteins[sample_edge][1] <= (freq_go+r)):
sample_edge = random.choice(protein_list)
sample_edge = sample_edge['id']
end = time.time()
if (end-start) > t: #Slightly arbitrary, but the program cycles through all options relatively fast
r += 10
t += .01
negative_dataset["protein"].append(sample_edge)
negative_dataset["go"].append(go)
print_progress(i, sample_size)
i += 1
positive_df = pd.DataFrame(positive_dataset)
negative_df = pd.DataFrame(negative_dataset)
positive_df.to_csv(
Path(input_directory_path, "rep_" + str(num) + "_positive_protein_go_term_pairs" + name + ".csv"),
index=False,
sep="\t",
)
negative_df.to_csv(
Path(input_directory_path, "rep_" + str(num) + "_negative_protein_go_term_pairs" + name + ".csv"),
index=False,
sep="\t",
)
return positive_dataset, negative_dataset
def go_neighbor_sample_list(proGo, proLst):
"""
Creates a list of how many go term neighbors each protein has
Parameters:
proGo {list}: a list of protein go term pairs
proLst {list} : a list of protein nodes
Returns:
a dictionary containing the number of go term neighbors for all proteins
"""
proteins = {} # Number of proteins annotated to each go term
for term in proLst:
x = term['id']
if x not in proteins.keys():
proteins[x] = 0
for term in proGo:
i = term[0]
proteins[i] += 1
df = pd.DataFrame.from_dict(proteins, orient = "index")
df.to_csv(
Path("./output/data", "number_of_annotated_go_terms.csv"),
index=True,
sep="\t",
)
return proteins
def protein_neighbor_sample_list(proproLst):
"""
Creates a list of how many go term neighbors each protein has
Parameters:
proproLst {list} : a list protein neighbors
Returns:
a dictionary containing the number of protein neighbors for all proteins
"""
proteins = {} # Number of proteins neighbors for each protein
for term in proproLst:
x = term[0]
y = term[1]
if x not in proteins.keys():
proteins[x] = 1
else:
proteins[x] += 1
if y not in proteins.keys():
proteins[y] = 1
else:
proteins[y] += 1
df = pd.DataFrame.from_dict(proteins, orient = "index")
df.to_csv(
Path("./output/data", "number_of_annotated_proteins.csv"),
index=True,
sep="\t",
)
return proteins
def lowest_go_depth(protein_list):
"""
Finds the associated lowest depth go term for each protein in the list
Parameters:
proLst {list} : a list of protein nodes
Returns:
a dictionary containing the lowest depth go term for all proteins
"""
depth_dict = {}
for pro in protein_list:
pro = pro['id']
go_neighbors = get_neighbors(G, pro, "protein_go_term")
highest = 0
for go in go_neighbors:
go = int(go[1]['weight'])
if go > highest:
highest = go
depth_dict[pro] = highest
return(depth_dict)
remove_samples("./output/dataset")
if pair_type == "protein_protein":
proteins = protein_neighbor_sample_list(interactome)
elif pair_type == "protein_go":
proteins = go_neighbor_sample_list(go_protein_pairs, protein_list) #Make this a perminant list?
elif pair_type == "both":
proteins = protein_neighbor_sample_list(interactome)
protein_go = go_neighbor_sample_list(go_protein_pairs, protein_list)
for i in proteins:
proteins[i] = [proteins[i], 0] #Protein neighbors in index 0, go neighbors in index 1
for i in protein_go:
proteins[i][1] = protein_go[i]
for num in range(reps):
paired_sample_data_multi_input(go_protein_pairs, sample_size, protein_list, proteins, G, input_directory_path, num, short_name)
print(" Sample " + str(num) + " created")
elif pair_type == "depth":
proteins = lowest_go_depth(protein_list)
if pair_type != "both":
for num in range(reps):
paired_sample_data(go_protein_pairs, sample_size, protein_list, proteins, G, input_directory_path, num, short_name)
print(" Sample " + str(num) + " created")