case invariant matching of seqeunce feature to sequence id string
[jalview.git] / src / jalview / datamodel / Alignment.java
index b3a5266..3689433 100755 (executable)
@@ -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++)
     {
@@ -188,7 +190,7 @@ public class Alignment
   public void finalize()
   {
     if(getDataset()!=null)
-      getDataset().finalize();
+      getDataset().removeAlignmentRef();
 
     dataset = null;
     sequences = null;
@@ -197,6 +199,16 @@ public class Alignment
     hiddenSequences = null;
   }
 
+  /**
+   * decrement the alignmentRefs counter by one and call finalize if it goes to zero.
+   */
+  private void removeAlignmentRef()
+  {
+    if (--alignmentRefs==0)
+    {
+      finalize();
+    }
+  }
 
   /**
    * DOCUMENT ME!
@@ -324,11 +336,25 @@ public class Alignment
   /**    */
   public SequenceI findName(String name)
   {
-    int i = 0;
+    return findName(name,false);
+  }
+
+  /* (non-Javadoc)
+   * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
+   */
+  public SequenceI findName(String token, boolean b)
+  {
 
+    int i = 0;
+    SequenceI sq=null;
+    String sqname=null;
     while (i < sequences.size())
     {
-      if (getSequenceAt(i).getName().equals(name))
+      sq = getSequenceAt(i);
+      sqname = sq.getName();
+      if (sqname.equals(token) // exact match
+          || (b && // allow imperfect matches - case varies 
+              (sqname.equalsIgnoreCase(token))))
       {
         return getSequenceAt(i);
       }
@@ -338,7 +364,6 @@ public class Alignment
 
     return null;
   }
-
   public SequenceI[] findSequenceMatch(String name)
   {
     Vector matches = new Vector();
@@ -605,7 +630,7 @@ public class Alignment
       // Create a new dataset for this alignment.
       // Can only be done once, if dataset is not null
       // This will not be performed
-      Sequence[] seqs = new Sequence[getHeight()];
+      SequenceI[] seqs = new SequenceI[getHeight()];
       SequenceI currentSeq;
       for (int i = 0; i < getHeight(); i++)
       {
@@ -616,17 +641,7 @@ public class Alignment
         }
         else
         {
-          seqs[i] = new Sequence(currentSeq.getName(),
-                                 AlignSeq.extractGaps(
-                                     jalview.util.Comparison.GapChars,
-                                     currentSeq.getSequenceAsString()
-                                 ),
-                                 currentSeq.getStart(),
-                                 currentSeq.getEnd());
-          seqs[i].sequenceFeatures = currentSeq.getSequenceFeatures();
-          seqs[i].setDescription(currentSeq.getDescription());
-          getSequenceAt(i).setSequenceFeatures(null);
-          getSequenceAt(i).setDatasetSequence(seqs[i]);
+          seqs[i] = currentSeq.createDatasetSequence();
         }
       }
 
@@ -636,6 +651,18 @@ public class Alignment
     {
       dataset = data;
     }
+    dataset.addAlignmentRef();
+  }
+  /**
+   * reference count for number of alignments referencing this one.
+   */
+  int alignmentRefs=0;
+  /**
+   * increase reference count to this alignment.
+   */
+  private void addAlignmentRef()
+  {
+    alignmentRefs++;
   }
 
   public Alignment getDataset()
@@ -740,7 +767,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 +790,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;