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