replace substring of filename with string

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

replace substring of filename with string

OwenGwydion
Hi everyone,

I'm writing a macro for grid/collection stitching and I've hit a big, hard
brick wall.

I would like to replace the middle part of a string with a new piece of
string. I've defined the middle part of the string as the variable
"fullSiteName", and the string I would like to replace it with is "_s{ii}".


example filename =
Condition1_A01_s01_w1591FD421-91D8-454A-B944-B5D678E36C5D.tif

FirstPartOfFileName = Condition1_A01
fullSiteName = _s01
SecondPartOfFileName = _w1591FD421-91D8-454A-B944-B5D678E36C5D.tif (this is
unique to filename)


The problem I'm getting is that, though "fullSiteName" is replaced by the
new string, every file in my directory ends up with the same
SecondPartOfFileName as the first file it processes, and so the stitching
crashes.

I can't work out why this is. If I perform the same replace function on a
specified list (see example below), then it works fine. But if it is part of
my long macro with user-specified subfolders etc, it crashes.




//Working example:
list=
newArray("Condition1_A01_s01_w1591FD421-91D8-454A-B944-B5D678E36C5D.tif",
"Condition1_A01_s02_w145F67118-77DB-4613-A0E4-43F7F911C05E.tif",
"Condition1_A01_s03_w171933024-AE6E-4642-8BAB-6D78DADBEDFF.tif");
Array.print(list);
for(i=0; i<list.length; i++){
        indexSite=indexOf(list[i], "_s");
                                indexEndSite=indexOf(list[i], "_w");
                                fullSiteName=substring(list[i], indexSite, indexEndSite);
                                print(fullSiteName);
                                VariableName="_s{ii}";
                                genericFilename= replace(list[i], fullSiteName, VariableName); // this
replaces the full site name eg. "_s01" with "_s{ii}" to make it variable for
the stitching plugin
                                print("this is the generic fileName: " + genericFilename);
}





I've provided a section of my longer macro. In order to test it out, you
will need to make a directory called "newDir". Then a subdirectory eg.
"Well_A01_Channel_1". Fill that directory with three files with the
filenames: Condition1_A01_s01_w1591FD421-91D8-454A-B944-B5D678E36C5D.tif",
"Condition1_A01_s02_w145F67118-77DB-4613-A0E4-43F7F911C05E.tif",
"Condition1_A01_s03_w171933024-AE6E-4642-8BAB-6D78DADBEDFF.tif"




//Problem macro:
newDir=getDirectory("specify the newDir directory");
// Creating a list of the Well+Channel subfolders
        newDirlist=getFileList(newDir);
        wellChannelList=newArray();
                for(j=0; j<newDirlist.length;
j++){wellChannelList=Array.concat(wellChannelList, newDirlist[j]);}
                Array.print(wellChannelList);


// Choose which Well+Channel subfolder you want to process for tiled
stitching
        checkSelection();

        function checkSelection(){
                Dialog.create("Prepare to start stitching files");
                Dialog.addMessage("Choose which Well+Channel folder you want to stitch, or
select 'Exit macro' when finished");
                Dialog.addChoice("Folder to stitch", wellChannelList); //Choose
which channel you want to stitch
                Dialog.addCheckbox("Exit macro", false); //Opt to exit macro
                Dialog.show();
                Exit=Dialog.getCheckbox();
                Choice=Dialog.getChoice();
                tempDir=newDir+Choice;
                File.makeDirectory(tempDir); //Creates new directory based on
user choice called tempDir
                File.isDirectory(tempDir);
                tempList=getFileList(tempDir);
                Array.print(tempList);
               
                for(i=0; i<tempList.length; i++){

                        // identifies the full site name and replaces with variable {ii}
                        if(endsWith(tempList[i],".tif") || endsWith(tempList[i], ".TIF")){
                                indexSite=indexOf(tempList[i], "_s");
                                indexEndSite=indexOf(tempList[i], "_w");
                                fullSiteName=substring(tempList[i], indexSite, indexEndSite);
                                print(fullSiteName);
                                VariableName="_s{ii}";
                                genericFilename= replace(tempList[i], fullSiteName, VariableName); //
this replaces the full site name eg. "_s01" with "_s{ii}" to make it
variable for the stitching plugin
                                print("this is the generic fileName: " + genericFilename);
                                }


                                        // run grid/collection stitching
                                        if(Exit==false){
                                                        print("Prepare to stitch: " +Choice);
                                                        run("Grid/Collection stitching", "type=[Grid: row-by-row]
order=[Right & Down                ] grid_size_x=3 grid_size_y=1
tile_overlap=10 first_file_index_i=1 directory=[&tempDir]
file_names=[&genericFilename] output_textfile_name=TileConfiguration.txt
fusion_method=[Linear Blending] regression_threshold=0.30
max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50
compute_overlap computation_parameters=[Save memory (but be slower)]
image_output=[Fuse and display]");
                                                        saveAs("tiff", tempDir + "Stitched_image" + ".tif");
                                                        checkSelection();   //loop to provide option for stitching
another channel, or exit macro
                                                        }
                                                        else {
                                                        exit("You have exited the macro")
                                                        }
                                        }
}



Sorry for the convoluted message. I'm not sure why the long macro fails, and
its driving me insane. If somebody out there can help me out, I would be
really grateful!

Thanks!!

Owen




--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: replace substring of filename with string

MChapman
Owen,
If I understand what is going on in your macro correctly, I think you may be looking at the Grid Collection/Stitching plugin wrong. 
The plugin doesn't want all of the images to have the {ii} nomenclature but to actually have the numbers where the "i"s are located.  The rest of the name besides the {ii} needs to be the exact same in order for the plugin to work.  When you call the plugin is when you need to have the "{ii}" in the filename so that it knows the structure of what to look for.
Have you gotten the grid collection/stitching to work with the image names that you are trying to convert to manually yet, or are you trying to do both steps (creating the names and testing the automation of the process in the macro) together?
Mike

    On Friday, February 23, 2018 1:40 PM, OwenGwydion <[hidden email]> wrote:
 

 Hi everyone,

I'm writing a macro for grid/collection stitching and I've hit a big, hard
brick wall.

I would like to replace the middle part of a string with a new piece of
string. I've defined the middle part of the string as the variable
"fullSiteName", and the string I would like to replace it with is "_s{ii}".


example filename =
Condition1_A01_s01_w1591FD421-91D8-454A-B944-B5D678E36C5D.tif

FirstPartOfFileName = Condition1_A01
fullSiteName = _s01
SecondPartOfFileName = _w1591FD421-91D8-454A-B944-B5D678E36C5D.tif (this is
unique to filename)


The problem I'm getting is that, though "fullSiteName" is replaced by the
new string, every file in my directory ends up with the same
SecondPartOfFileName as the first file it processes, and so the stitching
crashes.

I can't work out why this is. If I perform the same replace function on a
specified list (see example below), then it works fine. But if it is part of
my long macro with user-specified subfolders etc, it crashes.




//Working example:
list=
newArray("Condition1_A01_s01_w1591FD421-91D8-454A-B944-B5D678E36C5D.tif",
"Condition1_A01_s02_w145F67118-77DB-4613-A0E4-43F7F911C05E.tif",
"Condition1_A01_s03_w171933024-AE6E-4642-8BAB-6D78DADBEDFF.tif");
Array.print(list);
for(i=0; i<list.length; i++){
    indexSite=indexOf(list[i], "_s");
                indexEndSite=indexOf(list[i], "_w");
                fullSiteName=substring(list[i], indexSite, indexEndSite);
                print(fullSiteName);
                VariableName="_s{ii}";
                genericFilename= replace(list[i], fullSiteName, VariableName);            // this
replaces the full site name eg. "_s01" with "_s{ii}" to make it variable for
the stitching plugin
                print("this is the generic fileName: " + genericFilename);   
}





I've provided a section of my longer macro. In order to test it out, you
will need to make a directory called "newDir". Then a subdirectory eg.
"Well_A01_Channel_1". Fill that directory with three files with the
filenames: Condition1_A01_s01_w1591FD421-91D8-454A-B944-B5D678E36C5D.tif",
"Condition1_A01_s02_w145F67118-77DB-4613-A0E4-43F7F911C05E.tif",
"Condition1_A01_s03_w171933024-AE6E-4642-8BAB-6D78DADBEDFF.tif"




//Problem macro:
newDir=getDirectory("specify the newDir directory");
// Creating a list of the Well+Channel subfolders
    newDirlist=getFileList(newDir);   
    wellChannelList=newArray();
        for(j=0; j<newDirlist.length;
j++){wellChannelList=Array.concat(wellChannelList, newDirlist[j]);}
        Array.print(wellChannelList);


// Choose which Well+Channel subfolder you want to process for tiled
stitching
    checkSelection();

    function checkSelection(){
        Dialog.create("Prepare to start stitching files");
        Dialog.addMessage("Choose which Well+Channel folder you want to stitch, or
select 'Exit macro' when finished");
        Dialog.addChoice("Folder to stitch", wellChannelList);                                //Choose
which channel you want to stitch
        Dialog.addCheckbox("Exit macro", false);                                             //Opt to exit macro
        Dialog.show();
        Exit=Dialog.getCheckbox();
        Choice=Dialog.getChoice();
        tempDir=newDir+Choice;
        File.makeDirectory(tempDir);                                                        //Creates new directory based on
user choice called tempDir
        File.isDirectory(tempDir);   
        tempList=getFileList(tempDir);
        Array.print(tempList);
       
        for(i=0; i<tempList.length; i++){

            // identifies the full site name and replaces with variable {ii}
            if(endsWith(tempList[i],".tif") || endsWith(tempList[i], ".TIF")){
                indexSite=indexOf(tempList[i], "_s");
                indexEndSite=indexOf(tempList[i], "_w");
                fullSiteName=substring(tempList[i], indexSite, indexEndSite);
                print(fullSiteName);
                VariableName="_s{ii}";
                genericFilename= replace(tempList[i], fullSiteName, VariableName);            //
this replaces the full site name eg. "_s01" with "_s{ii}" to make it
variable for the stitching plugin
                print("this is the generic fileName: " + genericFilename);   
                }


                    // run grid/collection stitching
                    if(Exit==false){
                            print("Prepare to stitch: " +Choice);
                            run("Grid/Collection stitching", "type=[Grid: row-by-row]
order=[Right & Down                ] grid_size_x=3 grid_size_y=1
tile_overlap=10 first_file_index_i=1 directory=[&tempDir]
file_names=[&genericFilename] output_textfile_name=TileConfiguration.txt
fusion_method=[Linear Blending] regression_threshold=0.30
max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50
compute_overlap computation_parameters=[Save memory (but be slower)]
image_output=[Fuse and display]");
                            saveAs("tiff", tempDir + "Stitched_image" + ".tif");
                            checkSelection();                                              //loop to provide option for stitching
another channel, or exit macro
                            }               
                            else {
                            exit("You have exited the macro")
                            }
                    }
}



Sorry for the convoluted message. I'm not sure why the long macro fails, and
its driving me insane. If somebody out there can help me out, I would be
really grateful!

Thanks!!

Owen




--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html


   

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: replace substring of filename with string

OwenGwydion
Hi Mike,

Thanks for your response! I think I understand now. So my real problem then
is that the second part of the file name (after the "_s01") changes and is
unique to each file. I'm able to do the manual method for grid/collection
stitching if I remove the unique string at the end of each file, but Ideally
I'd like to find a way around this.

I need to specify a variable filename in the plugin call eg:

run("Grid/Collection stitching"......file_names = [&genericFileName]

Can you specify a variable within another variable?

I would like to make the secondPartOfFilename a variable within the
&genericFileName variable? Don't know if that makes sense or is even the
best way around this.

Cheers,

Owen
////////////////////////




--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: replace substring of filename with string

MChapman
Hey Owen,

I do not know of a way to make it a callable list inside of grid collection/stitching, but stephan preibisch would know more about how the algorithm works and what the input structure looks like.

If the reason that you are wanting the filenames to be consistent with the original is because you are using the TileConfiguration.registered.txt file that comes out of a run, then I would manipulate the filename strings in the .txt file after running the plugin.  

Alternatively, if you know the grid spacing on the mosaic that you collected, you could just create your own TileConfiguration.txt file and then you can have whatever file names you would like.  Then instead of selecting “Grid:row by row” you would select “Positions from file”

Hope this helps!
Mike


> On Feb 25, 2018, at 4:51 AM, OwenGwydion <[hidden email]> wrote:
>
> Hi Mike,
>
> Thanks for your response! I think I understand now. So my real problem then
> is that the second part of the file name (after the "_s01") changes and is
> unique to each file. I'm able to do the manual method for grid/collection
> stitching if I remove the unique string at the end of each file, but Ideally
> I'd like to find a way around this.
>
> I need to specify a variable filename in the plugin call eg:
>
> run("Grid/Collection stitching"......file_names = [&genericFileName]
>
> Can you specify a variable within another variable?
>
> I would like to make the secondPartOfFilename a variable within the
> &genericFileName variable? Don't know if that makes sense or is even the
> best way around this.
>
> Cheers,
>
> Owen
> ////////////////////////
>
>
>
>
> --
> Sent from: http://imagej.1557.x6.nabble.com/
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: replace substring of filename with string

OwenGwydion
Hi Mike,

Thanks for your help in pointing me in the right direction.

I ended up renaming the files to show a consistent filename to the plugin.
It now runs smoothly.

For interest I've copied it below:



        function checkSelection(){
                Dialog.create("Prepare to start stitching files");
                Dialog.addMessage("Choose which Well+Channel folder you want to stitch, or
select 'Exit macro' when finished");
                Dialog.addChoice("Folder to stitch", wellChannelList); //Choose
which channel you want to stitch
                Dialog.addCheckbox("Exit macro", false); //Opt to exit macro
                Dialog.show();
                Exit=Dialog.getCheckbox();
                Choice=Dialog.getChoice();
                tempDir=newDir+Choice;
                print("the TempDir is: "+ tempDir);
                ChoiceIndex=indexOf(tempDir, "Well");
                ChoiceNameEnd=lengthOf(tempDir);
                ChoiceName=substring(tempDir, ChoiceIndex, ChoiceNameEnd-1);
//Extracts the name of the chosen file to be used for saving stitched image
                print("the choiceName is: "+ ChoiceName);
                File.makeDirectory(tempDir); //Creates new directory based on
user choice called tempDir
                File.isDirectory(tempDir); //Returns 1 if tempDir is a true
directory
                tempList=getFileList(tempDir);
                Array.print(tempList);

               
                        for(i=0; i<tempList.length; i++){

                                // identifies the full site name and replaces with variable {ii}
                                if(endsWith(tempList[i],".tif") || endsWith(tempList[i], ".TIF") &&
(matches(tempList[i], ".*Stitched.*") != 1)){
                                        currentName=tempDir+tempList[i];
                                        indexSiteX=indexOf(tempList[i], "_s");
                                        indexEndSiteX=indexOf(tempList[i], "_w");
                                        firstPart=substring(tempList[i], 0, indexEndSiteX+3);
                                        shortenedName=firstPart + ".tif";
                                        print("the ShortenedName is: " + shortenedName);
                                        newTempName=tempDir+shortenedName;
                                                tempSiteName=substring(tempList[i], indexSite, indexEndSite);
                                                genericFileName=replace(shortenedName, tempSiteName, "_s{ii}");
                                                File.rename(currentName, newTempName);
                                                print("the generic File Name is: " + genericFileName);
                                }}

                                // run grid/collection stitching
                                if(Exit==false){
                                                print("Prepare to stitch: " +Choice);
                                                run("Grid/Collection stitching", "type=[Grid: row-by-row] order=[Right
& Down                ] grid_size_x=Xtile grid_size_y=Ytile tile_overlap=10
first_file_index_i=1 directory=[&tempDir] file_names=[&genericFileName]
output_textfile_name=TileConfiguration.txt fusion_method=[Linear Blending]
regression_threshold=0.30 max/avg_displacement_threshold=2.50
absolute_displacement_threshold=3.50 compute_overlap
computation_parameters=[Save memory (but be slower)] image_output=[Fuse and
display]");
                                                saveAs("tiff", newDir + ChoiceName + "_Stitched_image" + ".tif");
                                                checkSelection();   //loop to provide option for stitching
another channel, or exit macro
                                                }
                                                else {
                                                exit("You have exited the macro")
                                                }
                                       
}

Cheers,

Owen

///////////////////////////////////////////




--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html