-
Notifications
You must be signed in to change notification settings - Fork 0
/
klustercaller_to_vcf.R
298 lines (234 loc) · 9.34 KB
/
klustercaller_to_vcf.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
### FOR DEBUG ###
keyfile <- "klustercaller_keyfile_example.txt"
kc_file <- "klustercaller_file_example.txt"
verbose <- TRUE
out_file <- "klustercaller_to_vcf_example_output"
# Get command-line arguments from Bash
args <- commandArgs(trailingOnly = TRUE)
# Assign arguments to variables
# keyfile <- args[1]
# kc_file <- args[2]
# verbose <- ifelse(args[3]=="true", TRUE, FALSE)
# out_file <- args[4]
# Check for verbose
if(verbose==TRUE){
# Get message
print_string="### Initiating conversion of KlusterCaller data to VCF in R! ###"
# Measure string
n=nchar(print_string)
# Print message
print(paste(rep("#", n), collapse = ""))
print(print_string)
print(paste(rep("#", n), collapse = ""))
}
# Read in data
kasp_data <- read.table(kc_file, header=TRUE, sep="\t", na.strings=c("", "NA"), check.names=FALSE)
keyfile <- read.table(keyfile, header=TRUE, sep="\t", na.strings=c("", "NA"), check.names=FALSE)
# Check for verbose
if(verbose==TRUE){
# Display head of KlusterCaller data
print("### KlusterCaller head")
print(kasp_data[1:5,1:3])
# Display head of KlusterCaller keyfile
print("### Keyfile head")
print(keyfile[1:5,1:5])
}
# Pull list of markers in KlusterCaller data
markers_kasp_data <- colnames(kasp_data)[2:ncol(kasp_data)]
# Pull list of markers in keyfile
markers_keyfile <- keyfile[,"marker"]
# Pull all markers in the keyfile found in the kasp data
markers_keyfile <- markers_keyfile[markers_keyfile %in% markers_kasp_data]
# Check for duplicated markers in either file
if(any(duplicated(markers_kasp_data))){
# If there is an error, print the following
print("Error: There are duplicated markers in the ClusterCaller file. Check inputs and resubmit with unique marker names!")
stop("There are duplicated markers in the ClusterCaller file. Check inputs and resubmit with unique marker names!")
}else if(any(duplicated(markers_keyfile))){
# If there is an error, print the following
print("Error: There are duplicated markers in the key file. Check inputs and resubmit with unique marker names!")
stop("There are duplicated markers in the key file. Check inputs and resubmit with unique marker names!")
}
# make a pattern for illegal characters
illegal_pattern <- "[^XY:No Call]"
# Check if all markers are found in both files
if(length(markers_keyfile)==length(markers_kasp_data)){
# Make an empty vector for the vcf output
vcf<-c()
# Run for loop
for(i in unique(markers_kasp_data)){
# Print
if(verbose==TRUE){print(paste("### Formatting marker =", i))}
# Get colnames
temp1 <- c(colnames(kasp_data)[1], i)
# Pull data
temp1 <- as.data.frame(kasp_data[,colnames(kasp_data) %in% temp1])
# Check illegal character is found
if (any(grepl(illegal_pattern, temp1[,2]))) {
# If illegal characters are found, throw an error
print(paste("Error: Illegal character found in column ", i, " of ClusterCaller file!", sep = ""))
stop("Illegal character found in column ", i, " of ClusterCaller file!")
}
# Make into rownames
rownames(temp1) <- temp1[,1]
# Remove column
temp1 <- data.frame(temp1[,2],
row.names = rownames(temp1),
check.names = FALSE)
# Pull key
temp2 <- keyfile[keyfile[,"marker"]==i,]
# Replace things in temp1
temp1[,1] <- suppressWarnings(gsub("X", temp2[1,"X"], temp1[,1]))
temp1[,1] <- suppressWarnings(gsub("Y", temp2[1,"Y"], temp1[,1]))
temp1[,1] <- suppressWarnings(gsub("No Call",
paste(temp2[,"No Call"],
":",
temp2[,"No Call"],
sep = ""),
temp1[,1]))
temp1[,1] <- suppressWarnings(gsub(":", "/", temp1[,1]))
temp1[,1] <- suppressWarnings(gsub(temp2[1,"X"], 0, temp1[,1]))
temp1[,1] <- suppressWarnings(gsub(temp2[1,"Y"], 1, temp1[,1]))
temp1[,1] <- suppressWarnings(gsub("N", ".", temp1[,1]))
temp1.1 <- as.vector(temp1[,1])
names(temp1.1) <- rownames(temp1)
temp1 <- temp1.1
remove(temp1.1)
# Pull info
temp3 <- suppressWarnings(as.character(temp2[1,"chr"]))
temp4 <- suppressWarnings(as.numeric(temp2[1,"position"]))
temp5 <- suppressWarnings(as.character(temp2[1,"marker"]))
temp6 <- suppressWarnings(as.character(temp2[1,"X"]))
temp7 <- suppressWarnings(as.character(temp2[1,"Y"]))
# Make vcf line
temp3 <- data.frame("#CHROM" = temp3,
POS = temp4,
ID = temp5,
REF = ifelse(temp6=="N", ".", temp6),
ALT = ifelse(temp7=="N", ".", temp7),
QUAL = ".",
FILTER = "PASS",
INFO = ".",
FORMAT = "GT",
t(temp1),
check.names = FALSE)
rownames(temp3) <- NULL
# Rbind into vcf
vcf <- rbind(vcf, temp3)
# Remove
remove(temp1, temp2, temp3, temp4, temp5, temp6, temp7)
}
# Make header text
header <- c("##fileformat=VCFv4.2",
'##clustercaller_to_vcf=<ID=GenotypeTable,Version=1.0,Description="KASP assays converted to VCF format. Missing positions reported as sudo-positions starting at 1.">',
'##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">')
# Turn missing positions into a new position
vcf_mod <- c()
# Chromosome names
chrs <- unique(vcf[,"#CHROM"])
chrs <- chrs[order(chrs)]
# For loop
for (i in chrs){
# Pull markers
temp1 <- vcf[vcf[,"#CHROM"]==i & is.na(vcf[,"POS"])==FALSE,]
# Separate out markers with "." for those positions
temp2 <- vcf[vcf[,"#CHROM"]==i & is.na(vcf[,"POS"])==TRUE,]
# Check
if (nrow(temp2)==0){
# Place in the modified VCF
vcf_mod<-rbind(vcf_mod, temp1)
# Add row to header
header <- c(header, paste("##contig=<ID=",
i,
",length=",
max(temp1[,"POS"]),
">",
sep=""))
# Remove
remove(temp1, temp2)
}else if (nrow(temp1)>0 & nrow(temp2)>0){
# Pull markers with positions and markers without position
temp3 <- temp1[is.na(temp1[,"POS"])==FALSE,]
# Assign number for position
temp2[,"POS"] <- seq(1:nrow(temp2))
# Rbind
temp3 <- rbind(temp2, temp3)
# Report
vcf_mod<-rbind(vcf_mod, temp3)
# Add row to header
header <- c(header, paste("##contig=<ID=",
i,
",length=",
max(temp3[,"POS"]),
">",
sep=""))
# Remove
remove(temp1, temp2, temp3)
}else if (nrow(temp1)==0){
# Assign number for position
temp2[,"POS"] <- seq(1:nrow(temp2))
# Place in the modified VCF
vcf_mod<-rbind(vcf_mod, temp2)
# Add row to header
header <- c(header, paste("##contig=<ID=",
i,
",length=",
max(temp2[,"POS"]),
">",
sep=""))
# Remove
remove(temp1, temp2)
}else{
# Stop and exit with error
stop("Something is wrong with markers on CHR = ",i)
exit(status = 0)
}
}
# Replace
vcf <- vcf_mod
# Make numeric
vcf[,"POS"] <- as.numeric(vcf[,"POS"])
# Order the vcf
vcf <- vcf[order(vcf[,"#CHROM"], vcf[,"POS"]),]
# Add header
vcf <- rbind(colnames(vcf), vcf)
# Make matrix
vcf <- as.matrix(vcf)
# Get rid of column names
colnames(vcf) <- NULL
# Add metadata header
header_alt <- matrix(nrow = length(header), ncol = ncol(vcf))
header_alt[,1] <- as.character(header)
# Turn NA into nothing
header_alt[is.na(header_alt)] <- ""
# Bind header and body together
vcf <- rbind(header_alt, vcf)
# Write vcf
write.table(vcf,
paste(out_file, ".vcf", sep = ""),
row.names = FALSE,
col.names = FALSE,
quote = FALSE,
sep = "\t")
# Else if there is not a complete list of markers in both files
}else{
# Throw error
if(verbose==TRUE){
# Print error for read out
print("ERROR: Not all marker names are found in both files. Check case, presence, and spelling of markers listed below!")
print("################################################")
print("### Printing marker names in files for debug ###")
print("################################################")
print("Markers in KlusterCaller file yet not in key file:")
print("------------------------------")
string <- ifelse(length(markers_kasp_data[!markers_kasp_data %in% markers_keyfile])==0,
"No Missing Markers!",
markers_kasp_data[!markers_kasp_data %in% markers_keyfile])
print(string)
print("######################################")
}
# Exit with error
stop("Not all marker names are found in both files. Check case, presence, and spelling of marker names!")
}
# Displaying warnings
if(verbose==TRUE){warnings()}