-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmully_import.R
100 lines (96 loc) · 3.07 KB
/
mully_import.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
#' Import a mully graph from CSV files
#'
#' @param name The name of the graph
#' @param direct A boolean to indicate if the graph is directed or not
#' @param layers The path to the CSV file containing the layers' information
#' @param nodes The path to the CSV file containing the nodes' information
#' @param edges The path to the CSV file containing the edges' information
#'
#' @return A new mully graph
#' @export
importGraphCSV<-function(name=NA,direct=FALSE,layers,nodes,edges){
if(missing(layers) || !file.exists(layers) ||
missing(nodes) || !file.exists(nodes) ||
missing(edges) || !file.exists(edges)){
stop("Invalid arguments")
}
g=mully(name,direct)
g=importLayersCSV(g,layers)
g=importNodesCSV(g,nodes)
g=importEdgesCSV(g,edges)
#name the class
class(g) = c("mully",class(g))
return(g)
}
#' Import Layers to a mully graph from a CSV file
#'
#' @param g The mully graph to which the layers will be added. If missing, a new mully graph is created
#' @param file The path to the CSV file containing the layers' information
#'
#' @return The mully graph with the added layers
#' @export
#' @import igraph
#' @importFrom utils read.csv
#' @import igraph
importLayersCSV<-function(g,file){
if(missing(g) || missing(file) || !file.exists(file)){
stop("Invalid arguments")
}
layers=read.csv(file,header=TRUE,stringsAsFactors = FALSE)
g=addLayer(g,layers$Name)
return(g)
}
#' Import Nodes to a mully graph from a CSV file
#'
#' @param g The mully graph to which the nodes will be added. The graph should already have the layers.
#' @param file The path to the CSV file containing the nodes' information
#' @param name The name of the column containing the names of the nodes
#'
#' @return The mully graph with the added nodes
#' @export
#' @importFrom utils read.csv
#' @import igraph
importNodesCSV<-function(g,file,name="name"){
if(missing(g) || missing(file) || !file.exists(file)){
stop("Invalid arguments")
}
nodes=read.csv(file,header=TRUE,stringsAsFactors = FALSE)
rows=dim(nodes)[1]
for(i in 1:rows){
node=nodes[i,]
attr=node[3:length(node)]
attr$n=NULL
attr$name=NULL
attr=as.list(attr)
if(!isLayer(g,node$n)){
next
g=addLayer(g,node$n)
}
g=addNode(g,nodeName = as.character(node$name),layerName = as.character(node$n),attributes = attr)
}
class(g)=c("mully",class(g))
return(g)
}
#' Import Edges to a mully graph from a CSV file
#'
#' @param g The mully graph to which the nodes will be added. The graph should already have the layers and the nodes.
#' @param file The path to the CSV file containing the edges' information
#'
#' @return The mully graph with the added edges
#'
#'
#' @export
#' @importFrom utils read.csv
importEdgesCSV<-function(g,file){
if(missing(g) || missing(file) || !file.exists(file)){
stop("Invalid arguments")
}
edges=read.csv(file,header=TRUE,stringsAsFactors = FALSE)
rows=dim(edges)[1]
for(i in 1:rows){
edge=edges[i,]
attr=as.list(edge[-1][-1])
g=addEdge(g,nodeStart = as.character(edge$V1),nodeDest = as.character(edge$V2),attributes = attr)
}
return(g)
}