JAL-1259 patch to avoid NPE
[jalview.git] / src / jalview / datamodel / Alignment.java
index 6c11936..1553710 100755 (executable)
@@ -1,18 +1,18 @@
 /*
- * 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)
+ * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
+ * 
  * 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
+ * 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
+ *  
+ * 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/>.
  */
 package jalview.datamodel;
@@ -24,7 +24,7 @@ import java.util.*;
  */
 /**
  * @author JimP
- *
+ * 
  */
 public class Alignment implements AlignmentI
 {
@@ -77,7 +77,7 @@ public class Alignment implements AlignmentI
 
   /**
    * Make an alignment from an array of Sequences.
-   *
+   * 
    * @param sequences
    */
   public Alignment(SequenceI[] seqs)
@@ -87,7 +87,7 @@ public class Alignment implements AlignmentI
 
   /**
    * Make a new alignment from an array of SeqCigars
-   *
+   * 
    * @param seqs
    *          SeqCigar[]
    */
@@ -103,7 +103,7 @@ 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
    */
@@ -115,7 +115,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -146,10 +146,10 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @param i
    *          DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -167,7 +167,7 @@ public class Alignment implements AlignmentI
 
   /**
    * Adds a sequence to the alignment. Recalculates maxLength and size.
-   *
+   * 
    * @param snew
    */
   @Override
@@ -206,7 +206,7 @@ public class Alignment implements AlignmentI
 
   /**
    * Adds a sequence to the alignment. Recalculates maxLength and size.
-   *
+   * 
    * @param snew
    */
   @Override
@@ -222,7 +222,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -258,7 +258,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @param s
    *          DOCUMENT ME!
    */
@@ -270,7 +270,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @param i
    *          DOCUMENT ME!
    */
@@ -289,7 +289,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
    */
   @Override
@@ -312,7 +312,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see
    * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
    */
@@ -378,7 +378,7 @@ public class Alignment implements AlignmentI
 
   /**
    * remove any annotation that references gp
-   *
+   * 
    * @param gp
    *          (if null, removes all group associated annotation)
    */
@@ -472,7 +472,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
    */
   @Override
@@ -483,7 +483,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
    * boolean)
    */
@@ -555,7 +555,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
    */
   @Override
@@ -578,7 +578,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see
    * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
    */
@@ -600,7 +600,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -611,7 +611,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -632,7 +632,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @param gc
    *          DOCUMENT ME!
    */
@@ -652,7 +652,7 @@ public class Alignment implements AlignmentI
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   @Override
@@ -663,7 +663,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#isAligned()
    */
   @Override
@@ -674,7 +674,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#isAligned(boolean)
    */
   @Override
@@ -701,7 +701,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
    * AlignmentAnnotation)
    */
@@ -755,7 +755,7 @@ public class Alignment implements AlignmentI
 
   /**
    * remove any object references associated with this annotation
-   *
+   * 
    * @param aa
    */
   private void unhookAnnotation(AlignmentAnnotation aa)
@@ -773,7 +773,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
    * AlignmentAnnotation)
    */
@@ -785,7 +785,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
    * AlignmentAnnotation, int)
    */
@@ -1005,7 +1005,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
@@ -1159,7 +1159,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see
    * jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame
    * )
@@ -1183,7 +1183,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
    */
   @Override
@@ -1194,7 +1194,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see
    * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
    */
@@ -1218,7 +1218,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see jalview.datamodel.AlignmentI#getCodonFrames()
    */
   @Override
@@ -1229,7 +1229,7 @@ public class Alignment implements AlignmentI
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @seejalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.
    * AlignedCodonFrame)
    */
@@ -1381,21 +1381,27 @@ public class Alignment implements AlignmentI
 
   @Override
   public AlignmentAnnotation findOrCreateAnnotation(String name,
-          boolean autoCalc, SequenceI seqRef, SequenceGroup groupRef)
+          String calcId, boolean autoCalc, SequenceI seqRef,
+          SequenceGroup groupRef)
   {
-    for (AlignmentAnnotation annot : getAlignmentAnnotation())
+    assert (name != null);
+    if (annotations != null)
     {
-      if (annot.autoCalculated == autoCalc
-              && annot.getCalcId().equals(name)
-              && 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,
             new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
     annot.hasText = false;
-    annot.setCalcId(new String(name));
+    annot.setCalcId(new String(calcId));
     annot.autoCalculated = autoCalc;
     if (seqRef != null)
     {