Newer
Older
/* 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.9 2008-01-22 15:18:13 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.EntryInformationException;
import uk.ac.sanger.artemis.io.Qualifier;
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.event.*;
import java.io.*;
import javax.swing.*;
/**
* A menu of commands for writing out protein and bases.
*
* @author Kim Rutherford
/**
* 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)
final JMenuItem aa_item = new JMenuItem("Amino Acids Of Selected Features");
aa_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
writeAminoAcids();
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
final JMenuItem aa_to_qualifier_item = new JMenuItem("Amino Acids Of Selected Features to Qualifier");
aa_to_qualifier_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
final FeatureVector features_to_write =
getSelection().getAllFeatures();
try
{
for(int i = 0; i < features_to_write.size(); ++i)
{
final Feature selection_feature = features_to_write.elementAt(i);
final String translation_string =
selection_feature.getTranslation().toString().toUpperCase();
selection_feature.setQualifier(new Qualifier("translation", translation_string));
}
}
catch(ReadOnlyException e)
{
e.printStackTrace();
}
catch(EntryInformationException e)
{
e.printStackTrace();
}
}
});
add(aa_to_qualifier_item);
new JMenuItem("PIR Database Of Selected Features");
pir_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
writePIRDataBase();
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);
final JMenuItem fasta_bases_item = new JMenuItem("FASTA Format");
fasta_bases_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
writeBasesOfSelection(StreamSequenceFactory.FASTA_FORMAT);
final JMenuItem embl_bases_item = new JMenuItem("EMBL Format");
embl_bases_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
writeBasesOfSelection(StreamSequenceFactory.EMBL_FORMAT);
final JMenuItem genbank_bases_item = new JMenuItem("Genbank Format");
genbank_bases_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
writeBasesOfSelection(StreamSequenceFactory.GENBANK_FORMAT);
// final JMenu exons_menu = new JMenu("Exons Of Selection");
// final JMenuItem fasta_exons_item = new JMenuItem("Multiple FASTA Format");
// fasta_exons_item.addActionListener(new ActionListener()
// {
// public void actionPerformed(ActionEvent event)
// {
// writeExonsOfSelection(StreamSequenceFactory.FASTA_FORMAT);
// }
// });
// exons_menu.add(fasta_exons_item);
// add(exons_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);
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);
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);
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);
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);
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);
new JMenuItem("Genbank Format");
genbank_downstream_bases_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
writeDownstreamBases(StreamSequenceFactory.GENBANK_FORMAT);
final JMenuItem write_raw_item = new JMenuItem("Raw Format");
write_raw_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
writeAllSequence(StreamSequenceFactory.RAW_FORMAT);
final JMenuItem write_fasta_item = new JMenuItem("FASTA Format");
write_fasta_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
writeAllSequence(StreamSequenceFactory.FASTA_FORMAT);
final JMenuItem write_embl_item = new JMenuItem("EMBL Format");
write_embl_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
writeAllSequence(StreamSequenceFactory.EMBL_FORMAT);
final JMenuItem write_genbank_item = new JMenuItem("Genbank Format");
write_genbank_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
writeAllSequence(StreamSequenceFactory.GENBANK_FORMAT);
codon_usage_item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
writeCodonUsage();
}
/**
* 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.
**/
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.
**/
getWriteFile("Select a PIR output file name ...", "cosmid.pir", null);
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.
**/
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();
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
JTextField headerField = null;
if(output_type == StreamSequenceFactory.FASTA_FORMAT ||
output_type == StreamSequenceFactory.EMBL_FORMAT)
headerField = new JTextField(header);
final File write_file = getWriteFile("Select an output file name ...",
default_output_filename, headerField);
if(headerField != null && !headerField.getText().equals(""))
header = headerField.getText().trim();
stream_sequence = new FastaStreamSequence(sequence, header);
stream_sequence = new EmblStreamSequence(sequence);
if(headerField != null && !headerField.getText().equals(""))
{
header = "ID "+headerField.getText().trim();
header = header.concat("\nFH Key "+
"Location/Qualifiers\nFH\n");
((EmblStreamSequence)stream_sequence).setHeader(header);
}
stream_sequence.writeToStream(writer);
writer.close();
}
catch(IOException e)
{
new MessageDialog(getParentFrame(),
"error while writing: " + e.getMessage());
/**
*
* Write selected exons to a mutiple FASTA file.
*
*/
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
private void writeExonsOfSelection(final int output_type)
{
if(!checkForSelectionFeatures())
return;
final File write_file =
getWriteFile("Select an output file name ...", "exons", null);
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);
int seg_size = selection_feature.getSegments().size();
for(int j = 0; j < seg_size; ++j)
{
String bases = selection_feature.getSegments().elementAt(j).getBases();
Range range = selection_feature.getSegments().elementAt(j).getRawRange();
String s_range = " "+range.getStart()+":"+range.getEnd();
if(selection_feature.isForwardFeature())
s_range = s_range+" forward";
else
s_range = s_range+" reverse";
final StreamSequence stream_sequence =
getStreamSequence(bases,
header_buffer.toString()+s_range,
output_type);
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())
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(" ");
if(product == null)
header_buffer.append("undefined product");
else
header_buffer.append(product);
getStreamSequence(selection_feature.getBases(),
header_buffer.toString(),
output_type);
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())
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)
if(base_count_string.length() == 0)
{
new MessageDialog(getParentFrame(), "no bases written");
selection_feature.getIDString() + " - " +
sequence_string.length() + " bases upstream";
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);
}
/**
* 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())
text_requester.addTextRequesterListener(new TextRequesterListener()
{
public void actionPerformed(final TextRequesterEvent event)
{
if(event.getType() == TextRequesterEvent.CANCEL)
if(base_count_string.length() == 0)
{
new MessageDialog(getParentFrame(), "no bases written");
selection_feature.getIDString() + " - " +
sequence_string.length() + " bases downstream ";
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);
* Helper method for writeFeatureBases(), writeUpstreamBases() and
* writeDownstreamBases().
* @return A string of the form "100:200 reverse" or "1:2222 forward".
**/
{
return (feature.isForwardFeature() ?
feature.getFirstCodingBaseMarker().getRawPosition() + ":" +
feature.getLastBaseMarker().getRawPosition() +
feature.getLastBaseMarker().getRawPosition() + ":" +
feature.getFirstCodingBaseMarker().getRawPosition() +
/**
* 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 JTextField idField)
JCheckBox header = new JCheckBox("Add Header", false);
if(idField != null)
dialog.setAccessory(header);
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());
new YesNoDialog(getParentFrame(),
"this file exists: " + write_file +
" overwrite it?");
// request user to provide header
if(idField != null)
{
if(header.isSelected())
{
Box bdown = Box.createVerticalBox();
bdown.add(idField);
int n = JOptionPane.showConfirmDialog(null, bdown,
"Enter the entry ID",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(n == JOptionPane.CANCEL_OPTION)
idField.setText("");
}
else
idField.setText("");
}
return write_file;
}
}
/**
* Write a table of codon usage for the selected features to a file choosen
* by the user.
**/
try
{
final PrintWriter writer = new PrintWriter(new FileWriter(write_file));