JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / io / vamsas / Sequencefeature.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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.io.vamsas;
22
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;
28
29 import java.util.Enumeration;
30 import java.util.Vector;
31
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;
40
41 /**
42  * @author JimP
43  * 
44  */
45 public class Sequencefeature extends Rangetype
46 {
47
48   uk.ac.vamsas.objects.core.DataSet dataset;
49
50   uk.ac.vamsas.objects.core.Sequence sequence;
51
52   private SequenceI dsSeq;
53
54   public Sequencefeature(VamsasAppDatastore vamsasAppDatastore,
55           SequenceFeature sequenceFeature,
56           uk.ac.vamsas.objects.core.DataSet dataset,
57           uk.ac.vamsas.objects.core.Sequence sequence)
58   {
59     super(vamsasAppDatastore, sequenceFeature, DataSetAnnotations.class);
60     this.dataset = dataset;
61     this.sequence = sequence;
62     doSync();
63   }
64
65   public Sequencefeature(VamsasAppDatastore vamsasAppDatastore,
66           DataSetAnnotations dseta, SequenceI dsSeq)
67   {
68     super(vamsasAppDatastore, dseta,
69             jalview.datamodel.SequenceFeature.class);
70     this.dsSeq = dsSeq;
71     doJvUpdate();
72   }
73
74   public void addToDocument()
75   {
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)
81     {
82       dsa.setProvenance(new Provenance());
83     }
84     addProvenance(dsa.getProvenance(), "created"); // JBPNote - need
85     // to update
86     dsa.addSeqRef(sequence); // we have just created this annotation
87     // - so safe to use this
88     bindjvvobj(feature, dsa);
89     dataset.addDataSetAnnotations(dsa);
90   }
91
92   public void addFromDocument()
93   {
94     DataSetAnnotations dsa = (DataSetAnnotations) vobj;
95     if (dsa.getSeqRefCount() != 1)
96     {
97       Cache.log
98               .warn("Not binding "
99                       + dsa.getVorbaId()
100                       + " to Sequence Feature - has multiple dataset sequence references.");
101       return;
102     }
103     jalview.datamodel.SequenceFeature sf = (jalview.datamodel.SequenceFeature) jvobj;
104     dsSeq.addSequenceFeature(sf = getJalviewSeqFeature(dsa));
105     jvobj = sf;
106     bindjvvobj(sf, dsa);
107   }
108
109   public void conflict()
110   {
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
119   }
120
121   public void updateToDoc()
122   {
123     DataSetAnnotations dsa = (DataSetAnnotations) vobj;
124     jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) jvobj;
125     if (dsa.getSeqRefCount() != 1)
126     {
127       replaceJvObjMapping(feature, null);
128       Cache.log
129               .warn("Binding of annotation to jalview feature has changed. Removing binding and recreating.");
130       doSync(); // re-verify bindings.
131     }
132     else
133     {
134       // Sync the features from Jalview
135       long oldref = dsa.get__last_hash();
136       getDSAnnotationFromJalview(dsa, feature);
137       if (oldref != dsa.hashCode())
138       {
139         Cache.log
140                 .debug("Updated dataset sequence annotation from feature.");
141         addProvenance(dsa.getProvenance(), "modified");
142       }
143     }
144
145   }
146
147   public void updateFromDoc()
148   {
149     DataSetAnnotations dsa = (DataSetAnnotations) vobj;
150     jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) jvobj;
151     if (dsa.getSeqRefCount() != 1)
152     {
153       // conflicting update from document - we cannot map this feature anymore.
154       replaceJvObjMapping(feature, null);
155       Cache.log
156               .warn("annotation ("
157                       + dsa.getVorbaId()
158                       + " bound to jalview feature cannot be mapped. Removing binding, deleting feature, and deleting feature.");
159       // - consider deleting the feature ?
160       dsSeq.deleteFeature(feature);
161       // doSync();
162     }
163     else
164     {
165       // Sync the features to Jalview - easiest to delete and add the feature
166       // again
167       jalview.datamodel.SequenceFeature newsf = getJalviewSeqFeature(dsa);
168       dsSeq.deleteFeature(feature);
169       replaceJvObjMapping(feature, newsf);
170       dsSeq.addSequenceFeature(newsf);
171       if (feature.otherDetails != null)
172       {
173         // TODO later: leave this to finalise method ?
174         feature.otherDetails.clear();
175       }
176     }
177   }
178
179   /**
180    * correctly create/update a RangeAnnotation from a jalview sequence feature
181    * TODO: refactor to a method in jalview.io.vamsas.RangeAnnotation class
182    * 
183    * @param dsa
184    *          (typically DataSetAnnotations or AlignmentSequenceAnnotation)
185    * @param feature
186    *          (the feature to be mapped from)
187    * @return
188    */
189   private RangeAnnotation getDSAnnotationFromJalview(RangeAnnotation dsa,
190           jalview.datamodel.SequenceFeature feature)
191   {
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)
198     {
199       Cache.log
200               .debug("About to destroy complex annotation in vamsas document mapped to sequence feature ("
201                       + dsa.getVorbaId() + ")");
202     }
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)
207     {
208       for (int i = 0, iSize = feature.links.size(); i < iSize; i++)
209       {
210         String link = feature.links.elementAt(i);
211         UrlLink ulink = new UrlLink(link);
212         if (ulink.isValid())
213         {
214           // We only add static links to the document.
215           Link vLink = new Link();
216           vLink.setContent(ulink.getLabel());
217           vLink.setHref(ulink.getTarget());
218           dsa.addLink(vLink);
219         }
220       }
221     }
222     dsa.setGroup(feature.getFeatureGroup());
223     if (!Float.isNaN(feature.getScore()))
224     {
225       Score fscore = new Score();
226       dsa.setScore(new Score[] { fscore });
227       fscore.setContent(feature.getScore());
228       fscore.setName(feature.getType());
229     }
230     if (feature.otherDetails != null)
231     {
232       Enumeration iter = feature.otherDetails.keys();
233       Vector props = dsa.getPropertyAsReference();
234       while (iter.hasMoreElements())
235       {
236         String key = (String) iter.nextElement();
237         if (!key.equalsIgnoreCase("score")
238                 && !key.equalsIgnoreCase("status"))
239         {
240           Property nprop = new Property();
241           nprop.setName(key);
242           Object vlu = feature.getValue(key);
243           nprop.setContent(feature.getValue(key).toString());
244           boolean valid = false;
245           if (vlu instanceof String)
246           {
247             nprop.setType(uk.ac.vamsas.objects.utils.Properties.STRINGTYPE);
248             valid = true;
249           }
250           else if (vlu instanceof Integer)
251           {
252             valid = true;
253             nprop.setType(uk.ac.vamsas.objects.utils.Properties.INTEGERTYPE);
254           }
255           else if (vlu instanceof Float)
256           {
257             nprop.setType(uk.ac.vamsas.objects.utils.Properties.FLOATTYPE);
258             valid = true;
259           }
260           if (valid)
261           {
262             if (props != null)
263             {
264               uk.ac.vamsas.objects.utils.Properties.addOrReplace(props,
265                       nprop);
266             }
267             else
268             {
269               dsa.addProperty(nprop);
270             }
271           }
272         }
273       }
274     }
275     return dsa;
276   }
277
278   private SequenceFeature getJalviewSeqFeature(RangeAnnotation dseta)
279   {
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)
285     {
286       Link[] links = dseta.getLink();
287       for (int i = 0; i < links.length; i++)
288       {
289         // TODO: use URLLink parsing/validation here.
290         sf.addLink(links[i].getContent() + "|" + links[i].getHref());
291       }
292     }
293     if (dseta.getScoreCount() > 0)
294     {
295       Enumeration scr = dseta.enumerateScore();
296       while (scr.hasMoreElements())
297       {
298         Score score = (Score) scr.nextElement();
299         if (score.getName().equals(sf.getType()))
300         {
301           sf.setScore(score.getContent());
302         }
303         else
304         {
305           sf.setValue(score.getName(), "" + score.getContent());
306         }
307       }
308     }
309     // other details
310     Enumeration props = dseta.enumerateProperty();
311     while (props.hasMoreElements())
312     {
313       Property p = (Property) props.nextElement();
314       Object val = null;
315       if (Properties.isValid(p))
316       {
317         if (Properties.isString(p))
318         {
319           val = p.getContent();
320         }
321         if (Properties.isBoolean(p))
322         {
323           try
324           {
325             val = new Boolean(p.getContent());
326           } catch (Exception e)
327           {
328           }
329         }
330         if (Properties.isFloat(p))
331         {
332           try
333           {
334             val = new Float(p.getContent());
335
336           } catch (Exception e)
337           {
338           }
339         }
340         if (Properties.isInteger(p))
341         {
342           try
343           {
344             val = new Integer(p.getContent());
345           } catch (Exception e)
346           {
347           }
348         }
349         if (val != null)
350         {
351           sf.setValue(p.getName(), val);
352         }
353       }
354     }
355
356     return sf;
357   }
358
359 }