From d7563d2df09ba5146008951448b941a69bf74744 Mon Sep 17 00:00:00 2001
From: jprocter <Jim Procter>
Date: Fri, 13 Jul 2007 15:01:48 +0000
Subject: [PATCH] mapping bug fixes

---
 src/jalview/datamodel/AlignedCodonFrame.java |   86 +++++++++++++++++++-------
 src/jalview/datamodel/Alignment.java         |    6 +-
 2 files changed, 66 insertions(+), 26 deletions(-)

diff --git a/src/jalview/datamodel/AlignedCodonFrame.java b/src/jalview/datamodel/AlignedCodonFrame.java
index c89c170..f4e2d97 100644
--- a/src/jalview/datamodel/AlignedCodonFrame.java
+++ b/src/jalview/datamodel/AlignedCodonFrame.java
@@ -1,5 +1,8 @@
 package jalview.datamodel;
 
+import java.util.Enumeration;
+import java.util.Vector;
+
 import jalview.util.MapList;
 
 /**
@@ -24,6 +27,11 @@ public class AlignedCodonFrame
    */
   public AlignedCodonFrame(int aWidth)
   {
+    if (aWidth<=0)
+   {
+      codons=null;
+      return;
+   }
     codons = new int[aWidth][];
     for (int res = 0; res < aWidth; res++)
       codons[res] = null;
@@ -57,6 +65,11 @@ public class AlignedCodonFrame
     return aaWidth;
   }
   /**
+   * TODO: not an ideal solution - we reference the aligned amino acid sequences in order to make insertions on them
+   * Better would be dnaAlignment and aaAlignment reference....
+   */
+  Vector a_aaSeqs=new Vector();
+  /**
    * increase aaWidth by one and insert a new aligned codon position space at aspos.
    * @param aspos
    */
@@ -64,10 +77,15 @@ public class AlignedCodonFrame
   {            
     // this aa appears before the aligned codons at aspos - so shift them in each pair of mapped sequences
     aaWidth++;
-    for (int sq=0;aaSeqs!=null && sq<aaSeqs.length; sq++) {
-      aaSeqs[sq].insertCharAt(aspos, gapCharacter);
+    if (a_aaSeqs!=null)
+    {
+      // we actually have to modify the aligned sequences here, so use the a_aaSeqs vector
+      Enumeration sq = a_aaSeqs.elements();
+      while (sq.hasMoreElements())
+      {
+        ((SequenceI) sq.nextElement()).insertCharAt(aspos, gapCharacter);
+      }
     }
-
     checkCodonFrameWidth(aspos);
     if (aspos<aaWidth)
     {
@@ -86,14 +104,17 @@ public class AlignedCodonFrame
    */
   SequenceI[] dnaSeqs=null;
   /**
-   * tied array of protein sequence Objects
+   * tied array of Mappings to protein sequence Objects and 
+     SequenceI[] aaSeqs=null;
+   * MapLists where eac maps from the corresponding dnaSeqs element to corresponding aaSeqs element  
    */
-  SequenceI[] aaSeqs=null;
+  Mapping[] dnaToProt=null;
   /**
-   * tied array of MapLists where eac maps from the corresponding dnaSeqs element to corresponding aaSeqs element  
+   * add a mapping between the dataset sequences for the associated dna and protein sequence objects
+   * @param dnaseq
+   * @param aaseq
+   * @param map
    */
-  MapList[] dnaToProt=null;
-  
   public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
   { 
     int nlen=1;
@@ -102,21 +123,20 @@ public class AlignedCodonFrame
       nlen = dnaSeqs.length+1;
     }
     SequenceI[] ndna = new SequenceI[nlen];
-    SequenceI[] naa = new SequenceI[nlen];
-    MapList[] ndtp = new MapList[nlen];
+    Mapping[] ndtp = new Mapping[nlen];
     if (dnaSeqs!=null)
     {
       System.arraycopy(dnaSeqs,0,ndna, 0, dnaSeqs.length);
-      System.arraycopy(aaSeqs,0,naa, 0, dnaSeqs.length);
       System.arraycopy(dnaToProt,0,ndtp, 0, dnaSeqs.length);
     }
     dnaSeqs = ndna;
-    aaSeqs = naa;
     dnaToProt = ndtp;
     nlen--;
     dnaSeqs[nlen] = (dnaseq.getDatasetSequence()==null) ? dnaseq : dnaseq.getDatasetSequence();
-    aaSeqs[nlen] = (aaseq.getDatasetSequence()==null) ? aaseq : aaseq.getDatasetSequence();
-    dnaToProt[nlen] = map;
+    Mapping mp = new Mapping(map);
+    mp.to = (aaseq.getDatasetSequence()==null) ? aaseq : aaseq.getDatasetSequence();
+    a_aaSeqs.addElement(aaseq);
+    dnaToProt[nlen] = mp;
   }
 
 
@@ -126,10 +146,28 @@ public class AlignedCodonFrame
   }
   public SequenceI[] getAaSeqs()
   {
-    return aaSeqs;
+    if (dnaToProt==null)
+      return null;
+    SequenceI[] sqs = new SequenceI[dnaToProt.length];
+    for (int sz=0; sz<dnaToProt.length; sz++)
+    {
+      sqs[sz]  = dnaToProt[sz].to;
+    }
+    return sqs;
   }
   public MapList[] getdnaToProt()
   {
+    if (dnaToProt==null)
+      return null;
+    MapList[] sqs = new MapList[dnaToProt.length];
+    for (int sz=0; sz<dnaToProt.length; sz++)
+    {
+      sqs[sz]  = dnaToProt[sz].map;
+    }
+    return sqs;
+  }
+  public Mapping[] getProtMappings()
+  {
     return dnaToProt;
   }
   /**
@@ -147,7 +185,7 @@ public class AlignedCodonFrame
     for (int ds=0;ds<dnaSeqs.length; ds++)
     {
       if (dnaSeqs[ds]==dnaSeqRef || dnaSeqs[ds]==dnads)
-        return aaSeqs[ds];
+        return dnaToProt[ds].to;
     }
     return null;
   }
@@ -158,14 +196,14 @@ public class AlignedCodonFrame
    */
   public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
   {
-    if (aaSeqs==null)
+    if (dnaToProt==null)
     {
       return null;
     }
     SequenceI aads = aaSeqRef.getDatasetSequence();
-    for (int as=0;as<aaSeqs.length; as++)
+    for (int as=0;as<dnaToProt.length; as++)
     {
-      if (aaSeqs[as]==aaSeqRef || aaSeqs[as]==aads)
+      if (dnaToProt[as].to==aaSeqRef || dnaToProt[as].to==aads)
         return dnaSeqs[as];
     }
     return null;
@@ -189,25 +227,25 @@ public class AlignedCodonFrame
   {
     int[] codon;
     SequenceI ds = seq.getDatasetSequence();
-    for (int mi = 0; mi<aaSeqs.length; mi++)
+    for (int mi = 0; mi<dnaToProt.length; mi++)
     {
       if (dnaSeqs[mi]==seq || dnaSeqs[mi]==ds)
       {
         // DEBUG System.err.println("dna pos "+index);
-        codon = dnaToProt[mi].locateInTo(index,index);
+        codon = dnaToProt[mi].map.locateInTo(index,index);
         if (codon!=null)
         {
           for (int i=0; i<codon.length; i+=2)
             {
-              results.addResult(aaSeqs[mi], codon[i], codon[i+1]);
+              results.addResult(dnaToProt[mi].to, codon[i], codon[i+1]);
             }
         }
       } else
-        if (aaSeqs[mi]==seq || aaSeqs[mi]==ds)
+        if (dnaToProt[mi].to==seq || dnaToProt[mi].to==ds)
         {
           // DEBUG System.err.println("aa pos "+index);
           {
-            codon = dnaToProt[mi].locateInFrom(index, index);
+            codon = dnaToProt[mi].map.locateInFrom(index, index);
             if (codon!=null)
             {
             for (int i=0; i<codon.length; i+=2)
diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java
index b3a5266..c474142 100755
--- a/src/jalview/datamodel/Alignment.java
+++ b/src/jalview/datamodel/Alignment.java
@@ -108,6 +108,8 @@ public class Alignment
 
   public SequenceI[] getSequencesArray()
   {
+    if (sequences==null)
+      return null;
     SequenceI[] reply = new SequenceI[sequences.size()];
     for (int i = 0; i < sequences.size(); i++)
     {
@@ -740,7 +742,7 @@ public class Alignment
     }
     AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length+1];
     System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
-    t[codonFrameList.length+1] = codons;
+    t[codonFrameList.length] = codons;
     codonFrameList = t;
   }
 
@@ -763,7 +765,7 @@ public class Alignment
     for (int f=0;f<codonFrameList.length; f++)
     {
       if (codonFrameList[f].involvesSequence(seq))
-        cframes.add(codonFrameList[f]);
+        cframes.addElement(codonFrameList[f]);
     }
     if (cframes.size()==0)
       return null;
-- 
1.7.10.2