JAL-1620 version bump and release notes
[jalview.git] / src / jalview / datamodel / Alignment.java
index 271374f..7c4b3c7 100755 (executable)
@@ -1,30 +1,40 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
- * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
- *
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
+ * Copyright (C) 2014 The Jalview Authors
+ * 
  * This file is part of Jalview.
- *
+ * 
  * Jalview is free software: you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * Jalview is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty
- * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  * PURPOSE.  See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.datamodel;
 
-import java.util.*;
+import jalview.util.MessageManager;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import java.util.Vector;
 
 /**
  * Data structure to hold and manipulate a multiple sequence alignment
  */
 /**
  * @author JimP
- *
+ * 
  */
 public class Alignment implements AlignmentI
 {
@@ -77,7 +87,7 @@ public class Alignment implements AlignmentI
 
   /**
    * Make an alignment from an array of Sequences.
-   *
+   * 
    * @param sequences
    */
   public Alignment(SequenceI[] seqs)
@@ -87,7 +97,7 @@ public class Alignment implements AlignmentI
 
   /**
    * Make a new alignment from an array of SeqCigars
-   *
+   * 
    * @param seqs
    *          SeqCigar[]
    */
@@ -103,19 +113,19 @@ public class Alignment implements AlignmentI
    * compactAlignment does not contain hidden regions. JBPNote - must also check
    * that compactAlignment resolves to a set of SeqCigars - or construct them
    * appropriately.
-   *
+   * 
    * @param compactAlignment
    *          CigarArray
    */
   public static AlignmentI createAlignment(CigarArray compactAlignment)
   {
-    throw new Error("Alignment(CigarArray) not yet implemented");
+    throw new Error(MessageManager.getString("error.alignment_cigararray_not_implemented"));
     // this(compactAlignment.refCigars);
   }
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -137,7 +147,9 @@ public class Alignment implements AlignmentI
   public SequenceI[] getSequencesArray()
   {
     if (sequences == null)
+    {
       return null;
+    }
     synchronized (sequences)
     {
       return sequences.toArray(new SequenceI[sequences.size()]);
@@ -146,10 +158,10 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @param i
    *          DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -167,7 +179,7 @@ public class Alignment implements AlignmentI
 
   /**
    * Adds a sequence to the alignment. Recalculates maxLength and size.
-   *
+   * 
    * @param snew
    */
   @Override
@@ -201,12 +213,14 @@ public class Alignment implements AlignmentI
       }
     }
     if (hiddenSequences != null)
+    {
       hiddenSequences.adjustHeightSequenceAdded();
+    }
   }
 
   /**
    * Adds a sequence to the alignment. Recalculates maxLength and size.
-   *
+   * 
    * @param snew
    */
   @Override
@@ -222,7 +236,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -235,7 +249,9 @@ public class Alignment implements AlignmentI
   public void finalize()
   {
     if (getDataset() != null)
+    {
       getDataset().removeAlignmentRef();
+    }
 
     dataset = null;
     sequences = null;
@@ -258,7 +274,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @param s
    *          DOCUMENT ME!
    */
@@ -270,7 +286,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @param i
    *          DOCUMENT ME!
    */
@@ -289,7 +305,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
    */
   @Override
@@ -312,7 +328,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see
    * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
    */
@@ -370,7 +386,7 @@ public class Alignment implements AlignmentI
             return;
           }
         }
-
+        sg.setContext(this);
         groups.add(sg);
       }
     }
@@ -378,7 +394,7 @@ public class Alignment implements AlignmentI
 
   /**
    * remove any annotation that references gp
-   *
+   * 
    * @param gp
    *          (if null, removes all group associated annotation)
    */
@@ -445,6 +461,10 @@ public class Alignment implements AlignmentI
       {
         removeAnnotationForGroup(null);
       }
+      for (SequenceGroup sg : groups)
+      {
+        sg.setContext(null);
+      }
       groups.clear();
     }
   }
@@ -459,6 +479,7 @@ public class Alignment implements AlignmentI
       {
         removeAnnotationForGroup(g);
         groups.remove(g);
+        g.setContext(null);
       }
     }
   }
@@ -472,7 +493,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
    */
   @Override
@@ -483,7 +504,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
    * boolean)
    */
@@ -555,7 +576,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
    */
   @Override
@@ -578,7 +599,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see
    * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
    */
@@ -600,7 +621,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -611,7 +632,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -632,7 +653,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @param gc
    *          DOCUMENT ME!
    */
@@ -652,7 +673,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -663,7 +684,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#isAligned()
    */
   @Override
@@ -674,7 +695,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#isAligned(boolean)
    */
   @Override
@@ -701,7 +722,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
    * AlignmentAnnotation)
    */
@@ -739,7 +760,9 @@ public class Alignment implements AlignmentI
         continue;
       }
       if (tIndex < temp.length)
+      {
         temp[tIndex++] = annotations[i];
+      }
     }
 
     if (swap)
@@ -755,7 +778,7 @@ public class Alignment implements AlignmentI
 
   /**
    * remove any object references associated with this annotation
-   *
+   * 
    * @param aa
    */
   private void unhookAnnotation(AlignmentAnnotation aa)
@@ -773,7 +796,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
    * AlignmentAnnotation)
    */
@@ -785,7 +808,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
    * AlignmentAnnotation, int)
    */
@@ -935,6 +958,27 @@ public class Alignment implements AlignmentI
     else if (dataset == null && data != null)
     {
       dataset = data;
+      for (int i = 0; i < getHeight(); i++)
+      {
+        SequenceI currentSeq = getSequenceAt(i);
+        SequenceI dsq = currentSeq.getDatasetSequence();
+        if (dsq == null)
+        {
+          dsq = currentSeq.createDatasetSequence();
+          dataset.addSequence(dsq);
+        }
+        else
+        {
+          while (dsq.getDatasetSequence() != null)
+          {
+            dsq = dsq.getDatasetSequence();
+          }
+          if (dataset.findIndex(dsq) == -1)
+          {
+            dataset.addSequence(dsq);
+          }
+        }
+      }
     }
     dataset.addAlignmentRef();
   }
@@ -1005,7 +1049,7 @@ public class Alignment implements AlignmentI
   /**
    * Justify the sequences to the left or right by deleting and inserting gaps
    * before the initial residue or after the terminal residue
-   *
+   * 
    * @param right
    *          true if alignment padded to right, false to justify to left
    * @return true if alignment was changed
@@ -1135,7 +1179,9 @@ public class Alignment implements AlignmentI
   public void setProperty(Object key, Object value)
   {
     if (alignmentProperties == null)
+    {
       alignmentProperties = new Hashtable();
+    }
 
     alignmentProperties.put(key, value);
   }
@@ -1144,9 +1190,13 @@ public class Alignment implements AlignmentI
   public Object getProperty(Object key)
   {
     if (alignmentProperties != null)
+    {
       return alignmentProperties.get(key);
+    }
     else
+    {
       return null;
+    }
   }
 
   @Override
@@ -1159,7 +1209,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see
    * jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame
    * )
@@ -1168,7 +1218,9 @@ public class Alignment implements AlignmentI
   public void addCodonFrame(AlignedCodonFrame codons)
   {
     if (codons == null)
+    {
       return;
+    }
     if (codonFrameList == null)
     {
       codonFrameList = new AlignedCodonFrame[]
@@ -1183,7 +1235,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
    */
   @Override
@@ -1194,7 +1246,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see
    * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
    */
@@ -1202,15 +1254,21 @@ public class Alignment implements AlignmentI
   public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
   {
     if (seq == null || codonFrameList == null)
+    {
       return null;
+    }
     Vector cframes = new Vector();
     for (int f = 0; f < codonFrameList.length; f++)
     {
       if (codonFrameList[f].involvesSequence(seq))
+      {
         cframes.addElement(codonFrameList[f]);
+      }
     }
     if (cframes.size() == 0)
+    {
       return null;
+    }
     AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
     cframes.copyInto(cfr);
     return cfr;
@@ -1218,7 +1276,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#getCodonFrames()
    */
   @Override
@@ -1229,7 +1287,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @seejalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.
    * AlignedCodonFrame)
    */
@@ -1237,7 +1295,9 @@ public class Alignment implements AlignmentI
   public boolean removeCodonFrame(AlignedCodonFrame codons)
   {
     if (codons == null || codonFrameList == null)
+    {
       return false;
+    }
     boolean removed = false;
     int i = 0, iSize = codonFrameList.length;
     while (i < iSize)
@@ -1381,17 +1441,21 @@ public class Alignment implements AlignmentI
 
   @Override
   public AlignmentAnnotation findOrCreateAnnotation(String name,
-          String calcId, boolean autoCalc, SequenceI seqRef, SequenceGroup groupRef)
+          String calcId, boolean autoCalc, SequenceI seqRef,
+          SequenceGroup groupRef)
   {
-    assert(name!=null);
-    for (AlignmentAnnotation annot : getAlignmentAnnotation())
+    assert (name != null);
+    if (annotations != null)
     {
-      if (annot.autoCalculated == autoCalc
-              && (name.equals(annot.label))
-              && (calcId==null || annot.getCalcId().equals(calcId))
-              && annot.sequenceRef == seqRef && annot.groupRef == groupRef)
+      for (AlignmentAnnotation annot : getAlignmentAnnotation())
       {
-        return annot;
+        if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
+                && (calcId == null || annot.getCalcId().equals(calcId))
+                && annot.sequenceRef == seqRef
+                && annot.groupRef == groupRef)
+        {
+          return annot;
+        }
       }
     }
     AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
@@ -1425,6 +1489,27 @@ public class Alignment implements AlignmentI
     return aa;
   }
 
+  /**
+   * Returns an iterable collection of any annotations that match on given
+   * sequence ref, calcId and label (ignoring null values).
+   */
+  @Override
+  public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
+          String calcId, String label)
+  {
+    ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
+    for (AlignmentAnnotation ann : getAlignmentAnnotation())
+    {
+      if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
+              && ann.sequenceRef != null && ann.sequenceRef == seq
+              && ann.label != null && ann.label.equals(label))
+      {
+        aa.add(ann);
+      }
+    }
+    return aa;
+  }
+
   @Override
   public void moveSelectedSequencesByOne(SequenceGroup sg,
           Map<SequenceI, SequenceCollectionI> map, boolean up)
@@ -1476,4 +1561,37 @@ public class Alignment implements AlignmentI
     }
   }
 
+  @Override
+  public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
+  {
+    alignmentAnnotation.validateRangeAndDisplay();
+    if (isNucleotide() && alignmentAnnotation.isValidStruc())
+    {
+      hasRNAStructure = true;
+    }
+  }
+
+  @Override
+  public int getEndRes()
+  {
+    return getWidth() - 1;
+  }
+
+  @Override
+  public int getStartRes()
+  {
+    return 0;
+  }
+
+  /*
+   * In the case of AlignmentI - returns the dataset for the alignment, if set
+   * (non-Javadoc)
+   * 
+   * @see jalview.datamodel.AnnotatedCollectionI#getContext()
+   */
+  @Override
+  public AnnotatedCollectionI getContext()
+  {
+    return dataset;
+  }
 }