Getting number of datasets with flowCore
1
0
Entering edit mode
mark.holmes ▴ 10
@user-24965
Last seen 3.0 years ago

I am using read.FCS to load an FCS file containing multiple data segments, and am trying to automate the process but the number of datasets varies - I would like to be able to automatically get the number of data segments to then use a loop to store them all. There seems to be some built-in function for quickly checking the number of segments, as if I use read.FCS without specifying a dataset, it instantly returns:

"1: The file contains 35 additional data segments. The default is to read the first segment only."

However, I cannot find a way to manually obtain this information: what is the function that I can use to do this? I know that I could a) use a loop to continually read datasets until it fails, or b) have some external file indicating the number of datasets, but these methods are slower than whatever method already exists within flowCore.

flowCore • 940 views
ADD COMMENT
1
Entering edit mode
SamGG ▴ 330
@samgg-6428
Last seen 1 day ago
France/Marseille/Inserm

Hi, Interesting problem. I think the code of findOffsets (link) might be a starting point. Best.

ADD COMMENT
1
Entering edit mode

Getting findOffsets and extracting the relevant part did the trick, thanks. For the record, this part of the function did what was needed:

# get file connection where 'filename' is some local file with multiple data segments
con <- file(filename, open = 'rb')

## relevant code taken from findOffsets as just calling the function itself doesn't return the right info
offsets <- flowCore:::readFCSheader(con)
offsets <- matrix(offsets, nrow = 1, dimnames = list(NULL, names(offsets)))
txt <- flowCore:::readFCStext(con, offsets[1, ], emptyValue = FALSE)

addOff <- 0

if("$NEXTDATA" %in% names(txt)) {
  nd <- as.numeric(txt[["$NEXTDATA"]]) } else {nd <- 0}

txt.list <- list(txt)
i <- 1

while(nd != 0) {
  i <- i + 1
  addOff <- addOff + nd
  offsets <- rbind(offsets, flowCore:::readFCSheader(con, addOff))
  this.txt <- flowCore:::readFCStext(con, offsets[nrow(offsets),], emptyValue = FALSE)
  nd <- as.numeric(this.txt[["$NEXTDATA"]])
  txt.list[[i]] <- this.txt
}

# return number of data sets
nDataset <- length(txt.list)

Thanks for the pointer!

ADD REPLY
0
Entering edit mode

Great job!

Thanks for sharing.

ADD REPLY

Login before adding your answer.

Traffic: 753 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6