Using R and flowCore's write.FCS to append a parameter










0














I'm trying to write a script that will open an FCS file, create a parameter from the sum of the other parameters, and write that back out as a legal FCS.



My code looks like this:



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)


This does not work because I am adding to the fcs@exprs matrix but not updating the description and parameters sections to match.



Does anyone have an example of rewriting these sections to match changes in the data?










share|improve this question


























    0














    I'm trying to write a script that will open an FCS file, create a parameter from the sum of the other parameters, and write that back out as a legal FCS.



    My code looks like this:



    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)


    This does not work because I am adding to the fcs@exprs matrix but not updating the description and parameters sections to match.



    Does anyone have an example of rewriting these sections to match changes in the data?










    share|improve this question
























      0












      0








      0







      I'm trying to write a script that will open an FCS file, create a parameter from the sum of the other parameters, and write that back out as a legal FCS.



      My code looks like this:



      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)


      This does not work because I am adding to the fcs@exprs matrix but not updating the description and parameters sections to match.



      Does anyone have an example of rewriting these sections to match changes in the data?










      share|improve this question













      I'm trying to write a script that will open an FCS file, create a parameter from the sum of the other parameters, and write that back out as a legal FCS.



      My code looks like this:



      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)


      This does not work because I am adding to the fcs@exprs matrix but not updating the description and parameters sections to match.



      Does anyone have an example of rewriting these sections to match changes in the data?







      r






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 20:56









      adamstuart

      485




      485






















          1 Answer
          1






          active

          oldest

          votes


















          1














          How about this:



          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.


          Best,
          Josef



          Josef Spidlen, Ph.D., Director of Bioinformatics, FlowJo






          share|improve this answer




















          • perfect. works like a charm
            – adamstuart
            Nov 14 '18 at 23:16










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269975%2fusing-r-and-flowcores-write-fcs-to-append-a-parameter%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          How about this:



          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.


          Best,
          Josef



          Josef Spidlen, Ph.D., Director of Bioinformatics, FlowJo






          share|improve this answer




















          • perfect. works like a charm
            – adamstuart
            Nov 14 '18 at 23:16















          1














          How about this:



          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.


          Best,
          Josef



          Josef Spidlen, Ph.D., Director of Bioinformatics, FlowJo






          share|improve this answer




















          • perfect. works like a charm
            – adamstuart
            Nov 14 '18 at 23:16













          1












          1








          1






          How about this:



          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.


          Best,
          Josef



          Josef Spidlen, Ph.D., Director of Bioinformatics, FlowJo






          share|improve this answer












          How about this:



          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.


          Best,
          Josef



          Josef Spidlen, Ph.D., Director of Bioinformatics, FlowJo







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 23:15









          Josef Spidlen

          261




          261











          • perfect. works like a charm
            – adamstuart
            Nov 14 '18 at 23:16
















          • perfect. works like a charm
            – adamstuart
            Nov 14 '18 at 23:16















          perfect. works like a charm
          – adamstuart
          Nov 14 '18 at 23:16




          perfect. works like a charm
          – adamstuart
          Nov 14 '18 at 23:16

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269975%2fusing-r-and-flowcores-write-fcs-to-append-a-parameter%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto