X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSequenceGroup.java;h=6ded8bd79adac1a57f27893c8d819d0dd496ae19;hb=942de34caf73a7d61104783c8d278c32eaf6b65d;hp=6f6850bc1ecf657d461642b49159e4788ec02178;hpb=ba0bffb2a7cda833ff1dd2691f756294402b48cd;p=jalview.git diff --git a/src/jalview/datamodel/SequenceGroup.java b/src/jalview/datamodel/SequenceGroup.java index 6f6850b..6ded8bd 100755 --- a/src/jalview/datamodel/SequenceGroup.java +++ b/src/jalview/datamodel/SequenceGroup.java @@ -1,344 +1,645 @@ -/* - * 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 ClustalxColourScheme) - { - ( (ClustalxColourScheme) cs).resetClustalX(sequences,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; - if (ccs.cs instanceof ClustalxColourScheme) - { - ( (ClustalxColourScheme) ccs.cs).resetClustalX(sequences, getWidth()); - } - - } - } - - 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) 2007 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 java.util.*; + +import java.awt.*; + +import jalview.analysis.*; +import jalview.schemes.*; + +/** + * Collects a set contiguous ranges on a set of sequences + * @author $author$ + * @version $Revision$ + */ +public class SequenceGroup +{ + String groupName; + String description; + Conservation conserve; + Vector aaFrequency; + boolean displayBoxes = true; + boolean displayText = true; + boolean colourText = false; + private Vector sequences = new Vector(); + int width = -1; + + /** DOCUMENT ME!! */ + public ColourSchemeI cs; + int startRes = 0; + int endRes = 0; + public Color outlineColour = Color.black; + public Color idColour = null; + public int thresholdTextColour = 0; + public Color textColour = Color.black; + public Color textColour2 = Color.white; + + /** + * Creates a new SequenceGroup object. + */ + public SequenceGroup() + { + groupName = "JGroup:" + this.hashCode(); + } + + /** + * Creates a new SequenceGroup object. + * + * @param sequences + * @param groupName + * @param scheme + * @param displayBoxes + * @param displayText + * @param colourText + * @param start first column of group + * @param end last column of group + */ + 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 SequenceI[] getSelectionAsNewSequences(AlignmentI align) + { + int iSize = sequences.size(); + SequenceI[] seqs = new SequenceI[iSize]; + SequenceI[] inorder = getSequencesInOrder(align); + + for (int i = 0,ipos=0; i < inorder.length; i++) + { + SequenceI seq = inorder[i]; + + seqs[ipos] = seq.getSubSequence(startRes, endRes+1); + if (seqs[ipos]!=null) + { + seqs[ipos].setDescription(seq.getDescription()); + seqs[ipos].setDBRef(seq.getDBRef()); + seqs[ipos].setSequenceFeatures(seq.getSequenceFeatures()); + if (seq.getDatasetSequence() != null) + { + seqs[ipos].setDatasetSequence(seq.getDatasetSequence()); + } + + if (seq.getAnnotation() != null) + { + AlignmentAnnotation[] alann = align.getAlignmentAnnotation(); + // Only copy annotation that is either a score or referenced by the alignment's annotation vector + for (int a = 0; a < seq.getAnnotation().length; a++) + { + AlignmentAnnotation tocopy = seq.getAnnotation()[a]; + if (alann!=null) + { + boolean found=false; + for (int pos=0;pos 0) + { + eres += seq.getStart() - 1; + } + + return eres; + } + + public Vector getSequences(Hashtable hiddenReps) + { + if (hiddenReps == null) + { + return sequences; + } + else + { + Vector allSequences = new Vector(); + SequenceI seq, seq2; + for (int i = 0; i < sequences.size(); i++) + { + seq = (SequenceI) sequences.elementAt(i); + allSequences.addElement(seq); + if (hiddenReps.containsKey(seq)) + { + SequenceGroup hsg = (SequenceGroup) hiddenReps.get(seq); + for (int h = 0; h < hsg.getSize(); h++) + { + seq2 = hsg.getSequenceAt(h); + if (seq2 != seq + && !allSequences.contains(seq2)) + { + allSequences.addElement(seq2); + } + } + } + } + + return allSequences; + } + } + + public SequenceI[] getSequencesAsArray(Hashtable hiddenReps) + { + Vector tmp = getSequences(hiddenReps); + if (tmp == null) + { + return null; + } + SequenceI[] result = new SequenceI[tmp.size()]; + for (int i = 0; i < result.length; i++) + { + result[i] = (SequenceI) tmp.elementAt(i); + } + + return result; + } + + /** + * DOCUMENT ME! + * + * @param col DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + 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; + } + + /** + * 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; + } + + public String getDescription() + { + return description; + } + + /** + * DOCUMENT ME! + * + * @param name DOCUMENT ME! + */ + public void setName(String name) + { + groupName = name; + } + + public void setDescription(String desc) + { + description = desc; + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Conservation getConservation() + { + return conserve; + } + + /** + * DOCUMENT ME! + * + * @param c DOCUMENT ME! + */ + public void setConservation(Conservation c) + { + conserve = c; + } + + /** + * Add s to this sequence group + * + * @param s alignment sequence to be added + * @param recalc true means Group's conservation should be recalculated + */ + public void addSequence(SequenceI s, boolean recalc) + { + if (s != null && !sequences.contains(s)) + { + sequences.addElement(s); + } + + if (recalc) + { + recalcConservation(); + } + } + + /** + * calculate residue conservation for group + */ + public void recalcConservation() + { + if (cs == null) + { + return; + } + + try + { + cs.setConsensus(AAFrequency.calculate(sequences, startRes, endRes + 1)); + + if (cs instanceof ClustalxColourScheme) + { + ( (ClustalxColourScheme) cs).resetClustalX(sequences, getWidth()); + } + + if (cs.conservationApplied()) + { + Conservation c = new Conservation(groupName, + ResidueProperties.propHash, 3, + sequences, + startRes, endRes + 1); + 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() + { + return sequences.size(); + } + + /** + * 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 sSize = sequences.size(); + int alHeight = al.getHeight(); + + SequenceI[] seqs = new SequenceI[sSize]; + + int index = 0; + for (int i = 0; i < alHeight && index < sSize; i++) + { + if (sequences.contains(al.getSequenceAt(i))) + { + seqs[index++] = al.getSequenceAt(i); + } + } + + return seqs; + } + + /** + * @return the idColour + */ + public Color getIdColour() + { + return idColour; + } + + /** + * @param idColour the idColour to set + */ + public void setIdColour(Color idColour) + { + this.idColour = idColour; + } +}