Moving every 'n' files into a unique folder (using a sorted array of filenames)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Moving every 'n' files into a unique folder (using a sorted array of filenames)

michaelrw
This post was updated on .
I begin with a directory that contains loose image files, all are sequentially named, in the order they were captured. In this example, I took 28 grayscale images total (7 series, 4 wavelengths each [TRITC, GFP, DAPI, Cy5] = 28 total).
I am writing this script to be able to move every 'n' images into a new, unique directory for each series. The value of 'n' depends on how many channels were imaged (as notted, i used 4 stains.. next time I may only use 2, so i need this to be a variable).
Since each series is in order (I always capture my sets in the same order ... tritc > gfp > dapi > cy5), I have created an Array with all of the files (filelist = getFileList(dir)), in the hope that I can iterate over it, pulling out every 'n' images and putting into a new folder (already created and titled sequentially).
Here is the start of my code:

////DIRECTORY WITH ALL IMAGES THAT WERE CAPTURED FROM THE SAME MICROSCOPE SLIDE////
     dir = getDirectory("Select the image directory of the slide you want to process");

     filelist = getFileList(dir);                   //ALL IMAGES

     sortedlist = Array.sort(filelist);          //SORTED

////HOW MANY COLOR CHANNELS FOR EACH SET OF CAPTURES////
     channels_int = getNumber("How many channels/images do you have in each series",0);

     num_series = sortedlist.length / channels_int;          //TOTAL NUMBER OF "SERIES"

//CREATING MY FOLDERS -- series1, series2, series3, series4... etc.
     for (i=1; i<num_series+1; i++) {
             File.makeDirectory(dir+File.separator+"series" + i);




This is where I begin to get lost conceptually. After creating my new directories (I tried creating directories and moving every n'th image from the array all in the same loop, but im confused on how to iterate with two different increments)
I want to pull the first 4 files in the array (elements 0-3) and put them into the <i>series1 folder. The next 4 (array elements 4-7) will go into series2. My obvious first thought was some form of iterative loop. The problem is, I can't think of a way to do it, while also keeping everything as variables (because the number of total series and the number of channels/images per series will change from experiment to experiment.
I could hard code it by adding a loop like:



for (i=0; i<channels_int; i++) {
        if (i <= 3)
                File.rename(dir + sortedlist[i],dir + "series1" + File.separator + sortedlist[i]);
        else if (i <= 7 && > 3)
                File.rename(dir + sortedlist[i],dir + "series2" + File.separator + sortedlist[i]);
        else if (i <= 11 && > 7)
                File.rename(dir + sortedlist[i],dir + "series3" + File.separator + sortedlist[i]);
        else if (i <= 15 && > 11)
                File.rename(dir + sortedlist[i],dir + "series4" + File.separator + sortedlist[i]);
        else if (i <= 19 && > 15)
                File.rename(dir + sortedlist[i],dir + "series5" + File.separator + sortedlist[i]);
        else if (i <= 23 && > 19)
                File.rename(dir + sortedlist[i],dir + "series6" + File.separator + sortedlist[i]);
        else (i <= 27 && > 23)
                File.rename(dir + sortedlist[i],dir + "series7" + File.separator + sortedlist[i]);
        }


But this doesn't take into account the variable number of series or the variable number of channels/images in each series folder. I am having a hard time conceptualizing how to implement this while accounting for these variables. With respect to the number of channels per series, I could just write a separate function for each potential number of channels (since you can only stain for a finite number of cell features at one time. But the number of series is highly variable. It really depends on how many sets I feel like capturing, based on how good the cells look, what kind of mood I'm in, etc. So, ideally, I can keep using these variables I have already defined, and use some clever iteration to solve my problem.

Thanks for any help.