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 htsjdk.samtools.Cigar;
25 import java.util.List;
27 public class CigarArray extends CigarBase
30 * Do CIGAR operations on a set of sequences from many other cigars BAD THINGS
31 * WILL HAPPEN IF A CIGARARRAY IS PASSED TO A CIGARARRAY or a CIGARCIGAR is
32 * given a CIGARARRAY to insert gaps into.
35 * array of subject cigars
37 public CigarSimple refCigars[] = null;
39 private boolean seqcigararray = false;
49 * @return boolean true if all refCigars resolve to a SeqCigar or a CigarCigar
51 public boolean isSeqCigarArray()
57 * Apply CIGAR operations to several cigars in parallel will throw an error if
58 * any of cigar are actually CigarArrays.
63 public CigarArray(CigarSimple[] cigars)
67 if (cigars != null && cigars.length > 0)
69 refCigars = new CigarSimple[cigars.length];
70 for (int c = 0; c < cigars.length; c++)
72 refCigars[c] = cigars[c];
73 if (!((cigars[c] instanceof SeqCigar) || cigars[c] instanceof CigarCigar))
75 seqcigararray = false;
82 * construct a cigar array from the current alignment, or just the subset of
83 * the current alignment specified by selectionGroup. Any columns marked as
84 * hidden in columnSelection will be marked as deleted in the array.
87 * @param columnSelection
88 * @param selectionGroup
90 public CigarArray(AlignmentI alignment, HiddenColumns hidden,
91 SequenceGroup selectionGroup)
93 this(constructSeqCigarArray(alignment, selectionGroup));
94 constructFromAlignment(alignment,
95 hidden != null ? hidden.getHiddenRegions()
96 : null, selectionGroup);
99 private static int[] _calcStartEndBounds(AlignmentI alignment,
100 SequenceGroup selectionGroup)
102 int[] startend = new int[] { 0, 0, 0 };
103 if (selectionGroup != null)
105 startend[0] = selectionGroup.getSize();
106 startend[1] = selectionGroup.getStartRes();
107 startend[2] = selectionGroup.getEndRes(); // inclusive for start and end
109 // SeqCigar constructor
113 startend[0] = alignment.getHeight();
114 startend[2] = alignment.getWidth() - 1;
119 public static SeqCigar[] constructSeqCigarArray(AlignmentI alignment,
120 SequenceGroup selectionGroup)
122 SequenceI[] seqs = null;
124 int _startend[] = _calcStartEndBounds(alignment, selectionGroup);
125 int start = _startend[1], end = _startend[2];
126 if (selectionGroup != null)
128 iSize = selectionGroup.getSize();
129 seqs = selectionGroup.getSequencesInOrder(alignment);
130 start = selectionGroup.getStartRes();
131 end = selectionGroup.getEndRes(); // inclusive for start and end in
132 // SeqCigar constructor
136 iSize = alignment.getHeight();
137 seqs = alignment.getSequencesArray();
138 end = alignment.getWidth() - 1;
140 SeqCigar[] selseqs = new SeqCigar[iSize];
141 for (i = 0; i < iSize; i++)
143 selseqs[i] = new SeqCigar(seqs[i], start, end);
149 * internal constructor function - called by CigarArray(AlignmentI, ...);
153 * - vector of visible regions as returned from
154 * columnSelection.getHiddenColumns()
155 * @param selectionGroup
157 private void constructFromAlignment(AlignmentI alignment,
158 List<int[]> list, SequenceGroup selectionGroup)
160 int[] _startend = _calcStartEndBounds(alignment, selectionGroup);
161 int start = _startend[1], end = _startend[2];
162 // now construct the CigarArray operations
166 int hideStart, hideEnd;
168 for (int j = 0; last < end & j < list.size(); j++)
170 region = list.get(j);
171 hideStart = region[0];
173 // edit hidden regions to selection range
174 if (hideStart < last)
196 if (hideStart > hideEnd)
203 if (last < hideStart)
205 addOperation(CigarArray.M, hideStart - last);
207 addOperation(CigarArray.D, 1 + hideEnd - hideStart);
210 // Final match if necessary.
213 addOperation(CigarArray.M, end - last + 1);
218 addOperation(CigarArray.M, end - start + 1);
223 * @see Cigar.getSequenceAndDeletions
228 protected Object[][] getArrayofSequenceAndDeletions(char GapChar)
230 if (refCigars == null || refCigars.length == 0 || length == 0)
234 Object[][] sqanddels = new Object[refCigars.length][];
235 for (int c = 0; c < refCigars.length; c++)
237 String refString = refCigars[c].getSequenceString(GapChar);
238 if (refString != null)
240 sqanddels[c] = getSequenceAndDeletions(refString, GapChar);
251 * NOTE: this is an improper sequence string function
253 * @return String formed by newline concatenated results of applying CIGAR
254 * operations to each reference object in turn.
257 * @return '\n' separated strings (empty results included as \n\n)
259 public String getSequenceString(char GapChar)
261 if (length == 0 || refCigars == null)
265 StringBuffer seqStrings = new StringBuffer();
266 Object[][] sqanddels = getArrayofSequenceAndDeletions(GapChar);
267 for (int c = 0; c < refCigars.length; c++)
269 if (sqanddels[c] != null)
271 seqStrings.append((String) sqanddels[c][0]);
272 sqanddels[c][0] = null;
274 seqStrings.append('\n');
276 return seqStrings.toString();
280 * return string results of applying cigar string to all reference cigars
286 public String[] getSequenceStrings(char GapChar)
289 if (length == 0 || refCigars == null || refCigars.length == 0)
293 Object[][] sqanddels = getArrayofSequenceAndDeletions(GapChar);
294 String[] seqs = new String[sqanddels.length];
295 for (int c = 0; c < refCigars.length; c++)
297 seqs[c] = (String) sqanddels[c][0];
303 * Combines the CigarArray cigar operations with the operations in each
304 * reference cigar - creating a new reference cigar
308 * public CigarBase[] getEditedCigars() {
310 * return new CigarBase[] {}; }
313 * applyDeletions edits underlying refCigars to propagate deleted regions, and
314 * removes deletion operations from CigarArray operation list.
316 * @return int[] position after deletion occured and range of deletion in
317 * cigarArray or null if none occured
319 public int[] applyDeletions()
321 java.util.Vector delpos = null;
326 int cursor = 0; // range counter for deletions
327 int vcursor = 0; // visible column index
328 int offset = 0; // shift in visible column index as deletions are made
332 if (operation[i] != D)
334 if (operation[i] == M)
338 vcursor += range[i++];
344 delpos = new java.util.Vector();
346 int delstart = cursor, delend = cursor + range[i] - 1; // inclusive
347 delpos.addElement(new int[] { vcursor + offset, range[i] }); // index of
352 // hidden region boundary
353 offset += range[i] - 1; // shift in visible column coordinates
354 System.arraycopy(operation, i + 1, operation, i, length - i);
355 System.arraycopy(range, i + 1, range, i, length - i);
358 * int dmax=0; for (int s=0; s<refCigars.length; s++) { int d =
359 * refCigars[s].deleteRange(delstart, delend); if (d>dmax) dmax=d; }
360 * offset+=dmax; // shift in visible column coordinates
362 for (int s = 0; s < refCigars.length; s++)
364 int d = refCigars[s].deleteRange(delstart, delend);
371 int[] pos = new int[delpos.size() * 2];
372 for (int k = 0, l = delpos.size(); k < l; k++)
374 int[] dr = ((int[]) delpos.elementAt(k));
376 pos[k * 2 + 1] = dr[1];
377 delpos.setElementAt(null, k);
387 * @return SeqCigar[] or null if CigarArray is not a SeqCigarArray (ie it does
388 * not resolve to set of seqCigars)
390 public SeqCigar[] getSeqCigarArray()
392 if (!isSeqCigarArray())
396 SeqCigar[] sa = new SeqCigar[refCigars.length];
397 for (int i = 0; i < refCigars.length; i++)
399 sa[i] = (SeqCigar) refCigars[i];