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
32 * the mapping to chromosome (genome) is held as an instance with
34 * version = assemblyId
35 * accessionId = "chromosome:" + chromosomeId
36 * map = mapping from sequence to reference assembly
38 public static final String CHROMOSOME = "chromosome";
40 private String source = "";
42 private String version = "";
44 private String ucversion = "";
46 private String accessionId = "";
48 int sourceKey = Integer.MIN_VALUE;
50 String canonicalSourceName;
54 * maps from associated sequence to the database sequence's coordinate system
64 * @param source may not be null
65 * @param version may be null
66 * @param accessionId may be null
68 public DBRefEntry(String source, String version, String accessionId)
70 this(source, version, accessionId, null);
76 * canonical source (turned to uppercase; cannot be null)
78 * (source dependent version string or null)
80 * (source dependent accession number string or null)
82 * (mapping from local sequence numbering to source accession
85 public DBRefEntry(String source, String version, String accessionId,
89 this.source = source.toUpperCase();
91 this.accessionId = accessionId;
96 * Clone an entry, this time not allowing any null fields except map.
99 public DBRefEntry(DBRefEntryI entry)
101 this((entry.getSource() == null ? "" : new String(entry.getSource())),
102 (entry.getVersion() == null ? ""
103 : new String(entry.getVersion())),
104 (entry.getAccessionId() == null ? ""
105 : new String(entry.getAccessionId())),
106 (entry.getMap() == null ? null : new Mapping(entry.getMap())));
110 public boolean equals(Object o)
112 // TODO should also override hashCode to ensure equal objects have equal
116 // if (o == null || !(o instanceof DBRefEntry))
120 // DBRefEntry entry = (DBRefEntry) o;
121 // if (entry == this)
126 return (o != null && o instanceof DBRefEntry
128 || equalRef((DBRefEntry) o)
129 && (map == null) == ((em = ((DBRefEntry) o).map) == null)
130 && (map == null || map.equals(em))));
139 * Answers true if this object is either equivalent to, or can be 'improved'
140 * by, the given entry. Specifically, answers true if
142 * <li>source and accession are identical (ignoring case)</li>
143 * <li>version is identical (ignoring case), or this version is of the format
144 * "someSource:0", in which case the version for the other entry replaces
146 * <li>mappings are not compared but if this entry has no mapping, replace
147 * with that for the other entry</li>
154 public boolean updateFrom(DBRefEntryI other)
166 * source must either match or be both null
168 String otherSource = other.getSource();
169 if ((source == null && otherSource != null)
170 || (source != null && otherSource == null)
171 || (source != null && !source.equalsIgnoreCase(otherSource)))
177 * accession id must either match or be both null
179 String otherAccession = other.getAccessionId();
180 if ((accessionId == null && otherAccession != null)
181 || (accessionId != null && otherAccession == null)
182 || (accessionId != null
183 && !accessionId.equalsIgnoreCase(otherAccession)))
189 * if my version is null, "0" or "source:0" then replace with other version,
190 * otherwise the versions have to match
192 String otherVersion = other.getVersion();
194 if ((version == null || version.equals("0") || version.endsWith(":0"))
195 && otherVersion != null)
197 setVersion(otherVersion);
201 if (version != null && (otherVersion == null
202 || !version.equalsIgnoreCase(otherVersion)))
209 * if I have no mapping, take that of the other dbref
213 setMap(other.getMap());
219 * test for similar DBRef attributes, except for the map object.
222 * @return true if source, accession and version are equal with those of entry
225 public boolean equalRef(DBRefEntryI entry)
227 // TODO is this method and equals() not needed?
237 // BH 2019.01.25/2019.02.04 source cannot/should not be null.
238 // for example, StructureChooser has dbRef.getSource().equalsIgnoreCase...
240 return (entry != null
241 && (source != null && entry.getSource() != null
242 && source.equalsIgnoreCase(entry.getSource()))
243 && (accessionId != null && entry.getAccessionId() != null
244 && accessionId.equalsIgnoreCase(entry.getAccessionId()))
245 && (version != null && entry.getVersion() != null
246 && version.equalsIgnoreCase(entry.getVersion())));
250 public String getSource()
255 public int getSourceKey()
257 return (sourceKey == Integer.MIN_VALUE ? (sourceKey = DBRefSource.getSourceKey(getCanonicalSourceName())) : sourceKey);
264 public String getVersion()
273 public String getAccessionId()
279 public void setAccessionId(String accessionId)
281 this.accessionId = accessionId;
282 // this.accessionId = (accessionId == null ? "" : accessionId).toUpperCase();
286 * CAUTION! allows setting source null or not uppercase!
289 public void setSource(String source)
291 this.source = source;
293 // this.source = (source == null ? "" : source).toUpperCase();
294 // this.canonicalSourceName = DBRefUtils.getCanonicalName(this.source);
295 // this.sourceKey = DBRefSource.getSourceKey(this.canonicalSourceName);
299 public void setVersion(String version)
301 this.version = version;
302 this.ucversion = (version == null ? null : version.toUpperCase());
306 public Mapping getMap()
315 public void setMap(Mapping map)
320 public boolean hasMap()
327 * @return source+":"+accessionId
329 public String getSrcAccString()
331 return ((source != null) ? source : "") + ":"
332 + ((accessionId != null) ? accessionId : "");
336 public String toString()
338 return getSrcAccString();
342 public boolean isPrimaryCandidate()
345 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
349 SequenceI mto = map.getTo();
354 MapList ml = map.getMap();
355 if (ml.getFromRatio() != ml.getToRatio()
356 || ml.getFromRatio() != 1)
360 // check map is between identical single contiguous ranges
361 List<int[]> fromRanges, toRanges;
362 if ((fromRanges = ml.getFromRanges()).size() != 1 || (toRanges = ml.getToRanges()).size() != 1)
366 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
367 || fromRanges.get(0)[1] != toRanges.get(0)[1])
374 // no version string implies the reference has not been verified at all.
378 return DBRefSource.isPrimaryCandidate(ucversion);
382 * Mappings to chromosome are held with accessionId as "chromosome:id"
386 public boolean isChromosome()
388 return accessionId != null && accessionId.startsWith(CHROMOSOME + ":");
392 * stores the upper-case canonical name of the source for use in
393 * Sequence.getPrimaryDBRefs().
399 public String getCanonicalSourceName() {
400 return (canonicalSourceName == null ? (canonicalSourceName = DBRefUtils.getCanonicalName(this.source)) : canonicalSourceName);