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";
44 private String ucversion;
46 String accessionId = "";
50 String canonicalSourceName;
54 * maps from associated sequence to the database sequence's coordinate system
63 public DBRefEntry(String source, String version, String accessionId)
65 this(source, version, accessionId, null);
71 * canonical source (uppercase only)
73 * (source dependent version string)
75 * (source dependent accession number string)
77 * (mapping from local sequence numbering to source accession
80 public DBRefEntry(String source, String version, String accessionId,
83 // BH 2019.01.25 made these always non-null.
84 // Is there a difference between "" and null for version?
85 // evidence is that source CANNOT be null.
88 setAccessionId(accessionId);
92 public DBRefEntry(DBRefEntryI entry)
94 this(entry.getSource(), entry.getVersion(), entry.getAccessionId(), entry.getMap() == null ? null : new Mapping(entry.getMap()));
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 source, accessionId, and version cannot be null.
232 // for example, StructureChooser has dbRef.getSource().equalsIgnoreCase...
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())))
248 public String getSource()
253 public String getSourceKey()
259 public String getVersion()
265 public String getAccessionId()
271 public void setAccessionId(String accessionId)
273 this.accessionId = (accessionId == null ? "" : accessionId).toUpperCase();
277 public void setSource(String source)
279 this.source = (source == null ? "" : source).toUpperCase();
280 this.canonicalSourceName = DBRefUtils.getCanonicalName(this.source);
281 this.sourceKey = ";" + canonicalSourceName + ";";
285 public void setVersion(String version)
287 this.version = (version == null ? "" : version);
288 this.ucversion = this.version.toUpperCase();
292 public Mapping getMap()
301 public void setMap(Mapping map)
306 public boolean hasMap()
313 * @return source+":"+accessionId
315 public String getSrcAccString()
317 return ((source != null) ? source : "") + ":"
318 + ((accessionId != null) ? accessionId : "");
322 public String toString()
324 return getSrcAccString();
328 public boolean isPrimaryCandidate()
331 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
335 SequenceI mto = map.getTo();
340 MapList ml = map.getMap();
341 if (ml.getFromRatio() != ml.getToRatio()
342 || ml.getFromRatio() != 1)
346 // check map is between identical single contiguous ranges
347 List<int[]> fromRanges, toRanges;
348 if ((fromRanges = ml.getFromRanges()).size() != 1 || (toRanges = ml.getToRanges()).size() != 1)
352 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
353 || fromRanges.get(0)[1] != toRanges.get(0)[1])
358 if (version == null || version == "")
360 // no version string implies the reference has not been verified at all.
363 // tricky - this test really needs to search the sequence's set of dbrefs to
364 // see if there is a primary reference that derived this reference.
365 String[] sources = DBRefSource.allSources();
366 for (int i = sources.length; --i >= 0;)
368 if (ucversion.startsWith(sources[i])) // BH 2019.01.25 .toUpperCase() unnecessary here for allSources
370 // by convention, many secondary references inherit the primary
372 // source string as a prefix for any version information from the
373 // secondary reference.
381 * Mappings to chromosome are held with accessionId as "chromosome:id"
385 public boolean isChromosome()
387 return accessionId != null && accessionId.startsWith(CHROMOSOME + ":");
391 * stores the upper-case canonical name of the source for use in
392 * Sequence.getPrimaryDBRefs().
398 public Object getCanonicalSourceName() {
399 return canonicalSourceName;