Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.datamodel

File DBRefEntry.java

 

Coverage histogram

../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

48
60
21
1
386
208
63
1.05
2.86
21
3

Classes

Class Line # Actions
DBRefEntry 29 60 63
0.945736494.6%
 

Contributing tests

This file is covered by 76 tests. .

Source view

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    String source = "";
32   
33    private String version = "";
34   
35    private String ucversion;
36   
37    private String accessionId = "";
38   
39    int sourceKey = Integer.MIN_VALUE;
40   
41    String canonicalSourceName;
42   
43    /*
44    * maps from associated sequence to the database sequence's coordinate system
45    */
46    Mapping map = null;
47   
 
48  181 toggle public DBRefEntry()
49    {
50   
51    }
52   
53    /**
54    *
55    * @param source
56    * may not be null
57    * @param version
58    * may be null
59    * @param accessionId
60    * may be null
61    */
 
62  2884 toggle public DBRefEntry(String source, String version, String accessionId)
63    {
64  2884 this(source, version, accessionId, null);
65    }
66   
67    /**
68    *
69    * @param source
70    * canonical source (turned to uppercase; cannot be null)
71    * @param version
72    * (source dependent version string or null)
73    * @param accessionId
74    * (source dependent accession number string or null)
75    * @param map
76    * (mapping from local sequence numbering to source accession
77    * numbering or null)
78    */
 
79  3043 toggle public DBRefEntry(String source, String version, String accessionId,
80    Mapping map)
81    {
82   
83  3043 this.source = source.toUpperCase();
84  3043 setVersion(version);
85  3043 this.accessionId = accessionId;
86  3043 this.map = map;
87    }
88   
89    /**
90    * Clone an entry, this time not allowing any null fields except map.
91    *
92    */
 
93  19 toggle public DBRefEntry(DBRefEntryI entry)
94    {
95  19 this((entry.getSource() == null ? "" : new String(entry.getSource())),
96  19 (entry.getVersion() == null ? ""
97    : new String(entry.getVersion())),
98  19 (entry.getAccessionId() == null ? ""
99    : new String(entry.getAccessionId())),
100  19 (entry.getMap() == null ? null : new Mapping(entry.getMap())));
101    }
102   
 
103  34 toggle @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  34 Mapping em;
120  34 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  68236 toggle @Override
148    public boolean updateFrom(DBRefEntryI other)
149    {
150  68236 if (other == null)
151    {
152  1 return false;
153    }
154  68235 if (other == this)
155    {
156  1 return true;
157    }
158   
159    /*
160    * source must either match or be both null
161    */
162  68234 String otherSource = other.getSource();
163  68234 if ((source == null && otherSource != null)
164    || (source != null && otherSource == null)
165    || (source != null && !source.equalsIgnoreCase(otherSource)))
166    {
167  56410 return false;
168    }
169   
170    /*
171    * accession id must either match or be both null
172    */
173  11824 String otherAccession = other.getAccessionId();
174  11824 if ((accessionId == null && otherAccession != null)
175    || (accessionId != null && otherAccession == null)
176    || (accessionId != null
177    && !accessionId.equalsIgnoreCase(otherAccession)))
178    {
179  11691 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  133 String otherVersion = other.getVersion();
187   
188  133 if ((version == null || version.equals("0") || version.endsWith(":0"))
189    && otherVersion != null)
190    {
191  4 setVersion(otherVersion);
192    }
193    else
194    {
195  129 if (version != null && (otherVersion == null
196    || !version.equalsIgnoreCase(otherVersion)))
197    {
198  28 return false;
199    }
200    }
201   
202    /*
203    * if I have no mapping, take that of the other dbref
204    */
205  105 if (map == null)
206    {
207  25 setMap(other.getMap());
208    }
209  105 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  36 toggle @Override
219    public boolean equalRef(DBRefEntryI entry)
220    {
221    // TODO is this method and equals() not needed?
222  36 if (entry == null)
223    {
224  1 return false;
225    }
226  35 if (entry == this)
227    {
228  1 return true;
229    }
230   
231    // BH 2019.01.25/2019.02.04 source cannot/should not be null.
232    // for example, StructureChooser has dbRef.getSource().equalsIgnoreCase...
233   
234  34 return (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   
 
243  626703 toggle @Override
244    public String getSource()
245    {
246  626703 return source;
247    }
248   
 
249  604 toggle public int getSourceKey()
250    {
251  604 return (sourceKey == Integer.MIN_VALUE ? (sourceKey = DBRefSource.getSourceKey(getCanonicalSourceName())) : sourceKey);
252    }
253   
254    /**
255    * can be null
256    */
 
257  568 toggle @Override
258    public String getVersion()
259    {
260  568 return version;
261    }
262   
263    /**
264    * can be null
265    */
 
266  63494 toggle @Override
267    public String getAccessionId()
268    {
269  63494 return accessionId;
270    }
271   
 
272  183 toggle @Override
273    public void setAccessionId(String accessionId)
274    {
275  183 this.accessionId = accessionId;
276    // this.accessionId = (accessionId == null ? "" : accessionId).toUpperCase();
277    }
278   
279    /**
280    * CAUTION! allows setting source null or not uppercase!
281    */
 
282  170 toggle @Override
283    public void setSource(String source)
284    {
285  170 this.source = source;
286   
287    // this.source = (source == null ? "" : source).toUpperCase();
288    // this.canonicalSourceName = DBRefUtils.getCanonicalName(this.source);
289    // this.sourceKey = DBRefSource.getSourceKey(this.canonicalSourceName);
290    }
291   
 
292  3225 toggle @Override
293    public void setVersion(String version)
294    {
295  3225 this.version = version;
296  3225 this.ucversion = (version == null ? null : version.toUpperCase());
297    }
298   
 
299  3265 toggle @Override
300    public Mapping getMap()
301    {
302  3265 return map;
303    }
304   
305    /**
306    * @param map
307    * the map to set
308    */
 
309  167 toggle public void setMap(Mapping map)
310    {
311  167 this.map = map;
312    }
313   
 
314  942 toggle public boolean hasMap()
315    {
316  942 return map != null;
317    }
318   
319    /**
320    *
321    * @return source+":"+accessionId
322    */
 
323  1 toggle public String getSrcAccString()
324    {
325  1 return ((source != null) ? source : "") + ":"
326  1 + ((accessionId != null) ? accessionId : "");
327    }
328   
 
329  1 toggle @Override
330    public String toString()
331    {
332  1 return getSrcAccString();
333    }
334   
 
335  1167 toggle @Override
336    public boolean isPrimaryCandidate()
337    {
338    /*
339    * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
340    */
341  1167 if (map != null)
342    {
343  106 SequenceI mto = map.getTo();
344  106 if (mto != null)
345    {
346  69 return false;
347    }
348  37 MapList ml = map.getMap();
349  37 if (ml.getFromRatio() != ml.getToRatio()
350    || ml.getFromRatio() != 1)
351    {
352  4 return false;
353    }
354    // check map is between identical single contiguous ranges
355  33 List<int[]> fromRanges, toRanges;
356  ? if ((fromRanges = ml.getFromRanges()).size() != 1 || (toRanges = ml.getToRanges()).size() != 1)
357    {
358  24 return false;
359    }
360  9 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
361    || fromRanges.get(0)[1] != toRanges.get(0)[1])
362    {
363  3 return false;
364    }
365    }
366  1067 if (version == null)
367    {
368    // no version string implies the reference has not been verified at all.
369  1 return false;
370    }
371   
372  1066 return DBRefSource.isPrimaryCandidate(ucversion);
373    }
374   
375    /**
376    * stores the upper-case canonical name of the source for use in
377    * Sequence.getPrimaryDBRefs().
378    *
379    * @author Bob Hanson
380    *
381    * @return
382    */
 
383  382 toggle public String getCanonicalSourceName() {
384  382 return (canonicalSourceName == null ? (canonicalSourceName = DBRefUtils.getCanonicalName(this.source)) : canonicalSourceName);
385    }
386    }