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;
44 * maps from associated sequence to the database sequence's coordinate system
62 public DBRefEntry(String source, String version, String accessionId)
64 this(source, version, accessionId, null);
70 * canonical source (turned to uppercase; cannot be null)
72 * (source dependent version string or null)
74 * (source dependent accession number string or null)
76 * (mapping from local sequence numbering to source accession
79 public DBRefEntry(String source, String version, String accessionId,
83 this.source = source.toUpperCase();
85 this.accessionId = accessionId;
90 * Clone an entry, this time not allowing any null fields except map.
93 public DBRefEntry(DBRefEntryI entry)
95 this((entry.getSource() == null ? "" : new String(entry.getSource())),
96 (entry.getVersion() == null ? ""
97 : new String(entry.getVersion())),
98 (entry.getAccessionId() == null ? ""
99 : new String(entry.getAccessionId())),
100 (entry.getMap() == null ? null : new Mapping(entry.getMap())));
104 public boolean equals(Object o)
106 // TODO should also override hashCode to ensure equal objects have equal
110 // if (o == null || !(o instanceof DBRefEntry))
114 // DBRefEntry entry = (DBRefEntry) o;
115 // if (entry == this)
120 return (o != null && o instanceof DBRefEntry
122 || equalRef((DBRefEntry) o)
123 && (map == null) == ((em = ((DBRefEntry) o).map) == null)
124 && (map == null || map.equals(em))));
133 * Answers true if this object is either equivalent to, or can be 'improved'
134 * by, the given entry. Specifically, answers true if
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
140 * <li>mappings are not compared but if this entry has no mapping, replace
141 * with that for the other entry</li>
148 public boolean updateFrom(DBRefEntryI other)
160 * source must either match or be both null
162 String otherSource = other.getSource();
163 if ((source == null && otherSource != null)
164 || (source != null && otherSource == null)
165 || (source != null && !source.equalsIgnoreCase(otherSource)))
171 * accession id must either match or be both null
173 String otherAccession = other.getAccessionId();
174 if ((accessionId == null && otherAccession != null)
175 || (accessionId != null && otherAccession == null)
176 || (accessionId != null
177 && !accessionId.equalsIgnoreCase(otherAccession)))
183 * if my version is null, "0" or "source:0" then replace with other version,
184 * otherwise the versions have to match
186 String otherVersion = other.getVersion();
188 if ((version == null || version.equals("0") || version.endsWith(":0"))
189 && otherVersion != null)
191 setVersion(otherVersion);
195 if (version != null && (otherVersion == null
196 || !version.equalsIgnoreCase(otherVersion)))
203 * if I have no mapping, take that of the other dbref
207 setMap(other.getMap());
213 * test for similar DBRef attributes, except for the map object.
216 * @return true if source, accession and version are equal with those of entry
219 public boolean equalRef(DBRefEntryI entry)
221 // TODO is this method and equals() not needed?
231 // BH 2019.01.25/2019.02.04 source cannot/should not be null.
232 // for example, StructureChooser has dbRef.getSource().equalsIgnoreCase...
234 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())));
244 public String getSource()
249 public int getSourceKey()
251 return (sourceKey == Integer.MIN_VALUE ? (sourceKey = DBRefSource.getSourceKey(getCanonicalSourceName())) : sourceKey);
258 public String getVersion()
267 public String getAccessionId()
273 public void setAccessionId(String accessionId)
275 this.accessionId = accessionId;
276 // this.accessionId = (accessionId == null ? "" : accessionId).toUpperCase();
280 * CAUTION! allows setting source null or not uppercase!
283 public void setSource(String source)
285 this.source = source;
287 // this.source = (source == null ? "" : source).toUpperCase();
288 // this.canonicalSourceName = DBRefUtils.getCanonicalName(this.source);
289 // this.sourceKey = DBRefSource.getSourceKey(this.canonicalSourceName);
293 public void setVersion(String version)
295 this.version = version;
296 this.ucversion = (version == null ? null : version.toUpperCase());
300 public Mapping getMap()
309 public void setMap(Mapping map)
314 public boolean hasMap()
321 * @return source+":"+accessionId
323 public String getSrcAccString()
325 return ((source != null) ? source : "") + ":"
326 + ((accessionId != null) ? accessionId : "");
330 public String toString()
332 return getSrcAccString();
336 public boolean isPrimaryCandidate()
339 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
343 SequenceI mto = map.getTo();
348 MapList ml = map.getMap();
349 if (ml.getFromRatio() != ml.getToRatio()
350 || ml.getFromRatio() != 1)
354 // check map is between identical single contiguous ranges
355 List<int[]> fromRanges, toRanges;
356 if ((fromRanges = ml.getFromRanges()).size() != 1 || (toRanges = ml.getToRanges()).size() != 1)
360 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
361 || fromRanges.get(0)[1] != toRanges.get(0)[1])
368 // no version string implies the reference has not been verified at all.
372 return DBRefSource.isPrimaryCandidate(ucversion);
376 * stores the upper-case canonical name of the source for use in
377 * Sequence.getPrimaryDBRefs().
383 public String getCanonicalSourceName() {
384 return (canonicalSourceName == null ? (canonicalSourceName = DBRefUtils.getCanonicalName(this.source)) : canonicalSourceName);