-
Notifications
You must be signed in to change notification settings - Fork 0
/
searching_graphlets_in_defined_Graph.py
247 lines (199 loc) · 12.3 KB
/
searching_graphlets_in_defined_Graph.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
import os
import itertools
import networkx as nx
from create_graph_from_db_implemented import generate_graph, plot_generated_graph
from building_graphlet_library import graphlet_library
def not_Egoist_search_Graphlets(Graph, graphlet_library, structured_known_ls=None, log=True):
global contempary_list
if structured_known_ls is not None:
known_ls = []
for cluster in structured_known_ls:
for element in cluster:
known_ls.append(element)
else:
known_ls = []
"""
for iter in itertools.combinations(Graph.nodes, len((graphlet_library[0].pattern))):
defined_subgraph = Graph.subgraph(iter)
#print(defined_subgraph.nodes)
"""
span_list=[]
for target_graphlet in graphlet_library:
contempary_list=[]
if log:
print('\n-------------------'+str(target_graphlet.name)+'-------------------\n'); contempary_list=[target_graphlet.name]
for nodes_of_subgraph in itertools.combinations(Graph.nodes, len((target_graphlet.pattern))):
defined_subgraph = Graph.subgraph(nodes_of_subgraph)
if nx.is_connected(defined_subgraph) and nx.is_isomorphic(defined_subgraph, target_graphlet.pattern):
contempary_list.append(defined_subgraph.edges)
if log:
print(defined_subgraph.edges)
span_list.append(contempary_list)
return span_list
def generate_image_of_all_found_motifs(found_motif_list,ID, log=True):
counter =0; starting_path = os.getcwd()+'/sub_graph_dir/'
ID_path = os.path.join(starting_path, str(ID) )
try:
os.mkdir(ID_path)
except:
pass
for found_motif_category in found_motif_list:
if log:
print(found_motif_category)
path = os.path.join( ID_path, str(found_motif_category[0]))
try:
os.mkdir(path)
except:
pass
for found_actual_motif in found_motif_category[1:]:
plot_generated_graph(nx.Graph(found_actual_motif),str(ID)+'_'+str(counter),path=path,plot_show=False, dpi_chosen=80); counter+=1
if log:
print(found_actual_motif)
if log:
print("The actual subgraph's images are extracted to " + str(starting_path) +" file to it's relevant categoris.")
def search_Graphlets_in_egoistic_database(ego_dictionary, Graph):
span_list = []
for target_graphlet in graphlet_library:
print('\n-------------------' + str(target_graphlet.name) + '-------------------\n');
contempary_list = [target_graphlet.name]
for node_key in ego_dictionary.keys():
for primary_dist_node in ego_dictionary[node_key].keys():
for secondary_dist_node in ego_dictionary[node_key][primary_dist_node]:
if (node_key not in [primary_dist_node, secondary_dist_node] and primary_dist_node not in [node_key, secondary_dist_node] and secondary_dist_node not in [
node_key, primary_dist_node]) and (
None not in [node_key, primary_dist_node, secondary_dist_node]):
defined_subgraph = Graph.subgraph(
[node_key, primary_dist_node, secondary_dist_node])
if nx.is_connected(defined_subgraph) and nx.is_isomorphic(defined_subgraph,
target_graphlet.pattern):
contempary_list.append(defined_subgraph.edges)
print(defined_subgraph.edges)
span_list.append(contempary_list)
return span_list
if __name__ == '__main__':
##Trial-1
#Last Trial
"""
## Generating Graph from the DB
## Import Graph to the Indexing Function
## This block do not search graphlets it only reindex the database
from reindex_as_egoistic_structure_databases import reindex_dataset_as_ego_structure
from create_graph_from_db_implemented import generate_graph, plot_generated_graph
#Although plot_generated function is implemented, it is not going to run
path = os.getcwd() + "/actual_databases/"
Graph_HIPPIE_confidence_075, trial_ID=generate_graph(path+"/HIPPIE-confidence-075.csv", trial_ID="Actual_")
ego_dictionary = reindex_dataset_as_ego_structure(Graph_HIPPIE_confidence_075, save_dict=True, name="Actual_HIPPIE-confidence-075", path="/actual_databases/")
"""
##Trial-2
##Import Genearated Graph_Not_egoistic_Search
"""
import pandas as pd
from manuel_graph_generator import assign_wire, Wire
wire_database = pd.read_csv(str(os.getcwd()) + '/actual_databases/HIPPIE-confidence-075.csv')
wire_database = wire_database.drop(range(5000,96343),axis='index')
print(wire_database)
Graph_HIPPIE_confidence_075 = nx.Graph()
for index, row in wire_database.iterrows():
assign_wire(Graph_HIPPIE_confidence_075,
Wire(row['Gene Name Interactor A'], row['Gene Name Interactor B'], row['Confidence Value'], True))
found_motifs_from_actual = not_Egoist_search_Graphlets(Graph_HIPPIE_confidence_075, graphlet_library)
print(found_motifs_from_actual)
"""
#Trial-3
##Defined_egoistic_structure
# Database is diminished also manuel graph generation processes are takes placed
#Last One 19.07
import pickle
from reindex_as_egoistic_structure_databases import reindex_dataset_as_ego_structure, assign_wire, Wire, pd
wire_database = pd.read_csv(str(os.getcwd()) + '/actual_databases/HIPPIE-confidence-075.csv')
wire_database_diminished = wire_database.drop(range(10000, 96348), axis='index')
wire_database_diminished.to_csv(os.getcwd()+"/actual_databases/wire_database_diminished_Actual", index=True)
print(wire_database_diminished)
#This is the diminished actual databased no any differentiation (randirect is not executed)
Graph_HIPPIE_confidence_075_diminished = nx.Graph()
for index, row in wire_database_diminished.iterrows():
assign_wire(Graph_HIPPIE_confidence_075_diminished,
Wire(row['Gene Name Interactor A'], row['Gene Name Interactor B'], row['Confidence Value'], True))
ego_dictionary_diminished = reindex_dataset_as_ego_structure(Graph_HIPPIE_confidence_075_diminished, save_dict=False,
name="HIPPIE-confidence-075_diminished", path="/actual_databases/")
#Dict database save externally because save_dict=True is not working I don't know why
file2 = open(os.getcwd() + "/actual_databases/Graph_HIPPIE_confidence_075_diminished_dictionary_db.pkl", "wb")
pickle.dump(ego_dictionary_diminished, file2)
file2.close()
#found_graphlets_diminished = search_Graphlets_in_egoistic_database(ego_dictionary_diminished, Graph_HIPPIE_confidence_075_diminished) #AMAZING
## The graph obtained from diminished dataset is formed
## Now we should generate a randirect data
"""
import generate_randirected_db_as_main
defined_db_name, defined_path = os.getcwd()+"/actual_databases/wire_database_diminished_Actual", os.getcwd()
Graph_will_be_pruned, trial_ID = generate_graph(defined_db_name)
print("-----")
generate_randirected_db_as_main.generate_db_name_csv_for_edge_files(Graph_will_be_pruned, trial_ID_or_Name="node_database_diminished_Actual", target_path=defined_path+"/actual_databases/")
actual_target_path = os.getcwd()+"/actual_databases/"
generate_randirected_db_as_main.change_refer(actual_target_path+"node_database_diminished_Actual", actual_target_path+"wire_database_diminished_Actual",20,0.6,0.4,"/actual_databases/001-HIPPIE")
wire_database_diminished_pruned = pd.read_csv(actual_target_path+"001-HIPPIE_confidence_db")
print(wire_database_diminished_pruned)
Graph_HIPPIE_confidence_075_diminished_pruned = nx.Graph()
for index, row in wire_database_diminished_pruned.iterrows():
assign_wire(Graph_HIPPIE_confidence_075_diminished_pruned,
Wire(row['Gene Name Interactor A'], row['Gene Name Interactor B'], row['Confidence Value'], True))
ego_dictionary_diminished_pruned = reindex_dataset_as_ego_structure(Graph_HIPPIE_confidence_075_diminished_pruned, save_dict=False,
name="Graph_HIPPIE_confidence_075_diminished_pruned", path="/actual_databases/")
#Dict database save externally because save_dict=True is not working I don't know why
file1 = open(os.getcwd()+"/actual_databases/Graph_HIPPIE_confidence_075_diminished_pruned_dictionary_db.pkl", "wb")
pickle.dump(ego_dictionary_diminished_pruned, file1)
file1.close()
found_graphlets_diminished_pruned = search_Graphlets_in_egoistic_database(ego_dictionary_diminished_pruned,
Graph_HIPPIE_confidence_075_diminished_pruned) # AMAZING
"""
# Trial-3
##Defined_egoistic_structure
## Here there is pickle load
"""
from reindex_as_egoistic_structure_databases import load_dict_with_pickle, pd, assign_wire, Wire, reindex_dataset_as_ego_structure
wire_database = pd.read_csv(str(os.getcwd()) + '/actual_databases/HIPPIE-confidence-075.csv')
wire_database = wire_database.drop(range(100, 95000), axis='index')
wire_database.to_csv("wire_database_diminished_Actual", index=True)
Graph_HIPPIE_confidence_075 = nx.Graph()
for index, row in wire_database.iterrows():
assign_wire(Graph_HIPPIE_confidence_075,
Wire(row['Gene Name Interactor A'], row['Gene Name Interactor B'], row['Confidence Value'], True))
#ego_dictionary = reindex_dataset_as_ego_structure(Graph_HIPPIE_confidence_075, save_dict=False,
# name="HIPPIE-confidence-075", path="/actual_databases/")
path = os.path.join(os.getcwd(),"actual_databases/HIPPIE-confidence_dictionary_db.pkl")
egoistic_dict_db=load_dict_with_pickle(path)
search_Graphlets_in_egoistic_database(egoistic_dict_db,Graph_HIPPIE_confidence_075)
"""
#Trial -4
"""
from reindex_as_egoistic_structure_databases import reindex_dataset_as_ego_structure, assign_wire, Wire, pd, plot_generated_graph
wire_database312 = pd.read_csv(str(os.getcwd()) + '/example_dbs/312_confidence_db', sep=';')
#wire_database312 = wire_database312.drop(range(100, 95000), axis='index')
#wire_database312.to_csv("wire_database_diminished_Actual", index=True)
wire_Graph_312 = nx.Graph()
for index, row in wire_database312.iterrows():
assign_wire(wire_Graph_312,
Wire(row['InNode'], row['TermNode'], row['confidence'], True))
ego_dictionary = reindex_dataset_as_ego_structure(wire_Graph_312, save_dict=False,
name="HIPPIE-confidence-075", path="/actual_databases/")
found_graphlets_312 = search_Graphlets_in_egoistic_database(ego_dictionary, wire_Graph_312) # AMAZING
plot_generated_graph(wire_Graph_312,312, name_of_map="/Map_Prototoype_312")
#found_graphlets_312 = set(found_graphlets_312[1])
############ Second
print("#############################################################################################################")
from reindex_as_egoistic_structure_databases import reindex_dataset_as_ego_structure, assign_wire, Wire, pd, plot_generated_graph
wire_database313 = pd.read_csv(str(os.getcwd()) + '/example_dbs/313_confidence_db', sep=';')
#wire_database313 = wire_database313.drop(range(100, 95000), axis='index')
#wire_database313.to_csv("wire_database_diminished_Actual", index=True)
wire_Graph_313 = nx.Graph()
for index, row in wire_database313.iterrows():
assign_wire(wire_Graph_313,
Wire(row['InNode'], row['TermNode'], row['confidence'], True))
ego_dictionary = reindex_dataset_as_ego_structure(wire_Graph_313, save_dict=False,
name="HIPPIE-confidence-075", path="/actual_databases/")
found_graphlets_313 = search_Graphlets_in_egoistic_database(ego_dictionary, wire_Graph_313) # AMAZING
plot_generated_graph(wire_Graph_313,313, name_of_map="/Map_Prototoype_313")
#found_graphlets_313 = set(found_graphlets_313[1])
#print(found_graphlets_312.difference(found_graphlets_313))
"""
#Trial-5