forked from skirkpatrick/iSite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisite.cpp
222 lines (177 loc) · 5.41 KB
/
isite.cpp
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
#include<cstdio> //fscanf can be useful if sites are read as strings
#include<cstdlib> //for atoi
#include<iostream> //for input and output
#include<fstream> //for file input and output
#include<utility> //for std::pair
#include<vector> //for std::vector
#include<map> //for std::map
#include<cstring> //for std::string
#include<boost/graph/adjacency_list.hpp>
#include<boost/graph/connected_components.hpp>
using namespace std;
using namespace boost;
typedef adjacency_list_traits<listS,vecS,undirectedS>::edge_descriptor edge_descriptor;
typedef adjacency_list_traits<listS,vecS,undirectedS>::vertex_descriptor vertex_descriptor;
struct isite
{
vector<edge_descriptor> edges;
unsigned int age;
};
struct vertexsites
{
vector<isite> sites;
};
typedef adjacency_list<listS, vecS, undirectedS, vertexsites> Graph;
/*
adjacency_list<OutEdgeList, <-listS=time, vecS=space
VertexList, <-listS=time, vecS=space
Directed, <-iSite graphs are undirected
VertexProperties, <-Possibly not needed?
EdgeProperties, <-Needed for iSites
GraphProperties, <-Possibly not needed?
EdgeList> <-Not needed
*/
typedef graph_traits<Graph>::vertex_iterator vertex_iterator;
typedef graph_traits<Graph>::edge_iterator edge_iterator;
typedef graph_traits<Graph>::adjacency_iterator AdjIter;
/*
Counts the number of triangles in a graph
*/
int triangles(Graph& graph)
{
vertex_iterator vi, viend;
AdjIter adji, adjiend, adji2;
int numTriangles=0;
//Cycle through all vertices
for (tie(vi, viend)=vertices(graph); vi!=viend; vi++)
{
//Cycle through adjacent vertices
for (tie(adji, adjiend)=adjacent_vertices(*vi, graph); adji!=adjiend; adji++)
{
//Check for edges between vertices adjacent to vi aka triangle
for (adji2=adji/*works with self-loops?*/; adji2!=adjiend; ++adji2)
{
if (edge(*adji, *adji2, graph).second)
numTriangles++;
}
}
}
return numTriangles/3;
}
/*
Counts the number of triples in a graph
*/
int triples(Graph& graph)
{
vertex_iterator vi, viend;
int numTriples=0;
for (tie(vi, viend)=vertices(graph); vi!=viend; vi++)
numTriples+=((degree(*vi, graph)*(degree(*vi,graph)-1))/2);
return numTriples;
}
/*
Counts the number of components in a graph
*/
int components(Graph& graph)
{
vector<int> c(num_vertices(graph));
return connected_components(graph, &c[0]);//this is an easier way I found
}
//Stores input parameters
struct parameters
{
string infile;
double prob_loss;
double prob_asym;
int end_order;
int iterations;
} param;
pair<int, int> duplication(Graph& graph)
{
srand(time(NULL));
int parent = (int)((float)rand()/(RAND_MAX)*(boost::num_vertices(graph)-1) );
//the graph -1 and node +1 make it so the range is between 1 to last_node
parent+=1;
cout << "parent: " << parent << endl;
//get a new vertex
Graph::vertex_descriptor v_description = *child;
child = boost::add_vertex(graph);
cout << "added vertex: " << *child << endl;
//give all of the selectedNode edges to the cloned node
out_edge_iterator oei, oeiend;
for( tie(oei, oeiend) = out_edges(parent, graph); oei != oeiend; ++oei )
{
boost::add_edge(target(*oei,graph), *child, graph);
cout << "adding: " << target(*oei,graph) << " to: " << *child << endl;
}
//return parent, child
return pair<parent, *child>
}
//Perform iSite algorithm
void isite(Graph& graph)
{
pair<int,int> vertices;
vertices=duplication(graph);
}
int main(int argc, char* argv[])
{
if (argc!=6)
{
cerr<<"Usage: ./iSite <seed-graph> <probability of loss> "
"<probability of assymetry> <end order> <iterations>"<<endl;
exit(1);
}
Graph graph; //Protein network
param.infile=argv[1]; //Seed graph
param.prob_loss=atof(argv[2]); //Probability of loss of redundancy
param.prob_asym=atof(argv[3]); //Probability of assymetry
param.end_order=atoi(argv[4]); //Order at which to stop
param.iterations=atoi(argv[5]); //Number of times to run algorithm
ifstream infile(param.infile.c_str());
if (!infile)
{
cerr<<"Error opening specified seed graph!"<<endl;
exit(1);
}
map<string,int> nodes;
int counter=0;
string v1,v2;
//Building seed graph
while(!(infile>>v1>>v2).eof())
{
if(nodes[v1]==0)
{
nodes[v1]=++count;
add_vertex(graph);
}
if(nodes[v2]==0)
{
nodes[v2]=++count;
add_vertex(graph);
}
add_edge(nodes[v1],nodes[v2],graph);
//loop to add 1 to all member vectices' age.
}
while (num_vertices(graph)!=param.end_order)
{
}
//Output
ofstream outfile("result");
if (!outfile)
{
cerr<<"Error opening output file: result"<<endl;
exit(1);
}
outfile<<param.prob_loss<<" ";
outfile<<param.prob_asym<<" ";
outfile<<num_vertices(graph)<<" ";
outfile<<num_edges(graph)<<" ";
int triangles=triangles(graph);
int triples=triples(graph);
outfile<<triangles<<" ";
outfile<<triples<<" ";
outfile<<(3*triangles)/triples<<" ";
outifle<<components(graph);
outfile.close();
infile.close();
}