JAL-3863 promote non-canonical version of dbref to a canonical one
[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 import jalview.util.DBRefUtils;
25 import jalview.util.MapList;
26
27 import java.util.List;
28
29 public class DBRefEntry implements DBRefEntryI
30 {
31   String source = "";
32
33   private String version = "";
34   
35   private String ucversion;
36
37   private String accessionId = "";
38   
39   int sourceKey = Integer.MIN_VALUE;
40
41   String canonicalSourceName;
42   
43   boolean isCanonicalAccession;
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   /**
56    * 
57    * @param source
58    *                      may not be null
59    * @param version
60    *                      may be null
61    * @param accessionId
62    *                      may be null
63    */
64   public DBRefEntry(String source, String version, String accessionId)
65   {
66     this(source, version, accessionId, null,false);
67   }
68
69   /**
70    * 
71    * @param source
72    *                      may not be null
73    * @param version
74    *                      may be null
75    * @param accessionId
76    *                      may be null
77    */
78   public DBRefEntry(String source, String version, String accessionId, Mapping map)
79   {
80     this(source, version, accessionId, map,false);
81   }
82   /**
83    * 
84    * @param source
85    *          canonical source (turned to uppercase; cannot be null)
86    * @param version
87    *          (source dependent version string or null)
88    * @param accessionId
89    *          (source dependent accession number string or null)
90    * @param map
91    *          (mapping from local sequence numbering to source accession
92    *          numbering or null)
93    */
94   public DBRefEntry(String source, String version, String accessionId,
95           Mapping map,boolean isCanonical)
96   {
97         
98     this.source = source.toUpperCase();
99     setVersion(version);
100     this.accessionId = accessionId;
101     this.map = map;
102     this.isCanonicalAccession=isCanonical;
103   }
104
105   /**
106    * Clone an entry, this time not allowing any null fields except map.
107    * 
108    */
109   public DBRefEntry(DBRefEntryI entry)
110   {
111     this((entry.getSource() == null ? "" : new String(entry.getSource())),
112             (entry.getVersion() == null ? ""
113                     : new String(entry.getVersion())),
114             (entry.getAccessionId() == null ? ""
115                     : new String(entry.getAccessionId())),
116             (entry.getMap() == null ? null : new Mapping(entry.getMap())),entry.isCanonical());
117   }
118
119   @Override
120   public boolean equals(Object o)
121   {
122     // TODO should also override hashCode to ensure equal objects have equal
123     // hashcodes
124           
125           
126 //    if (o == null || !(o instanceof DBRefEntry))
127 //    {
128 //      return false;
129 //    }
130 //    DBRefEntry entry = (DBRefEntry) o;
131 //    if (entry == this)
132 //    {
133 //      return true;
134 //    }
135     Mapping em;
136     return (o != null && o instanceof DBRefEntry 
137                 && (o == this 
138                 || equalRef((DBRefEntry) o) 
139                   && (map == null) == ((em = ((DBRefEntry) o).map) == null) 
140                   && (map == null || map.equals(em))));
141 //      
142 //    {
143 //      return true;
144 //    }
145 //    return false;
146   }
147
148   /**
149    * Answers true if this object is either equivalent to, or can be 'improved'
150    * by, the given entry. Specifically, answers true if
151    * <ul>
152    * <li>source and accession are identical (ignoring case)</li>
153    * <li>version is identical (ignoring case), or this version is of the format
154    * "someSource:0", in which case the version for the other entry replaces
155    * it</li>
156    * <li>mappings are not compared but if this entry has no mapping, replace
157    * with that for the other entry</li>
158    * </ul>
159    * 
160    * @param other
161    * @return
162    */
163   @Override
164   public boolean updateFrom(DBRefEntryI other)
165   {
166     if (other == null)
167     {
168       return false;
169     }
170     if (other == this)
171     {
172       return true;
173     }
174
175     boolean improved=false;
176     /*
177      * source must either match or be both null
178      */
179     String otherSource = other.getSource();
180     if ((source == null && otherSource != null)
181             || (source != null && otherSource == null)
182             || (source != null && !source.equalsIgnoreCase(otherSource)))
183     {
184       return false;
185     }
186
187     /*
188      * accession id must either match or be both null
189      */
190     String otherAccession = other.getAccessionId();
191     if ((accessionId == null && otherAccession != null)
192             || (accessionId != null && otherAccession == null)
193             || (accessionId != null
194                     && !accessionId.equalsIgnoreCase(otherAccession)))
195     {
196       return false;
197     }
198
199     if (!isCanonicalAccession && other.isCanonical())
200     {
201       isCanonicalAccession = true;
202       improved = true;
203     }
204     else
205     {
206       if (isCanonicalAccession && !other.isCanonical())
207       {
208         // other is not an authoritative source of canonical accessions
209         return false;
210       }
211     }
212     /*
213      * if my version is null, "0" or "source:0" then replace with other version,
214      * otherwise the versions have to match
215      */
216     String otherVersion = other.getVersion();
217
218     if ((version == null || version.equals("0") || version.endsWith(":0"))
219             && otherVersion != null)
220     {
221       setVersion(otherVersion);
222     }
223     else
224     {
225       if (version != null && (otherVersion == null
226               || !version.equalsIgnoreCase(otherVersion)))
227       {
228         // FIXME: there may be a problem with old version strings not allowing
229         // updating of dbrefentries
230         return improved;
231       }
232     }
233
234     /*
235      * if I have no mapping, take that of the other dbref 
236      * - providing it had a version and so do I
237      */
238     if (map == null)
239     {
240       setMap(other.getMap());
241     }
242     return true;
243   }
244
245   /**
246    * test for similar DBRef attributes, except for the map object.
247    * 
248    * @param entry
249    * @return true if source, accession and version are equal with those of entry
250    */
251   @Override
252   public boolean equalRef(DBRefEntryI entry)
253   {
254     // TODO is this method and equals() not needed?
255     if (entry == null)
256     {
257       return false;
258     }
259     if (entry == this)
260     {
261       return true;
262     }
263     
264     // BH 2019.01.25/2019.02.04  source cannot/should not be null. 
265     // for example, StructureChooser has dbRef.getSource().equalsIgnoreCase...
266     
267     return (entry != null
268             && (source != null && entry.getSource() != null
269                     && source.equalsIgnoreCase(entry.getSource()))
270             && (accessionId != null && entry.getAccessionId() != null
271                     && accessionId.equalsIgnoreCase(entry.getAccessionId()))
272             && (version != null && entry.getVersion() != null
273                     && version.equalsIgnoreCase(entry.getVersion())));
274   }
275
276   @Override
277   public String getSource()
278   {
279     return source;
280   }
281
282   public int getSourceKey() 
283   {
284         return (sourceKey == Integer.MIN_VALUE ? (sourceKey =  DBRefSource.getSourceKey(getCanonicalSourceName())) : sourceKey);
285   }
286
287   /**
288    * can be null
289    */
290   @Override
291   public String getVersion()
292   {
293     return version;
294   }
295
296   /**
297    * can be null
298    */
299   @Override
300   public String getAccessionId()
301   {
302     return accessionId;
303   }
304
305   @Override
306   public void setAccessionId(String accessionId)
307   {
308           this.accessionId = accessionId;
309 //    this.accessionId = (accessionId == null ? "" : accessionId).toUpperCase();
310   }
311
312   /**
313    * CAUTION! allows setting source null or not uppercase!
314    */
315   @Override
316   public void setSource(String source)
317   {
318           this.source = source;
319           
320 //    this.source = (source == null ? "" : source).toUpperCase();
321 //    this.canonicalSourceName =        DBRefUtils.getCanonicalName(this.source);
322 //    this.sourceKey = DBRefSource.getSourceKey(this.canonicalSourceName);
323   }
324
325   @Override
326   public void setVersion(String version)
327   {
328     this.version = version;
329     this.ucversion = (version == null ? null : version.toUpperCase());
330   }
331
332   @Override
333   public Mapping getMap()
334   {
335     return map;
336   }
337
338   /**
339    * @param map
340    *          the map to set
341    */
342   public void setMap(Mapping map)
343   {
344     this.map = map;
345   }
346
347   public boolean hasMap()
348   {
349     return map != null;
350   }
351
352   /**
353    * 
354    * @return source+":"+accessionId
355    */
356   public String getSrcAccString()
357   {
358     return ((source != null) ? source : "") + ":"
359             + ((accessionId != null) ? accessionId : "");
360   }
361
362   @Override
363   public String toString()
364   {
365     return getSrcAccString();
366   }
367
368   @Override
369   public boolean isPrimaryCandidate()
370   {
371     /*
372      * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.  
373      */
374     if (map != null)
375     {
376       SequenceI mto = map.getTo();
377       if (mto != null)
378       {
379         return false;
380       }
381       MapList ml = map.getMap();
382       if (ml.getFromRatio() != ml.getToRatio()
383               || ml.getFromRatio() != 1)
384       {
385         return false;
386       }
387       // check map is between identical single contiguous ranges
388       List<int[]> fromRanges, toRanges;
389       if ((fromRanges = ml.getFromRanges()).size() != 1 || (toRanges = ml.getToRanges()).size() != 1)
390       {
391         return false;
392       }
393       if (fromRanges.get(0)[0] != toRanges.get(0)[0]
394               || fromRanges.get(0)[1] != toRanges.get(0)[1])
395       {
396         return false;
397       }
398     }
399     if (version == null)
400     {
401       // no version string implies the reference has not been verified at all.
402       return false;
403     }
404     
405     return DBRefSource.isPrimaryCandidate(ucversion);
406   }
407
408   /**
409    * stores the upper-case canonical name of the source for use in
410    * Sequence.getPrimaryDBRefs().
411    * 
412    * @author Bob Hanson
413    * 
414    * @return
415    */
416   public String getCanonicalSourceName() {
417         return (canonicalSourceName == null ? (canonicalSourceName = DBRefUtils.getCanonicalName(this.source)) : canonicalSourceName);
418   }
419
420   /**
421    * 
422    * @param canonical
423    */
424   public void setCanonical(boolean canonical)
425   {
426     isCanonicalAccession = canonical;
427   }
428   /**
429    * 
430    * @return true if this is the primary canonical accession for the database source
431    */
432   public boolean isCanonical()
433   {
434     // TODO Auto-generated method stub
435     return isCanonicalAccession;
436   }
437 }