Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP read in metadata #487

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion R/neuron-io.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ read.neurons<-function(paths, pattern=NULL, neuronnames=NULL, format=NULL,
total = length(paths),
show_after=2)

mdl=list()
for(n in nn){
if(interactive())
pb$tick()
Expand All @@ -287,6 +288,11 @@ read.neurons<-function(paths, pattern=NULL, neuronnames=NULL, format=NULL,
nl=x
break
}
md <- attr(x, 'metadata')
if(!is.null(md)) {
mdl[[n]]=md
attr(x, 'metadata')=NULL
}
nl[[n]]=x
}

Expand All @@ -305,6 +311,10 @@ read.neurons<-function(paths, pattern=NULL, neuronnames=NULL, format=NULL,
}
# nb only keep dataframe rows for neurons that were successfully read in
# Look after the attached dataframe
if(is.null(df) && length(mdl)>0) {
df <- do.call(rbind, mdl)
rownames(df) <- names(mdl)
}
if(!is.null(df)){
data.frame(nl)=df
}
Expand Down Expand Up @@ -548,7 +558,11 @@ read.neuron.swc<-function(f, ...){
d=read.swc(f)
# multiply by 2 to get diam which is what I work with internally
d$W=d$W*2
as.neuron(d, InputFileName=f, ...)
d=as.neuron(d, InputFileName=f, ...)
metadata=read_swc_meta(f)
if(!is.null(metadata))
attr(d, 'metadata')=metadata
d
}

# internal function that just reads a table of SWC format data
Expand All @@ -566,6 +580,27 @@ read.swc<-function(f){
d
}

read_swc_comments <- function(f) {
con=file(f, open='r')
on.exit(close(con))
header=character()
while(TRUE) {
l=try(readLines(con = con, n=1), silent = T)
if(inherits(l, 'try-error') || nchar(l)<1 || substr(l,1,1)!="#")
break;
header=c(header, l)
}
header
}

read_swc_meta <- function(f, parse=TRUE) {
h=read_swc_comments(f)
if(!isTRUE(any(nchar(metal <- grep("^# Meta: ", h, value = TRUE))>0)))
return(NULL)
meta=substr(metal, nchar("# Meta: ")+1, nchar(metal))
if(isTRUE(parse)) jsonlite::fromJSON(meta, simplifyVector = F) else meta
}

#' @rdname read.neuron.swc
#' @inheritParams ngraph
#' @export
Expand Down