JAL-3200 fast lookup of DSSP3 notation
[jalview.git] / src / jalview / schemes / ResidueProperties.java
index 751175d..18b11c1 100755 (executable)
@@ -39,14 +39,14 @@ public class ResidueProperties
 
   public static final int[] purinepyrimidineIndex;
 
-  public static final Map<String, Integer> aa3Hash = new HashMap<String, Integer>();
+  public static final Map<String, Integer> aa3Hash = new HashMap<>();
 
-  public static final Map<String, String> aa2Triplet = new HashMap<String, String>();
+  public static final Map<String, String> aa2Triplet = new HashMap<>();
 
-  public static final Map<String, String> nucleotideName = new HashMap<String, String>();
+  public static final Map<String, String> nucleotideName = new HashMap<>();
 
   // lookup from modified amino acid (e.g. MSE) to canonical form (e.g. MET)
-  public static final Map<String, String> modifications = new HashMap<String, String>();
+  public static final Map<String, String> modifications = new HashMap<>();
 
   static
   {
@@ -363,8 +363,11 @@ public class ResidueProperties
   };
 
   // Added for PurinePyrimidineColourScheme
-  public static final Color[] purinepyrimidine = {
-      new Color(255, 131, 250), // A, G, R purines purplish/orchid
+  public static final Color[] purinepyrimidine = { new Color(255, 131, 250), // A,
+                                                                             // G,
+                                                                             // R
+                                                                             // purines
+                                                                             // purplish/orchid
       new Color(64, 224, 208), // C,U, T, Y pyrimidines turquoise
       Color.white, // all other nucleotides
       Color.white // Gap
@@ -379,7 +382,7 @@ public class ResidueProperties
       Color.green, // Q
       Color.red, // E
       Color.magenta, // G
-      midBlue,// Color.red, // H
+      midBlue, // Color.red, // H
       Color.pink, // I
       Color.pink, // L
       midBlue, // K
@@ -426,9 +429,9 @@ public class ResidueProperties
       0.0 // X
   };
 
-  public static final double[] helix = { 1.42, 0.98, 0.67, 1.01, 0.70,
-      1.11, 1.51, 0.57, 1.00, 1.08, 1.21, 1.16, 1.45, 1.13, 0.57, 0.77,
-      0.83, 1.08, 0.69, 1.06, 0.84, 1.31, 1.00, 0.0 };
+  public static final double[] helix = { 1.42, 0.98, 0.67, 1.01, 0.70, 1.11,
+      1.51, 0.57, 1.00, 1.08, 1.21, 1.16, 1.45, 1.13, 0.57, 0.77, 0.83,
+      1.08, 0.69, 1.06, 0.84, 1.31, 1.00, 0.0 };
 
   public static final double helixmin = 0.57;
 
@@ -450,9 +453,9 @@ public class ResidueProperties
 
   public static final double turnmax = 1.56;
 
-  public static final double[] buried = { 1.7, 0.1, 0.4, 0.4, 4.6, 0.3,
-      0.3, 1.8, 0.8, 3.1, 2.4, 0.05, 1.9, 2.2, 0.6, 0.8, 0.7, 1.6, 0.5,
-      2.9, 0.4, 0.3, 1.358, 0.00 };
+  public static final double[] buried = { 1.7, 0.1, 0.4, 0.4, 4.6, 0.3, 0.3,
+      1.8, 0.8, 3.1, 2.4, 0.05, 1.9, 2.2, 0.6, 0.8, 0.7, 1.6, 0.5, 2.9, 0.4,
+      0.3, 1.358, 0.00 };
 
   public static final double buriedmin = 0.05;
 
@@ -493,25 +496,27 @@ public class ResidueProperties
    * Color.white, // R Color.white, // Y Color.white, // N Color.white, // Gap
    */
 
-  public static List<String> STOP = Arrays.asList("TGA", "TAA", "TAG");
+  public static String STOP = "STOP";
+
+  public static List<String> STOP_CODONS = Arrays.asList("TGA", "TAA", "TAG");
 
   public static String START = "ATG";
 
   /**
    * Nucleotide Ambiguity Codes
    */
-  public static final Map<String, String[]> ambiguityCodes = new Hashtable<String, String[]>();
+  public static final Map<String, String[]> ambiguityCodes = new Hashtable<>();
 
   /**
    * Codon triplets with additional symbols for unambiguous codons that include
    * ambiguity codes
    */
-  public static final Hashtable<String, String> codonHash2 = new Hashtable<String, String>();
+  public static final Hashtable<String, String> codonHash2 = new Hashtable<>();
 
   /**
    * all ambiguity codes for a given base
    */
-  public final static Hashtable<String, List<String>> _ambiguityCodes = new Hashtable<String, List<String>>();
+  public final static Hashtable<String, List<String>> _ambiguityCodes = new Hashtable<>();
 
   static
   {
@@ -635,7 +640,7 @@ public class ResidueProperties
         List<String> codesfor = _ambiguityCodes.get(r);
         if (codesfor == null)
         {
-          _ambiguityCodes.put(r, codesfor = new ArrayList<String>());
+          _ambiguityCodes.put(r, codesfor = new ArrayList<>());
         }
         if (!codesfor.contains(acode.getKey()))
         {
@@ -643,16 +648,16 @@ public class ResidueProperties
         }
         else
         {
-          System.err
-                  .println("Inconsistency in the IUBMB ambiguity code nomenclature table: collision for "
+          System.err.println(
+                  "Inconsistency in the IUBMB ambiguity code nomenclature table: collision for "
                           + acode.getKey() + " in residue " + r);
         }
       }
     }
     // and programmatically add in the ambiguity codes that yield the same amino
     // acid
-    String[] unambcodons = codonHash2.keySet().toArray(
-            new String[codonHash2.size()]);
+    String[] unambcodons = codonHash2.keySet()
+            .toArray(new String[codonHash2.size()]);
     for (String codon : unambcodons)
     {
       String residue = codonHash2.get(codon);
@@ -671,7 +676,8 @@ public class ResidueProperties
         }
       }
       // enumerate all combinations and test for veracity of translation
-      int tpos[] = new int[codon.length()], cpos[] = new int[codon.length()];
+      int tpos[] = new int[codon.length()],
+              cpos[] = new int[codon.length()];
       for (int i = 0; i < tpos.length; i++)
       {
         tpos[i] = -1;
@@ -751,27 +757,27 @@ public class ResidueProperties
   }
 
   // Stores residue codes/names and colours and other things
-  public static Map<String, Map<String, Integer>> propHash = new Hashtable<String, Map<String, Integer>>();
+  public static Map<String, Map<String, Integer>> propHash = new Hashtable<>();
 
-  public static Map<String, Integer> hydrophobic = new Hashtable<String, Integer>();
+  public static Map<String, Integer> hydrophobic = new Hashtable<>();
 
-  public static Map<String, Integer> polar = new Hashtable<String, Integer>();
+  public static Map<String, Integer> polar = new Hashtable<>();
 
-  public static Map<String, Integer> small = new Hashtable<String, Integer>();
+  public static Map<String, Integer> small = new Hashtable<>();
 
-  public static Map<String, Integer> positive = new Hashtable<String, Integer>();
+  public static Map<String, Integer> positive = new Hashtable<>();
 
-  public static Map<String, Integer> negative = new Hashtable<String, Integer>();
+  public static Map<String, Integer> negative = new Hashtable<>();
 
-  public static Map<String, Integer> charged = new Hashtable<String, Integer>();
+  public static Map<String, Integer> charged = new Hashtable<>();
 
-  public static Map<String, Integer> aromatic = new Hashtable<String, Integer>();
+  public static Map<String, Integer> aromatic = new Hashtable<>();
 
-  public static Map<String, Integer> aliphatic = new Hashtable<String, Integer>();
+  public static Map<String, Integer> aliphatic = new Hashtable<>();
 
-  public static Map<String, Integer> tiny = new Hashtable<String, Integer>();
+  public static Map<String, Integer> tiny = new Hashtable<>();
 
-  public static Map<String, Integer> proline = new Hashtable<String, Integer>();
+  public static Map<String, Integer> proline = new Hashtable<>();
 
   static
   {
@@ -924,7 +930,7 @@ public class ResidueProperties
     charged.put("Q", ZERO);
     charged.put("D", ONE);
     charged.put("N", ZERO); // Asparagine is polar but not
-                                          // charged.
+                            // charged.
     // Alternative would be charged and
     // negative (in basic form)?
     charged.put("S", ZERO);
@@ -1053,7 +1059,9 @@ public class ResidueProperties
   }
   static
   {
-    int[][] propMatrixF = new int[maxProteinIndex][maxProteinIndex], propMatrixPos = new int[maxProteinIndex][maxProteinIndex], propMatrixEpos = new int[maxProteinIndex][maxProteinIndex];
+    int[][] propMatrixF = new int[maxProteinIndex][maxProteinIndex],
+            propMatrixPos = new int[maxProteinIndex][maxProteinIndex],
+            propMatrixEpos = new int[maxProteinIndex][maxProteinIndex];
     for (int i = 0; i < maxProteinIndex; i++)
     {
       int maxF = 0, maxP = 0, maxEP = 0;
@@ -1143,52 +1151,56 @@ public class ResidueProperties
     String cdn = codonHash2.get(lccodon.toUpperCase());
     if ("*".equals(cdn))
     {
-      return "STOP";
+      return STOP;
     }
     return cdn;
   }
 
-  public static Hashtable<String, String> toDssp3State;
+  /*
+   * lookup of (A-Z) alternative secondary structure symbols'
+   * equivalents in DSSP3 notation
+   */
+  private static char[] toDssp3State;
   static
   {
-    toDssp3State = new Hashtable<String, String>();
-    toDssp3State.put("H", "H");
-    toDssp3State.put("E", "E");
-    toDssp3State.put("C", " ");
-    toDssp3State.put(" ", " ");
-    toDssp3State.put("T", " ");
-    toDssp3State.put("B", "E");
-    toDssp3State.put("G", "H");
-    toDssp3State.put("I", "H");
-    toDssp3State.put("X", " ");
+    toDssp3State = new char[9]; // for 'A'-'I'; extend if needed
+    Arrays.fill(toDssp3State, ' ');
+    toDssp3State['B' - 'A'] = 'E';
+    toDssp3State['E' - 'A'] = 'E';
+    toDssp3State['G' - 'A'] = 'H';
+    toDssp3State['H' - 'A'] = 'H';
+    toDssp3State['I' - 'A'] = 'H';
   }
 
   /**
    * translate from other dssp secondary structure alphabets to 3-state
    * 
-   * @param ssstring
-   * @return ssstring as a three-state secondary structure assignment.
+   * @param ssString
+   * @return ssstring
    */
-  public static String getDssp3state(String ssstring)
+  public static String getDssp3state(String ssString)
   {
-    if (ssstring == null)
+    if (ssString == null)
     {
       return null;
     }
-    StringBuffer ss = new StringBuffer();
-    for (int i = 0; i < ssstring.length(); i++)
+    int lookupSize = toDssp3State.length;
+    int len = ssString.length();
+    char[] trans = new char[len];
+    for (int i = 0; i < len; i++)
     {
-      String ssc = ssstring.substring(i, i + 1);
-      if (toDssp3State.containsKey(ssc))
+      char c = ssString.charAt(i);
+      int index = c - 'A';
+      if (index < 0 || index >= lookupSize)
       {
-        ss.append(toDssp3State.get(ssc));
+        trans[i] = ' ';
       }
       else
       {
-        ss.append(" ");
+        trans[i] = toDssp3State[index];
       }
     }
-    return ss.toString();
+    return new String(trans);
   }
 
   static
@@ -2519,7 +2531,7 @@ public class ResidueProperties
   // / cut here
   public static void main(String[] args)
   {
-    Hashtable<String, Vector<String>> aaProps = new Hashtable<String, Vector<String>>();
+    Hashtable<String, Vector<String>> aaProps = new Hashtable<>();
     System.out.println("my %aa = {");
     // invert property hashes
     for (String pname : propHash.keySet())
@@ -2530,7 +2542,7 @@ public class ResidueProperties
         Vector<String> aprops = aaProps.get(rname);
         if (aprops == null)
         {
-          aprops = new Vector<String>();
+          aprops = new Vector<>();
           aaProps.put(rname, aprops);
         }
         Integer hasprop = phash.get(rname);
@@ -2572,7 +2584,7 @@ public class ResidueProperties
   public static List<String> getResidues(boolean forNucleotide,
           boolean includeAmbiguous)
   {
-    List<String> result = new ArrayList<String>();
+    List<String> result = new ArrayList<>();
     if (forNucleotide)
     {
       for (String nuc : nucleotideName.keySet())
@@ -2625,8 +2637,8 @@ public class ResidueProperties
     {
       return '0';
     }
-    Integer index = ResidueProperties.aa3Hash.get(threeLetterCode
-            .toUpperCase());
+    Integer index = ResidueProperties.aa3Hash
+            .get(threeLetterCode.toUpperCase());
     return index == null ? '0' : aa[index].charAt(0);
   }
 }