forked from snow942/mtDNA_Comparative
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtDNAFilter.R
154 lines (127 loc) · 5.96 KB
/
mtDNAFilter.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
#Ryan Snow
#mtDNA_Comparative_Analysis
####
library (dplyr)
library (devtools)
library (magrittr)
library (GenomicRanges)
library (knitr)
library (ggplot2)
library (tidyr)
library (rmarkdown)
library (tibble)
#Multiple sequence alignment completed via Nucmer, using the MUMmer package/software
#Read file(s)
Sfa_ABas.snps <- read.table (file="/Users/snow4/Desktop/Mummerv4/Sfa_ABas_Query.fasta.m.snps")
Sfa_CBas.snps <- read.table (file="/Users/snow4/Desktop/Mummerv4/Sfa_CBas_Query.fasta.m.snps")
Sfa_Bas.snps <- read.table (file="/Users/snow4/Desktop/Mummerv4/Sfa_Bas_Query.fasta.m.snps")
#Change column names to better represent data/incorporate metadata
#https://mummer.sourceforge.net/manual/#snps review output format for more info
colnames (Sfa_ABas.snps) <- c ("P1",
"SUB_Ref",
"SUB_Query",
"P2",
"BUFF",
"DIST",
"R",
"Q",
"LEN_R",
"LEN_Q",
"FRM1",
"FRM2",
"mtDNA_Rep",
"SampleName")
colnames (Sfa_CBas.snps) <- c ("P1",
"SUB_Ref",
"SUB_Query",
"P2",
"BUFF",
"DIST",
"R",
"Q",
"LEN_R",
"LEN_Q",
"FRM1",
"FRM2",
"mtDNA_Rep",
"SampleName")
colnames (Sfa_Bas.snps) <- c ("P1",
"SUB_Ref",
"SUB_Query",
"P2",
"BUFF",
"DIST",
"R",
"Q",
"LEN_R",
"LEN_Q",
"FRM1",
"FRM2",
"mtDNA_Rep",
"SampleName")
FilterVariants <- function (SnpsInput, FilterPercent) {
#Used to filter variants in .snps files based on read depth
#SnpsInput <- Sfa_ABas.snps
#Determines number of total samples to implement a filter based on 90% of sample population
SampleCounts.df <- data.frame (table (SnpsInput$SampleName))
TotalSamples <- nrow (SampleCounts.df)
#Filter
Filter <- TotalSamples*(FilterPercent/100)
#Counts the number of times a position appears in the dataset
Positions.df <- data.frame (Var1=SnpsInput$P1, Ref=SnpsInput$SUB_Ref, Query=SnpsInput$SUB_Query)
Counts.df <- data.frame (table (SnpsInput$P1))
Positions.df$Var1 <- as.factor (Positions.df$Var1)
To_Filter <- left_join (Counts.df, Positions.df, by=c("Var1"), multiple="first")
colnames(To_Filter)[colnames(To_Filter) == "Var1"] ="POS"
#Filters To_Filter to show only variant positions in 90% of sample population
To_Filter.df <- data.frame (To_Filter)
Output <- To_Filter.df %>% filter (Freq >= Filter)
#Incorporates percentage of population with SNP per posiiton
Output <- data.frame (Output, Percent = (as.integer(Output$Freq)/TotalSamples)*100)
return (Output)
}
#Calling high confidence variants in Sfa_ABas and Sfa_CBas files
ABas <- FilterVariants (Sfa_ABas.snps, 0)
CBas <- FilterVariants (Sfa_CBas.snps, 0)
#Compares filtered results of ABas and CBas populations
AC.compare <- full_join(ABas, CBas, by = "POS")
colnames (AC.compare) <- c ("Position",
"ABas.Presence", "ABas.Ref", "ABas.Qry", "ABas.Percent",
"CBas.Presence", "CBas.Ref", "CBas.Qry", "CBas.Percent")
write.csv (AC.compare, file = "/Users/snow4/Desktop/Mummerv4/AC.compare.csv")
#Compares ABas and CBas df to identify SNP positions unique to the population
Unique.ABas <- as_tibble (setdiff (ABas$POS, CBas$POS))
Unique.CBas <- as_tibble (setdiff (CBas$POS, ABas$POS))
####
library ("vcfR")
ABas.vcf <- read.vcfR ("/Users/snow4/Desktop/Mummerv4/Sfa_ABas_Query.fasta.m.vcf")
CBas.vcf <- read.vcfR ("/Users/snow4/Desktop/Mummerv4/Sfa_CBas_Query.fasta.m.vcf")
ReadDepth <- function (vcf, Filtered.df) {
#Incorporate freq as QUAL scores for filtering of vcf
vcf.df <- vcfR2tidy (vcf)
Filtered.df$POS <- as.integer( as.character(Filtered.df$POS))
#Changes class of df values to integer, where available
merge.vcf <- full_join (vcf.df$fix, Filtered.df, by= "POS")
#Merges the data from the vcf file and dataframe
#Uses "POS" column as like values
merge.vcf <- merge.vcf %>% relocate (Freq, .after = "QUAL")
#Moves Freq column position to after QUAL
merge.vcf <- (merge.vcf[,!names(merge.vcf) %in%
c ("QUAL", "Ref", "Query", "ChromKey")])
#Removes column with no signif value to exp
colnames(merge.vcf)[colnames(merge.vcf) == "Freq"] ="QUAL"
#Rename Freq column to QUAL
merge.vcf$QUAL[is.na(merge.vcf$QUAL)] <- 1
#All NAs in QUAL are changes to 0s
INFO = c (NA)
#Appends INFO column that was lost due to unknown reasons
merge.vcf <- cbind(merge.vcf, INFO)
#Replaces old QUAL scores with new ones by replacing "fix" matrix in vcf
vcf@fix <- as.matrix(merge.vcf)
return (vcf)
}
ABas.DP.vcf <- ReadDepth (ABas.vcf, ABas)
CBas.DP.vcf <- ReadDepth (CBas.vcf, CBas)
#Files need to be written as zipped files
write.vcf (ABas.DP.vcf, file = "/Users/snow4/Desktop/Mummerv4/ABas.m.Filter.vcf.gz", mask = FALSE, APPEND = FALSE)
write.vcf (CBas.DP.vcf, file = "/Users/snow4/Desktop/Mummerv4/CBas.m.Filter.vcf.gz", mask = FALSE, APPEND = FALSE)