-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.2.1_visualize_covmaps_stats.R
187 lines (133 loc) · 8.23 KB
/
1.2.1_visualize_covmaps_stats.R
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env Rscript
###########################################################################
# Project: Orania Phylogeny MT
# Script: visualize_covmaps_stats.R
# --- Action: Visualization of a "coverage map" for each supercontig, and summary statistics over all coverage maps
# ----------- A coverage map represents how many reads support each
# ----------- position over the supercontig.
# --- Input: Coverage map in form of a table, generated by script "coverage_maps.sh"
# --- Output: Summary statistics, coverage map visualization for each genes-individual combination
# Author: Maya Schroedl ([email protected])
###########################################################################
rm(list=ls())
# Libraries ---------------------------------------------------------------
if (!require('ggplot2')) install.packages('ggplot2'); library('ggplot2')
theme_set(theme_classic())
if (!require('rlist')) install.packages('rlist'); library('rlist')
if (!require('plyr')) install.packages('plyr'); library('plyr') #important to load plyr first to not overwrite dplyr
if (!require('dplyr')) install.packages('dplyr'); library('dplyr')
# Basic data --------------------------------------------------------------
wd=file.path(getwd(),"1_phylo_reconstruction")
#read namelist; = list of TAGs
namelist=as.character(read.table(file.path(wd,"namelist.txt"))$V1)
#read genelist; = list of gene names
genelist=as.character(read.table(file.path(wd,"genelist.txt"))$V1)
# Example -----------------------------------------------------------------
tag="TAG-29"
gene="E1007"
t = 2 #threshold for number of reads
# Get coverage maps -------------------------------------------------------
# This function reads the coverage maps which were produced by a part of the script "1.1.0_coverage_maps.sh".
# The function adds also a boolean according to the threshold.
#over_t==T means that this position has a coverage depth higher than the threshold.
# Each line corresponds to a base pair [bp] of a specific gene for a specific TAG.
coverage=function(tag){
file=file.path(wd,"1.1_coverage_maps",paste(tag,"_supercontigs_sorted.coverage",sep=""))#the coverage file corresponding to the TAG
if (file.exists(file) & file.info(file)$size != 0) #if file exists and is not empty
{
cover=read.table(file, sep="\t", header=F) #read coverage map
colnames(cover)=c(V1="gene", V2="bp", V3="depth") # renames the header
cover$tag=rep(tag, length(cover$gene))#add column with the tag name
cover$over_t=cover$depth>t #add column with whether position have a depth over threshold (TRUE) or not (FALSE)
return(cover)
}
}
cov.ex=coverage(tag)#coverage map for example
#### Add coverage maps of TAGs to one df ####
cover_all=lapply(namelist,coverage) %>% bind_rows() #ignore warnings
write.table(cover_all,file.path(wd,"1.1_coverage_maps","all_covmaps.txt"),row.names = F,quote = F)
# Stats -------------------------------------------------------------------
#---- Simple stat summary ----
cover_all=read.table(file.path(wd,"1.1_coverage_maps","all_covmaps.txt"),h=T)
# General stats
mean(cover_all$depth)
sd(cover_all$depth)
min(cover_all$depth)
max(cover_all$depth)
#density plot for all basepairs
plot(density(cover_all$depth[which(cover_all$depth<50)]))
abline(v=t,col="red")
#### proportion of genes having a base with a depth <= threshold
length(unique(cover_all$gene[which(cover_all$over_t==FALSE)]))/length(genelist)
#### proportion of contigs having a base with a depth <= threshold
cov.contig=cover_all %>%
group_by(gene,tag)%>% #for each gene/tag combination:
summarize(have.tF=(FALSE %in% over_t)) #have.tF: booleean - TRUE = gene/tag combi has at least one base pair whose depth is under the threshold (over_thr==FALSE)
#### percentage of bases having a depth <= threshold # how many bases did we get rid off
length(which(cover_all$over_t==FALSE))/length(cover_all$gene)
##### percentage [per gene] of bases having a depth <= threshold
cov_per_gene=cover_all %>%
group_by(gene)%>% #calculate for each gene
summarize(perc=length(which(over_t==FALSE))/length(over_t)*100) #perc: percentage of base pairs having a depth under threshold (over_t == FALSE)
hist(cov_per_gene$perc) #histogram of "bad base percentage" over genes
#gene with most bases <=t (over all TAGs)
worst_gene=cov_per_gene$gene[which(max(cov_per_gene$perc)==cov_per_gene$perc)]
worst_gene
# Position stats ----------------------------------------------------------
# This function determines how many positions have a depth under threshold in the flanking regions, and in the middle respectively.
pos.stat=function(gene,tag){
cover=cover_all[which(cover_all$tag==tag & cover_all$gene==gene),]
if (length(cover$gene)==0){return(NA)}#if this combination tag/gene does not exist - then function returns NA
len=length(cover$over_t) #total gene length
over_t_true=grep("TRUE",cover$over_t) #which positions have a depth over threshold
flan_l=over_t_true[1]-1
# flan_l: length of the left flanking region
# from 0 to position of the first time the depth is > threshold (over_t==TRUE), indicates the end of the flanking region 1
flan_r=length(cover$gene)-over_t_true[length(over_t_true)]
#flan_r: length of the right flanking region
# from position of the last time the depth is > threshold (over_t_true[length(over_t_true)]) to the last position of the gene length(cov.ex$gene)
flan.len=
flan_l+flan_r #flan.tot: how many positions have a depth<t in the flanking regions
#sum of left flanking region + right flanking region lengths
mid.len=
length(grep("FALSE",cover$over_t))-flan_l-flan_r #mid.len: how many positions have a depth<t in the middle regions
#length of how many postitions have a depth < threshold (length(grep("FALSE",cov.ex$over_t))), but the lengths of the flanking regions need to be substracted to only have the middle.
perc.flan=round(flan.len/len*100,3) #percentage of positions having a depth < threshold in the flanking regions
perc.mid=round(mid.len/len*100,3) #percentage of positions having a depth < threshold in the middle of the gene
perc.true=round(length(over_t_true)/len*100,3) #percentage of positions having a depth > threshold in the flanking regions
return(c(perc.flan,perc.mid,perc.true))}
pos.stat(gene,tag)
#Summary of the posititon stats
combi_list=expand.grid(genelist,namelist)#dataframe of combinations of gene/tag
colnames(combi_list)=c("gene","tag")
pos.stat_sum=plyr::mdply(combi_list,pos.stat) #for each gene/tag-combination, calculate pos.stat
colnames(pos.stat_sum)=c("gene","tag","perc.flan","perc.mid","perc.good")
#doc with stats for each gene/tag-combination
setwd(file.path(wd,"1.1_coverage_maps"))
write.table(pos.stat_sum,paste("supercontigs_covmap_pos_stats_",t,".txt",sep=""),row.names = F)
# Coverage Map visualization -----------------------------------------------------------
# This function plots read coverage (=depth) as a function of position on the gene
# for each gene and tag
covmap_plot=function(gene,tag){
cover=cover_all[which(cover_all$gene==gene & cover_all$tag==tag),]
if (length(cover$gene)==0){return(NA)}#if this combination tag/gene does not exist - then function returns NA
#plot coverage map (depth as a function of position [bp])
g=ggplot(cover,
aes(x=bp, y=depth)) +
geom_ribbon(aes(ymin=pmin(cover$depth,t), ymax=t),
fill="red", col="red", alpha=0.5) + #all values lower than threshold t are coloured in red
geom_ribbon(aes(ymin=t, ymax=pmax(cover$depth,t)),
fill="green", col="green", alpha=0.5) + #all values higher than threshold t are coloured in green
geom_line(aes(y=t),col="red")+ggtitle(paste(gene, tag, "t =", t)) + #abline with threshold t
ylab("depth (reads)")
return(g)}
#example of coverage map plot with worst gene
covmap_plot(worst_gene,cover_all$tag[which(cover_all$gene==worst_gene)[1]])
#plot all plots for all tags for one gene at the same time
#to have a better overall understanding of the read coverage for this specific gene
covmap_plot_alltags=function(gene){
taglist=unique(cover_all[which(cover_all$gene==gene),]$tag)#list of tags for this gene
plots_alltags=sapply(taglist,covmap_plot, gene=gene,simplify=F)#make a list of all the plots for this gene
return(plots_alltags)
}
covmap_plot_alltags(worst_gene)