From 31216d32ec4d34fd1de7fac7a2806cde2754e2b2 Mon Sep 17 00:00:00 2001 From: Romain Franconville Date: Wed, 8 Jan 2020 09:51:02 -0500 Subject: [PATCH 1/4] - Use ConnectsTo to return info about connectivity in all the ROIs --- R/connectivity.R | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/R/connectivity.R b/R/connectivity.R index b4b229f7..1adbf89b 100644 --- a/R/connectivity.R +++ b/R/connectivity.R @@ -71,20 +71,28 @@ neuprint_connection_table <- function(bodyids, prepost = c("PRE","POST"), roi = } dp=neuprint_dataset_prefix(dataset, conn=conn) prefixed=paste0(dp, all_segments.json) - cypher = sprintf("WITH %s AS bodyIds UNWIND bodyIds AS bodyId MATCH (a:`%s`)<-[:From]-(c:ConnectionSet)-[:To]->(b:`%s`), (c)-[:Contains]->(s:Synapse) WHERE %s (s.type='post') AND %s.bodyId=bodyId RETURN a.bodyId AS %s, b.bodyId AS %s, count(*) AS weight", + cypher = sprintf(paste("WITH %s AS bodyIds UNWIND bodyIds AS bodyId", + "MATCH (a:`%s`)-[c:ConnectsTo]->(b:`%s`)", + "WHERE %s.bodyId=bodyId", + "UNWIND %s AS k", + "RETURN a.bodyId AS %s, b.bodyId AS %s, k AS roi,", + "apoc.convert.fromJsonMap(c.roiInfo)[k].post AS weight"), jsonlite::toJSON(unique(as.numeric(unlist(bodyids)))), prefixed, prefixed, - ifelse(is.null(roi),"",sprintf("(exists(s.`%s`)) AND",roi)), - ifelse(prepost=="POST","b","a"), - ifelse(prepost=="POST","partner","bodyid"), - ifelse(prepost=="POST","bodyid","partner")) + ifelse(prepost=="POST","a","b"), + ifelse(is.null(roi),"keys(apoc.convert.fromJsonMap(c.roiInfo))",roi), + ifelse(prepost=="POST","bodyid","partner"), + ifelse(prepost=="POST","partner","bodyid") + ) nc = neuprint_fetch_custom(cypher=cypher, conn = conn) - d = data.frame(do.call(rbind,lapply(nc$data,unlist))) - colnames(d) = unlist(nc$columns) + ## Filter out the rare cases where PSDs and tbars are in different ROIs (hence post is null) + nc$data <- nc$data[sapply(nc$data,function(x) !is.null(x[[4]]))] + d <- data.frame(do.call(rbind,lapply(nc$data,unlist))) + colnames(d) <- unlist(nc$columns) d$prepost = ifelse(prepost=="PRE",0,1) d = d[order(d$weight,decreasing=TRUE),] - d[,c("bodyid", "partner", "weight", "prepost")] + d[,c("bodyid", "partner", "roi","weight", "prepost")] } #' @title Get the common synaptic partners for a set of neurons From 923c990d941e7cee1aa8755e909e1b1f187674de Mon Sep 17 00:00:00 2001 From: Romain Franconville Date: Wed, 8 Jan 2020 10:45:31 -0500 Subject: [PATCH 2/4] Can now input a ROI (or a vector of ROIs). Still need to double check results accuracy --- R/connectivity.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/connectivity.R b/R/connectivity.R index 1adbf89b..4cb205c8 100644 --- a/R/connectivity.R +++ b/R/connectivity.R @@ -81,7 +81,7 @@ neuprint_connection_table <- function(bodyids, prepost = c("PRE","POST"), roi = prefixed, prefixed, ifelse(prepost=="POST","a","b"), - ifelse(is.null(roi),"keys(apoc.convert.fromJsonMap(c.roiInfo))",roi), + ifelse(is.null(roi),"keys(apoc.convert.fromJsonMap(c.roiInfo))",paste("['",paste(roi,collapse="','"),"']",sep="")), ifelse(prepost=="POST","bodyid","partner"), ifelse(prepost=="POST","partner","bodyid") ) From be0d41cb94f5f77ab620add5131a9c5e46a8895c Mon Sep 17 00:00:00 2001 From: Romain Franconville Date: Wed, 8 Jan 2020 11:05:08 -0500 Subject: [PATCH 3/4] Fix type for the weight column --- R/connectivity.R | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/R/connectivity.R b/R/connectivity.R index 4cb205c8..ba3866ee 100644 --- a/R/connectivity.R +++ b/R/connectivity.R @@ -88,10 +88,11 @@ neuprint_connection_table <- function(bodyids, prepost = c("PRE","POST"), roi = nc = neuprint_fetch_custom(cypher=cypher, conn = conn) ## Filter out the rare cases where PSDs and tbars are in different ROIs (hence post is null) nc$data <- nc$data[sapply(nc$data,function(x) !is.null(x[[4]]))] - d <- data.frame(do.call(rbind,lapply(nc$data,unlist))) + d <- data.frame(do.call(rbind,lapply(nc$data,unlist)),stringsAsFactors = FALSE) colnames(d) <- unlist(nc$columns) - d$prepost = ifelse(prepost=="PRE",0,1) - d = d[order(d$weight,decreasing=TRUE),] + d$weight <- as.integer(d$weight) + d$prepost <- ifelse(prepost=="PRE",0,1) + d <- d[order(d$weight,decreasing=TRUE),] d[,c("bodyid", "partner", "roi","weight", "prepost")] } From 3e5627993755758c5e5e609106ef8065504ec00f Mon Sep 17 00:00:00 2001 From: Romain Franconville Date: Wed, 8 Jan 2020 11:06:13 -0500 Subject: [PATCH 4/4] Small doc edit --- R/connectivity.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/connectivity.R b/R/connectivity.R index ba3866ee..6e69cee4 100644 --- a/R/connectivity.R +++ b/R/connectivity.R @@ -42,7 +42,7 @@ neuprint_get_adjacency_matrix <- function(bodyids, dataset = NULL, all_segments #' @inheritParams neuprint_find_neurons #' @param prepost whether to look for partners presynaptic to postsynaptic to the given bodyids #' @param progress default FALSE. If TRUE, the API is called separately for each neuron and yuo can asses its progress, if an error is thrown by any one bodyid, that bodyid is ignored -#' @return a data frame giving partners within an ROI, the connection strength for weights to or from that partner, and the direction, for the given bodyid +#' @return a data frame giving partners within a set of ROIs, the connection strength for weights to or from that partner, and the direction, for the given bodyid #' @seealso \code{\link{neuprint_fetch_custom}}, \code{\link{neuprint_simple_connectivity}}, \code{\link{neuprint_common_connectivity}} #' @export #' @rdname neuprint_connection_table