1. Example Cartesian 데이터 생성
Pos <- c("Chr1_100_200","Chr1_200_300","Chr1_300_400","Chr1_400_500","Chr2_100_200","Chr2_200_300","Chr2_300_400","Chr2_400_500")
ExData_Cartesian <- data.frame(Pos,BarcodeA=rep(c(1,'.'),each=4),BarcodeB=rep(c('.',1),each=4),BarcodeC=rep(c(1,'.',1,1),each=1))
> ExData
Pos BarcodeA BarcodeB BarcodeC
1 Chr1_100_200 1 . 1
2 Chr1_200_300 1 . .
3 Chr1_300_400 1 . 1
4 Chr1_400_500 1 . 1
5 Chr2_100_200 . 1 1
6 Chr2_200_300 . 1 .
7 Chr2_300_400 . 1 1
8 Chr2_400_500 . 1 1
2. tidyverse 불러오기. Indexed 로 테이블 변경
library(tidyverse)
ExData_Indexed <- pivot_longer(ExData, - Pos)
ExData_Indexed <- pivot_longer(ExData, - Pos, names_to = "cellID", values_to = "accessability")
> pivot_longer(ExData, - Pos)
# A tibble: 24 × 3
Pos name value
<chr> <chr> <chr>
1 Chr1_100_200 BarcodeA 1
2 Chr1_100_200 BarcodeB .
3 Chr1_100_200 BarcodeC 1
4 Chr1_200_300 BarcodeA 1
5 Chr1_200_300 BarcodeB .
6 Chr1_200_300 BarcodeC .
7 Chr1_300_400 BarcodeA 1
8 Chr1_300_400 BarcodeB .
9 Chr1_300_400 BarcodeC 1
10 Chr1_400_500 BarcodeA 1
> ExData_Indexed
# A tibble: 24 × 3
Pos cellID accessability
<chr> <chr> <chr>
1 Chr1_100_200 BarcodeA 1
2 Chr1_100_200 BarcodeB .
3 Chr1_100_200 BarcodeC 1
4 Chr1_200_300 BarcodeA 1
5 Chr1_200_300 BarcodeB .
6 Chr1_200_300 BarcodeC .
7 Chr1_300_400 BarcodeA 1
8 Chr1_300_400 BarcodeB .
9 Chr1_300_400 BarcodeC 1
10 Chr1_400_500 BarcodeA 1
3. Cartesian 로 테이블 변경
ExData_Cartesian <- spread(ExData_Indexed,key = cellID,value =accessability)
> ExData_Cartesian
# A tibble: 8 × 4
Pos BarcodeA BarcodeB BarcodeC
<chr> <chr> <chr> <chr>
1 Chr1_100_200 1 . 1
2 Chr1_200_300 1 . .
3 Chr1_300_400 1 . 1
4 Chr1_400_500 1 . 1
5 Chr2_100_200 . 1 1
6 Chr2_200_300 . 1 .
7 Chr2_300_400 . 1 1
8 Chr2_400_500 . 1 1
4. Group별로 Indexed Data 만들기
ExData_Indexed$Celltype <- "GroundCells" ExData_Indexed[which(ExData_Indexed$cellID=="BarcodeA"),]$Celltype <- c("Meristems") ExData_Indexed$accessability <- as.numeric(ExData_Indexed$accessability) Group_Indexed <- ExData_Indexed %>% group_by(Celltype,Pos) %>% summarise_at(c("accessability"), sum, na.rm = TRUE)
> Group_Indexed
# A tibble: 16 × 3
# Groups: Celltype [2]
Celltype Pos accessability
<chr> <chr> <dbl>
1 GroundCells Chr1_100_200 1
2 GroundCells Chr1_200_300 0
3 GroundCells Chr1_300_400 1
4 GroundCells Chr1_400_500 1
5 GroundCells Chr2_100_200 2
6 GroundCells Chr2_200_300 1
7 GroundCells Chr2_300_400 2
8 GroundCells Chr2_400_500 2
9 Meristems Chr1_100_200 1
10 Meristems Chr1_200_300 1
11 Meristems Chr1_300_400 1
12 Meristems Chr1_400_500 1
13 Meristems Chr2_100_200 0
14 Meristems Chr2_200_300 0
15 Meristems Chr2_300_400 0
16 Meristems Chr2_400_500 0
5. Group별로 CartesianData 만들기
Group_Cartesian <- spread(Group_Indexed,key = Celltype,value =accessability)
> Group_Cartesian
# A tibble: 8 × 3
Pos GroundCells Meristems
<chr> <dbl> <dbl>
1 Chr1_100_200 1 1
2 Chr1_200_300 0 1
3 Chr1_300_400 1 1
4 Chr1_400_500 1 1
5 Chr2_100_200 2 0
6 Chr2_200_300 1 0
7 Chr2_300_400 2 0
8 Chr2_400_500 2 0