1 package jalview.datamodel;
10 * <p>Copyright: Copyright (c) 2004</p>
12 * <p>Company: Dundee University</p>
14 * @author not attributable
17 public class AlignmentView
20 * Transient object compactly representing a 'view' of an alignment - with discontinuities marked.
22 private SeqCigar[] sequences = null;
23 private int[] contigs = null;
25 public AlignmentView(CigarArray seqcigararray)
27 if (!seqcigararray.isSeqCigarArray())
28 throw new Error("Implementation Error - can only make an alignment view from a CigarArray of sequences.");
29 //contigs = seqcigararray.applyDeletions();
30 contigs = seqcigararray.getDeletedRegions();
31 sequences = seqcigararray.getSeqCigarArray();
32 width = seqcigararray.getWidth(); // visible width
35 public void setSequences(SeqCigar[] sequences)
37 this.sequences = sequences;
40 public void setContigs(int[] contigs)
42 this.contigs = contigs;
45 public SeqCigar[] getSequences()
50 public int[] getContigs()
55 * get the full alignment and a columnselection object marking the hidden regions
56 * @param gapCharacter char
57 * @return Object[] { SequenceI[], ColumnSelection}
59 public Object[] getAlignmentAndColumnSelection(char gapCharacter) {
60 ColumnSelection colsel = new ColumnSelection();
62 return new Object[] { SeqCigar.createAlignmentSequences(sequences, gapCharacter, colsel, contigs), colsel};
70 public String[] getSequenceStrings(char c)
72 String[] seqs=new String[sequences.length];
73 for (int n=0; n<sequences.length; n++) {
74 seqs[n] = sequences[n].getSequenceString(c);
80 * @return visible number of columns in alignment view
82 public int getWidth() {
86 protected void setWidth(int width) {
90 * get the contiguous subalignments in an alignment view.
91 * @param gapCharacter char
92 * @return SequenceI[][]
94 public SequenceI[][] getVisibleContigs(char gapCharacter) {
97 if (sequences==null || width<=0)
99 if (contigs != null && contigs.length > 0)
104 for (int contig = 0; contig < contigs.length; contig += 3)
106 if ( (contigs[contig + 1] - start) > 0)
110 fwidth += contigs[contig + 2]; // end up with full region width (including hidden regions)
111 start = contigs[contig + 1] + contigs[contig + 2];
117 smsa = new SequenceI[njobs][];
120 for (int contig = 0; contig < contigs.length; contig += 3)
122 if (contigs[contig + 1] - start > 0)
124 SequenceI mseq[] = new SequenceI[sequences.length];
125 for (int s = 0; s < mseq.length; s++)
127 mseq[s] = sequences[s].getSeq(gapCharacter).getSubSequence(start,
128 contigs[contig + 1]);
133 start = contigs[contig + 1] + contigs[contig + 2];
137 SequenceI mseq[] = new SequenceI[sequences.length];
138 for (int s = 0; s < mseq.length; s++)
140 mseq[s] = sequences[s].getSeq(gapCharacter).getSubSequence(start,
149 smsa = new SequenceI[1][];
150 smsa[0] = new SequenceI[sequences.length];
151 for (int s = 0; s < sequences.length; s++)
153 smsa[0][s] = sequences[s].getSeq(gapCharacter);
159 * return full msa and hidden regions with visible blocks replaced with new sub alignments
160 * @param nvismsa SequenceI[][]
161 * @param orders AlignmentOrder[] corresponding to each SequenceI[] block.
164 public Object[] getUpdatedView(SequenceI[][] nvismsa, AlignmentOrder[] orders, char gapCharacter) {
165 if (sequences == null || width <= 0)
167 throw new Error("empty view cannot be updated.");
171 "nvismsa==null. use getAlignmentAndColumnSelection() instead.");
172 if (contigs != null && contigs.length > 0)
174 SequenceI[] alignment = new SequenceI[sequences.length];
175 ColumnSelection columnselection = new ColumnSelection();
176 if (contigs != null && contigs.length > 0)
182 for (int contig = 0; contig < contigs.length; contig += 3)
184 owidth += contigs[contig + 2]; // recover final column width
185 if (contigs[contig + 1] - start > 0)
187 int swidth = 0; // subalignment width
188 if (nvismsa[j] != null)
190 SequenceI mseq[] = nvismsa[j];
191 AlignmentOrder order=(orders==null) ? null : orders[j];
193 if (mseq.length!=sequences.length)
194 throw new Error("Mismatch between number of sequences in block "+j+" ("+mseq.length+") and the original view ("+sequences.length+")");
195 swidth = mseq[0].getLength(); // JBPNote: could ensure padded here.
196 for (int s = 0; s < mseq.length; s++)
198 if (alignment[s] == null)
200 alignment[s] = mseq[s];
204 alignment[s].setSequence(alignment[s].getSequence() +
205 mseq[s].getSequence());
206 if (mseq[s].getStart() <= mseq[s].getEnd())
208 alignment[s].setEnd(mseq[s].getEnd());
211 order.updateSequence(mseq[s], alignment[s]);
218 // recover original alignment block or place gaps
221 // recover input data
222 for (int s = 0; s < sequences.length; s++)
224 SequenceI oseq = sequences[s].getSeq(gapCharacter).getSubSequence(start,
225 contigs[contig + 1]);
226 if (swidth < oseq.getLength())
228 swidth = oseq.getLength();
230 if (alignment[s] == null)
236 alignment[s].setSequence(alignment[s].getSequence() +
238 if (oseq.getEnd() >= oseq.getStart())
240 alignment[s].setEnd(oseq.getEnd());
250 // advance to begining of visible region
251 start = contigs[contig + 1] + contigs[contig + 2];
252 // add hidden segment to right of next region
253 for (int s = 0; s < sequences.length; s++)
255 SequenceI hseq = sequences[s].getSeq(gapCharacter).getSubSequence(contigs[contig +
257 if (alignment[s] == null)
263 alignment[s].setSequence(alignment[s].getSequence() +
265 if (hseq.getEnd() >= hseq.getStart())
267 alignment[s].setEnd(hseq.getEnd());
271 // mark hidden segment as hidden in the new alignment
272 columnselection.hideColumns(nwidth, nwidth + contigs[contig + 2] - 1);
273 nwidth += contigs[contig + 2];
275 // Do final segment - if it exists
276 if (j < nvismsa.length)
279 if (nvismsa[j] != null)
281 SequenceI mseq[] = nvismsa[j];
282 AlignmentOrder order = (orders!=null) ? orders[j] : null;
283 swidth = mseq[0].getLength();
284 for (int s = 0; s < mseq.length; s++)
286 if (alignment[s] == null)
288 alignment[s] = mseq[s];
292 alignment[s].setSequence(alignment[s].getSequence() +
293 mseq[s].getSequence());
294 if (mseq[s].getEnd() >= mseq[s].getStart())
296 alignment[s].setEnd(mseq[s].getEnd());
299 order.updateSequence(mseq[s], alignment[s]);
308 // recover input data or place gaps
311 // recover input data
312 for (int s = 0; s < sequences.length; s++)
314 SequenceI oseq = sequences[s].getSeq(gapCharacter).getSubSequence(start,
316 if (swidth < oseq.getLength())
318 swidth = oseq.getLength();
320 if (alignment[s] == null)
326 alignment[s].setSequence(alignment[s].getSequence() +
328 if (oseq.getEnd() >= oseq.getStart())
330 alignment[s].setEnd(oseq.getEnd());
339 throw new Error("Padding not yet implemented.");
345 return new Object[] { alignment, columnselection};
347 if (nvismsa.length!=1)
348 throw new Error("Mismatch between visible blocks to update and number of contigs in view (contigs=0,blocks="+nvismsa.length);
349 if (nvismsa[0]!=null)
350 return new Object[] { nvismsa[0], new ColumnSelection()};
352 return getAlignmentAndColumnSelection(gapCharacter);