From c3280dffcb908ebb0e6b1ddc252a63c6ae20c623 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 5 Mar 2015 12:47:39 +0000 Subject: [PATCH] JAL-845 minor tidy / tests / javadoc tweaks --- src/jalview/analysis/AAFrequency.java | 60 +++++---- src/jalview/analysis/AlignmentSorter.java | 2 +- src/jalview/analysis/AlignmentUtils.java | 2 +- src/jalview/io/MSFfile.java | 15 +-- src/jalview/schemes/ClustalxColourScheme.java | 178 +++++++++++++------------ src/jalview/util/QuickSort.java | 51 +++++-- src/jalview/ws/jws2/JPred301Client.java | 13 +- test/jalview/analysis/AAFrequencyTest.java | 115 ++++++++++++++++ test/jalview/util/QuickSortTest.java | 19 +++ 9 files changed, 322 insertions(+), 133 deletions(-) create mode 100644 test/jalview/analysis/AAFrequencyTest.java diff --git a/src/jalview/analysis/AAFrequency.java b/src/jalview/analysis/AAFrequency.java index 69d32cb..8dfda39 100755 --- a/src/jalview/analysis/AAFrequency.java +++ b/src/jalview/analysis/AAFrequency.java @@ -20,10 +20,13 @@ */ package jalview.analysis; -import java.util.*; - +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.Annotation; +import jalview.datamodel.SequenceI; import jalview.util.Format; -import jalview.datamodel.*; + +import java.util.Hashtable; +import java.util.List; /** * Takes in a vector or array of sequences and column start and column end and @@ -36,8 +39,8 @@ import jalview.datamodel.*; */ public class AAFrequency { - // No need to store 1000s of strings which are not - // visible to the user. + private static final int TO_UPPER_CASE = 'A' - 'a'; // -32 + public static final String MAXCOUNT = "C"; public static final String MAXRESIDUE = "R"; @@ -48,6 +51,19 @@ public class AAFrequency public static final String PROFILE = "P"; + /* + * Quick look-up of String value of char 'A' to 'Z' + */ + private static final String[] CHARS = new String['Z' - 'A' + 1]; + + static + { + for (char c = 'A'; c <= 'Z'; c++) + { + CHARS[c - 'A'] = String.valueOf(c); + } + } + public static final Hashtable[] calculate(List list, int start, int end) { @@ -83,16 +99,11 @@ public class AAFrequency } public static final void calculate(SequenceI[] sequences, int start, - int end, Hashtable[] result) - { - calculate(sequences, start, end, result, false); - } - - public static final void calculate(SequenceI[] sequences, int start, int end, Hashtable[] result, boolean profile) { Hashtable residueHash; - int maxCount, nongap, i, j, v, jSize = sequences.length; + int maxCount, nongap, i, j, v; + int jSize = sequences.length; String maxResidue; char c = '-'; float percentage; @@ -134,7 +145,7 @@ public class AAFrequency } else if ('a' <= c && c <= 'z') { - c -= 32; // ('a' - 'A'); + c += TO_UPPER_CASE; } nongap++; @@ -153,7 +164,7 @@ public class AAFrequency } else { - for (v = 'A'; v < 'Z'; v++) + for (v = 'A'; v <= 'Z'; v++) { if (values[v] < 2 || values[v] < maxCount) { @@ -162,11 +173,11 @@ public class AAFrequency if (values[v] > maxCount) { - maxResidue = String.valueOf((char) v); + maxResidue = CHARS[v - 'A']; } else if (values[v] == maxCount) { - maxResidue += String.valueOf((char) v); + maxResidue += CHARS[v - 'A']; } maxCount = values[v]; } @@ -218,9 +229,7 @@ public class AAFrequency { completeConsensus(consensus, hconsensus, iStart, width, ignoreGapsInConsensusCalculation, includeAllConsSymbols, null, - nseq); // new - // char[] - // { 'A', 'C', 'G', 'T', 'U' }); + nseq); } public static void completeConsensus(AlignmentAnnotation consensus, @@ -356,8 +365,10 @@ public class AAFrequency int[] rtnval = new int[64]; int[][] profile = (int[][]) hconsensus.get(AAFrequency.PROFILE); if (profile == null) + { return null; - Object[] ca = new Object[profile[0].length]; + } + char[][] ca = new char[profile[0].length][]; float[] vl = new float[profile[0].length]; for (int c = 0; c < ca.length; c++) { @@ -365,16 +376,15 @@ public class AAFrequency { (char) c }; vl[c] = profile[0][c]; } - ; jalview.util.QuickSort.sort(vl, ca); rtnval[0] = 2; rtnval[1] = 0; - for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--) + for (int c = ca.length - 1; profile[0][ca[c][0]] > 0; c--) { - if (((char[]) ca[c])[0] != '-') + if (ca[c][0] != '-') { - rtnval[rtnval[0]++] = ((char[]) ca[c])[0]; - rtnval[rtnval[0]] = (int) (profile[0][((char[]) ca[c])[0]] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1 + rtnval[rtnval[0]++] = ca[c][0]; + rtnval[rtnval[0]] = (int) (profile[0][ca[c][0]] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]); rtnval[1] += rtnval[rtnval[0]++]; } diff --git a/src/jalview/analysis/AlignmentSorter.java b/src/jalview/analysis/AlignmentSorter.java index 4f52741..44cde6e 100755 --- a/src/jalview/analysis/AlignmentSorter.java +++ b/src/jalview/analysis/AlignmentSorter.java @@ -129,7 +129,7 @@ public class AlignmentSorter seqs[i] = align.getSequenceAt(i); } - QuickSort.sort(scores, 0, scores.length - 1, seqs); + QuickSort.sort(scores, seqs); setReverseOrder(align, seqs); } diff --git a/src/jalview/analysis/AlignmentUtils.java b/src/jalview/analysis/AlignmentUtils.java index 74406dc..6b7d18b 100644 --- a/src/jalview/analysis/AlignmentUtils.java +++ b/src/jalview/analysis/AlignmentUtils.java @@ -405,7 +405,7 @@ public class AlignmentUtils // TODO there may be one AlignedCodonFrame per dataset sequence, or one with // all mappings. Would it help to constrain this? List mappings = al.getCodonFrame(seq); - if (mappings == null) + if (mappings == null || mappings.isEmpty()) { return false; } diff --git a/src/jalview/io/MSFfile.java b/src/jalview/io/MSFfile.java index 05765e3..621122c 100755 --- a/src/jalview/io/MSFfile.java +++ b/src/jalview/io/MSFfile.java @@ -20,11 +20,14 @@ */ package jalview.io; -import java.io.*; -import java.util.*; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceI; +import jalview.util.Format; -import jalview.datamodel.*; -import jalview.util.*; +import java.io.IOException; +import java.util.Hashtable; +import java.util.StringTokenizer; +import java.util.Vector; /** * DOCUMENT ME! @@ -63,10 +66,6 @@ public class MSFfile extends AlignFile super(source); } - { - // TODO Auto-generated constructor stub - } - /** * DOCUMENT ME! */ diff --git a/src/jalview/schemes/ClustalxColourScheme.java b/src/jalview/schemes/ClustalxColourScheme.java index 92d2dd7..6bc5cb8 100755 --- a/src/jalview/schemes/ClustalxColourScheme.java +++ b/src/jalview/schemes/ClustalxColourScheme.java @@ -25,41 +25,47 @@ import jalview.datamodel.SequenceCollectionI; import jalview.datamodel.SequenceI; import java.awt.Color; -import java.util.Hashtable; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Vector; -public class ClustalxColourScheme extends ResidueColourScheme // implements -// IParameterizable +public class ClustalxColourScheme extends ResidueColourScheme { - public static Hashtable colhash = new Hashtable(); + private static final int EIGHTY_FIVE = 85; - Hashtable[] cons; + private static final int FIFTY = 50; - int[][] cons2; + private static final int EIGHTY = 80; - ConsensusColour[] colours; + private static final int SIXTY = 60; - ConsensusColour[] ResidueColour; + /* + * Map from conventional colour names to Clustal version of the same + */ + private static Map colhash = new HashMap(); + + private int[][] cons2; + + private ConsensusColour[] colours; - int size; + private ConsensusColour[] residueColour; - Consensus[] conses = new Consensus[32]; + private int size; - Vector colourTable = new Vector(); + private Consensus[] conses = new Consensus[32]; private boolean includeGaps = true; + static { - colhash.put("RED", new Color((float) 0.9, (float) 0.2, (float) 0.1)); - colhash.put("BLUE", new Color((float) 0.5, (float) 0.7, (float) 0.9)); - colhash.put("GREEN", new Color((float) 0.1, (float) 0.8, (float) 0.1)); - colhash.put("ORANGE", new Color((float) 0.9, (float) 0.6, (float) 0.3)); - colhash.put("CYAN", new Color((float) 0.1, (float) 0.7, (float) 0.7)); - colhash.put("PINK", new Color((float) 0.9, (float) 0.5, (float) 0.5)); - colhash.put("MAGENTA", new Color((float) 0.8, (float) 0.3, (float) 0.8)); - colhash.put("YELLOW", new Color((float) 0.8, (float) 0.8, (float) 0.0)); + colhash.put(Color.RED, new Color(0.9f, 0.2f, 0.1f)); + colhash.put(Color.BLUE, new Color(0.5f, 0.7f, 0.9f)); + colhash.put(Color.GREEN, new Color(0.1f, 0.8f, 0.1f)); + colhash.put(Color.ORANGE, new Color(0.9f, 0.6f, 0.3f)); + colhash.put(Color.CYAN, new Color(0.1f, 0.7f, 0.7f)); + colhash.put(Color.PINK, new Color(0.9f, 0.5f, 0.5f)); + colhash.put(Color.MAGENTA, new Color(0.8f, 0.3f, 0.8f)); + colhash.put(Color.YELLOW, new Color(0.8f, 0.8f, 0.0f)); } public ClustalxColourScheme(AnnotatedCollectionI alignment, @@ -122,36 +128,36 @@ public class ClustalxColourScheme extends ResidueColourScheme // implements public void makeColours() { - conses[0] = new Consensus("WLVIMAFCYHP", 60); - conses[1] = new Consensus("WLVIMAFCYHP", 80); - conses[2] = new Consensus("ED", 50); - conses[3] = new Consensus("KR", 60); - conses[4] = new Consensus("G", 50); - conses[5] = new Consensus("N", 50); - conses[6] = new Consensus("QE", 50); - conses[7] = new Consensus("P", 50); - conses[8] = new Consensus("TS", 50); - - conses[26] = new Consensus("A", 85); - conses[27] = new Consensus("C", 85); - conses[10] = new Consensus("E", 85); - conses[11] = new Consensus("F", 85); - conses[12] = new Consensus("G", 85); - conses[13] = new Consensus("H", 85); - conses[14] = new Consensus("I", 85); - conses[15] = new Consensus("L", 85); - conses[16] = new Consensus("M", 85); - conses[17] = new Consensus("N", 85); - conses[18] = new Consensus("P", 85); - conses[19] = new Consensus("Q", 85); - conses[20] = new Consensus("R", 85); - conses[21] = new Consensus("S", 85); - conses[22] = new Consensus("T", 85); - conses[23] = new Consensus("V", 85); - conses[24] = new Consensus("W", 85); - conses[25] = new Consensus("Y", 85); - conses[28] = new Consensus("K", 85); - conses[29] = new Consensus("D", 85); + conses[0] = new Consensus("WLVIMAFCYHP", SIXTY); + conses[1] = new Consensus("WLVIMAFCYHP", EIGHTY); + conses[2] = new Consensus("ED", FIFTY); + conses[3] = new Consensus("KR", SIXTY); + conses[4] = new Consensus("G", FIFTY); + conses[5] = new Consensus("N", FIFTY); + conses[6] = new Consensus("QE", FIFTY); + conses[7] = new Consensus("P", FIFTY); + conses[8] = new Consensus("TS", FIFTY); + + conses[26] = new Consensus("A", EIGHTY_FIVE); + conses[27] = new Consensus("C", EIGHTY_FIVE); + conses[10] = new Consensus("E", EIGHTY_FIVE); + conses[11] = new Consensus("F", EIGHTY_FIVE); + conses[12] = new Consensus("G", EIGHTY_FIVE); + conses[13] = new Consensus("H", EIGHTY_FIVE); + conses[14] = new Consensus("I", EIGHTY_FIVE); + conses[15] = new Consensus("L", EIGHTY_FIVE); + conses[16] = new Consensus("M", EIGHTY_FIVE); + conses[17] = new Consensus("N", EIGHTY_FIVE); + conses[18] = new Consensus("P", EIGHTY_FIVE); + conses[19] = new Consensus("Q", EIGHTY_FIVE); + conses[20] = new Consensus("R", EIGHTY_FIVE); + conses[21] = new Consensus("S", EIGHTY_FIVE); + conses[22] = new Consensus("T", EIGHTY_FIVE); + conses[23] = new Consensus("V", EIGHTY_FIVE); + conses[24] = new Consensus("W", EIGHTY_FIVE); + conses[25] = new Consensus("Y", EIGHTY_FIVE); + conses[28] = new Consensus("K", EIGHTY_FIVE); + conses[29] = new Consensus("D", EIGHTY_FIVE); conses[30] = new Consensus("G", 0); conses[31] = new Consensus("P", 0); @@ -161,15 +167,15 @@ public class ClustalxColourScheme extends ResidueColourScheme // implements Consensus[] tmp8 = new Consensus[1]; tmp8[0] = conses[30]; // G - colours[7] = new ConsensusColour((Color) colhash.get("ORANGE"), tmp8); + colours[7] = new ConsensusColour(colhash.get(Color.ORANGE), tmp8); Consensus[] tmp9 = new Consensus[1]; tmp9[0] = conses[31]; // P - colours[8] = new ConsensusColour((Color) colhash.get("YELLOW"), tmp9); + colours[8] = new ConsensusColour(colhash.get(Color.YELLOW), tmp9); Consensus[] tmp10 = new Consensus[1]; tmp10[0] = conses[27]; // C - colours[9] = new ConsensusColour((Color) colhash.get("PINK"), tmp8); + colours[9] = new ConsensusColour(colhash.get(Color.PINK), tmp8); Consensus[] tmp1 = new Consensus[14]; tmp1[0] = conses[0]; // % @@ -186,9 +192,9 @@ public class ClustalxColourScheme extends ResidueColourScheme // implements tmp1[11] = conses[25]; // Y tmp1[12] = conses[18]; // P tmp1[13] = conses[19]; // p - colours[0] = new ConsensusColour((Color) colhash.get("BLUE"), tmp1); + colours[0] = new ConsensusColour(colhash.get(Color.BLUE), tmp1); - colours[10] = new ConsensusColour((Color) colhash.get("CYAN"), tmp1); + colours[10] = new ConsensusColour(colhash.get(Color.CYAN), tmp1); Consensus[] tmp2 = new Consensus[5]; tmp2[0] = conses[8]; // t @@ -196,14 +202,14 @@ public class ClustalxColourScheme extends ResidueColourScheme // implements tmp2[2] = conses[22]; // T tmp2[3] = conses[0]; // % tmp2[4] = conses[1]; // # - colours[1] = new ConsensusColour((Color) colhash.get("GREEN"), tmp2); + colours[1] = new ConsensusColour(colhash.get(Color.GREEN), tmp2); Consensus[] tmp3 = new Consensus[3]; tmp3[0] = conses[17]; // N tmp3[1] = conses[29]; // D tmp3[2] = conses[5]; // n - colours[2] = new ConsensusColour((Color) colhash.get("GREEN"), tmp3); + colours[2] = new ConsensusColour(colhash.get(Color.GREEN), tmp3); Consensus[] tmp4 = new Consensus[6]; tmp4[0] = conses[6]; // q = QE @@ -212,14 +218,14 @@ public class ClustalxColourScheme extends ResidueColourScheme // implements tmp4[3] = conses[3]; // + tmp4[4] = conses[28]; // K tmp4[5] = conses[20]; // R - colours[3] = new ConsensusColour((Color) colhash.get("GREEN"), tmp4); + colours[3] = new ConsensusColour(colhash.get(Color.GREEN), tmp4); Consensus[] tmp5 = new Consensus[4]; tmp5[0] = conses[3]; // + tmp5[1] = conses[28]; // K tmp5[2] = conses[20]; // R tmp5[3] = conses[19]; // Q - colours[4] = new ConsensusColour((Color) colhash.get("RED"), tmp5); + colours[4] = new ConsensusColour(colhash.get(Color.RED), tmp5); Consensus[] tmp6 = new Consensus[6]; tmp6[0] = conses[3]; // - @@ -228,7 +234,7 @@ public class ClustalxColourScheme extends ResidueColourScheme // implements tmp6[3] = conses[6]; // QE tmp6[4] = conses[19]; // Q tmp6[5] = conses[2]; // DE - colours[5] = new ConsensusColour((Color) colhash.get("MAGENTA"), tmp6); + colours[5] = new ConsensusColour(colhash.get(Color.MAGENTA), tmp6); Consensus[] tmp7 = new Consensus[5]; tmp7[0] = conses[3]; // - @@ -236,30 +242,30 @@ public class ClustalxColourScheme extends ResidueColourScheme // implements tmp7[2] = conses[10]; // E tmp7[3] = conses[17]; // N tmp7[4] = conses[2]; // DE - colours[6] = new ConsensusColour((Color) colhash.get("MAGENTA"), tmp7); + colours[6] = new ConsensusColour(colhash.get(Color.MAGENTA), tmp7); // Now attach the ConsensusColours to the residue letters - ResidueColour = new ConsensusColour[20]; - ResidueColour[0] = colours[0]; // A - ResidueColour[1] = colours[4]; // R - ResidueColour[2] = colours[2]; // N - ResidueColour[3] = colours[6]; // D - ResidueColour[4] = colours[0]; // C - ResidueColour[5] = colours[3]; // Q - ResidueColour[6] = colours[5]; // E - ResidueColour[7] = colours[7]; // G - ResidueColour[8] = colours[10]; // H - ResidueColour[9] = colours[0]; // I - ResidueColour[10] = colours[0]; // L - ResidueColour[11] = colours[4]; // K - ResidueColour[12] = colours[0]; // M - ResidueColour[13] = colours[0]; // F - ResidueColour[14] = colours[8]; // P - ResidueColour[15] = colours[1]; // S - ResidueColour[16] = colours[1]; // T - ResidueColour[17] = colours[0]; // W - ResidueColour[18] = colours[10]; // Y - ResidueColour[19] = colours[0]; // V + residueColour = new ConsensusColour[20]; + residueColour[0] = colours[0]; // A + residueColour[1] = colours[4]; // R + residueColour[2] = colours[2]; // N + residueColour[3] = colours[6]; // D + residueColour[4] = colours[0]; // C + residueColour[5] = colours[3]; // Q + residueColour[6] = colours[5]; // E + residueColour[7] = colours[7]; // G + residueColour[8] = colours[10]; // H + residueColour[9] = colours[0]; // I + residueColour[10] = colours[0]; // L + residueColour[11] = colours[4]; // K + residueColour[12] = colours[0]; // M + residueColour[13] = colours[0]; // F + residueColour[14] = colours[8]; // P + residueColour[15] = colours[1]; // S + residueColour[16] = colours[1]; // T + residueColour[17] = colours[0]; // W + residueColour[18] = colours[10]; // Y + residueColour[19] = colours[0]; // V } @Override @@ -288,12 +294,12 @@ public class ClustalxColourScheme extends ResidueColourScheme // implements return currentColour; } - for (int k = 0; k < ResidueColour[i].conses.length; k++) + for (int k = 0; k < residueColour[i].conses.length; k++) { - if (ResidueColour[i].conses[k].isConserved(cons2, j, size, + if (residueColour[i].conses[k].isConserved(cons2, j, size, includeGaps)) { - currentColour = ResidueColour[i].c; + currentColour = residueColour[i].c; } } @@ -301,7 +307,7 @@ public class ClustalxColourScheme extends ResidueColourScheme // implements { if (conses[27].isConserved(cons2, j, size, includeGaps)) { - currentColour = (Color) colhash.get("PINK"); + currentColour = colhash.get(Color.PINK); } } diff --git a/src/jalview/util/QuickSort.java b/src/jalview/util/QuickSort.java index e3bb514..cedf656 100755 --- a/src/jalview/util/QuickSort.java +++ b/src/jalview/util/QuickSort.java @@ -20,29 +20,64 @@ */ package jalview.util; +/** + * A class to perform efficient sorting of arrays of objects based on arrays of + * scores or other attributes. For example, residues by frequency. + * + * @author gmcarstairs + * + */ public class QuickSort { + /** + * Sorts both arrays with respect to ascending order of the items in the first + * array. + * + * @param arr + * @param s + */ public static void sort(int[] arr, Object[] s) { sort(arr, 0, arr.length - 1, s); } + /** + * Sorts both arrays with respect to ascending order of the items in the first + * array. + * + * @param arr + * @param s + */ public static void sort(float[] arr, Object[] s) { sort(arr, 0, arr.length - 1, s); } + /** + * Sorts both arrays with respect to ascending order of the items in the first + * array. + * + * @param arr + * @param s + */ public static void sort(double[] arr, Object[] s) { sort(arr, 0, arr.length - 1, s); } + /** + * Sorts both arrays with respect to descending order of the items in the + * first array. + * + * @param arr + * @param s + */ public static void sort(String[] arr, Object[] s) { stringSort(arr, 0, arr.length - 1, s); } - public static void stringSort(String[] arr, int p, int r, Object[] s) + static void stringSort(String[] arr, int p, int r, Object[] s) { int q; @@ -54,7 +89,7 @@ public class QuickSort } } - public static void sort(float[] arr, int p, int r, Object[] s) + static void sort(float[] arr, int p, int r, Object[] s) { int q; @@ -66,7 +101,7 @@ public class QuickSort } } - public static void sort(double[] arr, int p, int r, Object[] s) + static void sort(double[] arr, int p, int r, Object[] s) { int q; @@ -78,7 +113,7 @@ public class QuickSort } } - public static void sort(int[] arr, int p, int r, Object[] s) + static void sort(int[] arr, int p, int r, Object[] s) { int q; @@ -90,7 +125,7 @@ public class QuickSort } } - private static int partition(float[] arr, int p, int r, Object[] s) + static int partition(float[] arr, int p, int r, Object[] s) { float x = arr[p]; int i = p - 1; @@ -125,7 +160,7 @@ public class QuickSort } } - private static int partition(int[] arr, int p, int r, Object[] s) + static int partition(int[] arr, int p, int r, Object[] s) { int x = arr[p]; int i = p - 1; @@ -160,7 +195,7 @@ public class QuickSort } } - private static int partition(double[] arr, int p, int r, Object[] s) + static int partition(double[] arr, int p, int r, Object[] s) { double x = arr[p]; int i = p - 1; @@ -195,7 +230,7 @@ public class QuickSort } } - private static int stringPartition(String[] arr, int p, int r, Object[] s) + static int stringPartition(String[] arr, int p, int r, Object[] s) { String x = arr[p]; int i = p - 1; diff --git a/src/jalview/ws/jws2/JPred301Client.java b/src/jalview/ws/jws2/JPred301Client.java index 8328d45..41d8a7f 100644 --- a/src/jalview/ws/jws2/JPred301Client.java +++ b/src/jalview/ws/jws2/JPred301Client.java @@ -35,6 +35,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; import compbio.data.sequence.FastaSequence; import compbio.data.sequence.JpredAlignment; @@ -90,15 +92,18 @@ public class JPred301Client extends JabawsMsaInterfaceAlignCalcWorker return "calculating consensus secondary structure prediction using JPred service"; } - private static HashMap jpredRowLabels = new HashMap(); + private static Map jpredRowLabels = new HashMap(); - private static HashSet jpredRes_graph, jpredRes_ssonly; + private static final Set jpredRes_graph; + + private static final Set jpredRes_ssonly; + static { - jpredRes_ssonly = new HashSet(); + jpredRes_ssonly = new HashSet(); jpredRes_ssonly.add("jnetpred".toLowerCase()); jpredRes_ssonly.add("jnetpssm".toLowerCase()); jpredRes_ssonly.add("jnethmm".toLowerCase()); - jpredRes_graph = new HashSet(); + jpredRes_graph = new HashSet(); jpredRes_graph.add("jnetconf".toLowerCase()); jpredRes_graph.add("jnet burial".toLowerCase()); } diff --git a/test/jalview/analysis/AAFrequencyTest.java b/test/jalview/analysis/AAFrequencyTest.java new file mode 100644 index 0000000..1c30c79 --- /dev/null +++ b/test/jalview/analysis/AAFrequencyTest.java @@ -0,0 +1,115 @@ +package jalview.analysis; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceI; + +import java.util.Hashtable; + +import org.junit.Test; + +public class AAFrequencyTest +{ + private static final String C = AAFrequency.MAXCOUNT; + + private static final String R = AAFrequency.MAXRESIDUE; + + private static final String G = AAFrequency.PID_GAPS; + + private static final String N = AAFrequency.PID_NOGAPS; + + private static final String P = AAFrequency.PROFILE; + + @Test + public void testCalculate_noProfile() + { + SequenceI seq1 = new Sequence("Seq1", "CAGT"); + SequenceI seq2 = new Sequence("Seq2", "CACT"); + SequenceI seq3 = new Sequence("Seq3", "C--G"); + SequenceI seq4 = new Sequence("Seq4", "CA-t"); + SequenceI[] seqs = new SequenceI[] + { seq1, seq2, seq3, seq4 }; + Hashtable[] result = new Hashtable[seq1.getLength()]; + + AAFrequency.calculate(seqs, 0, seq1.getLength(), result, false); + Hashtable col = result[0]; + assertEquals(100f, (Float) col.get(G), 0.0001f); + assertEquals(100f, (Float) col.get(N), 0.0001f); + assertEquals(4, col.get(C)); + assertEquals("C", col.get(R)); + assertNull(col.get(P)); + col = result[1]; + assertEquals(75f, (Float) col.get(G), 0.0001f); + assertEquals(100f, (Float) col.get(N), 0.0001f); + assertEquals(3, col.get(C)); + assertEquals("A", col.get(R)); + col = result[2]; + assertEquals(0f, (Float) col.get(G), 0.0001f); + assertEquals(0f, (Float) col.get(N), 0.0001f); + assertEquals(0, col.get(C)); + assertEquals("-", col.get(R)); + col = result[3]; + assertEquals(75f, (Float) col.get(G), 0.0001f); + assertEquals(75f, (Float) col.get(N), 0.0001f); + assertEquals(3, col.get(C)); + assertEquals("T", col.get(R)); + } + + @Test + public void testCalculate_withProfile() + { + SequenceI seq1 = new Sequence("Seq1", "CAGT"); + SequenceI seq2 = new Sequence("Seq2", "CACT"); + SequenceI seq3 = new Sequence("Seq3", "C--G"); + SequenceI seq4 = new Sequence("Seq4", "CA-t"); + SequenceI[] seqs = new SequenceI[] + { seq1, seq2, seq3, seq4 }; + Hashtable[] result = new Hashtable[seq1.getLength()]; + + AAFrequency.calculate(seqs, 0, seq1.getLength(), result, true); + int[][] profile = (int[][]) result[0].get(P); + assertEquals(4, profile[0]['C']); + assertEquals(4, profile[1][0]); // no of seqs + assertEquals(4, profile[1][1]); // nongapped in column + + profile = (int[][]) result[1].get(P); + assertEquals(3, profile[0]['A']); + assertEquals(4, profile[1][0]); + assertEquals(3, profile[1][1]); + + profile = (int[][]) result[2].get(P); + assertEquals(1, profile[0]['G']); + assertEquals(1, profile[0]['C']); + assertEquals(4, profile[1][0]); + assertEquals(2, profile[1][1]); + + profile = (int[][]) result[3].get(P); + assertEquals(3, profile[0]['T']); + assertEquals(1, profile[0]['G']); + assertEquals(4, profile[1][0]); + assertEquals(4, profile[1][1]); + } + + @Test + public void testCalculate_withProfileTiming() + { + SequenceI seq1 = new Sequence("Seq1", "CAGT"); + SequenceI seq2 = new Sequence("Seq2", "CACT"); + SequenceI seq3 = new Sequence("Seq3", "C--G"); + SequenceI seq4 = new Sequence("Seq4", "CA-t"); + SequenceI[] seqs = new SequenceI[] + { seq1, seq2, seq3, seq4 }; + Hashtable[] result = new Hashtable[seq1.getLength()]; + + // ensure class loaded and initialized + AAFrequency.calculate(seqs, 0, seq1.getLength(), result, true); + int reps = 100000; + long start = System.currentTimeMillis(); + for (int i = 0; i < reps; i++) + { + AAFrequency.calculate(seqs, 0, seq1.getLength(), result, true); + } + System.out.println(System.currentTimeMillis() - start); + } +} diff --git a/test/jalview/util/QuickSortTest.java b/test/jalview/util/QuickSortTest.java index 6631871..03aa463 100644 --- a/test/jalview/util/QuickSortTest.java +++ b/test/jalview/util/QuickSortTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue; import java.util.Arrays; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; public class QuickSortTest @@ -76,4 +77,22 @@ public class QuickSortTest assertTrue(Arrays.equals(new Object[] { c3, c2, c1, c4 }, things)); } + + /** + * Test whether sort is stable i.e. equal values retain their mutual ordering. + */ + @Test + @Ignore + public void testSort_withDuplicates() + { + int[] values = new int[] + { 3, 4, 2, 4, 1 }; + Object [] things = new Object [] {"A", "X", "Y", "B", "Z"}; + QuickSort.sort(values, things); + assertTrue(Arrays.equals(new int[] + { 1, 2, 3, 4, 4 }, values)); + // this fails - do we care? + assertTrue(Arrays.equals(new Object[] + { "Z", "Y", "A", "X", "B" }, things)); + } } -- 1.7.10.2