Formatting
[jalview.git] / src / jalview / datamodel / SeqCigar.java
index 7363e9e..c896555 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 package jalview.datamodel;
 
 import jalview.analysis.*;
-import jalview.util.ShiftList;
-import java.util.Vector;
+import jalview.util.*;
 
 public class SeqCigar
     extends CigarSimple
 {
-    /**
-     * start(inclusive) and end(exclusive) of subsequence on refseq
-     */
-    private int start, end;
+  /**
+   * start(inclusive) and end(exclusive) of subsequence on refseq
+   */
+  private int start, end;
   private SequenceI refseq = null;
   /**
    * Reference dataset sequence for the cigar string
@@ -38,20 +37,25 @@ public class SeqCigar
   {
     return refseq;
   }
+
   /**
    *
    * @return int start index of cigar ops on refSeq
    */
-  public int getStart() {
+  public int getStart()
+  {
     return start;
   }
+
   /**
    *
    * @return int end index (exclusive) of cigar ops on refSeq
    */
-  public int getEnd() {
+  public int getEnd()
+  {
     return end;
   }
+
   /**
    * Returns sequence as a string with cigar operations applied to it
    * @return String
@@ -59,7 +63,8 @@ public class SeqCigar
   public String getSequenceString(char GapChar)
   {
     return (length == 0) ? "" :
-        (String) getSequenceAndDeletions(refseq.getSequenceAsString(start, end), GapChar)[0];
+        (String) getSequenceAndDeletions(refseq.getSequenceAsString(start, end),
+                                         GapChar)[0];
   }
 
   /**
@@ -73,7 +78,8 @@ public class SeqCigar
     {
       return null;
     }
-    Object[] edit_result = getSequenceAndDeletions(refseq.getSequenceAsString(start,end),
+    Object[] edit_result = getSequenceAndDeletions(refseq.getSequenceAsString(
+        start, end),
         GapChar);
     if (edit_result == null)
     {
@@ -82,8 +88,9 @@ public class SeqCigar
     }
     int bounds[] = (int[]) edit_result[1];
     seq = new Sequence(refseq.getName(), (String) edit_result[0],
-                       refseq.getStart() + start+bounds[0],
-                       refseq.getStart() + start+((bounds[2]==0) ? -1 : bounds[2]));
+                       refseq.getStart() + start + bounds[0],
+                       refseq.getStart() + start +
+                       ( (bounds[2] == 0) ? -1 : bounds[2]));
     // seq.checkValidRange(); probably not needed
     seq.setDatasetSequence(refseq);
     seq.setDescription(refseq.getDescription());
@@ -105,30 +112,35 @@ public class SeqCigar
    * @param _e index after last position in (possibly gapped) seq
    * @return true if gaps are present in seq
    */
-  private boolean _setSeq(SequenceI seq, boolean initialDeletion, int _s, int _e)
+  private boolean _setSeq(SequenceI seq, boolean initialDeletion, int _s,
+                          int _e)
   {
     boolean hasgaps = false;
     if (seq == null)
     {
       throw new Error("Implementation Error - _setSeq(null,...)");
     }
-    if (_s<0)
-      throw new Error("Implementation Error: _s="+_s);
+    if (_s < 0)
+    {
+      throw new Error("Implementation Error: _s=" + _s);
+    }
     String seq_string = seq.getSequenceAsString();
-    if (_e==0 || _e<_s || _e>seq_string.length())
-      _e=seq_string.length();
+    if (_e == 0 || _e < _s || _e > seq_string.length())
+    {
+      _e = seq_string.length();
+    }
     // resolve start and end positions relative to ungapped reference sequence
-    start = seq.findPosition(_s)-seq.getStart();
-    end = seq.findPosition(_e)-seq.getStart();
-    int l_ungapped = end-start;
+    start = seq.findPosition(_s) - seq.getStart();
+    end = seq.findPosition(_e) - seq.getStart();
+    int l_ungapped = end - start;
     // Find correct sequence to reference and correct start and end - if necessary
     SequenceI ds = seq.getDatasetSequence();
     if (ds == null)
     {
       // make a new dataset sequence
       String ungapped = AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
-                                               new String(seq_string));
-      l_ungapped=ungapped.length();
+                                             new String(seq_string));
+      l_ungapped = ungapped.length();
       // check that we haven't just duplicated an ungapped sequence.
       if (l_ungapped == seq.getLength())
       {
@@ -138,7 +150,7 @@ public class SeqCigar
       {
         ds = new Sequence(seq.getName(), ungapped,
                           seq.getStart(),
-                          seq.getStart()+ungapped.length()-1);
+                          seq.getStart() + ungapped.length() - 1);
         // JBPNote: this would be consistent but may not be useful
         //        seq.setDatasetSequence(ds);
       }
@@ -146,29 +158,35 @@ public class SeqCigar
     // add in offset between seq and the dataset sequence
     if (ds.getStart() < seq.getStart())
     {
-      int offset=seq.getStart()-ds.getStart();
-      if (initialDeletion) {
-          // absolute cigar string
-          addDeleted(_s+offset);
-          start=0;
-          end+=offset;
-      } else {
-          // normal behaviour - just mark start and end subsequence
-          start+=offset;
-          end+=offset;
+      int offset = seq.getStart() - ds.getStart();
+      if (initialDeletion)
+      {
+        // absolute cigar string
+        addDeleted(_s + offset);
+        start = 0;
+        end += offset;
+      }
+      else
+      {
+        // normal behaviour - just mark start and end subsequence
+        start += offset;
+        end += offset;
 
       }
 
     }
 
     // any gaps to process ?
-    if (l_ungapped!=(_e-_s))
-      hasgaps=true;
+    if (l_ungapped != (_e - _s))
+    {
+      hasgaps = true;
+    }
 
     this.refseq = ds;
 
     // Check  offsets
-    if (end>ds.getLength()) {
+    if (end > ds.getLength())
+    {
       throw new Error("SeqCigar: Possible implementation error: sequence is longer than dataset sequence");
 //      end = ds.getLength();
     }
@@ -223,7 +241,7 @@ public class SeqCigar
       this.operation = null;
       this.range = null;
       this.length = 0;
-      if (_setSeq(seq, false,0, 0))
+      if (_setSeq(seq, false, 0, 0))
       {
         throw new Error("NOT YET Implemented: Constructing a Cigar object from a cigar string and a gapped sequence.");
       }
@@ -238,6 +256,7 @@ public class SeqCigar
   {
     this.addOperation(M, range);
   }
+
   /**
    * Adds
    * insertion and match operations based on seq to the cigar up to
@@ -250,15 +269,17 @@ public class SeqCigar
    * @param initialDeletions if true then initial deletions will be added from start of seq to startpos
    */
   protected static void addSequenceOps(CigarBase cigar, SequenceI seq,
-                                       int startpos, int endpos, boolean initialDeletions)
+                                       int startpos, int endpos,
+                                       boolean initialDeletions)
   {
     char op = '\0';
     int range = 0;
     int p = 0, res = seq.getLength();
 
     if (!initialDeletions)
-      p=startpos;
-
+    {
+      p = startpos;
+    }
 
     while (p <= endpos)
     {
@@ -324,7 +345,7 @@ public class SeqCigar
     }
     _setSeq(seq, false, 0, 0);
     // there is still work to do
-    addSequenceOps(this, seq, 0, seq.getLength()-1, false);
+    addSequenceOps(this, seq, 0, seq.getLength() - 1, false);
   }
 
   /**
@@ -340,7 +361,7 @@ public class SeqCigar
     {
       throw new Error("Implementation error for new Cigar(SequenceI)");
     }
-    _setSeq(seq, false, start, end+1);
+    _setSeq(seq, false, start, end + 1);
     // there is still work to do
     addSequenceOps(this, seq, start, end, false);
   }
@@ -371,13 +392,14 @@ public class SeqCigar
   {
     SequenceI[] seqs = new SequenceI[alseqs.length];
     StringBuffer[] g_seqs = new StringBuffer[alseqs.length];
-    String[] alseqs_string=new String[alseqs.length];
+    String[] alseqs_string = new String[alseqs.length];
     Object[] gs_regions = new Object[alseqs.length];
     for (int i = 0; i < alseqs.length; i++)
     {
-      alseqs_string[i]=alseqs[i].getRefSeq().
-          getSequenceAsString(alseqs[i].start,alseqs[i].end);
-      gs_regions[i] = alseqs[i].getSequenceAndDeletions(alseqs_string[i], gapCharacter); // gapped sequence, {start, start col, end. endcol}, hidden regions {{start, end, col}})
+      alseqs_string[i] = alseqs[i].getRefSeq().
+          getSequenceAsString(alseqs[i].start, alseqs[i].end);
+      gs_regions[i] = alseqs[i].getSequenceAndDeletions(alseqs_string[i],
+          gapCharacter); // gapped sequence, {start, start col, end. endcol}, hidden regions {{start, end, col}})
       if (gs_regions[i] == null)
       {
         throw new Error("Implementation error: " + i +
@@ -420,13 +442,16 @@ public class SeqCigar
             else
             {
               g_seqs[s].insert(inspos,
-                               alseqs_string[i].substring(region[0], region[1] + 1));
+                               alseqs_string[i].substring(region[0],
+                  region[1] + 1));
             }
           }
           shifts.addShift(region[2], insert.length); // update shift in alignment frame of reference
-          if (segments==null)
+          if (segments == null)
+          {
             // add a hidden column for this deletion
-            colsel.hideColumns(inspos, inspos+insert.length-1);
+            colsel.hideColumns(inspos, inspos + insert.length - 1);
+          }
         }
       }
     }
@@ -435,16 +460,20 @@ public class SeqCigar
       int[] bounds = ( (int[]) ( (Object[]) gs_regions[i])[1]);
       SequenceI ref = alseqs[i].getRefSeq();
       seqs[i] = new Sequence(ref.getName(), g_seqs[i].toString(),
-                             ref.getStart() + alseqs[i].start+bounds[0],
-                             ref.getStart() + alseqs[i].start+(bounds[2]==0 ? -1 : bounds[2]));
+                             ref.getStart() + alseqs[i].start + bounds[0],
+                             ref.getStart() + alseqs[i].start +
+                             (bounds[2] == 0 ? -1 : bounds[2]));
       seqs[i].setDatasetSequence(ref);
       seqs[i].setDescription(ref.getDescription());
     }
-    if (segments!=null) {
-      for (int i=0; i<segments.length; i+=3) {
+    if (segments != null)
+    {
+      for (int i = 0; i < segments.length; i += 3)
+      {
         //int start=shifts.shift(segments[i]-1)+1;
         //int end=shifts.shift(segments[i]+segments[i+1]-1)-1;
-        colsel.hideColumns(segments[i+1], segments[i+1]+segments[i+2]-1);
+        colsel.hideColumns(segments[i + 1],
+                           segments[i + 1] + segments[i + 2] - 1);
       }
     }
     return seqs;
@@ -474,7 +503,7 @@ public class SeqCigar
   public static boolean testSeqRecovery(SeqCigar gen_sgapped,
                                         SequenceI s_gapped)
   {
-      // this is non-rigorous - start and end  recovery is not tested.
+    // this is non-rigorous - start and end  recovery is not tested.
     SequenceI gen_sgapped_s = gen_sgapped.getSeq('-');
     if (!gen_sgapped_s.getSequence().equals(s_gapped.getSequence()))
     {
@@ -491,17 +520,20 @@ public class SeqCigar
   {
     String o_seq;
     Sequence s = new Sequence("MySeq",
-                              o_seq = "asdfktryasdtqwrtsaslldddptyipqqwaslchvhttt",
+                              o_seq =
+                              "asdfktryasdtqwrtsaslldddptyipqqwaslchvhttt",
                               39, 80);
     String orig_gapped;
     Sequence s_gapped = new Sequence("MySeq",
-        orig_gapped = "----asdf------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhttt",
+                                     orig_gapped =
+        "----asdf------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhttt",
                                      39, 80);
     String ex_cs_gapped = "4I4M6I6M3I11M4I12M4I9M";
     s_gapped.setDatasetSequence(s);
     String sub_gapped_s;
     Sequence s_subsequence_gapped = new Sequence("MySeq",
-        sub_gapped_s = "------ktryas---dtqwrtsasll----dddptyipqqwa----slchvh",
+                                                 sub_gapped_s =
+        "------ktryas---dtqwrtsasll----dddptyipqqwa----slchvh",
                                                  43, 77);
 
     s_subsequence_gapped.setDatasetSequence(s);
@@ -564,7 +596,7 @@ public class SeqCigar
         sub_se_gp.deleteRange(st, e);
         String ssgapedseq = sub_se_gp.getSeq('-').getSequenceAsString();
         System.out.println(st + "," + e + "\t:" + ssgapedseq);
-        st -=3;
+        st -= 3;
       }
     }
     {