Skip to content

Commit

Permalink
Add read/write_catmaid_selection
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferis committed Aug 4, 2017
1 parent 36e7ba1 commit 7dea1ce
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Imports:
rgl,
dplyr,
nabor,
xml2
xml2,
grDevices
Suggests:
testthat
Remotes: jefferis/nat
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ export(connectors)
export(copy_tags_connectors)
export(read.neuron.catmaid)
export(read.neurons.catmaid)
export(read_catmaid_selection)
export(write_catmaid_selection)
import(httr)
import(nat)
importFrom(dplyr,as_data_frame)
importFrom(dplyr,bind_rows)
importFrom(dplyr,right_join)
importFrom(grDevices,col2rgb)
importFrom(grDevices,rgb)
importFrom(jsonlite,fromJSON)
importFrom(jsonlite,rbind.pages)
importFrom(nabor,knn)
Expand Down
113 changes: 113 additions & 0 deletions R/catmaid_selection.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#' Read a CATMAID selection file
#'
#' @param f Path to the \code{.json} file saved by CATMAID
#' @param readNeurons Whether or not to read the neurons listed in the selection
#' file
#' @param getNames Whether to fetch the names of the neurons (default
#' \code{TRUE}, ignored if neurons are being fetched)
#' @param ... Connection parameters passed to \code{read.neurons.catmaid}
#' @return A \code{data.frame} containing the selection information or (if
#' \code{readNeurons=TRUE}) a \code{\link[nat]{neuronlist}} containing this
#' information in the attached metadata. The columns will be \itemize{
#'
#' \item{skid} The numeric skeleton id
#'
#' \item{color} The colour of the selected neuron in catmaid
#'
#' \item{opacity} The opacity (alpha value) of the neuron name
#'
#' \item{name} Optionally the name of the neuron
#'
#' }
#' @export
#' @examples
#' f=system.file('catmaid-skeletons.json', package = 'catmaid')
#' read_catmaid_selection(f, getNames=FALSE)
#' \dontrun{
#' # read in the neurons for the selection
#' x=read_catmaid_selection(f, readNeurons=TRUE)
#' # plot using the CATMAID colour specifications
#' plot3d(x, col=color)
#' }
#' @seealso \code{\link{write_catmaid_selection}}
read_catmaid_selection <- function(f, readNeurons=FALSE, getNames=TRUE, ...) {
j=jsonlite::read_json(f, simplifyVector = TRUE)
colnames(j)[colnames(j)=='skeleton_id']='skid'

if(readNeurons) {
x=read.neurons.catmaid(j$skid, OmitFailures = T, ...)
m=merge(x[,], j, by='skid')
rownames(m)=names(x)
x[,]=m
x
} else {
if(getNames)
j$name=catmaid_get_neuronnames(j$skid, ...)
class(j)=c(class(j),'catmaid_view')
j
}
}


#' Make a CATMAID selection file based on neuronlist or skids
#'
#' @param x A neuronlist, data.frame or vector of skids
#' @param f Path to output file (usually ends in .json)
#' @param color Optional vector of colours in any format understood by col2rgb
#' @param opacity Optional vector of opacities (alpha values) in range 0-1
#' @param ... Additional arguments passed to \code{\link[jsonlite]{toJSON}}
#'
#' @export
#' @seealso \code{\link{read_catmaid_selection}}
#' @examples
#' \dontrun{
#' pns=read.neurons.catmaid("annotation:^PN$")
#' # extract the glomerulus from the name
#' pns[, 'glomerulus'] = stringr::str_match(pns[, 'name'],
#' ".*glomerulus ([A-z0-9]+) .*")[, 2]
#' pns[, 'glomerulus']=addNA(factor(pns[, 'glomerulus']))
#' # get the same colours that nat would normally use in a plot
#' plotres=plot3d(pns, col=glomerulus)
#' cols=attr(plotres,'df')$col
#' # write out selection file with those colours
#' write_catmaid_selection(pns, f='pns-by-glom.json', color=cols)
#' }
write_catmaid_selection <- function(x, f, color=NULL, opacity=NULL, ...) {
if(is.neuronlist(x))
x=as.data.frame(x)
if(is.data.frame(x)){
df=x
x=df$skid
# see if the data.frame has colour or opacity information if these are
# missing
if(is.null(color)) {
colcol=na.omit(match(c("colour", "color", "col"), colnames(df)))
if(any(is.finite(colcol)))
color=df[[colcol[1]]]
}
if(is.null(opacity)) {
opac_col=na.omit(match(c("opacity","alpha"), colnames(df)))
if(any(is.finite(opac_col)))
opacity=df[[opac_col[1]]]
}
}
df=data.frame(skeleton_id=x)
if(length(color))
df$color=normalise_colours(color)
if(length(opacity))
df$opacity=opacity

json=jsonlite::toJSON(df, pretty=TRUE, ...)
json=gsub(" ", " ", json, fixed = T)
writeLines(json, f)
}

# will take any unitary colour specification ("red", 1, "#FF0000")
# and turn into a web style colour specification #FF0000
#' @importFrom grDevices col2rgb rgb
normalise_colours <- function(x) {
if(is.null(x))
return(NULL)
cols=apply(col2rgb(x),2, function(x) rgb(x[1], x[2], x[3], maxColorValue = 255))
tolower(cols)
}
177 changes: 177 additions & 0 deletions inst/catmaid-skeletons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
[
{
"skeleton_id": 1180353,
"color": "#ffff00",
"opacity": 1
},
{
"skeleton_id": 1181286,
"color": "#ff00ff",
"opacity": 1
},
{
"skeleton_id": 1182615,
"color": "#0000ff",
"opacity": 1
},
{
"skeleton_id": 1313485,
"color": "#00ff00",
"opacity": 1
},
{
"skeleton_id": 1334552,
"color": "#ffffff",
"opacity": 1
},
{
"skeleton_id": 1334561,
"color": "#00ffff",
"opacity": 1
},
{
"skeleton_id": 1371629,
"color": "#ff7f00",
"opacity": 1
},
{
"skeleton_id": 2390270,
"color": "#7fff00",
"opacity": 1
},
{
"skeleton_id": 2390280,
"color": "#7f7f7f",
"opacity": 1
},
{
"skeleton_id": 3826596,
"color": "#ff0000",
"opacity": 1
},
{
"skeleton_id": 3842618,
"color": "#7f7fff",
"opacity": 1
},
{
"skeleton_id": 3853002,
"color": "#bfbfbf",
"opacity": 1
},
{
"skeleton_id": 3872709,
"color": "#ff7f7f",
"opacity": 1
},
{
"skeleton_id": 4232956,
"color": "#7fff7f",
"opacity": 1
},
{
"skeleton_id": 4260599,
"color": "#ff007f",
"opacity": 1
},
{
"skeleton_id": 1182625,
"color": "#d8a31e",
"opacity": 1
},
{
"skeleton_id": 1273130,
"color": "#3fe0d1",
"opacity": 1
},
{
"skeleton_id": 1334538,
"color": "#ffbfc9",
"opacity": 1
},
{
"skeleton_id": 1338786,
"color": "#08f62f",
"opacity": 1
},
{
"skeleton_id": 3842350,
"color": "#f30b61",
"opacity": 1
},
{
"skeleton_id": 3852709,
"color": "#02fc9f",
"opacity": 1
},
{
"skeleton_id": 3908112,
"color": "#04fa3f",
"opacity": 1
},
{
"skeleton_id": 3910750,
"color": "#ffffff",
"opacity": 1
},
{
"skeleton_id": 3927073,
"color": "#57f608",
"opacity": 1
},
{
"skeleton_id": 3930323,
"color": "#ff0046",
"opacity": 1
},
{
"skeleton_id": 3931453,
"color": "#98ff00",
"opacity": 1
},
{
"skeleton_id": 4149101,
"color": "#89bf3f",
"opacity": 1
},
{
"skeleton_id": 4165941,
"color": "#00ff51",
"opacity": 1
},
{
"skeleton_id": 4214127,
"color": "#ff9a00",
"opacity": 1
},
{
"skeleton_id": 4233149,
"color": "#819cfd",
"opacity": 1
},
{
"skeleton_id": 4260588,
"color": "#dfae9f",
"opacity": 1
},
{
"skeleton_id": 4276626,
"color": "#fc82f7",
"opacity": 1
},
{
"skeleton_id": 4311394,
"color": "#99fa83",
"opacity": 1
},
{
"skeleton_id": 4328317,
"color": "#1306f8",
"opacity": 1
},
{
"skeleton_id": 4344859,
"color": "#790a05",
"opacity": 1
}
]
50 changes: 50 additions & 0 deletions man/read_catmaid_selection.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7dea1ce

Please sign in to comment.