8441609643c31a29453ecbd1054d105f291aef13
[jalview.git] / src / jalview / datamodel / SeqCigar.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 import java.util.Enumeration;
24 import java.util.Hashtable;
25
26 import jalview.analysis.*;
27 import jalview.util.*;
28
29 public class SeqCigar extends CigarSimple
30 {
31   /**
32    * start(inclusive) and end(exclusive) of subsequence on refseq
33    */
34   private int start, end;
35
36   private SequenceI refseq = null;
37
38   private Hashtable seqProps;
39
40   /**
41    * Reference dataset sequence for the cigar string
42    * 
43    * @return SequenceI
44    */
45   public SequenceI getRefSeq()
46   {
47     return refseq;
48   }
49
50   /**
51    * 
52    * @return int start index of cigar ops on refSeq
53    */
54   public int getStart()
55   {
56     return start;
57   }
58
59   /**
60    * 
61    * @return int end index (exclusive) of cigar ops on refSeq
62    */
63   public int getEnd()
64   {
65     return end;
66   }
67
68   /**
69    * Returns sequence as a string with cigar operations applied to it
70    * 
71    * @return String
72    */
73   public String getSequenceString(char GapChar)
74   {
75     return (length == 0) ? "" : (String) getSequenceAndDeletions(
76             refseq.getSequenceAsString(start, end), GapChar)[0];
77   }
78
79   /**
80    * recreates a gapped and edited version of RefSeq or null for an empty cigar
81    * string
82    * 
83    * @return SequenceI
84    */
85   public SequenceI getSeq(char GapChar)
86   {
87     Sequence seq;
88     if (refseq == null || length == 0)
89     {
90       return null;
91     }
92     Object[] edit_result = getSequenceAndDeletions(
93             refseq.getSequenceAsString(start, end), GapChar);
94     if (edit_result == null)
95     {
96       throw new Error(MessageManager.getString("error.implementation_error_unexpected_null_from_get_sequence_and_deletions"));
97     }
98     int bounds[] = (int[]) edit_result[1];
99     seq = new Sequence(refseq.getName(), (String) edit_result[0],
100             refseq.getStart() + start + bounds[0], refseq.getStart()
101                     + start + ((bounds[2] == 0) ? -1 : bounds[2]));
102     seq.setDescription(refseq.getDescription());
103     int sstart = seq.getStart(), send = seq.getEnd();
104     // seq.checkValidRange(); probably not needed
105     // recover local properties if present
106     if (seqProps != null)
107     {
108       // this recovers dataset sequence reference as well as local features,
109       // names, start/end settings.
110       SeqsetUtils.SeqCharacterUnhash(seq, seqProps);
111     }
112     // ensure dataset sequence is up to date from local reference
113     seq.setDatasetSequence(refseq);
114     seq.setStart(sstart);
115     seq.setEnd(send);
116     return seq;
117   }
118
119   /*
120    * We don't allow this - refseq is given at construction time only public void
121    * setSeq(SequenceI seq) { this.seq = seq; }
122    */
123   /**
124    * internal constructor - sets seq to a gapless sequence derived from seq and
125    * prepends any 'D' operations needed to get to the first residue of seq.
126    * 
127    * @param seq
128    *          SequenceI
129    * @param initialDeletion
130    *          true to mark initial dataset sequence residues as deleted in
131    *          subsequence
132    * @param _s
133    *          index of first position in seq
134    * @param _e
135    *          index after last position in (possibly gapped) seq
136    * @return true if gaps are present in seq
137    */
138   private boolean _setSeq(SequenceI seq, boolean initialDeletion, int _s,
139           int _e)
140   {
141     boolean hasgaps = false;
142     if (seq == null)
143     {
144       throw new Error(MessageManager.getString("error.implementation_error_set_seq_null"));
145     }
146     if (_s < 0)
147     {
148       throw new Error(MessageManager.formatMessage("error.implementation_error_s", new String[]{Integer.valueOf(_s).toString()}));
149     }
150     String seq_string = seq.getSequenceAsString();
151     if (_e == 0 || _e < _s || _e > seq_string.length())
152     {
153       _e = seq_string.length();
154     }
155     // resolve start and end positions relative to ungapped reference sequence
156     start = seq.findPosition(_s) - seq.getStart();
157     end = seq.findPosition(_e) - seq.getStart();
158     int l_ungapped = end - start;
159     // Find correct sequence to reference and correct start and end - if
160     // necessary
161     SequenceI ds = seq.getDatasetSequence();
162     if (ds == null)
163     {
164       // make a new dataset sequence
165       String ungapped = AlignSeq.extractGaps(
166               jalview.util.Comparison.GapChars, new String(seq_string));
167       l_ungapped = ungapped.length();
168       // check that we haven't just duplicated an ungapped sequence.
169       if (l_ungapped == seq.getLength())
170       {
171         ds = seq;
172       }
173       else
174       {
175         ds = new Sequence(seq.getName(), ungapped, seq.getStart(),
176                 seq.getStart() + ungapped.length() - 1);
177         // JBPNote: this would be consistent but may not be useful
178         // seq.setDatasetSequence(ds);
179       }
180     }
181     // add in offset between seq and the dataset sequence
182     if (ds.getStart() < seq.getStart())
183     {
184       int offset = seq.getStart() - ds.getStart();
185       if (initialDeletion)
186       {
187         // absolute cigar string
188         addDeleted(_s + offset);
189         start = 0;
190         end += offset;
191       }
192       else
193       {
194         // normal behaviour - just mark start and end subsequence
195         start += offset;
196         end += offset;
197
198       }
199
200     }
201
202     // any gaps to process ?
203     if (l_ungapped != (_e - _s))
204     {
205       hasgaps = true;
206     }
207
208     refseq = ds;
209     // copy over local properties for the sequence instance of the refseq
210     seqProps = SeqsetUtils.SeqCharacterHash(seq);
211     // Check offsets
212     if (end > ds.getLength())
213     {
214       throw new Error(MessageManager.getString("error.implementation_error_seqcigar_possible"));
215       // end = ds.getLength();
216     }
217
218     return hasgaps;
219   }
220
221   /**
222    * directly initialise a cigar object with a sequence of range, operation
223    * pairs and a sequence to apply it to. operation and range should be relative
224    * to the seq.getStart()'th residue of the dataset seq resolved from seq.
225    * 
226    * @param seq
227    *          SequenceI
228    * @param operation
229    *          char[]
230    * @param range
231    *          int[]
232    */
233   public SeqCigar(SequenceI seq, char operation[], int range[])
234   {
235     super();
236     if (seq == null)
237     {
238       throw new Error(MessageManager.getString("error.implmentation_bug_seq_null"));
239     }
240     if (operation.length != range.length)
241     {
242       throw new Error(MessageManager.getString("error.implementation_bug_cigar_operation_list_range_list"));
243     }
244
245     if (operation != null)
246     {
247       this.operation = new char[operation.length + _inc_length];
248       this.range = new int[operation.length + _inc_length];
249
250       if (_setSeq(seq, false, 0, 0))
251       {
252         throw new Error(MessageManager.getString("error.not_yet_implemented_cigar_object_from_cigar_string"));
253       }
254       for (int i = this.length, j = 0; j < operation.length; i++, j++)
255       {
256         char op = operation[j];
257         if (op != M && op != I && op != D)
258         {
259           throw new Error(MessageManager.formatMessage("error.implementation_bug_cigar_operation", new String[]{Integer.valueOf(j).toString(),Integer.valueOf(op).toString(),Integer.valueOf(M).toString(),Integer.valueOf(I).toString(),Integer.valueOf(D).toString()}));
260         }
261         this.operation[i] = op;
262         this.range[i] = range[j];
263       }
264       this.length += operation.length;
265     }
266     else
267     {
268       this.operation = null;
269       this.range = null;
270       this.length = 0;
271       if (_setSeq(seq, false, 0, 0))
272       {
273         throw new Error(MessageManager.getString("error.not_yet_implemented_cigar_object_from_cigar_string"));
274       }
275     }
276   }
277
278   /**
279    * add range matched residues to cigar string
280    * 
281    * @param range
282    *          int
283    */
284   public void addMatch(int range)
285   {
286     this.addOperation(M, range);
287   }
288
289   /**
290    * Adds insertion and match operations based on seq to the cigar up to the
291    * endpos column of seq.
292    * 
293    * @param cigar
294    *          CigarBase
295    * @param seq
296    *          SequenceI
297    * @param startpos
298    *          int
299    * @param endpos
300    *          int
301    * @param initialDeletions
302    *          if true then initial deletions will be added from start of seq to
303    *          startpos
304    */
305   protected static void addSequenceOps(CigarBase cigar, SequenceI seq,
306           int startpos, int endpos, boolean initialDeletions)
307   {
308     char op = '\0';
309     int range = 0;
310     int p = 0, res = seq.getLength();
311
312     if (!initialDeletions)
313     {
314       p = startpos;
315     }
316
317     while (p <= endpos)
318     {
319       boolean isGap = (p < res) ? jalview.util.Comparison.isGap(seq
320               .getCharAt(p)) : true;
321       if ((startpos <= p) && (p <= endpos))
322       {
323         if (isGap)
324         {
325           if (range > 0 && op != I)
326           {
327             cigar.addOperation(op, range);
328             range = 0;
329           }
330           op = I;
331           range++;
332         }
333         else
334         {
335           if (range > 0 && op != M)
336           {
337             cigar.addOperation(op, range);
338             range = 0;
339           }
340           op = M;
341           range++;
342         }
343       }
344       else
345       {
346         if (!isGap)
347         {
348           if (range > 0 && op != D)
349           {
350             cigar.addOperation(op, range);
351             range = 0;
352           }
353           op = D;
354           range++;
355         }
356         else
357         {
358           // do nothing - insertions are not made in flanking regions
359         }
360       }
361       p++;
362     }
363     if (range > 0)
364     {
365       cigar.addOperation(op, range);
366     }
367   }
368
369   /**
370    * create a cigar string for given sequence
371    * 
372    * @param seq
373    *          SequenceI
374    */
375   public SeqCigar(SequenceI seq)
376   {
377     super();
378     if (seq == null)
379     {
380       throw new Error(MessageManager.getString("error.implementation_error_for_new_cigar"));
381     }
382     _setSeq(seq, false, 0, 0);
383     // there is still work to do
384     addSequenceOps(this, seq, 0, seq.getLength() - 1, false);
385   }
386
387   /**
388    * Create Cigar from a range of gaps and residues on a sequence object
389    * 
390    * @param seq
391    *          SequenceI
392    * @param start
393    *          int - first column in range
394    * @param end
395    *          int - last column in range
396    */
397   public SeqCigar(SequenceI seq, int start, int end)
398   {
399     super();
400     if (seq == null)
401     {
402       throw new Error(MessageManager.getString("error.implementation_error_for_new_cigar"));
403     }
404     _setSeq(seq, false, start, end + 1);
405     // there is still work to do
406     addSequenceOps(this, seq, start, end, false);
407   }
408
409   /**
410    * Create a cigar object from a cigar string like '[<I|D|M><range>]+' Will
411    * fail if the given seq already contains gaps (JBPNote: future implementation
412    * will fix)
413    * 
414    * @param seq
415    *          SequenceI object resolvable to a dataset sequence
416    * @param cigarString
417    *          String
418    * @return Cigar
419    */
420   public static SeqCigar parseCigar(SequenceI seq, String cigarString)
421           throws Exception
422   {
423     Object[] opsandrange = parseCigarString(cigarString);
424     return new SeqCigar(seq, (char[]) opsandrange[0],
425             (int[]) opsandrange[1]);
426   }
427
428   /**
429    * create an alignment from the given array of cigar sequences and gap
430    * character, and marking the given segments as visible in the given
431    * columselection.
432    * 
433    * @param alseqs
434    * @param gapCharacter
435    * @param colsel
436    *          - columnSelection where hidden regions are marked
437    * @param segments
438    *          - visible regions of alignment
439    * @return SequenceI[]
440    */
441   public static SequenceI[] createAlignmentSequences(SeqCigar[] alseqs,
442           char gapCharacter, ColumnSelection colsel, int[] segments)
443   {
444     SequenceI[] seqs = new SequenceI[alseqs.length];
445     StringBuffer[] g_seqs = new StringBuffer[alseqs.length];
446     String[] alseqs_string = new String[alseqs.length];
447     Object[] gs_regions = new Object[alseqs.length];
448     for (int i = 0; i < alseqs.length; i++)
449     {
450       alseqs_string[i] = alseqs[i].getRefSeq().getSequenceAsString(
451               alseqs[i].start, alseqs[i].end);
452       gs_regions[i] = alseqs[i].getSequenceAndDeletions(alseqs_string[i],
453               gapCharacter); // gapped sequence, {start, start col, end.
454       // endcol}, hidden regions {{start, end, col}})
455       if (gs_regions[i] == null)
456       {
457         throw new Error(MessageManager.formatMessage("error.implementation_error_cigar_seq_no_operations", new String[]{Integer.valueOf(i).toString()}));
458       }
459       g_seqs[i] = new StringBuffer((String) ((Object[]) gs_regions[i])[0]); // the
460       // visible
461       // gapped
462       // sequence
463     }
464     // Now account for insertions. (well - deletions)
465     // this is complicated because we must keep track of shifted positions in
466     // each sequence
467     ShiftList shifts = new ShiftList();
468     for (int i = 0; i < alseqs.length; i++)
469     {
470       Object[] gs_region = ((Object[]) ((Object[]) gs_regions[i])[2]);
471       if (gs_region != null)
472
473       {
474         for (int hr = 0; hr < gs_region.length; hr++)
475         {
476           int[] region = (int[]) gs_region[hr];
477           char[] insert = new char[region[1] - region[0] + 1];
478           for (int s = 0; s < insert.length; s++)
479           {
480             insert[s] = gapCharacter;
481           }
482           int inspos = shifts.shift(region[2]); // resolve insertion position in
483           // current alignment frame of
484           // reference
485           for (int s = 0; s < alseqs.length; s++)
486           {
487             if (s != i)
488             {
489               if (g_seqs[s].length() <= inspos)
490               {
491                 // prefix insertion with more gaps.
492                 for (int l = inspos - g_seqs[s].length(); l > 0; l--)
493                 {
494                   g_seqs[s].append(gapCharacter); // to debug - use a diffferent
495                   // gap character here
496                 }
497               }
498               g_seqs[s].insert(inspos, insert);
499             }
500             else
501             {
502               g_seqs[s].insert(inspos,
503                       alseqs_string[i].substring(region[0], region[1] + 1));
504             }
505           }
506           shifts.addShift(region[2], insert.length); // update shift in
507           // alignment frame of
508           // reference
509           if (segments == null)
510           {
511             // add a hidden column for this deletion
512             colsel.hideColumns(inspos, inspos + insert.length - 1);
513           }
514         }
515       }
516     }
517     for (int i = 0; i < alseqs.length; i++)
518     {
519       int[] bounds = ((int[]) ((Object[]) gs_regions[i])[1]);
520       SequenceI ref = alseqs[i].getRefSeq();
521       seqs[i] = new Sequence(ref.getName(), g_seqs[i].toString(),
522               ref.getStart() + alseqs[i].start + bounds[0], ref.getStart()
523                       + alseqs[i].start + (bounds[2] == 0 ? -1 : bounds[2]));
524       seqs[i].setDatasetSequence(ref);
525       seqs[i].setDescription(ref.getDescription());
526     }
527     if (segments != null)
528     {
529       for (int i = 0; i < segments.length; i += 3)
530       {
531         // int start=shifts.shift(segments[i]-1)+1;
532         // int end=shifts.shift(segments[i]+segments[i+1]-1)-1;
533         colsel.hideColumns(segments[i + 1], segments[i + 1]
534                 + segments[i + 2] - 1);
535       }
536     }
537     return seqs;
538   }
539
540   /**
541    * non rigorous testing
542    */
543   /**
544    * 
545    * @param seq
546    *          Sequence
547    * @param ex_cs_gapped
548    *          String
549    * @return String
550    */
551   public static String testCigar_string(Sequence seq, String ex_cs_gapped)
552   {
553     SeqCigar c_sgapped = new SeqCigar(seq);
554     String cs_gapped = c_sgapped.getCigarstring();
555     if (!cs_gapped.equals(ex_cs_gapped))
556     {
557       System.err.println("Failed getCigarstring: incorect string '"
558               + cs_gapped + "' != " + ex_cs_gapped);
559     }
560     return cs_gapped;
561   }
562
563   public static boolean testSeqRecovery(SeqCigar gen_sgapped,
564           SequenceI s_gapped)
565   {
566     // this is non-rigorous - start and end recovery is not tested.
567     SequenceI gen_sgapped_s = gen_sgapped.getSeq('-');
568     if (!gen_sgapped_s.getSequence().equals(s_gapped.getSequence()))
569     {
570       System.err.println("Couldn't reconstruct sequence.\n"
571               + gen_sgapped_s.getSequenceAsString() + "\n"
572               + s_gapped.getSequenceAsString());
573       return false;
574     }
575     return true;
576   }
577
578   public static void main(String argv[]) throws Exception
579   {
580     String o_seq;
581     Sequence s = new Sequence("MySeq",
582             o_seq = "asdfktryasdtqwrtsaslldddptyipqqwaslchvhttt", 39, 80);
583     String orig_gapped;
584     Sequence s_gapped = new Sequence(
585             "MySeq",
586             orig_gapped = "----asdf------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhttt",
587             39, 80);
588     String ex_cs_gapped = "4I4M6I6M3I11M4I12M4I9M";
589     s_gapped.setDatasetSequence(s);
590     String sub_gapped_s;
591     Sequence s_subsequence_gapped = new Sequence(
592             "MySeq",
593             sub_gapped_s = "------ktryas---dtqwrtsasll----dddptyipqqwa----slchvh",
594             43, 77);
595
596     s_subsequence_gapped.setDatasetSequence(s);
597     SeqCigar c_null = new SeqCigar(s);
598     String cs_null = c_null.getCigarstring();
599     if (!cs_null.equals("42M"))
600     {
601       System.err
602               .println("Failed to recover ungapped sequence cigar operations:"
603                       + ((cs_null == "") ? "empty string" : cs_null));
604     }
605     testCigar_string(s_gapped, ex_cs_gapped);
606     SeqCigar gen_sgapped = SeqCigar.parseCigar(s, ex_cs_gapped);
607     if (!gen_sgapped.getCigarstring().equals(ex_cs_gapped))
608     {
609       System.err.println("Failed parseCigar(" + ex_cs_gapped
610               + ")->getCigarString()->'" + gen_sgapped.getCigarstring()
611               + "'");
612     }
613     testSeqRecovery(gen_sgapped, s_gapped);
614     // Test dataset resolution
615     SeqCigar sub_gapped = new SeqCigar(s_subsequence_gapped);
616     if (!testSeqRecovery(sub_gapped, s_subsequence_gapped))
617     {
618       System.err
619               .println("Failed recovery for subsequence of dataset sequence");
620     }
621     // width functions
622     if (sub_gapped.getWidth() != sub_gapped_s.length())
623     {
624       System.err.println("Failed getWidth()");
625     }
626
627     sub_gapped.getFullWidth();
628     if (sub_gapped.hasDeletedRegions())
629     {
630       System.err.println("hasDeletedRegions is incorrect.");
631     }
632     // Test start-end region SeqCigar
633     SeqCigar sub_se_gp = new SeqCigar(s_subsequence_gapped, 8, 48);
634     if (sub_se_gp.getWidth() != 41)
635     {
636       System.err
637               .println("SeqCigar(seq, start, end) not properly clipped alignsequence.");
638     }
639     System.out.println("Original sequence align:\n" + sub_gapped_s
640             + "\nReconstructed window from 8 to 48\n" + "XXXXXXXX"
641             + sub_se_gp.getSequenceString('-') + "..." + "\nCigar String:"
642             + sub_se_gp.getCigarstring() + "\n");
643     SequenceI ssgp = sub_se_gp.getSeq('-');
644     System.out.println("\t " + ssgp.getSequenceAsString());
645     for (int r = 0; r < 10; r++)
646     {
647       sub_se_gp = new SeqCigar(s_subsequence_gapped, 8, 48);
648       int sl = sub_se_gp.getWidth();
649       int st = sl - 1 - r;
650       for (int rs = 0; rs < 10; rs++)
651       {
652         int e = st + rs;
653         sub_se_gp.deleteRange(st, e);
654         String ssgapedseq = sub_se_gp.getSeq('-').getSequenceAsString();
655         System.out.println(st + "," + e + "\t:" + ssgapedseq);
656         st -= 3;
657       }
658     }
659     {
660       SeqCigar[] set = new SeqCigar[]
661       { new SeqCigar(s), new SeqCigar(s_subsequence_gapped, 8, 48),
662           new SeqCigar(s_gapped) };
663       Alignment al = new Alignment(set);
664       for (int i = 0; i < al.getHeight(); i++)
665       {
666         System.out.println("" + al.getSequenceAt(i).getName() + "\t"
667                 + al.getSequenceAt(i).getStart() + "\t"
668                 + al.getSequenceAt(i).getEnd() + "\t"
669                 + al.getSequenceAt(i).getSequenceAsString());
670       }
671     }
672     {
673       System.out.println("Gapped.");
674       SeqCigar[] set = new SeqCigar[]
675       { new SeqCigar(s), new SeqCigar(s_subsequence_gapped, 8, 48),
676           new SeqCigar(s_gapped) };
677       set[0].deleteRange(20, 25);
678       Alignment al = new Alignment(set);
679       for (int i = 0; i < al.getHeight(); i++)
680       {
681         System.out.println("" + al.getSequenceAt(i).getName() + "\t"
682                 + al.getSequenceAt(i).getStart() + "\t"
683                 + al.getSequenceAt(i).getEnd() + "\t"
684                 + al.getSequenceAt(i).getSequenceAsString());
685       }
686     }
687     // if (!ssgapedseq.equals("ryas---dtqqwa----slchvh"))
688     // System.err.println("Subseqgaped\n------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhryas---dtqwrtsasll--qwa----slchvh\n"+ssgapedseq+"\n"+sub_se_gp.getCigarstring());
689   }
690
691   /**
692    * references to entities that this sequence cigar is associated with.
693    */
694   private Hashtable selGroups = null;
695
696   public void setGroupMembership(Object group)
697   {
698     if (selGroups == null)
699     {
700       selGroups = new Hashtable();
701     }
702     selGroups.put(group, new int[0]);
703   }
704
705   /**
706    * Test for and if present remove association to group.
707    * 
708    * @param group
709    * @return true if group was associated and it was removed
710    */
711   public boolean removeGroupMembership(Object group)
712   {
713     if (selGroups != null && selGroups.containsKey(group))
714     {
715       selGroups.remove(group);
716       return true;
717     }
718     return false;
719   }
720
721   /**
722    * forget all associations for this sequence.
723    */
724   public void clearMemberships()
725   {
726     if (selGroups != null)
727     {
728       selGroups.clear();
729     }
730     selGroups = null;
731   }
732
733   /**
734    * 
735    * @return null or array of all associated entities
736    */
737   public Object[] getAllMemberships()
738   {
739     if (selGroups == null)
740     {
741       return null;
742     }
743     Object[] mmbs = new Object[selGroups.size()];
744     Enumeration en = selGroups.keys();
745     for (int i = 0; en.hasMoreElements(); i++)
746     {
747       mmbs[i] = en.nextElement();
748     }
749     return mmbs;
750   }
751
752   /**
753    * Test for group membership
754    * 
755    * @param sgr
756    *          - a selection group or some other object that may be associated
757    *          with seqCigar
758    * @return true if sgr is associated with this seqCigar
759    */
760   public boolean isMemberOf(Object sgr)
761   {
762     return (selGroups != null) && selGroups.get(sgr) != null;
763   }
764 }