header updated
[jalview.git] / src / jalview / util / DBRefUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.util;
20
21 import java.util.*;
22
23 import jalview.datamodel.*;
24
25
26 public class DBRefUtils
27 {
28     /**
29      * Utilities for handling DBRef objects and their collections.
30      */
31     /**
32      *
33      * @param dbrefs Vector of DBRef objects to search
34      * @param sources String[] array of source DBRef IDs to retrieve
35      * @return Vector
36      */
37     public static DBRefEntry [] selectRefs(DBRefEntry [] dbrefs, String[] sources) {
38       if (dbrefs==null)
39         return null;
40       if (sources==null)
41         return dbrefs;
42       Hashtable srcs = new Hashtable();
43       Vector res=new Vector();
44
45       for (int i=0; i<sources.length; i++)
46         srcs.put(new String(sources[i]), new Integer(i));
47       for (int i = 0, j = dbrefs.length; i < j; i++)
48         {
49           if (srcs.containsKey(dbrefs[i].getSource()))
50             res.add(dbrefs[i]);
51         }
52
53       if (res.size()>0)
54       {
55         DBRefEntry [] reply = new DBRefEntry[res.size()];
56         for(int i=0; i<res.size(); i++)
57           reply[i] = (DBRefEntry)res.elementAt(i);
58         return reply;
59       }
60       res = null;
61       // there are probable  memory leaks in the hashtable!
62       return null;
63     }
64
65   /**
66    * isDasCoordinateSystem
67    *
68    * @param string String
69    * @param dBRefEntry DBRefEntry
70    * @return boolean true if Source DBRefEntry is compatible with DAS CoordinateSystem name
71    */
72   public static Hashtable DasCoordinateSystemsLookup = null;
73   public static boolean isDasCoordinateSystem(String string,
74                                               DBRefEntry dBRefEntry)
75   {
76     if (DasCoordinateSystemsLookup==null)
77     { // Initialise
78       DasCoordinateSystemsLookup = new Hashtable();
79       DasCoordinateSystemsLookup.put("pdbresnum",
80                                      jalview.datamodel.DBRefSource.PDB);
81       DasCoordinateSystemsLookup.put("uniprot",
82                                      jalview.datamodel.DBRefSource.UNIPROT);
83     }
84
85     String coordsys = (String) DasCoordinateSystemsLookup.get(string.toLowerCase());
86     if (coordsys!=null)
87       return coordsys.equals(dBRefEntry.getSource());
88     return false;
89   }
90 }