-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHIV_MDS_SeqeuenceAnalysis.R
170 lines (150 loc) · 8.14 KB
/
HIV_MDS_SeqeuenceAnalysis.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
library(tidyverse)
library(reshape2)
library(openxlsx)
library(RColorBrewer)
library(pheatmap)
setwd("/Users/morganmeissner/Desktop/GagUpdate/")
##Read in files of interest
#samplesheet: An excel file containing information from your samples. Required column headers:
#sampleName (sample ID), Virus, SampleType (Plasmid vs gDNA), ROI; other columns may be included based
#on your individual experiment
#files: All .vcf files of interest, generated by mpileup
samplesheet <- read.xlsx("SampleSheetInfo.xlsx")
files <- list.files(pattern = glob2rx("*.vcf"))
readfiles = function(name){
tmp <- read.delim(name, comment.char = "#",
col.names = c("Chr", "Pos", "ID", "Ref", "Alt",
"Qual", "Filter", "Info", "Format", "Counts"),
header = F, stringsAsFactors = F)
sampleName <- gsub(".vcf", "", name)
tmp$sampleName <- sampleName
tmp
}
##Build .vcf files into a dataframe
all <- map_dfr(files, readfiles)
all <- select(all, -Info)
all <- separate(all, Alt, c("A1","A2","A3"), sep = ",", remove = F)
all <- separate(all, Counts, c("Quals","Depth","AltDepth"), sep = ":", remove = T)
all <- separate(all, AltDepth, c("RefDepth",
"A1Depth", "A2Depth", "A3Depth"),
sep = ",", remove = F)
all <- select(all, -Quals)
all$A1Depth <- as.numeric(all$A1Depth)
all$A2Depth <- as.numeric(all$A2Depth)
all$A3Depth <- as.numeric(all$A3Depth)
all$RefDepth <- as.numeric(all$RefDepth)
all$Depth <- as.numeric(all$Depth)
all <- all %>% rowwise() %>%
mutate( totalAltDepth = sum(A1Depth, A2Depth, A3Depth, na.rm = T))
##Add sample details from samplesheet
all <- left_join(all, samplesheet, by = "sampleName")
##Remove positions where plasmid sequence was incorrect (all positions are alternate)
all <- mutate(all, PercAlt = (totalAltDepth/Depth)*100)
all <- filter(all, PercAlt < 100)
##Plot coverage across entire ROI (to ensure some areas aren't underrepresented)
#Files printed for each individual virus/ROI combination
dir.create("Coverage Plots")
combos <- unique(all[,c("Virus","ROI")])
for (i in 1:length(combos$Virus)){
u <- as.character(combos[i,1])
r <- as.character(combos[i,2])
filter(all, Virus==u & ROI==r) %>%
ggplot(data = ., aes(x = Pos, y = Depth, color = as.character(Sample)))+
geom_point() +
theme(legend.position = "bottom") +
facet_grid(Virus~ROI, scales = "free_x")
ggsave(filename=paste0("CoveragePlot_",u, "_", r,".pdf"), path="Coverage Plots")
}
##Remove positions with low depth coverage
all <- filter(all, Depth > 10000)
##Plot coverage across entire ROI (to ensure some areas aren't underrepresented)
#Files printed for each individual virus/ROI combination
for (i in 1:length(combos$Virus)){
u <- as.character(combos[i,1])
r <- as.character(combos[i,2])
filter(all, Virus==u & ROI==r) %>%
ggplot(data = ., aes(x = Pos, y = Depth, color = as.character(Sample)))+
geom_point() +
theme(legend.position = "bottom") +
facet_grid(Virus~ROI, scales = "free_x")
ggsave(filename=paste0("CoveragePlot_Filtered_",u, "_", r,".pdf"), path="Coverage Plots")
}
##Calculate the mean percent alternate at each position for each sample type and ROI
#For identifying hotspots
MeanPerAlt <- all %>% group_by(SampleType,ROI,Pos,Virus) %>%
summarise(meanPercAlt = mean(PercAlt, na.rm = T))
##Calculate the interquartile range of plasmid mutations for each ROI to determine exclusion value
calcIQR <- filter(MeanPerAlt, SampleType == "Plasmid") %>%
group_by(ROI,Virus) %>% summarise(
q1PercAlt = quantile(meanPercAlt, probs = 0.25, na.rm = T),
q3PerAlt = quantile(meanPercAlt, probs = 0.75, na.rm =T),
InterQRange = q3PerAlt - q1PercAlt,
ExclusionValue = InterQRange*1.5)
all <- left_join(all, calcIQR[, c("ROI","Virus","ExclusionValue")])
##Plot the exclusion value and the perc alt at each position in plasmid samples for each ROI/virus combo
dir.create("Percent Alternate Plots")
dir.create("Plasmid Mutation Plots")
for (i in 1:length(combos$Virus)){
u <- as.character(combos[i,1])
r <- as.character(combos[i,2])
temp = filter(calcIQR, Virus==u & ROI==r)
EV = as.numeric(temp$ExclusionValue)
filter(all, Virus==u & ROI==r & SampleType == "Plasmid") %>% ggplot(., aes(x=Pos, y = PercAlt, color = as.character(Sample))) +
geom_point() +
scale_y_log10() +
geom_hline(yintercept = EV) +
scale_color_brewer(palette = "Set1") +
theme(legend.position = "bottom", legend.text = element_text(size=6)) +
facet_grid(Virus~ROI, scales = "free_x")
ggsave(filename = paste0("Exclude_",u,"_",r,".pdf"),path="Plasmid Mutation Plots")
filter(all, Virus==u & ROI==r) %>% ggplot(., aes(x=Pos, y = PercAlt, color = SampleType)) +
geom_point() +
scale_y_log10() +
scale_color_brewer(palette = "Set1") +
theme(legend.position = "none") +
facet_grid(Virus~ROI, scales = "free_x")
ggsave(filename = paste0("PercentAlternateByPosition_",u,"_",r,".pdf"),path="Percent Alternate Plots")
}
##Identify and remove positions where ≥2 plasmid samples are above exclusion value AND the average number is ≥1 (hotspots)
PosToExclude <- filter(all, SampleType == "Plasmid") %>% group_by(Pos,ROI,Virus) %>%
summarise(numbadpos = sum(PercAlt > ExclusionValue))
BadPositions <- filter(PosToExclude, numbadpos>1)
BadPositions <- unite(BadPositions, LocID, Pos, ROI, Virus, sep="_", remove = F)
all <- unite(all, LocID, Pos, ROI, Virus, sep="_", remove = F)
excluded_positions <- filter(all, LocID %in% BadPositions$LocID)
Plasmid_HS_test <- filter(excluded_positions, SampleType == "Plasmid")
Plasmid_HS_test <- Plasmid_HS_test[,c("Pos", "ROI", "Virus","totalAltDepth","LocID")]
avg_mutbp <- Plasmid_HS_test %>% group_by(LocID) %>% summarise(AvgNumMut = mean(totalAltDepth))
Plasmid_HS_test <- left_join(Plasmid_HS_test, avg_mutbp, by =c("LocID"))
NewPosExclude <- filter(Plasmid_HS_test, AvgNumMut >= 1)
NumBadPos2 <- NewPosExclude %>% group_by(ROI,Virus) %>% summarise(num_excluded = length(unique(LocID)))
hot_spots <- filter(all, LocID %in% NewPosExclude$LocID)
all <- filter(all, !LocID %in% NewPosExclude$LocID)
##Calculate mutation rate by sample
dir.create("Mutation Analysis Files")
mutationRate <- all %>% group_by(Sample) %>%
summarise(totalBasesSeq = sum(Depth), totalAltBasesSeq = sum(totalAltDepth),
mutationRate = totalAltBasesSeq / totalBasesSeq)
mutationRate <- left_join(mutationRate, unique(all[,c("Sample","SampleType")]), by = "Sample")
mutationRate <- left_join(mutationRate,samplesheet)
write.xlsx(mutationRate, file = "Mutation Analysis Files/MutationRate.xlsx")
##Create a dataframe of all alternate bases and reference
a1 <- filter(all, A1 != "<*>")[,c("Pos","A1","A1Depth","Sample","ROI","Virus","ExperimentalCondition")]
a2 <- filter(all, A2 != "<*>")[,c("Pos","A2","A2Depth","Sample","ROI","Virus","ExperimentalCondition")]
a3 <- filter(all, A3 != "<*>")[,c("Pos","A3","A3Depth","Sample","ROI","Virus","ExperimentalCondition")]
MutSpectra <- data.frame(Pos = c(a1$Pos, a2$Pos, a3$Pos),
Alt = c(a1$A1, a2$A2,a3$A3),
AltDepth = c(a1$A1Depth, a2$A2Depth, a3$A3Depth),
Sample = c(a1$Sample, a2$Sample, a3$Sample),
ROI = c(a1$ROI, a2$ROI, a3$ROI),
Virus = c(a1$Virus, a2$Virus, a3$Virus),
ExperimentalCondition = c(a1$ExperimentalCondition, a2$ExperimentalCondition, a3$ExperimentalCondition))
MutSpectra <- left_join(MutSpectra , all[,c("Pos", "Ref", "RefDepth","Sample","ROI","Virus","ExperimentalCondition")],
by = c("Pos","Sample", "ROI", "Virus","ExperimentalCondition"))
MutSpectra <- MutSpectra[,c("Pos", "Ref","RefDepth","Alt","AltDepth","Sample","ROI", "Virus","ExperimentalCondition")]
MutSpectra$Mutation <- paste(MutSpectra$Ref, MutSpectra$Alt, sep =">")
MutSpectraCounts <- MutSpectra %>% group_by(Mutation,ROI,Virus,ExperimentalCondition) %>% summarize(TotalCount = sum(AltDepth))
write.xlsx(MutSpectraCounts,file="Mutation Analysis Files/MutationSpectraByROI.xlsx")
##Print additional files for analysis of plasmid hotspots and positioning of mutations
write.xlsx(hot_spots,file="Mutation Analysis Files/PlasmidHotspots.xlsx")
write.xlsx(MutSpectra,file="Mutation Analysis Files/MutationSpectraByPos.xlsx")