a641b1bfdd3300ed55c4d8044512dd27efa06448
[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    * maps from associated sequence to the database sequence's coordinate system
30    */
31   Mapping map = null;
32
33   public DBRefEntry()
34   {
35
36   }
37
38
39   public DBRefEntry(String source, String version, String accessionId)
40   {
41     this(source, version, accessionId, null);
42   }
43
44   /**
45    * 
46    * @param source
47    *          canonical source (uppercase only)
48    * @param version
49    *          (source dependent version string)
50    * @param accessionId
51    *          (source dependent accession number string)
52    * @param map
53    *          (mapping from local sequence numbering to source accession
54    *          numbering)
55    */
56   public DBRefEntry(String source, String version, String accessionId,
57           Mapping map)
58   {
59     this.source = source.toUpperCase();
60     this.version = version;
61     this.accessionId = accessionId;
62     this.map = map;
63   }
64
65   public DBRefEntry(DBRefEntryI entry)
66   {
67     this((entry.getSource() == null ? "" : new String(entry.getSource())),
68             (entry.getVersion() == null ? "" : new String(
69                     entry.getVersion())),
70             (entry.getAccessionId() == null ? "" : new String(
71                     entry.getAccessionId())),
72             (entry.getMap() == null ? null : new Mapping(entry.getMap())));
73   }
74
75   @Override
76   public boolean equals(Object o)
77   {
78     // TODO should also override hashCode to ensure equal objects have equal
79     // hashcodes
80     if (o == null || !(o instanceof DBRefEntry))
81     {
82       return false;
83     }
84     DBRefEntry entry = (DBRefEntry) o;
85     if (entry == this)
86     {
87       return true;
88     }
89     if (equalRef(entry)
90             && ((map == null && entry.map == null) || (map != null
91                     && entry.map != null && map.equals(entry.map))))
92     {
93       return true;
94     }
95     return false;
96   }
97
98   /**
99    * Answers true if this object is either equivalent to, or can be 'improved'
100    * by, the given entry. Specifically, answers true if
101    * <ul>
102    * <li>source and accession are identical (ignoring case)</li>
103    * <li>version is identical (ignoring case), or this version is of the format
104    * "someSource:0", in which case the version for the other entry replaces it</li>
105    * <li>mappings are not compared but if this entry has no mapping, replace
106    * with that for the other entry</li>
107    * </ul>
108    * 
109    * @param other
110    * @return
111    */
112   @Override
113   public boolean updateFrom(DBRefEntryI other)
114   {
115     if (other == null)
116     {
117       return false;
118     }
119     if (other == this)
120     {
121       return true;
122     }
123
124     /*
125      * source must either match or be both null
126      */
127     String otherSource = other.getSource();
128     if ((source == null && otherSource != null)
129             || (source != null && otherSource == null)
130             || (source != null && !source.equalsIgnoreCase(otherSource)))
131     {
132       return false;
133     }
134
135     /*
136      * accession id must either match or be both null
137      */
138     String otherAccession = other.getAccessionId();
139     if ((accessionId == null && otherAccession != null)
140             || (accessionId != null && otherAccession == null)
141             || (accessionId != null && !accessionId.equalsIgnoreCase(otherAccession)))
142     {
143       return false;
144     }
145
146     /*
147      * if my version is null, "0" or "source:0" then replace with other version,
148      * otherwise the versions have to match
149      */
150     String otherVersion = other.getVersion();
151       
152     if ((version == null || version.equals("0") || version.endsWith(":0"))
153             && otherVersion != null)
154     {
155       setVersion(otherVersion);
156     }
157     else
158     {
159       if (version != null
160               && (otherVersion == null || !version
161                       .equalsIgnoreCase(otherVersion)))
162       {
163         return false;
164       }
165     }
166
167     /*
168      * if I have no mapping, take that of the other dbref
169      */
170     if (map == null)
171     {
172       setMap(other.getMap());
173     }
174     return true;
175   }
176
177   /**
178    * test for similar DBRef attributes, except for the map object.
179    * 
180    * @param entry
181    * @return true if source, accession and version are equal with those of entry
182    */
183   @Override
184   public boolean equalRef(DBRefEntryI entry)
185   {
186     // TODO is this method and equals() not needed?
187     if (entry == null)
188     {
189       return false;
190     }
191     if (entry == this)
192     {
193       return true;
194     }
195     if (entry != null
196             && (source != null && entry.getSource() != null && source
197                     .equalsIgnoreCase(entry.getSource()))
198             && (accessionId != null && entry.getAccessionId() != null && accessionId
199                     .equalsIgnoreCase(entry.getAccessionId()))
200             && (version != null && entry.getVersion() != null && version
201                     .equalsIgnoreCase(entry.getVersion())))
202     {
203       return true;
204     }
205     return false;
206   }
207
208   @Override
209   public String getSource()
210   {
211     return source;
212   }
213
214   @Override
215   public String getVersion()
216   {
217     return version;
218   }
219
220   @Override
221   public String getAccessionId()
222   {
223     return accessionId;
224   }
225
226
227   @Override
228   public void setAccessionId(String accessionId)
229   {
230     this.accessionId = accessionId;
231   }
232
233
234   @Override
235   public void setSource(String source)
236   {
237     this.source = source;
238   }
239
240
241   @Override
242   public void setVersion(String version)
243   {
244     this.version = version;
245   }
246
247
248   @Override
249   public Mapping getMap()
250   {
251     return map;
252   }
253
254   /**
255    * @param map
256    *          the map to set
257    */
258   public void setMap(Mapping map)
259   {
260     this.map = map;
261   }
262
263   public boolean hasMap()
264   {
265     return map != null;
266   }
267
268   /**
269    * 
270    * @return source+":"+accessionId
271    */
272   public String getSrcAccString()
273   {
274     return ((source != null) ? source : "") + ":"
275             + ((accessionId != null) ? accessionId : "");
276   }
277
278   @Override
279   public String toString()
280   {
281     return getSrcAccString();
282   }
283 }