From: Paolo Di Tommaso Date: Tue, 10 Apr 2012 11:54:33 +0000 (+0200) Subject: JAL-1065 + JAL-1066 + JAL-1067 - Added T-Coffee score size constraints X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=1379350f04bc63ca05bd428afb86717c4764755d;p=jalview.git JAL-1065 + JAL-1066 + JAL-1067 - Added T-Coffee score size constraints --- diff --git a/src/jalview/appletgui/AlignFrame.java b/src/jalview/appletgui/AlignFrame.java index c1dbf30..15904df 100644 --- a/src/jalview/appletgui/AlignFrame.java +++ b/src/jalview/appletgui/AlignFrame.java @@ -2335,7 +2335,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, ItemLis { try { - sg.cs = (ColourSchemeI) cs.getClass().newInstance(); + sg.cs = cs.getClass().newInstance(); } catch (Exception ex) { ex.printStackTrace(); @@ -3685,6 +3685,14 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, ItemLis throw new RuntimeException("The file provided does not match the T-Coffee scores file format"); } + /* + * check that the score matrix matches the alignment dimensions + */ + AlignmentI aln; + if( (aln=viewport.alignment) != null && (aln.getHeight() != file.getHeight() || aln.getWidth() != file.getWidth()) ) { + throw new RuntimeException("The scores matrix does not match the alignment dimensions"); + } + tcoffeeColour.setEnabled(true); tcoffeeScoreFile = file; diff --git a/src/jalview/bin/JalviewLite.java b/src/jalview/bin/JalviewLite.java index 5785ed1..2e5a184 100644 --- a/src/jalview/bin/JalviewLite.java +++ b/src/jalview/bin/JalviewLite.java @@ -1871,7 +1871,6 @@ public class JalviewLite extends Applet implements try { URL urlScore = new URL(sScoreFile); newAlignFrame.loadScoreFile(urlScore); - //TODO check the scores matrix matches the MSA dimensions } catch( Exception e ) { diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index b3e34fb..3ba7319 100755 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -1016,7 +1016,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } FormatAdapter f = new FormatAdapter(); String output = f.formatSequences(format, - (Alignment) viewport.alignment, // class cast exceptions will + viewport.alignment, // class cast exceptions will // occur in the distant future omitHidden, f.getCacheSuffixDefault(format), viewport.colSel); @@ -3067,7 +3067,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, { try { - sg.cs = (ColourSchemeI) cs.getClass().newInstance(); + sg.cs = cs.getClass().newInstance(); } catch (Exception ex) { } @@ -3883,9 +3883,18 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, try { TCoffeeScoreFile result = TCoffeeScoreFile.load(new File(sFilePath)); - if( result == null ) { throw new RuntimeException("The file provided does not match the T-Coffee scores file format"); } + if( result == null ) { + throw new RuntimeException("The file provided does not match the T-Coffee scores file format"); + } + + /* + * check that the score matrix matches the alignment dimensions + */ + AlignmentI aln; + if( (aln=viewport.alignment) != null && (aln.getHeight() != result.getHeight() || aln.getWidth() != result.getWidth()) ) { + throw new RuntimeException("The scores matrix does not match the alignment dimensions"); + } - // TODO check that the loaded scores matches the current MSA 'dimension' changeColour( new TCoffeeColourScheme(result) ); tcoffeeScoreFile = result; tcoffeeColour.setEnabled(true); diff --git a/src/jalview/io/TCoffeeScoreFile.java b/src/jalview/io/TCoffeeScoreFile.java index 7a3f2d9..2de4573 100644 --- a/src/jalview/io/TCoffeeScoreFile.java +++ b/src/jalview/io/TCoffeeScoreFile.java @@ -75,7 +75,8 @@ public class TCoffeeScoreFile { * insertion order. */ LinkedHashMap scores = new LinkedHashMap(); - + + Integer fWidth; /** * Parse the specified file. @@ -110,6 +111,25 @@ public class TCoffeeScoreFile { } /** + * @return The 'height' of the score matrix i.e. the numbers of score rows that should matches + * the number of sequences in the alignment + */ + public int getHeight() { + // the last entry will always be the 'global' alingment consensus scores, so it is removed + // from the 'height' count to make this value compatible with the number of sequences in the MSA + return scores != null && scores.size() > 0 ? scores.size()-1 : 0; + } + + /** + * @return The 'width' of the score matrix i.e. the number of columns. + * Since teh score value are supposd to be calculated for an 'aligned' MSA, all the entries + * have to have the same width. + */ + public int getWidth() { + return fWidth != null ? fWidth : 0; + } + + /** * The default constructor is marked as {@code protected} since this class is meant to created * through the {@link #load(File)} or {@link #load(Reader)} factory methods */ @@ -190,14 +210,27 @@ public class TCoffeeScoreFile { for( Map.Entry entry : block.items.entrySet() ) { StringBuilder scoreStringBuilder = scores.get(entry.getKey()); if( scoreStringBuilder == null ) { - throw new RuntimeException(String.format("Invalid T-Coffee score file. Sequence ID '%s' is not declared in header section", entry.getKey())); + throw new RuntimeException(String.format("Invalid T-Coffee score file: Sequence ID '%s' is not declared in header section", entry.getKey())); } scoreStringBuilder.append( entry.getValue() ); } - } + /* + * verify that all rows have the same width + */ + for( StringBuilder str : scores.values() ) { + if( fWidth == null ) { + fWidth = str.length(); + } + else if( fWidth != str.length() ) { + throw new RuntimeException("Invalid T-Coffee score file: All the score sequences must have the same length"); + } + } + + + } diff --git a/test/jalview/io/TCoffeeScoreFileTest.java b/test/jalview/io/TCoffeeScoreFileTest.java index 27df5e0..7257e14 100644 --- a/test/jalview/io/TCoffeeScoreFileTest.java +++ b/test/jalview/io/TCoffeeScoreFileTest.java @@ -42,6 +42,14 @@ public class TCoffeeScoreFileTest { TCoffeeScoreFile result = TCoffeeScoreFile.load(new File("./test/jalview/io/tcoffee.fasta_aln")); assertNull(result); } + + @Test + public void testHeightAndWidth() { + TCoffeeScoreFile result = TCoffeeScoreFile.load(new File("./test/jalview/io/tcoffee.score_ascii")); + assertNotNull(result); + assertEquals( 8, result.getHeight() ); + assertEquals( 83, result.getWidth() ); + } @Test public void testReadBlock( ) throws IOException {