X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSequenceGroup.java;h=9236712b0b51d9d5c721fb269e68260bbd8093a6;hb=336e0c59bfd53d054da0bb332f858834f57e7bec;hp=679997e3c1a4847bc086eaeb2d26445bf6da757e;hpb=70d8cbb3e0f3e4986c09d9e5e42a7997d77cf9d5;p=jalview.git diff --git a/src/jalview/datamodel/SequenceGroup.java b/src/jalview/datamodel/SequenceGroup.java index 679997e..9236712 100755 --- a/src/jalview/datamodel/SequenceGroup.java +++ b/src/jalview/datamodel/SequenceGroup.java @@ -1,599 +1,990 @@ -/* - * 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); - - for (int i = 0; i < iSize; i++) - { - SequenceI seq = inorder[i]; - - seqs[i] = new Sequence(seq.getName(), - seq.getSequence(startRes, endRes + 1), - seq.findPosition(startRes), - findEndRes(seq)); - - 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 0) - { - eres += seq.getStart() - 1; - } - - return eres; - } - - public Vector getSequences(boolean includeHidden) - { - if(!includeHidden) - return sequences; - else - { - Vector allSequences = new Vector(); - SequenceI seq; - for (int i = 0; i < sequences.size(); i++) - { - seq = (SequenceI) sequences.elementAt(i); - allSequences.addElement(seq); - if (seq.getHiddenSequences() != null) - { - for (int h = 0; h < seq.getHiddenSequences().getSize(false); h++) - { - allSequences.addElement( - seq.getHiddenSequences().getSequenceAt(h) - ); - } - } - } - - return allSequences; - } - } - - /** - * 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; - } - - /** - * 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 sSize = sequences.size(); - int alHeight = al.getHeight(); - - SequenceI[] seqs = new SequenceI[sSize]; - - int index = 0; - for (int i = 0; i < alHeight && index 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 && consensus == null && conservation == null) + { + return; + } + + try + { + Hashtable cnsns[] = AAFrequency.calculate(sequences, startRes, endRes + 1); + if (consensus != null) + { + _updateConsensusRow(cnsns); + } + if (cs!=null) + { + cs.setConsensus(cnsns); + + if (cs instanceof ClustalxColourScheme) + { + ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth()); + } + } + + if ((conservation!=null) || (cs!=null && cs.conservationApplied())) + { + Conservation c = new Conservation(groupName, + ResidueProperties.propHash, 3, sequences, startRes, + endRes + 1); + c.calculate(); + c.verdict(false, 25); + if (conservation!=null) + { + _updateConservationRow(c); + } + if (cs!=null) + { + cs.setConservation(c); + + if (cs instanceof ClustalxColourScheme) + { + ((ClustalxColourScheme) cs).resetClustalX(sequences, getWidth()); + } + } + } + } catch (java.lang.OutOfMemoryError err) + { + // TODO: catch OOM + System.out.println("Out of memory loading groups: " + err); + } + + } + + private void _updateConservationRow(Conservation c) + { + if (conservation==null) + { + getConservation(); + } + // preserve width if already set + int aWidth = (conservation.annotations!=null) ? (endRes 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. + * this used to return an array with null entries regardless, new behaviour is below. + * TODO: verify that this does not affect use in applet or application + * @param al + * Alignment + * @return SequenceI[] intersection of sequences in group with al, ordered by al, or null if group does not intersect with al + */ + 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); + } + } + if (index==0) + { + return null; + } + if (index=0) + { + seqs[index] = dummy[index]; + dummy[index] = null; + } + } + return seqs; + } + + /** + * @return the idColour + */ + public Color getIdColour() + { + return idColour; + } + + /** + * @param idColour + * the idColour to set + */ + public void setIdColour(Color idColour) + { + this.idColour = idColour; + } + + /** + * @return the representative sequence for this group + */ + public SequenceI getSeqrep() + { + return seqrep; + } + + /** + * set the representative sequence for this group. + * Note - this affects the interpretation of the Hidereps attribute. + * @param seqrep the seqrep to set (null means no sequence representative) + */ + public void setSeqrep(SequenceI seqrep) + { + this.seqrep = seqrep; + } + /** + * + * @return true if group has a sequence representative + */ + public boolean hasSeqrep() + { + return seqrep != null; + } + /** + * visibility of rows or represented rows covered by group + */ + private boolean hidereps=false; + /** + * set visibility of sequences covered by (if no sequence representative is defined) + * or represented by this group. + * @param visibility + */ + public void setHidereps(boolean visibility) + { + hidereps = visibility; + } + /** + * + * @return true if sequences represented (or covered) by this group should be hidden + */ + public boolean isHidereps() + { + return hidereps; + } + /** + * visibility of columns intersecting this group + */ + private boolean hidecols=false; + /** + * set intended visibility of columns covered by this group + * @param visibility + */ + public void setHideCols(boolean visibility) + { + hidecols = visibility; + } + /** + * + * @return true if columns covered by group should be hidden + */ + public boolean isHideCols() + { + return hidecols; + } + /** + * create a new sequence group from the intersection of this group + * with an alignment Hashtable of hidden representatives + * + * @param alignment (may not be null) + * @param hashtable (may be null) + * @return new group containing sequences common to this group and alignment + */ + public SequenceGroup intersect(AlignmentI alignment, Hashtable hashtable) + { + SequenceGroup sgroup = new SequenceGroup(this); + SequenceI[] insect=getSequencesInOrder(alignment); + sgroup.sequences = new Vector(); + for (int s=0;insect!=null && s