View Javadoc
1   package com.github.celldynamics.quimp.plugin.protanalysis;
2   
3   import java.awt.Point;
4   import java.awt.event.ActionEvent;
5   import java.util.Arrays;
6   import java.util.List;
7   import java.util.regex.Matcher;
8   import java.util.regex.Pattern;
9   
10  import com.github.celldynamics.quimp.QParamsQconf;
11  import com.github.celldynamics.quimp.filesystem.QconfLoader;
12  import com.github.celldynamics.quimp.plugin.protanalysis.Prot_Analysis.PointHashSet;
13  import com.github.celldynamics.quimp.plugin.qanalysis.STmap;
14  
15  import ij.gui.PointRoi;
16  import ij.gui.Roi;
17  import ij.plugin.frame.RoiManager;
18  
19  /**
20   * Action for ROI to/from transfer.
21   * 
22   * @author p.baniukiewicz
23   *
24   */
25  @SuppressWarnings("serial")
26  public class ActionFromRoi extends ProtAnalysisAbstractAction {
27  
28    /**
29     * Create action.
30     * 
31     * @param name name
32     * @param desc tooltip
33     * @param ui reference to gui manager
34     */
35    public ActionFromRoi(String name, String desc, ProtAnalysisUi ui) {
36      super(name, desc, ui);
37    }
38  
39    /*
40     * (non-Javadoc)
41     * 
42     * @see
43     * com.github.celldynamics.quimp.plugin.protanalysis.ProtAnalysisAbstractAction#actionPerformed(
44     * java.awt.event.ActionEvent)
45     */
46    @Override
47    public void actionPerformed(ActionEvent e) {
48      int modifiers = e.getModifiers();
49      PointHashSet points = ui.getModel().selected;
50      points.clear(); // Here we clear points!
51      RoiManager rm = RoiManager.getRoiManager();
52      QconfLoader qconfLoader = ui.getModel().getQconfLoader();
53      STmap[] stMap = ((QParamsQconf) qconfLoader.getQp()).getLoadedDataContainer().getQState();
54      List<Roi> rois = Arrays.asList(rm.getRoisAsArray());
55      int cellNo = 0;
56  
57      if ((modifiers & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) {
58        cellNo = options.selActiveCellMap.getValue();
59        logger.debug("CTRL detected. Import all to current cell: " + cellNo);
60      }
61      for (Roi roi : rois) { // over ROIs in RoiManager
62        if (!(roi instanceof PointRoi)) {
63          logger.warn("ROI " + roi.getName() + " is not PointRoi");
64        }
65        // if NO ctrl get cell number from each ROI
66        if ((modifiers & ActionEvent.CTRL_MASK) != ActionEvent.CTRL_MASK) {
67          if (roi.getName().startsWith(ProtAnalysisOptions.roiPrefix)) { // our roi
68            cellNo = stripCellNo(roi.getName());
69            if (cellNo > stMap.length) {
70              continue;
71            }
72            if (cellNo < 0) {
73              logger.warn(
74                      "Can not obtain cell index from ROI name: " + roi.getName() + " Assume cell 0");
75              cellNo = 0;
76            }
77          }
78        }
79        // now each roi can contain some points (e.g. two point form one ROI object in ROI Manager)
80        for (Point p : roi) {
81          int tmpIndex = (int) p.getX();
82          int frame = (int) Math.round(
83                  p.getY() * (stMap[cellNo].getT() - 1) / stMap[cellNo].getVerticalResolution());
84          // get screen coordinates (ints)
85          // FIXME Why p.getY is larger than 400?? This is on tiffs as well.
86          // see com.github.celldynamics.quimp.plugin.qanalysis.STmap.resize(ImagePlus),
87          // (int) Math.ceil((double) res / (double) T)*T = vertRes
88          try {
89            int x = (int) Math.round(stMap[cellNo].getxMap()[frame][tmpIndex]);
90            int y = (int) Math.round(stMap[cellNo].getyMap()[frame][tmpIndex]);
91            logger.trace("name: " + roi.getName() + " point: " + p + " tmpIndex: " + tmpIndex
92                    + " frame: " + frame + " cell: " + cellNo + " x: " + x + " y: " + y);
93            PointCoordsimp/plugin/protanalysis/PointCoords.html#PointCoords">PointCoords point = new PointCoords(new Point(x, y), cellNo, frame);
94            points.addRaw(point); // add without overwriting frame number
95          } catch (ArrayIndexOutOfBoundsException ex) {
96            logger.error(
97                    "Point " + p + " can not be mapped to array (ArrayIndexOutOfBoundsException)");
98          }
99        }
100 
101     }
102     updateCurrentView();
103   }
104 
105   /**
106    * Extract cell number from ROI name.
107    * 
108    * @param name roi name in format set in {@link ActionToRoi}
109    * @return only cell number without additional roi number added by Fiji or -1 if number could not
110    *         be retrieved.
111    */
112   int stripCellNo(String name) {
113     // contain index and optional roi subnumber -0
114     String tmp = name.substring(ProtAnalysisOptions.roiPrefix.length());
115     Pattern p = Pattern.compile("^[\\d]*");
116     Matcher m = p.matcher(tmp);
117     if (m.find()) {
118       return Integer.parseInt(m.group(0));
119     }
120     return -1;
121   }
122 
123 }