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