Saving multiple logs to single file by a macro

imagej

#1

Hello,
I have a macro measuring colocalisation by JACoP plugin. The output is written to the log file. I’m trying to change the macro to enable batch processing, but the problem is consolidating the results. I want the logs to form columns in the excel sheet. It’s easy to save a single log, but I don’t know how to automatically save them all in a single file as columns. The output from JACoP lookes like this:

**************************************************
Image A: C1
Image B: C2

Pearson's Coefficient:
r=0.624

I tried to paste it to the results table, but I only managed to paste it as a row rather than column, separated by \n, and Excel couldn’t read the resulting file as table. For this, I used this code:

for (i=0; i<list.length; i++) {
	open(directory + list[i]);
	coloc();
	result=getInfo("log");
	setResult(1, i, result);
}

coloc() is my colocalisation function. I’m new to batch macro writing and any help would be very appreciated.


#2

IF there are only three essential rows (the two image names and Pearson’s coefficient), grab these as you did with getInfo and keep three strings that you build by concatenating results of each run and a tab character. At the end of your loop, add to each of the three strings a newline and print them to a file.


#3

Thanks. And how do I get just one line of the log by getInfo? I only know how to get the whole log.


#4

Good day!

Here is a demo macro that shows how to get the three items as a tab-delimited string:

print("**************************************************\nImage A: C1\nImage B: C2\n\nPearson's Coefficient:\nr=0.624");
dataStr = split( getInfo("log"), "\n" );
str = substring( dataStr[1], indexOf( dataStr[1], ":" ) + 2, lengthOf( dataStr[1] ) ) + "\t";
str += substring( dataStr[2], indexOf( dataStr[2], ":" ) + 2, lengthOf( dataStr[1] ) ) + "\t";
str += substring( dataStr[5], indexOf( dataStr[5], "=" ) + 1, lengthOf( dataStr[5] ) ) + "\n";
print("____________");
print( str );
exit();

HTH

Herbie


#5

Herbie beat me to it and his excellent and complete example yields strings with the data in rows, e.g. when colocalisation between im1 and im2, im3 and im4 and between im5 and im6 with coefficients r1, r2 and r3:

im1\tim2\tr1\n
im3\tim4\tr2\n
im5\tim6\tr3\n

It will give you enough information to split the individual log entries and build the output usint tabs (\t) and newlines (\n) for a column-wise output like

im1\tim3\tim5\n
im2\tim4\tim6\n
r1\t\r2\t\r3\n

Building on Herbie’s code:

str1="";str2="";str3="";

for (i=0;i<3;i++){
	i1=i*2+1;i2=i*2+2;
	print("\\Clear");
	print("**************************************************\nImage A: C"+i1+"\nImage B: C"+i2+"\n\nPearson's Coefficient:\nr=0.62"+i);
	dataStr = split( getInfo("log"), "\n" );
	str1 += substring( dataStr[1], indexOf( dataStr[1], ":" ) + 2, lengthOf( dataStr[1] ) ) + "\t";
	str2 += substring( dataStr[2], indexOf( dataStr[2], ":" ) + 2, lengthOf( dataStr[1] ) ) + "\t";
	str3 += substring( dataStr[5], indexOf( dataStr[5], "=" ) + 1, lengthOf( dataStr[5] ) ) + "\t";

}
str1+="\n";
str2+="\n";
str3+="\n";
print("____________");
print( str1 );
print( str2 );
print( str3 );exit();

#6

Thank you both very much, it works now. I never worked with strings before.
Since my coloc function actually measures colocalisations between all the channels in the picture, I needed to adapt your code a bit, and sue setResult rather than print to get the table. It works nicely (although the part that makes it work with up to 4 channels is a bit clumsy, but I didn’t need a general version since I don’t use it for more than 4 channels. Here is the final version, in case you (or anyone in the future) are interested.

print("\\Clear");
run("Clear Results");
directory=getDirectory("Choose a Directory");
setBatchMode(true);
list = getFileList(directory);
for (i=0; i<list.length; i++) {
	open(directory + list[i]);
	tit=getTitle();
	getDimensions(width, height, channels, slices, frames); // To get nubmer of channels to analyze
	if(channels>1) {
		measurements=1;
		if(channels>2) {
			measurements=3;
			if(channels>3) {
				measurements=6;
			}
		}
	}
	coloc();
	for (j=0; j<measurements; j++) {
		
		dataStr = split( getInfo("log"), "\n" );
		ch1 = substring( dataStr[6*j+1], indexOf( dataStr[6*j+1], ":" ) + 2, lengthOf( dataStr[6*j+1] ) );
		ch2 = substring( dataStr[6*j+2], indexOf( dataStr[6*j+2], ":" ) + 2, lengthOf( dataStr[6*j+2] ) );
		r = substring( dataStr[6*j+5], indexOf( dataStr[6*j+5], "=" ) + 1, lengthOf( dataStr[6*j+5] ) );
		setResult("Image", i, tit);
		setResult("Coloc "+ch1+" "+ch2, i, r);
	}
}
updateResults;
close("Log");

This way I get a table with names of the image and all the values for all the colocalisations in columns labelled by channels used for that particular calculation. It’s even better than I wanted.


#7

Also for future reference: the way to generalise this for n channels is to use an array of strings, created by ch=newArray(n);