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