DrBurlao

Gel Intensity Normalization and Band Analysis

May 21st, 2023
85
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.40 KB | Science | 0 0
  1. // Wait for the gel image to open
  2. waitForImage();
  3.  
  4. // Display a message and prompt the user to draw a rectangle around the gel
  5. waitForUser("Draw a rectangle around the gel");
  6.  
  7. // Get the ROI (Region of Interest) of the gel
  8. getSelectionBounds(x, y, width, height);
  9.  
  10. // Check if a valid ROI has been selected
  11. if (width > 0 && height > 0) {
  12.   // Select the ROI
  13.   makeRectangle(x, y, width, height);
  14.  
  15.   // Calculate the mean intensity value within the ROI
  16.   run("Measure");
  17.  
  18.   // Get the mean intensity value
  19.   var mean = getResult("Mean", 0);
  20.  
  21.   // Check if the mean intensity value is valid
  22.   if (mean > 0) {
  23.     // Normalize the image by dividing it by the mean intensity value
  24.     run("Divide...", "value=" + mean);
  25.  
  26.     // Enhance the contrast and display the normalized image
  27.     run("Enhance Contrast", "saturated=0.35");
  28.  
  29.     // Create a copy of the original gel image
  30.     selectWindow("Original gel");
  31.     run("Duplicate...", "title=Original_gel");
  32.  
  33.     // Display a message and prompt the user to select the bands of interest
  34.     waitForUser("Use the 'Rectangular' tool to select the bands of interest");
  35.  
  36.     // Create a list to store the coordinates of the selected bands
  37.     var bandCoords = [];
  38.  
  39.     // Loop for the user to select multiple bands of interest
  40.     while (true) {
  41.       // Wait for the user to select a band of interest
  42.       waitForSelection("rectangle");
  43.  
  44.       // Get the coordinates of the selection
  45.       getSelectionBounds(x_band, y_band, width_band, height_band);
  46.  
  47.       // Check if a valid band has been selected
  48.       if (width_band > 0 && height_band > 0) {
  49.         // Store the coordinates of the selected band in the list
  50.         bandCoords.push([x_band, y_band, width_band, height_band]);
  51.  
  52.         // Draw the lines selected by the user on the graph
  53.         setForegroundColor(255, 0, 0); // Red color
  54.         drawLine(x_band, y_band + height_band / 2, x_band + width_band, y_band + height_band / 2);
  55.         drawLine(x_band, y_band + height_band / 2 - 1, x_band + width_band, y_band + height_band / 2 - 1);
  56.         drawLine(x_band, y_band + height_band / 2 + 1, x_band + width_band, y_band + height_band / 2 + 1);
  57.       } else {
  58.         // No valid band selected, end the selection process
  59.         if (bandCoords.length === 0) {
  60.           showError("Error: No bands of interest have been selected.");
  61.         }
  62.         break;
  63.       }
  64.     }
  65.  
  66.     // Check if bands of interest have been selected
  67.     if (bandCoords.length > 0) {
  68.       // Create a plot with the normalized intensity values
  69.       newPlot("Normalized Intensity Profile", "Position", "Intensity");
  70.  
  71.       // Process each selected band
  72.       for (var i = 0; i < bandCoords.length; i++) {
  73.         var band = bandCoords[i];
  74.         var x_band = band[0];
  75.         var y_band = band[1];
  76.         var width_band = band[2];
  77.         var height_band = band[3];
  78.  
  79.         // Select the band of interest
  80.         makeRectangle(x_band, y_band, width_band, height_band);
  81.  
  82.         // Calculate the normalized intensity profile in the selected band
  83.         run("Plot Profile");
  84.  
  85.         // Get the normalized intensity values
  86.         var intensityArray = newArray(width_band);
  87.         getProfile(intensityArray);
  88.  
  89.         // Add the normalized intensity values to the plot
  90.         addValues("Normalized Intensity Profile", newArray(width_band), intensityArray);
  91.       }
  92.  
  93.       // Save the original gel image
  94.       selectWindow("Original_gel");
  95.       var originalGelPath = getPath("Save");
  96.       if (originalGelPath) {
  97.         saveAs("Tiff", originalGelPath);
  98.       } else {
  99.         showError("Error: No valid location has been selected to save the original gel image.");
  100.       }
  101.  
  102.       // Save the plot
  103.       selectWindow("Normalized Intensity Profile");
  104.       var graphPath = getPath("Save");
  105.       if (graphPath) {
  106.         saveAs("PNG", graphPath);
  107.       } else {
  108.         showError("Error: No valid location has been selected to save the plot.");
  109.       }
  110.  
  111.       // Save the normalized gel image
  112.       selectWindow("Normalized gel");
  113.       var normalizedGelPath = getPath("Save");
  114.       if (normalizedGelPath) {
  115.         saveAs("Tiff", normalizedGelPath);
  116.       } else {
  117.         showError("Error: No valid location has been selected to save the normalized gel image.");
  118.       }
  119.  
  120.       // Display a success message with the saved files
  121.       showSuccess("Process completed. The original gel image, normalized gel image, and plot have been saved.");
  122.  
  123.     } else {
  124.       showError("Error: No valid bands of interest have been selected.");
  125.     }
  126.   } else {
  127.     showError("Error: The mean intensity value is zero or negative. The image cannot be normalized.");
  128.   }
  129. } else {
  130.   showError("Error: No valid region of interest has been selected.");
  131. }
  132.  
  133. // Function to display an error message
  134. function showError(message) {
  135.   showMessage("Error", message, "Error");
  136. }
  137.  
  138. // Function to display a success message
  139. function showSuccess(message) {
  140.   showMessage("Success", message, "Information");
  141. }
  142.  
  143. // Generic function to display a message
  144. function showMessage(title, message, type) {
  145.   IJ.showMessage(title, message, type);
  146. }
  147.  
  148. // Function to prompt the user for a location and file name to save
  149. function getPath(action) {
  150.   var dialog = FileDialog(action);
  151.   dialog.setVisible(true);
  152.   var selectedPath = dialog.getDirectory() + dialog.getFileName();
  153.   return selectedPath !== "nullnull" ? selectedPath : null;
  154. }
  155.  
Advertisement
Comments
  • DrBurlao
    2 years
    Comment was deleted
  • DrBurlao
    2 years
    # text 1.38 KB | 0 0
    1. Wait for the gel image to open.
    2. Ask the user to draw a rectangle around the gel.
    3. Get the coordinates of the Region of Interest (ROI) of the gel.
    4. Check if a valid ROI has been selected.
    5. If a valid ROI has been selected, perform the following actions:
    6. a. Select the ROI in the image.
    7. b. Calculate the mean intensity value within the ROI.
    8. c. Check if the mean intensity value is valid.
    9. d. If the mean intensity value is valid, normalize the image by dividing it by the mean value.
    10. e. Enhance the contrast of the normalized image and display it.
    11. f. Create a copy of the original gel image.
    12. g. Ask the user to select the bands of interest in the normalized gel.
    13. h. Store the coordinates of the selected bands.
    14. i. Display the selected bands on the graph.
    15. j. Calculate and display the normalized intensity profile for each selected band.
    16. k. Save the original gel image, the graph, and the normalized image to files.
    17. l. Display a success message indicating that the files have been saved.
    18.  
    19. If a valid ROI has not been selected, display an error message.
    20. If the mean intensity value is zero or negative, display an error message.
    21. If no valid bands of interest have been selected, display an error message.
    22.  
    23. In summary, this program automates the processing of gel images, normalizes the intensity of the image, allows the user to select and analyze bands of interest, and saves the results to files.
Add Comment
Please, Sign In to add comment