Skip to content
Snippets Groups Projects
WriteMenu.java 36.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
    /* 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.11 2009-05-05 16:18:43 tjc Exp $
    
    tjc's avatar
    tjc committed
     **/
    
    package uk.ac.sanger.artemis.components;
    
    
    tjc's avatar
    tjc committed
    import uk.ac.sanger.artemis.EntryGroup;
    import uk.ac.sanger.artemis.Feature;
    import uk.ac.sanger.artemis.FeatureVector;
    import uk.ac.sanger.artemis.Selection;
    import uk.ac.sanger.artemis.sequence.Bases;
    import uk.ac.sanger.artemis.sequence.MarkerRange;
    import uk.ac.sanger.artemis.sequence.Strand;
    
    tjc's avatar
    tjc committed
    import uk.ac.sanger.artemis.util.ReadOnlyException;
    
    tjc's avatar
    tjc committed
    
    
    import uk.ac.sanger.artemis.circular.TextFieldInt;
    
    tjc's avatar
    tjc committed
    import uk.ac.sanger.artemis.io.EntryInformationException;
    import uk.ac.sanger.artemis.io.Qualifier;
    
    tjc's avatar
    tjc committed
    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.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    tjc's avatar
    tjc committed
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.swing.Box;
    import javax.swing.JCheckBox;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    
    tjc's avatar
    tjc committed
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    
    tjc's avatar
    tjc committed
    import javax.swing.JTextField;
    
    tjc's avatar
    tjc committed
    
    /**
     *  A menu of commands for writing out protein and bases.
     *
     *  @author Kim Rutherford
    
     *  @version $Id: WriteMenu.java,v 1.11 2009-05-05 16:18:43 tjc Exp $
    
    tjc's avatar
    tjc committed
     **/
    
    tjc's avatar
    tjc committed
    public class WriteMenu extends SelectionMenu 
    {
    
    tjc's avatar
    tjc committed
      private static final long serialVersionUID = 1L;
    
    
    tjc's avatar
    tjc committed
      /**
       *  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.
       **/
    
    tjc's avatar
    tjc committed
      public WriteMenu(final JFrame frame, final Selection selection,
                       final EntryGroup entry_group, final String menu_name)
    
    tjc's avatar
    tjc committed
      {
        super(frame, menu_name, selection);
    
    tjc's avatar
    tjc committed
    
        this.entry_group = entry_group;
    
    
    tjc's avatar
    tjc committed
        final JMenuItem aa_item = new JMenuItem("Amino Acids Of Selected Features");
        aa_item.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent event) 
          {
            writeAminoAcids();
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        add(aa_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        
        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);
        
    
    tjc's avatar
    tjc committed
        final JMenuItem pir_item =
    
    tjc's avatar
    tjc committed
          new JMenuItem("PIR Database Of Selected Features");
        pir_item.addActionListener(new ActionListener() 
        {
          public void actionPerformed(ActionEvent event) 
          {
            writePIRDataBase();
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        add(pir_item);
        addSeparator();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        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);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        bases_menu.add(raw_bases_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final JMenuItem fasta_bases_item = new JMenuItem("FASTA Format");
        fasta_bases_item.addActionListener(new ActionListener() 
        {
          public void actionPerformed(ActionEvent event) 
          {
            writeBasesOfSelection(StreamSequenceFactory.FASTA_FORMAT);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        bases_menu.add(fasta_bases_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final JMenuItem embl_bases_item = new JMenuItem("EMBL Format");
        embl_bases_item.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent event) 
          {
            writeBasesOfSelection(StreamSequenceFactory.EMBL_FORMAT);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        bases_menu.add(embl_bases_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final JMenuItem genbank_bases_item = new JMenuItem("Genbank Format");
        genbank_bases_item.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent event) 
          {
            writeBasesOfSelection(StreamSequenceFactory.GENBANK_FORMAT);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        bases_menu.add(genbank_bases_item);
        add(bases_menu);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
    //  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);
    
    tjc's avatar
    tjc committed
        final JMenu upstream_bases_menu =
    
    tjc's avatar
    tjc committed
          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);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        upstream_bases_menu.add(raw_upstream_bases_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        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);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        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);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        upstream_bases_menu.add(embl_upstream_bases_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        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);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        upstream_bases_menu.add(genbank_upstream_bases_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        add(upstream_bases_menu);
    
    tjc's avatar
    tjc committed
    
        final JMenu downstream_bases_menu =
    
    tjc's avatar
    tjc committed
          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);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        downstream_bases_menu.add(raw_downstream_bases_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        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);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        downstream_bases_menu.add(fasta_downstream_bases_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        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);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        downstream_bases_menu.add(embl_downstream_bases_item);
    
    tjc's avatar
    tjc committed
    
        final JMenuItem genbank_downstream_bases_item =
    
    tjc's avatar
    tjc committed
          new JMenuItem("Genbank Format");
        genbank_downstream_bases_item.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent event) 
          {
            writeDownstreamBases(StreamSequenceFactory.GENBANK_FORMAT);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        downstream_bases_menu.add(genbank_downstream_bases_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        add(downstream_bases_menu);
    
    tjc's avatar
    tjc committed
    
    
        addSeparator();
        
        final JMenuItem write_combo_bases = new JMenuItem("Upstream+Feature+Downstream Bases ...");
        write_combo_bases.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent event) 
          {
            writeComboBases();
          }
        });
        add(write_combo_bases);
        
    
    tjc's avatar
    tjc committed
        addSeparator();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final JMenu write_all_bases_menu = new JMenu("All Bases");
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final JMenuItem write_raw_item = new JMenuItem("Raw Format");
        write_raw_item.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent event) 
          {
            writeAllSequence(StreamSequenceFactory.RAW_FORMAT);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        write_all_bases_menu.add(write_raw_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final JMenuItem write_fasta_item = new JMenuItem("FASTA Format");
        write_fasta_item.addActionListener(new ActionListener() 
        {
          public void actionPerformed(ActionEvent event) 
          {
            writeAllSequence(StreamSequenceFactory.FASTA_FORMAT);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        write_all_bases_menu.add(write_fasta_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final JMenuItem write_embl_item = new JMenuItem("EMBL Format");
        write_embl_item.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent event) 
          {
            writeAllSequence(StreamSequenceFactory.EMBL_FORMAT);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        write_all_bases_menu.add(write_embl_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final JMenuItem write_genbank_item = new JMenuItem("Genbank Format");
        write_genbank_item.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent event) 
          {
            writeAllSequence(StreamSequenceFactory.GENBANK_FORMAT);
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        write_all_bases_menu.add(write_genbank_item);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        add(write_all_bases_menu);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        addSeparator();
    
    tjc's avatar
    tjc committed
    
        final JMenuItem codon_usage_item =
    
    tjc's avatar
    tjc committed
          new JMenuItem("Codon Usage of Selected Features");
    
    tjc's avatar
    tjc committed
        codon_usage_item.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent event)
          {
            writeCodonUsage();
    
    tjc's avatar
    tjc committed
          }
        });
    
    
    tjc's avatar
    tjc committed
        add(codon_usage_item);
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  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.
       **/
    
    tjc's avatar
    tjc committed
      public WriteMenu(final JFrame frame, final Selection selection,
    
    tjc's avatar
    tjc committed
                       final EntryGroup entry_group) 
      {
        this(frame, selection, entry_group, "Write");
    
    tjc's avatar
    tjc committed
      }
      
      /**
       *  Write a PIR database of the selected features to a file choosen by the
       *  user.
       **/
    
    tjc's avatar
    tjc committed
      private void writePIRDataBase()
      {
        if(!checkForSelectionFeatures()) 
    
    tjc's avatar
    tjc committed
          return;
    
        final File write_file =
    
    tjc's avatar
    tjc committed
          getWriteFile("Select a PIR output file name ...", "cosmid.pir", null);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        if(write_file == null)
    
    tjc's avatar
    tjc committed
          return;
    
    
    tjc's avatar
    tjc committed
        try 
        {
          final FileWriter writer = new FileWriter(write_file);
    
    tjc's avatar
    tjc committed
    
          final FeatureVector features_to_write =
    
    tjc's avatar
    tjc committed
            getSelection().getAllFeatures();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          for(int i = 0; i < features_to_write.size() ; ++i)
          {
            final Feature selection_feature = features_to_write.elementAt(i);
            selection_feature.writePIROfFeature(writer);
    
    tjc's avatar
    tjc committed
          }
    
    
    tjc's avatar
    tjc committed
          writer.close();
        }
        catch(IOException e) 
        {
          new MessageDialog(getParentFrame(),
                            "error while writing: " + e.getMessage());
    
    tjc's avatar
    tjc committed
        }
      }
    
      /**
       *  Write the amino acid symbols of the selected features to a file choosen
       *  by the user.
       **/
    
    tjc's avatar
    tjc committed
      private void writeAminoAcids()
      {
        if(!checkForSelectionFeatures())
    
    tjc's avatar
    tjc committed
          return;
    
        final File write_file =
    
    tjc's avatar
    tjc committed
          getWriteFile("Select an output file name..", "amino_acids", null);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        if(write_file == null)
    
    tjc's avatar
    tjc committed
          return;
    
    
    tjc's avatar
    tjc committed
        try
        {
          final FileWriter writer = new FileWriter(write_file);
    
    tjc's avatar
    tjc committed
    
          final FeatureVector features_to_write =
    
    tjc's avatar
    tjc committed
            getSelection().getAllFeatures();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          for(int i = 0; i < features_to_write.size(); ++i) 
          {
            final Feature selection_feature = features_to_write.elementAt(i);
            selection_feature.writeAminoAcidsOfFeature(writer);
    
    tjc's avatar
    tjc committed
          }
    
    
    tjc's avatar
    tjc committed
          writer.close();
        } 
        catch(IOException e) 
        {
          new MessageDialog(getParentFrame(),
                            "error while writing: " + e.getMessage());
    
    tjc's avatar
    tjc committed
        }
      }
    
      /**
       *  Write the bases of the selection to a file choosen by the user.
       *  @param output_type One of EMBL_FORMAT, RAW_FORMAT etc.
       **/
    
    tjc's avatar
    tjc committed
      private void writeBasesOfSelection(final int output_type) 
      {
        final MarkerRange marker_range = getSelection().getMarkerRange();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        if(marker_range == null) 
    
    tjc's avatar
    tjc committed
          writeFeatureBases (output_type);
    
    tjc's avatar
    tjc committed
        else  
        {
          final String selection_bases = Strand.markerRangeBases(marker_range);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          writeBases(selection_bases, "selected bases",
                     getSequenceFileName(output_type),
                     output_type);
    
    tjc's avatar
    tjc committed
        }
      }
    
    
    tjc's avatar
    tjc committed
      /**
       *  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
    
    tjc's avatar
    tjc committed
       *    getWriteFile()
    
    tjc's avatar
    tjc committed
       *  @param output_type One of EMBL_FORMAT, RAW_FORMAT etc.
       **/
    
    tjc's avatar
    tjc committed
      private void writeBases(final String sequence, String header,
    
    tjc's avatar
    tjc committed
                              final String default_output_filename,
                              final int output_type) 
      {
    
    tjc's avatar
    tjc committed
        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);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        if(write_file == null)
    
    tjc's avatar
    tjc committed
          return;
    
    
    tjc's avatar
    tjc committed
        try 
        {
          final FileWriter writer = new FileWriter(write_file);
    
    tjc's avatar
    tjc committed
          final StreamSequence stream_sequence;
    
    
    tjc's avatar
    tjc committed
          switch(output_type) 
          {
            case StreamSequenceFactory.FASTA_FORMAT:
    
    tjc's avatar
    tjc committed
              if(headerField != null && !headerField.getText().equals(""))
                header = headerField.getText().trim();
    
              stream_sequence = new FastaStreamSequence(sequence, header);
    
    tjc's avatar
    tjc committed
              break;
    
            case StreamSequenceFactory.EMBL_FORMAT:
    
    tjc's avatar
    tjc committed
              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);
              }
    
    
    tjc's avatar
    tjc committed
              break;
    
            case StreamSequenceFactory.GENBANK_FORMAT:
    
    tjc's avatar
    tjc committed
              stream_sequence = new GenbankStreamSequence(sequence);
    
    tjc's avatar
    tjc committed
              break;
    
            case StreamSequenceFactory.RAW_FORMAT:
            default:
    
    tjc's avatar
    tjc committed
              stream_sequence = new RawStreamSequence(sequence);
    
    tjc's avatar
    tjc committed
              break;
    
    tjc's avatar
    tjc committed
          }
    
    
    tjc's avatar
    tjc committed
          stream_sequence.writeToStream(writer);
          writer.close();
        } 
        catch(IOException e) 
        {
          new MessageDialog(getParentFrame(),
                            "error while writing: " + e.getMessage());
    
    tjc's avatar
    tjc committed
        }
      }
    
    
    tjc's avatar
    tjc committed
      /**
      *
      * Write selected exons to a mutiple FASTA file.
      *
      */
    
    tjc's avatar
    tjc committed
    /*
    
    tjc's avatar
    tjc committed
      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());
        }
      }
    
    tjc's avatar
    tjc committed
    */
    
    tjc's avatar
    tjc committed
      /**
       *  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.
       **/
    
    tjc's avatar
    tjc committed
      private void writeFeatureBases(final int output_type) 
      {
        if(!checkForSelectionFeatures())
    
    tjc's avatar
    tjc committed
          return;
    
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final File write_file =
    
    tjc's avatar
    tjc committed
          getWriteFile("Select an output file name ...", "bases", null);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        if(write_file == null)
    
    tjc's avatar
    tjc committed
          return;
    
    
    tjc's avatar
    tjc committed
        try 
        {
          final FileWriter writer = new FileWriter(write_file);
    
    tjc's avatar
    tjc committed
          final FeatureVector features_to_write =
    
    tjc's avatar
    tjc committed
            getSelection().getAllFeatures();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          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();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            header_buffer.append(selection_feature.getSystematicName());
            header_buffer.append(" ");
            header_buffer.append(selection_feature.getIDString());
            header_buffer.append(" ");
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            final String product = selection_feature.getProductString();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            if(product == null) 
              header_buffer.append("undefined product");
            else 
              header_buffer.append(product);
    
    tjc's avatar
    tjc committed
            
    
    tjc's avatar
    tjc committed
            header_buffer.append(" ").append(selection_feature.getWriteRange());
    
    tjc's avatar
    tjc committed
    
            final StreamSequence stream_sequence =
    
    tjc's avatar
    tjc committed
              getStreamSequence(selection_feature.getBases(),
                                header_buffer.toString(),
                                output_type);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            stream_sequence.writeToStream(writer);
    
    tjc's avatar
    tjc committed
          }
    
    
    tjc's avatar
    tjc committed
          writer.close();
        } 
        catch(IOException e) 
        {
          new MessageDialog(getParentFrame(),
                            "error while writing: " + e.getMessage());
    
    tjc's avatar
    tjc committed
        }
      }
    
      /**
       *  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.
       **/
    
    tjc's avatar
    tjc committed
      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);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          case StreamSequenceFactory.EMBL_FORMAT:
            return new EmblStreamSequence (sequence);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          case StreamSequenceFactory.GENBANK_FORMAT:
            return new GenbankStreamSequence (sequence);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          case StreamSequenceFactory.RAW_FORMAT:
          default:
            return new RawStreamSequence (sequence);
    
    tjc's avatar
    tjc committed
        }
      }
    
      /**
       *  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.
       **/
    
    tjc's avatar
    tjc committed
      private void writeUpstreamBases(final int output_type) 
      {
        if(!checkForSelectionFeatures()) 
    
    tjc's avatar
    tjc committed
          return;
    
        final TextRequester text_requester =
    
    tjc's avatar
    tjc committed
          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)
    
    tjc's avatar
    tjc committed
              return;
    
    
    tjc's avatar
    tjc committed
            final String base_count_string = event.getRequesterText().trim();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            if(base_count_string.length() == 0)
            {
              new MessageDialog(getParentFrame(), "no bases written");
    
    tjc's avatar
    tjc committed
              return;
            }
    
    
    tjc's avatar
    tjc committed
            try 
            {
    
    tjc's avatar
    tjc committed
              final int base_count =
    
    tjc's avatar
    tjc committed
                Integer.valueOf(base_count_string).intValue();
    
    tjc's avatar
    tjc committed
    
              final File write_file =
    
    tjc's avatar
    tjc committed
                getWriteFile("Select an output file name ...",
    
    tjc's avatar
    tjc committed
                             "upstream_" + getSequenceFileName(output_type), null);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
              if(write_file == null)
    
    tjc's avatar
    tjc committed
                return;
    
    
    tjc's avatar
    tjc committed
              try 
              {
                final FileWriter writer = new FileWriter(write_file);
    
    tjc's avatar
    tjc committed
    
                final FeatureVector features_to_write =
    
    tjc's avatar
    tjc committed
                  getSelection().getAllFeatures();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
                for(int i = 0; i < features_to_write.size(); ++i)
                {
    
    tjc's avatar
    tjc committed
                  final Feature selection_feature =
    
    tjc's avatar
    tjc committed
                    features_to_write.elementAt(i);
    
    tjc's avatar
    tjc committed
    
                  final String sequence_string =
    
    tjc's avatar
    tjc committed
                    selection_feature.getUpstreamBases(base_count);
    
    tjc's avatar
    tjc committed
    
    
                  final String header_line =
    
    tjc's avatar
    tjc committed
                    selection_feature.getIDString() + " - " +
                    sequence_string.length() + " bases upstream";
    
    tjc's avatar
    tjc committed
    
                  final StreamSequence stream_sequence =
    
    tjc's avatar
    tjc committed
                    getStreamSequence(sequence_string,
    
    tjc's avatar
    tjc committed
                                       header_line,
                                       output_type);
                  
    
    tjc's avatar
    tjc committed
                  stream_sequence.writeToStream(writer);
    
    tjc's avatar
    tjc committed
                }
    
    
    tjc's avatar
    tjc committed
                writer.close();
              }
              catch(IOException e) 
              {
                new MessageDialog(getParentFrame(),
                                  "error while writing: " + e.getMessage());
    
    tjc's avatar
    tjc committed
              }
    
    tjc's avatar
    tjc committed
            } 
            catch(NumberFormatException e)
            {
              new MessageDialog(getParentFrame(),
                                "this is not a number: " + base_count_string);
    
    tjc's avatar
    tjc committed
            }
          }
        });
    
    
        text_requester.setVisible(true);
    
    tjc's avatar
    tjc committed
      }
    
      
      /**
       * Write a combination of upstream + feature + downstream bases for 
       * selected features.
       */
      private void writeComboBases()
      {
        TextFieldInt upstreamBases   = new TextFieldInt();
        upstreamBases.setValue(0);
        Dimension preferredSize = new Dimension(200, 
             upstreamBases.getPreferredSize().height);
        upstreamBases.setPreferredSize(preferredSize);
        TextFieldInt downstreamBases = new TextFieldInt();
        downstreamBases.setValue(0);
        downstreamBases.setPreferredSize(preferredSize);
        TextFieldInt featureBases = new TextFieldInt();
        featureBases.setValue(0);
        featureBases.setPreferredSize(preferredSize);
        JCheckBox allFeatureBases = new JCheckBox("All bases of the feature(s)", false);
        
        int row = 0;
        JPanel gridPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = row;
        c.anchor = GridBagConstraints.WEST;
        
        gridPanel.add(new JLabel("Number of bases upstream of each feature to write:"), c);
        c.gridy = ++row;
        gridPanel.add(upstreamBases, c);
        c.gridy = ++row;
        gridPanel.add(Box.createVerticalStrut(15), c);
        
        c.gridy = ++row;
        gridPanel.add(new JLabel("Number of feature bases to write:"), c);
        c.gridy = ++row;
        gridPanel.add(featureBases, c);
        c.gridy = ++row;
        gridPanel.add(allFeatureBases, c);
        c.gridy = ++row;
        gridPanel.add(Box.createVerticalStrut(15), c);
        
        c.gridy = ++row;
        gridPanel.add(new JLabel("Number of bases downstream of each feature to write:"),c);
        c.gridy = ++row;
        gridPanel.add(downstreamBases, c);
        
        JOptionPane.showMessageDialog(null, gridPanel, 
                       "Bases to write to FASTA file for selected features", 
                       JOptionPane.QUESTION_MESSAGE);
        
        int output_type = StreamSequenceFactory.FASTA_FORMAT;
        final File write_file =
          getWriteFile("Select an output file name ...",
                       "bases_" + getSequenceFileName(output_type), null);
        if(write_file == null)
          return;
        
        try 
        {
          final FileWriter writer = new FileWriter(write_file);
          final FeatureVector features_to_write =
            getSelection().getAllFeatures();
    
          int upstream_base_count   = upstreamBases.getValue();
          int downstream_base_count = downstreamBases.getValue();
          int feature_base_count    = featureBases.getValue();
          
          for(int i = 0; i < features_to_write.size(); ++i) 
          {
            final Feature selection_feature =
              features_to_write.elementAt(i);
     
            StringBuffer sequenceBuff = new StringBuffer();
            
            if(upstream_base_count > 0)
              sequenceBuff.append(
                  selection_feature.getUpstreamBases(upstream_base_count));
            
            if(allFeatureBases.isSelected())       
              sequenceBuff.append(selection_feature.getBases());
            else if (feature_base_count > 0)
              sequenceBuff.append(
                  selection_feature.getBases().substring(0, feature_base_count));
            
            if(downstream_base_count > 0)
              sequenceBuff.append(
                selection_feature.getDownstreamBases(downstream_base_count));
    
            final String header_line =
              selection_feature.getIDString() + " - " +
              sequenceBuff.length();
    
            final StreamSequence stream_sequence =
              getStreamSequence(sequenceBuff.toString(),
                                header_line,
                                output_type);
            
            stream_sequence.writeToStream(writer);
          }
    
          writer.close();
        } 
        catch(IOException e) 
        {
          new MessageDialog(getParentFrame(),
                            "error while writing: " + e.getMessage());
        }
        
        if(write_file == null)
          return;
        
      }
    
    tjc's avatar
    tjc committed
    
      /**
       *  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.
       **/
    
    tjc's avatar
    tjc committed
      private void writeDownstreamBases(final int output_type) 
      {
        if(!checkForSelectionFeatures())
    
    tjc's avatar
    tjc committed
          return;
    
        final TextRequester text_requester =
    
    tjc's avatar
    tjc committed
          new TextRequester("write how many bases downstream of each feature?",
    
    tjc's avatar
    tjc committed
                             18, "");
    
    
    tjc's avatar
    tjc committed
        text_requester.addTextRequesterListener(new TextRequesterListener()
        {
          public void actionPerformed(final TextRequesterEvent event) 
          {
            if(event.getType() == TextRequesterEvent.CANCEL)
    
    tjc's avatar
    tjc committed
              return;
    
    
    tjc's avatar
    tjc committed
            final String base_count_string = event.getRequesterText().trim();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            if(base_count_string.length() == 0) 
            {
              new MessageDialog(getParentFrame(), "no bases written");
    
    tjc's avatar
    tjc committed
              return;
            }
    
    
    tjc's avatar
    tjc committed
            try 
            {
    
    tjc's avatar
    tjc committed
              final int base_count =
    
    tjc's avatar
    tjc committed
                Integer.valueOf(base_count_string).intValue();
    
    tjc's avatar
    tjc committed
    
              final File write_file =
    
    tjc's avatar
    tjc committed
                getWriteFile("Select an output file name ...",
    
    tjc's avatar
    tjc committed
                             "downstream_" + getSequenceFileName(output_type), null);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
              if(write_file == null)
    
    tjc's avatar
    tjc committed
                return;
    
    
    tjc's avatar
    tjc committed
              try 
              {
                final FileWriter writer = new FileWriter(write_file);
    
    tjc's avatar
    tjc committed
                final FeatureVector features_to_write =
    
    tjc's avatar
    tjc committed
                  getSelection().getAllFeatures();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
                for(int i = 0; i < features_to_write.size(); ++i) 
                {
    
    tjc's avatar
    tjc committed
                  final Feature selection_feature =
    
    tjc's avatar
    tjc committed
                    features_to_write.elementAt(i);
    
    tjc's avatar
    tjc committed
    
                  final String sequence_string =
    
    tjc's avatar
    tjc committed
                    selection_feature.getDownstreamBases(base_count);
    
    tjc's avatar
    tjc committed
    
    
                  final String header_line =
    
    tjc's avatar
    tjc committed
                    selection_feature.getIDString() + " - " +
                    sequence_string.length() + " bases downstream ";
    
    tjc's avatar
    tjc committed
    
                  final StreamSequence stream_sequence =
    
    tjc's avatar
    tjc committed
                    getStreamSequence(sequence_string,
                                      header_line,
                                      output_type);
    
    tjc's avatar
    tjc committed
                  
    
    tjc's avatar
    tjc committed
                  stream_sequence.writeToStream(writer);
    
    tjc's avatar
    tjc committed
                }
    
    
    tjc's avatar
    tjc committed
                writer.close();
              } 
              catch(IOException e) 
              {
                new MessageDialog(getParentFrame(),
                                  "error while writing: " + e.getMessage());
    
    tjc's avatar
    tjc committed
              }
    
    tjc's avatar
    tjc committed
            }