使用R和flowCore的write.FCS附加参数

时间:2018-11-12 20:56:30

标签: r

我正在尝试编写一个脚本,该脚本将打开FCS文件,从其他参数的总和中创建一个参数,并将其写回为合法的FCS。

我的代码如下:

require("flowCore")

fname = paste(getwd(),"/8b_tonsil2_1.fcs", sep="");
outname = paste(getwd(),"/8bOUT.fcs", sep="");
fcs <- read.FCS(fname,transformation = FALSE)

nCols1 <- ncol(fcs)
sum <-rowSums(fcs@exprs[,2:nCols1])
fcs@exprs <- cbind(fcs@exprs, sum)
write.FCS(cytof, outname)

这不起作用,因为我要添加到fcs @ exprs矩阵中,但不更新描述和参数部分以使其匹配。

有人有重写这些部分以匹配数据更改的示例吗?

2 个答案:

答案 0 :(得分:1)

如何?

library('flowCore')
## Read the original file
original <- read.FCS("/Users/josef/Downloads/myFCS.fcs")

## Let's create a new parameter as an AnnotatedDataFrame by copying the first parameter from the original flowFrame
new_p <- parameters(original)[1,]

## Now, let's change it's name from $P1 to $Px (whatever the next new number is)
new_p_number <- as.integer(dim(original)[2]+1)
rownames(new_p) <- c(paste0("$P", new_p_number))

## Now, let's combine the original parameter with the new parameter 
library('BiocGenerics') ## for the combine function
allPars <- combine(parameters(original), new_p)

## Fix the name and description of the newly added parameter, say we want to be calling it cluster_id
new_p_name <- "cluster_id"
allPars@data$name[new_p_number] <- new_p_name
allPars@data$desc[new_p_number] <- new_p_name

## Check that allPars contains what it should
allPars@data

## Let's get our cluster ID into a single column matrix
## Using random numbers here; replace with your own code as appropriate
num.events <- as.integer(dim(original)[1])
cluster_ids <- as.matrix(runif(num.events, 1, max(original@exprs)), ncol=1)
new_exprs <- cbind(original@exprs, cluster_ids)

## Now, let's get all the original keywords and let's add to it
new_kw <- original@description
new_kw["$PAR"] <- as.character(new_p_number)
new_kw[paste0("$P",as.character(new_p_number),"N")] <- new_p_name
new_kw[paste0("$P",as.character(new_p_number),"S")] <- new_p_name
new_kw[paste0("$P",as.character(new_p_number),"E")] <- "0,0"
new_kw[paste0("$P",as.character(new_p_number),"G")] <- "1"
new_kw[paste0("$P",as.character(new_p_number),"B")] <- new_kw["$P1B"]
new_kw[paste0("$P",as.character(new_p_number),"R")] <- new_kw["$P1R"]
new_kw[paste0("flowCore_$P",as.character(new_p_number),"Rmin")] <- new_kw["flowCore_$P1Rmin"]
new_kw[paste0("flowCore_$P",as.character(new_p_number),"Rmax")] <- new_kw["flowCore_$P1Rmax"]

## Now, let's just combine it into a new flowFrame
new_fcs <- new("flowFrame", exprs=new_exprs, parameters=allPars, description=new_kw)

## Now, let's just use the regular write.FCS from flowCore to save the new FCS file.
write.FCS(new_fcs, filename="/Users/josef/Downloads/flowjo_test/FCSwithParAdded.fcs", delimiter="#")

## This new file should now be readable nicely R or any other software.

最好, 约瑟夫(Josef)

FlowJo生物信息学总监Josef Spidlen博士

答案 1 :(得分:0)

接受的答案确实确实为flowFrame添加了一个参数。但是,保存到新exprs的{​​{1}}插槽中的矩阵缺少所添加参数的列名。这与其他列不一致。要解决此问题,请在可接受的答案中替换代码块,以创建新的表达式矩阵flowFrame,如下所示:

new_exprs

(请注意,这是作为编辑提出的,但被拒绝了,因为它显然应该是答案/评论;我没有评论的代表)。