-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from ImmuneDynamics/development
Updates
- Loading branch information
Showing
71 changed files
with
261 additions
and
500 deletions.
There are no files selected for viewing
252 changes: 252 additions & 0 deletions
252
workflows/Spatial - FlowJo/Spectre TIFF to FCS/TIFF to FCS.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
################################################################################### | ||
### Spectre: TIFF to FCS | ||
################################################################################### | ||
|
||
### Load libraries | ||
|
||
library('Spectre') | ||
|
||
Spectre::package.check(type = 'spatial') | ||
Spectre::package.load(type = 'spatial') | ||
|
||
### Set PrimaryDirectory | ||
|
||
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 | ||
|
||
### Set InputDirectory (ROI TIFFs) | ||
|
||
setwd(PrimaryDirectory) | ||
setwd("../data/ROIs/") | ||
InputDirectory <- getwd() | ||
InputDirectory | ||
|
||
### Set MaskDirectory (ROI mask TIFFs) | ||
|
||
setwd(PrimaryDirectory) | ||
setwd("../data/masks") | ||
MaskDirectory <- getwd() | ||
MaskDirectory | ||
|
||
### Create output directory | ||
|
||
setwd(PrimaryDirectory) | ||
dir.create("Output - TIFF to FCS") | ||
setwd("Output - TIFF to FCS") | ||
OutputDirectory <- getwd() | ||
OutputDirectory | ||
|
||
################################################################################### | ||
### Check ROIs and TIFFs | ||
################################################################################### | ||
|
||
### Initialise the spatial data object with channel TIFF files | ||
|
||
setwd(InputDirectory) | ||
|
||
rois <- list.dirs(full.names = FALSE, recursive = FALSE) | ||
as.matrix(rois) | ||
|
||
### Check channel names | ||
|
||
tiff.list <- list() | ||
|
||
for(i in rois){ | ||
setwd(InputDirectory) | ||
setwd(i) | ||
tiff.list[[i]] <- list.files(getwd()) | ||
} | ||
|
||
t(as.data.frame(tiff.list)) | ||
|
||
################################################################################### | ||
### Read in TIFF files and create spatial objects | ||
################################################################################### | ||
|
||
### Read in ROI channel TIFFs | ||
|
||
setwd(InputDirectory) | ||
spatial.dat <- read.spatial.files(dir = InputDirectory) | ||
|
||
### Check results | ||
|
||
str(spatial.dat, 3) | ||
spatial.dat[[1]]@RASTERS | ||
|
||
################################################################################### | ||
### Read in masks files | ||
################################################################################### | ||
|
||
### Define cell mask extension for different mask types | ||
|
||
setwd(MaskDirectory) | ||
|
||
all.masks <- list.files(pattern = '.tif') | ||
as.matrix(all.masks) | ||
|
||
mask.types <- list('cell.mask' = '_ilastik_s2_Object Identities.tif', | ||
'cell.type' = '_ilastik_s2_Object Predictions.tif', | ||
'region' = '_ilastik_s2_Simple Segmentation.tif') | ||
mask.types | ||
|
||
### Read in masks | ||
|
||
for(i in names(mask.types)){ | ||
spatial.dat <- do.add.masks(dat = spatial.dat, | ||
mask.dir = MaskDirectory, | ||
mask.pattern = mask.types[[i]], | ||
mask.label = i) | ||
} | ||
|
||
str(spatial.dat, 3) | ||
str(spatial.dat[[1]]@MASKS, 3) | ||
|
||
################################################################################### | ||
### Rename rasters (if required) | ||
################################################################################### | ||
|
||
### Check channel names | ||
|
||
channel.names <- list() | ||
|
||
for(i in names(spatial.dat)){ | ||
channel.names[[i]] <- names(spatial.dat[[i]]@RASTERS) | ||
} | ||
|
||
t(as.data.frame(channel.names)) | ||
|
||
### List of corrections (first entry is the 'correct' one) | ||
|
||
# corrections <- list(c('CD4','Cd4'), | ||
# c('CD8','CD8a') | ||
# ) | ||
|
||
### Replace the 'incorrect' names | ||
|
||
# for(i in names(spatial.dat)){ | ||
# # i <- names(spatial.dat)[[1]] | ||
# | ||
# for(a in c(1:length(corrections))){ | ||
# # a <- 1 | ||
# | ||
# trg <- which(names(spatial.dat[[i]]@RASTERS) == corrections[[a]][2]) | ||
# if(length(trg) != 0){ | ||
# names(spatial.dat[[i]]@RASTERS)[trg] <- corrections[[a]][1] | ||
# } | ||
# } | ||
# } | ||
|
||
### Check channel names | ||
|
||
# channel.names <- list() | ||
# | ||
# for(i in names(spatial.dat)){ | ||
# channel.names[[i]] <- names(spatial.dat[[i]]@RASTERS) | ||
# } | ||
# | ||
# t(as.data.frame(channel.names)) | ||
|
||
################################################################################### | ||
### Generate polygons and outlines | ||
################################################################################### | ||
|
||
### Generate polygons and outlines | ||
|
||
for(i in names(spatial.dat[[1]]@MASKS)){ | ||
spatial.dat <- do.create.outlines(dat = spatial.dat, mask.name = i) | ||
} | ||
|
||
|
||
### Checks | ||
|
||
str(spatial.dat, 3) | ||
str(spatial.dat[[1]]@MASKS, 2) | ||
|
||
################################################################################### | ||
### Mask QC plots | ||
################################################################################### | ||
|
||
### Mask plot setup | ||
|
||
setwd(OutputDirectory) | ||
dir.create('Plots - cell masks') | ||
setwd('Plots - cell masks') | ||
|
||
as.matrix(names(spatial.dat[[1]]@RASTERS)) | ||
base <- 'DNA1_Ir191' | ||
base | ||
|
||
as.matrix(names(spatial.dat[[1]]@MASKS)) | ||
mask <- 'cell.mask' | ||
mask | ||
|
||
### Create plots | ||
|
||
for(i in names(spatial.dat)){ | ||
make.spatial.plot(dat = spatial.dat, | ||
image.roi = i, | ||
image.channel = base, | ||
mask.outlines = mask) | ||
} | ||
|
||
################################################################################### | ||
### Calculate cellular data and plot | ||
################################################################################### | ||
|
||
### Calculate cellular data for each cell mask (this step may take some time) | ||
|
||
spatial.dat <- do.extract(spatial.dat, 'cell.mask') | ||
str(spatial.dat, 3) | ||
|
||
spatial.dat[[1]]@DATA | ||
|
||
all.dat <- do.pull.data(spatial.dat, 'CellData') | ||
all.dat | ||
|
||
################################################################################### | ||
### Save data | ||
################################################################################### | ||
|
||
### Output QS and CSV file | ||
|
||
setwd(OutputDirectory) | ||
dir.create('Data') | ||
setwd('Data') | ||
|
||
qsave(spatial.dat, "spatial.dat.qs") | ||
fwrite(all.dat, 'all.dat.csv') | ||
|
||
### Pull cellular data and write FCS file from each ROI independently | ||
|
||
setwd(OutputDirectory) | ||
dir.create('FCS files') | ||
setwd('FCS files') | ||
|
||
for(i in names(spatial.dat)){ | ||
|
||
## Extract data and setup cols | ||
|
||
tmp <- list() | ||
tmp[[i]] <- spatial.dat[[i]] | ||
|
||
cell.dat <- do.pull.data(tmp, 'CellData') | ||
cell.dat <- do.asinh(cell.dat, names(spatial.dat[[i]]@RASTERS), cofactor = 1) | ||
|
||
### Invert y axis | ||
|
||
all.neg <- function(test) -1*abs(test) | ||
|
||
y_invert <- cell.dat[['y']] | ||
y_invert <- all.neg(y_invert) | ||
cell.dat[['y_invert']] <- y_invert | ||
|
||
### Write FCS files | ||
|
||
write.files(cell.dat, i, write.csv = FALSE, write.fcs = TRUE) | ||
rm(cell.dat) | ||
rm(i) | ||
} | ||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+146 KB
workflows/Spatial - FlowJo/data/masks/ROI002_ilastik_s2_Object Identities.tif
Binary file not shown.
Binary file added
BIN
+12.7 KB
workflows/Spatial - FlowJo/data/masks/ROI002_ilastik_s2_Object Predictions.tif
Binary file not shown.
Binary file added
BIN
+7.41 KB
workflows/Spatial - FlowJo/data/masks/ROI002_ilastik_s2_Simple Segmentation.tif
Binary file not shown.
Binary file added
BIN
+164 KB
workflows/Spatial - FlowJo/data/masks/ROI004_ilastik_s2_Object Identities.tif
Binary file not shown.
Binary file added
BIN
+5.21 KB
workflows/Spatial - FlowJo/data/masks/ROI004_ilastik_s2_Object Predictions.tif
Binary file not shown.
Binary file added
BIN
+4.56 KB
workflows/Spatial - FlowJo/data/masks/ROI004_ilastik_s2_Simple Segmentation.tif
Binary file not shown.
Binary file added
BIN
+160 KB
workflows/Spatial - FlowJo/data/masks/ROI006_ilastik_s2_Object Identities.tif
Binary file not shown.
Binary file added
BIN
+7.15 KB
workflows/Spatial - FlowJo/data/masks/ROI006_ilastik_s2_Object Predictions.tif
Binary file not shown.
Binary file added
BIN
+5.6 KB
workflows/Spatial - FlowJo/data/masks/ROI006_ilastik_s2_Simple Segmentation.tif
Binary file not shown.
Binary file added
BIN
+133 KB
workflows/Spatial - FlowJo/data/masks/ROI008_ilastik_s2_Object Identities.tif
Binary file not shown.
Binary file added
BIN
+13.9 KB
workflows/Spatial - FlowJo/data/masks/ROI008_ilastik_s2_Object Predictions.tif
Binary file not shown.
Binary file added
BIN
+12.2 KB
workflows/Spatial - FlowJo/data/masks/ROI008_ilastik_s2_Simple Segmentation.tif
Binary file not shown.
Binary file added
BIN
+140 KB
workflows/Spatial - FlowJo/data/masks/ROI010_ilastik_s2_Object Identities.tif
Binary file not shown.
Binary file added
BIN
+12.3 KB
workflows/Spatial - FlowJo/data/masks/ROI010_ilastik_s2_Object Predictions.tif
Binary file not shown.
Binary file added
BIN
+10.4 KB
workflows/Spatial - FlowJo/data/masks/ROI010_ilastik_s2_Simple Segmentation.tif
Binary file not shown.
Binary file added
BIN
+107 KB
workflows/Spatial - FlowJo/data/masks/ROI012_ilastik_s2_Object Identities.tif
Binary file not shown.
Binary file added
BIN
+16.3 KB
workflows/Spatial - FlowJo/data/masks/ROI012_ilastik_s2_Object Predictions.tif
Binary file not shown.
Binary file added
BIN
+9.56 KB
workflows/Spatial - FlowJo/data/masks/ROI012_ilastik_s2_Simple Segmentation.tif
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ROI,Sample,Group,Batch | ||
ROI002,r2,Control,A | ||
ROI004,r4,Control,B | ||
ROI006,r7,Control,B | ||
ROI008,r8,Test,A | ||
ROI010,r10,Test,A | ||
ROI012,r12,Test,B |
Oops, something went wrong.