X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSequenceGroup.java;h=7bb8ce0e4e97f0f374db7a7287466d5534caa533;hb=e894eb87d40be6e8452d19032ddffe80dad5f804;hp=b4c21fc10b66ba07238e2170e4b26c7b41e0d8e5;hpb=927ff64eabd7bb06ed31f3a94a7eee6b927d68a1;p=jalview.git diff --git a/src/jalview/datamodel/SequenceGroup.java b/src/jalview/datamodel/SequenceGroup.java index b4c21fc..7bb8ce0 100755 --- a/src/jalview/datamodel/SequenceGroup.java +++ b/src/jalview/datamodel/SequenceGroup.java @@ -1,333 +1,604 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * This program 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 2 - * of the License, or (at your option) any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ -package jalview.datamodel; - -import jalview.analysis.*; - -import jalview.datamodel.*; - -import jalview.schemes.*; - -import java.awt.*; - -import java.util.Vector; - -public class SequenceGroup -{ - String groupName; - Conservation conserve; - Vector aaFrequency; - boolean displayBoxes; - boolean displayText; - boolean colourText; - public Vector sequences = new Vector(); - int width = -1; - public ColourSchemeI cs; - int startRes = 0; - int endRes = 0; - Color outlineColour = Color.black; - - public SequenceGroup() - { - groupName = "Group"; - this.displayBoxes = true; - this.displayText = true; - this.colourText = false; - cs = null; - } - - public SequenceGroup(Vector sequences, String groupName, ColourSchemeI scheme, - boolean displayBoxes, boolean displayText, - boolean colourText, - int start, int end) - { - this.sequences = sequences; - this.groupName = groupName; - this.displayBoxes = displayBoxes; - this.displayText = displayText; - this.colourText = colourText; - this.cs = scheme; - startRes = start; - endRes = end; - recalcConservation(); - } - - public SequenceGroup(String groupName, ColourSchemeI scheme, - boolean displayBoxes, boolean displayText, - boolean colourText, - int start, int end) - { - this.groupName = groupName; - this.displayBoxes = displayBoxes; - this.displayText = displayText; - this.colourText = colourText; - this.cs = scheme; - startRes = start; - endRes = end; - } - - public boolean adjustForRemoveLeft(int col) - { - // return value is true if the group still exists - if (startRes >= col) - { - startRes = startRes - col; - } - - if (endRes >= col) - { - endRes = endRes - col; - - if (startRes > endRes) - { - startRes = 0; - } - } - else - { - // must delete this group!! - return false; - } - - return true; - } - - public boolean adjustForRemoveRight(int col) - { - if (startRes > col) - { - // delete this group - return false; - } - - if (endRes >= col) - { - endRes = col; - } - - return true; - } - - public String getName() - { - return groupName; - } - - public void setName(String name) - { - groupName = name; - } - - public Conservation getConservation() - { - return conserve; - } - - public void setConservation(Conservation c) - { - conserve = c; - } - - public void addSequence(SequenceI s, boolean recalc) - { - if (!sequences.contains(s)) - sequences.addElement(s); - - if(recalc) - recalcConservation(); - } - - - public void recalcConservation() - { - if (cs != null) - { - cs.setConsensus(AAFrequency.calculate(sequences, 0, getWidth())); - } - - if ( cs instanceof ConservationColourScheme) - { - Conservation c = new Conservation(groupName, - ResidueProperties.propHash, 3, sequences, - 0, getWidth()); - c.calculate(); - c.verdict(false, 25); - - ConservationColourScheme ccs = (ConservationColourScheme) cs; - ccs.conserve = c; - } - } - - public void addOrRemove(SequenceI s, boolean recalc) - { - if (sequences.contains(s)) - { - deleteSequence(s, recalc); - } - else - { - addSequence(s, recalc); - } - } - - public void deleteSequence(SequenceI s, boolean recalc) - { - sequences.removeElement(s); - if(recalc) - recalcConservation(); - } - - public int getStartRes() - { - return startRes; - } - - public int getEndRes() - { - return endRes; - } - - public void setStartRes(int i) - { - startRes = i; - } - - public void setEndRes(int i) - { - endRes = i; - } - - public int getSize() - { - return sequences.size(); - } - - public SequenceI getSequenceAt(int i) - { - return (SequenceI) sequences.elementAt(i); - } - - public void setColourText(boolean state) - { - colourText = state; - } - - public boolean getColourText() - { - return colourText; - } - - public void setDisplayText(boolean state) - { - displayText = state; - } - - public boolean getDisplayText() - { - return displayText; - } - - public void setDisplayBoxes(boolean state) - { - displayBoxes = state; - } - - public boolean getDisplayBoxes() - { - return displayBoxes; - } - - public int getWidth() - { - // MC This needs to get reset when characters are inserted and deleted - if (sequences.size() > 0) - { - width = ( (SequenceI) sequences.elementAt(0)).getLength(); - } - - for (int i = 1; i < sequences.size(); i++) - { - SequenceI seq = (SequenceI) sequences.elementAt(i); - - if (seq.getLength() > width) - { - width = seq.getLength(); - } - } - - return width; - } - - public void setOutlineColour(Color c) - { - outlineColour = c; - } - - public Color getOutlineColour() - { - return outlineColour; - } - - /** - * - * returns the sequences in the group ordered by the ordering given by al - * - * @param al Alignment - * @return SequenceI[] - */ - public SequenceI[] getSequencesInOrder(Alignment al) - { - int sz; - java.util.Hashtable orderedSeqs = new java.util.Hashtable(); - SequenceI[] seqs = new SequenceI[sz = sequences.size()]; - - for (int i = 0; i < sz; i++) - { - SequenceI seq = (SequenceI) sequences.elementAt(i); - int index = al.findIndex(seq); - orderedSeqs.put(index + "", seq); - } - - int index = 0; - - for (int i = 0; i < sz; i++) - { - SequenceI seq = null; - - while (seq == null) - { - if (orderedSeqs.containsKey(index + "")) - { - seq = (SequenceI) orderedSeqs.get(index + ""); - index++; - - break; - } - else - { - index++; - } - } - - seqs[index] = seq; - } - - return seqs; - } -} +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ +package jalview.datamodel; + +import jalview.analysis.*; + +import jalview.schemes.*; + +import java.awt.*; + +import java.util.*; + + +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class SequenceGroup +{ + String groupName; + Conservation conserve; + Vector aaFrequency; + boolean displayBoxes; + boolean displayText; + boolean colourText; + private Vector sequences = new Vector(); + int width = -1; + + /** DOCUMENT ME!! */ + public ColourSchemeI cs; + int startRes = 0; + int endRes = 0; + Color outlineColour = Color.black; + + /** + * Creates a new SequenceGroup object. + */ + public SequenceGroup() + { + groupName = "Group"; + this.displayBoxes = true; + this.displayText = true; + this.colourText = false; + cs = null; + } + + /** + * Creates a new SequenceGroup object. + * + * @param sequences DOCUMENT ME! + * @param groupName DOCUMENT ME! + * @param scheme DOCUMENT ME! + * @param displayBoxes DOCUMENT ME! + * @param displayText DOCUMENT ME! + * @param colourText DOCUMENT ME! + * @param start DOCUMENT ME! + * @param end DOCUMENT ME! + */ + public SequenceGroup(Vector sequences, String groupName, + ColourSchemeI scheme, boolean displayBoxes, boolean displayText, + boolean colourText, int start, int end) + { + this.sequences = sequences; + this.groupName = groupName; + this.displayBoxes = displayBoxes; + this.displayText = displayText; + this.colourText = colourText; + this.cs = scheme; + startRes = start; + endRes = end; + recalcConservation(); + } + + /** + * Creates a new SequenceGroup object. + * + * @param groupName DOCUMENT ME! + * @param scheme DOCUMENT ME! + * @param displayBoxes DOCUMENT ME! + * @param displayText DOCUMENT ME! + * @param colourText DOCUMENT ME! + * @param start DOCUMENT ME! + * @param end DOCUMENT ME! + */ + public SequenceGroup(String groupName, ColourSchemeI scheme, + boolean displayBoxes, boolean displayText, boolean colourText, + int start, int end) + { + this.groupName = groupName; + this.displayBoxes = displayBoxes; + this.displayText = displayText; + this.colourText = colourText; + this.cs = scheme; + startRes = start; + endRes = end; + } + + public SequenceI [] getSelectionAsNewSequences(AlignmentI align) + { + int iSize = sequences.size(); + SequenceI [] seqs = new SequenceI[iSize]; + SequenceI [] inorder = getSequencesInOrder(align); + + char ch; + int sres, eres; + + for (int i = 0; i < iSize; i++) + { + SequenceI seq = inorder[i]; + + //FIND START RES + //Returns residue following index if gap + sres = seq.findPosition(startRes); + + //FIND END RES + //Need to find the residue preceeding index if gap + eres = 0; + + for (int j = 0; j < endRes + 1 && j < seq.getLength(); j++) + { + ch = seq.getCharAt(j); + if (!jalview.util.Comparison.isGap( (ch))) + { + eres++; + } + } + + if (eres > 0) + { + eres += seq.getStart() - 1; + } + + seqs[i] = new Sequence(seq.getName(), + seq.getSequence(startRes, endRes + 1), + sres, + eres); + seqs[i].setDescription(seq.getDescription()); + seqs[i].setDBRef(seq.getDBRef()); + seqs[i].setSequenceFeatures(seq.getSequenceFeatures()); + if (seq.getDatasetSequence() != null) + seqs[i].setDatasetSequence(seq.getDatasetSequence()); + + if(seq.getAnnotation()!=null) + { + for(int a=0; a= col) + { + startRes = startRes - col; + } + + if (endRes >= col) + { + endRes = endRes - col; + + if (startRes > endRes) + { + startRes = 0; + } + } + else + { + // must delete this group!! + return false; + } + + return true; + } + + /** + * DOCUMENT ME! + * + * @param col DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public boolean adjustForRemoveRight(int col) + { + if (startRes > col) + { + // delete this group + return false; + } + + if (endRes >= col) + { + endRes = col; + } + + return true; + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String getName() + { + return groupName; + } + + /** + * DOCUMENT ME! + * + * @param name DOCUMENT ME! + */ + public void setName(String name) + { + groupName = name; + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Conservation getConservation() + { + return conserve; + } + + /** + * DOCUMENT ME! + * + * @param c DOCUMENT ME! + */ + public void setConservation(Conservation c) + { + conserve = c; + } + + /** + * DOCUMENT ME! + * + * @param s DOCUMENT ME! + * @param recalc DOCUMENT ME! + */ + public void addSequence(SequenceI s, boolean recalc) + { + if (!sequences.contains(s)) + { + sequences.addElement(s); + } + + if (recalc) + { + recalcConservation(); + } + } + + /** + * DOCUMENT ME! + */ + public void recalcConservation() + { + if(cs == null) + return; + + try + { + cs.setConsensus(AAFrequency.calculate(sequences, 0, getWidth())); + + if (cs instanceof ClustalxColourScheme) + { + ( (ClustalxColourScheme) cs).resetClustalX(sequences, getWidth()); + } + + if (cs.conservationApplied()) + { + Conservation c = new Conservation(groupName, + ResidueProperties.propHash, 3, sequences, + 0, getWidth()); + c.calculate(); + c.verdict(false, 25); + + cs.setConservation(c); + + if (cs instanceof ClustalxColourScheme) + { + ( (ClustalxColourScheme) cs).resetClustalX(sequences, + getWidth()); + } + } + } + catch (java.lang.OutOfMemoryError err) + { + System.out.println("Out of memory loading groups: " + err); + } + + } + + /** + * DOCUMENT ME! + * + * @param s DOCUMENT ME! + * @param recalc DOCUMENT ME! + */ + public void addOrRemove(SequenceI s, boolean recalc) + { + if (sequences.contains(s)) + { + deleteSequence(s, recalc); + } + else + { + addSequence(s, recalc); + } + } + + /** + * DOCUMENT ME! + * + * @param s DOCUMENT ME! + * @param recalc DOCUMENT ME! + */ + public void deleteSequence(SequenceI s, boolean recalc) + { + sequences.removeElement(s); + + if (recalc) + { + recalcConservation(); + } + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public int getStartRes() + { + return startRes; + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public int getEndRes() + { + return endRes; + } + + /** + * DOCUMENT ME! + * + * @param i DOCUMENT ME! + */ + public void setStartRes(int i) + { + startRes = i; + } + + /** + * DOCUMENT ME! + * + * @param i DOCUMENT ME! + */ + public void setEndRes(int i) + { + endRes = i; + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public int getSize(boolean includeHidden) + { + if(!includeHidden) + return sequences.size(); + else + { + int total = sequences.size(); + SequenceI seq; + for (int i = 0; i < sequences.size(); i++) + { + seq = (SequenceI) sequences.elementAt(i); + if (seq.getHiddenSequences() != null) + { + total += seq.getHiddenSequences().getSize(false); + } + } + return total; + } + } + + /** + * DOCUMENT ME! + * + * @param i DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public SequenceI getSequenceAt(int i) + { + return (SequenceI) sequences.elementAt(i); + } + + /** + * DOCUMENT ME! + * + * @param state DOCUMENT ME! + */ + public void setColourText(boolean state) + { + colourText = state; + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public boolean getColourText() + { + return colourText; + } + + /** + * DOCUMENT ME! + * + * @param state DOCUMENT ME! + */ + public void setDisplayText(boolean state) + { + displayText = state; + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public boolean getDisplayText() + { + return displayText; + } + + /** + * DOCUMENT ME! + * + * @param state DOCUMENT ME! + */ + public void setDisplayBoxes(boolean state) + { + displayBoxes = state; + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public boolean getDisplayBoxes() + { + return displayBoxes; + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public int getWidth() + { + // MC This needs to get reset when characters are inserted and deleted + if (sequences.size() > 0) + { + width = ((SequenceI) sequences.elementAt(0)).getLength(); + } + + for (int i = 1; i < sequences.size(); i++) + { + SequenceI seq = (SequenceI) sequences.elementAt(i); + + if (seq.getLength() > width) + { + width = seq.getLength(); + } + } + + return width; + } + + /** + * DOCUMENT ME! + * + * @param c DOCUMENT ME! + */ + public void setOutlineColour(Color c) + { + outlineColour = c; + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Color getOutlineColour() + { + return outlineColour; + } + + /** + * + * returns the sequences in the group ordered by the ordering given by al + * + * @param al Alignment + * @return SequenceI[] + */ + public SequenceI[] getSequencesInOrder(AlignmentI al) + { + int sz = sequences.size(); + java.util.Hashtable orderedSeqs = new java.util.Hashtable(); + SequenceI[] seqs = new SequenceI[sz]; + + for (int i = 0; i < sz; i++) + { + SequenceI seq = (SequenceI) sequences.elementAt(i); + int index = al.findIndex(seq); + orderedSeqs.put(index + "", seq); + } + + int index = 0; + + for (int i = 0; i < al.getHeight(); i++) + { + if (orderedSeqs.containsKey(i + "")) + { + seqs[index++] = (SequenceI) orderedSeqs.get(i + ""); + } + } + + return seqs; + } +}