This repository has been archived by the owner on May 4, 2022. It is now read-only.
forked from sydneycytometry/CSV-to-FCS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFCS-to-CSV v2.0.R
45 lines (36 loc) · 1.69 KB
/
FCS-to-CSV v2.0.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# FCS to CSV
# Converting .fcs file data into an .csv file
# Thomas Ashhurst (2017-09-13) [github.com/sydneycytometry]
# *adapted from [https://gist.github.com/yannabraham/c1f9de9b23fb94105ca5]
# Code refactoring by Christian Rickert (2022-03-31)
# Install required packages
if(!requireNamespace("BiocManager", quietly = TRUE)) {install.packages("BiocManager", quiet = TRUE)}
if(!require('flowCore')) {BiocManager::install("flowCore", update = FALSE)}
if(!require('Biobase')) {BiocManager::install("Biobase", update = FALSE)}
if(!require('data.table')) {install.packages('data.table')}
if(!require('rstudioapi')) {install.packages('rstudioapi')}
# Load packages
library('flowCore')
library('Biobase')
library('data.table')
library('rstudioapi')
# Set input and output variables
currentFolder <- dirname(rstudioapi::getSourceEditorContext()$path) # script location
importFolder <- file.path(currentFolder, "import", fsep = .Platform$file.sep) # relative path
exportFolder <- file.path(currentFolder, "export", fsep = .Platform$file.sep)
filePattern <- "\\.fcs$" # match file extension (case-insensitive)
# Set up working directories
if (!file.exists(importFolder)) {dir.create(importFolder)}
print(paste("Input folder: ", importFolder))
if (!file.exists(exportFolder)) {dir.create(exportFolder)}
print(paste("Export folder: ", exportFolder))
# Get list of input files
FileNames <- list.files(path = importFolder, pattern = filePattern, ignore.case = TRUE)
# Read data from CSV and write FCS data
for (FileName in FileNames) {
print(FileName)
setwd(importFolder)
FileData <- exprs(read.FCS(FileName, transformation = FALSE))
setwd(exportFolder)
write.csv(FileData, paste0(FileName, ".csv"))
}