2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.datamodel;
24 * <p>Description: </p>
26 * <p>Copyright: Copyright (c) 2004</p>
28 * <p>Company: Dundee University</p>
30 * @author not attributable
33 public class AlignmentView
36 * Transient object compactly representing a 'view' of an alignment - with discontinuities marked.
38 private SeqCigar[] sequences = null;
39 private int[] contigs = null;
40 private int width = 0;
41 private int firstCol = 0;
42 public AlignmentView(CigarArray seqcigararray)
44 if (!seqcigararray.isSeqCigarArray())
46 throw new Error("Implementation Error - can only make an alignment view from a CigarArray of sequences.");
48 //contigs = seqcigararray.applyDeletions();
49 contigs = seqcigararray.getDeletedRegions();
50 sequences = seqcigararray.getSeqCigarArray();
51 width = seqcigararray.getWidth(); // visible width
55 * Create an alignmentView where the first column corresponds with the 'firstcol' column of some reference alignment
59 public AlignmentView(CigarArray sdata, int firstcol)
65 public void setSequences(SeqCigar[] sequences)
67 this.sequences = sequences;
70 public void setContigs(int[] contigs)
72 this.contigs = contigs;
75 public SeqCigar[] getSequences()
81 * @see CigarArray.getDeletedRegions
82 * @return int[] { vis_start, sym_start, length }
84 public int[] getContigs()
90 * get the full alignment and a columnselection object marking the hidden regions
91 * @param gapCharacter char
92 * @return Object[] { SequenceI[], ColumnSelection}
94 public Object[] getAlignmentAndColumnSelection(char gapCharacter)
96 ColumnSelection colsel = new ColumnSelection();
100 SeqCigar.createAlignmentSequences(sequences, gapCharacter, colsel,
110 public String[] getSequenceStrings(char c)
112 String[] seqs = new String[sequences.length];
113 for (int n = 0; n < sequences.length; n++)
115 String fullseq = sequences[n].getSequenceString(c);
120 for (int h = 0; h < contigs.length; h += 3)
122 seqs[n] += fullseq.substring(p, contigs[h + 1]);
123 p = contigs[h + 1] + contigs[h + 2];
125 seqs[n] += fullseq.substring(p);
137 * @return visible number of columns in alignment view
139 public int getWidth()
144 protected void setWidth(int width)
150 * get the contiguous subalignments in an alignment view.
151 * @param gapCharacter char
152 * @return SequenceI[][]
154 public SequenceI[][] getVisibleContigs(char gapCharacter)
158 if (sequences == null || width <= 0)
162 if (contigs != null && contigs.length > 0)
167 for (int contig = 0; contig < contigs.length; contig += 3)
169 if ( (contigs[contig + 1] - start) > 0)
173 fwidth += contigs[contig + 2]; // end up with full region width (including hidden regions)
174 start = contigs[contig + 1] + contigs[contig + 2];
180 smsa = new SequenceI[njobs][];
183 for (int contig = 0; contig < contigs.length; contig += 3)
185 if (contigs[contig + 1] - start > 0)
187 SequenceI mseq[] = new SequenceI[sequences.length];
188 for (int s = 0; s < mseq.length; s++)
190 mseq[s] = sequences[s].getSeq(gapCharacter).getSubSequence(start,
191 contigs[contig + 1]);
196 start = contigs[contig + 1] + contigs[contig + 2];
200 SequenceI mseq[] = new SequenceI[sequences.length];
201 for (int s = 0; s < mseq.length; s++)
203 mseq[s] = sequences[s].getSeq(gapCharacter).getSubSequence(start,
212 smsa = new SequenceI[1][];
213 smsa[0] = new SequenceI[sequences.length];
214 for (int s = 0; s < sequences.length; s++)
216 smsa[0][s] = sequences[s].getSeq(gapCharacter);
223 * return full msa and hidden regions with visible blocks replaced with new sub alignments
224 * @param nvismsa SequenceI[][]
225 * @param orders AlignmentOrder[] corresponding to each SequenceI[] block.
228 public Object[] getUpdatedView(SequenceI[][] nvismsa, AlignmentOrder[] orders,
231 if (sequences == null || width <= 0)
233 throw new Error("empty view cannot be updated.");
238 "nvismsa==null. use getAlignmentAndColumnSelection() instead.");
240 if (contigs != null && contigs.length > 0)
242 SequenceI[] alignment = new SequenceI[sequences.length];
243 ColumnSelection columnselection = new ColumnSelection();
244 if (contigs != null && contigs.length > 0)
250 for (int contig = 0; contig < contigs.length; contig += 3)
252 owidth += contigs[contig + 2]; // recover final column width
253 if (contigs[contig + 1] - start > 0)
255 int swidth = 0; // subalignment width
256 if (nvismsa[j] != null)
258 SequenceI mseq[] = nvismsa[j];
259 AlignmentOrder order = (orders == null) ? null : orders[j];
261 if (mseq.length != sequences.length)
264 "Mismatch between number of sequences in block " + j + " (" +
265 mseq.length + ") and the original view (" +
266 sequences.length + ")");
268 swidth = mseq[0].getLength(); // JBPNote: could ensure padded here.
269 for (int s = 0; s < mseq.length; s++)
271 if (alignment[s] == null)
273 alignment[s] = mseq[s];
277 alignment[s].setSequence(alignment[s].getSequenceAsString() +
278 mseq[s].getSequenceAsString());
279 if (mseq[s].getStart() <= mseq[s].getEnd())
281 alignment[s].setEnd(mseq[s].getEnd());
285 order.updateSequence(mseq[s], alignment[s]);
292 // recover original alignment block or place gaps
295 // recover input data
296 for (int s = 0; s < sequences.length; s++)
298 SequenceI oseq = sequences[s].getSeq(gapCharacter).
299 getSubSequence(start,
300 contigs[contig + 1]);
301 if (swidth < oseq.getLength())
303 swidth = oseq.getLength();
305 if (alignment[s] == null)
311 alignment[s].setSequence(alignment[s].getSequenceAsString() +
312 oseq.getSequenceAsString());
313 if (oseq.getEnd() >= oseq.getStart())
315 alignment[s].setEnd(oseq.getEnd());
325 // advance to begining of visible region
326 start = contigs[contig + 1] + contigs[contig + 2];
327 // add hidden segment to right of next region
328 for (int s = 0; s < sequences.length; s++)
330 SequenceI hseq = sequences[s].getSeq(gapCharacter).getSubSequence(
333 if (alignment[s] == null)
339 alignment[s].setSequence(alignment[s].getSequenceAsString() +
340 hseq.getSequenceAsString());
341 if (hseq.getEnd() >= hseq.getStart())
343 alignment[s].setEnd(hseq.getEnd());
347 // mark hidden segment as hidden in the new alignment
348 columnselection.hideColumns(nwidth, nwidth + contigs[contig + 2] - 1);
349 nwidth += contigs[contig + 2];
351 // Do final segment - if it exists
352 if (j < nvismsa.length)
355 if (nvismsa[j] != null)
357 SequenceI mseq[] = nvismsa[j];
358 AlignmentOrder order = (orders != null) ? orders[j] : null;
359 swidth = mseq[0].getLength();
360 for (int s = 0; s < mseq.length; s++)
362 if (alignment[s] == null)
364 alignment[s] = mseq[s];
368 alignment[s].setSequence(alignment[s].getSequenceAsString() +
369 mseq[s].getSequenceAsString());
370 if (mseq[s].getEnd() >= mseq[s].getStart())
372 alignment[s].setEnd(mseq[s].getEnd());
376 order.updateSequence(mseq[s], alignment[s]);
385 // recover input data or place gaps
388 // recover input data
389 for (int s = 0; s < sequences.length; s++)
391 SequenceI oseq = sequences[s].getSeq(gapCharacter).
392 getSubSequence(start,
394 if (swidth < oseq.getLength())
396 swidth = oseq.getLength();
398 if (alignment[s] == null)
404 alignment[s].setSequence(alignment[s].getSequenceAsString() +
405 oseq.getSequenceAsString());
406 if (oseq.getEnd() >= oseq.getStart())
408 alignment[s].setEnd(oseq.getEnd());
417 throw new Error("Padding not yet implemented.");
425 alignment, columnselection};
429 if (nvismsa.length != 1)
431 throw new Error("Mismatch between visible blocks to update and number of contigs in view (contigs=0,blocks=" +
434 if (nvismsa[0] != null)
438 nvismsa[0], new ColumnSelection()};
442 return getAlignmentAndColumnSelection(gapCharacter);
448 * returns simple array of start end positions of visible range on alignment.
449 * vis_start and vis_end are inclusive - use SequenceI.getSubSequence(vis_start, vis_end+1) to recover visible sequence from underlying alignment.
450 * @return int[] { start_i, end_i } for 1<i<n visible regions.
452 public int[] getVisibleContigs()
454 if (contigs != null && contigs.length > 0)
459 for (int contig = 0; contig < contigs.length; contig += 3)
461 if ( (contigs[contig + 1] - start) > 0)
465 fwidth += contigs[contig + 2]; // end up with full region width (including hidden regions)
466 start = contigs[contig + 1] + contigs[contig + 2];
472 int viscontigs[] = new int[nvis * 2];
475 for (int contig = 0; contig < contigs.length; contig += 3)
477 if ( (contigs[contig + 1] - start) > 0)
479 viscontigs[nvis] = start;
480 viscontigs[nvis + 1] = contigs[contig + 1] - 1; // end is inclusive
483 start = contigs[contig + 1] + contigs[contig + 2];
487 viscontigs[nvis] = start;
488 viscontigs[nvis + 1] = fwidth; // end is inclusive
503 * @return position of first visible column of AlignmentView within its parent's alignment reference frame
505 public int getAlignmentOrigin()
507 // TODO Auto-generated method stub