Newer
Older
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/* WriteMenu.java
*
* created: Mon Jan 11 1999
*
* This file is part of Artemis
*
* Copyright (C) 1998,1999,2000 Genome Research Limited
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/WriteMenu.java,v 1.1 2004-06-09 09:48:00 tjc Exp $
**/
package uk.ac.sanger.artemis.components;
import uk.ac.sanger.artemis.*;
import uk.ac.sanger.artemis.sequence.*;
import uk.ac.sanger.artemis.io.Sequence;
import uk.ac.sanger.artemis.io.StreamSequence;
import uk.ac.sanger.artemis.io.FastaStreamSequence;
import uk.ac.sanger.artemis.io.RawStreamSequence;
import uk.ac.sanger.artemis.io.EmblStreamSequence;
import uk.ac.sanger.artemis.io.GenbankStreamSequence;
import uk.ac.sanger.artemis.io.StreamSequenceFactory;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
/**
* A menu of commands for writing out protein and bases.
*
* @author Kim Rutherford
* @version $Id: WriteMenu.java,v 1.1 2004-06-09 09:48:00 tjc Exp $
**/
public class WriteMenu extends SelectionMenu {
/**
* Create a new WriteMenu component.
* @param frame The JFrame that owns this JMenu.
* @param selection The Selection that the commands in the menu will
* operate on.
* @param entry_group The EntryGroup object to use when writing a sequence.
* @param menu_name The name of the new menu.
**/
public WriteMenu (final JFrame frame,
final Selection selection,
final EntryGroup entry_group,
final String menu_name) {
super (frame, menu_name, selection);
this.entry_group = entry_group;
final JMenuItem aa_item = new JMenuItem ("Amino Acids Of Selected Features");
aa_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeAminoAcids ();
}
});
add (aa_item);
final JMenuItem pir_item =
new JMenuItem ("PIR Database Of Selected Features");
pir_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writePIRDataBase ();
}
});
add (pir_item);
addSeparator ();
final JMenu bases_menu = new JMenu ("Bases Of Selection");
final JMenuItem raw_bases_item = new JMenuItem ("Raw Format");
raw_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeBasesOfSelection (StreamSequenceFactory.RAW_FORMAT);
}
});
bases_menu.add (raw_bases_item);
final JMenuItem fasta_bases_item = new JMenuItem ("FASTA Format");
fasta_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeBasesOfSelection (StreamSequenceFactory.FASTA_FORMAT);
}
});
bases_menu.add (fasta_bases_item);
final JMenuItem embl_bases_item = new JMenuItem ("EMBL Format");
embl_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeBasesOfSelection (StreamSequenceFactory.EMBL_FORMAT);
}
});
bases_menu.add (embl_bases_item);
final JMenuItem genbank_bases_item = new JMenuItem ("Genbank Format");
genbank_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeBasesOfSelection (StreamSequenceFactory.GENBANK_FORMAT);
}
});
bases_menu.add (genbank_bases_item);
add (bases_menu);
final JMenu upstream_bases_menu =
new JMenu ("Upstream Bases Of Selected Features");
final JMenuItem raw_upstream_bases_item = new JMenuItem ("Raw Format");
raw_upstream_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeUpstreamBases (StreamSequenceFactory.RAW_FORMAT);
}
});
upstream_bases_menu.add (raw_upstream_bases_item);
final JMenuItem fasta_upstream_bases_item = new JMenuItem ("FASTA Format");
fasta_upstream_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeUpstreamBases (StreamSequenceFactory.FASTA_FORMAT);
}
});
upstream_bases_menu.add (fasta_upstream_bases_item);
final JMenuItem embl_upstream_bases_item = new JMenuItem ("EMBL Format");
embl_upstream_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeUpstreamBases (StreamSequenceFactory.EMBL_FORMAT);
}
});
upstream_bases_menu.add (embl_upstream_bases_item);
final JMenuItem genbank_upstream_bases_item = new JMenuItem ("Genbank Format");
genbank_upstream_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeUpstreamBases (StreamSequenceFactory.GENBANK_FORMAT);
}
});
upstream_bases_menu.add (genbank_upstream_bases_item);
add (upstream_bases_menu);
final JMenu downstream_bases_menu =
new JMenu ("Downstream Bases Of Selected Features");
final JMenuItem raw_downstream_bases_item = new JMenuItem ("Raw Format");
raw_downstream_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeDownstreamBases (StreamSequenceFactory.RAW_FORMAT);
}
});
downstream_bases_menu.add (raw_downstream_bases_item);
final JMenuItem fasta_downstream_bases_item = new JMenuItem ("FASTA Format");
fasta_downstream_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeDownstreamBases (StreamSequenceFactory.FASTA_FORMAT);
}
});
downstream_bases_menu.add (fasta_downstream_bases_item);
final JMenuItem embl_downstream_bases_item = new JMenuItem ("EMBL Format");
embl_downstream_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeDownstreamBases (StreamSequenceFactory.EMBL_FORMAT);
}
});
downstream_bases_menu.add (embl_downstream_bases_item);
final JMenuItem genbank_downstream_bases_item =
new JMenuItem ("Genbank Format");
genbank_downstream_bases_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeDownstreamBases (StreamSequenceFactory.GENBANK_FORMAT);
}
});
downstream_bases_menu.add (genbank_downstream_bases_item);
add (downstream_bases_menu);
addSeparator ();
final JMenu write_all_bases_menu = new JMenu ("Write All Bases");
final JMenuItem write_raw_item = new JMenuItem ("Raw Format");
write_raw_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeAllSequence (StreamSequenceFactory.RAW_FORMAT);
}
});
write_all_bases_menu.add (write_raw_item);
final JMenuItem write_fasta_item = new JMenuItem ("FASTA Format");
write_fasta_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeAllSequence (StreamSequenceFactory.FASTA_FORMAT);
}
});
write_all_bases_menu.add (write_fasta_item);
final JMenuItem write_embl_item = new JMenuItem ("EMBL Format");
write_embl_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeAllSequence (StreamSequenceFactory.EMBL_FORMAT);
}
});
write_all_bases_menu.add (write_embl_item);
final JMenuItem write_genbank_item = new JMenuItem ("Genbank Format");
write_genbank_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeAllSequence (StreamSequenceFactory.GENBANK_FORMAT);
}
});
write_all_bases_menu.add (write_genbank_item);
add (write_all_bases_menu);
addSeparator ();
final JMenuItem codon_usage_item =
new JMenuItem ("Write Codon Usage of Selected Features");
codon_usage_item.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
writeCodonUsage ();
}
});
add (codon_usage_item);
}
/**
* Create a new WriteMenu component.
* @param frame The JFrame that owns this JMenu.
* @param selection The Selection that the commands in the menu will
* operate on.
* @param entry_group The EntryGroup object to use when writing a sequence.
**/
public WriteMenu (final JFrame frame,
final Selection selection,
final EntryGroup entry_group) {
this (frame, selection, entry_group, "Write");
}
/**
* Write a PIR database of the selected features to a file choosen by the
* user.
**/
private void writePIRDataBase () {
if (! checkForSelectionFeatures ()) {
return;
}
final File write_file =
getWriteFile ("Select a PIR output file name ...", "cosmid.pir");
if (write_file == null) {
return;
}
try {
final FileWriter writer = new FileWriter (write_file);
final FeatureVector features_to_write =
getSelection ().getAllFeatures ();
for (int i = 0 ; i < features_to_write.size () ; ++i) {
final Feature selection_feature = features_to_write.elementAt (i);
selection_feature.writePIROfFeature (writer);
}
writer.close ();
} catch (IOException e) {
new MessageDialog (getParentFrame (),
"error while writing: " + e.getMessage ());
}
}
/**
* Write the amino acid symbols of the selected features to a file choosen
* by the user.
**/
private void writeAminoAcids () {
if (! checkForSelectionFeatures ()) {
return;
}
final File write_file =
getWriteFile ("Select an output file name..", "amino_acids");
if (write_file == null) {
return;
}
try {
final FileWriter writer = new FileWriter (write_file);
final FeatureVector features_to_write =
getSelection ().getAllFeatures ();
for (int i = 0 ; i < features_to_write.size () ; ++i) {
final Feature selection_feature = features_to_write.elementAt (i);
selection_feature.writeAminoAcidsOfFeature (writer);
}
writer.close ();
} catch (IOException e) {
new MessageDialog (getParentFrame (),
"error while writing: " + e.getMessage ());
}
}
/**
* Write the bases of the selection to a file choosen by the user.
* @param output_type One of EMBL_FORMAT, RAW_FORMAT etc.
**/
private void writeBasesOfSelection (final int output_type) {
final MarkerRange marker_range = getSelection ().getMarkerRange ();
if (marker_range == null) {
writeFeatureBases (output_type);
} else {
final String selection_bases = Strand.markerRangeBases (marker_range);
writeBases (selection_bases,
"selected bases",
getSequenceFileName (output_type),
output_type);
}
}
/**
* Write the bases of the selected features to a file choosen by the user
* or show a message and return immediately if there are no selected
* features.
* @param sequence The sequence to be written.
* @param header The header line that will be used on those output formats
* that need it.
* @param default_output_filename The filename that is passed to
* getWriteFile ()
* @param output_type One of EMBL_FORMAT, RAW_FORMAT etc.
**/
private void writeBases (final String sequence,
final String header,
final String default_output_filename,
final int output_type) {
final File write_file =
getWriteFile ("Select an output file name ...", default_output_filename);
if (write_file == null) {
return;
}
try {
final FileWriter writer = new FileWriter (write_file);
final StreamSequence stream_sequence;
switch (output_type) {
case StreamSequenceFactory.FASTA_FORMAT:
stream_sequence =
new FastaStreamSequence (sequence, header);
break;
case StreamSequenceFactory.EMBL_FORMAT:
stream_sequence =
new EmblStreamSequence (sequence);
break;
case StreamSequenceFactory.GENBANK_FORMAT:
stream_sequence =
new GenbankStreamSequence (sequence);
break;
case StreamSequenceFactory.RAW_FORMAT:
default:
stream_sequence =
new RawStreamSequence (sequence);
break;
}
stream_sequence.writeToStream (writer);
writer.close ();
} catch (IOException e) {
new MessageDialog (getParentFrame (),
"error while writing: " + e.getMessage ());
}
}
/**
* Write the bases of the selected features to a file choosen by the user
* or show a message and return immediately if there are no selected
* features.
* @param output_type One of EMBL_FORMAT, RAW_FORMAT etc.
**/
private void writeFeatureBases (final int output_type) {
if (! checkForSelectionFeatures ()) {
return;
}
final File write_file =
getWriteFile ("Select an output file name ...", "bases");
if (write_file == null) {
return;
}
try {
final FileWriter writer = new FileWriter (write_file);
final FeatureVector features_to_write =
getSelection ().getAllFeatures ();
for (int i = 0 ; i < features_to_write.size () ; ++i) {
final Feature selection_feature = features_to_write.elementAt (i);
final StringBuffer header_buffer = new StringBuffer ();
header_buffer.append (selection_feature.getSystematicName ());
header_buffer.append (" ");
header_buffer.append (selection_feature.getIDString ());
header_buffer.append (" ");
final String product = selection_feature.getProductString ();
if (product == null) {
header_buffer.append ("undefined product");
} else {
header_buffer.append (product);
}
header_buffer.append (" ").append (selection_feature.getWriteRange ());
final StreamSequence stream_sequence =
getStreamSequence (selection_feature.getBases (),
header_buffer.toString (),
output_type);
stream_sequence.writeToStream (writer);
}
writer.close ();
} catch (IOException e) {
new MessageDialog (getParentFrame (),
"error while writing: " + e.getMessage ());
}
}
/**
* Return a StreamSequence object for the given sequence string and the
* given type.
* @param sequence A String containing the sequence.
* @param header The header line that will be used on those output formats
* that need it.
* @param output_type One of EMBL_FORMAT, RAW_FORMAT etc.
**/
private StreamSequence getStreamSequence (final String sequence,
final String header,
final int output_type) {
switch (output_type) {
case StreamSequenceFactory.FASTA_FORMAT:
return new FastaStreamSequence (sequence, header);
case StreamSequenceFactory.EMBL_FORMAT:
return new EmblStreamSequence (sequence);
case StreamSequenceFactory.GENBANK_FORMAT:
return new GenbankStreamSequence (sequence);
case StreamSequenceFactory.RAW_FORMAT:
default:
return new RawStreamSequence (sequence);
}
}
/**
* Write the upstream bases of the selected features to a file choosen by
* the user. The user can also choose the number of upstream base to
* write.
* @param output_type One of EMBL_FORMAT, RAW_FORMAT etc.
**/
private void writeUpstreamBases (final int output_type) {
if (! checkForSelectionFeatures ()) {
return;
}
final TextRequester text_requester =
new TextRequester ("write how many bases upstream of each feature?",
18, "");
text_requester.addTextRequesterListener (new TextRequesterListener () {
public void actionPerformed (final TextRequesterEvent event) {
if (event.getType () == TextRequesterEvent.CANCEL) {
return;
}
final String base_count_string = event.getRequesterText ().trim ();
if (base_count_string.length () == 0) {
new MessageDialog (getParentFrame (), "no bases written");
return;
}
try {
final int base_count =
Integer.valueOf (base_count_string).intValue ();
final File write_file =
getWriteFile ("Select an output file name ...",
"upstream_" + getSequenceFileName (output_type));
if (write_file == null) {
return;
}
try {
final FileWriter writer = new FileWriter (write_file);
final FeatureVector features_to_write =
getSelection ().getAllFeatures ();
for (int i = 0 ; i < features_to_write.size () ; ++i) {
final Feature selection_feature =
features_to_write.elementAt (i);
final String sequence_string =
selection_feature.getUpstreamBases (base_count);
final String header_line =
selection_feature.getIDString () + " - " +
sequence_string.length () + " bases upstream";
final StreamSequence stream_sequence =
getStreamSequence (sequence_string,
header_line,
output_type);
stream_sequence.writeToStream (writer);
}
writer.close ();
} catch (IOException e) {
new MessageDialog (getParentFrame (),
"error while writing: " + e.getMessage ());
}
} catch (NumberFormatException e) {
new MessageDialog (getParentFrame (),
"this is not a number: " + base_count_string);
}
}
});
text_requester.show ();
}
/**
* Write the downstream bases of the selected features to a file choosen by
* the user. The user can also choose the number of downstream base to
* write.
* @param output_type One of EMBL_FORMAT, RAW_FORMAT etc.
**/
private void writeDownstreamBases (final int output_type) {
if (! checkForSelectionFeatures ()) {
return;
}
final TextRequester text_requester =
new TextRequester ("write how many bases downstream of each feature?",
18, "");
text_requester.addTextRequesterListener (new TextRequesterListener () {
public void actionPerformed (final TextRequesterEvent event) {
if (event.getType () == TextRequesterEvent.CANCEL) {
return;
}
final String base_count_string = event.getRequesterText ().trim ();
if (base_count_string.length () == 0) {
new MessageDialog (getParentFrame (), "no bases written");
return;
}
try {
final int base_count =
Integer.valueOf (base_count_string).intValue ();
final File write_file =
getWriteFile ("Select an output file name ...",
"downstream_" + getSequenceFileName (output_type));
if (write_file == null) {
return;
}
try {
final FileWriter writer = new FileWriter (write_file);
final FeatureVector features_to_write =
getSelection ().getAllFeatures ();
for (int i = 0 ; i < features_to_write.size () ; ++i) {
final Feature selection_feature =
features_to_write.elementAt (i);
final String sequence_string =
selection_feature.getDownstreamBases (base_count);
final String header_line =
selection_feature.getIDString () + " - " +
sequence_string.length () + " bases downstream ";
final StreamSequence stream_sequence =
getStreamSequence (sequence_string,
header_line,
output_type);
stream_sequence.writeToStream (writer);
}
writer.close ();
} catch (IOException e) {
new MessageDialog (getParentFrame (),
"error while writing: " + e.getMessage ());
}
} catch (NumberFormatException e) {
new MessageDialog (getParentFrame (),
"this is not a number: " + base_count_string);
}
}
});
text_requester.show ();
}
/**
* Helper method for writeFeatureBases (), writeUpstreamBases () and
* writeDownstreamBases ().
* @return A string of the form "100:200 reverse" or "1:2222 forward".
**/
private static String getWriteRange (final Feature feature) {
return (feature.isForwardFeature () ?
feature.getFirstCodingBaseMarker ().getRawPosition () + ":" +
feature.getLastBaseMarker ().getRawPosition () +
" forward" :
feature.getLastBaseMarker ().getRawPosition () + ":" +
feature.getFirstCodingBaseMarker ().getRawPosition () +
" reverse");
}
/**
* Return a sensible file name to write sequence of the given type to.
* @param output_type One of EMBL_FORMAT, RAW_FORMAT etc.
**/
private String getSequenceFileName (final int output_type) {
switch (output_type) {
case StreamSequenceFactory.FASTA_FORMAT:
return "sequence.dna";
case StreamSequenceFactory.EMBL_FORMAT:
return "sequence.embl";
case StreamSequenceFactory.GENBANK_FORMAT:
return "sequence.genbank";
case StreamSequenceFactory.RAW_FORMAT:
default:
return "sequence.seq";
}
}
/**
* Write the bases from entry_group as raw sequence.
* @param output_type One of EMBL_FORMAT, RAW_FORMAT etc.
**/
private void writeAllSequence (final int output_type) {
final String file_name = getSequenceFileName (output_type);
writeBases (entry_group.getBases ().toString (),
"all_bases", file_name, output_type);
}
/**
* Popup a requester and ask for a file name to write to. If the file
* exists ask the user whether to overwrite.
* @param title The title of the new FileDialog JFrame.
* @param default_name The name to put in the FileDialog as a default.
**/
private File getWriteFile (final String title, final String default_name) {
final JFileChooser dialog = new StickyFileChooser ();
dialog.setDialogTitle (title);
dialog.setFileSelectionMode (JFileChooser.FILES_ONLY);
dialog.setSelectedFile (new File (default_name));
dialog.setDialogType (JFileChooser.SAVE_DIALOG);
final int status = dialog.showSaveDialog (getParentFrame ());
if (status != JFileChooser.APPROVE_OPTION) {
return null;
}
if (dialog.getSelectedFile () == null) {
return null;
} else {
final File write_file = dialog.getSelectedFile ();
if (write_file.exists ()) {
final YesNoDialog yes_no_dialog =
new YesNoDialog (getParentFrame (),
"this file exists: " + write_file +
" overwrite it?");
if (yes_no_dialog.getResult ()) {
// yes - continue
} else {
// no
return null;
}
}
return write_file;
}
}
/**
* Write a table of codon usage for the selected features to a file choosen
* by the user.
**/
private void writeCodonUsage () {
if (! checkForSelectionFeatures ()) {
return;
}
final File write_file =
getWriteFile ("Select a codon usage output file ...", "usage");
if (write_file == null) {
return;
}
try {
final PrintWriter writer = new PrintWriter (new FileWriter (write_file));
final int [] [] [] codon_counts = new int [4][4][4];
final FeatureVector features_to_write =
getSelection ().getAllFeatures ();
int codon_total = 0;
for (int i = 0 ; i < features_to_write.size () ; ++i) {
final Feature selection_feature = features_to_write.elementAt (i);
for (int base1 = 0 ; base1 < 4 ; ++base1) {
for (int base2 = 0 ; base2 < 4 ; ++base2) {
for (int base3 = 0 ; base3 < 4 ; ++base3) {
codon_counts[base1][base2][base3] +=
selection_feature.getCodonCount (base1, base2, base3);
}
}
}
codon_total += selection_feature.getTranslationBasesLength () / 3;
}
for (int base1 = 0 ; base1 < 4 ; ++base1) {
for (int base3 = 0 ; base3 < 4 ; ++base3) {
final StringBuffer buffer = new StringBuffer ();
for (int base2 = 0 ; base2 < 4 ; ++base2) {
buffer.append (Bases.letter_index[base1]);
buffer.append (Bases.letter_index[base2]);
buffer.append (Bases.letter_index[base3]);
buffer.append (' ');
final float per_thousand;
if (codon_total > 0) {
per_thousand =
10000 * codon_counts[base1][base2][base3] / codon_total *
1.0F / 10;
} else {
per_thousand = 0.0F;
}
buffer.append (per_thousand);
buffer.append ("( ").append (codon_counts[base1][base2][base3]);
buffer.append (") ");
}
writer.println (buffer.toString ());
}
}
writer.close ();
} catch (IOException e) {
new MessageDialog (getParentFrame (),
"error while writing: " + e.getMessage ());
}
}
/**
* The EntryGroup object that was passed to the constructor.
**/
private EntryGroup entry_group;
}