-
Notifications
You must be signed in to change notification settings - Fork 0
/
hepatocystis_dn_and_mca_clusters.Rmd
133 lines (106 loc) · 6.04 KB
/
hepatocystis_dn_and_mca_clusters.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
---
title: "Hepatocystis_dN_MCA_clusters"
author: "Eerik Aunin"
date: "Created in December 2019, edited in May 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Distributions of Hepatocystis dN values in Malaria Cell Atlas (MCA) clusters
This R Markdown file contains the R code for generating Figure S12 from the manuscript on the draft genome of Hepatocystis sp. ex Piliocolobus tephrosceles.
The input files contain:
1) codeml 3-way dN comparison between Hepatocystis sp. ex Piliocolobus tephrosceles, P. bergei ANKA and P. ovale curtisi
2) Malaria Cell Atlas cluster numbers assigned to P. berghei ANKA genes.
Here they will be loaded and merged into one table.
```{r loading data}
library("ggplot2")
crop_gene_name <- function(name_with_p1_tag) {
# Function for removing the -p1 tag from a gene name
cropped_name <- strsplit(name_with_p1_tag, "-p1", fixed=TRUE)[[1]][1]
return(cropped_name)
}
plus1_formatter <- function(x) {x + 1}
in_path <- "hep_pberghei_povale_3-way_codeml_dn_results.txt"
df <- read.csv(in_path, header=TRUE, sep="\t", stringsAsFactors=FALSE)
mca_clusters_df_path <- "mca_gene_clusters.dat"
mca_clusters_df <- read.csv(mca_clusters_df_path, header=TRUE, sep="\t", stringsAsFactors=FALSE)
colnames(mca_clusters_df) <- c("PBANKA_transcript", "mca_cluster")
mca_clusters_df$PBANKA_transcript <- sapply(mca_clusters_df$PBANKA_transcript, crop_gene_name)
df <- merge(df, mca_clusters_df, by="PBANKA_transcript", all=TRUE)
```
## Hepatocystis dN values per each Malaria Cell Atlas cluster
The distributions of dN values per cluster can be plotted as a boxplot.
```{r boxplot, echo=FALSE}
boxplot_df <- df
boxplot_df$mca_cluster <- as.factor(boxplot_df$mca_cluster)
boxplot_df <- boxplot_df[,c("Gene", "hep_dn", "mca_cluster")]
ind <- which(is.na(boxplot_df$hep_dn))
boxplot_df <- boxplot_df[-ind,]
ind <- which(is.na(boxplot_df$mca_cluster))
boxplot_df <- boxplot_df[-ind,]
ggplot(boxplot_df, aes(x=mca_cluster, y=hep_dn)) + geom_boxplot() + xlab("Malaria Cell Atlas cluster") + ylab("Hepatocystis dN") + ggtitle("Hepatocystis dN values per each Malaria Cell Atlas cluster") + theme(plot.title = element_text(hjust = 0.5))
```
## Kolmogorov-Smirnov test to compare dN distributions in clusters 15 and 16 with the distribution in other clusters
```{r Kolmogorov-Smirnov test, echo=FALSE}
#15 and 16 vs others
ind <- which(is.element(boxplot_df$mca_cluster,c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20)))
dist1 <- boxplot_df$hep_dn[ind]
ind2 <- which(is.element(boxplot_df$mca_cluster,c(15, 16)))
dist2 <- boxplot_df$hep_dn[ind2]
ks_result_cl15_16 <- ks.test(dist1, dist2)
#15 vs others
ind <- which(is.element(boxplot_df$mca_cluster,c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20)))
dist1 <- boxplot_df$hep_dn[ind]
ind2 <- which(is.element(boxplot_df$mca_cluster,c(15)))
dist2 <- boxplot_df$hep_dn[ind2]
ks_result_cl15 <- ks.test(dist1, dist2)
#16 vs others
ind <- which(is.element(boxplot_df$mca_cluster,c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20)))
dist1 <- boxplot_df$hep_dn[ind]
ind2 <- which(is.element(boxplot_df$mca_cluster,c(16)))
dist2 <- boxplot_df$hep_dn[ind2]
ks_result_cl16 <- ks.test(dist1, dist2)
corrected_p_values <- p.adjust(c(ks_result_cl15$p.value, ks_result_cl16$p.value, ks_result_cl15_16$p.value))
```
Printing out the p-values from Kolmogorov-Smirnov test
```{r, echo=FALSE}
print(paste0("Cluster 15 vs other clusters, KS test p-value: ", corrected_p_values[1]))
print(paste0("Cluster 16 vs other clusters, KS test p-value: ", corrected_p_values[2]))
print(paste0("Clusters 15 and 16 combined vs other clusters, KS test p-value: ", corrected_p_values[3]))
```
## Fisher’s exact test to find clusters where the number of genes with high dN differs from what is expected by chance
```{r}
df <- df[order(df$hep_dn, decreasing=TRUE),]
df2 <- df[!is.na(df$hep_dn),]
top_gene_limit <- as.integer(0.05 * nrow(df2))
top_genes <- df2$Gene[1:top_gene_limit]
df$top_gene_flag <- sapply(df$Gene, is.element, top_genes)
mca_clusters <- unique(df$mca_cluster)
mca_clusters <- mca_clusters[!is.na(mca_clusters)]
total_nr_of_genes <- nrow(df2)
counts_df <- data.frame()
for(mca_cl in mca_clusters) {
top_gene_count <- length(which(df$mca_cluster == mca_cl & df$top_gene_flag == TRUE))
other_gene_count <- length(which(df$mca_cluster == mca_cl & df$top_gene_flag == FALSE))
cluster_total_gene_count <- top_gene_count + other_gene_count
expected_top_gene_count <- cluster_total_gene_count * top_gene_limit / total_nr_of_genes
sam <- matrix(c(top_gene_count, cluster_total_gene_count, round(expected_top_gene_count, 0), cluster_total_gene_count),nrow=2,ncol=2)
pvalue <- fisher.test(sam)$p.value
cluster_vect <- c(mca_cl, top_gene_count, other_gene_count, expected_top_gene_count, pvalue)
counts_df <- rbind(counts_df, cluster_vect)
}
colnames(counts_df) <- c("mca_cluster", "top_gene_count", "other_gene_count", "expected_top_gene_count", "pvalue")
counts_df$adjusted_pvalue <- p.adjust(counts_df$pvalue)
ind <- which(counts_df$adjusted_pvalue < 0.05)
print(paste0("Cluster where the number of genes with high dN differs from what is expected by chance (Fisher’s exact test with Holm multiple hypothesis testing correction, p-value < 0.05): cluster ", counts_df$mca_cluster[ind], ", p-value: ", counts_df$adjusted_pvalue[ind]))
```
## Bar chart of the ratio of observed vs expected nr of genes with dN rank in top 5%
```{r}
counts_df$observed_expected_ratio <- counts_df$top_gene_count/counts_df$expected_top_gene_count
counts_df$mca_cluster <- as.factor(counts_df$mca_cluster)
counts_df$observed_expected_ratio_minus_1 <- counts_df$observed_expected_ratio - 1
ggplot(data=counts_df, aes(x=mca_cluster, y=observed_expected_ratio_minus_1)) + geom_bar(stat="identity") + xlab("Malaria Cell Atlas cluster") +
ylab("Ratio of observed vs expected nr of genes with dN rank in top 5%") + theme(plot.title = element_text(hjust = 0.5)) + scale_y_continuous(labels=plus1_formatter) + coord_flip() +
ggtitle("Genes with dN in the top 5%: observed versus expected ratios for MCA clusters")
```