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
31 * the mapping to chromosome (genome) is held as an instance with
33 * version = assemblyId
34 * accessionId = "chromosome:" + chromosomeId
35 * map = mapping from sequence to reference assembly
37 public static final String CHROMOSOME = "chromosome";
43 String accessionId = "";
46 * maps from associated sequence to the database sequence's coordinate system
55 public DBRefEntry(String source, String version, String accessionId)
57 this(source, version, accessionId, null);
63 * canonical source (uppercase only)
65 * (source dependent version string)
67 * (source dependent accession number string)
69 * (mapping from local sequence numbering to source accession
72 public DBRefEntry(String source, String version, String accessionId,
75 this.source = source.toUpperCase();
76 this.version = version;
77 this.accessionId = accessionId;
81 public DBRefEntry(DBRefEntryI entry)
83 this((entry.getSource() == null ? "" : new String(entry.getSource())),
84 (entry.getVersion() == null ? ""
85 : new String(entry.getVersion())),
86 (entry.getAccessionId() == null ? ""
87 : new String(entry.getAccessionId())),
88 (entry.getMap() == null ? null : new Mapping(entry.getMap())));
92 public boolean equals(Object o)
94 // TODO should also override hashCode to ensure equal objects have equal
96 if (o == null || !(o instanceof DBRefEntry))
100 DBRefEntry entry = (DBRefEntry) o;
105 if (equalRef(entry) && ((map == null && entry.map == null)
106 || (map != null && entry.map != null && map.equals(entry.map))))
114 * Answers true if this object is either equivalent to, or can be 'improved'
115 * by, the given entry. Specifically, answers true if
117 * <li>source and accession are identical (ignoring case)</li>
118 * <li>version is identical (ignoring case), or this version is of the format
119 * "someSource:0", in which case the version for the other entry replaces
121 * <li>mappings are not compared but if this entry has no mapping, replace
122 * with that for the other entry</li>
129 public boolean updateFrom(DBRefEntryI other)
141 * source must either match or be both null
143 String otherSource = other.getSource();
144 if ((source == null && otherSource != null)
145 || (source != null && otherSource == null)
146 || (source != null && !source.equalsIgnoreCase(otherSource)))
152 * accession id must either match or be both null
154 String otherAccession = other.getAccessionId();
155 if ((accessionId == null && otherAccession != null)
156 || (accessionId != null && otherAccession == null)
157 || (accessionId != null
158 && !accessionId.equalsIgnoreCase(otherAccession)))
164 * if my version is null, "0" or "source:0" then replace with other version,
165 * otherwise the versions have to match
167 String otherVersion = other.getVersion();
169 if ((version == null || version.equals("0") || version.endsWith(":0"))
170 && otherVersion != null)
172 setVersion(otherVersion);
176 if (version != null && (otherVersion == null
177 || !version.equalsIgnoreCase(otherVersion)))
184 * if I have no mapping, take that of the other dbref
188 setMap(other.getMap());
194 * test for similar DBRef attributes, except for the map object.
197 * @return true if source, accession and version are equal with those of entry
200 public boolean equalRef(DBRefEntryI entry)
202 // TODO is this method and equals() not needed?
212 && (source != null && entry.getSource() != null
213 && source.equalsIgnoreCase(entry.getSource()))
214 && (accessionId != null && entry.getAccessionId() != null
215 && accessionId.equalsIgnoreCase(entry.getAccessionId()))
216 && (version != null && entry.getVersion() != null
217 && version.equalsIgnoreCase(entry.getVersion())))
225 public String getSource()
231 public String getVersion()
237 public String getAccessionId()
243 public void setAccessionId(String accessionId)
245 this.accessionId = accessionId;
249 public void setSource(String source)
251 this.source = source;
255 public void setVersion(String version)
257 this.version = version;
261 public Mapping getMap()
270 public void setMap(Mapping map)
275 public boolean hasMap()
282 * @return source+":"+accessionId
284 public String getSrcAccString()
286 return ((source != null) ? source : "") + ":"
287 + ((accessionId != null) ? accessionId : "");
291 public String toString()
293 return getSrcAccString();
297 public boolean isPrimaryCandidate()
300 * if a map is present, unless it is 1:1 and has no SequenceI mate, it cannot be a primary reference.
304 if (map.getTo() != null)
308 if (map.getMap().getFromRatio() != map.getMap().getToRatio()
309 || map.getMap().getFromRatio() != 1)
313 // check map is between identical single contiguous ranges
314 List<int[]> fromRanges = map.getMap().getFromRanges();
315 List<int[]> toRanges = map.getMap().getToRanges();
316 if (fromRanges.size() != 1 || toRanges.size() != 1)
320 if (fromRanges.get(0)[0] != toRanges.get(0)[0]
321 || fromRanges.get(0)[1] != toRanges.get(0)[1])
328 // no version string implies the reference has not been verified at all.
331 // tricky - this test really needs to search the sequence's set of dbrefs to
332 // see if there is a primary reference that derived this reference.
333 String ucv = version.toUpperCase();
334 for (String primsrc : Arrays.asList(DBRefSource.allSources()))
336 if (ucv.startsWith(primsrc.toUpperCase()))
338 // by convention, many secondary references inherit the primary
340 // source string as a prefix for any version information from the
341 // secondary reference.
349 * Mappings to chromosome are held with accessionId as "chromosome:id"
353 public boolean isChromosome()
355 return accessionId != null && accessionId.startsWith(CHROMOSOME + ":");