-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.nf
More file actions
295 lines (243 loc) · 9.63 KB
/
main.nf
File metadata and controls
295 lines (243 loc) · 9.63 KB
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
process COGAPS {
tag "$prefix"
label 'process_high'
label 'process_long'
container 'ghcr.io/fertiglab/cogaps:master'
input:
tuple val(meta), path(dgCMatrix), val(cparams)
output:
tuple val(meta), path("${prefix}/cogapsResult.rds"), emit: cogapsResult
path "versions.yml", emit: versions
script:
def args = task.ext.args ?: ''
prefix = task.ext.prefix ?: "${meta.id}/${cparams.niterations}-${cparams.npatterns}-${cparams.sparse}-${cparams.distributed}"
"""
mkdir -p "${prefix}"
Rscript -e 'library("CoGAPS");
sparse <- readRDS("$dgCMatrix");
data <- as.matrix(sparse);
#avoid errors with distributed params
dist_param <- NULL;
if(!("$cparams.distributed"=="null")){
dist_param <- "$cparams.distributed"};
params <- CogapsParams(seed=42,
nIterations = $cparams.niterations,
nPatterns = $cparams.npatterns,
sparseOptimization = as.logical($cparams.sparse),
distributed=dist_param);
if (!(is.null(dist_param))){
nsets <- $cparams.nsets;
allow_cpus <- as.numeric($task.cpus);
if( allow_cpus < 2){
stop("Error: distributed mode requires at least 2 cpus")
}
if (nsets > allow_cpus){
message("Warning: nsets is greater than available cpus. Setting nsets to ", allow_cpus);
}
params <- setDistributedParams(params, nSets = min(nsets,allow_cpus));
};
cogapsResult <- CoGAPS(data = data, params = params, nThreads = $cparams.nthreads,
outputFrequency = 100);
saveRDS(cogapsResult, file = "${prefix}/cogapsResult.rds")'
cat <<-END_VERSIONS > versions.yml
"${task.process}":
CoGAPS: \$(Rscript -e 'print(packageVersion("CoGAPS"))' | awk '{print \$2}')
R: \$(Rscript -e 'print(packageVersion("base"))' | awk '{print \$2}')
END_VERSIONS
"""
stub:
def args = task.ext.args ?: ''
prefix = task.ext.prefix ?: "${meta.id}/${cparams.niterations}-${cparams.npatterns}-${cparams.sparse}-${cparams.distributed}"
"""
mkdir "${prefix}"
touch "${prefix}/cogapsResult.rds"
cat <<-END_VERSIONS > versions.yml
"${task.process}":
CoGAPS: \$(Rscript -e 'print(packageVersion("CoGAPS"))' | awk '{print \$2}')
R: \$(Rscript -e 'print(packageVersion("base"))' | awk '{print \$2}')
END_VERSIONS
"""
}
process COGAPS_TENX2DGC {
tag "$meta.id"
label 'process_low'
container 'docker.io/satijalab/seurat:5.0.0'
input:
tuple val(meta), path(data)
output:
tuple val(meta), path("${prefix}/dgCMatrix.rds"), emit: dgCMatrix
path "versions.yml" , emit: versions
script:
def args = task.ext.args ?: ''
prefix = task.ext.prefix ?: "${meta.id}"
"""
mkdir "${prefix}"
Rscript -e 'res <- Seurat::Read10X("$data/filtered_feature_bc_matrix/");
saveRDS(res, file="${prefix}/dgCMatrix.rds")';
cat <<-END_VERSIONS > versions.yml
"${task.process}":
seurat: \$(Rscript -e 'print(packageVersion("Seurat"))' | awk '{print \$2}')
R: \$(Rscript -e 'print(packageVersion("base"))' | awk '{print \$2}')
END_VERSIONS
"""
stub:
def args = task.ext.args ?: ''
prefix = task.ext.prefix ?: "${meta.id}"
"""
mkdir "${prefix}"
touch "${prefix}/dgCMatrix.rds"
cat <<-END_VERSIONS > versions.yml
"${task.process}":
seurat: \$(Rscript -e 'print(packageVersion("Seurat"))' | awk '{print \$2}')
R: \$(Rscript -e 'print(packageVersion("base"))' | awk '{print \$2}')
END_VERSIONS
"""
}
process COGAPS_ADATA2DGC {
tag "$meta.id"
label 'process_medium'
container 'docker.io/satijalab/seurat:5.0.0'
input:
tuple val(meta), path(data)
output:
tuple val(meta), path("${prefix}/dgCMatrix.rds"), emit: dgCMatrix
path "versions.yml" , emit: versions
script:
def args = task.ext.args ?: ''
prefix = task.ext.prefix ?: "${meta.id}"
"""
mkdir "${prefix}"
Rscript -e 'message("Reading", "$data");
f <- hdf5r::h5file(filename = "$data", mode="r");
enctype <- hdf5r::h5attributes(f[["X/"]])[["encoding-type"]];
i <- hdf5r::readDataSet(f[["X/indices"]]);
p <- hdf5r::readDataSet(f[["X/indptr"]]);
x <- hdf5r::readDataSet(f[["X/data"]]);
var <- hdf5r::readDataSet(f[["var/_index"]]);
obs <- hdf5r::readDataSet(f[["obs/_index"]]);
message("Got", enctype, " ", length(var), "x", length(obs));
if(enctype=="csr_matrix"){
dimnames <- list(var, obs)
transpose <- FALSE
} else if (enctype=="csc_matrix"){
dimnames <- list(obs, var)
transpose <- TRUE
} else {
stop("Unknown encoding type")
};
message("Creating dgCMatrix");
res <- Matrix::sparseMatrix(i=i, p=p, x=x, dims=lengths(dimnames),
dimnames=dimnames, index1=FALSE, repr="C");
if(transpose){
res <- Matrix::t(res)
};
message("Saving dgCMatrix");
saveRDS(res, file="${prefix}/dgCMatrix.rds")';
cat <<-END_VERSIONS > versions.yml
"${task.process}":
hdf5r: \$(Rscript -e 'print(packageVersion("hdf5r"))' | awk '{print \$2}')
Matrix: \$(Rscript -e 'print(packageVersion("Matrix"))' | awk '{print \$2}')
Seurat: \$(Rscript -e 'print(packageVersion("Seurat"))' | awk '{print \$2}')
R: \$(Rscript -e 'print(packageVersion("base"))' | awk '{print \$2}')
END_VERSIONS
"""
stub:
def args = task.ext.args ?: ''
prefix = task.ext.prefix ?: "${meta.id}"
"""
mkdir "${prefix}"
touch "${prefix}/dgCMatrix.rds"
cat <<-END_VERSIONS > versions.yml
"${task.process}":
hdf5r: \$(Rscript -e 'print(packageVersion("hdf5r"))' | awk '{print \$2}')
R: \$(Rscript -e 'print(packageVersion("base"))' | awk '{print \$2}')
END_VERSIONS
"""
}
process COGAPS_PREPROCESS {
tag "$prefix"
label 'process_medium'
container 'ghcr.io/fertiglab/cogaps:master'
input:
tuple val(meta), path(dgCMatrix)
output:
tuple val(meta), path("${prefix}/dgCMatrix.rds"), emit: dgCMatrix
path "versions.yml", emit: versions
script:
def args = task.ext.args ?: ''
prefix = task.ext.prefix ?: "${meta.id}"
"""
mkdir -p "${prefix}"
Rscript -e 'library("Matrix");
library("sparseMatrixStats")
sparse <- readRDS("$dgCMatrix");
#sparsity is
message("sparsity: ", sum(sparse==0)/ (nrow(sparse)*ncol(sparse)));
#drop rows with > 95% zero counts
message("filtering rows with >95% zeros");
nz <- rowSums(sparse != 0);
sparse <- sparse[nz > 0.05 * ncol(sparse),];
message("filtered to ", nrow(sparse), " columns of ", length(nz));
#drop columns with > 95% zero counts
message("filtering columns with >95% zeros");
nz <- colSums(sparse != 0);
sparse <- sparse[,nz > 0.05 * nrow(sparse)];
message("filtered to ", ncol(sparse), " rows of ", length(nz));
#resulting sparsity is
message("sparsity: ", sum(sparse==0)/ (nrow(sparse)*ncol(sparse)));
#select top N genes
message("finding top ", ${params.n_top_genes}, " genes");
vars <- rowVars(sparse);
ngenes <- min(length(vars),${params.n_top_genes});
top_genes <- order(vars, decreasing=TRUE)[1:ngenes];
sparse <- sparse[top_genes,];
message("selected top ", length(top_genes), " genes of ", length(vars));
saveRDS(sparse, file = "${prefix}/dgCMatrix.rds")'
cat <<-END_VERSIONS > versions.yml
"${task.process}":
R: \$(Rscript -e 'print(packageVersion("base"))' | awk '{print \$2}')
END_VERSIONS
"""
stub:
def args = task.ext.args ?: ''
prefix = task.ext.prefix ?: "${meta.id}"
"""
mkdir "${prefix}"
touch "${prefix}/dgCMatrix.rds"
cat <<-END_VERSIONS > versions.yml
"${task.process}":
R: \$(Rscript -e 'print(packageVersion("base"))' | awk '{print \$2}')
END_VERSIONS
"""
}
//example workflow
workflow {
//example channel with data folders, for example
ch_adata = Channel.fromPath("${params.input}/**.h5ad")
.map { tuple([id:it.getName().replace('.', '-')], it)}
ch_rds = Channel.fromPath("${params.input}/**.rds")
.map { tuple([id:it.getName().replace('.', '-')], it)}
//make a channel with desired pattern number
def patterns = params.npatterns.split(',').collect { it.toInteger() }
ch_patterns = Channel.from(patterns)
//example channel with cparams
ch_fixed_params = Channel.of([niterations: params.niterations, sparse: params.sparse, distributed: params.distributed, nsets:params.nsets, nthreads:1])
ch_cparams = ch_patterns
.combine(ch_fixed_params)
.map { tuple([id:it[0].toString(), npatterns:it[0], niterations:it[1].niterations, sparse:it[1].sparse, distributed:it[1].distributed, nsets:it[1].nsets, nthreads:it[1].nthreads]) }
// convert adata to dgCMatrix
COGAPS_ADATA2DGC(ch_adata)
// preprocess dgCMatrix
ch_preprocess = COGAPS_ADATA2DGC.out.dgCMatrix
.map { tuple(it[0], it[1]) }
ch_preprocess = ch_preprocess.mix(ch_rds)
COGAPS_PREPROCESS(ch_preprocess)
// ch_cogaps_input of converted adatas and rdses
ch_input = COGAPS_PREPROCESS.out.dgCMatrix
.map { tuple(it[0], it[1]) }
// combine the two channels as input to CoGAPS
ch_input = ch_input.combine(ch_cparams)
COGAPS(ch_input)
}
//example:
//nextflow run main.nf --input tests/nextflow --outdir out -c nextflow.config -profile docker --max_memory 10GB --max_cpus 8