-
Notifications
You must be signed in to change notification settings - Fork 6
/
checkAlevinFryInputFiles.R
207 lines (186 loc) · 7.88 KB
/
checkAlevinFryInputFiles.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
#' Check that all required input files are available for alevin-fry
#'
#' @param mapDir Path to the directory containing the map.rad file
#' @param permitDir Path to the output directory of the alevin-fry
#' generate-permit-list command.
#' @param quantDir Path to the output of the alevin-fry quant command.
#'
#' @author Charlotte Soneson
#'
#' @export
#'
#' @importFrom utils read.delim
#'
#' @return Returns nothing, raises an error if any of the required files are
#' missing.
#'
#' @examples
#' checkAlevinFryInputFiles(
#' mapDir = system.file("extdata/alevinfry_example_v0.5.0/map",
#' package = "alevinQC"),
#' permitDir = system.file("extdata/alevinfry_example_v0.5.0/permit",
#' package = "alevinQC"),
#' quantDir = system.file("extdata/alevinfry_example_v0.5.0/quant",
#' package = "alevinQC"))
#'
checkAlevinFryInputFiles <- function(mapDir, permitDir, quantDir) {
msg <- NULL
## First check if the files are compatible with piscem v0.6.0 or newer
piscem_0.6.0 <- .checkAlevinFryInputFiles_piscem0.6.0(mapDir = mapDir,
permitDir = permitDir,
quantDir = quantDir)
if (is.null(piscem_0.6.0)) {
return("piscem_v0.6.0")
} else {
msg <- c(msg,
"Input directory not compatible with alevin-fry with the ",
"piscem mapper v0.6.0 or newer, the following required ",
"file(s) are missing or malformed:\n",
paste(piscem_0.6.0, collapse = "\n"), "\n\n")
}
## If not compatible with piscem v0.6.0, check if the files are
## compatible with alevin-fry v0.5.0 or newer
v0.5.0 <- .checkAlevinFryInputFiles_0.5.0(mapDir = mapDir,
permitDir = permitDir,
quantDir = quantDir)
if (is.null(v0.5.0)) {
return("v0.5.0")
} else {
msg <- c(msg,
"Input directory not compatible with alevin-fry v0.5.0 ",
"or newer, the following required file(s) are missing ",
"or malformed:\n",
paste(v0.5.0, collapse = "\n"), "\n\n")
}
## If not compatible with v0.5.0, check for compatibility with v0.4.3
v0.4.3 <- .checkAlevinFryInputFiles_0.4.3(mapDir = mapDir,
permitDir = permitDir,
quantDir = quantDir)
if (is.null(v0.4.3)) {
return("v0.4.3")
} else {
msg <- c(msg,
"Input directory not compatible with alevin-fry v0.4.3 ",
"or newer, the following required file(s) are missing ",
"or malformed:\n",
paste(v0.4.3, collapse = "\n"), "\n\n")
}
## If we are here, the baseDir doesn't match any of the known
## output structures.
stop(msg)
}
#' @keywords internal
#' @noRd
.checkAlevinFryInputFiles_0.4.3 <- function(mapDir, permitDir, quantDir) {
## Raise an error if any of the required files is missing
reqFiles <- c(file.path(permitDir, "all_freq.tsv"),
file.path(quantDir, "featureDump.txt"),
file.path(mapDir, "aux_info/meta_info.json"),
file.path(quantDir, "quant.json"),
file.path(mapDir, "cmd_info.json"),
file.path(quantDir, "alevin", "quants_mat_rows.txt"))
missingFiles <- reqFiles[vapply(reqFiles,
function(f) {
!file.exists(f)
}, TRUE)]
if (length(missingFiles) > 0) {
return(missingFiles)
}
## Check that all required columns in featureDump.txt are present
coln <- unlist(utils::read.delim(
file.path(quantDir, "featureDump.txt"),
header = FALSE, as.is = TRUE, sep = "\t", nrows = 1))
if (!(all(c("CorrectedReads", "MappingRate", "DedupRate",
"NumGenesOverMean", "MappedReads", "DeduplicatedReads",
"NumGenesExpressed") %in% coln))) {
missingFiles <- c(file.path(quantDir, "featureDump.txt"))
}
if (length(missingFiles) > 0) {
return(missingFiles)
}
## If we are here, the input is consistent with the expectation
return(NULL)
}
#' @keywords internal
#' @noRd
.checkAlevinFryInputFiles_0.5.0 <- function(mapDir, permitDir, quantDir) {
## Raise an error if any of the required files is missing
reqFiles <- file.path(permitDir, "generate_permit_list.json")
if (!file.exists(reqFiles)) {
return(reqFiles)
}
reqFiles <- c(reqFiles,
file.path(permitDir, "permit_freq.bin"),
file.path(quantDir, "featureDump.txt"),
file.path(mapDir, "aux_info/meta_info.json"),
file.path(quantDir, "quant.json"),
file.path(mapDir, "cmd_info.json"))
permitinfo <- rjson::fromJSON(file = file.path(permitDir,
"generate_permit_list.json"))
if (permitinfo$`permit-list-type` != "unfiltered") {
reqFiles <- c(reqFiles,
file.path(permitDir, "all_freq.bin"))
}
missingFiles <- reqFiles[vapply(reqFiles,
function(f) {
!file.exists(f)
}, TRUE)]
if (length(missingFiles) > 0) {
return(missingFiles)
}
## Check that all required columns in featureDump.txt are present
coln <- unlist(utils::read.delim(
file.path(quantDir, "featureDump.txt"),
header = FALSE, as.is = TRUE, sep = "\t", nrows = 1))
if (!(all(c("CorrectedReads", "MappingRate", "DedupRate",
"NumGenesOverMean", "MappedReads", "DeduplicatedReads",
"NumGenesExpressed") %in% coln))) {
missingFiles <- c(file.path(quantDir, "featureDump.txt"))
}
if (length(missingFiles) > 0) {
return(missingFiles)
}
## If we are here, the input is consistent with the expectation
return(NULL)
}
#' @keywords internal
#' @noRd
.checkAlevinFryInputFiles_piscem0.6.0 <- function(mapDir, permitDir, quantDir) {
## Raise an error if any of the required files is missing
reqFiles <- file.path(permitDir, "generate_permit_list.json")
if (!file.exists(reqFiles)) {
return(reqFiles)
}
reqFiles <- c(reqFiles,
file.path(permitDir, "permit_freq.bin"),
file.path(quantDir, "featureDump.txt"),
file.path(quantDir, "quant.json"),
file.path(mapDir, "map_info.json"))
permitinfo <- rjson::fromJSON(file = file.path(permitDir,
"generate_permit_list.json"))
if (permitinfo$`permit-list-type` != "unfiltered") {
reqFiles <- c(reqFiles,
file.path(permitDir, "all_freq.bin"))
}
missingFiles <- reqFiles[vapply(reqFiles,
function(f) {
!file.exists(f)
}, TRUE)]
if (length(missingFiles) > 0) {
return(missingFiles)
}
## Check that all required columns in featureDump.txt are present
coln <- unlist(utils::read.delim(
file.path(quantDir, "featureDump.txt"),
header = FALSE, as.is = TRUE, sep = "\t", nrows = 1))
if (!(all(c("CorrectedReads", "MappingRate", "DedupRate",
"NumGenesOverMean", "MappedReads", "DeduplicatedReads",
"NumGenesExpressed") %in% coln))) {
missingFiles <- c(file.path(quantDir, "featureDump.txt"))
}
if (length(missingFiles) > 0) {
return(missingFiles)
}
## If we are here, the input is consistent with the expectation
return(NULL)
}