1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import ij.measure.*;
import ij.plugin.frame.*;
import ij.plugin.filter.PlugInFilter;
import ij.*;
import ij.io.*;
import ij.plugin.*;
import ij.plugin.filter.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import java.util.zip.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.InterruptedException;
import java.lang.System;
import java.lang.Math.*;
class XYPair
{
public int x, y;
@Override
public boolean equals(Object o)
{
XYPair p = (XYPair)o;
return p.x == x && p.y == y;
}
int hash(int x)
{
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x);
return x;
}
@Override
public int hashCode()
{
int seed = hash(x);
seed ^= hash(y) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
};
///Plugin class to load up an old 3B run.
///
///@ingroup gPlugin
public class ThreeBLoader implements PlugIn {
private void close(InputStream i)
{
//Not sure what to do here...
if(i != null)
{
try{
i.close();
}
catch(IOException ioe)
{
Toolkit.getDefaultToolkit().beep();
ij.IJ.showStatus("Error closing file: " + ioe.getMessage());
}
}
}
private class Results
{
public ArrayList<Spot> spots;
public Rectangle roi;
public int its;
}
Results parse_stream(InputStream in, boolean do_filtering) throws IOException
{
//Yay @ cargoculting
InputStreamReader d = new InputStreamReader(in);
BufferedReader r = new BufferedReader(d);
String line;
final ArrayList<Spot> spots = new ArrayList<Spot>();
int iterations=0;
Rectangle roi=null;
HashSet<XYPair> filter_hash = new HashSet<XYPair>();
while((line = r.readLine()) != null)
{
String tokens[] = line.split("\\p{Space}+");
if(tokens[0].equals("PIXELS"))
{
if((tokens.length - 1)%2 != 0)
throw new IOException("corrupt file (bad pixels line)");
for(int i=1; i < tokens.length; i+= 2)
{
int x, y;
try
{
x = Integer.parseInt(tokens[i+0]);
y = Integer.parseInt(tokens[i+1]);
}
catch(NumberFormatException nerr)
{
throw new IOException("corrupt file (bad pixel coordinate)");
}
if(roi == null)
roi = new Rectangle(x, y, 1, 1);
else
roi.add(x, y);
}
}
if(tokens[0].equals("FILTER"))
{
if((tokens.length - 1)%2 != 0)
throw new IOException("corrupt file (bad filter line)");
filter_hash.clear();
for(int i=1; i < tokens.length; i+= 2)
{
XYPair xy = new XYPair();
try
{
xy.x = Integer.parseInt(tokens[i+0]);
xy.y = Integer.parseInt(tokens[i+1]);
}
catch(NumberFormatException nerr)
{
throw new IOException("corrupt file (bad filter coordinate)");
}
filter_hash.add(xy);
}
//System.out.println(filter_hash);
}
if(tokens[0].matches("PASS[0-9]+:"))
{
iterations++;
if((tokens.length - 1)%4 != 0)
throw new IOException("corrupt file (pad pass line)");
for(int i=1; i < tokens.length; i+= 4)
{
Spot s = new Spot();
try
{
s.x = Double.parseDouble(tokens[i+2]);
s.y = Double.parseDouble(tokens[i+3]);
}
catch(NumberFormatException nerr)
{
throw new IOException("corrupt file (bad spot position)");
}
if(!do_filtering)
spots.add(s);
else
{
XYPair xy = new XYPair();
xy.x = (int)Math.floor(s.x);
xy.y = (int)Math.floor(s.y);
if(filter_hash.isEmpty() || filter_hash.contains(xy))
spots.add(s);
}
}
}
}
if(roi == null)
throw new IOException("corrupt file (no ROI)");
Results res = new Results();
res.spots = spots;
res.roi = roi;
res.its = iterations;
return res;
}
public void run(String arg) {
InputStream in = null;
String name;
double ps;
double ini_FWHM=100., ini_reconstruction_pixel_size=10.;
boolean show_control_panel=true;
boolean do_filtering=true;
try{
if(arg.equals("test"))
{
name = "test_data.txt";
try{
in = new GZIPInputStream(getClass().getClassLoader().getResourceAsStream("test_data.txt.gz"));
}
catch(IOException ioe)
{
Toolkit.getDefaultToolkit().beep();
ij.IJ.showStatus("Could not open test data: " + ioe.getMessage());
return;
}
ps=100;
}
else if(arg.equals("convert"))
{
OpenDialog o = new OpenDialog("Open 3B run...", null);
name = o.getFileName();
try{
in = new FileInputStream(o.getDirectory() + o.getFileName());
}
catch(java.io.FileNotFoundException ferr)
{
Toolkit.getDefaultToolkit().beep();
ij.IJ.showStatus("Error opening file: " + ferr.getMessage());
return;
}
GenericDialog g = new GenericDialog("Convert to text");
g.addNumericField("Pixel size", 100., 0, 5, "nm");
g.addMessage("Positions are either in pixels or nm");
g.addCheckbox("Positions in nm", true);
g.addCheckbox("Do Filtering", true);
g.addCheckbox("Show control panel", show_control_panel);
((Checkbox)g.getCheckboxes().get(2)).hide();
g.showDialog();
ps = g.getNextNumber();
boolean position_in_nm = g.getNextBoolean();
do_filtering = g.getNextBoolean();
show_control_panel=g.getNextBoolean();
Results res = parse_stream(in, do_filtering);
ResultsTable tab = new ResultsTable();
int row=0;
String unit;
if(position_in_nm)
unit = "(nm)";
else
unit = "(px)";
double mul = 1;
if(position_in_nm)
mul = ps;
for(int i=0; i < res.spots.size(); i++)
{
tab.incrementCounter();
tab.addValue("X " + unit, res.spots.get(i).x * mul);
tab.addValue("Y " + unit, res.spots.get(i).y * mul);
row++;
}
tab.showRowNumbers(false);
tab.show(name + " localisations");
//Yikes! This needs some refactoring.
return;
}
else
{
OpenDialog o = new OpenDialog("Open 3B run...", null);
name = o.getFileName();
try{
in = new FileInputStream(o.getDirectory() + o.getFileName());
}
catch(java.io.FileNotFoundException ferr)
{
Toolkit.getDefaultToolkit().beep();
ij.IJ.showStatus("Error opening file: " + ferr.getMessage());
return;
}
GenericDialog g = new GenericDialog("Pixel size");
g.addNumericField("Pixel size", 100., 0, 5, "nm");
g.addNumericField("FWHM (initial) ", ini_FWHM, 0, 5, "nm");
g.addNumericField("Reconstruction size (initial)", ini_reconstruction_pixel_size, 0, 5, "nm");
g.addCheckbox("Do Filtering", do_filtering);
g.addCheckbox("Show control panel", show_control_panel);
((Checkbox)g.getCheckboxes().get(1)).hide();
g.showDialog();
ps = g.getNextNumber();
ini_FWHM = g.getNextNumber();
ini_reconstruction_pixel_size=g.getNextNumber();
do_filtering = g.getNextBoolean();
show_control_panel=g.getNextBoolean();
}
final double pixel_size_in_nm_ = ps;
final Results res = parse_stream(in, do_filtering);
if(show_control_panel)
{
final String fname = name;
final Rectangle roi_ = res.roi;
final int its = res.its;
final double ini_FWHM_ = ini_FWHM;
final double ini_reconstruction_pixel_size_ = ini_reconstruction_pixel_size;
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
EControlPanel e = new EControlPanel(roi_, pixel_size_in_nm_, fname, null);
e.append(res.spots, res.its);
e.send_update_canvas_event();
e.send_status_text_message("Using " + fname + ": " + Integer.toString(its) + " iterations.");
e.set_reconstructed_pixel_size(ini_reconstruction_pixel_size_);
e.set_reconstruction_blur_fwhm(ini_FWHM_);
e.send_update_canvas_event();
}
}
);
}
else
{
//System.out.println("etf\n");
double zoom=ps / ini_reconstruction_pixel_size;
ImageProcessor export = Reconstruction.reconstruct(res.roi, zoom, ini_FWHM, ps, res.spots).duplicate();
ImagePlus export_win;
export_win = new ImagePlus(name + " reconstruction", export);
export_win.getCalibration().pixelWidth = ini_reconstruction_pixel_size;
export_win.getCalibration().pixelHeight = ini_reconstruction_pixel_size;
export_win.getCalibration().setXUnit("nm");
export_win.getCalibration().setYUnit("nm");
export_win.show();
}
}
catch(IOException another_ioe){
Toolkit.getDefaultToolkit().beep();
ij.IJ.showStatus("Error reading data: " + another_ioe.getMessage());
}
finally{
close(in);
}
}
}
|