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;
25 import java.util.Arrays;
26 import java.util.List;
28 public class DBRefEntry implements DBRefEntryI
30 String source = "", version = "", accessionId = "";
33 * maps from associated sequence to the database sequence's coordinate system
42 public DBRefEntry(String source, String version, String accessionId)
44 this(source, version, accessionId, null);
50 * canonical source (uppercase only)
52 * (source dependent version string)
54 * (source dependent accession number string)
56 * (mapping from local sequence numbering to source accession
59 public DBRefEntry(String source, String version, String accessionId,
62 this.source = source.toUpperCase();
63 this.version = version;
64 this.accessionId = accessionId;
68 public DBRefEntry(DBRefEntryI entry)
70 this((entry.getSource() == null ? "" : new String(entry.getSource())),
71 (entry.getVersion() == null ? "" : new String(
73 (entry.getAccessionId() == null ? "" : new String(
74 entry.getAccessionId())),
75 (entry.getMap() == null ? null : new Mapping(entry.getMap())));
79 public boolean equals(Object o)
81 // TODO should also override hashCode to ensure equal objects have equal
83 if (o == null || !(o instanceof DBRefEntry))
87 DBRefEntry entry = (DBRefEntry) o;
93 && ((map == null && entry.map == null) || (map != null
94 && entry.map != null && map.equals(entry.map))))
102 * Answers true if this object is either equivalent to, or can be 'improved'
103 * by, the given entry. Specifically, answers true if
105 * <li>source and accession are identical (ignoring case)</li>
106 * <li>version is identical (ignoring case), or this version is of the format
107 * "someSource:0", in which case the version for the other entry replaces it</li>
108 * <li>mappings are not compared but if this entry has no mapping, replace
109 * with that for the other entry</li>
116 public boolean updateFrom(DBRefEntryI other)
128 * source must either match or be both null
130 String otherSource = other.getSource();
131 if ((source == null && otherSource != null)
132 || (source != null && otherSource == null)
133 || (source != null && !source.equalsIgnoreCase(otherSource)))
139 * accession id must either match or be both null
141 String otherAccession = other.getAccessionId();
142 if ((accessionId == null && otherAccession != null)
143 || (accessionId != null && otherAccession == null)
144 || (accessionId != null && !accessionId
145 .equalsIgnoreCase(otherAccession)))
151 * if my version is null, "0" or "source:0" then replace with other version,
152 * otherwise the versions have to match
154 String otherVersion = other.getVersion();
156 if ((version == null || version.equals("0") || version.endsWith(":0"))
157 && otherVersion != null)
159 setVersion(otherVersion);
164 && (otherVersion == null || !version
165 .equalsIgnoreCase(otherVersion)))
172 * if I have no mapping, take that of the other dbref
176 setMap(other.getMap());
182 * test for similar DBRef attributes, except for the map object.
185 * @return true if source, accession and version are equal with those of entry
188 public boolean equalRef(DBRefEntryI entry)
190 // TODO is this method and equals() not needed?
200 && (source != null && entry.getSource() != null && source
201 .equalsIgnoreCase(entry.getSource()))
202 && (accessionId != null && entry.getAccessionId() != null && accessionId
203 .equalsIgnoreCase(entry.getAccessionId()))
204 && (version != null && entry.getVersion() != null && version
205 .equalsIgnoreCase(entry.getVersion())))
213 public String getSource()
219 public String getVersion()
225 public String getAccessionId()
231 public void setAccessionId(String accessionId)
233 this.accessionId = accessionId;
237 public void setSource(String source)
239 this.source = source;
243 public void setVersion(String version)
245 this.version = version;
249 public Mapping getMap()
258 public void setMap(Mapping map)
263 public boolean hasMap()
270 * @return source+":"+accessionId
272 public String getSrcAccString()
274 return ((source != null) ? source : "") + ":"
275 + ((accessionId != null) ? accessionId : "");
279 public String toString()
281 return getSrcAccString();
285 public boolean isPrimaryCandidate()
288 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
292 if (map.getTo() != null)
296 if (map.getMap().getFromRatio() != map.getMap().getToRatio()
297 || map.getMap().getFromRatio() != 1)
301 // check map is between identical single contiguous ranges
302 List<int[]> fromRanges = map.getMap().getFromRanges();
303 List<int[]> toRanges = map.getMap().getToRanges();
304 if (fromRanges.size() != 1 || toRanges.size() != 1)
308 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
309 || fromRanges.get(0)[1] != toRanges.get(0)[1])
316 // no version string implies the reference has not been verified at all.
319 // tricky - this test really needs to search the sequence's set of dbrefs to
320 // see if there is a primary reference that derived this reference.
321 String ucv = version.toUpperCase();
322 for (String primsrc : Arrays.asList(DBRefSource.allSources()))
324 if (ucv.startsWith(primsrc.toUpperCase()))
326 // by convention, many secondary references inherit the primary
328 // source string as a prefix for any version information from the
329 // secondary reference.