98868ce64edbae6634dd50e80337b580a6ed6613
[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 import java.util.Arrays;
26 import java.util.List;
27
28 public class DBRefEntry implements DBRefEntryI
29 {
30   /*
31    * the mapping to chromosome (genome) is held as an instance with
32    * source = speciesId
33    * version = assemblyId
34    * accessionId = "chromosome:" + chromosomeId
35    * map = mapping from sequence to reference assembly
36    */
37   public static final String CHROMOSOME = "chromosome";
38
39   String source = "";
40
41   String version = "";
42
43   String accessionId = "";
44
45   /**
46    * maps from associated sequence to the database sequence's coordinate system
47    */
48   Mapping map = null;
49
50   public DBRefEntry()
51   {
52
53   }
54
55   public DBRefEntry(String source, String version, String accessionId)
56   {
57     this(source, version, accessionId, null);
58   }
59
60   /**
61    * 
62    * @param source
63    *          canonical source (uppercase only)
64    * @param version
65    *          (source dependent version string)
66    * @param accessionId
67    *          (source dependent accession number string)
68    * @param map
69    *          (mapping from local sequence numbering to source accession
70    *          numbering)
71    */
72   public DBRefEntry(String source, String version, String accessionId,
73           Mapping map)
74   {
75     this.source = source.toUpperCase();
76     this.version = version;
77     this.accessionId = accessionId;
78     this.map = map;
79   }
80
81   public DBRefEntry(DBRefEntryI entry)
82   {
83     this((entry.getSource() == null ? "" : new String(entry.getSource())),
84             (entry.getVersion() == null ? ""
85                     : new String(entry.getVersion())),
86             (entry.getAccessionId() == null ? ""
87                     : new String(entry.getAccessionId())),
88             (entry.getMap() == null ? null : new Mapping(entry.getMap())));
89   }
90
91   @Override
92   public boolean equals(Object o)
93   {
94     // TODO should also override hashCode to ensure equal objects have equal
95     // hashcodes
96     if (o == null || !(o instanceof DBRefEntry))
97     {
98       return false;
99     }
100     DBRefEntry entry = (DBRefEntry) o;
101     if (entry == this)
102     {
103       return true;
104     }
105     if (equalRef(entry) && ((map == null && entry.map == null)
106             || (map != null && entry.map != null && map.equals(entry.map))))
107     {
108       return true;
109     }
110     return false;
111   }
112
113   /**
114    * Answers true if this object is either equivalent to, or can be 'improved'
115    * by, the given entry. Specifically, answers true if
116    * <ul>
117    * <li>source and accession are identical (ignoring case)</li>
118    * <li>version is identical (ignoring case), or this version is of the format
119    * "someSource:0", in which case the version for the other entry replaces
120    * it</li>
121    * <li>mappings are not compared but if this entry has no mapping, replace
122    * with that for the other entry</li>
123    * </ul>
124    * 
125    * @param other
126    * @return
127    */
128   @Override
129   public boolean updateFrom(DBRefEntryI other)
130   {
131     if (other == null)
132     {
133       return false;
134     }
135     if (other == this)
136     {
137       return true;
138     }
139
140     /*
141      * source must either match or be both null
142      */
143     String otherSource = other.getSource();
144     if ((source == null && otherSource != null)
145             || (source != null && otherSource == null)
146             || (source != null && !source.equalsIgnoreCase(otherSource)))
147     {
148       return false;
149     }
150
151     /*
152      * accession id must either match or be both null
153      */
154     String otherAccession = other.getAccessionId();
155     if ((accessionId == null && otherAccession != null)
156             || (accessionId != null && otherAccession == null)
157             || (accessionId != null
158                     && !accessionId.equalsIgnoreCase(otherAccession)))
159     {
160       return false;
161     }
162
163     /*
164      * if my version is null, "0" or "source:0" then replace with other version,
165      * otherwise the versions have to match
166      */
167     String otherVersion = other.getVersion();
168
169     if ((version == null || version.equals("0") || version.endsWith(":0"))
170             && otherVersion != null)
171     {
172       setVersion(otherVersion);
173     }
174     else
175     {
176       if (version != null && (otherVersion == null
177               || !version.equalsIgnoreCase(otherVersion)))
178       {
179         return false;
180       }
181     }
182
183     /*
184      * if I have no mapping, take that of the other dbref
185      */
186     if (map == null)
187     {
188       setMap(other.getMap());
189     }
190     return true;
191   }
192
193   /**
194    * test for similar DBRef attributes, except for the map object.
195    * 
196    * @param entry
197    * @return true if source, accession and version are equal with those of entry
198    */
199   @Override
200   public boolean equalRef(DBRefEntryI entry)
201   {
202     // TODO is this method and equals() not needed?
203     if (entry == null)
204     {
205       return false;
206     }
207     if (entry == this)
208     {
209       return true;
210     }
211     if (entry != null
212             && (source != null && entry.getSource() != null
213                     && source.equalsIgnoreCase(entry.getSource()))
214             && (accessionId != null && entry.getAccessionId() != null
215                     && accessionId.equalsIgnoreCase(entry.getAccessionId()))
216             && (version != null && entry.getVersion() != null
217                     && version.equalsIgnoreCase(entry.getVersion())))
218     {
219       return true;
220     }
221     return false;
222   }
223
224   @Override
225   public String getSource()
226   {
227     return source;
228   }
229
230   @Override
231   public String getVersion()
232   {
233     return version;
234   }
235
236   @Override
237   public String getAccessionId()
238   {
239     return accessionId;
240   }
241
242   @Override
243   public void setAccessionId(String accessionId)
244   {
245     this.accessionId = accessionId;
246   }
247
248   @Override
249   public void setSource(String source)
250   {
251     this.source = source;
252   }
253
254   @Override
255   public void setVersion(String version)
256   {
257     this.version = version;
258   }
259
260   @Override
261   public Mapping getMap()
262   {
263     return map;
264   }
265
266   /**
267    * @param map
268    *          the map to set
269    */
270   public void setMap(Mapping map)
271   {
272     this.map = map;
273   }
274
275   public boolean hasMap()
276   {
277     return map != null;
278   }
279
280   /**
281    * 
282    * @return source+":"+accessionId
283    */
284   public String getSrcAccString()
285   {
286     return ((source != null) ? source : "") + ":"
287             + ((accessionId != null) ? accessionId : "");
288   }
289
290   @Override
291   public String toString()
292   {
293     return getSrcAccString();
294   }
295
296   @Override
297   public boolean isPrimaryCandidate()
298   {
299     /*
300      * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.  
301      */
302     if (map != null)
303     {
304       if (map.getTo() != null)
305       {
306         return false;
307       }
308       if (map.getMap().getFromRatio() != map.getMap().getToRatio()
309               || map.getMap().getFromRatio() != 1)
310       {
311         return false;
312       }
313       // check map is between identical single contiguous ranges
314       List<int[]> fromRanges = map.getMap().getFromRanges();
315       List<int[]> toRanges = map.getMap().getToRanges();
316       if (fromRanges.size() != 1 || toRanges.size() != 1)
317       {
318         return false;
319       }
320       if (fromRanges.get(0)[0] != toRanges.get(0)[0]
321               || fromRanges.get(0)[1] != toRanges.get(0)[1])
322       {
323         return false;
324       }
325     }
326     if (version == null)
327     {
328       // no version string implies the reference has not been verified at all.
329       return false;
330     }
331     // tricky - this test really needs to search the sequence's set of dbrefs to
332     // see if there is a primary reference that derived this reference.
333     String ucv = version.toUpperCase();
334     for (String primsrc : Arrays.asList(DBRefSource.allSources()))
335     {
336       if (ucv.startsWith(primsrc.toUpperCase()))
337       {
338         // by convention, many secondary references inherit the primary
339         // reference's
340         // source string as a prefix for any version information from the
341         // secondary reference.
342         return false;
343       }
344     }
345     return true;
346   }
347
348   /**
349    * Mappings to chromosome are held with accessionId as "chromosome:id"
350    * 
351    * @return
352    */
353   public boolean isChromosome()
354   {
355     return accessionId != null && accessionId.startsWith(CHROMOSOME + ":");
356   }
357 }