import java.awt.*; import java.awt.event.*; import java.awt.Container.*; import java.io.*; import ij.io.*; import java.awt.GridLayout.*; import ij.plugin.frame.*; import ij.*; import java.util.*; import ij.process.*; import ij.gui.*; import java.lang.Math; import ij.measure.*; import ij.plugin.*; import ij.plugin.frame.RoiManager; import ij.measure.Calibration; import ij.measure.ResultsTable; import javax.swing.*; import javax.swing.filechooser.*; /** FRET ROI - designed to calculate FRET from acceptor bleaching within regions of interest Written by Ben Corry July 2008 */ public class FRET_ROI extends PlugInFrame implements ActionListener { Panel panel; int previousID; static Frame instance; boolean useThreshold = true; double donorThreshold=0.0; double acceptorThreshold=0.0; public FRET_ROI() { super("FRET ROI"); if (instance!=null) { instance.toFront(); return; } instance = this; addKeyListener(IJ.getInstance()); setLayout(new FlowLayout()); panel = new Panel(); panel.setLayout(new GridLayout(0, 1, 5, 5)); panel.setBackground(new Color(240,240,220)); panel.add(new Label("FRET ROI ")); addButton("Open Files"); addButton("Build Stack"); addButton("LUT Fire"); addButton("Start ROI Manager"); // addButton("Settings"); addButton("FRET Calculation"); addButton("Mini Calculation"); addButton("Single ROI Calculation"); addButton("Help"); panel.add(new Label(" ")); add(panel); pack(); GUI.center(this); setVisible(true); } void addButton(String label) { Button b = new Button(label); b.addActionListener(this); b.addKeyListener(IJ.getInstance()); panel.add(b); } public void actionPerformed(ActionEvent e) { String label = e.getActionCommand(); if (label==null) return; new FRETRunner(label); } public void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID()==WindowEvent.WINDOW_CLOSING) { instance = null; } } } class FRETRunner extends Thread { private String command; FRETRunner(String command) { super(command); this.command = command; setPriority(Math.max(getPriority()-2, MIN_PRIORITY)); start(); } public void run() { try {runCommand(command);} catch(OutOfMemoryError e) { IJ.outOfMemory(command); } catch(Exception e) { CharArrayWriter caw = new CharArrayWriter(); PrintWriter pw = new PrintWriter(caw); e.printStackTrace(pw); IJ.write(caw.toString()); IJ.showStatus(""); } } void runCommand(String command) { double donorThreshold=0.0; IJ.showStatus(command + "..."); if (command.equals("Open Files")) macroOpen(); else if (command.equals("Build Stack")) macroStack(); else if (command.equals("LUT Fire")) IJ.run("Fire"); else if (command.equals("Start ROI Manager")) IJ.run("ROI Manager..."); // else if (command.equals("Settings")){ // GenericDialog gd = new GenericDialog("Settings"); // gd.addCheckbox("Use Donor Threshold values", false); // gd.addNumericField("Donor Threshold",0,0 ); // gd.addNumericField("Acceptor Threshold",10,0 ); // gd.showDialog(); // useThreshold = gd.getNextBoolean(); // donorThreshold = gd.getNextNumber(); // acceptorThreshold = (int)gd.getNextNumber(); // } else if (command.equals("FRET Calculation")){ int previousID=0; ImagePlus imp = WindowManager.getCurrentImage(); imp.unlock(); if (imp==null) { IJ.beep(); IJ.showStatus("No image"); IJ.showMessage("Open an image to use buttons"); previousID = 0; return; } if (!imp.lock()) {previousID = 0; return;} ImageProcessor ip = imp.getProcessor(); int id = imp.getID(); if (id!=previousID) ip.snapshot(); previousID = id; ResultsTable rt = ResultsTable.getResultsTable(); rt.reset(); rt.incrementCounter(); RoiManager roim; roim=RoiManager.getInstance(); if (roim==null) { IJ.showMessage("Must have the ROI Manager open"); } int nr=roim.getCount(); ImageStack stack = imp.getStack(); if (stack==null) { IJ.showMessage("Must have an image stack"); } int ns = stack.getSize(); GenericDialog gd = new GenericDialog("Settings"); gd.addNumericField("Donor Threshold",0,0 ); gd.showDialog(); donorThreshold = gd.getNextNumber(); int count[][] = new int[ns][nr]; double m[][] = new double[ns][nr]; double sum[][] = new double[ns][nr]; double normd[] = new double[3]; double norma[] = new double[3]; for(int j=0; j2 && i==0) { if (ipnow.getPixelValue(x+r.x, y+r.y)>donorThreshold) { count[j][i]++; sum[j][i] += ipnow.getPixelValue(x+r.x, y+r.y); } } else { count[j][i]++; sum[j][i] += ipnow.getPixelValue(x+r.x, y+r.y); } } } } m[j][i]=sum[j][i]/count[j][i]; } } for (int j=0; j<3; j++){ norma[j] = Math.max((m[j][1]-m[j][2])/(m[0][1]-m[0][2]),0); normd[j] = (m[j+3][0]-m[j+3][2])/(m[3][0]-m[3][2]); } double chbleach = Math.max(1 - (normd[2]/normd[1]),0.0); double corrD1 = normd[1]*(1.0+chbleach); double corrD2 = corrD1 + ((corrD1-1)*(norma[1]/(1-norma[1]))); double fret = Math.max(1-(1/corrD2),0.0); for (int j=0; j<3; j++){ rt.addValue("Donor:",m[j+3][0]); rt.addValue("Acceptor:",m[j][1]); rt.addValue("Donor bg:",m[j+3][2]); rt.addValue("Acceptor bg:",m[j][2]); rt.addValue("Norm Donor:",normd[j]); rt.addValue("Norm Acceptor:",norma[j]); rt.addValue("chbleach:",chbleach); rt.addValue("corrD1:",corrD1); rt.addValue("corrD2:",corrD2); rt.addValue("fret:",fret); if (j<2) rt.incrementCounter(); } rt.show("Results"); } else if (command.equals("Mini Calculation")){ int previousID=0; ImagePlus imp = WindowManager.getCurrentImage(); imp.unlock(); if (imp==null) { IJ.beep(); IJ.showStatus("No image"); IJ.showMessage("Open an image to use buttons"); previousID = 0; return; } if (!imp.lock()) {previousID = 0; return;} ImageProcessor ip = imp.getProcessor(); int id = imp.getID(); if (id!=previousID) ip.snapshot(); previousID = id; ResultsTable rt = ResultsTable.getResultsTable(); rt.reset(); rt.incrementCounter(); RoiManager roim; roim=RoiManager.getInstance(); if (roim==null) { IJ.showMessage("Must have the ROI Manager open"); } int nr=roim.getCount(); ImageStack stack = imp.getStack(); if (stack==null) { IJ.showMessage("Must have an image stack"); } int ns = stack.getSize(); GenericDialog gd = new GenericDialog("Settings"); gd.addNumericField("Donor Threshold",0,0 ); gd.showDialog(); donorThreshold = gd.getNextNumber(); int count[][] = new int[ns][nr]; double m[][] = new double[ns][nr]; double sum[][] = new double[ns][nr]; for(int j=0; j2 &&i==0) { if (ipnow.getPixelValue(x+r.x, y+r.y)>donorThreshold) { count[j][i]++; sum[j][i] += ipnow.getPixelValue(x+r.x, y+r.y); } } else { count[j][i]++; sum[j][i] += ipnow.getPixelValue(x+r.x, y+r.y); } } } } m[j][i]=sum[j][i]/count[j][i]; } } for (int j=0; j<3; j++){ rt.addValue("Donor:",m[j+3][0]); rt.addValue("Acceptor:",m[j][1]); rt.addValue("Donor bg:",m[j+3][2]); rt.addValue("Acceptor bg:",m[j][2]); if (j<2) rt.incrementCounter(); } rt.show("Results"); } else if (command.equals("Single ROI Calculation")){ int previousID=0; ImagePlus imp = WindowManager.getCurrentImage(); imp.unlock(); if (imp==null) { IJ.beep(); IJ.showStatus("No image"); IJ.showMessage("Open an image to use buttons"); previousID = 0; return; } if (!imp.lock()) {previousID = 0; return;} ImageProcessor ip = imp.getProcessor(); int id = imp.getID(); if (id!=previousID) ip.snapshot(); previousID = id; ResultsTable rt = ResultsTable.getResultsTable(); rt.reset(); rt.incrementCounter(); int nr=1; ImageStack stack = imp.getStack(); if (stack==null) { IJ.showMessage("Must have an image stack"); } int ns = stack.getSize(); GenericDialog gd = new GenericDialog("Settings"); gd.addNumericField("Donor Threshold",0,0 ); gd.showDialog(); donorThreshold = gd.getNextNumber(); int count[][] = new int[ns][nr]; double m[][] = new double[ns][nr]; double sum[][] = new double[ns][nr]; for(int j=0; j2 &&i==0) { if (ipnow.getPixelValue(x+r.x, y+r.y)>donorThreshold) { count[j][i]++; sum[j][i] += ipnow.getPixelValue(x+r.x, y+r.y); } } else { count[j][i]++; sum[j][i] += ipnow.getPixelValue(x+r.x, y+r.y); } } } } m[j][i]=sum[j][i]/count[j][i]; } } for (int j=0; j<6; j++){ rt.addValue("Mean:",m[j][0]); if (j<5) rt.incrementCounter(); } rt.show("Results"); } else if (command.equals("Help")) macroHelp(); IJ.showStatus(""); } void macroStack() { ImagePlus imp; ImageProcessor ip; int nbrFenetre; int[] listFenetre; Rectangle[] listRoi; int maxWidth, maxHeight; boolean rgbStack = false; nbrFenetre = WindowManager.getWindowCount(); listFenetre = new int[nbrFenetre]; listRoi = new Rectangle[nbrFenetre]; listFenetre = WindowManager.getIDList(); maxWidth=0; maxHeight=0; if (listFenetre==null) {IJ.noImage(); return;} // Create an array with ROIs attributes, search the biggest width and height // and check image types boolean allSameType =true; boolean is16or32BitImage = false; int typeOfFirstImage = WindowManager.getImage(listFenetre[0]).getType(); for(int i=0; imaxWidth) {maxWidth=listRoi[i].width;}; if (listRoi[i].height>maxHeight) {maxHeight=listRoi[i].height;}; //if (maxWidth>ip.getWidth()) {maxWidth=ip.getWidth();}; //if (maxHeight>ip.getHeight()) {maxHeight=ip.getHeight();}; } if (!allSameType && is16or32BitImage) { if (!IJ.showMessageWithCancel("Stack Builder", "Images are not all the same type and some are\n"+ "16 or 32 bits. All the images will be converted\n"+ "to 8-bits or RGB to create the stack.")) return; } // Resizes the ROIs according to the biggest size found for(int i=0; i=ip.getWidth()) listRoi[i].x =ip.getWidth()-listRoi[i].width; if (listRoi[i].y+listRoi[i].height>=ip.getHeight()) listRoi[i].y =ip.getHeight()-listRoi[i].height; ip.setRoi(listRoi[i]); ip=ip.crop(); if (ip.getWidth()!=maxWidth || ip.getHeight()!=maxHeight) { ImageProcessor ip2 = ip.createProcessor(maxWidth, maxHeight); ip2.setColor(Toolbar.getForegroundColor()); ip2.fill(); ip2.insert(ip, (maxWidth-ip.getWidth())/2, (maxHeight-ip.getHeight())/2); ip = ip2; } if (rgbStack) ip = ip.convertToRGB(); else if (!allSameType) ip = ip.convertToByte(true); //Rectangle r = ip.getRoi(); //IJ.write(i+" "+r.width+" "+r.height); newStack.addSlice(imp.getTitle(),ip); imp.close(); } ImagePlus newImage = new ImagePlus("the stack", newStack); newImage.show(); } // end of run void macroOpen() { File dir=null; JFileChooser fc = null; try {fc = new JFileChooser();} catch (Throwable e) {IJ.error("This plugin requires Java 2 or Swing."); return;} fc.setMultiSelectionEnabled(true); if (dir==null) { String sdir = OpenDialog.getDefaultDirectory(); if (sdir!=null) dir = new File(sdir); } if (dir!=null) fc.setCurrentDirectory(dir); int returnVal = fc.showOpenDialog(IJ.getInstance()); if (returnVal!=JFileChooser.APPROVE_OPTION) return; File[] files = fc.getSelectedFiles(); if (files.length==0) { // getSelectedFiles does not work on some JVMs files = new File[1]; files[0] = fc.getSelectedFile(); } String path = fc.getCurrentDirectory().getPath()+Prefs.getFileSeparator(); dir = fc.getCurrentDirectory(); Opener opener = new Opener(); for (int i=0; i More -> Remove slice info)\n"+ " \n"); } }