JAL-1065 JAL-1066 - refactored tcoffee score-> jalview vis object code to a create...
authorjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 4 May 2012 15:47:48 +0000 (16:47 +0100)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 4 May 2012 15:47:48 +0000 (16:47 +0100)
src/jalview/appletgui/AlignFrame.java
src/jalview/gui/AlignFrame.java
src/jalview/io/TCoffeeScoreFile.java
src/jalview/schemes/TCoffeeColourScheme.java

index c1dbf30..cc2c966 100644 (file)
@@ -100,7 +100,6 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,  ItemLis
 \r
   String jalviewServletURL;\r
   \r
-  TCoffeeScoreFile tcoffeeScoreFile;\r
 \r
   public AlignFrame(AlignmentI al, jalview.bin.JalviewLite applet, String title, boolean embedded)\r
   {\r
@@ -998,7 +997,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,  ItemLis
       changeColour(new Blosum62ColourScheme());\r
     }\r
     else if (source == tcoffeeColour) {\r
-        changeColour(new TCoffeeColourScheme(tcoffeeScoreFile));\r
+        changeColour(new TCoffeeColourScheme(alignPanel.getAlignment()));\r
     }\r
     else if (source == annotationColour)\r
     {\r
@@ -3684,12 +3683,14 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,  ItemLis
          if( file == null ) {\r
                  throw new RuntimeException("The file provided does not match the T-Coffee scores file format");\r
          }\r
-         \r
-         tcoffeeColour.setEnabled(true);\r
-         tcoffeeScoreFile = file;\r
-         \r
-         // switch to this color\r
-         changeColour(new TCoffeeColourScheme(tcoffeeScoreFile));\r
+         // TODO add parameter to indicate if matching should be done\r
+         if (file.annotateAlignment(alignPanel.getAlignment(), false))\r
+         {\r
+                 tcoffeeColour.setEnabled(true);\r
+                 // switch to this color\r
+                 changeColour(new TCoffeeColourScheme(alignPanel.getAlignment()));\r
+          }\r
+\r
   }\r
   \r
   \r
index b3e34fb..2ba0d59 100755 (executable)
@@ -143,8 +143,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   Vector alignPanels = new Vector();
   
-  TCoffeeScoreFile tcoffeeScoreFile;
-
   /**
    * Last format used to load or save alignments in this window
    */
@@ -3886,11 +3884,16 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                  if( result == null ) { throw new RuntimeException("The file provided does not match the T-Coffee scores file format"); }
 
                  // TODO check that the loaded scores matches the current MSA 'dimension'
-                 changeColour( new TCoffeeColourScheme(result) );
-                 tcoffeeScoreFile = result;
-                 tcoffeeColour.setEnabled(true);
-                 tcoffeeColour.setSelected(true);
-                 
+                 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);
+                 }
              } 
              catch (Exception ex) {
                JOptionPane.showMessageDialog(
@@ -3908,9 +3911,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   
   @Override
   protected void tcoffeeColorScheme_actionPerformed(ActionEvent e) {
-         if( tcoffeeScoreFile != null ) {
-         changeColour( new TCoffeeColourScheme(tcoffeeScoreFile) );
-         }
+         changeColour( new TCoffeeColourScheme(alignPanel.getAlignment()) );
   }
   
 //  /**
index 7a3f2d9..a28e461 100644 (file)
@@ -1,5 +1,12 @@
 package jalview.io;
 
+import jalview.analysis.SequenceIdMatcher;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Annotation;
+import jalview.datamodel.SequenceI;
+
+import java.awt.Color;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -358,7 +365,77 @@ public class TCoffeeScoreFile {
                        return items.get("cons");
                }
        }
-       
-       
+       /**
+        * TCOFFEE score colourscheme
+        */
+       static final Color[] colors = {
+                       new Color( 102, 102, 255 ),     // #6666FF
+                       new Color( 0, 255, 0),          // #00FF00
+                       new Color( 102, 255, 0),        // #66FF00
+                       new Color( 204, 255, 0),        // #CCFF00
+                       new Color( 255, 255, 0),        // #FFFF00
+                       new Color( 255, 204, 0),        // #FFCC00
+                       new Color( 255, 153, 0),        // #FF9900
+                       new Color( 255, 102, 0),        // #FF6600
+                       new Color( 255, 51, 0),         // #FF3300
+                       new Color( 255, 34, 0)          // #FF2000
+               };
+       public final static String TCOFFEE_SCORE="TCoffeeScore";
+       /**
+        * generate annotation for this TCoffee score set on the given alignment
+        * @param al alignment to annotate
+        * @param matchids if true, annotate sequences based on matching sequence names
+        * @return true if alignment annotation was modified, false otherwise.
+        */
+       public boolean annotateAlignment(AlignmentI al, boolean matchids)
+       {
+         boolean added=false;
+         int i=0;
+         SequenceIdMatcher sidmatcher = new SequenceIdMatcher(al.getSequencesArray());
+         byte[][] scoreMatrix=getScoresArray();
+         // for 2.8 - we locate any existing TCoffee annotation and remove it first before adding this.
+         for (Map.Entry<String,StringBuilder> id:scores.entrySet())
+         {
+           byte[] srow=scoreMatrix[i];
+           SequenceI s;
+           if (matchids)
+           {
+             s=sidmatcher.findIdMatch(id.getKey());
+           } else {
+             s=al.getSequenceAt(i);
+           }
+           i++;
+            if (s==null && i!=scores.size() && !id.getKey().equals("cons"))
+            {
+              System.err.println("No "+(matchids ? "match ":" sequences left ")+" for TCoffee score set : "+id.getKey());
+              continue;
+            }
+            int jSize=al.getWidth()< srow.length ? al.getWidth() : srow.length;
+           Annotation[] annotations=new Annotation[al.getWidth()];
+           for (int j=0;j<jSize;j++) {
+             byte val = srow[j];
+             annotations[j]=new Annotation(s==null ? ""+val:null,s==null ? ""+val:null,(char) val,val*1f,val >= 0 && val < colors.length ? colors[val] : Color.white);
+           }
+           AlignmentAnnotation aa=null;
+           if (s!=null)
+           {
+             // TODO - set per sequence score
+             aa=new AlignmentAnnotation(TCOFFEE_SCORE, "Score for "+id.getKey(), annotations);
+             
+             aa.setSequenceRef(s);
+             aa.visible=false;
+             aa.belowAlignment=false;
+           } else {
+             aa=new AlignmentAnnotation("T-COFFEE", "TCoffee column reliability score", annotations);
+              aa.belowAlignment=true;
+             aa.visible=true;
+             
+           }
+           al.addAnnotation(aa);
+           added=true;
+         }
+         return added;
+       }
+         
 
 }
index 740c15e..bbd2a23 100644 (file)
@@ -1,8 +1,17 @@
 package jalview.schemes;
 
+import jalview.analysis.SequenceIdMatcher;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.AnnotatedCollectionI;
+import jalview.datamodel.Annotation;
+import jalview.datamodel.SequenceI;
 import jalview.io.TCoffeeScoreFile;
 
 import java.awt.Color;
+import java.util.ArrayList;
+import java.util.IdentityHashMap;
+import java.util.TreeMap;
 
 /**
  * Defines the color score for T-Coffee MSA 
@@ -29,38 +38,47 @@ public class TCoffeeColourScheme extends ResidueColourScheme {
        };
 
 
-       byte[][] scoreMatrix = null;
-
+       IdentityHashMap<SequenceI, Color[]> seqMap;
        /**
-        * Initialize the color sheme based on the content of the T-Coffee score file 
+        * the color scheme needs to look at the alignment to get and cache T-COFFEE scores 
         * 
-        * @param scoreFile
+        * @param alignment - annotated sequences to be searched
         */
-       public TCoffeeColourScheme(TCoffeeScoreFile scoreFile) {
-
-               scoreMatrix = scoreFile != null ? scoreFile.getScoresArray() : null;
-
+       public TCoffeeColourScheme(AnnotatedCollectionI alignment) {
+         alignmentChanged(alignment);
+       }
+       @Override public void alignmentChanged(AnnotatedCollectionI alignment)
+       {
+          // assume only one set of TCOFFEE scores - but could have more than one potentially.
+          ArrayList<AlignmentAnnotation> annots = new ArrayList<AlignmentAnnotation>();
+          // Search alignment to get all tcoffee annotation and pick one set of annotation to use to colour seqs.
+          seqMap = new IdentityHashMap<SequenceI, Color[]>();
+          for (AlignmentAnnotation al:alignment.getAlignmentAnnotation())
+          {
+            if (al.sequenceRef!=null && !al.belowAlignment && al.label!=null && (al.label==TCoffeeScoreFile.TCOFFEE_SCORE || al.label.equals(TCoffeeScoreFile.TCOFFEE_SCORE)))
+            {
+              annots.add(al);
+              Color[] scores=new Color[al.annotations.length];
+              int i=0;
+              for (Annotation an:al.annotations)
+              {
+                scores[i++]=(an!=null) ? an.colour : Color.white;
+              }
+              seqMap.put(al.sequenceRef, scores);
+            }
+          }
        }
-
        @Override
-       public Color findColour(char c, int j, int sequenceIndex) {
-
-               if( scoreMatrix == null ) {
-                       return Color.white;
-               }
-               
-               if( sequenceIndex<0  || sequenceIndex >= scoreMatrix.length ) {
-                       System.out.printf("Sequence index out of range for the T-Coffee color scheme. Index:  %s\n", sequenceIndex);
-                       return Color.white;
-               }
+       public Color findColour(char c, int j, SequenceI seq) {
+          Color[] cols;
 
-               byte[] seqScores = scoreMatrix[sequenceIndex]; 
-               if( j < 0 || j>= seqScores.length ) {
-                       System.out.printf("Residue index out of range for the T-Coffee color scheme. Sequence %s - residue: %s\n", sequenceIndex, j);
-                       return Color.white;
-               }
-               
-               byte val = seqScores[j];
-               return val >= 0 && val < colors.length ? colors[val] : Color.white;
+         if( seqMap==null || (cols=seqMap.get(seq))==null) {
+           return Color.white;
+         }
+         
+         if( j < 0 || j>= cols.length ) {
+           return Color.white;
+         }
+         return cols[j];
        }
 }