X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FHiddenSequences.java;h=d3aa01af3aaacd2b668c9b5946e55d50f6357f6d;hb=7f4da0a2b0c10f158a03dbfce8878e67e90c4a68;hp=929e56efdeebe3645c1c48b1118c9e047f659324;hpb=71881f2cf715e6143cd594e20bd41db538282f81;p=jalview.git diff --git a/src/jalview/datamodel/HiddenSequences.java b/src/jalview/datamodel/HiddenSequences.java index 929e56e..d3aa01a 100755 --- a/src/jalview/datamodel/HiddenSequences.java +++ b/src/jalview/datamodel/HiddenSequences.java @@ -33,11 +33,21 @@ public class HiddenSequences AlignmentI alignment; + /** + * Constructor given a reference to an alignment (with no hidden sequences) + * + * @param al + */ public HiddenSequences(AlignmentI al) { alignment = al; } + /** + * Answers the number of hidden sequences + * + * @return + */ public int getSize() { if (hiddenSequences == null) @@ -45,9 +55,9 @@ public class HiddenSequences return 0; } int count = 0; - for (int i = 0; i < hiddenSequences.length; i++) + for (SequenceI seq : hiddenSequences) { - if (hiddenSequences[i] != null) + if (seq != null) { count++; } @@ -56,15 +66,23 @@ public class HiddenSequences return count; } + /** + * Answers the length of the longest hidden sequence + * + * @return + */ public int getWidth() { + if (hiddenSequences == null) + { + return 0; + } int width = 0; - for (int i = 0; i < hiddenSequences.length; i++) + for (SequenceI seq : hiddenSequences) { - if (hiddenSequences[i] != null - && hiddenSequences[i].getLength() > width) + if (seq != null && seq.getLength() > width) { - width = hiddenSequences[i].getLength(); + width = seq.getLength(); } } @@ -72,7 +90,7 @@ public class HiddenSequences } /** - * Call this method if sequences are removed from the main alignment + * Call this method after a sequence is removed from the main alignment */ public void adjustHeightSequenceDeleted(int seqIndex) { @@ -108,8 +126,7 @@ public class HiddenSequences } /** - * Call this method if sequences are added to or removed from the main - * alignment + * Call this method after a sequence is added to the main alignment */ public void adjustHeightSequenceAdded() { @@ -125,6 +142,11 @@ public class HiddenSequences hiddenSequences = tmp; } + /** + * Mark the specified sequence as hidden + * + * @param sequence + */ public void hideSequence(SequenceI sequence) { if (hiddenSequences == null) @@ -132,8 +154,8 @@ public class HiddenSequences hiddenSequences = new SequenceI[alignment.getHeight()]; } - int alignmentIndex = alignment.findIndex(sequence); - alignmentIndex = adjustForHiddenSeqs(alignmentIndex); + int absAlignmentIndex = alignment.findIndex(sequence); + int alignmentIndex = adjustForHiddenSeqs(absAlignmentIndex); if (hiddenSequences[alignmentIndex] != null) { @@ -142,7 +164,7 @@ public class HiddenSequences hiddenSequences[alignmentIndex] = sequence; - alignment.deleteSequence(sequence); + alignment.deleteHiddenSequence(absAlignmentIndex); } public List showAll( @@ -163,6 +185,17 @@ public class HiddenSequences return revealedSeqs; } + /** + * Reveals (unhides) consecutive hidden sequences just above the given + * alignment index. The revealed sequences are selected (including their + * visible representative sequence if there was one and 'reveal' is being + * performed on it). + * + * @param alignmentIndex + * @param hiddenRepSequences + * a map of representative sequences to the sequences they represent + * @return + */ public List showSequence(int alignmentIndex, Map hiddenRepSequences) { @@ -203,24 +236,38 @@ public class HiddenSequences + " has been deleted whilst hidden"); } } - } } - return revealedSeqs; } public SequenceI getHiddenSequence(int alignmentIndex) { - return hiddenSequences[alignmentIndex]; + return hiddenSequences == null ? null : hiddenSequences[alignmentIndex]; } + /** + * Convert absolute alignment index to visible alignment index + * + * @param alignmentIndex + * @return + */ public int findIndexWithoutHiddenSeqs(int alignmentIndex) { + if (hiddenSequences == null) + { + return alignmentIndex; + } int index = 0; int hiddenSeqs = 0; + int diff = 0; if (hiddenSequences.length <= alignmentIndex) { + // if the alignmentIndex runs past the end of hidden sequences + // and therefore actually past the end of the alignment + // store the difference to add back on at the end, so that behaviour + // is consistent with hidden columns behaviour (used by overview panel) + diff = alignmentIndex - hiddenSequences.length + 1; alignmentIndex = hiddenSequences.length - 1; } @@ -232,13 +279,57 @@ public class HiddenSequences } index++; } - ; - return (alignmentIndex - hiddenSeqs); + return (alignmentIndex - hiddenSeqs + diff); + } + + /** + * Find the visible row which is a given visible number of rows above another + * visible row. i.e. for a startRow x, the row which is distance 1 away will + * be row x-1. + * + * @param visibleDistance + * the number of visible rows to offset by + * @param startRow + * the row to start from + * @return the position of the row in the visible alignment + */ + public int findIndexNAboveRow(int visibleDistance, int startRow) + { + // walk upwards through the alignment + // count all the non-null sequences until we have visibleDistance counted + // then return the next visible sequence + if (hiddenSequences == null) + { + return startRow - visibleDistance; + } + + int index = startRow; + int count = 0; + while ((index > -1) && (count < visibleDistance)) + { + if (hiddenSequences[index] == null) + { + // count visible sequences + count++; + } + index--; + } + return index; } + /** + * Convert alignment index from visible alignment to absolute alignment + * + * @param alignmentIndex + * @return + */ public int adjustForHiddenSeqs(int alignmentIndex) { + if (hiddenSequences == null) + { + return alignmentIndex; + } int index = 0; int hSize = hiddenSequences.length; while (index <= alignmentIndex && index < hSize) @@ -254,26 +345,47 @@ public class HiddenSequences return alignmentIndex; } + /** + * makes a copy of the alignment with hidden sequences included. Using the + * copy for anything other than simple output is not recommended. Note - this + * method DOES NOT USE THE AlignmentI COPY CONSTRUCTOR! + * + * @return + */ public AlignmentI getFullAlignment() { - int isize = hiddenSequences.length; - SequenceI[] seq = new Sequence[isize]; - - int index = 0; - for (int i = 0; i < hiddenSequences.length; i++) + SequenceI[] seq; + if (hiddenSequences == null) { - if (hiddenSequences[i] != null) - { - seq[i] = hiddenSequences[i]; - } - else + seq = alignment.getSequencesArray(); + } + else + { + int isize = hiddenSequences.length; + seq = new Sequence[isize]; + + int index = 0; + for (int i = 0; i < hiddenSequences.length; i++) { - seq[i] = alignment.getSequenceAt(index); - index++; + if (hiddenSequences[i] != null) + { + seq[i] = hiddenSequences[i]; + } + else + { + seq[i] = alignment.getSequenceAt(index); + index++; + } } } - - return new Alignment(seq); + Alignment fAlignmt = new Alignment(seq); + fAlignmt.annotations = alignment.getAlignmentAnnotation(); + fAlignmt.alignmentProperties = alignment.getProperties(); + fAlignmt.groups = alignment.getGroups(); + fAlignmt.hasRNAStructure = alignment.hasRNAStructure(); + fAlignmt.setSeqrep(alignment.getSeqrep()); + + return fAlignmt; } public boolean isHidden(SequenceI seq)