2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
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.
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.
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
23 import jalview.datamodel.*;
25 public class DBRefUtils
28 * Utilities for handling DBRef objects and their collections.
32 * @param dbrefs Vector of DBRef objects to search
33 * @param sources String[] array of source DBRef IDs to retrieve
36 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs, String[] sources)
46 Hashtable srcs = new Hashtable();
47 Vector res = new Vector();
49 for (int i = 0; i < sources.length; i++)
51 srcs.put(new String(sources[i]), new Integer(i));
53 for (int i = 0, j = dbrefs.length; i < j; i++)
55 if (srcs.containsKey(dbrefs[i].getSource()))
57 res.addElement(dbrefs[i]);
63 DBRefEntry[] reply = new DBRefEntry[res.size()];
64 for (int i = 0; i < res.size(); i++)
66 reply[i] = (DBRefEntry) res.elementAt(i);
71 // there are probable memory leaks in the hashtable!
76 * isDasCoordinateSystem
78 * @param string String
79 * @param dBRefEntry DBRefEntry
80 * @return boolean true if Source DBRefEntry is compatible with DAS CoordinateSystem name
82 public static Hashtable DasCoordinateSystemsLookup = null;
83 public static boolean isDasCoordinateSystem(String string,
84 DBRefEntry dBRefEntry)
86 if (DasCoordinateSystemsLookup == null)
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);
99 String coordsys = (String) DasCoordinateSystemsLookup.get(string.
101 if (coordsys != null)
103 return coordsys.equals(dBRefEntry.getSource());
107 public static Hashtable CanonicalSourceNameLookup=null;
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.
113 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*) or original source
115 public static String getCanonicalName(String source)
117 if (CanonicalSourceNameLookup==null) {
118 CanonicalSourceNameLookup = new Hashtable();
119 CanonicalSourceNameLookup.put("uniprotkb/swiss-prot", jalview.datamodel.DBRefSource.UNIPROT);
120 CanonicalSourceNameLookup.put("uniprotkb/trembl", jalview.datamodel.DBRefSource.UNIPROT);
121 CanonicalSourceNameLookup.put("pdb", jalview.datamodel.DBRefSource.PDB);
123 String canonical = (String) CanonicalSourceNameLookup.get(source.
132 * find RefEntry corresponding to a particular pattern
133 * the equals method of each entry is used, from String attributes right down to Mapping attributes.
134 * @param ref Set of references to search
135 * @param entry pattern to collect - null any entry for wildcard match
138 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
140 return searchRefs(ref, entry, matchDbAndIdAndEitherMapOrEquivalentMapList);
142 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry, DbRefComp comparator)
144 if (ref==null || entry==null)
146 Vector rfs = new Vector();
147 for (int i=0; i<ref.length;i++)
149 if (comparator.matches(entry, ref[i]))
151 rfs.addElement(ref[i]);
154 // TODO Auto-generated method stub
157 DBRefEntry[] rf = new DBRefEntry[rfs.size()];
163 public interface DbRefComp {
164 public boolean matches(DBRefEntry refa, DBRefEntry refb);
166 public static DbRefComp matchNonNullonA = new DbRefComp()
168 public boolean matches(DBRefEntry refa, DBRefEntry refb)
170 if (refa.getSource()==null || refb.getSource().equals(refa.getSource()))
172 if (refa.getVersion()==null || refb.getVersion().equals(refa.getVersion()))
174 if (refa.getAccessionId()==null || refb.getAccessionId().equals(refa.getAccessionId()))
176 if (refa.getMap()==null || (refb.getMap()!=null && refb.getMap().equals(refa.getMap())))
187 * either field is null or field matches for all of source, version, accession id and map.
189 public static DbRefComp matchEitherNonNull = new DbRefComp()
191 public boolean matches(DBRefEntry refa, DBRefEntry refb)
193 if ((refa.getSource()==null || refb.getSource()==null)
194 || refb.getSource().equals(refa.getSource()))
196 if ((refa.getVersion()==null || refb.getVersion()==null)
197 || refb.getVersion().equals(refa.getVersion()))
199 if ((refa.getAccessionId()==null || refb.getAccessionId()==null)
200 || refb.getAccessionId().equals(refa.getAccessionId()))
202 if ((refa.getMap()==null || refb.getMap()==null)
203 || (refb.getMap()!=null && refb.getMap().equals(refa.getMap())))
214 * accession ID and DB must be identical. Version is ignored. Map is either not defined or is a match (or is compatible?)
216 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
218 public boolean matches(DBRefEntry refa, DBRefEntry refb)
220 if (refa.getSource()!=null && refb.getSource()!=null
221 && refb.getSource().equals(refa.getSource()))
223 // We dont care about version
224 //if ((refa.getVersion()==null || refb.getVersion()==null)
225 // || refb.getVersion().equals(refa.getVersion()))
227 if (refa.getAccessionId()!=null && refb.getAccessionId()!=null
228 || refb.getAccessionId().equals(refa.getAccessionId()))
230 if ((refa.getMap()==null || refb.getMap()==null)
231 || (refa.getMap()!=null && refb.getMap()!=null && refb.getMap().equals(refa.getMap())))
241 * accession ID and DB must be identical. Version is ignored.
242 * No map on either or map but no maplist on either or maplist of map on a is the complement of maplist of map on b.
244 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
246 public boolean matches(DBRefEntry refa, DBRefEntry refb)
248 if (refa.getSource()!=null && refb.getSource()!=null
249 && refb.getSource().equals(refa.getSource()))
251 // We dont care about version
252 //if ((refa.getVersion()==null || refb.getVersion()==null)
253 // || refb.getVersion().equals(refa.getVersion()))
255 if (refa.getAccessionId()!=null && refb.getAccessionId()!=null
256 || refb.getAccessionId().equals(refa.getAccessionId()))
258 if ((refa.getMap()==null && refb.getMap()==null)
259 || (refa.getMap()!=null && refb.getMap()!=null))
260 if ((refb.getMap().getMap()==null && refa.getMap().getMap()==null)
261 || (refb.getMap().getMap()!=null && refa.getMap().getMap()!=null
262 && refb.getMap().getMap().getInverse().equals(refa.getMap().getMap())))
272 * accession ID and DB must be identical. Version is ignored.
273 * No map on both or or map but no maplist on either or maplist of map on a is equivalent to the maplist of map on b.
275 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
277 public boolean matches(DBRefEntry refa, DBRefEntry refb)
279 if (refa.getSource()!=null && refb.getSource()!=null
280 && refb.getSource().equals(refa.getSource()))
282 // We dont care about version
283 //if ((refa.getVersion()==null || refb.getVersion()==null)
284 // || refb.getVersion().equals(refa.getVersion()))
286 if (refa.getAccessionId()!=null && refb.getAccessionId()!=null
287 || refb.getAccessionId().equals(refa.getAccessionId()))
289 if (refa.getMap()==null && refb.getMap()==null)
293 if (refa.getMap()!=null && refb.getMap()!=null
294 && ((refb.getMap().getMap()==null && refa.getMap().getMap()==null)
295 || (refb.getMap().getMap()!=null && refa.getMap().getMap()!=null
296 && refb.getMap().getMap().equals(refa.getMap().getMap()))))
306 * accession ID and DB must be identical. Version is ignored.
307 * No map on either or map but no maplist on either or maplist of map on a is equivalent to the maplist of map on b.
309 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
311 public boolean matches(DBRefEntry refa, DBRefEntry refb)
313 if (refa.getSource()!=null && refb.getSource()!=null
314 && refb.getSource().equals(refa.getSource()))
316 // We dont care about version
317 //if ((refa.getVersion()==null || refb.getVersion()==null)
318 // || refb.getVersion().equals(refa.getVersion()))
320 if (refa.getAccessionId()!=null && refb.getAccessionId()!=null
321 && refb.getAccessionId().equals(refa.getAccessionId()))
323 if (refa.getMap()==null || refb.getMap()==null)
327 if ((refa.getMap()!=null && refb.getMap()!=null)
328 && (refb.getMap().getMap()==null && refa.getMap().getMap()==null)
329 || (refb.getMap().getMap()!=null && refa.getMap().getMap()!=null
330 && refb.getMap().getMap().equals(refa.getMap().getMap())))