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, Mapping map)
82 this(source, version, accessionId, map,false);
87 * canonical source (turned to uppercase; cannot be null)
89 * (source dependent version string or null)
91 * (source dependent accession number string or null)
93 * (mapping from local sequence numbering to source accession
96 public DBRefEntry(String source, String version, String accessionId,
97 Mapping map,boolean isCanonical)
100 this.source = source.toUpperCase(Locale.ROOT);
102 this.accessionId = accessionId;
104 this.isCanonicalAccession=isCanonical;
108 * Clone an entry, this time not allowing any null fields except map.
111 public DBRefEntry(DBRefEntryI entry)
113 this((entry.getSource() == null ? "" : new String(entry.getSource())),
114 (entry.getVersion() == null ? ""
115 : new String(entry.getVersion())),
116 (entry.getAccessionId() == null ? ""
117 : new String(entry.getAccessionId())),
118 (entry.getMap() == null ? null : new Mapping(entry.getMap())),entry.isCanonical());
122 public boolean equals(Object o)
124 // TODO should also override hashCode to ensure equal objects have equal
128 // if (o == null || !(o instanceof DBRefEntry))
132 // DBRefEntry entry = (DBRefEntry) o;
133 // if (entry == this)
138 return (o != null && o instanceof DBRefEntry
140 || equalRef((DBRefEntry) o)
141 && (map == null) == ((em = ((DBRefEntry) o).map) == null)
142 && (map == null || map.equals(em))));
151 * Answers true if this object is either equivalent to, or can be 'improved'
152 * by, the given entry. Specifically, answers true if
154 * <li>source and accession are identical (ignoring case)</li>
155 * <li>version is identical (ignoring case), or this version is of the format
156 * "someSource:0", in which case the version for the other entry replaces
158 * <li>mappings are not compared but if this entry has no mapping, replace
159 * with that for the other entry</li>
166 public boolean updateFrom(DBRefEntryI other)
177 boolean improved=false;
179 * source must either match or be both null
181 String otherSource = other.getSource();
182 if ((source == null && otherSource != null)
183 || (source != null && otherSource == null)
184 || (source != null && !source.equalsIgnoreCase(otherSource)))
190 * accession id must either match or be both null
192 String otherAccession = other.getAccessionId();
193 if ((accessionId == null && otherAccession != null)
194 || (accessionId != null && otherAccession == null)
195 || (accessionId != null
196 && !accessionId.equalsIgnoreCase(otherAccession)))
201 if (!isCanonicalAccession && other.isCanonical())
203 isCanonicalAccession = true;
208 if (isCanonicalAccession && !other.isCanonical())
210 // other is not an authoritative source of canonical accessions
215 * if my version is null, "0" or "source:0" then replace with other version,
216 * otherwise the versions have to match
218 String otherVersion = other.getVersion();
220 if ((version == null || version.equals("0") || version.endsWith(":0"))
221 && otherVersion != null)
223 setVersion(otherVersion);
227 if (version != null && (otherVersion == null
228 || !version.equalsIgnoreCase(otherVersion)))
230 // FIXME: there may be a problem with old version strings not allowing
231 // updating of dbrefentries
237 * if I have no mapping, take that of the other dbref
238 * - providing it had a version and so do I
242 setMap(other.getMap());
248 * test for similar DBRef attributes, except for the map object.
251 * @return true if source, accession and version are equal with those of entry
254 public boolean equalRef(DBRefEntryI entry)
256 // TODO is this method and equals() not needed?
266 // BH 2019.01.25/2019.02.04 source cannot/should not be null.
267 // for example, StructureChooser has dbRef.getSource().equalsIgnoreCase...
269 return (entry != null
270 && (source != null && entry.getSource() != null
271 && source.equalsIgnoreCase(entry.getSource()))
272 && (accessionId != null && entry.getAccessionId() != null
273 && accessionId.equalsIgnoreCase(entry.getAccessionId()))
274 && (version != null && entry.getVersion() != null
275 && version.equalsIgnoreCase(entry.getVersion())));
279 public String getSource()
284 public int getSourceKey()
286 return (sourceKey == Integer.MIN_VALUE ? (sourceKey = DBRefSource.getSourceKey(getCanonicalSourceName())) : sourceKey);
293 public String getVersion()
302 public String getAccessionId()
308 public void setAccessionId(String accessionId)
310 this.accessionId = accessionId;
311 // this.accessionId = (accessionId == null ? "" : accessionId).toUpperCase(Locale.ROOT);
315 * CAUTION! allows setting source null or not uppercase!
318 public void setSource(String source)
320 this.source = source;
322 // this.source = (source == null ? "" : source).toUpperCase(Locale.ROOT);
323 // this.canonicalSourceName = DBRefUtils.getCanonicalName(this.source);
324 // this.sourceKey = DBRefSource.getSourceKey(this.canonicalSourceName);
328 public void setVersion(String version)
330 this.version = version;
331 this.ucversion = (version == null ? null : version.toUpperCase(Locale.ROOT));
335 public Mapping getMap()
344 public void setMap(Mapping map)
349 public boolean hasMap()
356 * @return source+":"+accessionId
358 public String getSrcAccString()
360 return ((source != null) ? source : "") + ":"
361 + ((accessionId != null) ? accessionId : "");
365 public String toString()
367 return getSrcAccString();
371 public boolean isPrimaryCandidate()
374 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
378 SequenceI mto = map.getTo();
383 MapList ml = map.getMap();
384 if (ml.getFromRatio() != ml.getToRatio()
385 || ml.getFromRatio() != 1)
389 // check map is between identical single contiguous ranges
390 List<int[]> fromRanges, toRanges;
391 if ((fromRanges = ml.getFromRanges()).size() != 1 || (toRanges = ml.getToRanges()).size() != 1)
395 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
396 || fromRanges.get(0)[1] != toRanges.get(0)[1])
403 // no version string implies the reference has not been verified at all.
407 return DBRefSource.isPrimaryCandidate(ucversion);
411 * stores the upper-case canonical name of the source for use in
412 * Sequence.getPrimaryDBRefs().
418 public String getCanonicalSourceName() {
419 return (canonicalSourceName == null ? (canonicalSourceName = DBRefUtils.getCanonicalName(this.source)) : canonicalSourceName);
426 public void setCanonical(boolean canonical)
428 isCanonicalAccession = canonical;
432 * @return true if this is the primary canonical accession for the database source
434 public boolean isCanonical()
436 // TODO Auto-generated method stub
437 return isCanonicalAccession;