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 ? ""
72 : new String(entry.getVersion())),
73 (entry.getAccessionId() == null ? ""
74 : new String(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;
92 if (equalRef(entry) && ((map == null && entry.map == null)
93 || (map != null && entry.map != null && map.equals(entry.map))))
101 * Answers true if this object is either equivalent to, or can be 'improved'
102 * by, the given entry. Specifically, answers true if
104 * <li>source and accession are identical (ignoring case)</li>
105 * <li>version is identical (ignoring case), or this version is of the format
106 * "someSource:0", in which case the version for the other entry replaces
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
145 && !accessionId.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);
163 if (version != null && (otherVersion == null
164 || !version.equalsIgnoreCase(otherVersion)))
171 * if I have no mapping, take that of the other dbref
175 setMap(other.getMap());
181 * test for similar DBRef attributes, except for the map object.
184 * @return true if source, accession and version are equal with those of entry
187 public boolean equalRef(DBRefEntryI entry)
189 // TODO is this method and equals() not needed?
199 && (source != null && entry.getSource() != null
200 && source.equalsIgnoreCase(entry.getSource()))
201 && (accessionId != null && entry.getAccessionId() != null
202 && accessionId.equalsIgnoreCase(entry.getAccessionId()))
203 && (version != null && entry.getVersion() != null
204 && version.equalsIgnoreCase(entry.getVersion())))
212 public String getSource()
218 public String getVersion()
224 public String getAccessionId()
230 public void setAccessionId(String accessionId)
232 this.accessionId = accessionId;
236 public void setSource(String source)
238 this.source = source;
242 public void setVersion(String version)
244 this.version = version;
248 public Mapping getMap()
257 public void setMap(Mapping map)
262 public boolean hasMap()
269 * @return source+":"+accessionId
271 public String getSrcAccString()
273 return ((source != null) ? source : "") + ":"
274 + ((accessionId != null) ? accessionId : "");
278 public String toString()
280 return getSrcAccString();
284 public boolean isPrimaryCandidate()
287 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
291 if (map.getTo() != null)
295 if (map.getMap().getFromRatio() != map.getMap().getToRatio()
296 || map.getMap().getFromRatio() != 1)
300 // check map is between identical single contiguous ranges
301 List<int[]> fromRanges = map.getMap().getFromRanges();
302 List<int[]> toRanges = map.getMap().getToRanges();
303 if (fromRanges.size() != 1 || toRanges.size() != 1)
307 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
308 || fromRanges.get(0)[1] != toRanges.get(0)[1])
315 // no version string implies the reference has not been verified at all.
318 // tricky - this test really needs to search the sequence's set of dbrefs to
319 // see if there is a primary reference that derived this reference.
320 String ucv = version.toUpperCase();
321 for (String primsrc : Arrays.asList(DBRefSource.allSources()))
323 if (ucv.startsWith(primsrc.toUpperCase()))
325 // by convention, many secondary references inherit the primary
327 // source string as a prefix for any version information from the
328 // secondary reference.