Formatting
[jalview.git] / src / jalview / datamodel / AlignmentView.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
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.
9  *
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.
14  *
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
18  */
19 package jalview.datamodel;
20
21 /**
22  * <p>Title: </p>
23  *
24  * <p>Description: </p>
25  *
26  * <p>Copyright: Copyright (c) 2004</p>
27  *
28  * <p>Company: Dundee University</p>
29  *
30  * @author not attributable
31  * @version 1.0
32  */
33 public class AlignmentView
34 {
35   /**
36    * Transient object compactly representing a 'view' of an alignment - with discontinuities marked.
37    */
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)
43   {
44     if (!seqcigararray.isSeqCigarArray())
45     {
46       throw new Error("Implementation Error - can only make an alignment view from a CigarArray of sequences.");
47     }
48     //contigs = seqcigararray.applyDeletions();
49     contigs = seqcigararray.getDeletedRegions();
50     sequences = seqcigararray.getSeqCigarArray();
51     width = seqcigararray.getWidth(); // visible width
52   }
53
54   /**
55    * Create an alignmentView where the first column corresponds with the 'firstcol' column of some reference alignment
56    * @param sdata
57    * @param firstcol
58    */
59   public AlignmentView(CigarArray sdata, int firstcol)
60   {
61     this(sdata);
62     firstCol = firstcol;
63   }
64
65   public void setSequences(SeqCigar[] sequences)
66   {
67     this.sequences = sequences;
68   }
69
70   public void setContigs(int[] contigs)
71   {
72     this.contigs = contigs;
73   }
74
75   public SeqCigar[] getSequences()
76   {
77     return sequences;
78   }
79
80   /**
81    * @see CigarArray.getDeletedRegions
82    * @return int[] { vis_start, sym_start, length }
83    */
84   public int[] getContigs()
85   {
86     return contigs;
87   }
88
89   /**
90    * get the full alignment and a columnselection object marking the hidden regions
91    * @param gapCharacter char
92    * @return Object[] { SequenceI[], ColumnSelection}
93    */
94   public Object[] getAlignmentAndColumnSelection(char gapCharacter)
95   {
96     ColumnSelection colsel = new ColumnSelection();
97
98     return new Object[]
99         {
100         SeqCigar.createAlignmentSequences(sequences, gapCharacter, colsel,
101                                           contigs), colsel};
102   }
103
104   /**
105    * getSequenceStrings
106    *
107    * @param c char
108    * @return String[]
109    */
110   public String[] getSequenceStrings(char c)
111   {
112     String[] seqs = new String[sequences.length];
113     for (int n = 0; n < sequences.length; n++)
114     {
115       String fullseq = sequences[n].getSequenceString(c);
116       if (contigs != null)
117       {
118         seqs[n] = "";
119         int p = 0;
120         for (int h = 0; h < contigs.length; h += 3)
121         {
122           seqs[n] += fullseq.substring(p, contigs[h + 1]);
123           p = contigs[h + 1] + contigs[h + 2];
124         }
125         seqs[n] += fullseq.substring(p);
126       }
127       else
128       {
129         seqs[n] = fullseq;
130       }
131     }
132     return seqs;
133   }
134
135   /**
136    *
137    * @return visible number of columns in alignment view
138    */
139   public int getWidth()
140   {
141     return width;
142   }
143
144   protected void setWidth(int width)
145   {
146     this.width = width;
147   }
148
149   /**
150    * get the contiguous subalignments in an alignment view.
151    * @param gapCharacter char
152    * @return SequenceI[][]
153    */
154   public SequenceI[][] getVisibleContigs(char gapCharacter)
155   {
156     SequenceI[][] smsa;
157     int njobs = 1;
158     if (sequences == null || width <= 0)
159     {
160       return null;
161     }
162     if (contigs != null && contigs.length > 0)
163     {
164       int start = 0;
165       njobs = 0;
166       int fwidth = width;
167       for (int contig = 0; contig < contigs.length; contig += 3)
168       {
169         if ( (contigs[contig + 1] - start) > 0)
170         {
171           njobs++;
172         }
173         fwidth += contigs[contig + 2]; // end up with full region width (including hidden regions)
174         start = contigs[contig + 1] + contigs[contig + 2];
175       }
176       if (start < fwidth)
177       {
178         njobs++;
179       }
180       smsa = new SequenceI[njobs][];
181       start = 0;
182       int j = 0;
183       for (int contig = 0; contig < contigs.length; contig += 3)
184       {
185         if (contigs[contig + 1] - start > 0)
186         {
187           SequenceI mseq[] = new SequenceI[sequences.length];
188           for (int s = 0; s < mseq.length; s++)
189           {
190             mseq[s] = sequences[s].getSeq(gapCharacter).getSubSequence(start,
191                 contigs[contig + 1]);
192           }
193           smsa[j] = mseq;
194           j++;
195         }
196         start = contigs[contig + 1] + contigs[contig + 2];
197       }
198       if (start < fwidth)
199       {
200         SequenceI mseq[] = new SequenceI[sequences.length];
201         for (int s = 0; s < mseq.length; s++)
202         {
203           mseq[s] = sequences[s].getSeq(gapCharacter).getSubSequence(start,
204               fwidth + 1);
205         }
206         smsa[j] = mseq;
207         j++;
208       }
209     }
210     else
211     {
212       smsa = new SequenceI[1][];
213       smsa[0] = new SequenceI[sequences.length];
214       for (int s = 0; s < sequences.length; s++)
215       {
216         smsa[0][s] = sequences[s].getSeq(gapCharacter);
217       }
218     }
219     return smsa;
220   }
221
222   /**
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.
226    * @return Object[]
227    */
228   public Object[] getUpdatedView(SequenceI[][] nvismsa, AlignmentOrder[] orders,
229                                  char gapCharacter)
230   {
231     if (sequences == null || width <= 0)
232     {
233       throw new Error("empty view cannot be updated.");
234     }
235     if (nvismsa == null)
236     {
237       throw new Error(
238           "nvismsa==null. use getAlignmentAndColumnSelection() instead.");
239     }
240     if (contigs != null && contigs.length > 0)
241     {
242       SequenceI[] alignment = new SequenceI[sequences.length];
243       ColumnSelection columnselection = new ColumnSelection();
244       if (contigs != null && contigs.length > 0)
245       {
246         int start = 0;
247         int nwidth = 0;
248         int owidth = width;
249         int j = 0;
250         for (int contig = 0; contig < contigs.length; contig += 3)
251         {
252           owidth += contigs[contig + 2]; // recover final column width
253           if (contigs[contig + 1] - start > 0)
254           {
255             int swidth = 0; // subalignment width
256             if (nvismsa[j] != null)
257             {
258               SequenceI mseq[] = nvismsa[j];
259               AlignmentOrder order = (orders == null) ? null : orders[j];
260               j++;
261               if (mseq.length != sequences.length)
262               {
263                 throw new Error(
264                     "Mismatch between number of sequences in block " + j + " (" +
265                     mseq.length + ") and the original view (" +
266                     sequences.length + ")");
267               }
268               swidth = mseq[0].getLength(); // JBPNote: could ensure padded here.
269               for (int s = 0; s < mseq.length; s++)
270               {
271                 if (alignment[s] == null)
272                 {
273                   alignment[s] = mseq[s];
274                 }
275                 else
276                 {
277                   alignment[s].setSequence(alignment[s].getSequenceAsString() +
278                                            mseq[s].getSequenceAsString());
279                   if (mseq[s].getStart() <= mseq[s].getEnd())
280                   {
281                     alignment[s].setEnd(mseq[s].getEnd());
282                   }
283                   if (order != null)
284                   {
285                     order.updateSequence(mseq[s], alignment[s]);
286                   }
287                 }
288               }
289             }
290             else
291             {
292               // recover original alignment block or place gaps
293               if (true)
294               {
295                 // recover input data
296                 for (int s = 0; s < sequences.length; s++)
297                 {
298                   SequenceI oseq = sequences[s].getSeq(gapCharacter).
299                       getSubSequence(start,
300                                      contigs[contig + 1]);
301                   if (swidth < oseq.getLength())
302                   {
303                     swidth = oseq.getLength();
304                   }
305                   if (alignment[s] == null)
306                   {
307                     alignment[s] = oseq;
308                   }
309                   else
310                   {
311                     alignment[s].setSequence(alignment[s].getSequenceAsString() +
312                                              oseq.getSequenceAsString());
313                     if (oseq.getEnd() >= oseq.getStart())
314                     {
315                       alignment[s].setEnd(oseq.getEnd());
316                     }
317                   }
318                 }
319
320               }
321               j++;
322             }
323             nwidth += swidth;
324           }
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++)
329           {
330             SequenceI hseq = sequences[s].getSeq(gapCharacter).getSubSequence(
331                 contigs[contig +
332                 1], start);
333             if (alignment[s] == null)
334             {
335               alignment[s] = hseq;
336             }
337             else
338             {
339               alignment[s].setSequence(alignment[s].getSequenceAsString() +
340                                        hseq.getSequenceAsString());
341               if (hseq.getEnd() >= hseq.getStart())
342               {
343                 alignment[s].setEnd(hseq.getEnd());
344               }
345             }
346           }
347           // mark hidden segment as hidden in the new alignment
348           columnselection.hideColumns(nwidth, nwidth + contigs[contig + 2] - 1);
349           nwidth += contigs[contig + 2];
350         }
351         // Do final segment - if it exists
352         if (j < nvismsa.length)
353         {
354           int swidth = 0;
355           if (nvismsa[j] != null)
356           {
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++)
361             {
362               if (alignment[s] == null)
363               {
364                 alignment[s] = mseq[s];
365               }
366               else
367               {
368                 alignment[s].setSequence(alignment[s].getSequenceAsString() +
369                                          mseq[s].getSequenceAsString());
370                 if (mseq[s].getEnd() >= mseq[s].getStart())
371                 {
372                   alignment[s].setEnd(mseq[s].getEnd());
373                 }
374                 if (order != null)
375                 {
376                   order.updateSequence(mseq[s], alignment[s]);
377                 }
378               }
379             }
380           }
381           else
382           {
383             if (start < owidth)
384             {
385               // recover input data or place gaps
386               if (true)
387               {
388                 // recover input data
389                 for (int s = 0; s < sequences.length; s++)
390                 {
391                   SequenceI oseq = sequences[s].getSeq(gapCharacter).
392                       getSubSequence(start,
393                                      owidth + 1);
394                   if (swidth < oseq.getLength())
395                   {
396                     swidth = oseq.getLength();
397                   }
398                   if (alignment[s] == null)
399                   {
400                     alignment[s] = oseq;
401                   }
402                   else
403                   {
404                     alignment[s].setSequence(alignment[s].getSequenceAsString() +
405                                              oseq.getSequenceAsString());
406                     if (oseq.getEnd() >= oseq.getStart())
407                     {
408                       alignment[s].setEnd(oseq.getEnd());
409                     }
410                   }
411                 }
412                 nwidth += swidth;
413               }
414               else
415               {
416                 // place gaps.
417                 throw new Error("Padding not yet implemented.");
418               }
419             }
420           }
421         }
422       }
423       return new Object[]
424           {
425           alignment, columnselection};
426     }
427     else
428     {
429       if (nvismsa.length != 1)
430       {
431         throw new Error("Mismatch between visible blocks to update and number of contigs in view (contigs=0,blocks=" +
432                         nvismsa.length);
433       }
434       if (nvismsa[0] != null)
435       {
436         return new Object[]
437             {
438             nvismsa[0], new ColumnSelection()};
439       }
440       else
441       {
442         return getAlignmentAndColumnSelection(gapCharacter);
443       }
444     }
445   }
446
447   /**
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.
451    */
452   public int[] getVisibleContigs()
453   {
454     if (contigs != null && contigs.length > 0)
455     {
456       int start = 0;
457       int nvis = 0;
458       int fwidth = width;
459       for (int contig = 0; contig < contigs.length; contig += 3)
460       {
461         if ( (contigs[contig + 1] - start) > 0)
462         {
463           nvis++;
464         }
465         fwidth += contigs[contig + 2]; // end up with full region width (including hidden regions)
466         start = contigs[contig + 1] + contigs[contig + 2];
467       }
468       if (start < fwidth)
469       {
470         nvis++;
471       }
472       int viscontigs[] = new int[nvis * 2];
473       nvis = 0;
474       start = 0;
475       for (int contig = 0; contig < contigs.length; contig += 3)
476       {
477         if ( (contigs[contig + 1] - start) > 0)
478         {
479           viscontigs[nvis] = start;
480           viscontigs[nvis + 1] = contigs[contig + 1] - 1; // end is inclusive
481           nvis += 2;
482         }
483         start = contigs[contig + 1] + contigs[contig + 2];
484       }
485       if (start < fwidth)
486       {
487         viscontigs[nvis] = start;
488         viscontigs[nvis + 1] = fwidth; // end is inclusive
489         nvis += 2;
490       }
491       return viscontigs;
492     }
493     else
494     {
495       return new int[]
496           {
497           0, width};
498     }
499   }
500
501   /**
502    *
503    * @return position of first visible column of AlignmentView within its parent's alignment reference frame
504    */
505   public int getAlignmentOrigin()
506   {
507     // TODO Auto-generated method stub
508     return firstCol;
509   }
510 }