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