-
Notifications
You must be signed in to change notification settings - Fork 0
/
7a_dm3_GenotypeGVCF.R
328 lines (294 loc) · 13.6 KB
/
7a_dm3_GenotypeGVCF.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env Rscript
# FILE DESCRIPTION: 7a_dm3_GenotypeGVCF -----------------------------------------
#
# DESCRIPTION :
#
# USAGE:
#
# OPTIONS: none
# REQUIREMENTS: data.table, lubridate, stringr
# BUGS: --
# NOTES: ---
# AUTHOR: Maria Litovchenko, maria.litovchenko@epfl.ch
# COMPANY: EPFL, Lausanne, Switzerland
# VERSION: 1
# CREATED:
# REVISION:
#setwd('~/Desktop/BitBucket/MitoSeq_RPJB_ML_Aug2017/')
setwd('~/Desktop/MitoSeq_RPJB_ML_Aug2017/')
source('17_functions.R')
# FUNCTIONS -------------------------------------------------------------------
#' deNovoVariants
#' Get do novo variants
#' @param testGR GRanges object with variants of the test sample
#' @param testGeno genotype table with variants of the test sample
#' @param refGR GRanges object with variants of the reference samples
#' @return
deNovoVariants <- function(testGR, testGeno, refGR) {
# detection of de novo variants - GRanges
testGRdeNovo <- testGR[testGR %outside% refGR]
if (length(testGRdeNovo) != 0) {
# detection of de novo variants - genotype table
testGenoDeNovo <- testGeno[names(testGRdeNovo), ]
# heteroplasmy of de-novo variants
testHeteroplDeNovo <- ifelse(testGenoDeNovo != '0/0' &
testGenoDeNovo != '1/1', T, F)
# structural type of de novo variants
testStrTypeDeNovo <- getVariantStructType(testGRdeNovo)
# read support of de-novo variants
testReadSuppDeNovo <- deplAD[names(testGRdeNovo)]
} else {
testGRdeNovo <- GRanges()
testHeteroplDeNovo <- c()
testStrTypeDeNovo <- c()
testReadSuppDeNovo <- data.table(DGRP = character(), Variant = character(),
ReadCount = integer(),
ReadCountPerc = numeric())
}
result <- list(testGRdeNovo, testHeteroplDeNovo, testStrTypeDeNovo,
testReadSuppDeNovo)
names(result) <- c('deNovoGRanges', 'deNovoHeteroplasmy',
'deNovoStructType', 'deNovoReadSupport')
result
}
#' compareToOneDGRP
#' Compare (genotype) one deplancke line to one dgrp line
#' @param oneDGRP one DGRP line (reference)
#' @param oneDepl one deplancke line
#' @return
compareToOneDGRP <- function(oneDGRP, oneDepl) {
oneDGRP <- gsub('\\.', NA, oneDGRP)
both <- cbind(as.character(oneDepl), as.character(oneDGRP))
rownames(both) <- names(oneDepl)
both <- both[complete.cases(both), ]
if (is.vector(both)) {
both <- data.frame(t(both), stringsAsFactors = F)
result <- list(sum(both[1] == both[2]),
data.frame(t(both[both[1] != both[2]])))
} else {
both <- data.frame(both, stringsAsFactors = F)
result <- list(sum(both[, 1] == both[, 2]) / nrow(both),
both[both[, 1] != both[, 2], ])
}
names(result) <- c('PercentMatch', 'Notmatching')
result
}
# INPUTS ----------------------------------------------------------------------
args <- commandArgs(trailingOnly = T)
# path to ALL VCFs
allVCFdir <- 'results/genotyping/vcfForGenotyping'
#allVCFdir <- args[1]
#sampleName <- args[2]
# path to all deplancke vcfs
allDepl <- list.files(allVCFdir, "_GT_in_Depl.vcf.gz", T, T)
names(allDepl) <- sapply(allDepl,
function(x) gsub("_GT_in_Depl.vcf.gz", "",
gsub(paste0(allVCFdir, '/'), '', x)))
#allDepl <- readRDS('allDepl.Rds')
# path to all DGRP vcfs
allDgrp <- list.files(allVCFdir, "_GT_in_DGRP2.vcf.gz", T, T)
names(allDgrp) <- sapply(allDgrp,
function(x) gsub("_GT_in_DGRP2.vcf.gz", "",
gsub(paste0(allVCFdir, '/'), '', x)))
# allDgrp <- readRDS('allDgrp.Rds')
# autosomes names
autosomes <- c("chr2L", "chr2LHet", "chr2R", "chr2RHet", "chr3L", "chr3LHet",
"chr3R", "chr3RHet", "chr4", "chrX", "chrXHet", "chrYHet")
# structural type order
strTypeOrder <- c('SNP', 'DEL', 'INS', 'MNP', 'SUBS')
# OUTPUTS ---------------------------------------------------------------------
genotypingOvrView <- c()
deNovoOvrView <- c()
heteroplOvrView <- c()
allelesOvrView <- c()
# PERFORM GENOTYPING ----------------------------------------------------------
# it's modified slightly to run on vital-it
#for (sampleName in names(allDgrp)) {
message(paste('Working with', sampleName, Sys.time()))
# read-in vcf of dgrps genotyped on the same variants as deplancke line
dgrpVcf <- readVcf(allDgrp[sampleName])
dgrpGeno <- geno(dgrpVcf)$GT
dgrpGR <- rowRanges(dgrpVcf)
# READING IN
# read-in one deplancke dgrp line genotyping
deplVcf <- readVcf(allDepl[sampleName])
deplGR <- rowRanges(deplVcf) # GRanges
deplGR <- deplGR[deplGR$FILTER == 'PASS'] # filter out bad quality
deplGR <- deplGR[seqnames(deplGR) %in% autosomes] # restrict to autosomes
deplGeno <- geno(deplVcf)$GT # genotype table
deplGeno <- as.data.frame(deplGeno[names(deplGR), ])
colnames(deplGeno) <- sampleName
# get read support of alternative allele
deplAD <- as.data.table(getReadSupportOfAlt(deplGeno, geno(deplVcf)$AD))
deplAD <- deplAD[deplAD$ReadCount != 0]
setkey(deplAD, Variant) # read support
deplGR <- deplGR[deplAD$Variant]
deplGeno <- as.data.frame(deplGeno[names(deplGR), ])
colnames(deplGeno) <- sampleName
rownames(deplGeno) <- names(deplGR)
message(paste('\t Finished reading', Sys.time()))
# DE NOVO VARIANTS
deNovoOneLine <- deNovoVariants(deplGR, deplGeno, dgrpGR)
toAdd <- c(sampleName, length(deplGR), length(deNovoOneLine$deNovoGRanges),
sum(deNovoOneLine$deNovoHeteroplasmy),
table(deNovoOneLine$deNovoStructType)[strTypeOrder],
summary(deNovoOneLine$deNovoReadSupport$ReadCount))
deNovoOvrView <- rbind(deNovoOvrView, toAdd)
message(paste('\t Finished de-novo variants', Sys.time()))
# HETEROPLASMIC NOT DE NOVO
deplGRhetero <- deplGR[deplGR %within% dgrpGR]
deplGenoHetero <- deplGeno[names(deplGRhetero), ]
names(deplGenoHetero) <- names(deplGRhetero)
deplGenoHetero <- deplGenoHetero[deplGenoHetero != '0/0' &
deplGenoHetero != '1/1']
deplGRhetero <- deplGRhetero[names(deplGenoHetero)]
if (length(deplGRhetero) != 0) {
# structural type of heteroplasmic variants
deplStrTypeHetero <- getVariantStructType(deplGRhetero)
# read support of heteroplasmic variants
deplReadSuppHetero <- deplAD[names(deplGRhetero)]
} else {
deplStrTypeHetero <- c()
deplReadSuppHetero <- data.frame(DGRP = character(), Variant = character(),
ReadCount = numeric(),
ReadCountPerc = numeric())
}
toAdd <- c(sampleName, length(deplGR), length(deplGRhetero),
table(deplStrTypeHetero)[strTypeOrder],
summary(deplReadSuppHetero$ReadCount))
heteroplOvrView <- rbind(heteroplOvrView, toAdd)
message(paste('\t Finished heteroplasmy variants', Sys.time()))
# RESTRICT DEPLANCKE TO THE VALID FOR GENOTYPING SITES
deplGRovrl <- deplGR[deplGR %within% dgrpGR]
deplGenoOvrl <- deplGeno[names(deplGRovrl), ]
names(deplGenoOvrl) <- names(deplGRovrl)
deplGenoOvrl <- deplGenoOvrl[deplGenoOvrl == '0/0' | deplGenoOvrl == '1/1']
deplGRovrl <- deplGRovrl[names(deplGenoOvrl)]
# RESTRICT REF DGRPs TO THE VALID FOR GENOTYPING SITES
dgrpGRovrl <- dgrpGR[dgrpGR %within% deplGRovrl]
dgrpGenoOvrl <- dgrpGeno[names(dgrpGRovrl), ]
# CHECK ON ALLELES MATCH
# check alt allele, sum is necesary in case there are MNPs, so if 1 allele
# overlaps, it's already enough
alleles <- sapply(1:length(deplGRovrl),
function(x) sum(deplGRovrl$ALT[[x]] == dgrpGRovrl$ALT[[x]]))
if (length(deplGRovrl[alleles == 0]) != 0) {
deplStrTypeAlleles <- getVariantStructType(deplGRovrl[alleles == 0])
deplReadSuppAlleles <- deplAD[names(deplGRovrl[alleles == 0])]
} else {
deplStrTypeAlleles <- c()
deplReadSuppAlleles <- data.frame(DGRP = character(), Variant = character(),
ReadCount = numeric(),
ReadCountPerc = numeric())
}
toAdd <- c(sampleName, length(deplGR), sum(alleles == 0),
table(deplStrTypeAlleles)[strTypeOrder],
summary(deplReadSuppAlleles$ReadCount))
allelesOvrView <- rbind(allelesOvrView, toAdd)
message(paste('\t Finished allele match', Sys.time()))
# RESTRICT DEPLANCKE AND DGRPs TO THE CLEAN VARIANTS
deplGRovrlAlleles <- deplGRovrl[alleles >= 1, ]
deplGenoOvrlAlleles <- deplGenoOvrl[names(deplGRovrlAlleles)]
deplGenoOvrlAlleles <- as.character(deplGenoOvrlAlleles)
names(deplGenoOvrlAlleles) <- names(deplGRovrlAlleles)
dgrpGRovrlAlleles <- dgrpGRovrl[alleles >= 1, ]
dgrpGenoOvrlAlleles <- dgrpGenoOvrl[names(dgrpGRovrlAlleles), ]
# GENOTYPE
GTs <- apply(dgrpGenoOvrlAlleles, 2, compareToOneDGRP, deplGenoOvrlAlleles)
GTsVals <- sapply(GTs, function(x) x$PercentMatch)
GTsVals <- GTsVals[order(-GTsVals)]
firstMatch <- names(GTsVals)[1]
secondMatch <- names(GTsVals)[2]
thirdMatch <- names(GTsVals)[3]
# check, where do variants come from if they don't overlap with 1st match
nonOvrlVars <- rownames(GTs[[firstMatch]]$Notmatching)
deplGRnonOvrlVars <- deplGRovrlAlleles[nonOvrlVars]
deplGenoNonOvrlVars <- deplGenoOvrlAlleles[names(deplGRnonOvrlVars)]
dgrpGRnonOvrlVars <- dgrpGRovrlAlleles[dgrpGRovrlAlleles %within% deplGRnonOvrlVars]
dgrpGenoNonOvrlVars <- dgrpGenoOvrlAlleles[names(dgrpGRnonOvrlVars), ]
if (is.vector(dgrpGenoNonOvrlVars)) {
GTsNotMatch <- lapply(dgrpGenoNonOvrlVars, compareToOneDGRP,
deplGenoNonOvrlVars)
} else {
GTsNotMatch <- apply(dgrpGenoNonOvrlVars, 2, compareToOneDGRP,
deplGenoNonOvrlVars)
}
GTsNotMatchVals <- sapply(GTsNotMatch, function(x) x$PercentMatch)
GTsNotMatchVals <- GTsNotMatchVals[order(-GTsNotMatchVals)]
firstMatchNotMatch <- names(GTsNotMatchVals)[1]
toAdd <- c(sampleName, length(deplGR), length(deNovoOneLine$deNovoGRanges),
length(deplGRhetero), sum(alleles == 0),
length(deplGR) - length(deNovoOneLine$deNovoGRanges) -
length(deplGRhetero) - sum(alleles == 0),
summary(deplAD[names(deplGRovrlAlleles)]$ReadCount),
firstMatch, secondMatch, thirdMatch, GTsVals[1], GTsVals[2],
GTsVals[3], length(nonOvrlVars),
GTsNotMatchVals[1], firstMatchNotMatch)
genotypingOvrView <- rbind(genotypingOvrView, toAdd)
message(paste('\t Finished fully', Sys.time()))
#}
colnames(deNovoOvrView)[1:9] <- c('Name', 'NumberOfGTsites', 'NumberOfDeNovo',
'NumberOfHeteropl', strTypeOrder)
colnames(heteroplOvrView)[1:8] <- c('Name', 'NumberOfGTsites',
'NumberOfHeteropl', strTypeOrder)
colnames(genotypingOvrView)[1:6] <- c('Name', 'NumberOfGTsites',
'NumberOfDeNovo', 'NumberOfHeteropl',
'NumberOfAlleleNotMatch', 'CleanSites')
colnames(genotypingOvrView)[13:21] <- c('firstMatch', 'secondMatch',
'thirdMatch', 'firstMatchPerc',
'secondMatchPerc', 'thirdMatchPerc',
'NumbNotOvrlVars',
'PercOfNotOvrlVarsOvrlWithOtherLine',
'OtherLine')
saveRDS(deNovoOvrView, paste0('deNovoOvrView_', sampleName, '.Rds'))
saveRDS(heteroplOvrView, paste0('heteroplOvrView_', sampleName, '.Rds'))
saveRDS(genotypingOvrView, paste0('genotypingOvrView_', sampleName, '.Rds'))
message('Done!')
# BASH CODE TO RUN ON VITAL-IT ------------------------------------------------
#!/bin/bash
#BSUB -L /bin/bash
#BSUB -e GENO.%I.err
#BSUB -o GENO.%I.out
#BSUB -J GENO[1-241]
#BSUB -M 20000000
#BSUB -R rusage[mem=20000]
#BSUB -n 1
#BSUB -u maria.litovchenko@epfl.ch
export PATH=/software/bin:$PATH;
module use /software/module/;
module add R/3.3.2;
vcfDIR=/scratch/el/monthly/mlitovch/MitoSeq_RPJB_ML_Aug2017/vcfForGenotyping
samp=($(ls $vcfDIR | grep _Depl_DGRP2.vcf.gz | sed 's/_Depl_DGRP2.vcf.gz//g'))
zeroArr=( zero )
samples=("${zeroArr[@]}" "${samp[@]}")
sample=${samples[${LSB_JOBINDEX}]};
echo $sample
# run R script
Rscript --vanilla 30_genotyping_v2.R $vcfDIR $sample
exit 0;
# MERGE ALL RDS FROM VITAL-IT -------------------------------------------------
genotypingRds <- 'results/genotyping/genotypingRDS/'
deNovoOvrView <- lapply(list.files(genotypingRds, 'deNovoOvrView', T, T),
readRDS)
deNovoOvrView <- as.data.table(do.call(rbind, deNovoOvrView))
setkey(deNovoOvrView, Name)
deNovoOvrView <- deNovoOvrView[names(allDepl)]
saveRDS(deNovoOvrView, 'deNovoOvrView_genotyping_v2.Rds')
write.table(deNovoOvrView, 'deNovoOvrView.csv', quote = F, sep = '\t',
col.names = T, row.names = F)
heteroplOvrView <- lapply(list.files(genotypingRds, 'heteroplOvrView',
T, T), readRDS)
heteroplOvrView <- as.data.table(do.call(rbind, heteroplOvrView))
setkey(heteroplOvrView, Name)
heteroplOvrView <- heteroplOvrView[names(allDepl)]
saveRDS(heteroplOvrView, 'heteroplOvrView_genotyping_v2.Rds')
write.table(heteroplOvrView, 'heteroplOvrView.csv', quote = F, sep = '\t',
col.names = T, row.names = F)
genotypingOvrView <- lapply(list.files(genotypingRds, 'genotypingOvrView',
T, T), readRDS)
genotypingOvrView <- as.data.table(do.call(rbind, genotypingOvrView))
setkey(genotypingOvrView, Name)
genotypingOvrView <- genotypingOvrView[names(allDepl)]
saveRDS(genotypingOvrView, 'ovrView_genotyping_v2.Rds')
write.table(genotypingOvrView, 'genotypingOvrView.csv', quote = F, sep = '\t',
col.names = T, row.names = F)