how to convert lmd file to csv? [Flow Cytometry data]

Question:

lmd file extension is often used for generating flow cytometry data. But this couldn’t be directly used for processing either in R or Python. Is there a way to somehow convert it to csv or some renowned format?

Asked By: Sachin

||

Answers:

This is a bit tricky. So far I haven’t come across any such direct conversion function. Perhaps, fcs is a more common format for flow-cytometry. Hence, we will first need to convert lmd to fcs and then to csv format.

Step 1: lmd to fcs [Using R]

# install
source("http://bioconductor.org/biocLite.R")
biocLite("flowCore")

# convert
library(tools) # for file_path_sans_ext

if (!requireNamespace("BiocManager", quietly = TRUE))
  install.packages("BiocManager")

BiocManager::install(version = '3.10')

BiocManager::install("flowCore")
library(flowCore)

in.fname <- 'Filename.LMD'
folder <- '/home/User/Desktop/Data/'
x <- read.FCS(paste0(folder, in.fname))
summary(x)
out.fname <- paste0(file_path_sans_ext(in.fname), '.fcs')
write.FCS(x, paste0(folder, out.fname))

Note: Change User and Filename with your desired path and filename respectively
Your fcs file will be saved in the same folder.

Source: https://github.com/eyurtsev/FlowCytometryTools/issues/3

Step 2: fcs to csv

This could be done simply using the read.FCS() function to read in a FCS file, and then using write.delim() or write.csv() to create a .txt or .csv file.

Source: https://www.bioconductor.org/packages/release/bioc/vignettes/flowCore/inst/doc/HowTo-flowCore.pdf

Answered By: Sachin

Exclusively using python:

Code to convert FCS file to CSV

import pandas as pd
import fcsparser
path = ('<filename>.fcs')
meta = fcsparser.parse(path,meta_data_only=True)
meta, data = fcsparser.parse(path, meta_data_only=False, reformat_meta=True)
print(data)
df = pd.DataFrame(data)
df.to_csv('<filename>.csv')

Code to convert FCS file to CSV

import pandas as pd
import numpy as np
import fcswrite
path = ('<filename>.csv')
data = pd.read_csv(path).to_numpy()
chn_names = <list of channel names>
fcswrite.write_fcs(filename="<filename.fcs",chn_names=chn_names, data=data)
Answered By: Sachin

@Sachin didn’t walk through the complete solution so I propose a simpler and complete answer.

in R, using flowCore library

library(flowCore)
my_fcs <- read.FCS("test.lmd") 
mat <- exprs(my_fcs) 
write.csv(mat, file = "my_lmd.csv")

Some additional info to be complete:

  • read.FCS() : This assume that the lmd to be converted is in your working directory. So be sure that you’ve set correctly your working directory.
  • exprs() : This function read the values from the lmd or fcs file (in reality from the flowFrame object created by flowCore) as a matrix
  • Be careful with the lmd format as it contains an fcs 2.0 file and a fcs 3.0 file. If you want to load the fcs 3.0 file, you will need to set my_fcs <- read.FCS("test.lmd, dataset = 2")

A mentioned by @Sachin, flowCore library is available at bioConductor.

Answered By: onthemoon01