JAL-1926 added supporting model changes to enable saving sourceDBRef data for sequences
[jalview.git] / src / jalview / datamodel / DBRefEntry.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.datamodel;
22
23 import jalview.api.DBRefEntryI;
24
25 public class DBRefEntry implements DBRefEntryI
26 {
27   String source = "", version = "", accessionId = "";
28
29   /**
30    * maps from associated sequence to the database sequence's coordinate system
31    */
32   Mapping map = null;
33
34   public DBRefEntry()
35   {
36
37   }
38
39   public DBRefEntry(String source, String version, String accessionId)
40   {
41     this(source, version, accessionId, null);
42   }
43
44   /**
45    * 
46    * @param source
47    *          canonical source (uppercase only)
48    * @param version
49    *          (source dependent version string)
50    * @param accessionId
51    *          (source dependent accession number string)
52    * @param map
53    *          (mapping from local sequence numbering to source accession
54    *          numbering)
55    */
56   public DBRefEntry(String source, String version, String accessionId,
57           Mapping map)
58   {
59     this.source = source.toUpperCase();
60     this.version = version;
61     this.accessionId = accessionId;
62     this.map = map;
63   }
64
65   public DBRefEntry(DBRefEntry entry)
66   {
67     this(
68             (entry.source == null ? "" : new String(entry.source)),
69             (entry.version == null ? "" : new String(entry.version)),
70             (entry.accessionId == null ? "" : new String(entry.accessionId)),
71             (entry.map == null ? null : new Mapping(entry.map)));
72   }
73
74   @Override
75   public boolean equals(Object o)
76   {
77     // TODO should also override hashCode to ensure equal objects have equal
78     // hashcodes
79     if (o == null || !(o instanceof DBRefEntry))
80     {
81       return false;
82     }
83     DBRefEntry entry = (DBRefEntry) o;
84     if (entry == this)
85     {
86       return true;
87     }
88     if (equalRef(entry)
89             && ((map == null && entry.map == null) || (map != null
90                     && entry.map != null && map.equals(entry.map))))
91     {
92       return true;
93     }
94     return false;
95   }
96
97   /**
98    * test for similar DBRef attributes, except for the map object.
99    * 
100    * @param entry
101    * @return true if source, accession and version are equal with those of entry
102    */
103   @Override
104   public boolean equalRef(DBRefEntryI entry)
105   {
106     if (entry == null)
107     {
108       return false;
109     }
110     if (entry == this)
111     {
112       return true;
113     }
114     if (entry != null
115             && (source != null && entry.getSource() != null && source
116                     .equalsIgnoreCase(entry.getSource()))
117             && (accessionId != null && entry.getAccessionId() != null && accessionId
118                     .equalsIgnoreCase(entry.getAccessionId()))
119             && (version != null && entry.getVersion() != null && version
120                     .equalsIgnoreCase(entry.getVersion())))
121     {
122       return true;
123     }
124     return false;
125   }
126
127   @Override
128   public String getSource()
129   {
130     return source;
131   }
132
133   @Override
134   public String getVersion()
135   {
136     return version;
137   }
138
139   @Override
140   public String getAccessionId()
141   {
142     return accessionId;
143   }
144
145   /**
146    * @param accessionId
147    *          the accessionId to set
148    */
149   @Override
150   public void setAccessionId(String accessionId)
151   {
152     this.accessionId = accessionId;
153   }
154
155   /**
156    * @param source
157    *          the source to set
158    */
159   @Override
160   public void setSource(String source)
161   {
162     this.source = source;
163   }
164
165   /**
166    * @param version
167    *          the version to set
168    */
169   @Override
170   public void setVersion(String version)
171   {
172     this.version = version;
173   }
174
175   /**
176    * @return the map
177    */
178   public Mapping getMap()
179   {
180     return map;
181   }
182
183   /**
184    * @param map
185    *          the map to set
186    */
187   public void setMap(Mapping map)
188   {
189     this.map = map;
190   }
191
192   public boolean hasMap()
193   {
194     return map != null;
195   }
196
197   /**
198    * 
199    * @return source+":"+accessionId
200    */
201   public String getSrcAccString()
202   {
203     return ((source != null) ? source : "") + ":"
204             + ((accessionId != null) ? accessionId : "");
205   }
206
207   @Override
208   public String toString()
209   {
210     return getSrcAccString();
211   }
212 }