From d69ea8f1997771890b44e4b332a7ca84fe6f0893 Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Fri, 6 Oct 2006 13:52:06 +0000 Subject: [PATCH] AAFrequency optimized --- src/jalview/analysis/AAFrequency.java | 181 +++++++++++++++--------------- src/jalview/appletgui/AlignFrame.java | 4 +- src/jalview/appletgui/AlignViewport.java | 43 +++---- src/jalview/appletgui/OverviewPanel.java | 2 +- src/jalview/datamodel/Alignment.java | 9 -- src/jalview/datamodel/AlignmentI.java | 6 - src/jalview/gui/AlignFrame.java | 12 +- src/jalview/gui/AlignViewport.java | 51 ++++----- src/jalview/gui/Jalview2XML.java | 2 +- src/jalview/gui/Jalview2XML_V1.java | 2 +- 10 files changed, 139 insertions(+), 173 deletions(-) diff --git a/src/jalview/analysis/AAFrequency.java b/src/jalview/analysis/AAFrequency.java index 6fa9248..15618de 100755 --- a/src/jalview/analysis/AAFrequency.java +++ b/src/jalview/analysis/AAFrequency.java @@ -35,105 +35,108 @@ import java.util.*; */ public class AAFrequency { - /** Takes in a vector of sequences and column start and column end - * and returns a vector of size (end-start+1). Each element of the + /** Takes in a !!ARRAY!! of sequences and column start and column end + * and fills given Vector of size (end-start+1). Each element of the * vector contains a hashtable with the keys being residues and * the values being the count of each residue in that column. * This class is used extensively in calculating alignment colourschemes * that depend on the amount of conservation in each alignment column. */ - public static final Vector calculate(Vector sequences, int start, int end) + + public static final Hashtable [] calculate(Vector sequences, int start, int end) + { + SequenceI [] seqs = new SequenceI[sequences.size()]; + int width = 0; + for(int i=0; iwidth) + width = seqs[i].getLength(); + } + + Hashtable [] reply = new Hashtable[width]; + + calculate(seqs, start, end, reply); + + return reply; + } + +public static final void calculate(SequenceI[] sequences, + int start, int end, + Hashtable [] result) +{ + Hashtable residueHash; + int maxCount, nongap, i, j, v, jSize = sequences.length; + String maxResidue; + char c; + float percentage; + + int[] values = new int[132]; + + for (i = start; i < end; i++) + { + residueHash = new Hashtable(); + maxCount = 0; + maxResidue = "-"; + nongap = 0; + values = new int[132]; + + for (j = 0; j < jSize; j++) { - Vector result = new Vector(); - Hashtable residueHash; - int count, maxCount, nongap, i, j, jSize = sequences.size(); - String maxResidue, sequence, res; - float percentage; + if (sequences[j].getLength() > i) + { + c = sequences[j].getCharAt(i); - for (i = start; i <= end; i++) + if ('a' <= c && c <= 'z') + { + c -= ('a' - 'A'); + } + + if (jalview.util.Comparison.isGap(c)) + { + c = '-'; // we always use this for gaps in the property vectors + } + else { - residueHash = new Hashtable(); - maxCount = 0; - maxResidue = "-"; - nongap = 0; - - for (j = 0; j < jSize; j++) - { - if (sequences.elementAt(j) instanceof Sequence) - { - sequence = ((Sequence) sequences.elementAt(j)).getSequence(); - - if (sequence.length() > i) - { - res = String.valueOf(Character.toUpperCase(sequence.charAt(i))); - - if (jalview.util.Comparison.isGap(res.charAt(0))) - { - res = "-"; // we always use this for gaps in the property vectors - } - else - { nongap++; } - - if (residueHash.containsKey(res)) - { - count = ((Integer) residueHash.get(res)).intValue(); - count++; - - if (!jalview.util.Comparison.isGap(res.charAt(0)) && - (count >= maxCount)) - { - if (count > maxCount) - { - maxResidue = res; - } - else if (maxResidue.indexOf(res) == -1) - { - maxResidue += res; - } - - maxCount = count; - } - - residueHash.put(res, new Integer(count)); - } - else - { - residueHash.put(res, new Integer(1)); - } - } - else - { - if (residueHash.containsKey("-")) - { - count = ((Integer) residueHash.get("-")).intValue(); - count++; - residueHash.put("-", new Integer(count)); - } - else - { - residueHash.put("-", new Integer(1)); - } - } - } - } - - residueHash.put("maxCount", new Integer(maxCount)); - residueHash.put("maxResidue", maxResidue); - - - //Size is redundant at present if we calculate percentage here - //residueHash.put("size", new Integer(jSize)); - //residueHash.put("nogaps", new Integer(nongap)); - - percentage = ((float)maxCount*100) / (float)jSize; - residueHash.put("pid_gaps", new Float(percentage) ); - - percentage = ((float)maxCount*100) / (float)nongap; - residueHash.put("pid_nogaps", new Float(percentage) ); - result.addElement(residueHash); + nongap++; } + values[c]++; + } + else + { + values['-']++; + } + } - return result; + for (v = 'A'; v < 'Z'; v++) + { + if (values[v] == 0 || values[v] < maxCount) + continue; + + if (values[v] > maxCount) + { + maxResidue = String.valueOf( (char) v); + } + else if (values[v] == maxCount) + { + maxResidue += String.valueOf( (char) v); + } + maxCount = values[v]; } + + + residueHash.put("maxCount", new Integer(maxCount)); + residueHash.put("maxResidue", maxResidue); + + percentage = ( (float) maxCount * 100) / (float) jSize; + residueHash.put("pid_gaps", new Float(percentage)); + + percentage = ( (float) maxCount * 100) / (float) nongap; + residueHash.put("pid_nogaps", new Float(percentage)); + result[i] = residueHash; + } +} } + + diff --git a/src/jalview/appletgui/AlignFrame.java b/src/jalview/appletgui/AlignFrame.java index 9152939..8e54ba9 100755 --- a/src/jalview/appletgui/AlignFrame.java +++ b/src/jalview/appletgui/AlignFrame.java @@ -1247,7 +1247,7 @@ public void itemStateChanged(ItemEvent evt) viewport.alignment.getWidth()); } - cs.setConsensus(viewport.vconsensus); + cs.setConsensus(viewport.hconsensus); if (cs.conservationApplied()) { Alignment al = (Alignment) viewport.alignment; @@ -1487,7 +1487,7 @@ public void itemStateChanged(ItemEvent evt) cs.setConservation(null); } - cs.setConsensus(viewport.vconsensus); + cs.setConsensus(viewport.hconsensus); } viewport.setGlobalColourScheme(cs); diff --git a/src/jalview/appletgui/AlignViewport.java b/src/jalview/appletgui/AlignViewport.java index fc46386..998a4a4 100755 --- a/src/jalview/appletgui/AlignViewport.java +++ b/src/jalview/appletgui/AlignViewport.java @@ -87,7 +87,7 @@ public class AlignViewport boolean showHiddenMarkers = true; - public Vector vconsensus; + public Hashtable [] hconsensus; AlignmentAnnotation consensus; AlignmentAnnotation conservation; AlignmentAnnotation quality; @@ -179,7 +179,7 @@ public class AlignViewport globalColourScheme = ColourSchemeProperty.getColour(alignment, colour); if (globalColourScheme != null) { - globalColourScheme.setConsensus(vconsensus); + globalColourScheme.setConsensus(hconsensus); } } @@ -305,37 +305,28 @@ public class AlignViewport public void updateConsensus() { - Annotation[] annotations = new Annotation[alignment.getWidth()]; - // this routine prevents vconsensus becoming a new object each time // consenus is calculated. Important for speed of Blosum62 // and PID colouring of alignment - if (vconsensus == null) - { - vconsensus = alignment.getAAFrequency(); - } - else - { - Vector temp = alignment.getAAFrequency(); - vconsensus.removeAllElements(); - Enumeration e = temp.elements(); - while (e.hasMoreElements()) - { - vconsensus.addElement(e.nextElement()); - } - } - Hashtable hash = null; - for (int i = 0; i < alignment.getWidth(); i++) + int aWidth = alignment.getWidth(); + + Annotation[] annotations = new Annotation[aWidth]; + + hconsensus = new Hashtable[aWidth]; + AAFrequency.calculate(alignment.getSequencesArray(), + 0, aWidth, + hconsensus); + + for (int i = 0; i < aWidth; i++) { - hash = (Hashtable) vconsensus.elementAt(i); float value = 0; if(ignoreGapsInConsensusCalculation) - value = ((Float)hash.get("pid_nogaps")).floatValue(); + value = ((Float)hconsensus[i].get("pid_nogaps")).floatValue(); else - value = ((Float)hash.get("pid_gaps")).floatValue(); + value = ((Float)hconsensus[i].get("pid_gaps")).floatValue(); - String maxRes = hash.get("maxResidue").toString(); - String mouseOver = hash.get("maxResidue") + " "; + String maxRes = hconsensus[i].get("maxResidue").toString(); + String mouseOver = hconsensus[i].get("maxResidue") + " "; if (maxRes.length() > 1) { mouseOver = "[" + maxRes + "] "; @@ -363,7 +354,7 @@ public class AlignViewport } if(globalColourScheme!=null) - globalColourScheme.setConsensus(vconsensus); + globalColourScheme.setConsensus(hconsensus); } /** diff --git a/src/jalview/appletgui/OverviewPanel.java b/src/jalview/appletgui/OverviewPanel.java index cbbfcff..867ce56 100755 --- a/src/jalview/appletgui/OverviewPanel.java +++ b/src/jalview/appletgui/OverviewPanel.java @@ -68,7 +68,7 @@ public class OverviewPanel float initialScale = (float) av.alignment.getWidth() / (float) av.alignment.getHeight(); - if(av.vconsensus==null) + if(av.hconsensus==null) graphHeight = 0; if (av.alignment.getWidth() > av.alignment.getHeight()) diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index 90a8b31..a770264 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -565,15 +565,6 @@ public class Alignment implements AlignmentI return gapCharacter; } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public Vector getAAFrequency() - { - return AAFrequency.calculate(sequences, 0, getWidth()); - } /** * DOCUMENT ME! diff --git a/src/jalview/datamodel/AlignmentI.java b/src/jalview/datamodel/AlignmentI.java index bcfcc81..aca108d 100755 --- a/src/jalview/datamodel/AlignmentI.java +++ b/src/jalview/datamodel/AlignmentI.java @@ -242,12 +242,6 @@ public interface AlignmentI */ public char getGapCharacter(); - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public Vector getAAFrequency(); /** * Returns true if alignment is nucleotide sequence diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 79f1e8b..1bcd39e 100755 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -115,7 +115,7 @@ public class AlignFrame { this.setDropTarget(new java.awt.dnd.DropTarget(this, this)); - if (viewport.vconsensus == null) + if (viewport.hconsensus == null) { //Out of memory calculating consensus. BLOSUM62Colour.setEnabled(false); @@ -832,7 +832,7 @@ public class AlignFrame */ void updateEditMenuBar() { - if (historyList.size() > 0) + /* if (historyList.size() > 0) { undoMenuItem.setEnabled(true); @@ -856,7 +856,7 @@ public class AlignFrame { redoMenuItem.setEnabled(false); redoMenuItem.setText("Redo"); - } + }*/ } /** @@ -1614,7 +1614,7 @@ public class AlignFrame if (av.padGaps) av.getAlignment().padGaps(); - if (av.vconsensus != null && av.autoCalculateConsensus) + if (av.hconsensus != null && av.autoCalculateConsensus) { av.updateConsensus(); av.updateConservation(); @@ -1643,7 +1643,7 @@ public class AlignFrame viewport.alignment.getWidth()); } - cs.setConsensus(viewport.vconsensus); + cs.setConsensus(viewport.hconsensus); if (cs.conservationApplied()) { Alignment al = (Alignment) viewport.alignment; @@ -2089,7 +2089,7 @@ public class AlignFrame cs.setConservation(null); } - cs.setConsensus(viewport.vconsensus); + cs.setConsensus(viewport.hconsensus); } viewport.setGlobalColourScheme(cs); diff --git a/src/jalview/gui/AlignViewport.java b/src/jalview/gui/AlignViewport.java index 6d0f766..07a7453 100755 --- a/src/jalview/gui/AlignViewport.java +++ b/src/jalview/gui/AlignViewport.java @@ -84,7 +84,7 @@ public class AlignViewport /** DOCUMENT ME!! */ - public Vector vconsensus; + public Hashtable [] hconsensus; AlignmentAnnotation consensus; AlignmentAnnotation conservation; AlignmentAnnotation quality; @@ -187,7 +187,7 @@ public class AlignViewport // We must set conservation and consensus before setting colour, // as Blosum and Clustal require this to be done - if(vconsensus==null && !isDataset) + if(hconsensus==null && !isDataset) { updateConservation(); updateConsensus(); @@ -206,7 +206,7 @@ public class AlignViewport if (globalColourScheme != null) { - globalColourScheme.setConsensus(vconsensus); + globalColourScheme.setConsensus(hconsensus); } } } @@ -236,6 +236,8 @@ public class AlignViewport if(alignment.isNucleotide()) return; + // System.out.println("UPDATING CONSERVATION"); + try{ Conservation cons = new jalview.analysis.Conservation("All", jalview.schemes.ResidueProperties.propHash, 3, @@ -360,48 +362,33 @@ public class AlignViewport } } + /** * DOCUMENT ME! */ public void updateConsensus() { try{ - Annotation[] annotations = new Annotation[alignment.getWidth()]; - - // this routine prevents vconsensus becoming a new object each time - // consenus is calculated. Important for speed of Blosum62 - // and PID colouring of alignment - if (vconsensus == null) - { - vconsensus = alignment.getAAFrequency(); - } - else - { - Vector temp = alignment.getAAFrequency(); - vconsensus.clear(); - - Enumeration e = temp.elements(); + int aWidth = alignment.getWidth(); - while (e.hasMoreElements()) - { - vconsensus.add(e.nextElement()); - } - } + Annotation[] annotations = new Annotation[aWidth]; - Hashtable hash = null; + hconsensus = new Hashtable[aWidth]; + AAFrequency.calculate(alignment.getSequencesArray(), + 0, + alignment.getWidth(), + hconsensus); - for (int i = 0; i < alignment.getWidth(); i++) + for (int i = 0; i < aWidth; i++) { - hash = (Hashtable) vconsensus.elementAt(i); - float value = 0; if (ignoreGapsInConsensusCalculation) - value = ( (Float) hash.get("pid_nogaps")).floatValue(); + value = ( (Float) hconsensus[i].get("pid_nogaps")).floatValue(); else - value = ( (Float) hash.get("pid_gaps")).floatValue(); + value = ( (Float) hconsensus[i].get("pid_gaps")).floatValue(); - String maxRes = hash.get("maxResidue").toString(); - String mouseOver = hash.get("maxResidue") + " "; + String maxRes = hconsensus[i].get("maxResidue").toString(); + String mouseOver = hconsensus[i].get("maxResidue") + " "; if (maxRes.length() > 1) { @@ -429,7 +416,7 @@ public class AlignViewport } if (globalColourScheme != null) - globalColourScheme.setConsensus(vconsensus); + globalColourScheme.setConsensus(hconsensus); }catch(OutOfMemoryError error) { diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java index e118309..a38c530 100755 --- a/src/jalview/gui/Jalview2XML.java +++ b/src/jalview/gui/Jalview2XML.java @@ -1481,7 +1481,7 @@ public class Jalview2XML if(cs!=null) { cs.setThreshold(view.getPidThreshold(), true); - cs.setConsensus(af.viewport.vconsensus); + cs.setConsensus(af.viewport.hconsensus); } } diff --git a/src/jalview/gui/Jalview2XML_V1.java b/src/jalview/gui/Jalview2XML_V1.java index 6a4fba8..428789f 100755 --- a/src/jalview/gui/Jalview2XML_V1.java +++ b/src/jalview/gui/Jalview2XML_V1.java @@ -388,7 +388,7 @@ public class Jalview2XML_V1 if(cs!=null) { cs.setThreshold(view.getPidThreshold(), true); - cs.setConsensus(af.viewport.vconsensus); + cs.setConsensus(af.viewport.hconsensus); } } -- 1.7.10.2