forked from biowdl/tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergecounts.wdl
75 lines (60 loc) · 2.54 KB
/
mergecounts.wdl
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
version 1.0
#DEPRECATED
task MergeCounts {
input {
Array[File] inputFiles
String outputFile
Int featureColumn
Int valueColumn
Boolean inputHasHeader
String featureAttribute = "gene_id"
File referenceGtf
Array[String]+? additionalAttributes
Int? memoryPerSample = 3
}
# Based on a script by Szymon Kielbasa/Ioannis Moustakas
command <<<
set -e
mkdir -p ~{sub(outputFile, basename(outputFile) + "$", "")}
R --no-save <<CODE
Sys.setlocale("LC_ALL","English") #FIXME this should be set in the docker image instead
library(dplyr)
library(reshape2)
library(refGenome)
list.of.files <- c("~{sep='", "' inputFiles}")
value.i <- ~{valueColumn}
feature.i <- ~{featureColumn}
header <- ~{true="TRUE" false="FALSE" inputHasHeader}
feature.attribute <- "~{featureAttribute}"
additional.attributes <- c(~{true='"' false="" defined(additionalAttributes)}~{sep='", "' additionalAttributes}~{true='"' false="" defined(additionalAttributes)})
reference.gtf <- "~{referenceGtf}"
output.path <- "~{outputFile}"
d <- do.call(rbind, lapply(list.of.files, function(file){
d <- read.table(file, sep="\t", header=header, comment.char="#")
filename <- basename(file)
colnames(d)[value.i] <- sub("\\\.[^\\\.]*\$", "", filename)
colnames(d)[feature.i] <- "feature"
d <- d %>% melt(id.vars=feature.i, variable.name="sample",
value.name="count")
}))
d <- d %>% dcast(feature ~ sample, value.var="count")
gtf <- ensemblGenome(dirname(reference.gtf))
read.gtf(gtf, basename(reference.gtf))
gtf.table <- gtf@ev\$gtf
gtf.table <- gtf.table[order(gtf.table[,feature.attribute]),]
gtf.table <- gtf.table[!duplicated(gtf.table[,feature.attribute]),]
id.table <- gtf.table[, c(feature.attribute, additional.attributes), drop=F]
output.table <- merge(id.table, d, all.y = T, by.y="feature",
by.x=feature.attribute)
write.table(output.table, file=output.path, sep="\t", quote=FALSE,
row.names=FALSE, na="")
CODE
>>>
output {
File mergedCounts = outputFile
}
runtime {
memory: 4 + (memoryPerSample * length(inputFiles))
docker: "biowdl/mergecounts:1.0"
}
}