From 44092975ac595a31c5b36acf32c3faec5426106e Mon Sep 17 00:00:00 2001 From: Givanna Putri Date: Mon, 19 Aug 2019 12:13:32 +1000 Subject: [PATCH 1/4] Added bioconductor and fix spelling --- FCS-to-CSV v2.0.R | 151 +++++++++++++++++++++++----------------------- 1 file changed, 77 insertions(+), 74 deletions(-) diff --git a/FCS-to-CSV v2.0.R b/FCS-to-CSV v2.0.R index d759438..4719147 100644 --- a/FCS-to-CSV v2.0.R +++ b/FCS-to-CSV v2.0.R @@ -1,79 +1,82 @@ # FCS to CSV - # Coverting .fcs file data into an .csv file - # Thomas Ashhurst - # 2017-09-13 - # github.com/sydneycytometry - # .fcs file reading and writing adapted from https://gist.github.com/yannabraham/c1f9de9b23fb94105ca5 +# Coverting .fcs file data into an .csv file +# Thomas Ashhurst +# 2017-09-13 +# github.com/sydneycytometry +# .fcs file reading and writing adapted from https://gist.github.com/yannabraham/c1f9de9b23fb94105ca5 ##### USER INPUT ##### - - # Install packages if required - if(!require('flowCore')) {install.packages('flowCore')} - if(!require('Biobase')) {install.packages('Biobase')} - if(!require('data.table')) {install.packages('data.table')} - - # Load packages - library('flowCore') - library('Biobase') - library('data.table') - - # In order for this to work, a) rstudioapi must be installed and b) the location of this .r script must be in your desired working directory - dirname(rstudioapi::getActiveDocumentContext()$path) # Finds the directory where this script is located - setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # Sets the working directory to where the script is located - getwd() - PrimaryDirectory <- getwd() - PrimaryDirectory - - # Use this to manually set the working directory - #setwd("/Users/Tom/Desktop/Experiment") # Set your working directory here (e.g. "/Users/Tom/Desktop/") -- press tab when selected after the '/' to see options - #getwd() # Check your working directory has changed correctly - #PrimaryDirectory <- getwd() # Assign the working directory as 'PrimaryDirectory' - #PrimaryDirectory - - ## Use to list the .csv files in the working directory -- important, the only CSV files in the directory should be the one desired for analysis. If more than one are found, only the first file will be used - FileNames <- list.files(path=PrimaryDirectory, pattern = ".fcs") # see a list of CSV files - as.matrix(FileNames) # See file names in a list - - ## Read data from Files into list of data frames - DataList=list() # Creates and empty list to start - - for (File in FileNames) { # Loop to read files into the list - tempdata <- exprs(read.FCS(File, transformation = FALSE)) - tempdata <- tempdata[1:nrow(tempdata),1:ncol(tempdata)] - File <- gsub(".fcs", "", File) - DataList[[File]] <- tempdata - } - - rm(tempdata) - AllSampleNames <- names(DataList) - - ## Chech data quality - head(DataList) - - - + +# Install packages if required using Bioconductor +if (!requireNamespace("BiocManager", quietly = TRUE)) + install.packages("BiocManager") +BiocManager::install("flowCore") +BiocManager::install("Biobase") + +# Install packages if required using standard installation +if(!require('data.table')) {install.packages('data.table')} + +# Load packages +library('flowCore') +library('Biobase') +library('data.table') + +# In order for this to work, a) rstudioapi must be installed and b) the location of this .r script must be in your desired working directory +dirname(rstudioapi::getActiveDocumentContext()$path) # Finds the directory where this script is located +setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # Sets the working directory to where the script is located +getwd() +PrimaryDirectory <- getwd() +PrimaryDirectory + +# Use this to manually set the working directory +#setwd("/Users/Tom/Desktop/") # Set your working directory here (e.g. "/Users/Tom/Desktop/") -- press tab when selected after the '/' to see options +#getwd() # Check your working directory has changed correctly +#PrimaryDirectory <- getwd() # Assign the working directory as 'PrimaryDirectory' +#PrimaryDirectory + +## Use to list the .fcs files in the working directory -- important, the only FCS files in the directory should be the one desired for analysis. If more than one are found, only the first file will be used +FileNames <- list.files(path=PrimaryDirectory, pattern = ".fcs") # see a list of FCS files +as.matrix(FileNames) # See file names in a list + +## Read data from Files into list of data frames +DataList=list() # Creates and empty list to start + +for (File in FileNames) { # Loop to read files into the list + tempdata <- exprs(read.FCS(File, transformation = FALSE)) + tempdata <- tempdata[1:nrow(tempdata),1:ncol(tempdata)] + File <- gsub(".fcs", "", File) + DataList[[File]] <- tempdata +} + +rm(tempdata) +AllSampleNames <- names(DataList) + +## Chech data quality +head(DataList) + + + ##### END USER INPUT ##### - - x <- Sys.time() - x <- gsub(":", "-", x) - x <- gsub(" ", "_", x) - - newdir <- paste0("Output_FCS-to-CSV", "_", x) - - setwd(PrimaryDirectory) - dir.create(paste0(newdir), showWarnings = FALSE) - setwd(newdir) - - - for(i in c(1:length(AllSampleNames))){ - data_subset <- DataList[i][[1]] - data_subset <- as.data.frame(data_subset) - colnames(data_subset) - #data_subset <- rbindlist(as.list(data_subset$sample_data)) - dim(data_subset) - a <- names(DataList)[i] - - write.csv(data_subset, paste0(a, ".csv")) - } - \ No newline at end of file + +x <- Sys.time() +x <- gsub(":", "-", x) +x <- gsub(" ", "_", x) + +newdir <- paste0("Output_FCS-to-CSV", "_", x) + +setwd(PrimaryDirectory) +dir.create(paste0(newdir), showWarnings = FALSE) +setwd(newdir) + + +for(i in c(1:length(AllSampleNames))){ + data_subset <- DataList[i][[1]] + data_subset <- as.data.frame(data_subset) + colnames(data_subset) + #data_subset <- rbindlist(as.list(data_subset$sample_data)) + dim(data_subset) + a <- names(DataList)[i] + + write.csv(data_subset, paste0(a, ".csv")) +} From b4d9974067abd76522bffc81d80db6ef37549993 Mon Sep 17 00:00:00 2001 From: Givanna Putri Date: Mon, 19 Aug 2019 12:15:18 +1000 Subject: [PATCH 2/4] Revert "Added bioconductor and fix spelling" This reverts commit 44092975ac595a31c5b36acf32c3faec5426106e. --- FCS-to-CSV v2.0.R | 151 +++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 77 deletions(-) diff --git a/FCS-to-CSV v2.0.R b/FCS-to-CSV v2.0.R index 4719147..d759438 100644 --- a/FCS-to-CSV v2.0.R +++ b/FCS-to-CSV v2.0.R @@ -1,82 +1,79 @@ # FCS to CSV -# Coverting .fcs file data into an .csv file -# Thomas Ashhurst -# 2017-09-13 -# github.com/sydneycytometry -# .fcs file reading and writing adapted from https://gist.github.com/yannabraham/c1f9de9b23fb94105ca5 + # Coverting .fcs file data into an .csv file + # Thomas Ashhurst + # 2017-09-13 + # github.com/sydneycytometry + # .fcs file reading and writing adapted from https://gist.github.com/yannabraham/c1f9de9b23fb94105ca5 ##### USER INPUT ##### - -# Install packages if required using Bioconductor -if (!requireNamespace("BiocManager", quietly = TRUE)) - install.packages("BiocManager") -BiocManager::install("flowCore") -BiocManager::install("Biobase") - -# Install packages if required using standard installation -if(!require('data.table')) {install.packages('data.table')} - -# Load packages -library('flowCore') -library('Biobase') -library('data.table') - -# In order for this to work, a) rstudioapi must be installed and b) the location of this .r script must be in your desired working directory -dirname(rstudioapi::getActiveDocumentContext()$path) # Finds the directory where this script is located -setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # Sets the working directory to where the script is located -getwd() -PrimaryDirectory <- getwd() -PrimaryDirectory - -# Use this to manually set the working directory -#setwd("/Users/Tom/Desktop/") # Set your working directory here (e.g. "/Users/Tom/Desktop/") -- press tab when selected after the '/' to see options -#getwd() # Check your working directory has changed correctly -#PrimaryDirectory <- getwd() # Assign the working directory as 'PrimaryDirectory' -#PrimaryDirectory - -## Use to list the .fcs files in the working directory -- important, the only FCS files in the directory should be the one desired for analysis. If more than one are found, only the first file will be used -FileNames <- list.files(path=PrimaryDirectory, pattern = ".fcs") # see a list of FCS files -as.matrix(FileNames) # See file names in a list - -## Read data from Files into list of data frames -DataList=list() # Creates and empty list to start - -for (File in FileNames) { # Loop to read files into the list - tempdata <- exprs(read.FCS(File, transformation = FALSE)) - tempdata <- tempdata[1:nrow(tempdata),1:ncol(tempdata)] - File <- gsub(".fcs", "", File) - DataList[[File]] <- tempdata -} - -rm(tempdata) -AllSampleNames <- names(DataList) - -## Chech data quality -head(DataList) - - - + + # Install packages if required + if(!require('flowCore')) {install.packages('flowCore')} + if(!require('Biobase')) {install.packages('Biobase')} + if(!require('data.table')) {install.packages('data.table')} + + # Load packages + library('flowCore') + library('Biobase') + library('data.table') + + # In order for this to work, a) rstudioapi must be installed and b) the location of this .r script must be in your desired working directory + dirname(rstudioapi::getActiveDocumentContext()$path) # Finds the directory where this script is located + setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # Sets the working directory to where the script is located + getwd() + PrimaryDirectory <- getwd() + PrimaryDirectory + + # Use this to manually set the working directory + #setwd("/Users/Tom/Desktop/Experiment") # Set your working directory here (e.g. "/Users/Tom/Desktop/") -- press tab when selected after the '/' to see options + #getwd() # Check your working directory has changed correctly + #PrimaryDirectory <- getwd() # Assign the working directory as 'PrimaryDirectory' + #PrimaryDirectory + + ## Use to list the .csv files in the working directory -- important, the only CSV files in the directory should be the one desired for analysis. If more than one are found, only the first file will be used + FileNames <- list.files(path=PrimaryDirectory, pattern = ".fcs") # see a list of CSV files + as.matrix(FileNames) # See file names in a list + + ## Read data from Files into list of data frames + DataList=list() # Creates and empty list to start + + for (File in FileNames) { # Loop to read files into the list + tempdata <- exprs(read.FCS(File, transformation = FALSE)) + tempdata <- tempdata[1:nrow(tempdata),1:ncol(tempdata)] + File <- gsub(".fcs", "", File) + DataList[[File]] <- tempdata + } + + rm(tempdata) + AllSampleNames <- names(DataList) + + ## Chech data quality + head(DataList) + + + ##### END USER INPUT ##### - -x <- Sys.time() -x <- gsub(":", "-", x) -x <- gsub(" ", "_", x) - -newdir <- paste0("Output_FCS-to-CSV", "_", x) - -setwd(PrimaryDirectory) -dir.create(paste0(newdir), showWarnings = FALSE) -setwd(newdir) - - -for(i in c(1:length(AllSampleNames))){ - data_subset <- DataList[i][[1]] - data_subset <- as.data.frame(data_subset) - colnames(data_subset) - #data_subset <- rbindlist(as.list(data_subset$sample_data)) - dim(data_subset) - a <- names(DataList)[i] - - write.csv(data_subset, paste0(a, ".csv")) -} + + x <- Sys.time() + x <- gsub(":", "-", x) + x <- gsub(" ", "_", x) + + newdir <- paste0("Output_FCS-to-CSV", "_", x) + + setwd(PrimaryDirectory) + dir.create(paste0(newdir), showWarnings = FALSE) + setwd(newdir) + + + for(i in c(1:length(AllSampleNames))){ + data_subset <- DataList[i][[1]] + data_subset <- as.data.frame(data_subset) + colnames(data_subset) + #data_subset <- rbindlist(as.list(data_subset$sample_data)) + dim(data_subset) + a <- names(DataList)[i] + + write.csv(data_subset, paste0(a, ".csv")) + } + \ No newline at end of file From 5aa3a275f553635aa5add01b49fdda063867c099 Mon Sep 17 00:00:00 2001 From: Givanna Putri Date: Mon, 19 Aug 2019 12:16:56 +1000 Subject: [PATCH 3/4] Using biocmanager to install flowcore and biobase --- FCS-to-CSV v2.0.R | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/FCS-to-CSV v2.0.R b/FCS-to-CSV v2.0.R index d759438..a00a2f4 100644 --- a/FCS-to-CSV v2.0.R +++ b/FCS-to-CSV v2.0.R @@ -9,8 +9,12 @@ ##### USER INPUT ##### # Install packages if required - if(!require('flowCore')) {install.packages('flowCore')} - if(!require('Biobase')) {install.packages('Biobase')} + # BiocManager is used to provide compatibility for flowCore for R version 3.6 and above. + if (!requireNamespace("BiocManager", quietly = TRUE)) + install.packages("BiocManager") + BiocManager::install("flowCore") + BiocManager::install("Biobase") + if(!require('data.table')) {install.packages('data.table')} # Load packages @@ -31,8 +35,8 @@ #PrimaryDirectory <- getwd() # Assign the working directory as 'PrimaryDirectory' #PrimaryDirectory - ## Use to list the .csv files in the working directory -- important, the only CSV files in the directory should be the one desired for analysis. If more than one are found, only the first file will be used - FileNames <- list.files(path=PrimaryDirectory, pattern = ".fcs") # see a list of CSV files + ## Use to list the .fcs files in the working directory -- important, the only FCS files in the directory should be the one desired for analysis. If more than one are found, only the first file will be used + FileNames <- list.files(path=PrimaryDirectory, pattern = ".fcs") # see a list of FCS files as.matrix(FileNames) # See file names in a list ## Read data from Files into list of data frames From bad5b4cc8933ff589619a56ad770f8e1be09bca3 Mon Sep 17 00:00:00 2001 From: Givanna Putri Date: Mon, 19 Aug 2019 12:43:18 +1000 Subject: [PATCH 4/4] Change the way files with csv/fcs extension are found --- CSV-to-FCS v2.0.R | 10 +++++++--- FCS-to-CSV v2.0.R | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CSV-to-FCS v2.0.R b/CSV-to-FCS v2.0.R index 333eb03..07636da 100755 --- a/CSV-to-FCS v2.0.R +++ b/CSV-to-FCS v2.0.R @@ -9,8 +9,12 @@ ##### USER INPUT ##### # Install packages if required - if(!require('flowCore')) {install.packages('flowCore')} - if(!require('Biobase')) {install.packages('Biobase')} + # BiocManager is used to provide compatibility for flowCore for R version 3.6 and above. + if (!requireNamespace("BiocManager", quietly = TRUE)) + install.packages("BiocManager") + BiocManager::install("flowCore") + BiocManager::install("Biobase") + if(!require('data.table')) {install.packages('data.table')} # Load packages @@ -32,7 +36,7 @@ #PrimaryDirectory ## Use to list the .csv files in the working directory -- important, the only CSV files in the directory should be the one desired for analysis. If more than one are found, only the first file will be used - FileNames <- list.files(path=PrimaryDirectory, pattern = ".csv") # see a list of CSV files + FileNames <- list.files(path=PrimaryDirectory, pattern = "\\.csv$") # see a list of CSV files as.matrix(FileNames) # See file names in a list ## Read data from Files into list of data frames diff --git a/FCS-to-CSV v2.0.R b/FCS-to-CSV v2.0.R index a00a2f4..d93a2d7 100644 --- a/FCS-to-CSV v2.0.R +++ b/FCS-to-CSV v2.0.R @@ -36,7 +36,7 @@ #PrimaryDirectory ## Use to list the .fcs files in the working directory -- important, the only FCS files in the directory should be the one desired for analysis. If more than one are found, only the first file will be used - FileNames <- list.files(path=PrimaryDirectory, pattern = ".fcs") # see a list of FCS files + FileNames <- list.files(path=PrimaryDirectory, pattern = "\\.fcs$") # see a list of FCS files as.matrix(FileNames) # See file names in a list ## Read data from Files into list of data frames