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 jalview.api.DBRefEntryI;
24 import jalview.util.DBRefUtils;
25 import jalview.util.MapList;
27 import java.util.List;
29 public class DBRefEntry implements DBRefEntryI
33 private String version = "";
35 private String ucversion;
37 private String accessionId = "";
39 int sourceKey = Integer.MIN_VALUE;
41 String canonicalSourceName;
43 boolean isCanonicalAccession;
46 * maps from associated sequence to the database sequence's coordinate system
64 public DBRefEntry(String source, String version, String accessionId)
66 this(source, version, accessionId, null,false);
78 public DBRefEntry(String source, String version, String accessionId, Mapping map)
80 this(source, version, accessionId, map,false);
85 * canonical source (turned to uppercase; cannot be null)
87 * (source dependent version string or null)
89 * (source dependent accession number string or null)
91 * (mapping from local sequence numbering to source accession
94 public DBRefEntry(String source, String version, String accessionId,
95 Mapping map,boolean isCanonical)
98 this.source = source.toUpperCase();
100 this.accessionId = accessionId;
102 this.isCanonicalAccession=isCanonical;
106 * Clone an entry, this time not allowing any null fields except map.
109 public DBRefEntry(DBRefEntryI entry)
111 this((entry.getSource() == null ? "" : new String(entry.getSource())),
112 (entry.getVersion() == null ? ""
113 : new String(entry.getVersion())),
114 (entry.getAccessionId() == null ? ""
115 : new String(entry.getAccessionId())),
116 (entry.getMap() == null ? null : new Mapping(entry.getMap())),entry.isCanonical());
120 public boolean equals(Object o)
122 // TODO should also override hashCode to ensure equal objects have equal
126 // if (o == null || !(o instanceof DBRefEntry))
130 // DBRefEntry entry = (DBRefEntry) o;
131 // if (entry == this)
136 return (o != null && o instanceof DBRefEntry
138 || equalRef((DBRefEntry) o)
139 && (map == null) == ((em = ((DBRefEntry) o).map) == null)
140 && (map == null || map.equals(em))));
149 * Answers true if this object is either equivalent to, or can be 'improved'
150 * by, the given entry. Specifically, answers true if
152 * <li>source and accession are identical (ignoring case)</li>
153 * <li>version is identical (ignoring case), or this version is of the format
154 * "someSource:0", in which case the version for the other entry replaces
156 * <li>mappings are not compared but if this entry has no mapping, replace
157 * with that for the other entry</li>
164 public boolean updateFrom(DBRefEntryI other)
175 boolean improved=false;
177 * source must either match or be both null
179 String otherSource = other.getSource();
180 if ((source == null && otherSource != null)
181 || (source != null && otherSource == null)
182 || (source != null && !source.equalsIgnoreCase(otherSource)))
188 * accession id must either match or be both null
190 String otherAccession = other.getAccessionId();
191 if ((accessionId == null && otherAccession != null)
192 || (accessionId != null && otherAccession == null)
193 || (accessionId != null
194 && !accessionId.equalsIgnoreCase(otherAccession)))
199 if (!isCanonicalAccession && other.isCanonical())
201 isCanonicalAccession = true;
206 if (isCanonicalAccession && !other.isCanonical())
208 // other is not an authoritative source of canonical accessions
213 * if my version is null, "0" or "source:0" then replace with other version,
214 * otherwise the versions have to match
216 String otherVersion = other.getVersion();
218 if ((version == null || version.equals("0") || version.endsWith(":0"))
219 && otherVersion != null)
221 setVersion(otherVersion);
225 if (version != null && (otherVersion == null
226 || !version.equalsIgnoreCase(otherVersion)))
228 // FIXME: there may be a problem with old version strings not allowing
229 // updating of dbrefentries
235 * if I have no mapping, take that of the other dbref
236 * - providing it had a version and so do I
240 setMap(other.getMap());
246 * test for similar DBRef attributes, except for the map object.
249 * @return true if source, accession and version are equal with those of entry
252 public boolean equalRef(DBRefEntryI entry)
254 // TODO is this method and equals() not needed?
264 // BH 2019.01.25/2019.02.04 source cannot/should not be null.
265 // for example, StructureChooser has dbRef.getSource().equalsIgnoreCase...
267 return (entry != null
268 && (source != null && entry.getSource() != null
269 && source.equalsIgnoreCase(entry.getSource()))
270 && (accessionId != null && entry.getAccessionId() != null
271 && accessionId.equalsIgnoreCase(entry.getAccessionId()))
272 && (version != null && entry.getVersion() != null
273 && version.equalsIgnoreCase(entry.getVersion())));
277 public String getSource()
282 public int getSourceKey()
284 return (sourceKey == Integer.MIN_VALUE ? (sourceKey = DBRefSource.getSourceKey(getCanonicalSourceName())) : sourceKey);
291 public String getVersion()
300 public String getAccessionId()
306 public void setAccessionId(String accessionId)
308 this.accessionId = accessionId;
309 // this.accessionId = (accessionId == null ? "" : accessionId).toUpperCase();
313 * CAUTION! allows setting source null or not uppercase!
316 public void setSource(String source)
318 this.source = source;
320 // this.source = (source == null ? "" : source).toUpperCase();
321 // this.canonicalSourceName = DBRefUtils.getCanonicalName(this.source);
322 // this.sourceKey = DBRefSource.getSourceKey(this.canonicalSourceName);
326 public void setVersion(String version)
328 this.version = version;
329 this.ucversion = (version == null ? null : version.toUpperCase());
333 public Mapping getMap()
342 public void setMap(Mapping map)
347 public boolean hasMap()
354 * @return source+":"+accessionId
356 public String getSrcAccString()
358 return ((source != null) ? source : "") + ":"
359 + ((accessionId != null) ? accessionId : "");
363 public String toString()
365 return getSrcAccString();
369 public boolean isPrimaryCandidate()
372 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
376 SequenceI mto = map.getTo();
381 MapList ml = map.getMap();
382 if (ml.getFromRatio() != ml.getToRatio()
383 || ml.getFromRatio() != 1)
387 // check map is between identical single contiguous ranges
388 List<int[]> fromRanges, toRanges;
389 if ((fromRanges = ml.getFromRanges()).size() != 1 || (toRanges = ml.getToRanges()).size() != 1)
393 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
394 || fromRanges.get(0)[1] != toRanges.get(0)[1])
401 // no version string implies the reference has not been verified at all.
405 return DBRefSource.isPrimaryCandidate(ucversion);
409 * stores the upper-case canonical name of the source for use in
410 * Sequence.getPrimaryDBRefs().
416 public String getCanonicalSourceName() {
417 return (canonicalSourceName == null ? (canonicalSourceName = DBRefUtils.getCanonicalName(this.source)) : canonicalSourceName);
424 public void setCanonical(boolean canonical)
426 isCanonicalAccession = canonical;
430 * @return true if this is the primary canonical accession for the database source
432 public boolean isCanonical()
434 // TODO Auto-generated method stub
435 return isCanonicalAccession;