I am new to ImageJ so I'm not even exactly sure what I should be searching for to find an answer to this question.
I have a bunch of image files in one folder (TIF). I already wrote a macro to split the files into RGB channels and then save them to the same directory. The output is thus something like this:
-File 1-blue.tif
-File 1-green.tif
-File 1-red.tif
-File 2-blue.tif
-File 2-green.tif
...
etc.
Next I am using the JACoP plugin which takes two image files and computes various kinds of correlation coefficients/analysis between the inputs. For each directory, I want to do the following:
Set global settings:
Then loop through the following for each file in my directory:
Then finally:
The problem is that I don't know how to tell ImageJ how to call each of the color files in that order. How do I specify that it pulls out the files in chunks of three, analyses them, and then moves on to the next chunk of three?
I just need some basic help on the general algorithm and possible functions I might need to get me started - I can write the actual macro code myself.
EDIT: It has just occurred to me that I may be able to pass the files to an ImageJ macro in sets of three by using R. Something like this:
file_list <- list.files(getwd())
rgbFiles <- file_list[grep(pattern = "blue|green|red", file_list)]
rgbFilesSplit <- split(rgbFiles, ceiling(seq_along(rgbFiles)/3))
So after setting the working directory, I just extract out the rgb files and split them into a list.
If this approach is valid, how would I then call an ImageJ macro on each of the files described in the sublist? I already know how to call the macro and specify the input directory using the R system() function but how would I get ImageJ to receive input files which are just file name strings generated from the R code above?
I asked this question on the ImageJ mailing list. The user Joost Willemse was very helpful in forming my final macro. Here is the full macro:
dir=getDirectory("Choose a Directory");
list = getFileList(dir);
Array.sort(list);
for (i=0; i<list.length; i+=3) {
open(list[i]);
blue=getTitle;
open(list[i+1]);
green=getTitle;
open(list[i+2]);
red=getTitle;
run("JACoP ", "imga="+red+" imgb="+blue+" thra=648 thrb=517 pearson mm");
run("JACoP ", "imga="+red+" imgb="+green+" thra=648 thrb=517 pearson mm");
run("JACoP ", "imga="+blue+" imgb="+green+" thra=648 thrb=517 pearson mm");
close(red);
close(green);
close(blue);
}
After setting the directory, the for loop begins. i+=3
lets the iterator count in steps of three (I didn't know this was possible when I asked the question)! Now each of the three images are opened and their titles are saved. Finally, the titles are send into the string portion of the run()
function via concatenation. Then the images are closed. As long as your list is sorted correctly in the directory before you begin this should work just fine. Make sure you set your thresholds in the run()
function for JACoP!!
Additionally, I stripped the coefficient values from the log using grep()
and gsub()
in R. Not the most efficient way of doing it but it gets the job done. You can modify this code as necessary for whatever you are pulling from the log file:
# This function takes the path to the log file. It then removes the Pearson's Coefficent, Manders M1/M2, and thresholded Manders M1/M2. It then gathers them into a table.
extract <- function(data){
dat <- read.table(data, header = FALSE, sep = "", fill = TRUE, stringsAsFactors = FALSE)
dat <- dat[ , "V1"]
coef <- dat[grep(pattern = "=", dat)]
coef <- as.numeric(gsub("r=|M1=|M2=", "",coef))
coef <- split(coef, ceiling(seq_along(coef)/5))
coef <- do.call(rbind.data.frame, coef)
names(coef) <- c("r", "M1", "M2", "M1(T)", "M2(T)")
coef <- cbind(Value = c("Red/Blue", "Red/Green", "Blue/Green"), coef)
return(coef)
}
Note that coef <- as.numeric(gsub("r=|M1=|M2=", "",coef))
will need to be modified depending on what you are extracting from the log file. Same goes for coef <- split(coef, ceiling(seq_along(coef)/5))
- change the 5 to the number of things being reported by the log file.
# Now just split the table into a list for each of the different analysis combinations fed into JACoP. Here I assume you set the output of the extract function to "dat".
output <- split(dat, dat$Value)
The output is a list of all the analysis values for each image fed into the macro divided by the different JACoP color channels analyzed. For example:
$`Red/Blue`
Value r M1 M2 M1(T) M2(T)
Red/Blue 0.743871077 0.395698602 0.963246489 0.513951407 0.700130944
Red/Blue 0.460021089 0.605613993 0.456788982 0.125648321 0.424468211
Red/Blue 0.967115553 0.357528694 0.767577893 0.073250688 0.720399867
$`Red/Green`
Value r M1 M2 M1(T) M2(T)
Red/Green 0.79367778 0.36556424 0.722980958 0.487698812 0.381559727
Red/Green 0.262211518 0.063695185 0.653330753 0.276610328 0.132548249
Red/Green 0.483240639 0.348516661 0.961846834 0.832706515 0.356203613
$`Blue/Green`
Value r M1 M2 M1(T) M2(T)
Blue/Green 0.549159913 0.834823152 0.389143503 0.655878106 0.446664812
Blue/Green 0.144388419 0.844781823 0.534304211 0.79041495 0.844326066
Blue/Green 0.805481028 0.344139017 0.490682901 0.246814106 0.641006611