Merge branch 'test_paolo_JAL-1065' into Tcoffee_JAL-1065
[jalview.git] / src / jalview / schemes / TCoffeeColourScheme.java
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];
        }
 }