JAL-1067 report any warning messages to user
[jalview.git] / src / jalview / gui / AlignFrame.java
index e1775d7..e19581f 100755 (executable)
@@ -55,6 +55,7 @@ import jalview.io.JalviewFileChooser;
 import jalview.io.JalviewFileView;
 import jalview.io.JnetAnnotationMaker;
 import jalview.io.NewickFile;
+import jalview.io.TCoffeeScoreFile;
 import jalview.jbgui.GAlignFrame;
 import jalview.schemes.Blosum62ColourScheme;
 import jalview.schemes.BuriedColourScheme;
@@ -69,13 +70,14 @@ import jalview.schemes.PurinePyrimidineColourScheme;
 import jalview.schemes.RNAHelicesColourChooser;
 import jalview.schemes.ResidueProperties;
 import jalview.schemes.StrandColourScheme;
+import jalview.schemes.TCoffeeColourScheme;
 import jalview.schemes.TaylorColourScheme;
 import jalview.schemes.TurnColourScheme;
 import jalview.schemes.UserColourScheme;
 import jalview.schemes.ZappoColourScheme;
-import jalview.ws.WSMenuEntryProviderI;
 import jalview.ws.jws1.Discoverer;
 import jalview.ws.jws2.Jws2Discoverer;
+import jalview.ws.seqfetcher.DbSourceProxy;
 
 import java.awt.BorderLayout;
 import java.awt.Color;
@@ -106,6 +108,7 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.Hashtable;
+import java.util.List;
 import java.util.Vector;
 
 import javax.swing.JButton;
@@ -143,7 +146,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   AlignViewport viewport;
 
   Vector alignPanels = new Vector();
-
+  
   /**
    * Last format used to load or save alignments in this window
    */
@@ -3052,6 +3055,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    */
   public void changeColour(ColourSchemeI cs)
   {
+    // TODO: compare with applet and pull up to model method
     int threshold = 0;
 
     if (cs != null)
@@ -3123,7 +3127,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
         {
           try
           {
-            sg.cs = (ColourSchemeI) cs.getClass().newInstance();
+            sg.cs = cs.getClass().newInstance();
           } catch (Exception ex)
           {
           }
@@ -3921,7 +3925,89 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       }
     }
   }
-
+  
+  @Override
+  public void loadScores_actionPerformed(ActionEvent e)
+  {
+           // Pick the tree file
+           JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
+           chooser.setFileView(new JalviewFileView());
+           chooser.setDialogTitle("Select a T-Coffee scores ascii file");
+           chooser.setToolTipText("Load a score file");
+
+           int value = chooser.showOpenDialog(null);
+
+           if (value == JalviewFileChooser.APPROVE_OPTION)
+           {
+             String sFilePath = chooser.getSelectedFile().getPath();
+             jalview.bin.Cache.setProperty("LAST_DIRECTORY", sFilePath);
+           
+             
+             try 
+             {
+                 TCoffeeScoreFile result = new TCoffeeScoreFile(sFilePath, FormatAdapter.FILE);
+                 if (!result.isValid()) { 
+                   JOptionPane.showMessageDialog(Desktop.desktop, result.getWarningMessage(),
+                            "Problem reading T-COFFEE score file", JOptionPane.WARNING_MESSAGE);
+                   return;
+                   }
+
+                 /*
+                  * check that the score matrix matches the alignment dimensions
+                  */
+                 AlignmentI aln; 
+                 if( (aln=viewport.getAlignment()) != null && (aln.getHeight() != result.getHeight() || aln.getWidth() != result.getWidth()) ) {
+                     // TODO: raise a dialog box here rather than bomb out.
+               JOptionPane.showMessageDialog(Desktop.desktop, "The scores matrix does not match the alignment dimensions",
+                        "Problem reading T-COFFEE score file", JOptionPane.WARNING_MESSAGE);
+                 }
+                 if (result.annotateAlignment(alignPanel.getAlignment(), true))
+                 {
+                         tcoffeeColour.setEnabled(true);
+                         tcoffeeColour.setSelected(true);
+                         // switch to this color
+                         changeColour(new TCoffeeColourScheme(alignPanel.getAlignment()));
+                 } else {
+                   tcoffeeColour.setEnabled(false);
+                   tcoffeeColour.setSelected(false);
+                 }
+                 if (result.getWarningMessage()!=null)
+                 {
+                   JOptionPane.showMessageDialog(Desktop.desktop, result.getWarningMessage(),"Problem reading T-COFEEE score file", JOptionPane.WARNING_MESSAGE);
+                 }
+             } 
+             catch (Exception ex) {
+               JOptionPane.showMessageDialog(
+                                       Desktop.desktop, 
+                                       ex.getMessage(), 
+                                       "unexpected problem reading T-COFFEE score file", 
+                                       JOptionPane.WARNING_MESSAGE);
+
+               ex.printStackTrace();
+             }
+           }
+
+  } 
+
+  
+  @Override
+  protected void tcoffeeColorScheme_actionPerformed(ActionEvent e) {
+         changeColour( new TCoffeeColourScheme(alignPanel.getAlignment()) );
+  }
+  
+//  /**
+//   * Load the (T-Coffee) score file from the specified url 
+//   * 
+//   * @param url The absolute path from where download and read the score file
+//   * @throws IOException 
+//   */
+//  public void loadScoreFile(URL url ) throws IOException {
+//       
+//       TCoffeeScoreFile result = new TCoffeeScoreFile();
+//       result.parse( new InputStreamReader( url.openStream() ) );
+//       tcoffeeScoreFile = result;
+//  }  
+  
   public TreePanel ShowNewickTree(NewickFile nf, String title)
   {
     return ShowNewickTree(nf, title, 600, 500, 4, 5);
@@ -4099,12 +4185,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
           wsmenu.add(secstrmenu);
           wsmenu.add(dismenu);
           wsmenu.add(analymenu);
-//          final ArrayList<JMenu> submens=new ArrayList<JMenu>();
-//          submens.add(msawsmenu);
- //         submens.add(secstrmenu);
- //         submens.add(dismenu);
-  //        submens.add(analymenu);
-          
+          // final ArrayList<JMenu> submens=new ArrayList<JMenu>();
+          // submens.add(msawsmenu);
+          // submens.add(secstrmenu);
+          // submens.add(dismenu);
+          // submens.add(analymenu);
+
           // No search services yet
           // wsmenu.add(seqsrchmenu);
 
@@ -4943,31 +5029,45 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       {
         final jalview.ws.SequenceFetcher sf = SequenceFetcher
                 .getSequenceFetcherSingleton(me);
-        final String[] otherdb = sf.getOrderedSupportedSources();
-        // sf.getDbInstances(jalview.ws.dbsources.DasSequenceSource.class);
-        // jalview.util.QuickSort.sort(otherdb, otherdb);
         javax.swing.SwingUtilities.invokeLater(new Runnable()
         {
           public void run()
           {
-
+            String[] dbclasses = sf.getOrderedSupportedSources();
+            // sf.getDbInstances(jalview.ws.dbsources.DasSequenceSource.class);
+            // jalview.util.QuickSort.sort(otherdb, otherdb);
+            List<DbSourceProxy> otherdb;
             JMenu dfetch = new JMenu();
-            JMenuItem fetchr;
-            rfetch.add(dfetch);
-            int comp = 0, mcomp = 15;
+            JMenu ifetch = new JMenu();
+            JMenuItem fetchr = null;
+            int comp = 0, icomp = 0, mcomp = 15;
             String mname = null;
-            if (otherdb != null && otherdb.length > 0)
+            int dbi = 0;
+            for (String dbclass : dbclasses)
             {
-              for (int i = 0; i < otherdb.length; i++)
+              otherdb = sf.getSourceProxy(dbclass);
+              // add a single entry for this class, or submenu allowing 'fetch
+              // all' or pick one
+              if (otherdb == null || otherdb.size() < 1)
               {
-                String dbname = sf.getSourceProxy(otherdb[i]).getDbName();
-                if (mname == null)
-                {
-                  mname = "from '" + dbname + "'";
-                }
-                fetchr = new JMenuItem(otherdb[i]);
-                final String[] dassource = new String[]
-                { otherdb[i] };
+                continue;
+              }
+              // List<DbSourceProxy> dbs=otherdb;
+              // otherdb=new ArrayList<DbSourceProxy>();
+              // for (DbSourceProxy db:dbs)
+              // {
+              // if (!db.isA(DBRefSource.ALIGNMENTDB)
+              // }
+              if (mname == null)
+              {
+                mname = "From " + dbclass;
+              }
+              if (otherdb.size() == 1)
+              {
+                final DbSourceProxy[] dassource = otherdb
+                        .toArray(new DbSourceProxy[0]);
+                DbSourceProxy src = otherdb.get(0);
+                fetchr = new JMenuItem(src.getDbSource());
                 fetchr.addActionListener(new ActionListener()
                 {
 
@@ -4987,17 +5087,108 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                   }
 
                 });
-                fetchr.setToolTipText("Retrieve from " + dbname);
+                fetchr.setToolTipText("<html>"
+                        + JvSwingUtils.wrapTooltip("Retrieve from "
+                                + src.getDbName()) + "<html>");
+                dfetch.add(fetchr);
+                comp++;
+              }
+              else
+              {
+                final DbSourceProxy[] dassource = otherdb
+                        .toArray(new DbSourceProxy[0]);
+                // fetch all entry
+                DbSourceProxy src = otherdb.get(0);
+                fetchr = new JMenuItem("Fetch All '" + src.getDbSource()
+                        + "'");
+                fetchr.addActionListener(new ActionListener()
+                {
+                  public void actionPerformed(ActionEvent e)
+                  {
+                    new Thread(new Runnable()
+                    {
+
+                      public void run()
+                      {
+                        new jalview.ws.DBRefFetcher(alignPanel.av
+                                .getSequenceSelection(),
+                                alignPanel.alignFrame, dassource)
+                                .fetchDBRefs(false);
+                      }
+                    }).start();
+                  }
+                });
+
+                fetchr.setToolTipText("<html>"
+                        + JvSwingUtils.wrapTooltip("Retrieve from all "
+                                + otherdb.size() + " sources in "
+                                + src.getDbSource() + "<br>First is :"
+                                + src.getDbName()) + "<html>");
                 dfetch.add(fetchr);
-                if (comp++ == mcomp || i == (otherdb.length - 1))
+                comp++;
+                // and then build the rest of the individual menus
+                ifetch = new JMenu("Sources from " + src.getDbSource());
+                icomp = 0;
+                String imname = null;
+                int i = 0;
+                for (DbSourceProxy sproxy : otherdb)
                 {
-                  dfetch.setText(mname + " to '" + dbname + "'");
-                  rfetch.add(dfetch);
-                  dfetch = new JMenu();
-                  mname = null;
-                  comp = 0;
+                  String dbname = sproxy.getDbName();
+                  String sname = dbname.length() > 5 ? dbname.substring(0,
+                          5) + "..." : dbname;
+                  String msname = dbname.length() > 10 ? dbname.substring(
+                          0, 10) + "..." : dbname;
+                  if (imname == null)
+                  {
+                    imname = "from '" + sname + "'";
+                  }
+                  fetchr = new JMenuItem(msname);
+                  final DbSourceProxy[] dassrc =
+                  { sproxy };
+                  fetchr.addActionListener(new ActionListener()
+                  {
+
+                    public void actionPerformed(ActionEvent e)
+                    {
+                      new Thread(new Runnable()
+                      {
+
+                        public void run()
+                        {
+                          new jalview.ws.DBRefFetcher(alignPanel.av
+                                  .getSequenceSelection(),
+                                  alignPanel.alignFrame, dassrc)
+                                  .fetchDBRefs(false);
+                        }
+                      }).start();
+                    }
+
+                  });
+                  fetchr.setToolTipText("<html>"
+                          + JvSwingUtils.wrapTooltip("Retrieve from "
+                                  + dbname) + "</html>");
+                  ifetch.add(fetchr);
+                  ++i;
+                  if (++icomp >= mcomp || i == (otherdb.size()))
+                  {
+                    ifetch.setText(imname + " to '" + sname + "'");
+                    dfetch.add(ifetch);
+                    ifetch = new JMenu();
+                    imname = null;
+                    icomp = 0;
+                    comp++;
+                  }
                 }
               }
+              ++dbi;
+              if (comp >= mcomp || dbi >= (dbclasses.length))
+              {
+                dfetch.setText(mname + " to '" + dbclass + "'");
+                rfetch.add(dfetch);
+                dfetch = new JMenu();
+                mname = null;
+                comp = 0;
+              }
             }
           }
         });