JAL-1065 + JAL-1066 + JAL-1067 - Added T-Coffee score size constraints
[jalview.git] / src / jalview / io / TCoffeeScoreFile.java
index 7a3f2d9..2de4573 100644 (file)
@@ -75,7 +75,8 @@ public class TCoffeeScoreFile {
         * insertion order. 
         */
        LinkedHashMap<String,StringBuilder> scores = new LinkedHashMap<String,StringBuilder>();
-       
+
+       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<String,String> 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");
+                       }
+               }
+               
+               
+               
        }