1.1 methods
[jalview.git] / src / jalview / util / DBRefUtils.java
index 5aed125..3ea47f2 100755 (executable)
@@ -1,6 +1,6 @@
 /*
  * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -22,45 +22,55 @@ import java.util.*;
 
 import jalview.datamodel.*;
 
-
 public class DBRefUtils
 {
-    /**
-     * Utilities for handling DBRef objects and their collections.
-     */
-    /**
-     *
-     * @param dbrefs Vector of DBRef objects to search
-     * @param sources String[] array of source DBRef IDs to retrieve
-     * @return Vector
-     */
-    public static DBRefEntry [] selectRefs(DBRefEntry [] dbrefs, String[] sources) {
-      if (dbrefs==null)
-        return null;
-      if (sources==null)
-        return dbrefs;
-      Hashtable srcs = new Hashtable();
-      Vector res=new Vector();
+  /**
+   * Utilities for handling DBRef objects and their collections.
+   */
+  /**
+   *
+   * @param dbrefs Vector of DBRef objects to search
+   * @param sources String[] array of source DBRef IDs to retrieve
+   * @return Vector
+   */
+  public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs, String[] sources)
+  {
+    if (dbrefs == null)
+    {
+      return null;
+    }
+    if (sources == null)
+    {
+      return dbrefs;
+    }
+    Hashtable srcs = new Hashtable();
+    Vector res = new Vector();
 
-      for (int i=0; i<sources.length; i++)
-        srcs.put(new String(sources[i]), new Integer(i));
-      for (int i = 0, j = dbrefs.length; i < j; i++)
-        {
-          if (srcs.containsKey(dbrefs[i].getSource()))
-            res.add(dbrefs[i]);
-        }
+    for (int i = 0; i < sources.length; i++)
+    {
+      srcs.put(new String(sources[i]), new Integer(i));
+    }
+    for (int i = 0, j = dbrefs.length; i < j; i++)
+    {
+      if (srcs.containsKey(dbrefs[i].getSource()))
+      {
+        res.addElement(dbrefs[i]);
+      }
+    }
 
-      if (res.size()>0)
+    if (res.size() > 0)
+    {
+      DBRefEntry[] reply = new DBRefEntry[res.size()];
+      for (int i = 0; i < res.size(); i++)
       {
-        DBRefEntry [] reply = new DBRefEntry[res.size()];
-        for(int i=0; i<res.size(); i++)
-          reply[i] = (DBRefEntry)res.elementAt(i);
-        return reply;
+        reply[i] = (DBRefEntry) res.elementAt(i);
       }
-      res = null;
-      // there are probable  memory leaks in the hashtable!
-      return null;
+      return reply;
     }
+    res = null;
+    // there are probable  memory leaks in the hashtable!
+    return null;
+  }
 
   /**
    * isDasCoordinateSystem
@@ -73,18 +83,48 @@ public class DBRefUtils
   public static boolean isDasCoordinateSystem(String string,
                                               DBRefEntry dBRefEntry)
   {
-    if (DasCoordinateSystemsLookup==null)
+    if (DasCoordinateSystemsLookup == null)
     { // Initialise
       DasCoordinateSystemsLookup = new Hashtable();
       DasCoordinateSystemsLookup.put("pdbresnum",
                                      jalview.datamodel.DBRefSource.PDB);
       DasCoordinateSystemsLookup.put("uniprot",
                                      jalview.datamodel.DBRefSource.UNIPROT);
+      DasCoordinateSystemsLookup.put("EMBL",
+              jalview.datamodel.DBRefSource.EMBL);
+      //DasCoordinateSystemsLookup.put("EMBL",
+      //        jalview.datamodel.DBRefSource.EMBLCDS);
     }
 
-    String coordsys = (String) DasCoordinateSystemsLookup.get(string.toLowerCase());
-    if (coordsys!=null)
+    String coordsys = (String) DasCoordinateSystemsLookup.get(string.
+        toLowerCase());
+    if (coordsys != null)
+    {
       return coordsys.equals(dBRefEntry.getSource());
+    }
     return false;
   }
+  public static Hashtable CanonicalSourceNameLookup=null;
+  /**
+   * look up source in an internal list of database reference sources
+   * and return the canonical jalview name for the source, or the original
+   * string if it has no canonical form.
+   * @param source
+   * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*) or original source
+   */
+  public static String getCanonicalName(String source)
+  {
+    if (CanonicalSourceNameLookup==null) {
+      CanonicalSourceNameLookup = new Hashtable();
+      CanonicalSourceNameLookup.put("uniprotkb/swiss-prot", jalview.datamodel.DBRefSource.UNIPROT);
+      CanonicalSourceNameLookup.put("pdb", jalview.datamodel.DBRefSource.PDB);
+    }
+    String canonical = (String) CanonicalSourceNameLookup.get(source.
+        toLowerCase());
+    if (canonical==null)
+    {
+      return source;
+    }
+    return canonical;
+  }
 }