File: SampleAlignments.java

package info (click to toggle)
proalign 0.603-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 492 kB
  • sloc: java: 8,673; sh: 27; makefile: 4
file content (446 lines) | stat: -rw-r--r-- 12,492 bytes parent folder | download | duplicates (5)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
 * Title:        ProAlign<p>
 * Description:  <p>
 * Copyright:    Copyright (c) Ari Loytynoja<p>
 * License:      GNU GENERAL PUBLIC LICENSE<p>
 * @see          http://www.gnu.org/copyleft/gpl.html
 * Company:      ULB<p>
 * @author Ari Loytynoja
 * @version 1.0
 */
package proalign;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.io.File;
import java.io.IOException;

public class SampleAlignments extends JFrame {

    JTextArea textOutput;
    JTextField textNumber;
    JCheckBox boxResult;
    JCheckBox boxFolder;
    JCheckBox boxNexus;
    JCheckBox boxFasta;
    JScrollPane sp;
    JScrollBar sb;

    String resultPath = new String("sample_result.csv");
    String folderPath = new String("");
    String nexusPath = new String("");
    String fastaPath = new String("");
    int numRun;

    boolean writeResults = true;
    boolean writeAlignments = false;
    boolean writeNexus = false;
    boolean writeFasta = false;
    boolean isRunning = true;

    OutFile resultOut;

    int height = 300;
    int width = 600;

    ProAlign pa;
    ResultWindow rw;
    SampleAlignments sa;

    SampleAlignments(ResultWindow rw) {

	setTitle("Sample alignments");   
	this.rw = rw;
	this.pa = rw.pa;
	sa = this;

	Font font = new Font(ProAlign.paFontName, Font.PLAIN, ProAlign.paFontSize);
	try {
	    resultOut = new OutFile(resultPath);
	} catch(IOException ioe) { }

	JButton buttonResult = new JButton("Select");
	buttonResult.setFont(font);
	buttonResult.setActionCommand("result");
	buttonResult.addActionListener(new ButtonL());
	JButton buttonFolder = new JButton("Select");
	buttonFolder.setFont(font);
	buttonFolder.setActionCommand("folder");
	buttonFolder.addActionListener(new ButtonL());
	JButton buttonNexus = new JButton("Select");
	buttonNexus.setFont(font);
	buttonNexus.setActionCommand("nexus");
	buttonNexus.addActionListener(new ButtonL());
	JButton buttonFasta = new JButton("Select");
	buttonFasta.setFont(font);
	buttonFasta.setActionCommand("fasta");
	buttonFasta.addActionListener(new ButtonL());
	JButton buttonStart = new JButton("Start");
	buttonStart.setFont(font);
	buttonStart.setActionCommand("start");
	buttonStart.addActionListener(new ButtonL());
	JButton buttonStop = new JButton("Stop");
	buttonStop.setFont(font);
	buttonStop.setActionCommand("stop");
	buttonStop.addActionListener(new ButtonL());
	JButton buttonClose = new JButton("Close");
	buttonClose.setFont(font);
	buttonClose.setActionCommand("close");
	buttonClose.addActionListener(new ButtonL());

	JLabel labelHeader = new JLabel("   Sampling parameters");
	labelHeader.setFont(font);
	JLabel labelResult = new JLabel("Change file");
	labelResult.setFont(font);
	JLabel labelFolder = new JLabel("Change folder");
	labelFolder.setFont(font);
	JLabel labelNexus = new JLabel("Change folder");
	labelNexus.setFont(font);
	JLabel labelFasta = new JLabel("Change folder");
	labelFasta.setFont(font);
	JLabel labelNumber = new JLabel("Permutations");
	labelNumber.setFont(font);
	JLabel labelSample = new JLabel("Traceback route");
	labelSample.setFont(font);

	textNumber = new JTextField("100");
	textNumber.setFont(font);

	boxResult = new JCheckBox("Write results");
	boxResult.setActionCommand("result");
	boxResult.setFont(font);
	if(writeResults) {
	    boxResult.setSelected(true);
	} else {
	    boxResult.setSelected(false);
	}

	boxFolder = new JCheckBox("Save ."+ProAlign.fileExt);
	boxFolder.setActionCommand("folder");
	boxFolder.setFont(font);
	boxFolder.setSelected(false);

	boxNexus = new JCheckBox("Save .nex");
	boxNexus.setActionCommand("nexus");
	boxNexus.setFont(font);
	boxNexus.setSelected(false);

	boxFasta = new JCheckBox("Save .pir");
	boxFasta.setActionCommand("fasta");
	boxFasta.setFont(font);
	boxFasta.setSelected(false);

	JRadioButton traceBest = new JRadioButton("select best");
	traceBest.setFont(font);
	traceBest.setActionCommand("best");
	JRadioButton traceSample = new JRadioButton("sample");
	traceSample.setFont(font);
	traceSample.setActionCommand("sample");
	if(ProAlign.trackBest) {
	    traceBest.setSelected(true);
	} else { 
	   traceSample.setSelected(true);
	}

	CheckBoxListener chboxL = new CheckBoxListener();
	boxResult.addItemListener(chboxL);
	boxFolder.addItemListener(chboxL);
	boxNexus.addItemListener(chboxL);
	boxFasta.addItemListener(chboxL);

	JPanel fullPanel = new JPanel();
	fullPanel.setLayout(new GridLayout(1,2,15,15));
	JPanel halfPanel = new JPanel();
	halfPanel.setLayout(new GridLayout(10,1,5,5));

	halfPanel.add(labelHeader);

	JPanel pnl = new JPanel();
	pnl.setLayout(new GridLayout(1,3,5,5));
	pnl.add(new JLabel());
	pnl.add(labelNumber);
	pnl.add(textNumber);
	halfPanel.add(pnl);

	pnl = new JPanel();
	pnl.setLayout(new GridLayout(1,3,5,5));
	pnl.add(boxResult);
	pnl.add(labelResult);
	pnl.add(buttonResult);
	halfPanel.add(pnl);

	pnl = new JPanel();
	pnl.setLayout(new GridLayout(1,3,5,5));
	pnl.add(boxFolder);
	pnl.add(labelFolder);
	pnl.add(buttonFolder);
	halfPanel.add(pnl);

	pnl = new JPanel();
	pnl.setLayout(new GridLayout(1,3,5,5));
	pnl.add(boxNexus);
	pnl.add(labelNexus);
	pnl.add(buttonNexus);
	halfPanel.add(pnl);

	pnl = new JPanel();
	pnl.setLayout(new GridLayout(1,3,5,5));
	pnl.add(boxFasta);
	pnl.add(labelFasta);
	pnl.add(buttonFasta);
	halfPanel.add(pnl);

	ButtonGroup traceBack = new ButtonGroup();
        traceBack.add(traceBest);
        traceBack.add(traceSample);

	RadioListener radioL = new RadioListener();
        traceBest.addActionListener(radioL);
        traceSample.addActionListener(radioL);

	pnl = new JPanel();
	pnl.setLayout(new GridLayout(1,2,5,5));
	pnl.add(labelSample);
	pnl.add(traceBest);
	halfPanel.add(pnl);

	pnl = new JPanel();
	pnl.setLayout(new GridLayout(1,2,5,5));
	pnl.add(new JPanel());
	pnl.add(traceSample);
	halfPanel.add(pnl);

	halfPanel.add(new JPanel());

	pnl = new JPanel();
	pnl.setLayout(new GridLayout(1,3,5,5));
	pnl.add(buttonStart);
	pnl.add(buttonStop);
	pnl.add(buttonClose);
	halfPanel.add(pnl);

	fullPanel.add(halfPanel);


	textOutput = new JTextArea();
	textOutput.setFont(font);

	sp = new JScrollPane(textOutput,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
			      JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	sb = sp.getVerticalScrollBar();

	fullPanel.add(sp);

	Container cp = getContentPane();
	cp.add(fullPanel);

	addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
		    dispose();
		}
	    });
    }
     
    public void write(String str) {
	if (str != "") {
	    textOutput.append("  " + str + "  \n");
	    sb.setValue(sb.getMaximum());
	}
    }

    class ButtonL implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    
	    JButton target = (JButton)e.getSource();
	    String actionCommand = target.getActionCommand();
	    
	    if(actionCommand.equals("result")) {
		
		OpenFileChooser opf = 
		    new OpenFileChooser(SampleAlignments.this,"Save",false);
		String filepath = opf.openFile();
	 	if(!filepath.equals("")) {
		    resultOut.close();
		    try {
			resultOut = new OutFile(filepath);
		    } catch(IOException ioe) { }
		    sa.write("result file is: "+filepath);
		    resultPath = filepath;
		    UserSettings user = new UserSettings(); 
		    String[] userdata = user.readSettings();
		    userdata[0] = new File(filepath).getParent();
                    user.writeSettings(userdata);
		}
		
	    } else if(actionCommand.equals("folder")) {

		OpenFolderChooser opf = 
		    new OpenFolderChooser(SampleAlignments.this,"Select");
		String filepath = opf.openFile();
	 	if(!filepath.equals("")) {
		    folderPath = filepath;
		    sa.write("alignment folder is: "+new File(folderPath).getAbsolutePath());
		    UserSettings user = new UserSettings(); 
		    String[] userdata = user.readSettings();
		    userdata[0] = new File(filepath).getParent();
                    user.writeSettings(userdata);
		}
		
	    } else if(actionCommand.equals("nexus")) {

		OpenFolderChooser opf = 
		    new OpenFolderChooser(SampleAlignments.this,"Select");
		String filepath = opf.openFile();
	 	if(!filepath.equals("")) {
		    nexusPath = filepath;
		    sa.write("nexus folder is: "+new File(nexusPath).getAbsolutePath());
		    UserSettings user = new UserSettings(); 
		    String[] userdata = user.readSettings();
		    userdata[0] = new File(filepath).getParent();
                    user.writeSettings(userdata);
		}

	    } else if(actionCommand.equals("fasta")) {

		OpenFolderChooser opf = 
		    new OpenFolderChooser(SampleAlignments.this,"Select");
		String filepath = opf.openFile();
	 	if(!filepath.equals("")) {
		    fastaPath = filepath;
		    sa.write("fasta folder is: "+new File(fastaPath).getAbsolutePath());
		    UserSettings user = new UserSettings(); 
		    String[] userdata = user.readSettings();
		    userdata[0] = new File(filepath).getParent();
                    user.writeSettings(userdata);
		}

	    } else if(actionCommand.equals("start")) {

		while(true) {
		    try{
			int maxRun = new Integer(textNumber.getText()).intValue();
			if(maxRun>0) {
			    numRun = maxRun;
			} else {
			    String text = new String("\n   Illegal value!  \n");
			    OpenDialog od = new OpenDialog(SampleAlignments.this);
			    od.showDialog("Error!", text); 
			    break;
			}
			if(ProAlign.DEBUG) {
			    ProAlign.log("SampleAlignments");
			    ProAlign.log("resultPath="+resultPath+","+writeResults);
			    ProAlign.log("folderPath="+folderPath+","+writeAlignments);
			    ProAlign.log("nexusPath="+nexusPath+","+writeNexus);
			    ProAlign.log("fastaPath="+fastaPath+","+writeFasta);
			    ProAlign.log("numRun="+numRun);
			    ProAlign.log("trackBest="+ProAlign.trackBest);
			    ProAlign.log.flush();
			}
		    } catch(NumberFormatException nfe) {
			String text = new String("\n   Illegal value!  \n");
			OpenDialog od = new OpenDialog(SampleAlignments.this);
			od.showDialog("Error!", text); 
			break;
		    }
		    
		    SampleLoop sl = new SampleLoop(SampleAlignments.this);
		    break;
		}

	    } else if(actionCommand.equals("stop")) {

		isRunning = false;

	    } else if(actionCommand.equals("close")) {

		dispatchEvent(new WindowEvent(SampleAlignments.this,
					      WindowEvent.WINDOW_CLOSING));	    
	    }
	}
    }


    class CheckBoxListener implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
	    
	    JCheckBox target = (JCheckBox)e.getItem();
	    String actionCommand = target.getActionCommand();

	    if(actionCommand.equals("result")) {

		if (e.getStateChange() == ItemEvent.DESELECTED) {
		    writeResults = false;
		    sa.write("writing results stopped!");
		} else {
		    writeResults = true;
		    sa.write("writing results started.");
		}
		
	    } else if(actionCommand.equals("folder")) {

		if (e.getStateChange() == ItemEvent.DESELECTED) {
		    writeAlignments = false;
		    sa.write("sampled alignments are not saved!");
		} else {
		    writeAlignments = true;
		    sa.write("all sampled alignments are saved.");
		}

	    } else if(actionCommand.equals("nexus")) {
		
		if (e.getStateChange() == ItemEvent.DESELECTED) {
		    writeNexus = false;
		    sa.write("nexus files are not saved!");
		} else {
		    writeNexus = true;
		    sa.write("alignments are saved as nexus files.");
		}

	    } else if(actionCommand.equals("fasta")) {
		
		if (e.getStateChange() == ItemEvent.DESELECTED) {
		    writeFasta = false;
		    sa.write("fasta files are not saved!");
		} else {
		    writeFasta = true;
		    sa.write("alignments are saved as fasta files.");
		}
	    }
	}
    }

    class RadioListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	    String actionCommand = e.getActionCommand();

	    if(actionCommand.equals("sample")) {
		
		ProAlign.trackBest = false;
		
	    } else if(actionCommand.equals("best")) {

		ProAlign.trackBest = true;
	    }
	}
    }
}