2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.datamodel;
23 import java.util.Locale;
25 import jalview.api.DBRefEntryI;
26 import jalview.util.DBRefUtils;
27 import jalview.util.MapList;
29 import java.util.List;
31 public class DBRefEntry implements DBRefEntryI
35 private String version = "";
37 private String ucversion;
39 private String accessionId = "";
41 int sourceKey = Integer.MIN_VALUE;
43 String canonicalSourceName;
45 boolean isCanonicalAccession = false;
48 * maps from associated sequence to the database sequence's coordinate system
66 public DBRefEntry(String source, String version, String accessionId)
68 this(source, version, accessionId, null, false);
80 public DBRefEntry(String source, String version, String accessionId,
83 this(source, version, accessionId, map, false);
89 * canonical source (turned to uppercase; cannot be null)
91 * (source dependent version string or null)
93 * (source dependent accession number string or null)
95 * (mapping from local sequence numbering to source accession
98 public DBRefEntry(String source, String version, String accessionId,
99 Mapping map, boolean isCanonical)
102 this.source = source.toUpperCase(Locale.ROOT);
104 this.accessionId = accessionId;
106 this.isCanonicalAccession = isCanonical;
110 * Clone an entry, this time not allowing any null fields except map.
113 public DBRefEntry(DBRefEntryI entry)
115 this((entry.getSource() == null ? "" : new String(entry.getSource())),
116 (entry.getVersion() == null ? ""
117 : new String(entry.getVersion())),
118 (entry.getAccessionId() == null ? ""
119 : new String(entry.getAccessionId())),
120 (entry.getMap() == null ? null : new Mapping(entry.getMap())),
121 entry.isCanonical());
125 public boolean equals(Object o)
127 // TODO should also override hashCode to ensure equal objects have equal
130 // if (o == null || !(o instanceof DBRefEntry))
134 // DBRefEntry entry = (DBRefEntry) o;
135 // if (entry == this)
140 return (o != null && o instanceof DBRefEntry && (o == this || equalRef(
142 && (map == null) == ((em = ((DBRefEntry) o).map) == null)
143 && (map == null || map.equals(em))));
152 * Answers true if this object is either equivalent to, or can be 'improved'
153 * by, the given entry. Specifically, answers true if
155 * <li>source and accession are identical (ignoring case)</li>
156 * <li>version is identical (ignoring case), or this version is of the format
157 * "someSource:0", in which case the version for the other entry replaces
159 * <li>mappings are not compared but if this entry has no mapping, replace
160 * with that for the other entry</li>
167 public boolean updateFrom(DBRefEntryI other)
178 boolean improved = false;
180 * source must either match or be both null
182 String otherSource = other.getSource();
183 if ((source == null && otherSource != null)
184 || (source != null && otherSource == null)
185 || (source != null && !source.equalsIgnoreCase(otherSource)))
191 * accession id must either match or be both null
193 String otherAccession = other.getAccessionId();
194 if ((accessionId == null && otherAccession != null)
195 || (accessionId != null && otherAccession == null)
196 || (accessionId != null
197 && !accessionId.equalsIgnoreCase(otherAccession)))
202 if (!isCanonicalAccession && other.isCanonical())
204 isCanonicalAccession = true;
209 if (isCanonicalAccession && !other.isCanonical())
211 // other is not an authoritative source of canonical accessions
216 * if my version is null, "0" or "source:0" then replace with other version,
217 * otherwise the versions have to match
219 String otherVersion = other.getVersion();
221 if ((version == null || version.equals("0") || version.endsWith(":0"))
222 && otherVersion != null)
224 setVersion(otherVersion);
228 if (version != null && (otherVersion == null
229 || !version.equalsIgnoreCase(otherVersion)))
231 // FIXME: there may be a problem with old version strings not allowing
232 // updating of dbrefentries
238 * if I have no mapping, take that of the other dbref
239 * - providing it had a version and so do I
243 setMap(other.getMap());
249 * test for similar DBRef attributes, except for the map object.
252 * @return true if source, accession and version are equal with those of entry
255 public boolean equalRef(DBRefEntryI entry)
257 // TODO is this method and equals() not needed?
267 // BH 2019.01.25/2019.02.04 source cannot/should not be null.
268 // for example, StructureChooser has dbRef.getSource().equalsIgnoreCase...
270 return (entry != null
271 && (source != null && entry.getSource() != null
272 && source.equalsIgnoreCase(entry.getSource()))
273 && (accessionId != null && entry.getAccessionId() != null
274 && accessionId.equalsIgnoreCase(entry.getAccessionId()))
275 && (version != null && entry.getVersion() != null
276 && version.equalsIgnoreCase(entry.getVersion())));
280 public String getSource()
285 public int getSourceKey()
287 return (sourceKey == Integer.MIN_VALUE
288 ? (sourceKey = DBRefSource
289 .getSourceKey(getCanonicalSourceName()))
297 public String getVersion()
306 public String getAccessionId()
312 public void setAccessionId(String accessionId)
314 this.accessionId = accessionId;
315 // this.accessionId = (accessionId == null ? "" :
316 // accessionId).toUpperCase(Locale.ROOT);
320 * CAUTION! allows setting source null or not uppercase!
323 public void setSource(String source)
325 this.source = source;
327 // this.source = (source == null ? "" : source).toUpperCase(Locale.ROOT);
328 // this.canonicalSourceName = DBRefUtils.getCanonicalName(this.source);
329 // this.sourceKey = DBRefSource.getSourceKey(this.canonicalSourceName);
333 public void setVersion(String version)
335 this.version = version;
336 this.ucversion = (version == null ? null
337 : version.toUpperCase(Locale.ROOT));
341 public Mapping getMap()
350 public void setMap(Mapping map)
355 public boolean hasMap()
362 * @return source+":"+accessionId
364 public String getSrcAccString()
366 return ((source != null) ? source : "") + ":"
367 + ((accessionId != null) ? accessionId : "");
371 public String toString()
373 return getSrcAccString();
377 public boolean isPrimaryCandidate()
380 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
384 SequenceI mto = map.getTo();
389 MapList ml = map.getMap();
390 if (ml.getFromRatio() != ml.getToRatio() || ml.getFromRatio() != 1)
394 // check map is between identical single contiguous ranges
395 List<int[]> fromRanges, toRanges;
396 if ((fromRanges = ml.getFromRanges()).size() != 1
397 || (toRanges = ml.getToRanges()).size() != 1)
401 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
402 || fromRanges.get(0)[1] != toRanges.get(0)[1])
409 // no version string implies the reference has not been verified at all.
413 return DBRefSource.isPrimaryCandidate(ucversion);
417 * stores the upper-case canonical name of the source for use in
418 * Sequence.getPrimaryDBRefs().
424 public String getCanonicalSourceName()
426 return (canonicalSourceName == null
427 ? (canonicalSourceName = DBRefUtils
428 .getCanonicalName(this.source))
429 : canonicalSourceName);
436 public void setCanonical(boolean canonical)
438 isCanonicalAccession = canonical;
443 * @return true if this is the primary canonical accession for the database
446 public boolean isCanonical()
448 // TODO Auto-generated method stub
449 return isCanonicalAccession;