Использование R и flow.ore's write.FCS для добавления параметра - PullRequest
0 голосов
/ 12 ноября 2018

Я пытаюсь написать скрипт, который откроет файл 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, но не обновляю соответствующие разделы описания и параметров.

У кого-нибудь есть пример переписывания этих разделовсопоставить изменения в данных?

1 Ответ

0 голосов
/ 13 ноября 2018

Как насчет этого:

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.

Бест, Йозеф

Йозеф Спидлен, доктор философии, директор по биоинформатике, FlowJo

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...