library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(ggforce)
library(cocoframer)
library(reshape2)
library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:reshape2':
## 
##     smiths
library(purrr)
options(stringsAsFactors = FALSE)


Get 3d region centroids:

ccf_anno <- get_ccf_annotation() %>% melt()

ccf_anno <- ccf_anno %>%
  filter(value != 0)
names(ccf_anno) <- c("x","y","z","id")


ont <- get_mba_ontology() %>% flatten_mba_ontology()
ont <- generate_mba_taxons(ont)



region_groups <- data.frame(
  
  region_group = c(rep("RHP", 5),
                   "ENT",
                   rep("HIP",4),
                   "ACA",
                   rep("TEa-PERI-ECT", 3),
                   "RSP",
                   rep("VIS", 7),
                   "VISp",
                   "PTLp",
                   "AUD",
                   "SSp",
                   rep("SSs-GU-VISC-AIp", 4),
                   rep("AI", 2),
                   "CLA",
                   rep("MOs-FRP", 2),
                   rep("MOp",1),
                   rep("PL-ILA-ORB", 3)),
  acronym = c("PAR","POST","PRE","SUB","ProS",
              "ENT",
              "CA","DG","FC","IG",
              "ACA",
              "TEa","PERI","ECT",
              "RSP",
              "VISal","VISam","VISl","VISpl","VISpm","VISli","VISpor",
              "VISp",
              "PTLp",
              "AUD",
              "SSp",
              "SSs","GU","VISC","AIp",
              "AId","AIv",
              "CLA",
              "MOs","FRP",
              "MOp",
              "PL","ILA","ORB")
)



region_ont <- map_dfr(1:nrow(region_groups),
                      function(x) {
                        parent_acronym <- region_groups$acronym[x]
                        ontology_children <- filter_mba_ontology_children(ont,
                                                                          parent_acronym,
                                                                          include_parent = TRUE)
                        ontology_children$region_group <- region_groups$region_group[x]
                        ontology_children
                      })



region_vox <- ccf_anno %>%
  filter(id %in% region_ont$id) %>%
  left_join(region_ont)
## Joining with `by = join_by(id)`



region_centroids <- region_vox %>%
  filter(z < 229) %>% # one hemisphere
  group_by(region_group) %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HJlZ2lvbl9ncm91cCA9IGMocmVwKCJSSFAiLCA1KSwNCiAgICAgICAgICAgICAgICAgICAiRU5UIiwNCiAgICAgICAgICAgICAgICAgICByZXAoIkhJUCIsNCksDQogICAgICAgICAgICAgICAgICAgIkFDQSIsDQogICAgICAgICAgICAgICAgICAgcmVwKCJURWEtUEVSSS1FQ1QiLCAzKSwNCiAgICAgICAgICAgICAgICAgICAiUlNQIiwNCiAgICAgICAgICAgICAgICAgICByZXAoIlZJUyIsIDcpLA0KICAgICAgICAgICAgICAgICAgICJWSVNwIiwNCiAgICAgICAgICAgICAgICAgICAiUFRMcCIsDQogICAgICAgICAgICAgICAgICAgIkFVRCIsDQogICAgICAgICAgICAgICAgICAgIlNTcCIsDQogICAgICAgICAgICAgICAgICAgcmVwKCJTU3MtR1UtVklTQy1BSXAiLCA0KSwNCiAgICAgICAgICAgICAgICAgICByZXAoIkFJIiwgMiksDQogICAgICAgICAgICAgICAgICAgIkNMQSIsDQogICAgICAgICAgICAgICAgICAgcmVwKCJNT3MtRlJQIiwgMiksDQogICAgICAgICAgICAgICAgICAgcmVwKCJNT3AiLDEpLA0KICAgICAgICAgICAgICAgICAgIHJlcCgiUEwtSUxBLU9SQiIsIDMpKSwNCiAgYWNyb255bSA9IGMoIlBBUiIsIlBPU1QiLCJQUkUiLCJTVUIiLCJQcm9TIiwNCiAgICAgICAgICAgICAgIkVOVCIsDQogICAgICAgICAgICAgICJDQSIsIkRHIiwiRkMiLCJJRyIsDQogICAgICAgICAgICAgICJBQ0EiLA0KICAgICAgICAgICAgICAiVEVhIiwiUEVSSSIsIkVDVCIsDQogICAgICAgICAgICAgICJSU1AiLA0KICAgICAgICAgICAgICAiVklTYWwiLCJWSVNhbSIsIlZJU2wiLCJWSVNwbCIsIlZJU3BtIiwiVklTbGkiLCJWSVNwb3IiLA0KICAgICAgICAgICAgICAiVklTcCIsDQogICAgICAgICAgICAgICJQVExwIiwNCiAgICAgICAgICAgICAgIkFVRCIsDQogICAgICAgICAgICAgICJTU3AiLA0KICAgICAgICAgICAgICAiU1NzIiwiR1UiLCJWSVNDIiwiQUlwIiwNCiAgICAgICAgICAgICAgIkFJZCIsIkFJdiIsDQogICAgICAgICAgICAgICJDTEEiLA0KICAgICAgICAgICAgICAiTU9zIiwiRlJQIiwNCiAgICAgICAgICAgICAgIk1PcCIsDQogICAgICAgICAgICAgICJQTCIsIklMQSIsIk9SQiIpDQopDQpgYGANClwNClwNCmBgYHtyfQ0KcmVnaW9uX29udCA8LSBtYXBfZGZyKDE6bnJvdyhyZWdpb25fZ3JvdXBzKSwNCiAgICAgICAgICAgICAgICAgICAgICBmdW5jdGlvbih4KSB7DQogICAgICAgICAgICAgICAgICAgICAgICBwYXJlbnRfYWNyb255bSA8LSByZWdpb25fZ3JvdXBzJGFjcm9ueW1beF0NCiAgICAgICAgICAgICAgICAgICAgICAgIG9udG9sb2d5X2NoaWxkcmVuIDwtIGZpbHRlcl9tYmFfb250b2xvZ3lfY2hpbGRyZW4ob250LA0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwYXJlbnRfYWNyb255bSwNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaW5jbHVkZV9wYXJlbnQgPSBUUlVFKQ0KICAgICAgICAgICAgICAgICAgICAgICAgb250b2xvZ3lfY2hpbGRyZW4kcmVnaW9uX2dyb3VwIDwtIHJlZ2lvbl9ncm91cHMkcmVnaW9uX2dyb3VwW3hdDQogICAgICAgICAgICAgICAgICAgICAgICBvbnRvbG9neV9jaGlsZHJlbg0KICAgICAgICAgICAgICAgICAgICAgIH0pDQpgYGANCg0KXA0KXA0KYGBge3J9DQpyZWdpb25fdm94IDwtIGNjZl9hbm5vICU+JQ0KICBmaWx0ZXIoaWQgJWluJSByZWdpb25fb250JGlkKSAlPiUNCiAgbGVmdF9qb2luKHJlZ2lvbl9vbnQpDQpgYGANClwNClwNCg0KYGBge3J9DQpyZWdpb25fY2VudHJvaWRzIDwtIHJlZ2lvbl92b3ggJT4lDQogIGZpbHRlcih6IDwgMjI5KSAlPiUgIyBvbmUgaGVtaXNwaGVyZQ0KICBncm91cF9ieShyZWdpb25fZ3JvdXApICU+JQ0KICBzdW1tYXJpc2Uodm94ID0gbigpLA0KICAgICAgICAgICAgeCA9IG1lYW4oeCksDQogICAgICAgICAgICB5ID0gbWVhbih5KSwNCiAgICAgICAgICAgIHogPSBtZWFuKHopKQ0KYGBgDQoNCg==