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.Iterator;
25 public class CigarArray extends CigarBase
28 * Do CIGAR operations on a set of sequences from many other cigars BAD THINGS
29 * WILL HAPPEN IF A CIGARARRAY IS PASSED TO A CIGARARRAY or a CIGARCIGAR is
30 * given a CIGARARRAY to insert gaps into.
33 * array of subject cigars
35 public CigarSimple refCigars[] = null;
37 private boolean seqcigararray = false;
47 * @return boolean true if all refCigars resolve to a SeqCigar or a CigarCigar
49 public boolean isSeqCigarArray()
55 * Apply CIGAR operations to several cigars in parallel will throw an error if
56 * any of cigar are actually CigarArrays.
61 public CigarArray(CigarSimple[] cigars)
65 if (cigars != null && cigars.length > 0)
67 refCigars = new CigarSimple[cigars.length];
68 for (int c = 0; c < cigars.length; c++)
70 refCigars[c] = cigars[c];
71 if (!((cigars[c] instanceof SeqCigar)
72 || cigars[c] instanceof CigarCigar))
74 seqcigararray = false;
81 * construct a cigar array from the current alignment, or just the subset of
82 * the current alignment specified by selectionGroup. Any columns marked as
83 * hidden in columnSelection will be marked as deleted in the array.
86 * @param columnSelection
87 * @param selectionGroup
89 public CigarArray(AlignmentI alignment, HiddenColumns hidden,
90 SequenceGroup selectionGroup)
92 this(constructSeqCigarArray(alignment, selectionGroup));
93 constructFromAlignment(alignment, hidden, selectionGroup);
96 private static int[] _calcStartEndBounds(AlignmentI alignment,
97 SequenceGroup selectionGroup)
99 int[] startend = new int[] { 0, 0, 0 };
100 if (selectionGroup != null)
102 startend[0] = selectionGroup.getSize();
103 startend[1] = selectionGroup.getStartRes();
104 startend[2] = selectionGroup.getEndRes(); // inclusive for start and end
106 // SeqCigar constructor
110 startend[0] = alignment.getHeight();
111 startend[2] = alignment.getWidth() - 1;
116 public static SeqCigar[] constructSeqCigarArray(AlignmentI alignment,
117 SequenceGroup selectionGroup)
119 SequenceI[] seqs = null;
121 int _startend[] = _calcStartEndBounds(alignment, selectionGroup);
122 int start = _startend[1], end = _startend[2];
123 if (selectionGroup != null)
125 iSize = selectionGroup.getSize();
126 seqs = selectionGroup.getSequencesInOrder(alignment);
127 start = selectionGroup.getStartRes();
128 end = selectionGroup.getEndRes(); // inclusive for start and end in
129 // SeqCigar constructor
133 iSize = alignment.getHeight();
134 seqs = alignment.getSequencesArray();
135 end = alignment.getWidth() - 1;
137 SeqCigar[] selseqs = new SeqCigar[iSize];
138 for (i = 0; i < iSize; i++)
140 selseqs[i] = new SeqCigar(seqs[i], start, end);
146 * internal constructor function - called by CigarArray(AlignmentI, ...);
150 * - vector of visible regions as returned from
151 * columnSelection.getHiddenColumns()
152 * @param selectionGroup
154 private void constructFromAlignment(AlignmentI alignment,
155 HiddenColumns hidden, SequenceGroup selectionGroup)
157 int[] _startend = _calcStartEndBounds(alignment, selectionGroup);
158 int start = _startend[1];
159 int end = _startend[2];
160 // now construct the CigarArray operations
168 Iterator<int[]> regions = hidden.getBoundedIterator(start, end);
169 while (regions.hasNext())
171 region = regions.next();
172 hideStart = region[0];
175 // truncate region at start if last falls in region
176 if ((hideStart < last) && (hideEnd >= last))
181 // truncate region at end if end falls in region
182 if (hideEnd > end) // already checked that hideStart<=end
190 if (last < hideStart)
192 addOperation(CigarArray.M, hideStart - last);
194 addOperation(CigarArray.D, 1 + hideEnd - hideStart);
198 // Final match if necessary.
201 addOperation(CigarArray.M, end - last + 1);
206 addOperation(CigarArray.M, end - start + 1);
211 * @see CigarBase.getSequenceAndDeletions
216 protected Object[][] getArrayofSequenceAndDeletions(char GapChar)
218 if (refCigars == null || refCigars.length == 0 || length == 0)
222 Object[][] sqanddels = new Object[refCigars.length][];
223 for (int c = 0; c < refCigars.length; c++)
225 String refString = refCigars[c].getSequenceString(GapChar);
226 if (refString != null)
228 sqanddels[c] = getSequenceAndDeletions(refString, GapChar);
239 * NOTE: this is an improper sequence string function
241 * @return String formed by newline concatenated results of applying CIGAR
242 * operations to each reference object in turn.
245 * @return '\n' separated strings (empty results included as \n\n)
247 public String getSequenceString(char GapChar)
249 if (length == 0 || refCigars == null)
253 StringBuffer seqStrings = new StringBuffer();
254 Object[][] sqanddels = getArrayofSequenceAndDeletions(GapChar);
255 for (int c = 0; c < refCigars.length; c++)
257 if (sqanddels[c] != null)
259 seqStrings.append((String) sqanddels[c][0]);
260 sqanddels[c][0] = null;
262 seqStrings.append('\n');
264 return seqStrings.toString();
268 * return string results of applying cigar string to all reference cigars
274 public String[] getSequenceStrings(char GapChar)
277 if (length == 0 || refCigars == null || refCigars.length == 0)
281 Object[][] sqanddels = getArrayofSequenceAndDeletions(GapChar);
282 String[] seqs = new String[sqanddels.length];
283 for (int c = 0; c < refCigars.length; c++)
285 seqs[c] = (String) sqanddels[c][0];
291 * Combines the CigarArray cigar operations with the operations in each
292 * reference cigar - creating a new reference cigar
296 * public CigarBase[] getEditedCigars() {
298 * return new CigarBase[] {}; }
301 * applyDeletions edits underlying refCigars to propagate deleted regions, and
302 * removes deletion operations from CigarArray operation list.
304 * @return int[] position after deletion occured and range of deletion in
305 * cigarArray or null if none occured
307 public int[] applyDeletions()
309 java.util.Vector delpos = null;
314 int cursor = 0; // range counter for deletions
315 int vcursor = 0; // visible column index
316 int offset = 0; // shift in visible column index as deletions are made
320 if (operation[i] != D)
322 if (operation[i] == M)
326 vcursor += range[i++];
332 delpos = new java.util.Vector();
334 int delstart = cursor, delend = cursor + range[i] - 1; // inclusive
335 delpos.addElement(new int[] { vcursor + offset, range[i] }); // index of
340 // hidden region boundary
341 offset += range[i] - 1; // shift in visible column coordinates
342 System.arraycopy(operation, i + 1, operation, i, length - i);
343 System.arraycopy(range, i + 1, range, i, length - i);
346 * int dmax=0; for (int s=0; s<refCigars.length; s++) { int d =
347 * refCigars[s].deleteRange(delstart, delend); if (d>dmax) dmax=d; }
348 * offset+=dmax; // shift in visible column coordinates
350 for (int s = 0; s < refCigars.length; s++)
352 int d = refCigars[s].deleteRange(delstart, delend);
359 int[] pos = new int[delpos.size() * 2];
360 for (int k = 0, l = delpos.size(); k < l; k++)
362 int[] dr = ((int[]) delpos.elementAt(k));
364 pos[k * 2 + 1] = dr[1];
365 delpos.setElementAt(null, k);
375 * @return SeqCigar[] or null if CigarArray is not a SeqCigarArray (ie it does
376 * not resolve to set of seqCigars)
378 public SeqCigar[] getSeqCigarArray()
380 if (!isSeqCigarArray())
384 SeqCigar[] sa = new SeqCigar[refCigars.length];
385 for (int i = 0; i < refCigars.length; i++)
387 sa[i] = (SeqCigar) refCigars[i];