JAL-3863 passing test to verify a uniprot accession with canonical flag set is transf...
[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     /*
176      * source must either match or be both null
177      */
178     String otherSource = other.getSource();
179     if ((source == null && otherSource != null)
180             || (source != null && otherSource == null)
181             || (source != null && !source.equalsIgnoreCase(otherSource)))
182     {
183       return false;
184     }
185
186     /*
187      * accession id must either match or be both null
188      */
189     String otherAccession = other.getAccessionId();
190     if ((accessionId == null && otherAccession != null)
191             || (accessionId != null && otherAccession == null)
192             || (accessionId != null
193                     && !accessionId.equalsIgnoreCase(otherAccession)))
194     {
195       return false;
196     }
197
198     /*
199      * if my version is null, "0" or "source:0" then replace with other version,
200      * otherwise the versions have to match
201      */
202     String otherVersion = other.getVersion();
203
204     if ((version == null || version.equals("0") || version.endsWith(":0"))
205             && otherVersion != null)
206     {
207       setVersion(otherVersion);
208     }
209     else
210     {
211       if (version != null && (otherVersion == null
212               || !version.equalsIgnoreCase(otherVersion)))
213       {
214         return false;
215       }
216     }
217
218     /*
219      * if I have no mapping, take that of the other dbref
220      */
221     if (map == null)
222     {
223       setMap(other.getMap());
224     }
225     return true;
226   }
227
228   /**
229    * test for similar DBRef attributes, except for the map object.
230    * 
231    * @param entry
232    * @return true if source, accession and version are equal with those of entry
233    */
234   @Override
235   public boolean equalRef(DBRefEntryI entry)
236   {
237     // TODO is this method and equals() not needed?
238     if (entry == null)
239     {
240       return false;
241     }
242     if (entry == this)
243     {
244       return true;
245     }
246     
247     // BH 2019.01.25/2019.02.04  source cannot/should not be null. 
248     // for example, StructureChooser has dbRef.getSource().equalsIgnoreCase...
249     
250     return (entry != null
251             && (source != null && entry.getSource() != null
252                     && source.equalsIgnoreCase(entry.getSource()))
253             && (accessionId != null && entry.getAccessionId() != null
254                     && accessionId.equalsIgnoreCase(entry.getAccessionId()))
255             && (version != null && entry.getVersion() != null
256                     && version.equalsIgnoreCase(entry.getVersion())));
257   }
258
259   @Override
260   public String getSource()
261   {
262     return source;
263   }
264
265   public int getSourceKey() 
266   {
267         return (sourceKey == Integer.MIN_VALUE ? (sourceKey =  DBRefSource.getSourceKey(getCanonicalSourceName())) : sourceKey);
268   }
269
270   /**
271    * can be null
272    */
273   @Override
274   public String getVersion()
275   {
276     return version;
277   }
278
279   /**
280    * can be null
281    */
282   @Override
283   public String getAccessionId()
284   {
285     return accessionId;
286   }
287
288   @Override
289   public void setAccessionId(String accessionId)
290   {
291           this.accessionId = accessionId;
292 //    this.accessionId = (accessionId == null ? "" : accessionId).toUpperCase();
293   }
294
295   /**
296    * CAUTION! allows setting source null or not uppercase!
297    */
298   @Override
299   public void setSource(String source)
300   {
301           this.source = source;
302           
303 //    this.source = (source == null ? "" : source).toUpperCase();
304 //    this.canonicalSourceName =        DBRefUtils.getCanonicalName(this.source);
305 //    this.sourceKey = DBRefSource.getSourceKey(this.canonicalSourceName);
306   }
307
308   @Override
309   public void setVersion(String version)
310   {
311     this.version = version;
312     this.ucversion = (version == null ? null : version.toUpperCase());
313   }
314
315   @Override
316   public Mapping getMap()
317   {
318     return map;
319   }
320
321   /**
322    * @param map
323    *          the map to set
324    */
325   public void setMap(Mapping map)
326   {
327     this.map = map;
328   }
329
330   public boolean hasMap()
331   {
332     return map != null;
333   }
334
335   /**
336    * 
337    * @return source+":"+accessionId
338    */
339   public String getSrcAccString()
340   {
341     return ((source != null) ? source : "") + ":"
342             + ((accessionId != null) ? accessionId : "");
343   }
344
345   @Override
346   public String toString()
347   {
348     return getSrcAccString();
349   }
350
351   @Override
352   public boolean isPrimaryCandidate()
353   {
354     /*
355      * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.  
356      */
357     if (map != null)
358     {
359       SequenceI mto = map.getTo();
360       if (mto != null)
361       {
362         return false;
363       }
364       MapList ml = map.getMap();
365       if (ml.getFromRatio() != ml.getToRatio()
366               || ml.getFromRatio() != 1)
367       {
368         return false;
369       }
370       // check map is between identical single contiguous ranges
371       List<int[]> fromRanges, toRanges;
372       if ((fromRanges = ml.getFromRanges()).size() != 1 || (toRanges = ml.getToRanges()).size() != 1)
373       {
374         return false;
375       }
376       if (fromRanges.get(0)[0] != toRanges.get(0)[0]
377               || fromRanges.get(0)[1] != toRanges.get(0)[1])
378       {
379         return false;
380       }
381     }
382     if (version == null)
383     {
384       // no version string implies the reference has not been verified at all.
385       return false;
386     }
387     
388     return DBRefSource.isPrimaryCandidate(ucversion);
389   }
390
391   /**
392    * stores the upper-case canonical name of the source for use in
393    * Sequence.getPrimaryDBRefs().
394    * 
395    * @author Bob Hanson
396    * 
397    * @return
398    */
399   public String getCanonicalSourceName() {
400         return (canonicalSourceName == null ? (canonicalSourceName = DBRefUtils.getCanonicalName(this.source)) : canonicalSourceName);
401   }
402
403   /**
404    * 
405    * @param canonical
406    */
407   public void setCanonical(boolean canonical)
408   {
409     isCanonicalAccession = canonical;
410   }
411   /**
412    * 
413    * @return true if this is the primary canonical accession for the database source
414    */
415   public boolean isCanonical()
416   {
417     // TODO Auto-generated method stub
418     return isCanonicalAccession;
419   }
420 }