Merge branch 'test_paolo_JAL-1065' into Tcoffee_JAL-1065
[jalview.git] / src / jalview / io / TCoffeeScoreFile.java
index 2de4573..6cab3ae 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;
@@ -391,7 +398,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;
+       }
+         
 
 }