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
34 String accessionId = "";
37 * maps from associated sequence to the database sequence's coordinate system
46 public DBRefEntry(String source, String version, String accessionId)
48 this(source, version, accessionId, null);
54 * canonical source (uppercase only)
56 * (source dependent version string)
58 * (source dependent accession number string)
60 * (mapping from local sequence numbering to source accession
63 public DBRefEntry(String source, String version, String accessionId,
66 this.source = source.toUpperCase();
67 this.version = version;
68 this.accessionId = accessionId;
72 public DBRefEntry(DBRefEntryI entry)
74 this((entry.getSource() == null ? "" : new String(entry.getSource())),
75 (entry.getVersion() == null ? ""
76 : new String(entry.getVersion())),
77 (entry.getAccessionId() == null ? ""
78 : new String(entry.getAccessionId())),
79 (entry.getMap() == null ? null : new Mapping(entry.getMap())));
83 public boolean equals(Object o)
85 // TODO should also override hashCode to ensure equal objects have equal
87 if (o == null || !(o instanceof DBRefEntry))
91 DBRefEntry entry = (DBRefEntry) o;
96 if (equalRef(entry) && ((map == null && entry.map == null)
97 || (map != null && entry.map != null && map.equals(entry.map))))
105 * Answers true if this object is either equivalent to, or can be 'improved'
106 * by, the given entry. Specifically, answers true if
108 * <li>source and accession are identical (ignoring case)</li>
109 * <li>version is identical (ignoring case), or this version is of the format
110 * "someSource:0", in which case the version for the other entry replaces
112 * <li>mappings are not compared but if this entry has no mapping, replace
113 * with that for the other entry</li>
120 public boolean updateFrom(DBRefEntryI other)
132 * source must either match or be both null
134 String otherSource = other.getSource();
135 if ((source == null && otherSource != null)
136 || (source != null && otherSource == null)
137 || (source != null && !source.equalsIgnoreCase(otherSource)))
143 * accession id must either match or be both null
145 String otherAccession = other.getAccessionId();
146 if ((accessionId == null && otherAccession != null)
147 || (accessionId != null && otherAccession == null)
148 || (accessionId != null
149 && !accessionId.equalsIgnoreCase(otherAccession)))
155 * if my version is null, "0" or "source:0" then replace with other version,
156 * otherwise the versions have to match
158 String otherVersion = other.getVersion();
160 if ((version == null || version.equals("0") || version.endsWith(":0"))
161 && otherVersion != null)
163 setVersion(otherVersion);
167 if (version != null && (otherVersion == null
168 || !version.equalsIgnoreCase(otherVersion)))
175 * if I have no mapping, take that of the other dbref
179 setMap(other.getMap());
185 * test for similar DBRef attributes, except for the map object.
188 * @return true if source, accession and version are equal with those of entry
191 public boolean equalRef(DBRefEntryI entry)
193 // TODO is this method and equals() not needed?
203 && (source != null && entry.getSource() != null
204 && source.equalsIgnoreCase(entry.getSource()))
205 && (accessionId != null && entry.getAccessionId() != null
206 && accessionId.equalsIgnoreCase(entry.getAccessionId()))
207 && (version != null && entry.getVersion() != null
208 && version.equalsIgnoreCase(entry.getVersion())))
216 public String getSource()
222 public String getVersion()
228 public String getAccessionId()
234 public void setAccessionId(String accessionId)
236 this.accessionId = accessionId;
240 public void setSource(String source)
242 this.source = source;
246 public void setVersion(String version)
248 this.version = version;
252 public Mapping getMap()
261 public void setMap(Mapping map)
266 public boolean hasMap()
273 * @return source+":"+accessionId
275 public String getSrcAccString()
277 return ((source != null) ? source : "") + ":"
278 + ((accessionId != null) ? accessionId : "");
282 public String toString()
284 return getSrcAccString();
288 public boolean isPrimaryCandidate()
291 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
295 if (map.getTo() != null)
299 if (map.getMap().getFromRatio() != map.getMap().getToRatio()
300 || map.getMap().getFromRatio() != 1)
304 // check map is between identical single contiguous ranges
305 List<int[]> fromRanges = map.getMap().getFromRanges();
306 List<int[]> toRanges = map.getMap().getToRanges();
307 if (fromRanges.size() != 1 || toRanges.size() != 1)
311 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
312 || fromRanges.get(0)[1] != toRanges.get(0)[1])
319 // no version string implies the reference has not been verified at all.
322 // tricky - this test really needs to search the sequence's set of dbrefs to
323 // see if there is a primary reference that derived this reference.
324 String ucv = version.toUpperCase();
325 for (String primsrc : Arrays.asList(DBRefSource.allSources()))
327 if (ucv.startsWith(primsrc.toUpperCase()))
329 // by convention, many secondary references inherit the primary
331 // source string as a prefix for any version information from the
332 // secondary reference.