JAL-1067 - T-Coffee score file reader
authorPaolo Di Tommaso <paolo.ditommaso@gmail.com>
Mon, 9 Apr 2012 19:21:14 +0000 (21:21 +0200)
committerPaolo Di Tommaso <paolo.ditommaso@gmail.com>
Mon, 9 Apr 2012 19:21:14 +0000 (21:21 +0200)
src/jalview/io/TCoffeeScoreFile.java
test/jalview/io/TCoffeeScoreFileTest.java

index df00986..7a3f2d9 100644 (file)
@@ -78,6 +78,44 @@ public class TCoffeeScoreFile {
        
 
        /**
+        * Parse the specified file.
+        * 
+        * @param file The file to be parsed 
+        */
+       public static TCoffeeScoreFile load(File file) {
+               try {
+                       return load(new FileReader(file));
+               } 
+               catch (FileNotFoundException e) {
+                       throw new RuntimeException(e);
+               }
+       }
+       
+       /**
+        * Parse the provided reader for the T-Coffee scores file format
+        * 
+        * @param reader 
+        */
+       public static TCoffeeScoreFile load(Reader reader) {
+
+               try {
+                       BufferedReader in = (BufferedReader) (reader instanceof BufferedReader ? reader : new BufferedReader(reader));
+                       TCoffeeScoreFile result = new TCoffeeScoreFile();
+                       result.doParsing(in);
+                       return result.header != null && result.scores != null ? result : null;
+               }
+               catch( Exception e) {
+                       throw new RuntimeException(e);
+               }
+       }
+               
+       /**
+        * 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
+        */
+       protected TCoffeeScoreFile() { } 
+       
+       /**
         * Get the string of score values for the specified seqeunce ID. 
         * @param id The sequence ID 
         * @return The scores as a string of values e.g. {@code 99999987-------432}. 
@@ -122,43 +160,17 @@ public class TCoffeeScoreFile {
                return result;
        }
        
-       /**
-        * Parse the specified file.
-        * 
-        * @param file The file to be parsed 
-        */
-       public void parse(File file) {
-               try {
-                       parse(new FileReader(file));
-               } 
-               catch (FileNotFoundException e) {
-                       throw new RuntimeException(e);
-               }
-       }
-       
-       /**
-        * Parse the provided reader for the T-Coffee scores file format
-        * 
-        * @param reader 
-        */
-       public void parse(Reader reader) {
 
-               try {
-                       BufferedReader in = (BufferedReader) (reader instanceof BufferedReader ? reader : new BufferedReader(reader));
-                       doParsing(in);
-               }
-               catch( Exception e) {
-                       throw new RuntimeException(e);
-               }
-       }
-       
        private void doParsing(BufferedReader in) throws IOException {
 
                /*
                 * read the header
                 */
                header = readHeader(in);
+
+               if( header == null ) { return; }
                
+       
                /*
                 * initilize the structure
                 */
index f654ba1..27df5e0 100644 (file)
@@ -36,6 +36,13 @@ public class TCoffeeScoreFileTest {
                assertEquals( 90, header.getScoreFor("cons") );
        }
        
+       
+       @Test
+       public void testWrongFile() {
+               TCoffeeScoreFile result = TCoffeeScoreFile.load(new File("./test/jalview/io/tcoffee.fasta_aln"));
+               assertNull(result);
+       } 
+       
        @Test
        public void testReadBlock( ) throws IOException {
                
@@ -70,8 +77,7 @@ public class TCoffeeScoreFileTest {
        @Test
        public void testParse() throws FileNotFoundException {
 
-               TCoffeeScoreFile parser = new TCoffeeScoreFile();
-               parser.parse(new BufferedReader(new FileReader(SCORE_FILE)) );
+               TCoffeeScoreFile parser = TCoffeeScoreFile.load(new BufferedReader(new FileReader(SCORE_FILE)) );
 
                assertEquals( "999999999999999999999999998762112222543211112134----------5666642367889999999999889", parser.getScoresFor("1PHT") );
                assertEquals( "99999999999999999999999999987-------4322----22341111111111676653-355679999999999889", parser.getScoresFor("1BB9") );
@@ -88,8 +94,7 @@ public class TCoffeeScoreFileTest {
        @Test
        public void testGetAsList() throws FileNotFoundException {
                
-               TCoffeeScoreFile parser = new TCoffeeScoreFile();
-               parser.parse(new BufferedReader(new FileReader(SCORE_FILE)) );
+               TCoffeeScoreFile parser = TCoffeeScoreFile.load(new BufferedReader(new FileReader(SCORE_FILE)) );
 
                List<String> scores = parser.getScoresList();
                assertEquals( "999999999999999999999999998762112222543211112134----------5666642367889999999999889", scores.get(0) );
@@ -108,8 +113,7 @@ public class TCoffeeScoreFileTest {
        @Test
        public void testGetAsArray() throws FileNotFoundException {
                
-               TCoffeeScoreFile parser = new TCoffeeScoreFile();
-               parser.parse(new BufferedReader(new FileReader(SCORE_FILE)) );
+               TCoffeeScoreFile parser = TCoffeeScoreFile.load(new BufferedReader(new FileReader(SCORE_FILE)) );
 
                byte[][] scores = parser.getScoresArray();