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