JAL-1723 tooltip dbrefs sorted and limited to two per source
[jalview.git] / src / jalview / datamodel / DBRefEntry.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 import jalview.api.DBRefEntryI;
24
25 public class DBRefEntry implements DBRefEntryI
26 {
27   String source = "", version = "", accessionId = "";
28
29   private int startRes, endRes;
30   /**
31    * maps from associated sequence to the database sequence's coordinate system
32    */
33   Mapping map = null;
34
35   public DBRefEntry()
36   {
37
38   }
39
40
41   public DBRefEntry(String source, String version, String accessionId)
42   {
43     this(source, version, accessionId, null);
44   }
45
46   /**
47    * 
48    * @param source
49    *          canonical source (uppercase only)
50    * @param version
51    *          (source dependent version string)
52    * @param accessionId
53    *          (source dependent accession number string)
54    * @param map
55    *          (mapping from local sequence numbering to source accession
56    *          numbering)
57    */
58   public DBRefEntry(String source, String version, String accessionId,
59           Mapping map)
60   {
61     this.source = source.toUpperCase();
62     this.version = version;
63     this.accessionId = accessionId;
64     this.map = map;
65   }
66
67   public DBRefEntry(DBRefEntryI entry)
68   {
69     this((entry.getSource() == null ? "" : new String(entry.getSource())),
70             (entry.getVersion() == null ? "" : new String(
71                     entry.getVersion())),
72             (entry.getAccessionId() == null ? "" : new String(
73                     entry.getAccessionId())),
74             (entry.getMap() == null ? null : new Mapping(entry.getMap())));
75   }
76
77   @Override
78   public boolean equals(Object o)
79   {
80     // TODO should also override hashCode to ensure equal objects have equal
81     // hashcodes
82     if (o == null || !(o instanceof DBRefEntry))
83     {
84       return false;
85     }
86     DBRefEntry entry = (DBRefEntry) o;
87     if (entry == this)
88     {
89       return true;
90     }
91     if (equalRef(entry)
92             && ((map == null && entry.map == null) || (map != null
93                     && entry.map != null && map.equals(entry.map))))
94     {
95       return true;
96     }
97     return false;
98   }
99
100   /**
101    * test for similar DBRef attributes, except for the map object.
102    * 
103    * @param entry
104    * @return true if source, accession and version are equal with those of entry
105    */
106   @Override
107   public boolean equalRef(DBRefEntryI entry)
108   {
109     if (entry == null)
110     {
111       return false;
112     }
113     if (entry == this)
114     {
115       return true;
116     }
117     if (entry != null
118             && (source != null && entry.getSource() != null && source
119                     .equalsIgnoreCase(entry.getSource()))
120             && (accessionId != null && entry.getAccessionId() != null && accessionId
121                     .equalsIgnoreCase(entry.getAccessionId()))
122             && (version != null && entry.getVersion() != null && version
123                     .equalsIgnoreCase(entry.getVersion())))
124     {
125       return true;
126     }
127     return false;
128   }
129
130   @Override
131   public String getSource()
132   {
133     return source;
134   }
135
136   @Override
137   public String getVersion()
138   {
139     return version;
140   }
141
142   @Override
143   public String getAccessionId()
144   {
145     return accessionId;
146   }
147
148
149   @Override
150   public void setAccessionId(String accessionId)
151   {
152     this.accessionId = accessionId;
153   }
154
155
156   @Override
157   public void setSource(String source)
158   {
159     this.source = source;
160   }
161
162
163   @Override
164   public void setVersion(String version)
165   {
166     this.version = version;
167   }
168
169
170   @Override
171   public Mapping getMap()
172   {
173     return map;
174   }
175
176   /**
177    * @param map
178    *          the map to set
179    */
180   public void setMap(Mapping map)
181   {
182     this.map = map;
183   }
184
185   public boolean hasMap()
186   {
187     return map != null;
188   }
189
190   /**
191    * 
192    * @return source+":"+accessionId
193    */
194   public String getSrcAccString()
195   {
196     return ((source != null) ? source : "") + ":"
197             + ((accessionId != null) ? accessionId : "");
198   }
199
200   @Override
201   public String toString()
202   {
203     return getSrcAccString();
204   }
205
206   @Override
207   public int getStartRes()
208   {
209     return startRes;
210   }
211
212   @Override
213   public void setStartRes(int startRes)
214   {
215     this.startRes = startRes;
216   }
217
218   @Override
219   public int getEndRes()
220   {
221     return endRes;
222   }
223
224   @Override
225   public void setEndRes(int endRes)
226   {
227     this.endRes = endRes;
228   }
229 }