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