JAL-1499 patch from Mungo Carstairs
[jalview.git] / src / jalview / io / AlignFile.java
old mode 100755 (executable)
new mode 100644 (file)
index 3f43582..977cb25
@@ -1,26 +1,36 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
- * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
+ * 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.
- * 
+ * 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.io;
 
-import java.io.*;
-import java.util.*;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
 
-import jalview.datamodel.*;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Set;
+import java.util.Vector;
 
 /**
  * DOCUMENT ME!
@@ -37,7 +47,7 @@ public abstract class AlignFile extends FileParse
   /**
    * Sequences to be added to form a new alignment.
    */
-  protected Vector seqs;
+  protected Vector<SequenceI> seqs;
 
   /**
    * annotation to be added to generated alignment object
@@ -47,7 +57,7 @@ public abstract class AlignFile extends FileParse
   /**
    * Properties to be added to generated alignment object
    */
-  protected Hashtable properties;
+  protected Hashtable<Object, Object> properties;
 
   long start;
 
@@ -60,6 +70,10 @@ public abstract class AlignFile extends FileParse
    */
   public AlignFile()
   {
+    // Shouldn't we init data structures (JBPNote: not sure - initData is for
+    // initialising the structures used for reading from a datasource, and the
+    // bare constructor hasn't got any datasource)
+    initData();
   }
 
   /**
@@ -73,10 +87,13 @@ public abstract class AlignFile extends FileParse
   public AlignFile(String inFile, String type) throws IOException
   {
     super(inFile, type);
-
     initData();
-
     parse();
+    // sets the index of each sequence in the alignment
+    for (int i = 0, c = seqs.size(); i < c; i++)
+    {
+      seqs.get(i).setIndex(i);
+    }
   }
 
   /**
@@ -91,12 +108,17 @@ public abstract class AlignFile extends FileParse
     super(source);
     initData();
     parse();
+    // sets the index of each sequence in the alignment
+    for (int i = 0, c = seqs.size(); i < c; i++)
+    {
+      seqs.get(i).setIndex(i);
+    }
   }
 
   /**
    * Return the seqs Vector
    */
-  public Vector getSeqs()
+  public Vector<SequenceI> getSeqs()
   {
     return seqs;
   }
@@ -110,7 +132,7 @@ public abstract class AlignFile extends FileParse
 
     for (int i = 0; i < seqs.size(); i++)
     {
-      s[i] = (SequenceI) seqs.elementAt(i);
+      s[i] = seqs.elementAt(i);
     }
 
     return s;
@@ -127,7 +149,17 @@ public abstract class AlignFile extends FileParse
     addProperties(al);
     for (int i = 0; i < annotations.size(); i++)
     {
-      al.addAnnotation((AlignmentAnnotation) annotations.elementAt(i));
+      // detect if annotations.elementAt(i) rna secondary structure
+      // if so then do:
+      /*
+       * SequenceFeature[] pairArray =
+       * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
+       * Rna.HelixMap(pairArray);
+       */
+      AlignmentAnnotation an = (AlignmentAnnotation) annotations
+              .elementAt(i);
+      an.validateRangeAndDisplay();
+      al.addAnnotation(an);
     }
 
   }
@@ -143,8 +175,8 @@ public abstract class AlignFile extends FileParse
   {
     if (properties != null && properties.size() > 0)
     {
-      Enumeration keys = properties.keys();
-      Enumeration vals = properties.elements();
+      Enumeration<Object> keys = properties.keys();
+      Enumeration<Object> vals = properties.elements();
       while (keys.hasMoreElements())
       {
         al.setProperty(keys.nextElement(), vals.nextElement());
@@ -175,7 +207,7 @@ public abstract class AlignFile extends FileParse
     }
     if (properties == null)
     {
-      properties = new Hashtable();
+      properties = new Hashtable<Object, Object>();
     }
     properties.put(key, value);
   }
@@ -194,11 +226,20 @@ public abstract class AlignFile extends FileParse
    */
   protected void initData()
   {
-    seqs = new Vector();
+    seqs = new Vector<SequenceI>();
     annotations = new Vector();
   }
 
   /**
+   * Return the alignment properties (or null if none set)
+   * 
+   * @return
+   */
+  protected Set<Map.Entry<Object, Object>> getAlignmentProperties()
+  {
+    return (this.properties == null ? null : this.properties.entrySet());
+  }
+  /**
    * DOCUMENT ME!
    * 
    * @param s
@@ -206,7 +247,7 @@ public abstract class AlignFile extends FileParse
    */
   protected void setSeqs(SequenceI[] s)
   {
-    seqs = new Vector();
+    seqs = new Vector<SequenceI>();
 
     for (int i = 0; i < s.length; i++)
     {
@@ -266,13 +307,13 @@ public abstract class AlignFile extends FileParse
   /**
    * vector of String[] treeName, newickString pairs
    */
-  Vector newickStrings = null;
+  Vector<String[]> newickStrings = null;
 
   protected void addNewickTree(String treeName, String newickString)
   {
     if (newickStrings == null)
     {
-      newickStrings = new Vector();
+      newickStrings = new Vector<String[]>();
     }
     newickStrings.addElement(new String[]
     { treeName, newickString });