DBRefs now array
[jalview.git] / src / jalview / util / DBRefUtils.java
1 package jalview.util;
2
3 import java.util.*;
4
5 import jalview.datamodel.*;
6
7
8 public class DBRefUtils
9 {
10     /**
11      * Utilities for handling DBRef objects and their collections.
12      */
13     /**
14      *
15      * @param dbrefs Vector of DBRef objects to search
16      * @param sources String[] array of source DBRef IDs to retrieve
17      * @return Vector
18      */
19     public static DBRefEntry [] selectRefs(DBRefEntry [] dbrefs, String[] sources) {
20       if (dbrefs==null)
21         return null;
22       if (sources==null)
23         return dbrefs;
24       Hashtable srcs = new Hashtable();
25       Vector res=new Vector();
26
27       for (int i=0; i<sources.length; i++)
28         srcs.put(new String(sources[i]), new Integer(i));
29       for (int i = 0, j = dbrefs.length; i < j; i++)
30         {
31           if (srcs.containsKey(dbrefs[i].getSource()))
32             res.add(dbrefs[i]);
33         }
34
35       if (res.size()>0)
36       {
37         DBRefEntry [] reply = new DBRefEntry[res.size()];
38         for(int i=0; i<res.size(); i++)
39           reply[i] = (DBRefEntry)res.elementAt(i);
40         return reply;
41       }
42       res = null;
43       // there are probable  memory leaks in the hashtable!
44       return null;
45     }
46
47   /**
48    * isDasCoordinateSystem
49    *
50    * @param string String
51    * @param dBRefEntry DBRefEntry
52    * @return boolean true if Source DBRefEntry is compatible with DAS CoordinateSystem name
53    */
54   public static Hashtable DasCoordinateSystemsLookup = null;
55   public static boolean isDasCoordinateSystem(String string,
56                                               DBRefEntry dBRefEntry)
57   {
58     if (DasCoordinateSystemsLookup==null)
59     { // Initialise
60       DasCoordinateSystemsLookup = new Hashtable();
61       DasCoordinateSystemsLookup.put("pdbresnum",
62                                      jalview.datamodel.DBRefSource.PDB);
63       DasCoordinateSystemsLookup.put("uniprot",
64                                      jalview.datamodel.DBRefSource.UNIPROT);
65     }
66
67     String coordsys = (String) DasCoordinateSystemsLookup.get(string.toLowerCase());
68     if (coordsys!=null)
69       return coordsys.equals(dBRefEntry.getSource());
70     return false;
71   }
72 }