new search method for finding references like other references.
[jalview.git] / src / jalview / util / DBRefUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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 public class DBRefUtils
26 {
27   /**
28    * Utilities for handling DBRef objects and their collections.
29    */
30   /**
31    *
32    * @param dbrefs Vector of DBRef objects to search
33    * @param sources String[] array of source DBRef IDs to retrieve
34    * @return Vector
35    */
36   public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs, String[] sources)
37   {
38     if (dbrefs == null)
39     {
40       return null;
41     }
42     if (sources == null)
43     {
44       return dbrefs;
45     }
46     Hashtable srcs = new Hashtable();
47     Vector res = new Vector();
48
49     for (int i = 0; i < sources.length; i++)
50     {
51       srcs.put(new String(sources[i]), new Integer(i));
52     }
53     for (int i = 0, j = dbrefs.length; i < j; i++)
54     {
55       if (srcs.containsKey(dbrefs[i].getSource()))
56       {
57         res.addElement(dbrefs[i]);
58       }
59     }
60
61     if (res.size() > 0)
62     {
63       DBRefEntry[] reply = new DBRefEntry[res.size()];
64       for (int i = 0; i < res.size(); i++)
65       {
66         reply[i] = (DBRefEntry) res.elementAt(i);
67       }
68       return reply;
69     }
70     res = null;
71     // there are probable  memory leaks in the hashtable!
72     return null;
73   }
74
75   /**
76    * isDasCoordinateSystem
77    *
78    * @param string String
79    * @param dBRefEntry DBRefEntry
80    * @return boolean true if Source DBRefEntry is compatible with DAS CoordinateSystem name
81    */
82   public static Hashtable DasCoordinateSystemsLookup = null;
83   public static boolean isDasCoordinateSystem(String string,
84                                               DBRefEntry dBRefEntry)
85   {
86     if (DasCoordinateSystemsLookup == null)
87     { // Initialise
88       DasCoordinateSystemsLookup = new Hashtable();
89       DasCoordinateSystemsLookup.put("pdbresnum",
90                                      jalview.datamodel.DBRefSource.PDB);
91       DasCoordinateSystemsLookup.put("uniprot",
92                                      jalview.datamodel.DBRefSource.UNIPROT);
93       DasCoordinateSystemsLookup.put("EMBL",
94               jalview.datamodel.DBRefSource.EMBL);
95       //DasCoordinateSystemsLookup.put("EMBL",
96       //        jalview.datamodel.DBRefSource.EMBLCDS);
97     }
98
99     String coordsys = (String) DasCoordinateSystemsLookup.get(string.
100         toLowerCase());
101     if (coordsys != null)
102     {
103       return coordsys.equals(dBRefEntry.getSource());
104     }
105     return false;
106   }
107   public static Hashtable CanonicalSourceNameLookup=null;
108   /**
109    * look up source in an internal list of database reference sources
110    * and return the canonical jalview name for the source, or the original
111    * string if it has no canonical form.
112    * @param source
113    * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*) or original source
114    */
115   public static String getCanonicalName(String source)
116   {
117     if (CanonicalSourceNameLookup==null) {
118       CanonicalSourceNameLookup = new Hashtable();
119       CanonicalSourceNameLookup.put("uniprotkb/swiss-prot", jalview.datamodel.DBRefSource.UNIPROT);
120       CanonicalSourceNameLookup.put("pdb", jalview.datamodel.DBRefSource.PDB);
121     }
122     String canonical = (String) CanonicalSourceNameLookup.get(source.
123         toLowerCase());
124     if (canonical==null)
125     {
126       return source;
127     }
128     return canonical;
129   }
130   /**
131    * find RefEntry corresponding to a particular pattern
132    * the equals method of each entry is used, from String attributes right down to Mapping attributes.
133    * @param ref Set of references to search
134    * @param entry pattern to collect - null any entry for wildcard match
135    * @return
136    */
137   public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
138   {
139     if (ref==null || entry==null)
140       return null;
141     Vector rfs = new Vector();
142     for (int i=0; i<ref.length;i++)
143     {
144       if (entry.getSource()==null || ref[i].getSource().equals(entry.getSource()))
145       {
146         if (entry.getVersion()==null || ref[i].getVersion().equals(entry.getVersion()))
147         {
148           if (entry.getAccessionId()==null || ref[i].getAccessionId().equals(entry.getAccessionId()))
149           {
150             if (entry.getMap()==null || (ref[i].getMap()!=null && ref[i].getMap().equals(entry.getMap())))
151             {
152               rfs.addElement(ref[i]);
153             }
154           }
155         }
156       }
157     }
158     // TODO Auto-generated method stub
159     if (rfs.size()>0)
160     {
161       DBRefEntry[] rf = new DBRefEntry[rfs.size()];
162       rfs.copyInto(rf);
163       return rf;
164     }
165     return null;
166   }
167 }