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