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.io.vamsas;
23 import jalview.bin.Cache;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.io.VamsasAppDatastore;
27 import jalview.util.UrlLink;
29 import java.util.Enumeration;
30 import java.util.Vector;
32 import uk.ac.vamsas.objects.core.DataSetAnnotations;
33 import uk.ac.vamsas.objects.core.Link;
34 import uk.ac.vamsas.objects.core.Property;
35 import uk.ac.vamsas.objects.core.Provenance;
36 import uk.ac.vamsas.objects.core.RangeAnnotation;
37 import uk.ac.vamsas.objects.core.Score;
38 import uk.ac.vamsas.objects.core.Seg;
39 import uk.ac.vamsas.objects.utils.Properties;
45 public class Sequencefeature extends Rangetype
48 uk.ac.vamsas.objects.core.DataSet dataset;
50 uk.ac.vamsas.objects.core.Sequence sequence;
52 private SequenceI dsSeq;
54 public Sequencefeature(VamsasAppDatastore vamsasAppDatastore,
55 SequenceFeature sequenceFeature,
56 uk.ac.vamsas.objects.core.DataSet dataset,
57 uk.ac.vamsas.objects.core.Sequence sequence)
59 super(vamsasAppDatastore, sequenceFeature, DataSetAnnotations.class);
60 this.dataset = dataset;
61 this.sequence = sequence;
65 public Sequencefeature(VamsasAppDatastore vamsasAppDatastore,
66 DataSetAnnotations dseta, SequenceI dsSeq)
68 super(vamsasAppDatastore, dseta,
69 jalview.datamodel.SequenceFeature.class);
74 public void addToDocument()
76 DataSetAnnotations dsa = (DataSetAnnotations) vobj;
77 jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) jvobj;
78 dsa = (DataSetAnnotations) getDSAnnotationFromJalview(
79 new DataSetAnnotations(), feature);
80 if (dsa.getProvenance() == null)
82 dsa.setProvenance(new Provenance());
84 addProvenance(dsa.getProvenance(), "created"); // JBPNote - need
86 dsa.addSeqRef(sequence); // we have just created this annotation
87 // - so safe to use this
88 bindjvvobj(feature, dsa);
89 dataset.addDataSetAnnotations(dsa);
92 public void addFromDocument()
94 DataSetAnnotations dsa = (DataSetAnnotations) vobj;
95 if (dsa.getSeqRefCount() != 1)
100 + " to Sequence Feature - has multiple dataset sequence references.");
103 jalview.datamodel.SequenceFeature sf = (jalview.datamodel.SequenceFeature) jvobj;
104 dsSeq.addSequenceFeature(sf = getJalviewSeqFeature(dsa));
109 public void conflict()
111 log.warn("Untested sequencefeature conflict code");
112 DataSetAnnotations dsa = (DataSetAnnotations) vobj;
113 jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) jvobj;
114 jalview.datamodel.SequenceFeature sf = getJalviewSeqFeature(dsa);
115 replaceJvObjMapping(feature, sf); // switch binding of dsa from old feature
116 // to newly created feature
117 dsSeq.addSequenceFeature(sf); // add new imported feature
118 addToDocument(); // and create a new feature in the document
121 public void updateToDoc()
123 DataSetAnnotations dsa = (DataSetAnnotations) vobj;
124 jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) jvobj;
125 if (dsa.getSeqRefCount() != 1)
127 replaceJvObjMapping(feature, null);
129 .warn("Binding of annotation to jalview feature has changed. Removing binding and recreating.");
130 doSync(); // re-verify bindings.
134 // Sync the features from Jalview
135 long oldref = dsa.get__last_hash();
136 getDSAnnotationFromJalview(dsa, feature);
137 if (oldref != dsa.hashCode())
140 .debug("Updated dataset sequence annotation from feature.");
141 addProvenance(dsa.getProvenance(), "modified");
147 public void updateFromDoc()
149 DataSetAnnotations dsa = (DataSetAnnotations) vobj;
150 jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) jvobj;
151 if (dsa.getSeqRefCount() != 1)
153 // conflicting update from document - we cannot map this feature anymore.
154 replaceJvObjMapping(feature, null);
158 + " bound to jalview feature cannot be mapped. Removing binding, deleting feature, and deleting feature.");
159 // - consider deleting the feature ?
160 dsSeq.deleteFeature(feature);
165 // Sync the features to Jalview - easiest to delete and add the feature
167 jalview.datamodel.SequenceFeature newsf = getJalviewSeqFeature(dsa);
168 dsSeq.deleteFeature(feature);
169 replaceJvObjMapping(feature, newsf);
170 dsSeq.addSequenceFeature(newsf);
171 if (feature.otherDetails != null)
173 // TODO later: leave this to finalise method ?
174 feature.otherDetails.clear();
180 * correctly create/update a RangeAnnotation from a jalview sequence feature
181 * TODO: refactor to a method in jalview.io.vamsas.RangeAnnotation class
184 * (typically DataSetAnnotations or AlignmentSequenceAnnotation)
186 * (the feature to be mapped from)
189 private RangeAnnotation getDSAnnotationFromJalview(RangeAnnotation dsa,
190 jalview.datamodel.SequenceFeature feature)
192 dsa.setType(feature.getType());
193 Seg vSeg = new Seg();
194 vSeg.setStart(feature.getBegin());
195 vSeg.setEnd(feature.getEnd());
196 vSeg.setInclusive(true);
197 if (dsa.getSegCount() > 1)
200 .debug("About to destroy complex annotation in vamsas document mapped to sequence feature ("
201 + dsa.getVorbaId() + ")");
203 dsa.setSeg(new Seg[] { vSeg });
204 dsa.setDescription(feature.getDescription());
205 dsa.setStatus(feature.getStatus());
206 if (feature.links != null && feature.links.size() > 0)
208 for (int i = 0, iSize = feature.links.size(); i < iSize; i++)
210 String link = feature.links.elementAt(i);
211 UrlLink ulink = new UrlLink(link);
214 // We only add static links to the document.
215 Link vLink = new Link();
216 vLink.setContent(ulink.getLabel());
217 vLink.setHref(ulink.getTarget());
222 dsa.setGroup(feature.getFeatureGroup());
223 if (!Float.isNaN(feature.getScore()))
225 Score fscore = new Score();
226 dsa.setScore(new Score[] { fscore });
227 fscore.setContent(feature.getScore());
228 fscore.setName(feature.getType());
230 if (feature.otherDetails != null)
232 Enumeration iter = feature.otherDetails.keys();
233 Vector props = dsa.getPropertyAsReference();
234 while (iter.hasMoreElements())
236 String key = (String) iter.nextElement();
237 if (!key.equalsIgnoreCase("score")
238 && !key.equalsIgnoreCase("status"))
240 Property nprop = new Property();
242 Object vlu = feature.getValue(key);
243 nprop.setContent(feature.getValue(key).toString());
244 boolean valid = false;
245 if (vlu instanceof String)
247 nprop.setType(uk.ac.vamsas.objects.utils.Properties.STRINGTYPE);
250 else if (vlu instanceof Integer)
253 nprop.setType(uk.ac.vamsas.objects.utils.Properties.INTEGERTYPE);
255 else if (vlu instanceof Float)
257 nprop.setType(uk.ac.vamsas.objects.utils.Properties.FLOATTYPE);
264 uk.ac.vamsas.objects.utils.Properties.addOrReplace(props,
269 dsa.addProperty(nprop);
278 private SequenceFeature getJalviewSeqFeature(RangeAnnotation dseta)
280 int[] se = getBounds(dseta);
281 SequenceFeature sf = new jalview.datamodel.SequenceFeature(
282 dseta.getType(), dseta.getDescription(), dseta.getStatus(),
283 se[0], se[1], dseta.getGroup());
284 if (dseta.getLinkCount() > 0)
286 Link[] links = dseta.getLink();
287 for (int i = 0; i < links.length; i++)
289 // TODO: use URLLink parsing/validation here.
290 sf.addLink(links[i].getContent() + "|" + links[i].getHref());
293 if (dseta.getScoreCount() > 0)
295 Enumeration scr = dseta.enumerateScore();
296 while (scr.hasMoreElements())
298 Score score = (Score) scr.nextElement();
299 if (score.getName().equals(sf.getType()))
301 sf.setScore(score.getContent());
305 sf.setValue(score.getName(), "" + score.getContent());
310 Enumeration props = dseta.enumerateProperty();
311 while (props.hasMoreElements())
313 Property p = (Property) props.nextElement();
315 if (Properties.isValid(p))
317 if (Properties.isString(p))
319 val = p.getContent();
321 if (Properties.isBoolean(p))
325 val = new Boolean(p.getContent());
326 } catch (Exception e)
330 if (Properties.isFloat(p))
334 val = new Float(p.getContent());
336 } catch (Exception e)
340 if (Properties.isInteger(p))
344 val = new Integer(p.getContent());
345 } catch (Exception e)
351 sf.setValue(p.getName(), val);