JAL-1524 fix
[jalview.git] / src / jalview / analysis / AlignmentUtils.java
index 2e607c0..85699a4 100644 (file)
@@ -28,7 +28,7 @@ public class AlignmentUtils
     for (SequenceI s:core.getSequences())
     {
       SequenceI newSeq = s.deriveSequence();
-      if (newSeq.getStart()>maxoffset)
+      if (newSeq.getStart()>maxoffset && newSeq.getDatasetSequence().getStart()<s.getStart())
       {
         maxoffset = newSeq.getStart();
       }
@@ -44,8 +44,9 @@ public class AlignmentUtils
       while (ds.getDatasetSequence()!=null) {
         ds=ds.getDatasetSequence();
       }
+      int s_end = s.findPosition(s.getStart()+s.getLength());
       // find available flanking residues for sequence
-      int ustream_ds=s.getStart()-ds.getStart(),dstream_ds=ds.getEnd()-s.getEnd();
+      int ustream_ds=s.getStart()-ds.getStart(),dstream_ds=ds.getEnd()-s_end;
       
       // build new flanked sequence
       
@@ -66,7 +67,7 @@ public class AlignmentUtils
         }
       }
       char[] upstream = new String(ds.getSequence(s.getStart()-1-ustream_ds, s.getStart()-1)).toLowerCase().toCharArray();
-      char[] downstream = new String(ds.getSequence(s.getEnd()+1,s.getEnd()+1+dstream_ds)).toLowerCase().toCharArray();
+      char[] downstream = new String(ds.getSequence(s_end-1,s_end+1+dstream_ds)).toLowerCase().toCharArray();
       char[] coreseq=s.getSequence();
       char[] nseq = new char[offset+upstream.length+downstream.length+coreseq.length]; 
       char c = core.getGapCharacter();
@@ -82,7 +83,7 @@ public class AlignmentUtils
       System.arraycopy(downstream, 0, nseq, p+coreseq.length+upstream.length, downstream.length);
       s.setSequence(new String(nseq));
       s.setStart(s.getStart()-ustream_ds);
-      s.setEnd(s.getEnd()+downstream.length);
+      s.setEnd(s_end+downstream.length);
     }
     AlignmentI newAl = new jalview.datamodel.Alignment(sq.toArray(new SequenceI[0]));
     newAl.setDataset(core.getDataset());