-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.R
110 lines (88 loc) · 3.17 KB
/
main.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
library(BiocManager)
library(tercen)
library(dplyr)
library(flowCore)
#####
# options("tercen.workflowId" = "237423aa306b89c403ec93aaaa0463c7")
# options("tercen.stepId" = "eae75c16-ddf8-4ebc-af7d-afe8a2fe46d8")
#
# getOption("tercen.workflowId")
# getOption("tercen.stepId")
#####
fcs_to_data <- function(filename, comp="false", comp_df=NULL) {
data_fcs <- read.FCS(filename, transformation = FALSE, dataset = 2,truncate_max_range = FALSE)
# dataset is set to 2 for LMD files, it is ignored if there is only 1 dataset
#In readFCSdata(con, offsets, txt, transformation, which.lines, scale, :
#Some data values of 'PE-Cy5-A' channel exceed its $PnR value 82897 and will be truncated!
#To avoid truncation, either fix $PnR before generating FCS or set 'truncate_max_range = FALSE'
if (comp == "true") {
if (is.null(comp_df)) {
if ( is.null ( data_fcs@description$SPILL) ) {
data_fcs <- compensate(data_fcs, spillover(data_fcs)$'$SPILLOVER')
} else {
data_fcs <- compensate(data_fcs, spillover(data_fcs)$SPILL)
}
} else {
subset <- indexed_flowdata %>% select(-contains(c('TIME', 'FSC', 'SSC',
'BSC', 'Index')))
print(colnames(subset))
print(colnames(comp_df))
colnames(comp_df) <- colnames(subset)
data_fcs <-compensate(data_fcs, compensation(as.matrix(comp_df)))
}
}
names_parameters <- data_fcs@parameters@data$desc
data <- as.data.frame(exprs(data_fcs))
col_names <- colnames(data)
names_parameters <- ifelse(is.na(names_parameters),col_names,names_parameters)
colnames(data) <- names_parameters
data %>%
mutate_if(is.logical, as.character) %>%
mutate_if(is.integer, as.double) %>%
mutate(.ci = rep_len(0, nrow(.))) %>%
mutate(filename = rep_len(basename(filename), nrow(.)))
}
################
ctx <- tercenCtx()
if (!any(ctx$cnames == "documentId")) stop("Column factor documentId is required")
# extract files
df <- ctx$cselect()
docId = df$documentId[1]
doc = ctx$client$fileService$get(docId)
filename = tempfile()
writeBin(ctx$client$fileService$download(docId), filename)
on.exit(unlink(filename))
# unzip if archive
if(length(grep(".zip", doc$name)) > 0) {
tmpdir <- tempfile()
unzip(filename, exdir = tmpdir)
f.names <- list.files(tmpdir, full.names = TRUE)
} else {
f.names <- filename
}
# check FCS
if(any(!isFCSfile(f.names))) stop("Not all imported files are FCS files.")
assign("actual", 0, envir = .GlobalEnv)
task = ctx$task
# convert them to FCS files
f.names %>%
lapply(function(filename){
data = fcs_to_data(filename,comp="true")
if (!is.null(task)) {
# task is null when run from RStudio
actual = get("actual", envir = .GlobalEnv) + 1
assign("actual", actual, envir = .GlobalEnv)
evt = TaskProgressEvent$new()
evt$taskId = task$id
evt$total = length(f.names)
evt$actual = actual
evt$message = paste0('processing FCS file ' , filename)
ctx$client$eventService$sendChannel(task$channelId, evt)
} else {
cat('processing FCS file ' , filename)
}
data
}) %>%
bind_rows() %>%
ctx$addNamespace() %>%
ctx$save()