return flag indicating if annotations were actually deleted from alignment
[jalview.git] / src / jalview / datamodel / Alignment.java
index ddb58c3..cec2abc 100755 (executable)
@@ -40,6 +40,8 @@ public class Alignment
 
   HiddenSequences hiddenSequences = new HiddenSequences(this);
 
+  public Hashtable alignmentProperties;
+
   private void initAlignment(SequenceI[] seqs)
   {
     int i = 0;
@@ -139,25 +141,26 @@ public class Alignment
   {
     if (dataset != null)
     {
+      // maintain dataset integrity
       if (snew.getDatasetSequence() != null)
       {
         getDataset().addSequence(snew.getDatasetSequence());
       }
       else
       {
-        Sequence ds = new Sequence(snew.getName(),
-                                   AlignSeq.extractGaps("-. ",
-            snew.getSequenceAsString()),
-                                   snew.getStart(),
-                                   snew.getEnd());
-
-        snew.setDatasetSequence(ds);
-        getDataset().addSequence(ds);
+        // derive new sequence
+        SequenceI adding = snew.deriveSequence();
+        getDataset().addSequence(adding.getDatasetSequence());
+        snew = adding;
       }
     }
-    sequences.addElement(snew);
-
-    hiddenSequences.adjustHeightSequenceAdded();
+    if (sequences==null) {
+      initAlignment(new SequenceI[] { snew });
+    } else {
+      sequences.addElement(snew);
+    }
+    if (hiddenSequences!=null)
+      hiddenSequences.adjustHeightSequenceAdded();
   }
 
   /** Adds a sequence to the alignment.  Recalculates maxLength and size.
@@ -182,10 +185,10 @@ public class Alignment
     return groups;
   }
 
-  public void destroyAlignment()
+  public void finalize()
   {
     if(getDataset()!=null)
-      getDataset().destroyAlignment();
+      getDataset().finalize();
 
     dataset = null;
     sequences = null;
@@ -457,13 +460,10 @@ public class Alignment
 
     return true;
   }
-
-  /**
-   * DOCUMENT ME!
-   *
-   * @param aa DOCUMENT ME!
+  /* (non-Javadoc)
+   * @see jalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.AlignmentAnnotation)
    */
-  public void deleteAnnotation(AlignmentAnnotation aa)
+  public boolean deleteAnnotation(AlignmentAnnotation aa)
   {
     int aSize = 1;
 
@@ -474,17 +474,19 @@ public class Alignment
 
     if (aSize < 1)
     {
-      return;
+      return false;
     }
 
     AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
 
+    boolean swap=false;
     int tIndex = 0;
 
-    for (int i = 0; i < aSize; i++)
+    for (int i = 0; tIndex<temp.length && i < aSize; i++)
     {
       if (annotations[i] == aa)
       {
+        swap=true;
         continue;
       }
 
@@ -492,7 +494,13 @@ public class Alignment
       tIndex++;
     }
 
-    annotations = temp;
+    if (swap)
+    {
+      annotations = temp;
+      if(aa.sequenceRef!=null)
+        aa.sequenceRef.removeAlignmentAnnotation(aa);
+    }
+    return swap;
   }
 
   /**
@@ -698,4 +706,25 @@ public class Alignment
     return cal;
   }
 
+  public void setProperty(Object key, Object value)
+  {
+    if(alignmentProperties==null)
+      alignmentProperties = new Hashtable();
+
+    alignmentProperties.put(key,value);
+  }
+
+  public Object getProperty(Object key)
+  {
+    if(alignmentProperties!=null)
+      return alignmentProperties.get(key);
+    else
+      return null;
+  }
+
+  public Hashtable getProperties()
+  {
+    return alignmentProperties;
+  }
+
 }