fix_filenames_and_create_quickview.ijm 2.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/*
 * Macro template to process multiple images in a folder
 */

#@ File (label = "Input directory", style = "directory") input
#@ String (label = "File suffix", value = ".zip") suffix


setBatchMode(true);
processFolder(input);
print("Macro Done");
setBatchMode(false);
return;

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	
	fixFileNames(input);
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input,  list[i]);
	}
}

/*
 * fixes file names
 */
function fixFileNames(input)
{
	print("Fixing file names in: "+input);
	//get prefix
	imageSetArr = split(input, File.separator);
	imageSetName = imageSetArr[ lengthOf(imageSetArr)-1 ];
	//stupid windows bug needs removal of forward slashed
	imageSetName = replace(imageSetName, "/", "");
	//print(imageSetName);
	list2 = getFileList(input);
	list2 = Array.sort(list2);
	for (i = 0; i < list2.length; i++) {
		//print(list2[i]);
		//only handle files with correct sufix starting with fused_
		if(endsWith(list2[i], suffix) && startsWith(list2[i], "fused_") )
			{
				newName = input+File.separator+imageSetName+"_"+list2[i];
				
				File.rename(input+File.separator+list2[i], newName);
				print("renaming: "+list2[i]+" to "+ imageSetName+"_"+list2[i]);
				//now set metadata
				open(newName);
				setMetadata("Label", ""+imageSetName+"_"+list2[i]);
				//print("Label: "+ getMetadata("Label") );
				save(newName);
				close(imageSetName+"_"+list2[i]);
				
			}

	}
}

/*
 * has 2 functions
 * - create a max Z projection per File
 * - if there are 2 channels present, creates max projects of the channel composite
 */
function processFile(input, file) {
	if ( endsWith(file, "ch_1.tif.zip") )
	{
		imageSetArr = split(input, File.separator);
		imageSetName = imageSetArr[ lengthOf(imageSetArr)-1 ];
		imageSetName = replace(imageSetName, "/", "");

		compositeMaxFileName = ""+input+File.separator+imageSetName+"_MAX_Composite.jpg";
		if ( !File.exists(compositeMaxFileName))
		{
			open(input + File.separator + replace(file, "_ch_1", "_ch_0"));
			open(input + File.separator + file);

			imageTitles = getList("image.titles");
			
			run("Merge Channels...", "c4="+imageTitles[1]+" c5="+imageTitles[0]);
			resetMinAndMax();
			run("Z Project...", "projection=[Max Intensity]");
			resetMinAndMax();
			saveAs("Jpeg", compositeMaxFileName);
			close();
			close();
		}
		}

	targetFileName = ""+input + File.separator + file+".jpg";
	if ( File.exists(targetFileName))
		return;
	
	print("Processing: " + input + File.separator + file);	
	open(input + File.separator + file);
	run("Z Project...", "projection=[Max Intensity]");
	saveAs("Jpeg",targetFileName );
	close();
	close();
}