Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.datamodel

File DBRefEntry.java

 

Coverage histogram

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

Code metrics

52
69
20
1
357
221
79
1.14
3.45
20
3.95

Classes

Class Line # Actions
DBRefEntry 28 69 79 7
0.9503546495%
 

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