2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.datamodel;
20 import java.util.Vector;
22 public class CigarArray extends CigarBase
25 * Do CIGAR operations on a set of sequences from many other cigars BAD THINGS
26 * WILL HAPPEN IF A CIGARARRAY IS PASSED TO A CIGARARRAY or a CIGARCIGAR is
27 * given a CIGARARRAY to insert gaps into.
30 * array of subject cigars
32 public CigarSimple refCigars[] = null;
34 private boolean seqcigararray = false;
44 * @return boolean true if all refCigars resolve to a SeqCigar or a CigarCigar
46 public boolean isSeqCigarArray()
52 * Apply CIGAR operations to several cigars in parallel will throw an error if
53 * any of cigar are actually CigarArrays.
58 public CigarArray(CigarSimple[] cigars)
62 if (cigars != null && cigars.length > 0)
64 refCigars = new CigarSimple[cigars.length];
65 for (int c = 0; c < cigars.length; c++)
67 refCigars[c] = cigars[c];
68 if (!((cigars[c] instanceof SeqCigar) || cigars[c] instanceof CigarCigar))
70 seqcigararray = false;
77 * construct a cigar array from the current alignment, or just the subset of
78 * the current alignment specified by selectionGroup. Any columns marked as
79 * hidden in columnSelection will be marked as deleted in the array.
82 * @param columnSelection
83 * @param selectionGroup
85 public CigarArray(AlignmentI alignment, ColumnSelection columnSelection,
86 SequenceGroup selectionGroup)
88 this(constructSeqCigarArray(alignment, selectionGroup));
89 constructFromAlignment(alignment,
90 columnSelection != null ? columnSelection.getHiddenColumns()
91 : null, selectionGroup);
94 private static int[] _calcStartEndBounds(AlignmentI alignment,
95 SequenceGroup selectionGroup)
97 int[] startend = new int[]
99 if (selectionGroup != null)
101 startend[0] = selectionGroup.getSize();
102 startend[1] = selectionGroup.getStartRes();
103 startend[2] = selectionGroup.getEndRes(); // inclusive for start and end
105 // SeqCigar constructor
109 startend[0] = alignment.getHeight();
110 startend[2] = alignment.getWidth() - 1;
115 public static SeqCigar[] constructSeqCigarArray(AlignmentI alignment,
116 SequenceGroup selectionGroup)
118 SequenceI[] seqs = null;
120 int _startend[] = _calcStartEndBounds(alignment, selectionGroup);
121 int start = _startend[1], end = _startend[2];
122 if (selectionGroup != null)
124 iSize = selectionGroup.getSize();
125 seqs = selectionGroup.getSequencesInOrder(alignment);
126 start = selectionGroup.getStartRes();
127 end = selectionGroup.getEndRes(); // inclusive for start and end in
128 // SeqCigar constructor
132 iSize = alignment.getHeight();
133 seqs = alignment.getSequencesArray();
134 end = alignment.getWidth() - 1;
136 SeqCigar[] selseqs = new SeqCigar[iSize];
137 for (i = 0; i < iSize; i++)
139 selseqs[i] = new SeqCigar(seqs[i], start, end);
145 * internal constructor function - called by CigarArray(AlignmentI, ...);
148 * @param columnSelection
149 * - vector of visible regions as returned from
150 * columnSelection.getHiddenColumns()
151 * @param selectionGroup
153 private void constructFromAlignment(AlignmentI alignment,
154 Vector columnSelection, SequenceGroup selectionGroup)
156 int[] _startend = _calcStartEndBounds(alignment, selectionGroup);
157 int start = _startend[1], end = _startend[2];
158 // now construct the CigarArray operations
159 if (columnSelection != null)
162 int hideStart, hideEnd;
164 for (int j = 0; last < end & j < columnSelection.size(); j++)
166 region = (int[]) columnSelection.elementAt(j);
167 hideStart = region[0];
169 // edit hidden regions to selection range
170 if (hideStart < last)
192 if (hideStart > hideEnd)
199 if (last < hideStart)
201 addOperation(CigarArray.M, hideStart - last);
203 addOperation(CigarArray.D, 1 + hideEnd - hideStart);
206 // Final match if necessary.
209 addOperation(CigarArray.M, end - last + 1);
214 addOperation(CigarArray.M, end - start + 1);
219 * @see Cigar.getSequenceAndDeletions
224 protected Object[][] getArrayofSequenceAndDeletions(char GapChar)
226 if (refCigars == null || refCigars.length == 0 || length == 0)
230 Object[][] sqanddels = new Object[refCigars.length][];
231 for (int c = 0; c < refCigars.length; c++)
233 String refString = refCigars[c].getSequenceString(GapChar);
234 if (refString != null)
236 sqanddels[c] = getSequenceAndDeletions(refString, GapChar);
247 * NOTE: this is an improper sequence string function
249 * @return String formed by newline concatenated results of applying CIGAR
250 * operations to each reference object in turn.
253 * @return '\n' separated strings (empty results included as \n\n)
255 public String getSequenceString(char GapChar)
257 if (length == 0 || refCigars == null)
261 StringBuffer seqStrings = new StringBuffer();
262 Object[][] sqanddels = getArrayofSequenceAndDeletions(GapChar);
263 for (int c = 0; c < refCigars.length; c++)
265 if (sqanddels[c] != null)
267 seqStrings.append((String) sqanddels[c][0]);
268 sqanddels[c][0] = null;
270 seqStrings.append('\n');
272 return seqStrings.toString();
276 * return string results of applying cigar string to all reference cigars
282 public String[] getSequenceStrings(char GapChar)
285 if (length == 0 || refCigars == null || refCigars.length == 0)
289 Object[][] sqanddels = getArrayofSequenceAndDeletions(GapChar);
290 String[] seqs = new String[sqanddels.length];
291 for (int c = 0; c < refCigars.length; c++)
293 seqs[c] = (String) sqanddels[c][0];
299 * Combines the CigarArray cigar operations with the operations in each
300 * reference cigar - creating a new reference cigar
304 * public CigarBase[] getEditedCigars() {
306 * return new CigarBase[] {}; }
309 * applyDeletions edits underlying refCigars to propagate deleted regions, and
310 * removes deletion operations from CigarArray operation list.
312 * @return int[] position after deletion occured and range of deletion in
313 * cigarArray or null if none occured
315 public int[] applyDeletions()
317 java.util.Vector delpos = null;
322 int cursor = 0; // range counter for deletions
323 int vcursor = 0; // visible column index
324 int offset = 0; // shift in visible column index as deletions are made
328 if (operation[i] != D)
330 if (operation[i] == M)
334 vcursor += range[i++];
340 delpos = new java.util.Vector();
342 int delstart = cursor, delend = cursor + range[i] - 1; // inclusive
343 delpos.addElement(new int[]
344 { vcursor + offset, range[i] }); // index of right hand column after
345 // hidden region boundary
346 offset += range[i] - 1; // shift in visible column coordinates
347 System.arraycopy(operation, i + 1, operation, i, length - i);
348 System.arraycopy(range, i + 1, range, i, length - i);
351 * int dmax=0; for (int s=0; s<refCigars.length; s++) { int d =
352 * refCigars[s].deleteRange(delstart, delend); if (d>dmax) dmax=d; }
353 * offset+=dmax; // shift in visible column coordinates
355 for (int s = 0; s < refCigars.length; s++)
357 int d = refCigars[s].deleteRange(delstart, delend);
364 int[] pos = new int[delpos.size() * 2];
365 for (int k = 0, l = delpos.size(); k < l; k++)
367 int[] dr = ((int[]) delpos.elementAt(k));
369 pos[k * 2 + 1] = dr[1];
370 delpos.setElementAt(null, k);
380 * @return SeqCigar[] or null if CigarArray is not a SeqCigarArray (ie it does
381 * not resolve to set of seqCigars)
383 public SeqCigar[] getSeqCigarArray()
385 if (!isSeqCigarArray())
389 SeqCigar[] sa = new SeqCigar[refCigars.length];
390 for (int i = 0; i < refCigars.length; i++)
392 sa[i] = (SeqCigar) refCigars[i];