Switch to unified view

a b/analysis/NeighborhoodAnalysis.Rmd
1
---
2
title: "Neighborhood analysis"
3
author: Tobias Roider
4
date: "Last compiled on `r format(Sys.time(), '%d %B, %Y, %X')`"
5
output: 
6
  
7
  rmdformats::readthedown:
8
    
9
editor_options: 
10
  chunk_output_type: console
11
---
12
13
```{r options, include=FALSE, warning = FALSE}
14
15
library(knitr)
16
opts_chunk$set(echo=TRUE, tidy=FALSE, include=TRUE, message=FALSE,
17
               dpi = 100, cache = FALSE, warning = FALSE)
18
opts_knit$set(root.dir = "../")
19
options(bitmapType = "cairo")
20
21
```
22
23
# Load packages, functions and data
24
```{r Load packages and functions}
25
26
library(Seurat)
27
library(tidyverse)
28
library(readxl)
29
30
source("R/ReadPackages.R")
31
source("R/Functions.R")
32
33
# Read CODEX data (only meta data) 
34
# Available at BioStudies database (https://www.ebi.ac.uk/biostudies/) under accession number S-BIAD565
35
codex_annotation <- data.table::fread("data/cells_annotation.csv") %>% tibble() %>% 
36
  filter(Merged_final!="na")
37
  
38
```
39
40
# Function
41
```{r function}
42
43
# Run nearest neighbor analysis
44
run_NNanalysis <- function(data=NULL, plan_session="sequential", n_workers=1, nn=25, regions=NULL, 
45
                           add.prop=TRUE){
46
  
47
  require(future)
48
  require(future.apply)
49
  require(FNN)
50
  
51
  if(plan_session=="sequential"){
52
    plan(strategy = plan_session)
53
  }
54
  
55
  if(plan_session=="multisession"){
56
    plan(strategy = plan_session, workers=n_workers)
57
  }
58
  
59
  options(future.globals.maxSize= 8000*1024^2)
60
  
61
  output <- future_lapply(regions, function(r){
62
    
63
    df <- data %>% 
64
      filter(unique_region==r) 
65
    
66
    df_nn <- get.knn(select(df,x,y), nn)
67
    
68
    df_nn <- df_nn$nn.index %>% 
69
      `rownames<-`(df$unique_cell_id) %>% 
70
      data.frame() %>% 
71
      rownames_to_column("unique_cell_id") %>% 
72
      pivot_longer(cols = 2:ncol(.)) %>% 
73
      left_join(., df %>% select(unique_cell_id_nn=unique_cell_id, Merged_final) %>% mutate(value=1:nrow(.)), by="value") %>% 
74
      select(-value) 
75
    
76
    if(add.prop==TRUE){
77
      mat <- df_nn %>% 
78
        add_prop(vars = c("unique_cell_id", "Merged_final"), group.vars = 1) %>% 
79
        pivot_wider(names_from = "Merged_final", values_from = "Prop", values_fill = 0) %>% 
80
        column_to_rownames("unique_cell_id")
81
      mat <- mat[names(which(rowSums(mat)>0)), ]
82
83
    } else {
84
      mat <- df_nn 
85
      }
86
87
    return(mat)
88
  })
89
  plan(strategy = "sequential")
90
  
91
  mat <- do.call(rbind, output)
92
  
93
  return(mat)
94
}
95
96
```
97
98
# Run Analysis
99
```{r run analysis}
100
101
# Read and process codex data (uses future package)
102
nn <- run_NNanalysis(data = codex_annotation, regions = unique(codex_annotation$unique_region), 
103
                     plan_session = "multisession", 
104
                     add.prop=TRUE,
105
                     n_workers = 10, 
106
                     nn = 25)
107
108
# Use k-means clustering to determine neighborhood classes
109
set.seed(1)
110
hclust <- kmeans(nn, centers = 10, iter.max = 50, nstart = 1)
111
nn_classes <- data.frame(Region=hclust$cluster)  %>% 
112
  mutate(Region=as.character(Region)) %>% 
113
  rownames_to_column("unique_cell_id")
114
115
# Use PCA on nearst neighbour matrix
116
pca_codex <- prcomp(nn, scale. = T, center = T)
117
118
# Remove objects
119
rm(list = setdiff(ls(), c("nn", "nn_classes", "pca_codex", "run_NNanalysis")))
120
121
```
122
123
# Save
124
```{r save}
125
126
save.image("output/Neighborhood_results.RData")
127
128
```
129
130
# Session Info
131
```{r session info}
132
133
sessionInfo()
134
135
```
136