From 643c94a3c546796b9a85889cea1fb65058f21623 Mon Sep 17 00:00:00 2001 From: jprocter Date: Fri, 4 May 2012 16:47:48 +0100 Subject: [PATCH] JAL-1065 JAL-1066 - refactored tcoffee score-> jalview vis object code to a create/update annotation method (will adapt to jalview 2.8 formalism when merged into that branch) --- src/jalview/appletgui/AlignFrame.java | 17 +++--- src/jalview/gui/AlignFrame.java | 21 +++---- src/jalview/io/TCoffeeScoreFile.java | 81 +++++++++++++++++++++++++- src/jalview/schemes/TCoffeeColourScheme.java | 72 ++++++++++++++--------- 4 files changed, 144 insertions(+), 47 deletions(-) diff --git a/src/jalview/appletgui/AlignFrame.java b/src/jalview/appletgui/AlignFrame.java index c1dbf30..cc2c966 100644 --- a/src/jalview/appletgui/AlignFrame.java +++ b/src/jalview/appletgui/AlignFrame.java @@ -100,7 +100,6 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, ItemLis String jalviewServletURL; - TCoffeeScoreFile tcoffeeScoreFile; public AlignFrame(AlignmentI al, jalview.bin.JalviewLite applet, String title, boolean embedded) { @@ -998,7 +997,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, ItemLis changeColour(new Blosum62ColourScheme()); } else if (source == tcoffeeColour) { - changeColour(new TCoffeeColourScheme(tcoffeeScoreFile)); + changeColour(new TCoffeeColourScheme(alignPanel.getAlignment())); } else if (source == annotationColour) { @@ -3684,12 +3683,14 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, ItemLis if( file == null ) { throw new RuntimeException("The file provided does not match the T-Coffee scores file format"); } - - tcoffeeColour.setEnabled(true); - tcoffeeScoreFile = file; - - // switch to this color - changeColour(new TCoffeeColourScheme(tcoffeeScoreFile)); + // TODO add parameter to indicate if matching should be done + if (file.annotateAlignment(alignPanel.getAlignment(), false)) + { + tcoffeeColour.setEnabled(true); + // switch to this color + changeColour(new TCoffeeColourScheme(alignPanel.getAlignment())); + } + } diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index b3e34fb..2ba0d59 100755 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -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()) ); } // /** diff --git a/src/jalview/io/TCoffeeScoreFile.java b/src/jalview/io/TCoffeeScoreFile.java index 7a3f2d9..a28e461 100644 --- a/src/jalview/io/TCoffeeScoreFile.java +++ b/src/jalview/io/TCoffeeScoreFile.java @@ -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 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= 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; + } + } diff --git a/src/jalview/schemes/TCoffeeColourScheme.java b/src/jalview/schemes/TCoffeeColourScheme.java index 740c15e..bbd2a23 100644 --- a/src/jalview/schemes/TCoffeeColourScheme.java +++ b/src/jalview/schemes/TCoffeeColourScheme.java @@ -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 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 annots = new ArrayList(); + // Search alignment to get all tcoffee annotation and pick one set of annotation to use to colour seqs. + seqMap = new IdentityHashMap(); + 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]; } } -- 1.7.10.2