-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_randirected_db_as_main.py
224 lines (208 loc) · 11.7 KB
/
generate_randirected_db_as_main.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
import random
from create_graph_from_db_implemented import generate_graph, plot_generated_graph, pd
import os
# Main purpose is manipulating main prototype_db to direct it (Affect similarity)
# To do that randomly impact the nodes
"There should be 3 feature to change system"#
"1)Randomly delete the node(s)"
"2)Generate new interactions that is not "
"Main part is differantiate step by step but choose the type differentiation," \
" total edge change is same but one of them belongs one node, another one independent"
def find_and_delete(pair1,pair2,df):
for index,row in df.iterrows():
if (row['Gene Name Interactor A'] == pair1 or row['Gene Name Interactor B'] == pair1) and (row['Gene Name Interactor A'] == pair2 or row['Gene Name Interactor B'] == pair2):
return index
def diff(li1, li2):
return list(set(li1) - set(li2)) + list(set(li2) - set(li1))
def change_refer(ref,ref_nod,unit_step,node_weight,edge_weight,trial_no):
"""
#Edge weight + Node weight should be equal to 1
database = pd.read_csv(ref)
ref_node=pd.read_csv(ref_nod)
unit_step_for_rand_edge, unit_step_for_node = round(edge_weight*unit_step), round(node_weight*unit_step)
init_db, init_edge=database['Gene_Symb'].values.tolist(), [ref_node['Gene Name Interactor A'].values.tolist(), ref_node['Gene Name Interactor B'].values.tolist()]
print('Initial node_count' + str(database.shape))
print('-------------------------1--------------------------------')
for iterator in range(unit_step_for_rand_edge):
try:
ref_node=ref_node.drop(random.randint(0,ref_node.shape[0]-1), axis='index')
except:
ref_node = ref_node.drop(random.randint(0, ref_node.shape[0]- 1), axis='index') #Dogru sayiyi buluna kadar while da dene
print(ref_node.shape)
print('-------------------------2--------------------------------')
while unit_step_for_node != 0:
random_numb=random.randint(0,len(database['Gene_Symb'].values.tolist())-unit_step)
chosen_node=database['Gene_Symb'].values.tolist()[random_numb]
#Find pairs
pair_ls_for_chosen_node=[]
#print(chosen_node)
for index, row in ref_node.iterrows():
if row['Gene Name Interactor A'] == chosen_node:
pair_ls_for_chosen_node.append(row['Gene Name Interactor B'])
if row['Gene Name Interactor B'] == chosen_node:
pair_ls_for_chosen_node.append(row['Gene Name Interactor A'])
#print(pair_ls_for_chosen_node)
if len(pair_ls_for_chosen_node) < unit_step_for_node:
'''
try:
ref_node=ref_node[ref_node['Gene Name Interactor A'].str.contains(chosen_node) == False]
except:
ref_node = ref_node[ref_node['Gene Name Interactor B'].str.contains(chosen_node) == False]
'''
for item in pair_ls_for_chosen_node:
ref_node=ref_node.drop(find_and_delete(item,chosen_node,ref_node), axis='index')
print(ref_node.shape)
print('-------------------------3--------------------------------')
unit_step_for_node = unit_step_for_node - len(pair_ls_for_chosen_node)
else:
pair_ls_for_chosen_node = pair_ls_for_chosen_node[:unit_step_for_node]
for pair in pair_ls_for_chosen_node:
indice=find_and_delete(pair,chosen_node,ref_node)
ref_node=ref_node.drop(indice, axis='index')
print(ref_node.shape)
print('-------------------------4--------------------------------')
break
All_Term_Nodes, All_In_Nodes = ref_node['Gene Name Interactor B'].values.tolist(), ref_node['Gene Name Interactor A'].values.tolist()
for index,row in database.iterrows():
if (row['Gene_Symb'] in All_Term_Nodes ) or (row['Gene_Symb'] in All_In_Nodes):
pass
else:
#database = database[database['Gene_Symb'].str.contains(row['Gene_Symb']) == False]
drop_ls=database.index[database['Gene_Symb'] == row['Gene_Symb']].tolist()
for drop_index in drop_ls:
database = database.drop(drop_index,axis='index')
ref_node.to_csv(os.getcwd()+str(trial_no)+'_confidence_db', index=None)
database.to_csv(os.getcwd()+str(trial_no)+'_prototype_db', index=None)
end_db, end_edge = database['Gene_Symb'].values.tolist(), [ref_node['Gene Name Interactor A'].values.tolist(), ref_node['Gene Name Interactor B'].values.tolist()]
print('Finished node_count' + str(database.shape))
print('Name(s) of removed nodes' + str(diff(init_db,end_db)))
dif_of_edge_in=diff(init_db[0],end_edge[0])
dif_of_edge_term=diff(init_db[1],end_edge[1])
for i in range(len(dif_of_edge_in)):
print(str(dif_of_edge_in[i])+'--'+str(dif_of_edge_term[i]))
"""
# Edge weight + Node weight should be equal to 1
database = pd.read_csv(ref)
ref_node = pd.read_csv(ref_nod)
unit_step_for_rand_edge, unit_step_for_node = round(edge_weight * unit_step), round(node_weight * unit_step)
init_db, init_edge = database['Gene_Symb'].values.tolist(), [ref_node['Gene Name Interactor A'].values.tolist(),
ref_node['Gene Name Interactor B'].values.tolist()]
print('Initial node_count' + str(database.shape))
print('-------------------------1--------------------------------')
for iterator in range(unit_step_for_rand_edge):
try:
random_number = random.randint(0, ref_node.shape[0] - 1)
print(ref_node.iloc[random_number])
ref_node = ref_node.drop(random_number, axis='index')
except:
random_number = random.randint(0, ref_node.shape[0] - 1)
print(ref_node.iloc[random_number])
ref_node = ref_node.drop(random_number, axis='index') # Dogru sayiyi buluna kadar while da dene
print(ref_node.shape)
print('-------------------------2--------------------------------')
while unit_step_for_node != 0:
random_numb = random.randint(0, len(database['Gene_Symb'].values.tolist()) - unit_step)
chosen_node = database['Gene_Symb'].values.tolist()[random_numb]
# Find pairs
pair_ls_for_chosen_node = []
# print(chosen_node)
for index, row in ref_node.iterrows():
if row['Gene Name Interactor A'] == chosen_node:
pair_ls_for_chosen_node.append(row['Gene Name Interactor B'])
if row['Gene Name Interactor B'] == chosen_node:
pair_ls_for_chosen_node.append(row['Gene Name Interactor A'])
# print(pair_ls_for_chosen_node)
if len(pair_ls_for_chosen_node) < unit_step_for_node:
'''
try:
ref_node=ref_node[ref_node['Gene Name Interactor A'].str.contains(chosen_node) == False]
except:
ref_node = ref_node[ref_node['Gene Name Interactor B'].str.contains(chosen_node) == False]
'''
for item in pair_ls_for_chosen_node:
# print(item), print(chosen_node), print(ref_node)
indice = find_and_delete(item, chosen_node, ref_node)
print(ref_node.iloc[indice])
ref_node = ref_node.drop(indice, axis='index')
print(ref_node.shape)
print('-------------------------3--------------------------------')
unit_step_for_node = unit_step_for_node - len(pair_ls_for_chosen_node)
else:
pair_ls_for_chosen_node = pair_ls_for_chosen_node[:unit_step_for_node]
for pair in pair_ls_for_chosen_node:
indice = find_and_delete(pair, chosen_node, ref_node)
print(ref_node.iloc[indice])
ref_node = ref_node.drop(indice, axis='index')
print(ref_node.shape)
print('-------------------------4--------------------------------')
break
All_Term_Nodes, All_In_Nodes = ref_node['Gene Name Interactor B'].values.tolist(), ref_node[
'Gene Name Interactor A'].values.tolist()
for index, row in database.iterrows():
if (row['Gene_Symb'] in All_Term_Nodes) or (row['Gene_Symb'] in All_In_Nodes):
pass
else:
# database = database[database['Gene_Symb'].str.contains(row['Gene_Symb']) == False]
drop_ls = database.index[database['Gene_Symb'] == row['Gene_Symb']].tolist()
for drop_index in drop_ls:
database = database.drop(drop_index, axis='index')
ref_node.to_csv(os.getcwd() + str(trial_no) + '_confidence_db', index=None)
database.to_csv(os.getcwd() + str(trial_no) + '_prototype_db', index=None)
end_db, end_edge = database['Gene_Symb'].values.tolist(), [ref_node['Gene Name Interactor A'].values.tolist(),
ref_node['Gene Name Interactor B'].values.tolist()]
print('Finished node_count' + str(database.shape))
print('Name(s) of removed nodes' + str(diff(init_db, end_db)))
#dif_of_edge_in = diff(init_db[0], end_edge[0])
#dif_of_edge_term = diff(init_db[1], end_edge[1])
#for i in range(len(dif_of_edge_in)):
# print(str(dif_of_edge_in[i]) + '--' + str(dif_of_edge_term[i]))
return str(trial_no)+'_prototype_db', str(trial_no)+'_confidence_db'
def generate_db_name_csv_for_edge_files(Graph,trial_ID_or_Name="?",headlines="Gene_Symb,Transcription_Level,Hotpoint_Mutation,CRISPR_KO_Effect,Differentiation_Rate", target_path=os.getcwd()+"/"):
db_headline_ls = headlines.rsplit(","); headline_count= len(db_headline_ls)
db_file = open(str(target_path)+str(trial_ID_or_Name), "w")
db_file.write(headlines)
db_file.write("\n")
for node in Graph.nodes:
line=str(node)
for number_of_headlines in range(headline_count-1):
line+=",None"
line+="\n"
db_file.write(line)
db_file.close()
if __name__ == '__main__':
#Trial for a database that have already a node_set database as csv
"""
n_weight,e_weight,s_size=0.3,0.7,15 #Define the random rate
print('node_diff_rate: '+str(n_weight) +' edge_diff_rate:' +str(e_weight) + ' total_step_size'+ str(s_size))
#trial_code=input('trial_number: ')
trial_code=313
print(os.getcwd())
database,wire_database=str(os.getcwd())+'/prototype_db.csv',str(os.getcwd())+'/confidence_db.csv'
directed_db, directed_wired = change_refer(database,wire_database,s_size,n_weight,e_weight,trial_code)
#print(directed_db, directed_wired)
G = generate_graph(directed_db, directed_wired)
#print(G)
#plot_generated_graph(G,trial_code)
"""
#Trial for generate an edge system from only nodes
"""
wire_database=str(os.getcwd()+'/confidence_db.csv')
G,trial_ID = generate_graph(wire_database, trial_ID="001")
generate_db_name_csv_for_edge_files(G,trial_ID_or_Name="002",)
"""
#Trial 3
#This is not working here because pwd is has to start example_dbs
"""
n_weight, e_weight, s_size = 0.3, 0.7, 15 # Define the random rate
print('node_diff_rate: ' + str(n_weight) + ' edge_diff_rate:' + str(e_weight) + ' total_step_size' + str(s_size))
trial_code = "HIPPIE-node-075.csv"
wire_database = str(os.getcwd() + '/actual_databases/HIPPIE-confidence-075.csv')
G, trial_ID = generate_graph(wire_database, trial_ID=trial_code)
generate_db_name_csv_for_edge_files(G, trial_ID_or_Name=trial_ID)
database, wire_database = str(os.getcwd()) + 'actual_databases/HIPPIE-node-075.csv', str(os.getcwd()) + 'actual_databases/HIPPIE-confidence-075.csv'
directed_db, directed_wired = change_refer(database, wire_database, s_size, n_weight, e_weight, trial_code)
print(directed_db, directed_wired)
G = generate_graph(directed_db, directed_wired)
# print(G)
# plot_generated_graph(G,trial_code)
"""