ensure current dataset reference is passed to the crossReference search method
[jalview.git] / src / jalview / gui / AlignFrame.java
index 5b56680..6302efb 100755 (executable)
@@ -498,14 +498,17 @@ public class AlignFrame
     showTranslation.setVisible( nucleotide );
     conservationMenuItem.setEnabled( !nucleotide );
     modifyConservation.setEnabled(   !nucleotide );
-
+    
     //Remember AlignFrame always starts as protein
     if(!nucleotide)
     {
       calculateMenu.remove(calculateMenu.getItemCount()-2);
     }
+    setShowProductsEnabled();
   }
 
+
+
   /**
    * Need to call this method when tabs are selected for multiple views,
    * or when loading from Jalview2XML.java
@@ -690,9 +693,9 @@ public class AlignFrame
     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
         getProperty( "LAST_DIRECTORY"),
         new String[]
-        { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc","jar" },
+        { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc","amsa","jar" },
         new String[]
-        { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview" },
+        { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview" },
         currentFileFormat,
         false);
 
@@ -723,7 +726,7 @@ public class AlignFrame
 
       jalview.bin.Cache.setProperty("LAST_DIRECTORY", fileName);
 
-      saveAlignment(fileName, currentFileFormat);
+      saveAlignment(fileName, currentFileFormat.substring(0,currentFileFormat.indexOf(" ")));
     }
   }
 
@@ -766,11 +769,12 @@ public class AlignFrame
           omitHidden = viewport.getViewAsString(false);
       }
       }
+      FormatAdapter f = new FormatAdapter();
 
-      String output = new FormatAdapter().formatSequences(
+      String output = f.formatSequences(
           format,
-          viewport.alignment.getSequencesArray(),
-          omitHidden);
+          (Alignment) viewport.alignment, // class cast exceptions will occur in the distant future
+          omitHidden, f.getCacheSuffixDefault(format));
 
       if (output == null)
       {
@@ -841,7 +845,7 @@ public class AlignFrame
 
     cap.setText(new FormatAdapter().formatSequences(
         e.getActionCommand(),
-        viewport.alignment.getSequencesArray(),
+        viewport.alignment,
         omitHidden));
   }
 
@@ -1614,18 +1618,19 @@ public class AlignFrame
        {
          // Duplicate sequence annotation in all views.
          AlignmentI[] alview = this.getViewAlignments();
-         for (int avnum=0;avnum<alview.length; avnum++)
+         for (int i = 0; i < sequences.length; i++)
          {
-           if (alview[avnum]!=alignment)
+           AlignmentAnnotation sann[] = sequences[i].getAnnotation();
+           if (sann == null)
+             continue;
+           for (int avnum=0;avnum<alview.length; avnum++)
            {
-             // duplicate in a view other than the one with input focus
-             int avwidth = alview[avnum].getWidth()+1;
-             for (int i=0; i < sequences.length; i++)
+             if (alview[avnum]!=alignment)
              {
-               AlignmentAnnotation sann[] = sequences[i].getAnnotation();
-               if (sann==null)
-                 continue;
-               // this relies on sann being preserved after we modify the sequence's annotation array
+               // duplicate in a view other than the one with input focus
+               int avwidth = alview[avnum].getWidth()+1;
+               // this relies on sann being preserved after we 
+               // modify the sequence's annotation array for each duplication
                for (int a=0; a<sann.length; a++)
                {
                  AlignmentAnnotation newann = new AlignmentAnnotation(sann[a]);
@@ -3526,7 +3531,7 @@ public class AlignFrame
 
       public void actionPerformed(ActionEvent e)
       {
-        new jalview.io.DBRefFetcher(
+        new jalview.ws.DBRefFetcher(
                 alignPanel.av.getSequenceSelection(),
                 alignPanel.alignFrame).fetchDBRefs(false);
       }
@@ -3552,8 +3557,135 @@ public class AlignFrame
       vs.storeJalview( chooser.getSelectedFile().getAbsolutePath(), this);
     }
   }*/
+  /**
+   * prototype of an automatically enabled/disabled analysis function
+   *
+   */
+  protected void setShowProductsEnabled()
+  {
+    SequenceI [] selection = viewport.getSequenceSelection();
+    if (canShowProducts(selection, viewport.getSelectionGroup()!=null, viewport.getAlignment().getDataset()))
+    {
+      showProducts.setEnabled(true);
+      
+    } else {
+      showProducts.setEnabled(false);
+    }
+  }
+  /**
+   * search selection for sequence xRef products and build the
+   * show products menu.
+   * @param selection
+   * @param dataset
+   * @return true if showProducts menu should be enabled.
+   */
+  public boolean canShowProducts(SequenceI[] selection, boolean isRegionSelection, Alignment dataset)
+  {
+    boolean showp=false;
+    try {
+      showProducts.removeAll();
+      final boolean dna = viewport.getAlignment().isNucleotide();
+      final Alignment ds = dataset;
+      String[] ptypes = CrossRef.findSequenceXrefTypes(dna, selection, dataset);
+      //Object[] prods = CrossRef.buildXProductsList(viewport.getAlignment().isNucleotide(), selection, dataset, true);
+      final SequenceI[] sel = selection;
+      for (int t=0; ptypes!=null && t<ptypes.length; t++)
+      {
+        showp=true;
+        final boolean isRegSel = isRegionSelection;
+        final AlignFrame af = this;
+        final String source = ptypes[t];
+        JMenuItem xtype = new JMenuItem(ptypes[t]);
+        xtype.addActionListener(new ActionListener() {
 
+          public void actionPerformed(ActionEvent e)
+          {
+            af.showProductsFor(sel, ds, isRegSel, dna, source);
+          }
+          
+        });
+        showProducts.add(xtype);
+      }
+      showProducts.setVisible(showp);
+      showProducts.setEnabled(showp);
+    } catch (Exception e)
+    {
+      jalview.bin.Cache.log.warn("canTranslate threw an exception - please report to help@jalview.org",e);
+     return false;
+    }
+    return showp;
+  }
+protected void showProductsFor(SequenceI[] sel, Alignment ds, boolean isRegSel, boolean dna, String source)
+  {
+  ds = this.getViewport().alignment.getDataset(); // update our local dataset reference
+  Alignment prods = CrossRef.findXrefSequences(sel, dna, source, ds);
+  if (prods!=null)
+  {
+    SequenceI[] sprods = new SequenceI[prods.getHeight()];
+    for (int s=0; s<sprods.length;s++)
+    {
+      sprods[s] = (prods.getSequenceAt(s)).deriveSequence();
+      if (ds.getSequences()==null || !ds.getSequences().contains(sprods[s].getDatasetSequence()))
+        ds.addSequence(sprods[s].getDatasetSequence());
+    }
+    Alignment al = new Alignment(sprods);
+    AlignedCodonFrame[] cf = prods.getCodonFrames();
+    for (int s=0; cf!=null && s<cf.length; s++)
+    {
+      al.addCodonFrame(cf[s]);
+      cf[s] = null;
+    }
+    al.setDataset(ds);
+    AlignFrame naf = new AlignFrame(al, DEFAULT_WIDTH, DEFAULT_HEIGHT);
+    String newtitle =""+((dna) ? "Proteins " : "Nucleotides ") + " for "+((isRegSel) ? "selected region of " : "")
+            + getTitle();
+    Desktop.addInternalFrame(naf, newtitle, DEFAULT_WIDTH,
+            DEFAULT_HEIGHT);
+  } else {
+    System.err.println("No Sequences generated for xRef type "+source);
+  }
+  }
+
+
+public boolean canShowTranslationProducts(SequenceI[] selection, AlignmentI alignment)
+{
+  // old way
+  try {
+      return (jalview.analysis.Dna.canTranslate(selection, viewport.getViewAsVisibleContigs(true)));
+  } catch (Exception e)
+  {
+    jalview.bin.Cache.log.warn("canTranslate threw an exception - please report to help@jalview.org",e);
+   return false;
+  }
+}
 
+public void showProducts_actionPerformed(ActionEvent e)
+{
+  ///////////////////////////////
+  // Collect Data to be translated/transferred
+  
+  SequenceI [] selection = viewport.getSequenceSelection();
+  AlignmentI al  = null;
+  try {
+      al = jalview.analysis.Dna.CdnaTranslate(selection, viewport.getViewAsVisibleContigs(true),
+          viewport.getGapCharacter(), viewport.getAlignment().getDataset());
+    } catch (Exception ex) {
+      al = null;
+      jalview.bin.Cache.log.debug("Exception during translation.",ex);
+    }
+    if (al==null)
+    {
+      JOptionPane.showMessageDialog(Desktop.desktop,
+          "Please select at least three bases in at least one sequence in order to perform a cDNA translation.",
+          "Translation Failed",
+          JOptionPane.WARNING_MESSAGE);
+    } else {
+      AlignFrame af = new AlignFrame(al, DEFAULT_WIDTH, DEFAULT_HEIGHT);
+      Desktop.addInternalFrame(af, "Translation of "+this.getTitle(),
+                               DEFAULT_WIDTH,
+                               DEFAULT_HEIGHT);
+    }
+  }
 
 public void showTranslation_actionPerformed(ActionEvent e)
 {
@@ -3566,7 +3698,7 @@ public void showTranslation_actionPerformed(ActionEvent e)
   try {
     al = jalview.analysis.Dna.CdnaTranslate(selection, seqstring, viewport.getViewAsVisibleContigs(true),
         viewport.getGapCharacter(), viewport.alignment.getAlignmentAnnotation(),
-        viewport.alignment.getWidth());
+        viewport.alignment.getWidth(), viewport.getAlignment().getDataset());
   } catch (Exception ex) {
     al = null;
     jalview.bin.Cache.log.debug("Exception during translation.",ex);