2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import java.util.ArrayList;
24 import java.util.List;
27 public class HiddenSequences
30 * holds a list of hidden sequences associated with an alignment.
32 public SequenceI[] hiddenSequences;
37 * Constructor given a reference to an alignment (with no hidden sequences)
41 public HiddenSequences(AlignmentI al)
47 * Answers the number of hidden sequences
53 if (hiddenSequences == null)
58 for (SequenceI seq : hiddenSequences)
70 * Answers the length of the longest hidden sequence
76 if (hiddenSequences == null)
81 for (SequenceI seq : hiddenSequences)
83 if (seq != null && seq.getLength() > width)
85 width = seq.getLength();
93 * Call this method after a sequence is removed from the main alignment
95 public void adjustHeightSequenceDeleted(int seqIndex)
97 if (hiddenSequences == null)
102 int alHeight = alignment.getHeight();
104 SequenceI[] tmp = new SequenceI[alHeight + getSize()];
105 int deletionIndex = adjustForHiddenSeqs(seqIndex);
107 for (int i = 0; i < hiddenSequences.length; i++)
109 if (hiddenSequences[i] == null)
114 if (i > deletionIndex)
116 tmp[i - 1] = hiddenSequences[i];
120 tmp[i] = hiddenSequences[i];
124 hiddenSequences = tmp;
129 * Call this method after a sequence is added to the main alignment
131 public void adjustHeightSequenceAdded()
133 if (hiddenSequences == null)
138 int alHeight = alignment.getHeight();
140 SequenceI[] tmp = new SequenceI[alHeight + getSize()];
141 System.arraycopy(hiddenSequences, 0, tmp, 0, hiddenSequences.length);
142 hiddenSequences = tmp;
146 * Mark the specified sequence as hidden
150 public void hideSequence(SequenceI sequence)
152 if (hiddenSequences == null)
154 hiddenSequences = new SequenceI[alignment.getHeight()];
157 int alignmentIndex = alignment.findIndex(sequence);
158 alignmentIndex = adjustForHiddenSeqs(alignmentIndex);
160 if (hiddenSequences[alignmentIndex] != null)
162 System.out.println("ERROR!!!!!!!!!!!");
165 hiddenSequences[alignmentIndex] = sequence;
167 alignment.deleteSequence(sequence);
170 public List<SequenceI> showAll(
171 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
173 List<SequenceI> revealedSeqs = new ArrayList<SequenceI>();
174 for (int i = 0; i < hiddenSequences.length; i++)
176 if (hiddenSequences[i] != null)
178 List<SequenceI> tmp = showSequence(i, hiddenRepSequences);
179 for (SequenceI seq : tmp)
181 revealedSeqs.add(seq);
189 * Reveals (unhides) consecutive hidden sequences just above the given
190 * alignment index. The revealed sequences are selected (including their
191 * visible representative sequence if there was one and 'reveal' is being
194 * @param alignmentIndex
195 * @param hiddenRepSequences
196 * a map of representative sequences to the sequences they represent
199 public List<SequenceI> showSequence(int alignmentIndex,
200 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
202 List<SequenceI> revealedSeqs = new ArrayList<SequenceI>();
203 SequenceI repSequence = alignment.getSequenceAt(alignmentIndex);
204 if (repSequence != null && hiddenRepSequences != null
205 && hiddenRepSequences.containsKey(repSequence))
207 hiddenRepSequences.remove(repSequence);
208 revealedSeqs.add(repSequence);
211 int start = adjustForHiddenSeqs(alignmentIndex - 1);
212 int end = adjustForHiddenSeqs(alignmentIndex);
213 if (end >= hiddenSequences.length)
215 end = hiddenSequences.length - 1;
218 List<SequenceI> asequences;
219 synchronized (asequences = alignment.getSequences())
221 for (int index = end; index > start; index--)
223 SequenceI seq = hiddenSequences[index];
224 hiddenSequences[index] = null;
228 if (seq.getLength() > 0)
230 revealedSeqs.add(seq);
231 asequences.add(alignmentIndex, seq);
235 System.out.println(seq.getName()
236 + " has been deleted whilst hidden");
244 public SequenceI getHiddenSequence(int alignmentIndex)
246 return hiddenSequences == null ? null : hiddenSequences[alignmentIndex];
249 public int findIndexWithoutHiddenSeqs(int alignmentIndex)
251 if (hiddenSequences == null)
253 return alignmentIndex;
257 if (hiddenSequences.length <= alignmentIndex)
259 alignmentIndex = hiddenSequences.length - 1;
262 while (index <= alignmentIndex)
264 if (hiddenSequences[index] != null)
271 return (alignmentIndex - hiddenSeqs);
274 public int adjustForHiddenSeqs(int alignmentIndex)
276 if (hiddenSequences == null)
278 return alignmentIndex;
281 int hSize = hiddenSequences.length;
282 while (index <= alignmentIndex && index < hSize)
284 if (hiddenSequences[index] != null)
292 return alignmentIndex;
296 * makes a copy of the alignment with hidden sequences included. Using the
297 * copy for anything other than simple output is not recommended. Note - this
298 * method DOES NOT USE THE AlignmentI COPY CONSTRUCTOR!
302 public AlignmentI getFullAlignment()
305 if (hiddenSequences == null)
307 seq = alignment.getSequencesArray();
311 int isize = hiddenSequences.length;
312 seq = new Sequence[isize];
315 for (int i = 0; i < hiddenSequences.length; i++)
317 if (hiddenSequences[i] != null)
319 seq[i] = hiddenSequences[i];
323 seq[i] = alignment.getSequenceAt(index);
328 Alignment fAlignmt = new Alignment(seq);
329 fAlignmt.annotations = alignment.getAlignmentAnnotation();
330 fAlignmt.alignmentProperties = alignment.getProperties();
331 fAlignmt.groups = alignment.getGroups();
332 fAlignmt.hasRNAStructure = alignment.hasRNAStructure();
333 fAlignmt.setSeqrep(alignment.getSeqrep());
338 public boolean isHidden(SequenceI seq)
340 if (hiddenSequences != null)
342 for (int i = 0; i < hiddenSequences.length; i++)
344 if (hiddenSequences[i] != null && hiddenSequences[i] == seq)