Skip to content

Commit

Permalink
use the actual range instead of 1024 for in write.FCS #122
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejiang committed Aug 24, 2018
1 parent 8ba0610 commit 6cc1fcc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ useDynLib("flowCore",.registration=TRUE)

import(methods)
import(Rcpp)
importFrom(matrixStats, colMins)
importFrom(matrixStats, colMins, colMaxs)
importClassesFrom(Biobase, AnnotatedDataFrame)
importFrom("grDevices", "devAskNewPage")
importFrom("graphics", "abline")
Expand Down
11 changes: 7 additions & 4 deletions R/IO.R
Original file line number Diff line number Diff line change
Expand Up @@ -1391,10 +1391,13 @@ write.FCS <- function(x, filename, what="numeric", delimiter = "|", endian = "bi
pne[sapply(pne, length)==0] <- "0,0"
mk <- c(mk, pne)

## The same for PnR, "1024" if missing
mat <- exprs(x)
## The same for PnR,
pnr <- orig.kw[paste0(pid, "R")]
names(pnr) <- sprintf("$P%sR", newid)
pnr[sapply(pnr, length)==0] <- "1024"
empty_range_ind <- sapply(pnr, length)==0
empty_range_chnl <- as.character(pd[pid[empty_range_ind],"name"])
pnr[empty_range_ind] <- colMaxs(mat[, empty_range_chnl, drop = FALSE])
mk <- c(mk, pnr)

## Now update the PnN keyword
Expand All @@ -1415,7 +1418,7 @@ write.FCS <- function(x, filename, what="numeric", delimiter = "|", endian = "bi
description(x) <- mk

## Figure out the offsets based on the size of the initial text section
ld <- length(exprs(x)) * types[what, "bitwidth"]
ld <- length(mat) * types[what, "bitwidth"]
ctxt <- collapseDesc(x, delimiter = delimiter)

# these two are estimated value and just to get the reserved length of characters for storing the
Expand All @@ -1437,7 +1440,7 @@ write.FCS <- function(x, filename, what="numeric", delimiter = "|", endian = "bi
on.exit(close(con))
writeFCSheader(con, offsets)
writeChar(ctxt, con, eos=NULL)
writeBin(as(t(exprs(x)), what), con, size=types[what, "bitwidth"],
writeBin(as(t(mat), what), con, size=types[what, "bitwidth"],
endian=endian)
writeChar("00000000", con, eos=NULL)
filename
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/helper-expectResults.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
library(digest)
library(Biobase)
expectRes <- readRDS("expectResults.rds")
data(GvHD)
12 changes: 12 additions & 0 deletions tests/testthat/test-IO.R
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ test_that("write.FCS -- update channel and marker", {
expect_equal(markernames(inputFcs), markernames(tmp1))
})

test_that("write.FCS -- data from the flowFrame constructor without $PnR keys", {
set.seed(1)
mat <- matrix(rnorm(1000),ncol=4)
colnames(mat) <- LETTERS[1:4]
fr1 <- flowFrame(mat)
keyword(fr1)
tmp <- tempfile()
write.FCS(fr1, tmp)
fr2 <- read.FCS(tmp)
expect_equal(as.numeric(keyword(fr2)[["$P1R"]]), 2.4977, tolerance = 3e-4)
})

test_that("write.FCS -- add new cols", {
tmp <- GvHD[[1]]

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-fr-methods.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
context("flowFrame methods...")
data("GvHD")
fr <- GvHD[[1]]

test_that("range", {
rng1 <- data.frame("FSC-H" = c(0,1023)
,"SSC-H" = c(0,1023)
Expand Down

0 comments on commit 6cc1fcc

Please sign in to comment.