/
Groups.R
303 lines (273 loc) · 13.9 KB
/
Groups.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# ==============================================================================
# Functions for working with GROUPS in Cytoscape.
#
# ------------------------------------------------------------------------------
# TODO: deleteGroup(title, group.name, base.url) http://localhost:1234/v1/networks/52/groups/count
# TODO: renameGroup (group.name, base.url) http://localhost:1234/v1/commands/group/rename
# TODO: getGroupList (title, base.url) http://localhost:1234/v1/networks/52/groups
# TODO: getGroupCount (title, base.url) http://localhost:1234/v1/networks/52/groups/count
# ------------------------------------------------------------------------------
#' @title Add to Group
#'
#' @description Adds the specified nodes and edges to the specified group.
#' @param group.name Specifies the name used to identify the group
#' @param nodes List of node SUIDs, names, other column values, or keyword:
#' selected, unselected or all. Default is currently selected nodes.
#' @param nodes.by.col name of node table column corresponding to provided nodes
#' list. Default is 'SUID'.
#' @param edges List of edge SUIDs, names, other column values, or keyword:
#' selected, unselected or all. Default is currently selected edges.
#' @param edges.by.col name of edge table column corresponding to provided edges
#' list. Default is 'SUID'.
#' @param network (optional) Name or SUID of the network. Default is the "current" network active in Cytoscape.
#' @param base.url (optional) Ignore unless you need to specify a custom domain,
#' port or version to connect to the CyREST API. Default is http://localhost:1234
#' and the latest version of the CyREST API supported by this version of RCy3.
#' @return None
#' @examples \donttest{
#' AddToGroup('myGroup')
#' }
#' @export
AddToGroup <- function(group.name, nodes=NULL, nodes.by.col='SUID',
edges=NULL, edges.by.col='SUID',
network=NULL, base.url=.defaultBaseUrl){
if (length(nodes)==1 && nodes[1] %in% c('all','selected','unselected'))
nodes.by.col = NULL
node.list <- .prepPostQueryLists(nodes, nodes.by.col)
if (length(edges)==1 && edges[1] %in% c('all','selected','unselected'))
edges.by.col = NULL
edge.list <- .prepPostQueryLists(edges, edges.by.col)
net.suid <- getNetworkSuid(network,base.url)
commandsPOST(paste0('group add groupName="',group.name,'"',
' nodeList="',node.list,'"',
' edgeList="',edge.list,'"',
' network="SUID:',net.suid,'"'),
base.url = base.url)
}
# ------------------------------------------------------------------------------
#' @title Collapse Group
#'
#' @description Replaces the representation of all of the nodes and edges in a group with a single node.
#' @param groups (optional) List of group names or keywords: all, selected,
#' unselected. Default is the currently selected group.
#' @param network (optional) Name or SUID of the network. Default is the "current" network active in Cytoscape.
#' @param base.url (optional) Ignore unless you need to specify a custom domain,
#' port or version to connect to the CyREST API. Default is http://localhost:1234
#' and the latest version of the CyREST API supported by this version of RCy3.
#' @return None
#' @examples \donttest{
#' collapseGroup()
#' }
#' @export
collapseGroup <- function(groups=NULL, network=NULL, base.url=.defaultBaseUrl){
group.list <- .prepPostQueryLists(groups)
net.suid <- getNetworkSuid(network,base.url)
commandsPOST(paste0('group collapse groupList="',group.list,'"',
' network="SUID:',net.suid,'"'),
base.url = base.url)
}
# ------------------------------------------------------------------------------
#' @title Create Group
#'
#' @description Create a group from the specified nodes.
#' @param group.name The name used to identify and optionaly label the group
#' @param nodes List of node SUIDs, names, other column values, or keyword:
#' selected, unselected or all. Default is currently selected nodes.
#' @param nodes.by.col name of node table column corresponding to provided nodes
#' list. Default is 'SUID'.
#' @param network (optional) Name or SUID of the network. Default is the "current" network active in Cytoscape.
#' @param base.url (optional) Ignore unless you need to specify a custom domain,
#' port or version to connect to the CyREST API. Default is http://localhost:1234
#' and the latest version of the CyREST API supported by this version of RCy3.
#' @return Group SUID
#' @examples \donttest{
#' createGroup('myGroup')
#' }
#' @export
createGroup <- function(group.name, nodes=NULL, nodes.by.col='SUID',
network=NULL, base.url=.defaultBaseUrl){
if (length(nodes)==1 && nodes[1] %in% c('all','selected','unselected'))
nodes.by.col = NULL
node.list <- .prepPostQueryLists(nodes, nodes.by.col)
net.suid <- getNetworkSuid(network,base.url)
commandsPOST(paste0('group create groupName="',group.name,'"',
' nodeList="',node.list,'"',
' network="SUID:',net.suid,'"'),
base.url = base.url)
}
# ------------------------------------------------------------------------------
#' @title Create Group by Column
#'
#' @description Create a group of nodes defined by a column value.
#' @param group.name The name used to identify and optionaly label the group
#' @param column The name or header of the Node Table column to use for selecting nodes to group
#' @param value The value in the column to use for selecting nodes to group
#' @param network (optional) Name or SUID of the network. Default is the "current" network active in Cytoscape.
#' @param base.url (optional) Ignore unless you need to specify a custom domain,
#' port or version to connect to the CyREST API. Default is http://localhost:1234
#' and the latest version of the CyREST API supported by this version of RCy3.
#' @return Group SUID
#' @examples \donttest{
#' createGroupByColumn('myGroup','Cluster', 'A')
#' }
#' @export
createGroupByColumn <- function(group.name, column=NULL, value=NULL,
network=NULL, base.url=.defaultBaseUrl) {
net.suid <- getNetworkSuid(network,base.url)
commandsPOST(paste0('group create groupName="',group.name,'"',
' nodeList="',column,'":"',value,'"',
' network="SUID:',net.suid,'"'),
base.url = base.url)
}
# ------------------------------------------------------------------------------
#' @title Expand Group
#'
#' @description Replaces the group node with member nodes for a set of groups.
#' @param groups (optional) List of group names or keywords: all, selected,
#' unselected. Default is the currently selected group.
#' @param network (optional) Name or SUID of the network. Default is the "current" network active in Cytoscape.
#' @param base.url (optional) Ignore unless you need to specify a custom domain,
#' port or version to connect to the CyREST API. Default is http://localhost:1234
#' and the latest version of the CyREST API supported by this version of RCy3.
#' @return None
#' @examples \donttest{
#' expandGroup()
#' }
#' @export
expandGroup <- function(groups=NULL, network=NULL, base.url=.defaultBaseUrl){
group.list <- .prepPostQueryLists(groups)
net.suid <- getNetworkSuid(network,base.url)
commandsPOST(paste0('group expand groupList="',group.list,'"',
' network="SUID:',net.suid,'"'),
base.url = base.url)
}
# ------------------------------------------------------------------------------
#' @title Get Group Information
#'
#' @description Retrieve information about a group by name or identifier.
#' @param group Group name or SUID.
#' @param network (optional) Name or SUID of the network. Default is the "current" network active in Cytoscape.
#' @param base.url (optional) Ignore unless you need to specify a custom domain,
#' port or version to connect to the CyREST API. Default is http://localhost:1234
#' and the latest version of the CyREST API supported by this version of RCy3.
#' @return Group information
#' @examples \donttest{
#' getGroupInfo('myGroup')
#' }
#' @export
getGroupInfo <- function(group, network=NULL, base.url=.defaultBaseUrl){
net.suid <- getNetworkSuid(network,base.url)
#group.suid <- .nodeNameToNodeSUID(group, network, base.url)
## Note: if not yet collapsed, then group node is not in node list!
## so work with the user-provided group name or SUID directly instead
if(is.numeric(group))
prefix <- 'SUID:'
else
prefix <- ''
commandsPOST(paste0('group get node="',prefix,group,'"',
' network="SUID:',net.suid,'"'),
base.url = base.url)
}
# ------------------------------------------------------------------------------
#' @title List Groups
#'
#' @description Retrieve a list of all group SUIDs in a network.
#' @param network (optional) Name or SUID of the network. Default is the "current" network active in Cytoscape.
#' @param base.url (optional) Ignore unless you need to specify a custom domain,
#' port or version to connect to the CyREST API. Default is http://localhost:1234
#' and the latest version of the CyREST API supported by this version of RCy3.
#' @return List of group SUIDs
#' @examples \donttest{
#' listGroups()
#' }
#' @export
listGroups <- function(network=NULL, base.url=.defaultBaseUrl){
net.suid <- getNetworkSuid(network,base.url)
commandsPOST(paste0('group list',
' network="SUID:',net.suid,'"'),
base.url = base.url)
}
# ------------------------------------------------------------------------------
#' @title Remove from Group
#'
#' @description Removes the specified nodes and edges from the specified group.
#' @param group.name Specifies the name used to identify the group
#' @param nodes List of node SUIDs, names, other column values, or keyword:
#' selected, unselected or all. Default is currently selected nodes.
#' @param nodes.by.col name of node table column corresponding to provided nodes
#' list. Default is 'SUID'.
#' @param edges List of edge SUIDs, names, other column values, or keyword:
#' selected, unselected or all. Default is currently selected edges.
#' @param edges.by.col name of edge table column corresponding to provided edges
#' list. Default is 'SUID'.
#' @param network (optional) Name or SUID of the network. Default is the "current" network active in Cytoscape.
#' @param base.url (optional) Ignore unless you need to specify a custom domain,
#' port or version to connect to the CyREST API. Default is http://localhost:1234
#' and the latest version of the CyREST API supported by this version of RCy3.
#' @return None
#' @examples \donttest{
#' RemoveFromGroup('myGroup')
#' }
#' @export
RemoveFromGroup <- function(group.name, nodes=NULL, nodes.by.col='SUID',
edges=NULL, edges.by.col='SUID',
network=NULL, base.url=.defaultBaseUrl){
if (length(nodes)==1 && nodes[1] %in% c('all','selected','unselected'))
nodes.by.col = NULL
node.list <- .prepPostQueryLists(nodes, nodes.by.col)
if (length(edges)==1 && edges[1] %in% c('all','selected','unselected'))
edges.by.col = NULL
edge.list <- .prepPostQueryLists(edges, edges.by.col)
net.suid <- getNetworkSuid(network,base.url)
commandsPOST(paste0('group remove groupName="',group.name,'"',
' nodeList="',node.list,'"',
' edgeList="',edge.list,'"',
' network="SUID:',net.suid,'"'),
base.url = base.url)
}
# ------------------------------------------------------------------------------
#' @title Delete (or Ungroup) a Group
#'
#' @description Deletes one or more groups, while leaving member nodes intact.
#' @param groups (optional) List of group SUIDs, names, other column values or keywords: all, selected,
#' unselected. Default is the currently selected group.
#' @param groups.by.col name of node table column corresponding to provided groups
#' list. Default is 'SUID'.
#' @param network (optional) Name or SUID of the network. Default is the "current" network active in Cytoscape.
#' @param base.url (optional) Ignore unless you need to specify a custom domain,
#' port or version to connect to the CyREST API. Default is http://localhost:1234
#' and the latest version of the CyREST API supported by this version of RCy3.
#' @return None
#' @examples \donttest{
#' deleteGroup()
#' }
#' @details Note: group nodes are ungrouped but not deleted in Cytoscape version 3.6.1
#' @export
deleteGroup <- function(groups=NULL, groups.by.col='SUID',
network=NULL, base.url=.defaultBaseUrl){
if (length(groups)==1 && groups[1] %in% c('all','selected','unselected'))
groups.by.col = NULL
group.list <- .prepPostQueryLists(groups, groups.by.col)
net.suid <- getNetworkSuid(network,base.url)
commandsPOST(paste0('group ungroup groupList="',group.list,'"',
' network="SUID:',net.suid,'"'),
base.url = base.url)
}
# ------------------------------------------------------------------------------
# Parses all the possible list types and keywords accepted by Commands API.
# If column designation is supported, simply provide a column name; otherwise
# it is assumed to not be supported and returns a simple list.
.prepPostQueryLists <- function(cmd.list=NULL, cmd.by.col=NULL){
if (is.null(cmd.list)) {
cmd.list.ready = "selected" #need something here for edge selections to work
} else if (!is.null(cmd.by.col)) {
cmd.list.col = NULL
for (i in seq_len(length(cmd.list))) {
cmd.list.col[i] = paste(cmd.by.col, cmd.list[i], sep = ":")
}
cmd.list.ready = paste(cmd.list.col, collapse = ",")
} else {
cmd.list.ready = paste(cmd.list, collapse=",")
}
return(cmd.list.ready)
}