-
Notifications
You must be signed in to change notification settings - Fork 4
/
mully_edge.R
251 lines (219 loc) · 6.98 KB
/
mully_edge.R
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
########### Edge Functions ##################
#' Get the ids of the edges connecting two nodes
#'
#' @param g The input graph
#' @param nodeStart The first endpoint of the edge
#' @param nodeDest The second endpoint of the edge
#'
#' @return A list containing the ids of the edges connecting the nodes
#'
#' @export
#' @import igraph
#' @examples
#' g=mully::demo()
#' getIDEdge(g,"d2","dr1")
getIDEdge <- function(g, nodeStart, nodeDest) {
if(missing(g) || !is.mully(g) || missing(nodeStart) || missing(nodeDest)){
stop("Invalid Arguments")
}
v1 = getNode(g, nodeStart)
v2 = getNode(g, nodeDest)
if(is.null(v1) || is.null(v2)){
stop("Invalid Nodes")
}
if(!are.connected(g,v1,v2)){
return(0)
}
edgeList=as.data.frame(get.edgelist(g),stringsAsFactors = FALSE)
if(is.directed(g)){
e=which(edgeList$V1==nodeStart & edgeList$V2==nodeDest)
}
else{
e=which((edgeList$V1==nodeStart & edgeList$V2==nodeDest) | (edgeList$V2==nodeStart & edgeList$V1==nodeDest))
}
return(e)
}
#' Get the attributes of the edges connecting two nodes
#'
#' @param g The input graph
#' @param nodeStart The first endpoint of the edge
#' @param nodeDest The second endpoint of the edge
#'
#' @return A dataframe containing the edges with their attributes. If both nodes' arguments are missing, it returns all the edges with their attributes.
#' @export
#' @import igraph
#' @examples
#' g=mully::demo()
#' #Print all Edges
#' getEdgeAttributes(g)
#' #Get a Single Edge
#' getEdgeAttributes(g,"d2","g1")
getEdgeAttributes<-function(g,nodeStart,nodeDest){
if(missing(g) || !is.mully(g)){
stop("Invalid Arguments")
}
edgeList=as.data.frame(get.edgelist(g),stringsAsFactors = FALSE)
attributes=as.data.frame(get.edge.attribute(g),stringsAsFactors = FALSE)
edgeAttributes=cbind(edgeList,attributes)
if(missing(nodeStart) && missing(nodeDest)){
return(edgeAttributes)
}
edge=c(1:dim(edgeAttributes)[1])
if(!missing(nodeStart) && !missing(nodeDest)){
if(!nodeStart%in%V(g)$name || !nodeDest%in%V(g)$name){
stop("Invalid Nodes")
}
edge=getIDEdge(g,nodeStart,nodeDest)
return(edgeAttributes[edge,])
}
if(missing(nodeStart))
return(edgeAttributes[(edgeAttributes$V1==nodeDest) | (edgeAttributes$V2==nodeDest),])
return(edgeAttributes[(edgeAttributes$V1==nodeStart) | (edgeAttributes$V2==nodeStart),])
}
#' Add an edge
#'
#' @param g The input graph
#' @param nodeStart The first endpoint of the edge
#' @param nodeDest The second endpoint of the edge
#' @param attributes The attributes to assign to the edge
#'
#' @return The mully graph, with the added edge
#'
#' @export
#' @import igraph
#' @examples
#' g=mully::demo()
#' addEdge(g,"dr3","g2",attributes=list(name="newEdge"))
addEdge <- function(g, nodeStart, nodeDest, attributes) {
#Check arguments
if (missing(g) || missing(nodeStart) || missing(nodeDest)) {
stop("Invalid Arguments")
}
if (are.connected(g, nodeStart, nodeDest)) {
if(missing(attributes)){
stop("Nodes are already Connected. Please provide attributes for the new Edge")
}
df=cbind(as.data.frame(list(V1=nodeStart,V2=nodeDest),stringsAsFactors = FALSE),as.data.frame(attributes))
df1=cbind(as.data.frame(list(V1=nodeDest,V2=nodeStart),stringsAsFactors = FALSE),as.data.frame(attributes))
allEdges=getEdgeAttributes(g,nodeStart,nodeDest)
if(is.directed(g) && !is.null(getIDCommonDF(allEdges,df))){
stop("Edge Already Exists ")
}
if(!is.directed(g) && (!is.null(getIDCommonDF(allEdges,df)) || !is.null(getIDCommonDF(allEdges,df1)))){
stop("Edge Already Exists ")
}
}
g <- g + edge(nodeStart, nodeDest)
edges=getIDEdge(g, nodeStart, nodeDest)
idEdge = edges[length(edges)]
#Assign attributes to the created edge
for (key in names(attributes)) {
g <- set.edge.attribute(g, key, index = idEdge, attributes[[key]])
}
#name the class
class(g) = c("mully",class(g))
return(g)
}
#' Delete an edge
#'
#' @param g The input graph
#' @param nodeStart The first endpoint of the edge
#' @param nodeDest The second endpoint of the edge
#' @param attributes The attributes of the edge to delete. Required if the nodes are multi-connected
#' @param multi A boolean. Specifies whether to delete multiple edges or not, in case they exist.
#'
#' @return The mully graph with the deleted edges
#'
#' @export
#' @import igraph
#' @examples
#' g=mully::demo()
#' removeEdge(g,"dr1","d2",multi=TRUE)
removeEdge <- function(g, nodeStart, nodeDest,attributes=NA, multi=FALSE) {
if (!are.connected(g,nodeStart,nodeDest)) {
stop("Nodes are not connected")
}
idEdge = as.numeric(getIDEdge(g, nodeStart, nodeDest))
if(length(idEdge)>1 && missing(attributes) && multi==FALSE){
message("Nodes have multiple Edges. Please provide specific attributes.")
message(getEdgeAttributes(g,nodeStart,nodeDest))
stop()
}
if(multi==TRUE && is.na(attributes)){
g <- g - edge(idEdge)
#name the class
class(g) = c("mully",class(g))
return(g)
}
allEdges=getEdgeAttributes(g,nodeStart,nodeDest)
df=as.data.frame(list(V1=nodeStart,V2=nodeDest),stringsAsFactors = FALSE)
df1=as.data.frame(list(V1=nodeDest,V2=nodeStart),stringsAsFactors = FALSE)
if(!is.na(attributes)){
df=cbind(df,as.data.frame(attributes))
df1=cbind(df1,as.data.frame(attributes))
colnames(df)=c(c("V1","V2",names(attributes)))
colnames(df1)=c(c("V1","V2",names(attributes)))
}
c1=getIDCommonDF(allEdges,df)
c2=getIDCommonDF(allEdges,df1)
comAll=c()
if(is.directed(g)){
comAll=c1
}
if(!is.directed(g)){
comAll=c(c1,c2)
}
if(is.null(comAll)){
stop("Edge Does not Exist ")
}
if(length(comAll)>1 && multi==FALSE){
message("Nodes have multiple Edges. Please provide specific attributes.")
message(getEdgeAttributes(g,nodeStart,nodeDest))
stop()
}
g <- g - edge(as.numeric(comAll))
#name the class
class(g) = c("mully",class(g))
return(g)
}
addTransEdges<-function(g,nodes){
if(missing(g) || missing(nodes) || !is.mully(g)){
stop("Invalid Arguments")
}
allEdges=as.data.frame(get.edgelist(g),stringsAsFactors = FALSE)
inN=c()
outN=c()
attributes=list("type"="trans","via"="")
for(node in nodes){
attributes$via=node
#Unique or Not???
inN=unique(as.character(allEdges[which(allEdges$V2==node),]$V1))
outN=unique(as.character(allEdges[which(allEdges$V1==node),]$V2))
#No Transitive edges to add
if(length(inN)==0 | length(outN)==0)
next
if(is.directed(g)){
for(inNode in inN){
for(outNode in outN){
message(paste("The edge ",inN,"-->",outN," will be added.",sep=""))
g<-addEdge(g,inNode,outNode,attributes)
}
}
}
else{
all=append(inN,outN)
for(i in 1:(length(all)-1)){
for(j in i+1:(length(all))){
if(j>length(all)){
break
}
message(paste("The edge ",all[i],"--",all[j]," will be added.",sep=""))
g<-addEdge(g,all[i],all[j],attributes)
}
}
}
}
#name the class
class(g) = c("mully",class(g))
return(g)
}