Skip to content

Commit

Permalink
Merge pull request #9 from yunshiuan/dev
Browse files Browse the repository at this point in the history
Include the external function show_cluster_composition()
  • Loading branch information
yunshiuan authored Jan 17, 2019
2 parents 50fc3cc + 2fbdf26 commit 09a9e79
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
export(list_brain_regions)
export(mni_to_region_name)
export(region_name_to_mni)
export(show_cluster_composition)
5 changes: 5 additions & 0 deletions R/label4MRI.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
#' \itemize{
#' \item List the brain regions defined by each labeling system.
#' }
#' \item \code{\link{show_cluster_composition}}
#' \itemize{
#' \item Show brain cluster composition (i.e., percentage of each brain region
#' within it).
#' }
#' }
#'
#' @author
Expand Down
1 change: 1 addition & 0 deletions R/mni_to_region_name.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#' otherwise output 'NULL'.
#' @seealso
#' \code{\link{region_name_to_mni}}
#' \code{\link{show_cluster_composition}}
#' @examples
#' # Exact matching brain region with distance mode on
#' mni_to_region_name(26, 0, 0, distance = TRUE)
Expand Down
86 changes: 86 additions & 0 deletions R/show_cluster_composition.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#' @title
#' Show brain cluster composition
#' @description
#' Input a matrix of all MNI coordinates within a cluster, and output
#' the cluster composition (i.e., percentage of each brain region within it).
#' @param coordinate_matrix A matrix of the size 3 x N, which N is the number of
#' coordinates with the cluster of interest. Three rows correspond to the
#' x, y, z MNI values of each coordinate.
#' @param template One character vector which indicates the templates to use
#' (\code{"aal"} or \code{"ba"}). Use both of them by default.
#' @return
#' Return a list of frequency table and each of them corresponds to a template.
#' Each table consists the brain region names and the corresponding percentage
#' of the input brain cluster.
#'
#' If there are coordinates which do not fall into any labeled brain region
#' (e.g., white matter), it will be labeled as "NULL".
#' @seealso
#' \code{\link{show_nii_clusters}}
#' @examples
#' # Assume there is a cluster of brain coordinates that you want to know
#' # its composition.
#' # The cluster has 10 coordinates which MNI coordinates fall in the cube with
#' # min corner [10, 10, -5] and max cormer [15, 15, 0]
#' set.seed(1)
#' brain_matrix <- matrix(cbind(
#' x = runif(n = 10, min = 10, max = 15),
#' y = runif(n = 10, min = 10, max = 15),
#' z = runif(n = 10, min = -5, max = 0)
#' ), nrow = 3, byrow = T)
#' show_cluster_composition(brain_matrix)
#' @export

show_cluster_composition <- function(coordinate_matrix, template = c("aal", "ba")) {

# example matrix used: coordinate_matrix <- matrix(4:39, nrow = 3)
if_template_exist <- template %in% names(label4mri_metadata)

if (sum(!if_template_exist) != 0) {
stop(paste0("Template `", paste(template[!if_template_exist], collapse = ", "), "` does not exist."))
}

list_frequency_table <-
lapply(
template,
function(.template) {
# Label the coordinate matrix
labeled_coordinate_matrix <-
mapply(
FUN = mni_to_region_name,
x = coordinate_matrix[1, ],
y = coordinate_matrix[2, ],
z = coordinate_matrix[3, ],
distance = F,
MoreArgs = list(template = .template)
)

# Create a frequency table of the coordinates of each brain region
brain_frequency <-
table(unlist(labeled_coordinate_matrix[paste0(.template, ".label"), ]))

sorted_brain_frequency <-
sort(brain_frequency, decreasing = T)

# Add a column of percentate of each brain region
sorted_brain_percentage <-
round(sorted_brain_frequency / ncol(coordinate_matrix), 3) * 100

sorted_table_frequency_percentage <-
cbind(sorted_brain_frequency, sorted_brain_percentage)

colnames(sorted_table_frequency_percentage) <-
c("Number of coordinates", "Percentage (%)")

sorted_table_frequency_percentage
}
)

names(list_frequency_table) <- paste(template,
"cluster",
"composition",
sep = "."
)

list_frequency_table
}
5 changes: 5 additions & 0 deletions man/label4MRI.Rd

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

1 change: 1 addition & 0 deletions man/mni_to_region_name.Rd

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

44 changes: 44 additions & 0 deletions man/show_cluster_composition.Rd

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

0 comments on commit 09a9e79

Please sign in to comment.