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.io.vamsas.Dbref;
25 import jalview.util.DBRefUtils;
26 import jalview.util.MapList;
28 import java.util.List;
30 public class DBRefEntry implements DBRefEntryI
33 * the mapping to chromosome (genome) is held as an instance with
35 * version = assemblyId
36 * accessionId = "chromosome:" + chromosomeId
37 * map = mapping from sequence to reference assembly
39 public static final String CHROMOSOME = "chromosome";
41 private String source = "";
43 private String version = "";
45 private String ucversion;
47 private String accessionId = "";
49 int sourceKey = Integer.MIN_VALUE;
51 String canonicalSourceName;
55 * maps from associated sequence to the database sequence's coordinate system
65 * @param source may not be null
66 * @param version may be null
67 * @param accessionId may be null
69 public DBRefEntry(String source, String version, String accessionId)
71 this(source, version, accessionId, null);
77 * canonical source (turned to uppercase; cannot be null)
79 * (source dependent version string or null)
81 * (source dependent accession number string or null)
83 * (mapping from local sequence numbering to source accession
86 public DBRefEntry(String source, String version, String accessionId,
90 this.source = source.toUpperCase();
92 this.accessionId = accessionId;
97 * Clone an entry, this time not allowing any null fields except map.
100 public DBRefEntry(DBRefEntryI entry)
102 this((entry.getSource() == null ? "" : new String(entry.getSource())),
103 (entry.getVersion() == null ? ""
104 : new String(entry.getVersion())),
105 (entry.getAccessionId() == null ? ""
106 : new String(entry.getAccessionId())),
107 (entry.getMap() == null ? null : new Mapping(entry.getMap())));
111 public boolean equals(Object o)
113 // TODO should also override hashCode to ensure equal objects have equal
117 // if (o == null || !(o instanceof DBRefEntry))
121 // DBRefEntry entry = (DBRefEntry) o;
122 // if (entry == this)
127 return (o != null && o instanceof DBRefEntry
129 || equalRef((DBRefEntry) o)
130 && (map == null) == ((em = ((DBRefEntry) o).map) == null)
131 && (map == null || map.equals(em))));
140 * Answers true if this object is either equivalent to, or can be 'improved'
141 * by, the given entry. Specifically, answers true if
143 * <li>source and accession are identical (ignoring case)</li>
144 * <li>version is identical (ignoring case), or this version is of the format
145 * "someSource:0", in which case the version for the other entry replaces
147 * <li>mappings are not compared but if this entry has no mapping, replace
148 * with that for the other entry</li>
155 public boolean updateFrom(DBRefEntryI other)
167 * source must either match or be both null
169 String otherSource = other.getSource();
170 if ((source == null && otherSource != null)
171 || (source != null && otherSource == null)
172 || (source != null && !source.equalsIgnoreCase(otherSource)))
178 * accession id must either match or be both null
180 String otherAccession = other.getAccessionId();
181 if ((accessionId == null && otherAccession != null)
182 || (accessionId != null && otherAccession == null)
183 || (accessionId != null
184 && !accessionId.equalsIgnoreCase(otherAccession)))
190 * if my version is null, "0" or "source:0" then replace with other version,
191 * otherwise the versions have to match
193 String otherVersion = other.getVersion();
195 if ((version == null || version.equals("0") || version.endsWith(":0"))
196 && otherVersion != null)
198 setVersion(otherVersion);
202 if (version != null && (otherVersion == null
203 || !version.equalsIgnoreCase(otherVersion)))
210 * if I have no mapping, take that of the other dbref
214 setMap(other.getMap());
220 * test for similar DBRef attributes, except for the map object.
223 * @return true if source, accession and version are equal with those of entry
226 public boolean equalRef(DBRefEntryI entry)
228 // TODO is this method and equals() not needed?
238 // BH 2019.01.25/2019.02.04 source cannot/should not be null.
239 // for example, StructureChooser has dbRef.getSource().equalsIgnoreCase...
241 return (entry != null
242 && (source != null && entry.getSource() != null
243 && source.equalsIgnoreCase(entry.getSource()))
244 && (accessionId != null && entry.getAccessionId() != null
245 && accessionId.equalsIgnoreCase(entry.getAccessionId()))
246 && (version != null && entry.getVersion() != null
247 && version.equalsIgnoreCase(entry.getVersion())));
251 public String getSource()
256 public int getSourceKey()
258 return (sourceKey == Integer.MIN_VALUE ? (sourceKey = DBRefSource.getSourceKey(getCanonicalSourceName())) : sourceKey);
265 public String getVersion()
274 public String getAccessionId()
280 public void setAccessionId(String accessionId)
282 this.accessionId = accessionId;
283 // this.accessionId = (accessionId == null ? "" : accessionId).toUpperCase();
287 * CAUTION! allows setting source null or not uppercase!
290 public void setSource(String source)
292 this.source = source;
294 // this.source = (source == null ? "" : source).toUpperCase();
295 // this.canonicalSourceName = DBRefUtils.getCanonicalName(this.source);
296 // this.sourceKey = DBRefSource.getSourceKey(this.canonicalSourceName);
300 public void setVersion(String version)
302 this.version = version;
303 this.ucversion = (version == null ? null : version.toUpperCase());
307 public Mapping getMap()
316 public void setMap(Mapping map)
321 public boolean hasMap()
328 * @return source+":"+accessionId
330 public String getSrcAccString()
332 return ((source != null) ? source : "") + ":"
333 + ((accessionId != null) ? accessionId : "");
337 public String toString()
339 return getSrcAccString();
343 public boolean isPrimaryCandidate()
346 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
350 SequenceI mto = map.getTo();
355 MapList ml = map.getMap();
356 if (ml.getFromRatio() != ml.getToRatio()
357 || ml.getFromRatio() != 1)
361 // check map is between identical single contiguous ranges
362 List<int[]> fromRanges, toRanges;
363 if ((fromRanges = ml.getFromRanges()).size() != 1 || (toRanges = ml.getToRanges()).size() != 1)
367 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
368 || fromRanges.get(0)[1] != toRanges.get(0)[1])
375 // no version string implies the reference has not been verified at all.
379 return DBRefSource.isPrimaryCandidate(ucversion);
383 * Mappings to chromosome are held with accessionId as "chromosome:id"
387 public boolean isChromosome()
389 return accessionId != null && accessionId.startsWith(CHROMOSOME + ":");
393 * stores the upper-case canonical name of the source for use in
394 * Sequence.getPrimaryDBRefs().
400 public String getCanonicalSourceName() {
401 return (canonicalSourceName == null ? (canonicalSourceName = DBRefUtils.getCanonicalName(this.source)) : canonicalSourceName);