Skip to content
Snippets Groups Projects
WriteMenu.java 36.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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
      }
    
      /**
    
    tjc's avatar
    tjc committed
       *  Helper method for writeFeatureBases(), writeUpstreamBases() and
       *  writeDownstreamBases(). 
    
    tjc's avatar
    tjc committed
       *  @return A string of the form "100:200 reverse" or "1:2222 forward".
       **/
    
    tjc's avatar
    tjc committed
      /*private static String getWriteRange(final Feature feature) 
    
    tjc's avatar
    tjc committed
      {
        return (feature.isForwardFeature() ?
                feature.getFirstCodingBaseMarker().getRawPosition() + ":" +
                feature.getLastBaseMarker().getRawPosition() +
    
    tjc's avatar
    tjc committed
                " forward" :
    
    tjc's avatar
    tjc committed
                feature.getLastBaseMarker().getRawPosition() + ":" +
                feature.getFirstCodingBaseMarker().getRawPosition() +
    
    tjc's avatar
    tjc committed
                " reverse");
    
    tjc's avatar
    tjc committed
      }*/
    
    tjc's avatar
    tjc committed
    
      
      /**
       *  Return a sensible file name to write sequence of the given type to.
       *  @param output_type One of EMBL_FORMAT, RAW_FORMAT etc.
       **/
    
    tjc's avatar
    tjc committed
      private String getSequenceFileName(final int output_type) 
      {
        switch(output_type) 
        {
    
    tjc's avatar
    tjc committed
          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.
       **/
    
    tjc's avatar
    tjc committed
      private void writeAllSequence(final int output_type) 
      {
        final String file_name = getSequenceFileName(output_type);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        writeBases(entry_group.getBases().toString(),
                   "all_bases", file_name, output_type);
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  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.
       **/
    
    tjc's avatar
    tjc committed
      private File getWriteFile(final String title, final String default_name,
                                final JTextField idField) 
    
    tjc's avatar
    tjc committed
      {
        final JFileChooser dialog = new StickyFileChooser();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        JCheckBox header = new JCheckBox("Add Header", false);
        if(idField != null)
          dialog.setAccessory(header);
    
    
    tjc's avatar
    tjc committed
        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());
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        if(status != JFileChooser.APPROVE_OPTION)
    
    tjc's avatar
    tjc committed
          return null;
    
    
    tjc's avatar
    tjc committed
        if(dialog.getSelectedFile() == null) 
    
    tjc's avatar
    tjc committed
          return null;
    
    tjc's avatar
    tjc committed
        else
        {
          final File write_file = dialog.getSelectedFile();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          if(write_file.exists())
          {
    
    tjc's avatar
    tjc committed
            final YesNoDialog yes_no_dialog =
    
    tjc's avatar
    tjc committed
              new YesNoDialog(getParentFrame(),
                              "this file exists: " + write_file +
                              " overwrite it?");
    
    tjc's avatar
    tjc committed
            
    
    tjc's avatar
    tjc committed
            if(!yes_no_dialog.getResult())
    
    tjc's avatar
    tjc committed
              return null;
          }
    
    
    tjc's avatar
    tjc committed
          // 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("");
          }
    
    tjc's avatar
    tjc committed
          return write_file;
        }
      }
    
      /**
       *  Write a table of codon usage for the selected features to a file choosen
       *  by the user.
       **/
    
    tjc's avatar
    tjc committed
      private void writeCodonUsage()
      {
        if(!checkForSelectionFeatures())
    
    tjc's avatar
    tjc committed
          return;
    
        final File write_file =
    
    tjc's avatar
    tjc committed
          getWriteFile("Select a codon usage output file ...", "usage", 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 PrintWriter writer = new PrintWriter(new FileWriter(write_file));
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          final int[][][] codon_counts = new int[4][4][4];
    
    tjc's avatar
    tjc committed
    
          final FeatureVector features_to_write =
    
    tjc's avatar
    tjc committed
            getSelection().getAllFeatures();
    
    tjc's avatar
    tjc committed
    
          int codon_total = 0;
    
    
    tjc's avatar
    tjc committed
          for(int i = 0; i < features_to_write.size(); ++i) 
          {
            final Feature selection_feature = features_to_write.elementAt(i);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            for(int base1 = 0 ; base1 < 4 ; ++base1) 
            {
              for(int base2 = 0 ; base2 < 4 ; ++base2) 
              {
                for(int base3 = 0 ; base3 < 4 ; ++base3) 
                {
    
    tjc's avatar
    tjc committed
                  codon_counts[base1][base2][base3] +=
    
    tjc's avatar
    tjc committed
                    selection_feature.getCodonCount(base1, base2, base3);
    
    tjc's avatar
    tjc committed
                }
              }
            }
    
    
    tjc's avatar
    tjc committed
            codon_total += selection_feature.getTranslationBasesLength() / 3;
    
    tjc's avatar
    tjc committed
          }
    
    
    tjc's avatar
    tjc committed
          for(int base1 = 0 ; base1 < 4 ; ++base1) 
          {
            for(int base3 = 0 ; base3 < 4 ; ++base3) 
            {
              final StringBuffer buffer = new StringBuffer();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
              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(' ');
    
    tjc's avatar
    tjc committed
    
                final float per_thousand;
    
    
    tjc's avatar
    tjc committed
                if(codon_total > 0)
                {
    
    tjc's avatar
    tjc committed
                  per_thousand =
                    10000 * codon_counts[base1][base2][base3] / codon_total *
                    1.0F / 10;
    
    tjc's avatar
    tjc committed
                } 
                else
    
    tjc's avatar
    tjc committed
                  per_thousand = 0.0F;
    
    
    tjc's avatar
    tjc committed
                buffer.append(per_thousand);
                buffer.append("( ").append(codon_counts[base1][base2][base3]);
                buffer.append(")  ");
    
    tjc's avatar
    tjc committed
              }
    
    
    tjc's avatar
    tjc committed
              writer.println(buffer.toString());
    
    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
        }
      }
    
      /**
       *  The EntryGroup object that was passed to the constructor.
       **/
      private EntryGroup entry_group;
    }