Project

General

Profile

Files » MS2Quant_PP.txt

Volker Baecker, 01/28/2015 03:05 PM

 
/**
* MRI MS2 QUANT PRE-PROCESSING
*
* Do the preprocessing for MS2Quant
* - concatenate stacks
* - measure and report the mean intensity in a 3D background region
* - crop 4D cells and save them in separate files
* - allow to outline and save transcription sites, nuclei and control regions as input to ms2quant
*
* written 2013-14 by Volker Baecker (INSERM) at Montpellier RIO Imaging (www.mri.cnrs.fr)
*/

var _SLICES = 12;
var _FRAMES = 304;
var sourceDir = "";
var _COUNTER = 0;
var _CELL_FOLDER;
var _CELL_NAME;
var _ENHANCE_CONTRAST = 1;
var _MARK = "PST";
var _IS_FOR_BATCH = false;
var _INDEX_STACK_1 = "1";
var _INDEX_STACK_2 = "2";

var helpURL = "http://dev.mri.cnrs.fr/projects/imagej-macros/wiki/MS2Quant_Pre-Processing";

macro "run tests [f12]" {
runTests();
}

macro "MS2 Quant PP Help [f1]" {
showHelp();
}

macro "MS2 Quant PP concatenate stacks [f2]" {
ms2quantPPConcatenateStacks();
}

macro "MS2 Quant PP measure background [f3]" {
ms2quantPPMeasureMeanBackground();
}

macro "MS2 Quant PP crop and save cell [f4]" {
ms2quantPPCropAndSave();
}

macro "MS2 Quant PP add selection [f5]" {
ms2quantPPAddRoi();
}

macro "MS2 Quant PP add selections [f6]" {
ms2quantPPAddRois();
}

macro "MS2 Quant PP save selections [f7]" {
ms2quantPPSaveRois();
}

macro "Unused Tool - C037" { }
macro "MS2 Quant PP Help (f1) Action Tool - C037T0d08MT8d08STed082"{
showHelp();
}

macro 'MS2 Quant PP Help (f1) Action Tool Options...' {
Dialog.create("MS2QuantPP Options");
Dialog.addNumber("z-slices: ", _SLICES);
Dialog.addNumber("time-frames: ", _FRAMES);
Dialog.addNumber("enhance contrast (%saturated): ", _ENHANCE_CONTRAST);
Dialog.addString("mark: ", _MARK);
Dialog.addCheckbox("is for batch: ", _IS_FOR_BATCH);
Dialog.show();
_SLICES = Dialog.getNumber();
_FRAMES = Dialog.getNumber();
_ENHANCE_CONTRAST = Dialog.getNumber();
_MARK = Dialog.getString();
_IS_FOR_BATCH = Dialog.getCheckbox();
}

macro "concatenate stacks (f2) Action Tool - C037T0d14cTad14s" {
ms2quantPPConcatenateStacks();
}

macro "concatenate stacks (f2) Action Tool Options..." {
Dialog.create("MS2QuantPP Options Concatenate");
Dialog.addString("index of stack 1: ", _INDEX_STACK_1);
Dialog.addString("index of stack 2: ", _INDEX_STACK_2);
Dialog.show();
_INDEX_STACK_1 = Dialog.getString();
_INDEX_STACK_2 = Dialog.getString();
}


macro "measure background (f3) Action Tool - C037T0d14mTcd14b" {
ms2quantPPMeasureMeanBackground();
}

macro "crop and save cell (f4) Action Tool - C037T0d09cT5d09rT8d09oTdd09p" {
ms2quantPPCropAndSave();
}

macro "add selection (f5) Action Tool - C037T0d09+T6d09rTad09oTfd09i" {
ms2quantPPAddRoi();
}

macro "add selections (f6) Action Tool - C037T0d09rT5d09oTbd09iTdd09s" {
ms2quantPPAddRois();
}

macro "save selections (f7) Action Tool - C037T0d09sT5d09aTbd09vTdd09e" {
ms2quantPPSaveRois();
}

function showHelp() {
run('URL...', 'url='+helpURL);
}

function ms2quantPPAddRoi() {
count = roiManager("count");
if (count==3) roiManager("reset");
count = roiManager("count");
name = "T";
if (count == 1) name = "N";
if (count == 2) name = "C";
roiManager("Add");
roiManager("Deselect");
roiManager("Show None");
roiManager("Show All");
run("Select None");
roiManager("Select", count);
roiManager("Rename", name);
roiManager("Deselect");
roiManager("Show None");
roiManager("Show All");
if (count == 2) {
roiManager("Save", _CELL_FOLDER + "/RoiSet.zip");
title = _CELL_NAME + ".tif";
closeImageAndProjections(title);
run("Add Selection...");
}
}

function ms2quantPPAddRois() {
count = roiManager("count");
if ((count % 3) == 0) name = "T";
if ((count % 3) == 1) name = "N";
if ((count % 3) == 2) name = "C";
name = name + "_" + floor(count / 3) + 1;
roiManager("Add");
roiManager("Deselect");
roiManager("Show None");
roiManager("Show All");
run("Select None");
roiManager("Select", count);
roiManager("Rename", name);
roiManager("Deselect");
roiManager("Show None");
roiManager("Show All");
}

function closeImageAndProjections(title) {
selectImage(title);
close();
selectImage("MAX_"+title);
close();
selectImage("MAX_MAX_"+title);
close();
}

function ms2quantPPSaveRois() {
roiManager("Save", sourceDir + "/" + "MAX_" + _CELL_NAME +"_RoiSet.zip");
title = _CELL_NAME + ".tif";
selectImage("MAX_" + title);
save(sourceDir + "/" + "MAX_" + title);
closeImageAndProjections(title);
}

function ms2quantPPCropAndSave() {
sourceDir = getDirectory("image");
title = getTitle();
parts = split(title, ".");
cellNumber = getCellNumber(sourceDir, parts[0]);
_CELL_NAME = parts[0];
if (!_IS_FOR_BATCH) _CELL_NAME = _CELL_NAME + "_c" + cellNumber;
newTitle = _CELL_NAME + ".tif";
run("Duplicate...", "title="+newTitle+" duplicate range=1-" + nSlices);
run("Select None");
run("Remove Overlay");
if (!_IS_FOR_BATCH) {
if (!File.exists(sourceDir + newTitle)) File.makeDirectory(sourceDir + _CELL_NAME);
_CELL_FOLDER = sourceDir + _CELL_NAME;
save(_CELL_FOLDER + "/" + newTitle);
}
showHyperstackAndProjections();
roiManager("Reset");
}

function showHyperstackAndProjections() {
run("Stack to Hyperstack...", "order=xyczt(default) channels=1 slices="+_SLICES+" frames="+_FRAMES+" display=Color");
run("Enhance Contrast", "saturated=" + _ENHANCE_CONTRAST);
getDimensions(width, height, channels, slices, frames);
run("Z Project...", "start=1 stop="+slices+" projection=[Max Intensity] all");
run("Enhance Contrast", "saturated=" + _ENHANCE_CONTRAST);
run("Z Project...", "start=1 stop="+frames+" projection=[Max Intensity]");
run("Enhance Contrast", "saturated=" + _ENHANCE_CONTRAST);
run("Tile");
setTool("freehand");
}

function ms2quantPPMeasureMeanBackground() {
setBatchMode(true);
sourceDir = getDirectory("Choose the source directory");
files = getFileList(sourceDir);
images = filterTifFiles(files);
_COUNTER = 0
logStart("MEASURE BACKGROUND");
for(i=0; i<images.length; i++) {
logUpdate(i, images);
measureBackgoundOnStack(images[i]);
}
if (isOpen("Background")) {
print("Saving measurements");
selectWindow("Background");
saveAs("Text", sourceDir + "Background.xls");
}
setBatchMode("exit and display");
logEnd("MEASURE BACKGROUND");
}

function ms2quantPPConcatenateStacks() {
print("\\Clear");
setBatchMode(true);
sourceDir = getDirectory("Choose the source directory");
files = getFileList(sourceDir);
files = filterFiles(files);
run("Clear Results");
logStart("CONCATENATE STACKS");
for(i=0; i<files.length; i++) {
logUpdate(i, files);
handleStack(files[i]);
}
setBatchMode("exit and display");
logEnd("CONCATENATE STACKS");
}

function getCellNumber(sourceDir, name) {
counter = 0;
files = getFileList(sourceDir);
for (i=0; i<files.length; i++) {
file = files[i];
if (File.isDirectory(sourceDir+ file) && startsWith(file, name + "_c")) counter++;
}
return counter + 1;
}

function measureBackgoundOnStack(file) {
open(file);
id = getImageID();
loadRois(file);
numberOfRois = roiManager("count");
if (numberOfRois != 1) {
print ("No selection for: " + file);
selectImage(id);
close();
return;
}
roiManager("Select", 0);
run("Clear Results");
if (!isOpen("Background")) {
run("Table...", "name=Background width=350 height=250");
print("[Background]", "\\Headings:n\timage\tmean background");
}
title = getTitle();
run("Set Measurements...", " mean display redirect=None decimal=3");
run("Plot Z-axis Profile");
run("Summarize");
res = getResult("Mean", nResults-4);
title = getTitle();
selectWindow(title);
close();
_COUNTER++;
print("[Background]", _COUNTER + "\t" + file + "\t"+ res);
selectImage(id);
close();
}

function handleStack(file) {
otherFile = otherStackFor(file);
open(file);
title1 = getTitle();
open(otherFile);
title2 = getTitle();
resultTitle = title1;
run("Concatenate...", " title=" + resultTitle + " image1=" + title1 + " image2=" + title2 + " image3=[-- None --]");
imageID = getImageID();
if (!File.exists(sourceDir+"STACKS")) File.makeDirectory(sourceDir+"STACKS");
newTitle = replaceMajorNumberWithOne(resultTitle);
saveAs("Tiff", sourceDir+"STACKS/" + newTitle);
selectImage(imageID);
close();
}

function otherStackFor(file) {
preFix =beforeMajorNumber(file);
postFix = afterMajorNumber(file);
majorNumber = getMajorNumber(file);
nextMajorNumber = substring(majorNumber, 0, 2) + _INDEX_STACK_2;
return "" + preFix + nextMajorNumber + postFix;
}

function replaceMajorNumberWithOne(file) {
preFix =beforeMajorNumber(file);
postFix = afterMajorNumber(file);
majorNumber = getMajorNumber(file);
nextMajorNumber = substring(majorNumber, 0, 2) + "1";
return "" + preFix + nextMajorNumber + postFix;
}

function filterFiles(files) {
count = countInterestingFiles(files);
filteredFiles = newArray(count);
counter = 0;
for(i=0; i<files.length; i++) {
if (isInteresting(files[i])) {
filteredFiles[counter] = files[i];
counter++;
}
}
return filteredFiles;
}

function filterTifFiles(files) {
count = 0;
for (i=0; i<files.length; i++) {
if (isTifFile(files[i])) count = count + 1;
}
images = newArray(count);
count = 0;
for (i=0; i<files.length; i++) {
if (isTifFile(files[i])) {
images[count] = files[i];
count = count + 1;
}
}
return images;
}

function countInterestingFiles(files) {
counter = 0;
for (i=0; i<files.length; i++) {
file = files[i];
if (isInteresting(file)) counter++;
}
return counter;
}

function isInteresting(file) {
if (!isImageFile(file)) return false;
majorNumber = getMajorNumber(file);
if (!endsWith(majorNumber, _INDEX_STACK_1) ) return false;
otherStack = otherStackFor(file);
fullPath = "" + sourceDir + otherStack;
if (!File.exists(fullPath)) return false;
return true;
}

function isImageFile(file) {
extensions = imageFileExtensions();
for (i=0; i<extensions.length; i++) {
extension = extensions[i];
if (endsWith(file, extension)) return true;
}
return false;
}

function isTifFile(file) {
extensions = newArray(".tif", ".TIF", ".tiff", ".TIFF");
for (i=0; i<extensions.length; i++) {
extension = extensions[i];
if (endsWith(file, extension)) return true;
}
return false;
}

function imageFileExtensions() {
exts = newArray(".dv");
return exts;
}

function afterMajorNumber(file) {
index = lastIndexOf(file, "_"+_MARK+"_");
result = substring(file, index + 5 + lengthOf(_MARK), lengthOf(file));
return result;
}

function beforeMajorNumber(file) {
index = lastIndexOf(file, "_"+_MARK+"_");
result = substring(file, 0, index+2+lengthOf(_MARK));
return result;
}

function getMajorNumber(file) {
index = lastIndexOf(file, "_"+_MARK+"_");
number = substring(file, index+2+lengthOf(_MARK), index + 2 + lengthOf(_MARK) + 3);
return number;
}

function logStart(nameOfMacro) {
print("\\Clear");
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
print("" + dayOfMonth + "-" + (month+1) + "-" + year + " " + hour + ":" + minute + ":" + second + "." + msec + " " + nameOfMacro + " STARTED");
}

function logUpdate(i, files) {
print("\\Update1:Processing image " + (i+1) +" of "+ files.length);
print(files[i]);
}

function logEnd(nameOfMacro) {
print("FINISHED " + nameOfMacro);
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
print("" + dayOfMonth + "-" + (month+1) + "-" + year + " " + hour + ":" + minute + ":" + second + "." + msec);
}


function loadRois(file) {
roiFile = sourceDir + file + ".zip";
roiManager("Reset");
if (File.exists(roiFile)) {
roiManager("Open", roiFile);
roiManager("Show None");
roiManager("Show All");
}
}

// Unit tests

function runTests() {
sourceDir = "/media/donnees/mri/in/tantale/1-2-3/";
print("START OF TESTRUN");
result = "failed";
if (testGetMajorNumber()) result = "ok";
print ("testGetMajorNumber - " + result);
result = "failed";
if (testOtherStackFor()) result = "ok";
print ("testOtherStackFor - " + result);
result = "failed";
if (testAfterMajorNumber()) result = "ok";
print ("testAfterMajorNumber - " + result);
result = "failed";
if (testBeforeMajorNumber()) result = "ok";
print ("testBeforeMajorNumber - " + result);
result = "failed";
if (testIsImageFile()) result = "ok";
print ("testIsImageFile - " + result);
result = "failed";
if (testIsInteresting()) result = "ok";
print ("testIsInteresting - " + result);
result = "failed";
if (testFilterFiles()) result = "ok";
print ("testFilterFiles - " + result);
result = "failed";
if (testIsTifFile()) result = "ok";
print ("testIsTifFile - " + result);
print("END OF TESTRUN");
}

function testGetMajorNumber() {
result = true;
majorNumber = getMajorNumber("20130816_100x_Movie_Calibration_RI510_128_PST_002_3.dv");
result = result && (majorNumber == "002");
return result;
}

function testOtherStackFor() {
result = true;
other = otherStackFor("20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2.dv");
result = result && (other == "20130816_100x_Movie_Calibration_RI510_128_PST_002_pt_2.dv");
return result;
}

function testAfterMajorNumber() {
result = true;
after = afterMajorNumber("20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2.dv");
result = result && (after=="_pt_2.dv");
return result;
}

function testBeforeMajorNumber() {
result = true;
before = beforeMajorNumber("20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2.dv");
result = result && (before=="20130816_100x_Movie_Calibration_RI510_128_PST_");
return result;
}

function testIsImageFile() {
result = true;
result = result && (!isImageFile("20130816_100x_Movie_Calibration_RI510_128_PST_001.log"));
result = result && (isImageFile("20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2.dv"));
return result;
}

function testIsInteresting() {
result = true;
result = result && (isInteresting("20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2.dv"));
result = result && (!isInteresting("20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2_PRJ.dv"));
return result;
}

function testIsTifFile() {
result = true;
result = result && (isTifFile("20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2.tif"));
result = result && (!isTifFile("20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2.tif.zip"));
return result;
}

function testFilterFiles() {
files = newArray("20130816_100x_Movie_Calibration_RI510_128_PST_001.log",
"20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2.dv",
"20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2_PRJ.dv",
"20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2_task.sh.joblog",
"20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_3.dv",
"20130816_100x_Movie_Calibration_RI510_128_PST_002_pt_2.dv");
filtered = filterFiles(files);
result = true;
result = result && (filtered.length == 2);
result = result && (filtered[0] == "20130816_100x_Movie_Calibration_RI510_128_PST_001_pt_2.dv");
return result;
}

(2-2/2)