JAL-629 Change all stdout and stderr output to use Console.outPrintln and Console...
[jalview.git] / src / jalview / analysis / GeneticCodes.java
index d07253e..4c826b2 100644 (file)
@@ -1,7 +1,26 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.analysis;
 
-import jalview.bin.Cache;
-
+import java.util.Locale;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
@@ -11,6 +30,8 @@ import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.StringTokenizer;
 
+import jalview.bin.Console;
+
 /**
  * A singleton that provides instances of genetic code translation tables
  * 
@@ -65,7 +86,7 @@ public final class GeneticCodes
       loadAmbiguityCodes(AMBIGUITY_CODES_FILE);
       loadCodes(RESOURCE_FILE);
     }
-  };
+  }
 
   /**
    * Returns the singleton instance of this class
@@ -117,6 +138,11 @@ public final class GeneticCodes
     try
     {
       InputStream is = getClass().getResourceAsStream(fileName);
+      if (is == null)
+      {
+        jalview.bin.Console.errPrintln("Resource file not found: " + fileName);
+        return;
+      }
       BufferedReader dataIn = new BufferedReader(new InputStreamReader(is));
 
       /*
@@ -135,9 +161,14 @@ public final class GeneticCodes
       }
     } catch (IOException | NullPointerException e)
     {
-      Cache.log.error(
-              "Error reading genetic codes data file: "
-              + e.getMessage());
+      Console.error("Error reading genetic codes data file " + fileName
+              + ": " + e.getMessage());
+    }
+    if (codeTables.isEmpty())
+    {
+      jalview.bin.Console.errPrintln(
+              "No genetic code tables loaded, check format of file "
+                      + fileName);
     }
   }
 
@@ -157,23 +188,35 @@ public final class GeneticCodes
     try
     {
       InputStream is = getClass().getResourceAsStream(fileName);
+      if (is == null)
+      {
+        jalview.bin.Console.errPrintln("Resource file not found: " + fileName);
+        return;
+      }
       BufferedReader dataIn = new BufferedReader(new InputStreamReader(is));
       String line = "";
       while (line != null)
       {
         line = readLine(dataIn);
-        if (line != null && !"DNA".equals(line.toUpperCase()))
+        if (line != null && !"DNA".equals(line.toUpperCase(Locale.ROOT)))
         {
           String[] tokens = line.split("\\t");
-          ambiguityCodes.put(tokens[0].toUpperCase(),
-                  tokens[1].toUpperCase());
+          if (tokens.length == 2)
+          {
+            ambiguityCodes.put(tokens[0].toUpperCase(Locale.ROOT),
+                    tokens[1].toUpperCase(Locale.ROOT));
+          }
+          else
+          {
+            jalview.bin.Console.errPrintln(
+                    "Unexpected data in " + fileName + ": " + line);
+          }
         }
       }
     } catch (IOException e)
     {
-      Cache.log.error(
-              "Error reading nucleotide ambiguity codes data file: "
-                      + e.getMessage());
+      Console.error("Error reading nucleotide ambiguity codes data file: "
+              + e.getMessage());
     }
   }
 
@@ -248,7 +291,7 @@ public final class GeneticCodes
                 line.lastIndexOf(QUOTE));
         if (aminos.length() != NUCS_COUNT_CUBED) // 4 * 4 * 4 combinations
         {
-          Cache.log.error("wrong data length in code table: " + line);
+          Console.error("wrong data length in code table: " + line);
         }
         else
         {
@@ -297,13 +340,13 @@ public final class GeneticCodes
       @Override
       public String translateCanonical(String codon)
       {
-        return codons.get(codon.toUpperCase());
+        return codons.get(codon.toUpperCase(Locale.ROOT));
       }
 
       @Override
       public String translate(String codon)
       {
-        String upper = codon.toUpperCase();
+        String upper = codon.toUpperCase(Locale.ROOT);
         String peptide = translateCanonical(upper);
 
         /*