/
applyQC.R
executable file
·313 lines (305 loc) · 14 KB
/
applyQC.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
#' Create plink dataset with individuals and markers passing quality control
#'
#' Individuals that fail per-individual QC and markers that fail
#' per-marker QC are removed from indir/name.bim/.bed/.fam and a new, dataset
#' with the remaining individuals and markers is created as
#' qcdir/name.clean.bim/.bed/.fam.
#' @param indir [character] /path/to/directory containing the basic PLINK data
#' files name.bim, name.bed, name.fam files.
#' @param qcdir [character] /path/to/directory where results will be written to.
#' If \code{\link{perIndividualQC}} was conducted, this directory should be the
#' same as qcdir specified in \code{\link{perIndividualQC}}, i.e. it contains
#' name.fail.IDs with IIDs of individuals that failed QC. User needs writing
#' permission to qcdir. Per default, qcdir=indir.
#' @param name [character] Prefix of PLINK files, i.e. name.bed, name.bim,
#' name.fam.
#' @param filterRelated [logical] Set to exclude samples that failed relatedness
#' check (via \code{\link{check_relatedness}} or \code{\link{perIndividualQC}}).
#' Requires file qcdir/name.fail-IBD.IDs (automatically created by
#' \code{\link{perIndividualQC}} if do.evaluate_check_relatedness set to TRUE).
#' @param filterAncestry [logical] Set to exclude samples that failed ancestry
#' check (via \code{\link{check_ancestry}} or \code{\link{perIndividualQC}}).
#' Requires file qcdir/name.fail-ancestry.IDs (automatically created by
#' \code{\link{perIndividualQC}} if do.check_ancestry set to TRUE).
#' @param filterHeterozygosity [logical] Set to exclude samples that failed
#' check for outlying heterozygosity rates (via
#' \code{\link{check_het_and_miss}} or
#' \code{\link{perIndividualQC}}). Requires file qcdir/name.fail-het.IDs
#' (automatically created by \code{\link{perIndividualQC}} if
#' do.evaluate_check_het_and_miss set to TRUE).
#' @param filterSampleMissingness [logical] Set to exclude samples that failed
#' check for excessive missing genotype rates (via
#' \code{\link{check_het_and_miss}} or
#' \code{\link{perIndividualQC}}). Requires file qcdir/name.fail-imiss.IDs
#' (automatically created by \code{\link{perIndividualQC}} if
#' do.evaluate_check_het_and_miss set to TRUE).
#' @param filterSex [logical] Set to exclude samples that failed the sex
#' check (via \code{\link{check_sex}} or \code{\link{perIndividualQC}}).
#' Requires file qcdir/name.fail-sexcheck.IDs (automatically created by
#' \code{\link{perIndividualQC}} if do.evaluate_check_sex set to TRUE).
#' @param filterHWE [logical] Set to exclude markers that fail HWE exact test
#' (via \code{\link{check_hwe}} or \code{\link{perMarkerQC}}). Requires hweTh to
#' be set.
#' @param filterMAF [logical] Set to exclude markers that fail minor allele
#' frequency or minor allele count threshold (via \code{\link{check_maf}} or
#' \code{\link{perMarkerQC}}). Requires mafTh or macTh to be set.
#' @param filterSNPMissingness [logical] Set to exclude markers that have
#' excessive missing rates across samples (via
#' \code{\link{check_snp_missingness}} or \code{\link{perMarkerQC}}). Requires
#' lmissTh to be set.
#' @inheritParams checkPlink
#' @inheritParams check_maf
#' @inheritParams check_hwe
#' @inheritParams check_snp_missingness
#' @param verbose [logical] If TRUE, progress info is printed to standard out.
#' @param showPlinkOutput [logical] If TRUE, plink log and error messages are
#' printed to standard out.
#' @return names [list] with i) passIDs, containing a [data.frame] with family
#' [FID] and individual [IID] IDs of samples that pass the QC, ii) failIDs,
#' containing a [data.frame] with family [FID] and individual [IID] IDs of
#' samples that fail the QC.
#' @export
#' @examples
#' package.dir <- find.package('plinkQC')
#' indir <- file.path(package.dir, 'extdata')
#' qcdir <- tempdir()
#' name <- "data"
#' path2plink <- '/path/to/plink'
#' # the following code is not run on package build, as the path2plink on the
#' # user system is not known.
#' \dontrun{
#' # Run individual QC checks
#' fail_individuals <- perIndividualQC(indir=indir, qcdir=qcdir, name=name,
#' refSamplesFile=paste(qcdir, "/HapMap_ID2Pop.txt",sep=""),
#' refColorsFile=paste(qcdir, "/HapMap_PopColors.txt", sep=""),
#' prefixMergedDataset="data.HapMapIII", interactive=FALSE, verbose=FALSE,
#' path2plink=path2plink)
#'
#' # Run marker QC checks
#' fail_markers <- perMarkerQC(indir=indir, qcdir=qcdir, name=name,
#' path2plink=path2plink)
#'
#' # Create new dataset of indiviudals and markers passing QC
#' ids_all <- cleanData(indir=indir, qcdir=qcdir, name=name, macTh=15,
#' verbose=TRUE, path2plink=path2plink, filterAncestry=FALSE,
#' filterRelated=TRUE)
#' }
cleanData <- function(indir, name, qcdir=indir,
filterSex=TRUE, filterHeterozygosity=TRUE,
filterSampleMissingness=TRUE,
filterAncestry=TRUE, filterRelated=TRUE,
filterSNPMissingness=TRUE, lmissTh=0.01,
filterHWE=TRUE, hweTh=1e-5,
filterMAF=TRUE, macTh=20, mafTh=NULL,
path2plink=NULL, verbose=FALSE,
showPlinkOutput=TRUE) {
sampleFilter <- c(filterAncestry, filterRelated, filterSex,
filterHeterozygosity, filterSampleMissingness)
markerFilter <- c(filterHWE, filterMAF, filterSNPMissingness)
prefix <- makepath(indir, name)
out <- makepath(qcdir, name)
if (!any(c(sampleFilter, markerFilter))) {
stop("No per-sample and per-marker filters chosen")
}
if (!any(sampleFilter)) {
message("No per-sample filter chosen, carry on with removing markers ",
"that fail per-marker QCs")
}
if (!any(markerFilter)) {
message("No per-marker filter chosen, carry on with removing samples ",
"that fail per-samples QCs")
}
checkFormat(prefix)
path2plink <- checkPlink(path2plink)
removeIDs <- NULL
IDs <- data.table::fread(paste(prefix, ".fam", sep=""),
data.table=FALSE, stringsAsFactors=FALSE,
header=FALSE)
if (any(sampleFilter)) {
if (filterRelated) {
fail_ibd_ids <- paste0(out, ".fail-IBD.IDs")
if (!file.exists(fail_ibd_ids)){
stop("filterRelated is TRUE but file ", out,
".fail-IBD.IDs does not exist")
} else {
if (verbose) {
message("Read individual IDs that failed relatedness check")
}
if (file.size(fail_ibd_ids) == 0) {
if (verbose) {
message("No individuals failed relatedness check")
}
} else {
removeIDs <- rbind(removeIDs,
data.table::fread(fail_ibd_ids,
data.table=FALSE,
stringsAsFactors=FALSE,
header=FALSE))
}
}
}
if (filterAncestry) {
fail_ancestry_ids <- paste0(out, ".fail-ancestry.IDs")
if (!file.exists(fail_ancestry_ids)){
stop("filterAncestry is TRUE but file ", out,
".fail-ancestry.IDs does not exist")
} else {
if (verbose) {
message("Read individual IDs that failed ancestry check")
}
if (file.size(fail_ancestry_ids) == 0) {
if (verbose) {
message("No individuals failed ancestry check")
}
} else {
removeIDs <- rbind(removeIDs,
data.table::fread(fail_ancestry_ids,
data.table=FALSE,
stringsAsFactors=FALSE,
header=FALSE))
}
}
}
if (filterHeterozygosity) {
fail_het_ids <- paste0(out, ".fail-het.IDs")
if (!file.exists(fail_het_ids)){
stop("filterHeterozygosity is TRUE but file ", out,
".fail-het.IDs does not exist")
} else {
if (verbose) {
message("Read individual IDs that failed heterozygosity ",
"check")
}
if (file.size(fail_het_ids) == 0) {
if (verbose) {
message("No individuals failed heterozygosity check")
}
} else {
removeIDs <- rbind(removeIDs,
data.table::fread(fail_het_ids,
data.table=FALSE,
stringsAsFactors=FALSE,
header=FALSE))
}
}
}
if (filterSampleMissingness) {
fail_imiss_ids <- paste0(out, ".fail-imiss.IDs")
if (!file.exists(fail_imiss_ids)){
stop("filterSampleMissingness is TRUE but file ", out,
".fail-imiss.IDs does not exist")
} else {
if (verbose) {
message("Read individual IDs that failed missingness check")
}
if (file.size(fail_imiss_ids) == 0) {
if (verbose) {
message("No individuals failed missingness check")
}
} else {
removeIDs <- rbind(removeIDs,
data.table::fread(fail_imiss_ids,
data.table=FALSE,
stringsAsFactors=FALSE,
header=FALSE))
}
}
}
if (filterSex) {
fail_sexcheck_ids <- paste0(out, ".fail-sexcheck.IDs")
if (!file.exists(fail_sexcheck_ids)){
stop("filterSex is TRUE but file ", out,
".fail-sexcheck.IDs does not exist")
} else {
if (verbose) {
message("Read individual IDs that failed sex check")
}
if (file.size(fail_sexcheck_ids) == 0) {
if (verbose) {
message("No individuals failed sex check")
}
} else {
removeIDs <- rbind(removeIDs,
data.table::fread(fail_sexcheck_ids,
data.table=FALSE,
stringsAsFactors=FALSE,
header=FALSE))
}
}
}
# ensure unique IDs in remove.IDs
if (!is.null(removeIDs)) {
removeIDs <- removeIDs[!duplicated(removeIDs),]
if (nrow(removeIDs) == nrow(IDs)) {
stop("All samples are flagged as .fail.IDs ",
"no samples remaining to generate the QCed dataset.")
}
if (verbose) message("Write file with remove IDs")
write.table(removeIDs, paste(out, ".remove.IDs", sep=""),
col.names=FALSE, row.names=FALSE, quote=FALSE)
keepIDs <- data.table::fread(paste(prefix, ".fam", sep=""),
data.table=FALSE,
stringsAsFactors=FALSE,
header=FALSE)
remove <- c("--remove", paste(out, ".remove.IDs", sep=""))
fail_samples <- nrow(removeIDs)
} else {
keepIDs <- IDs
remove <- NULL
fail_samples <- 0
}
} else {
keepIDs <- IDs
remove <- NULL
fail_samples <- 0
}
hwe <- NULL
maf <- NULL
missing <- NULL
if (filterHWE) {
if (is.null(hweTh)) {
stop("filterHWE is TRUE but hweTh not specified")
} else {
hwe <- c("--hwe midp", hweTh)
}
}
if (filterMAF) {
if (is.null(mafTh) && is.null(macTh)) {
stop("filterMAF is TRUE but neither mafTh or macTh are provided")
}
all_samples <- R.utils::countLines(paste(prefix, ".fam", sep=""))
keep_samples <- as.numeric(all_samples) - fail_samples
if (!is.null(mafTh) && !is.null(macTh)) {
if (verbose) {
message("Both mafTh and macTh provided, macTh=", macTh,
" is used (corresponds to mafTh=", round(mafTh, 6), ")")
}
} else if (!is.null(mafTh)) {
if(is.null(macTh)) macTh <- mafTh*(2*keep_samples)
if (verbose) {
message("The mafTh is ", mafTh, " which corresponds to a mcfTh=",
macTh)
}
} else {
if(is.null(mafTh)) mafTh <- macTh/(2*keep_samples)
if (verbose) {
message("The macTh is ", macTh," which corresponds to a mafTh=",
round(mafTh, 6))
}
}
maf <- c("--maf", mafTh)
}
if (filterSNPMissingness) {
if (is.null(lmissTh)) {
stop("filterSNPMissingness is TRUE but lmissTh not specified")
} else {
missing <- c("--geno", lmissTh)
}
}
if (verbose) message("Remove individual IDs and markers IDs that failed QC")
sys::exec_wait(path2plink,
args=c("--bfile", prefix, remove, maf, hwe, missing,
"--make-bed", "--out", paste(out, ".clean", sep="")),
std_out=showPlinkOutput, std_err=showPlinkOutput)
return(list(passIDs=keepIDs, failIDs=removeIDs))
}