Skip to content

Instantly share code, notes, and snippets.

@votti
Last active February 9, 2018 08:33
Show Gist options
  • Save votti/6a8274c0acc272f8b73bb2063aba0221 to your computer and use it in GitHub Desktop.
Save votti/6a8274c0acc272f8b73bb2063aba0221 to your computer and use it in GitHub Desktop.
Example how to compensate imc .txt files using CATALYST
---
title: ".txt file image compensation"
author: 'Vito Zanotelli et al.'
output:
html_document:
df_print: paged
keep_md: true
---
# Aim
This shows how to compensate a .txt file using CATALYST
```{r}
library(CATALYST)
library(data.table)
library(ggplot2)
library(flowCore)
library(dplyr)
library(dtplyr)
library(EBImage)
library(fields)
library(viridis)
```
## Helper functions
```{r}
col2img <- function(dat, valcol, xcol='X', ycol='Y'){
# Converts the image data into a 2D image
xmax = dat[, max(get(xcol))]
ymax = dat[, max(get(ycol))]
# sometimes the last row is incomplete, thus only take the data until the second last row in this case
if (xmax < dat[, get(xcol)[.N]]){
ymax = ymax-1
}
xmax = xmax+1
ymax = ymax+1
img = matrix(data=dat[1:(xmax*ymax), get(valcol)], nrow = xmax, ncol = ymax)
return(img)
}
normimg <- function(x){
#normalize image for visualization
xmax = max(x)
x = x/xmax
# this removes specle noise
x = EBImage::medianFilter(x, size=1)
x = x*xmax
return(x)
}
get_metals_from_txtname <- function(nam){
nam = gsub('.*\\(', '', nam)
nam = gsub('\\)', '',nam)
return(nam)
}
#' Estimates spillover directly from a folder containing IMC .txt files
comp_datimg <- function(datimg, sm, method='nnls',...){
orig_names = colnames(img)
metal_names = sapply(orig_names, get_metals_from_txtname)
img_mat = as.matrix(datimg)
colnames(img_mat) <- metal_names
img_comp = img_mat %>%
flowCore::flowFrame() %>%
CATALYST::compCytof(sm, method=method, ...) %>%
flowCore::exprs() %>%
as.data.table()
setnames(img_comp, orig_names)
return(img_comp)
}
```
## Set paths
```{r}
fn_sm = 'Spillover_Matrix_sm.csv'
fn_img = 'imc_example_image.txt'
fol_out = '.'
fn_out = 'imc_example_comp.txt'
```
## Read img
Fread is the fastest .csv reader I am aware of.
```{r}
img = fread(fn_img,sep = '\t')
```
### Visualization
```{r}
par(mfrow=c(1,2))
col2img(img, 'CarbAnhyd(Er166Di)') %>%
normimg() %>%
fields::image.plot(col=viridis(50))
col2img(img, '(Er167Di)') %>%
normimg() %>%
fields::image.plot(col=viridis(50))
```
## Compensate the image
```{r}
sm = read.csv(fn_sm, row.names = 1)
img_comp = comp_datimg(img, sm = sm)
```
### Visualization after compensation
```{r}
par(mfrow=c(1,2))
col2img(img_comp, 'CarbAnhyd(Er166Di)') %>%
normimg() %>%
fields::image.plot(col=viridis(50))
col2img(img_comp, '(Er167Di)') %>%
normimg() %>%
fields::image.plot(col=viridis(50))
```
### Write the image file
fwrite is a fast way to write .txt files
```{r}
fwrite(img_comp, file=file.path(fol_out, fn_out),sep='\t')
```
```{r}
sessionInfo()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment