2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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;
26 * <p>Description: </p>
28 * <p>Copyright: Copyright (c) 2004</p>
30 * <p>Company: Dundee University</p>
32 * @author not attributable
35 public class AlignmentView
38 * Transient object compactly representing a 'view' of an alignment - with discontinuities marked.
40 private SeqCigar[] sequences = null;
41 private int[] contigs = null;
43 private int firstCol=0;
44 public AlignmentView(CigarArray seqcigararray)
46 if (!seqcigararray.isSeqCigarArray())
47 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
54 * Create an alignmentView where the first column corresponds with the 'firstcol' column of some reference alignment
58 public AlignmentView(CigarArray sdata, int firstcol) {
63 public void setSequences(SeqCigar[] sequences)
65 this.sequences = sequences;
68 public void setContigs(int[] contigs)
70 this.contigs = contigs;
73 public SeqCigar[] getSequences()
78 * @see CigarArray.getDeletedRegions
79 * @return int[] { vis_start, sym_start, length }
81 public int[] getContigs()
86 * get the full alignment and a columnselection object marking the hidden regions
87 * @param gapCharacter char
88 * @return Object[] { SequenceI[], ColumnSelection}
90 public Object[] getAlignmentAndColumnSelection(char gapCharacter) {
91 ColumnSelection colsel = new ColumnSelection();
93 return new Object[] { SeqCigar.createAlignmentSequences(sequences, gapCharacter, colsel, contigs), colsel};
101 public String[] getSequenceStrings(char c)
103 String[] seqs=new String[sequences.length];
104 for (int n=0; n<sequences.length; n++) {
105 String fullseq = sequences[n].getSequenceString(c);
110 for (int h = 0; h < contigs.length; h += 3)
112 seqs[n] += fullseq.substring(p, contigs[h + 1]);
113 p = contigs[h + 1] + contigs[h + 2];
115 seqs[n] += fullseq.substring(p);
123 * @return visible number of columns in alignment view
125 public int getWidth() {
129 protected void setWidth(int width) {
133 * get the contiguous subalignments in an alignment view.
134 * @param gapCharacter char
135 * @return SequenceI[][]
137 public SequenceI[][] getVisibleContigs(char gapCharacter) {
140 if (sequences==null || width<=0)
142 if (contigs != null && contigs.length > 0)
147 for (int contig = 0; contig < contigs.length; contig += 3)
149 if ( (contigs[contig + 1] - start) > 0)
153 fwidth += contigs[contig + 2]; // end up with full region width (including hidden regions)
154 start = contigs[contig + 1] + contigs[contig + 2];
160 smsa = new SequenceI[njobs][];
163 for (int contig = 0; contig < contigs.length; contig += 3)
165 if (contigs[contig + 1] - start > 0)
167 SequenceI mseq[] = new SequenceI[sequences.length];
168 for (int s = 0; s < mseq.length; s++)
170 mseq[s] = sequences[s].getSeq(gapCharacter).getSubSequence(start,
171 contigs[contig + 1]);
176 start = contigs[contig + 1] + contigs[contig + 2];
180 SequenceI mseq[] = new SequenceI[sequences.length];
181 for (int s = 0; s < mseq.length; s++)
183 mseq[s] = sequences[s].getSeq(gapCharacter).getSubSequence(start,
192 smsa = new SequenceI[1][];
193 smsa[0] = new SequenceI[sequences.length];
194 for (int s = 0; s < sequences.length; s++)
196 smsa[0][s] = sequences[s].getSeq(gapCharacter);
202 * return full msa and hidden regions with visible blocks replaced with new sub alignments
203 * @param nvismsa SequenceI[][]
204 * @param orders AlignmentOrder[] corresponding to each SequenceI[] block.
207 public Object[] getUpdatedView(SequenceI[][] nvismsa, AlignmentOrder[] orders, char gapCharacter) {
208 if (sequences == null || width <= 0)
210 throw new Error("empty view cannot be updated.");
214 "nvismsa==null. use getAlignmentAndColumnSelection() instead.");
215 if (contigs != null && contigs.length > 0)
217 SequenceI[] alignment = new SequenceI[sequences.length];
218 ColumnSelection columnselection = new ColumnSelection();
219 if (contigs != null && contigs.length > 0)
225 for (int contig = 0; contig < contigs.length; contig += 3)
227 owidth += contigs[contig + 2]; // recover final column width
228 if (contigs[contig + 1] - start > 0)
230 int swidth = 0; // subalignment width
231 if (nvismsa[j] != null)
233 SequenceI mseq[] = nvismsa[j];
234 AlignmentOrder order=(orders==null) ? null : orders[j];
236 if (mseq.length!=sequences.length)
237 throw new Error("Mismatch between number of sequences in block "+j+" ("+mseq.length+") and the original view ("+sequences.length+")");
238 swidth = mseq[0].getLength(); // JBPNote: could ensure padded here.
239 for (int s = 0; s < mseq.length; s++)
241 if (alignment[s] == null)
243 alignment[s] = mseq[s];
247 alignment[s].setSequence(alignment[s].getSequence() +
248 mseq[s].getSequence());
249 if (mseq[s].getStart() <= mseq[s].getEnd())
251 alignment[s].setEnd(mseq[s].getEnd());
254 order.updateSequence(mseq[s], alignment[s]);
261 // recover original alignment block or place gaps
264 // recover input data
265 for (int s = 0; s < sequences.length; s++)
267 SequenceI oseq = sequences[s].getSeq(gapCharacter).getSubSequence(start,
268 contigs[contig + 1]);
269 if (swidth < oseq.getLength())
271 swidth = oseq.getLength();
273 if (alignment[s] == null)
279 alignment[s].setSequence(alignment[s].getSequence() +
281 if (oseq.getEnd() >= oseq.getStart())
283 alignment[s].setEnd(oseq.getEnd());
293 // advance to begining of visible region
294 start = contigs[contig + 1] + contigs[contig + 2];
295 // add hidden segment to right of next region
296 for (int s = 0; s < sequences.length; s++)
298 SequenceI hseq = sequences[s].getSeq(gapCharacter).getSubSequence(contigs[contig +
300 if (alignment[s] == null)
306 alignment[s].setSequence(alignment[s].getSequence() +
308 if (hseq.getEnd() >= hseq.getStart())
310 alignment[s].setEnd(hseq.getEnd());
314 // mark hidden segment as hidden in the new alignment
315 columnselection.hideColumns(nwidth, nwidth + contigs[contig + 2] - 1);
316 nwidth += contigs[contig + 2];
318 // Do final segment - if it exists
319 if (j < nvismsa.length)
322 if (nvismsa[j] != null)
324 SequenceI mseq[] = nvismsa[j];
325 AlignmentOrder order = (orders!=null) ? orders[j] : null;
326 swidth = mseq[0].getLength();
327 for (int s = 0; s < mseq.length; s++)
329 if (alignment[s] == null)
331 alignment[s] = mseq[s];
335 alignment[s].setSequence(alignment[s].getSequence() +
336 mseq[s].getSequence());
337 if (mseq[s].getEnd() >= mseq[s].getStart())
339 alignment[s].setEnd(mseq[s].getEnd());
342 order.updateSequence(mseq[s], alignment[s]);
351 // recover input data or place gaps
354 // recover input data
355 for (int s = 0; s < sequences.length; s++)
357 SequenceI oseq = sequences[s].getSeq(gapCharacter).getSubSequence(start,
359 if (swidth < oseq.getLength())
361 swidth = oseq.getLength();
363 if (alignment[s] == null)
369 alignment[s].setSequence(alignment[s].getSequence() +
371 if (oseq.getEnd() >= oseq.getStart())
373 alignment[s].setEnd(oseq.getEnd());
382 throw new Error("Padding not yet implemented.");
388 return new Object[] { alignment, columnselection};
390 if (nvismsa.length!=1)
391 throw new Error("Mismatch between visible blocks to update and number of contigs in view (contigs=0,blocks="+nvismsa.length);
392 if (nvismsa[0]!=null)
393 return new Object[] { nvismsa[0], new ColumnSelection()};
395 return getAlignmentAndColumnSelection(gapCharacter);
399 * returns simple array of start end positions of visible range on alignment.
400 * vis_start and vis_end are inclusive - use SequenceI.getSubSequence(vis_start, vis_end+1) to recover visible sequence from underlying alignment.
401 * @return int[] { start_i, end_i } for 1<i<n visible regions.
403 public int[] getVisibleContigs() {
404 if (contigs != null && contigs.length > 0)
409 for (int contig = 0; contig < contigs.length; contig += 3)
411 if ( (contigs[contig + 1] - start) > 0)
415 fwidth += contigs[contig + 2]; // end up with full region width (including hidden regions)
416 start = contigs[contig + 1] + contigs[contig + 2];
422 int viscontigs[] = new int[nvis*2];
425 for (int contig=0; contig<contigs.length; contig+=3) {
426 if ( (contigs[contig + 1] - start) > 0)
428 viscontigs[nvis] = start;
429 viscontigs[nvis+1]=contigs[contig+1]-1; // end is inclusive
432 start = contigs[contig + 1] + contigs[contig + 2];
435 viscontigs[nvis] = start;
436 viscontigs[nvis+1]=fwidth; // end is inclusive
441 return new int[] { 0, width};
446 * @return position of first visible column of AlignmentView within its parent's alignment reference frame
448 public int getAlignmentOrigin() {
449 // TODO Auto-generated method stub