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.Arrays;
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";
43 String version = "", ucversion;
45 String accessionId = "";
49 String canonicalSourceName;
53 * 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 (uppercase only)
72 * (source dependent version string)
74 * (source dependent accession number string)
76 * (mapping from local sequence numbering to source accession
79 public DBRefEntry(String source, String version, String accessionId,
82 // BH 2019.01.25 made these always non-null.
83 // Is there a difference between "" and null for version?
84 // evidence is that source CANNOT be null.
87 setAccessionId(accessionId);
91 public DBRefEntry(DBRefEntryI entry)
93 this(entry.getSource(), entry.getVersion(), entry.getAccessionId(), entry.getMap() == null ? null : new Mapping(entry.getMap()));
94 // this((entry.getSource() == null ? "" : new String(entry.getSource())),
95 // (entry.getVersion() == null ? ""
96 // : new String(entry.getVersion())),
97 // (entry.getAccessionId() == null ? ""
98 // : new String(entry.getAccessionId())),
99 // (entry.getMap() == null ? null : new Mapping(entry.getMap())));
103 public boolean equals(Object o)
105 // TODO should also override hashCode to ensure equal objects have equal
109 // if (o == null || !(o instanceof DBRefEntry))
113 // DBRefEntry entry = (DBRefEntry) o;
114 // if (entry == this)
119 return (o != null && o instanceof DBRefEntry
121 || equalRef((DBRefEntry) o)
122 && (map == null) == ((em = ((DBRefEntry) o).map) == null)
123 && (map == null || map.equals(em))));
132 * Answers true if this object is either equivalent to, or can be 'improved'
133 * by, the given entry. Specifically, answers true if
135 * <li>source and accession are identical (ignoring case)</li>
136 * <li>version is identical (ignoring case), or this version is of the format
137 * "someSource:0", in which case the version for the other entry replaces
139 * <li>mappings are not compared but if this entry has no mapping, replace
140 * with that for the other entry</li>
147 public boolean updateFrom(DBRefEntryI other)
159 * source must either match or be both null
161 String otherSource = other.getSource();
162 if ((source == null && otherSource != null)
163 || (source != null && otherSource == null)
164 || (source != null && !source.equalsIgnoreCase(otherSource)))
170 * accession id must either match or be both null
172 String otherAccession = other.getAccessionId();
173 if ((accessionId == null && otherAccession != null)
174 || (accessionId != null && otherAccession == null)
175 || (accessionId != null
176 && !accessionId.equalsIgnoreCase(otherAccession)))
182 * if my version is null, "0" or "source:0" then replace with other version,
183 * otherwise the versions have to match
185 String otherVersion = other.getVersion();
187 if ((version == null || version.equals("0") || version.endsWith(":0"))
188 && otherVersion != null)
190 setVersion(otherVersion);
194 if (version != null && (otherVersion == null
195 || !version.equalsIgnoreCase(otherVersion)))
202 * if I have no mapping, take that of the other dbref
206 setMap(other.getMap());
212 * test for similar DBRef attributes, except for the map object.
215 * @return true if source, accession and version are equal with those of entry
218 public boolean equalRef(DBRefEntryI entry)
220 // TODO is this method and equals() not needed?
230 // BH 2019.01.25 source, accessionId, and version cannot be null.
231 // for example, StructureChooser has dbRef.getSource().equalsIgnoreCase...
234 && (source != null && entry.getSource() != null
235 && source.equalsIgnoreCase(entry.getSource()))
236 && (accessionId != null && entry.getAccessionId() != null
237 && accessionId.equalsIgnoreCase(entry.getAccessionId()))
238 && (version != null && entry.getVersion() != null
239 && version.equalsIgnoreCase(entry.getVersion())))
247 public String getSource()
252 public String getSourceKey()
258 public String getVersion()
264 public String getAccessionId()
270 public void setAccessionId(String accessionId)
272 this.accessionId = (accessionId == null ? "" : accessionId).toUpperCase();
276 public void setSource(String source)
278 this.source = (source == null ? "" : source).toUpperCase();
279 this.canonicalSourceName = DBRefUtils.getCanonicalName(this.source);
280 this.sourceKey = ";" + canonicalSourceName + ";";
284 public void setVersion(String version)
286 this.version = (version == null ? "" : version);
287 this.ucversion = this.version.toUpperCase();
291 public Mapping getMap()
300 public void setMap(Mapping map)
305 public boolean hasMap()
312 * @return source+":"+accessionId
314 public String getSrcAccString()
316 return ((source != null) ? source : "") + ":"
317 + ((accessionId != null) ? accessionId : "");
321 public String toString()
323 return getSrcAccString();
327 public boolean isPrimaryCandidate()
330 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
334 SequenceI mto = map.getTo();
339 MapList ml = map.getMap();
340 if (ml.getFromRatio() != ml.getToRatio()
341 || ml.getFromRatio() != 1)
345 // check map is between identical single contiguous ranges
346 List<int[]> fromRanges, toRanges;
347 if ((fromRanges = ml.getFromRanges()).size() != 1 || (toRanges = ml.getToRanges()).size() != 1)
351 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
352 || fromRanges.get(0)[1] != toRanges.get(0)[1])
357 if (version == null || version == "")
359 // no version string implies the reference has not been verified at all.
362 // tricky - this test really needs to search the sequence's set of dbrefs to
363 // see if there is a primary reference that derived this reference.
364 String[] sources = DBRefSource.allSources();
365 for (int i = sources.length; --i >= 0;)
367 if (ucversion.startsWith(sources[i])) // BH 2019.01.25 .toUpperCase() unnecessary here for allSources
369 // by convention, many secondary references inherit the primary
371 // source string as a prefix for any version information from the
372 // secondary reference.
380 * Mappings to chromosome are held with accessionId as "chromosome:id"
384 public boolean isChromosome()
386 return accessionId != null && accessionId.startsWith(CHROMOSOME + ":");
389 public Object getCanonicalSourceName() {
390 return canonicalSourceName;