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.Console;
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.Iterator;
31 import java.util.Vector;
33 import uk.ac.vamsas.objects.core.DataSetAnnotations;
34 import uk.ac.vamsas.objects.core.Link;
35 import uk.ac.vamsas.objects.core.Property;
36 import uk.ac.vamsas.objects.core.Provenance;
37 import uk.ac.vamsas.objects.core.RangeAnnotation;
38 import uk.ac.vamsas.objects.core.Score;
39 import uk.ac.vamsas.objects.core.Seg;
40 import uk.ac.vamsas.objects.utils.Properties;
46 public class Sequencefeature extends Rangetype
49 uk.ac.vamsas.objects.core.DataSet dataset;
51 uk.ac.vamsas.objects.core.Sequence sequence;
53 private SequenceI dsSeq;
55 public Sequencefeature(VamsasAppDatastore vamsasAppDatastore,
56 SequenceFeature sequenceFeature,
57 uk.ac.vamsas.objects.core.DataSet dataset,
58 uk.ac.vamsas.objects.core.Sequence sequence)
60 super(vamsasAppDatastore, sequenceFeature, DataSetAnnotations.class);
61 this.dataset = dataset;
62 this.sequence = sequence;
66 public Sequencefeature(VamsasAppDatastore vamsasAppDatastore,
67 DataSetAnnotations dseta, SequenceI dsSeq)
69 super(vamsasAppDatastore, dseta,
70 jalview.datamodel.SequenceFeature.class);
76 public void addToDocument()
78 DataSetAnnotations dsa = (DataSetAnnotations) vobj;
79 jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) jvobj;
80 dsa = (DataSetAnnotations) getDSAnnotationFromJalview(
81 new DataSetAnnotations(), feature);
82 if (dsa.getProvenance() == null)
84 dsa.setProvenance(new Provenance());
86 addProvenance(dsa.getProvenance(), "created"); // JBPNote - need
88 dsa.addSeqRef(sequence); // we have just created this annotation
89 // - so safe to use this
90 bindjvvobj(feature, dsa);
91 dataset.addDataSetAnnotations(dsa);
95 public void addFromDocument()
97 DataSetAnnotations dsa = (DataSetAnnotations) vobj;
98 if (dsa.getSeqRefCount() != 1)
100 Console.warn("Not binding " + dsa.getVorbaId()
101 + " to Sequence Feature - has multiple dataset sequence references.");
104 jalview.datamodel.SequenceFeature sf = (jalview.datamodel.SequenceFeature) jvobj;
105 dsSeq.addSequenceFeature(sf = getJalviewSeqFeature(dsa));
111 public void conflict()
113 log.warn("Untested sequencefeature conflict code");
114 DataSetAnnotations dsa = (DataSetAnnotations) vobj;
115 jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) jvobj;
116 jalview.datamodel.SequenceFeature sf = getJalviewSeqFeature(dsa);
117 replaceJvObjMapping(feature, sf); // switch binding of dsa from old feature
118 // to newly created feature
119 dsSeq.addSequenceFeature(sf); // add new imported feature
120 addToDocument(); // and create a new feature in the document
124 public void updateToDoc()
126 DataSetAnnotations dsa = (DataSetAnnotations) vobj;
127 jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) jvobj;
128 if (dsa.getSeqRefCount() != 1)
130 replaceJvObjMapping(feature, null);
132 "Binding of annotation to jalview feature has changed. Removing binding and recreating.");
133 doSync(); // re-verify bindings.
137 // Sync the features from Jalview
138 long oldref = dsa.get__last_hash();
139 getDSAnnotationFromJalview(dsa, feature);
140 if (oldref != dsa.hashCode())
142 Console.debug("Updated dataset sequence annotation from feature.");
143 addProvenance(dsa.getProvenance(), "modified");
150 public void updateFromDoc()
152 DataSetAnnotations dsa = (DataSetAnnotations) vobj;
153 jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) jvobj;
154 if (dsa.getSeqRefCount() != 1)
156 // conflicting update from document - we cannot map this feature anymore.
157 replaceJvObjMapping(feature, null);
158 Console.warn("annotation (" + dsa.getVorbaId()
159 + " bound to jalview feature cannot be mapped. Removing binding, deleting feature, and deleting feature.");
160 // - consider deleting the feature ?
161 dsSeq.deleteFeature(feature);
166 // Sync the features to Jalview - easiest to delete and add the feature
168 jalview.datamodel.SequenceFeature newsf = getJalviewSeqFeature(dsa);
169 dsSeq.deleteFeature(feature);
170 replaceJvObjMapping(feature, newsf);
171 dsSeq.addSequenceFeature(newsf);
172 if (feature.otherDetails != null)
174 // TODO later: leave this to finalise method ?
175 feature.otherDetails.clear();
181 * correctly create/update a RangeAnnotation from a jalview sequence feature
182 * TODO: refactor to a method in jalview.io.vamsas.RangeAnnotation class
185 * (typically DataSetAnnotations or AlignmentSequenceAnnotation)
187 * (the feature to be mapped from)
190 private RangeAnnotation getDSAnnotationFromJalview(RangeAnnotation dsa,
191 jalview.datamodel.SequenceFeature feature)
193 dsa.setType(feature.getType());
194 Seg vSeg = new Seg();
195 vSeg.setStart(feature.getBegin());
196 vSeg.setEnd(feature.getEnd());
197 vSeg.setInclusive(true);
198 if (dsa.getSegCount() > 1)
201 "About to destroy complex annotation in vamsas document mapped to sequence feature ("
202 + dsa.getVorbaId() + ")");
204 dsa.setSeg(new Seg[] { vSeg });
205 dsa.setDescription(feature.getDescription());
206 dsa.setStatus(feature.getStatus());
207 if (feature.links != null && feature.links.size() > 0)
209 for (int i = 0, iSize = feature.links.size(); i < iSize; i++)
211 String link = feature.links.elementAt(i);
212 UrlLink ulink = new UrlLink(link);
215 // We only add static links to the document.
216 Link vLink = new Link();
217 vLink.setContent(ulink.getLabel());
218 vLink.setHref(ulink.getTarget());
223 dsa.setGroup(feature.getFeatureGroup());
224 if (!Float.isNaN(feature.getScore()))
226 Score fscore = new Score();
227 dsa.setScore(new Score[] { fscore });
228 fscore.setContent(feature.getScore());
229 fscore.setName(feature.getType());
231 if (feature.otherDetails != null)
233 Iterator<String> iter = feature.otherDetails.keySet().iterator();
234 Vector props = dsa.getPropertyAsReference();
235 while (iter.hasNext())
237 String key = iter.next();
238 if (!key.equalsIgnoreCase("score")
239 && !key.equalsIgnoreCase("status"))
241 Property nprop = new Property();
243 Object vlu = feature.getValue(key);
244 nprop.setContent(feature.getValue(key).toString());
245 boolean valid = false;
246 if (vlu instanceof String)
248 nprop.setType(uk.ac.vamsas.objects.utils.Properties.STRINGTYPE);
251 else if (vlu instanceof Integer)
255 uk.ac.vamsas.objects.utils.Properties.INTEGERTYPE);
257 else if (vlu instanceof Float)
259 nprop.setType(uk.ac.vamsas.objects.utils.Properties.FLOATTYPE);
266 uk.ac.vamsas.objects.utils.Properties.addOrReplace(props,
271 dsa.addProperty(nprop);
280 private SequenceFeature getJalviewSeqFeature(RangeAnnotation dseta)
282 int[] se = getBounds(dseta);
285 * try to identify feature score
287 boolean scoreFound = false;
289 String featureType = dseta.getType();
290 if (dseta.getScoreCount() > 0)
292 Enumeration scr = dseta.enumerateScore();
293 while (scr.hasMoreElements())
295 Score score = (Score) scr.nextElement();
296 if (score.getName().equals(featureType))
298 theScore = score.getContent();
304 SequenceFeature sf = null;
307 sf = new SequenceFeature(featureType, dseta.getDescription(), se[0],
308 se[1], theScore, dseta.getGroup());
312 sf = new SequenceFeature(featureType, dseta.getDescription(), se[0],
313 se[1], dseta.getGroup());
315 sf.setStatus(dseta.getStatus());
316 if (dseta.getLinkCount() > 0)
318 Link[] links = dseta.getLink();
319 for (int i = 0; i < links.length; i++)
321 // TODO: use URLLink parsing/validation here.
322 sf.addLink(links[i].getContent() + "|" + links[i].getHref());
325 if (dseta.getScoreCount() > 0)
327 Enumeration scr = dseta.enumerateScore();
328 while (scr.hasMoreElements())
330 Score score = (Score) scr.nextElement();
331 if (!score.getName().equals(sf.getType()))
333 sf.setValue(score.getName(), "" + score.getContent());
338 Enumeration props = dseta.enumerateProperty();
339 while (props.hasMoreElements())
341 Property p = (Property) props.nextElement();
343 if (Properties.isValid(p))
345 if (Properties.isString(p))
347 val = p.getContent();
349 if (Properties.isBoolean(p))
353 val = Boolean.valueOf(p.getContent());
354 } catch (Exception e)
358 if (Properties.isFloat(p))
362 val = Float.valueOf(p.getContent());
364 } catch (Exception e)
368 if (Properties.isInteger(p))
372 val = Integer.valueOf(p.getContent());
373 } catch (Exception e)
379 sf.setValue(p.getName(), val);