5. Loops and conditions


Home | << Previous Page

Learning Objectives

  • If/else booleans
  • Array data types
  • Loops

if/else

if/else conditional statements enable the control of a program based on a yes/no question.

There are several typical queries:

  • Equal to (==)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
  • Not equal to (!=)

E.g.

if (1>2){

print("one is greater than two")}

else{

print("One is not greater than two");

}


Exercise 1:
  1. a=d2s(random*10,0); will produce a random number between 0 and 10. Write a statement to print the word "Big" if a is greater than 5, and "small" if it is less than or equal to 5.
  2. You can build statements upon each other to acheive greter complexity. Can you modify your code so that a value equal to 5 will now print "Medium", while still printing "Big" and "small" for bigger and smaller values?

Arrays

Arrays are lists of values.

These could include values from the column of a table, a list of file names, or anything you like.

It is often helpful to store data in this form so it can be iterated through or used to make a table.

Values in a list can be called using their index position (which begins at 0).

E.g.

a=newArray(2,4,6,8);

print(a[0]);//Prints the first value in the array i.e. 2

2


It is possible to perform a range of functions upon arrays, including concatenation (sticking arrays together) with Array.concat(array1,array2);


Exercise 2:
  1. Make two arrays of values, assign these to variables
  2. Concatentate these together to make a new variable
  3. Put an array into a new table using the Table.showArrays function

Loops

"For Loops" enable iteration through data.

Below is an example of an array of numbers being looped through, and their values printed out in a string:

a=newArray(1,3,5,9);//a new array of four numbers

for (i = 0; i < a.length; i++) {//a.length defines the number of iterations as the number of values in "a"

value=a[i];//i will range from 0-3, to get the values of a at each position must be extracted

print("Number "+value);//prints the value in a string

}


Exercise3:

Next we're going to write a script to loop through a set of images, segment the nuclei (channel 1) and quantify the nuclear area. For assistance, see solution here.

It can be useful to experiment with small chunks of code separately, then gradually assemble into a bigger script.

  1. Use the getDirectory("Choose Directory") command to select the "Image Batch" directory in the downloaded course material. Set the output of this to a variable e.g. dir1.
  2. Use getFileList(dir1) to make a list of the files. Set a variable to save this file list to.
  3. Create a loop to go through the file names iteratively.
    • It can be useful to just print out the file name to check things are working as you intend
  4. With each iteration, open the image file.
    • You'll need to join the directory and file name strings to form a complete path to the image
    • Then open the image with open(path)
  5. Next separate the channels with run("Stack to Images");
    • Before separating, it can be useful to first rename the image something general using e.g. rename("Original"); Each of your images will have different names, but if they're renamed at the start then they can share code to find and manipulate them.
  6. Select the DAPI channel and segment (by thresholding etc, or Stardist works well with these images)
  7. Measure the area of each nucleus.
    • If you're using a binary mask, this can be done with "Set Measurements" and "Analyze Particles", if using a labelmap you need Plugins > MorpholibJ > Analyze > Analyze Regions
  8. Measurements are outputted to a table. Extract these values as an array, using Table.getColumn(columnName), where your "columnname" is whatever it appears as in the table
  9. Store these values in variable.
  10. Add an empty array areas_array=newArray() at the start of your script to which you can concatenate the values for each image, so it can ultimately be added to a table of total results
  11. Concatenate your values to the areas_array array
  12. Create an array for the Label values too, as with the Areas. This will allow youto record which areas relate to which cell
  13. It is also useful to know which image is being analysed. To do this you need to make another array repeating the image name, which is the same length of as the recorded values so it can fill an appropriate number of rows in the ultimate results table.
  14. This requires another loop, repeatedly concatenating the name into an array.
    • Note that as this is a loop within a loop, you must change the name of the variable used for iteration (e.g. from "i" to "j")
  15. Close images close("*");
  16. Close tables close("Label-Morphometry"). NB. The title of this table may vary depending on your method used
  17. Once the loops are complete, compose a results table from the compiled array, using the Table.showArrays command. Look up associated infor for assistance.


Home | << Previous Page