---
title: "Chromatin compaction classification using DAPI intensity"
author: "Volker Schmid"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Chromatin compaction classification using DAPI intensity}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
## Preparation

    setRepositories(ind=c(1,2))
    install.packages("nucim")

## code to classify one rgb image

    library(bioimagetools)
    library(nucim)
    
choose RGB file

    img = readTIF(file.choose())
    
number of slices 

    slices = dim(img)[4]
    
we need the dimensions of the image in microns

    x = attributes(img)$x.resolution
    y = attributes(img)$y.resolution
    z = as.numeric(attributes(img)$spacing) * slices
and the dimensions of each voxel

    X = x/dim(img)[1]
    Y = y/dim(img)[2]
    Z = as.numeric(attributes(img)$spacing)
    zscale=mean(c(X,Y))/Z
we assume that the third channel is blue, ie, DAPI

    blue = img[,,3,] 
we mask the kernel

    mask = dapimask(blue, c(x,y,z), thresh="auto")
classify the DAPI channel

    classes = classify(blue, mask, 7, z=zscale)
count voxel per class

    counts <- table.n(classes, 7)
percentages

    perc <- print(counts/sum(counts)*100, 1)
    barplot(perc, names.arg=1:7, xlab="DAPI intensity class", ylab="percentage")

## code to classify a folder full of rgb images

    library(bioimagetools)
    library(nucim)
choose one of the files in a folder of RGB files

    folder = file.choose()
    f = unlist(gregexpr("/",folder))
    folder = substr(folder,1,f[length(f)])
scripts can use parallel computing, if available (not under Windows)

    nr.cores=ifelse(.Platform$OS.type=="windows", 1, 4)
split channels

    splitchannels.folder(folder, rgb.folder="./", cores=nr.cores)
masks

    dapimask.folder(folder, cores=nr.cores)
classification 

    classify.folder(folder, 7, cores=nr.cores)
results will be in folders "class7" and "class7-n"