/
ModelLoad_Analyse.java
287 lines (247 loc) · 10.3 KB
/
ModelLoad_Analyse.java
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
/*
* To the extent possible under law, the ImageJ developers have waived
* all copyright and related or neighboring rights to this tutorial code.
*
* See the CC0 1.0 Universal license for details:
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package org.vcell.imagej.plugin;
import java.awt.Dimension;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
/*import org.eclipse.swt.widgets.Text; */
import org.scijava.command.ContextCommand;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.DialogPrompt.MessageType;
import org.scijava.ui.UIService;
import org.vcell.imagej.helper.VCellHelper;
import org.vcell.imagej.helper.VCellHelper.BasicStackDimensions;
import org.vcell.imagej.helper.VCellHelper.IJDataList;
import org.vcell.imagej.helper.VCellHelper.VCellModelSearchResults;
import net.imagej.ImageJ;
import net.imglib2.img.array.ArrayImg;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.img.basictypeaccess.array.DoubleArray;
import net.imglib2.type.numeric.real.DoubleType;
/**
* This example illustrates how to create an ImageJ {@link ContextCommand} plugin that uses VCellHelper.
* <p>
* You should replace the parameter fields with your own inputs and outputs,
* and replace the {@link run} method implementation with your own logic.
* </p>
* <p>
* To add VCellHelper to this project,
* rt-click on topmost tree element
* "imagej-plugin2"->Properties->Libraries tab->Add External Jars...->
* File Dialog->{EclipseVCellWorkspaceRootDir}/vcell/vcell-imagej-helper/target/vcell-imagej-helper-0.0.1-SNAPSHOT.jar.
* </p>
* <p>
* Once vcell-imagej-helper-0.0.1-SNAPSHOT.jar has been added to the Libraries tab open
* the small arrow to the left and select "Source Attachment"->Add/Edit->External Location->
* External File Dialog->{EclipseVCellWorkspaceRootDir}/vcell/vcell-imagej-helper/target/vcell-imagej-helper-0.0.1-SNAPSHOT-sources.jar.
* </p>
*/
@Plugin(type = ContextCommand.class, menuPath = "Plugins>ModelLoad_Analyse")
public class ModelLoad_Analyse extends ContextCommand {
// ModelLoad_Analyse(Toolkit.getDefaultToolkit().getImage(JFrame_Icon.class.getResource("/images/ccam")));
//
// Feel free to add more parameters here...
//
//"colreeze\",\"Monkeyflower_pigmentation_v2\",\"Pattern_formation\",\"WT\",null,null\n"+e.getMessage(), "Search failed", MessageType.ERROR_MESSAGE);
//displayProgressBar(false, null, null);
//These are the Parameters for our GUI which will be the model information for the generation of the output image
//The dimensions of our GUI is generated automatically according to the number of information
@Parameter
private UIService uiService;
@Parameter
private VCellHelper vcellHelper;
@Parameter
private String vCellUser = "colreeze";
@Parameter
private String vCellModel = "Monkeyflower_pigmentation_v2";
@Parameter
private String application = "Pattern_formation";
@Parameter
private String simulation = "WT";
@Parameter
private String variable = "A";
@Parameter
private int timePoint = 500;
/* @Parameter
private String imageName; */
/* @Parameter */
/*private String size; */
/**
* This main function serves for development purposes.
* It allows you to run the plugin immediately out of
* your integrated development environment (IDE).
*
* @param args whatever, it's ignored
* @throws Exception
*/
public static void main(final String... args) throws Exception {
// create the ImageJ application context with all available services
final ImageJ ij = new ImageJ();
ij.ui().showUI();
}
//These are for showing the progress in loading the result images after clicking OK on the GUI with the required information
private JDialog progressDialog = null;
private final Dimension dim = new Dimension(200,25); //these dimensions are for showing the loading of Image after clicking over OK on the information on the GUI
//@SuppressWarnings("serial") //omitted a serialVersionUID (not mandatory)
private final JProgressBar jProgressBar = new JProgressBar(0,100) //for showing the percentage of loading
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
// TODO Auto-generated method stub
return dim;
}
@Override
public Dimension getSize(Dimension rv) {
// TODO Auto-generated method stub
return dim;
}
};
private void displayProgressBar(boolean bShow,String message,String title) {
if(progressDialog == null) {
JFrame applicationFrame = (JFrame)uiService.getDefaultUI().getApplicationFrame();
progressDialog = new JDialog(applicationFrame,"Checking for VCell Client",false);
progressDialog.getContentPane().add(jProgressBar);
jProgressBar.setStringPainted(true);
progressDialog.pack();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if(!bShow) {
progressDialog.setVisible(false);
return;
}
jProgressBar.setValue(0);
progressDialog.setTitle(title);
jProgressBar.setString(message);
progressDialog.setVisible(true);
}
});
}
//This is for exception handling and pausing the execution of the thread for 50 miliseconds
private Hashtable<String,Thread> threadHash = new Hashtable<String,Thread>();
private void startJProgressThread0(String lastName,String newName) {
if(lastName != null && threadHash.get(lastName) != null) {
threadHash.get(lastName).interrupt();
while(threadHash.get(lastName) != null) {
try {
Thread.sleep(50); //pauses or stops the execution of current thread for 50 miliseconds
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
break;
}
}
}
if(newName == null) {
return;
}
final Thread progressThread = new Thread(new Runnable(){
@Override
public void run() {
final int[] progress = new int[] {1};
while(progressDialog.isVisible()) {
if(Thread.currentThread().isInterrupted()) {
break;
}
//this is an instance of an anonymous implementation of the Runnable interface and passing it to 'invokeLater', which will put it on a queue.
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jProgressBar.setValue(progress[0]);
}});
progress[0]++;
//and now a try catch exection to pause the thread for certain miliseconds
try {
Thread.sleep(500);
} catch (InterruptedException e) {
break;
}
}
threadHash.remove(Thread.currentThread().getName());
}});
// setDaemon() method of thread class is used to mark the thread either daemon thread or a user thread. Its life depends on the user threads i.e. when all user threads die, JVM terminates this thread automatically. It must be invoked before the thread is started.
threadHash.put(newName, progressThread);
progressThread.setName(newName);
progressThread.setDaemon(true);//So not block JVM exit
progressThread.start();
}
@Override
public void run() {
//This segment is for checking the activation of VCell
displayProgressBar(true, "Checking listening ports...", "Checking for VCell Client");
startJProgressThread0(null,"Check");
//this does the checking for the status of the VCell Port
//If the VCell Port is active
try {
//Find the port that a separately running VCell client is listening on
//
System.out.println("vcell service port="+vcellHelper.findVCellApiServerPort());
//uiService.getDisplayViewer(textDisplay).dispose();
displayProgressBar(false, null, null);
}
//If the VCell Port is inactive, Exception occurs
catch (Exception e) {
//e.printStackTrace();
displayProgressBar(false, null, null);
//uiService.getDisplayViewer(textDisplay).dispose();
uiService.showDialog("Activate VCell client ImageJ service\nTools->'Start Fiji (ImageJ) service'\n"+e.getMessage(), "Couldn't contact VCell client", MessageType.ERROR_MESSAGE);
return;
}
displayProgressBar(true, "Searching...", "Searching VCell Models");
startJProgressThread0("Check","Search");
//This segments takes the appropriate values and produces the desired results according to the model and the information realted to it
String theCacheKey = null;
VCellHelper.VCellModelSearch vcms = new VCellHelper.VCellModelSearch(VCellHelper.ModelType.bm,vCellUser,vCellModel,application,simulation,null,null);
try {
ArrayList<VCellModelSearchResults> vcmsr = vcellHelper.getSearchedModelSimCacheKey(false,vcms,null);
if(vcmsr.size() == 0) {
throw new Exception("No Results for search found");
}
theCacheKey = vcmsr.get(0).getCacheKey();
System.out.println("theCacheKey="+theCacheKey);
displayProgressBar(false, null, null);
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
uiService.showDialog("VCellHelper.ModelType.bm,vCellUser,vCellModel,application,simulation,null,null\n"+e.getMessage(), "Search failed", MessageType.ERROR_MESSAGE);
displayProgressBar(false, null, null);
}
displayProgressBar(true, "Loading Data...", "Loading Data");
startJProgressThread0("Search","getTimePointData");
//This segment is for displaying the results
try {
String var = variable;
int[] time = new int[] {timePoint};
IJDataList tpd = vcellHelper.getTimePointData(theCacheKey,var,VCellHelper.VARTYPE_POSTPROC.NotPostProcess,time,0);
double[] data = tpd.ijData[0].getDoubleData();
BasicStackDimensions bsd = tpd.ijData[0].stackInfo;
System.out.println(bsd.xsize+" "+bsd.ysize);
ArrayImg<DoubleType, DoubleArray> testimg = ArrayImgs.doubles( data, bsd.xsize,bsd.ysize);
uiService.show(testimg);
displayProgressBar(false, null, null);
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
uiService.showDialog("theCacheKey,var,VCellHelper.VARTYPE_POSTPROC.NotPostProcess,time,0\n"+e.getMessage(), "getTimePoint(...) failed", MessageType.ERROR_MESSAGE);
displayProgressBar(false, null, null);
}
startJProgressThread0("getTimePointData",null);
}
}