JAL-1620 version bump and release notes
[jalview.git] / src / jalview / io / vamsas / Dbref.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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.datamodel.DBRefEntry;
24 import jalview.datamodel.SequenceI;
25 import uk.ac.vamsas.objects.core.DataSet;
26 import uk.ac.vamsas.objects.core.DbRef;
27 import uk.ac.vamsas.objects.core.Map;
28 import uk.ac.vamsas.objects.core.Sequence;
29 import jalview.io.VamsasAppDatastore;
30
31 public class Dbref extends Rangetype
32 {
33   jalview.datamodel.SequenceI sq = null;
34
35   uk.ac.vamsas.objects.core.Sequence sequence = null;
36
37   DataSet ds;
38
39   public Dbref(VamsasAppDatastore datastore, DBRefEntry dbentry,
40           jalview.datamodel.SequenceI sq2,
41           uk.ac.vamsas.objects.core.Sequence sequence2, DataSet dataset)
42   {
43     super(datastore, dbentry, DbRef.class);
44     // initialise object specific attributes
45     sq = sq2;
46     sequence = sequence2;
47     this.jvobj = dbentry;
48     ds = dataset;
49     // call the accessors
50     doSync();
51   }
52
53   public Dbref(VamsasAppDatastore datastore, DbRef ref, Sequence vdseq,
54           SequenceI dsseq)
55   {
56     super(datastore, ref, jalview.datamodel.DBRefEntry.class);
57     sequence = vdseq;
58     sq = dsseq;
59     ds = (DataSet) vdseq.getV_parent();
60     doJvUpdate();
61   }
62
63   public void updateToDoc()
64   {
65     DbRef dbref = (DbRef) this.vobj;
66     DBRefEntry jvobj = (DBRefEntry) this.jvobj;
67     dbref.setAccessionId(jvobj.getAccessionId());
68     dbref.setSource(jvobj.getSource());
69     dbref.setVersion(jvobj.getVersion());
70     if (jvobj.getMap() != null)
71     {
72       // Record mapping to external database coordinate system.
73       jalview.datamodel.Mapping mp = jvobj.getMap();
74       if (mp.getMap() != null)
75       {
76         Map vMap = null;
77         if (dbref.getMapCount() == 0)
78         {
79           vMap = new Map();
80           initMapType(vMap, mp.getMap(), true);
81           dbref.addMap(vMap);
82         }
83         else
84         {
85           // we just update the data anyway.
86           vMap = dbref.getMap(0);
87           initMapType(vMap, mp.getMap(), true);
88         }
89         updateMapTo(mp);
90       }
91     }
92     else
93     {
94       jalview.bin.Cache.log.debug("Ignoring mapless DbRef.Map "
95               + jvobj.getSrcAccString());
96     }
97
98   }
99
100   /**
101    * ugly hack to try to get the embedded sequences within a database reference
102    * to be stored in the document's dataset.
103    * 
104    * @param mp
105    */
106   private void updateMapTo(jalview.datamodel.Mapping mp)
107   {
108     log.info("Performing updateMapTo remove this message when we know what we're doing.");
109     // TODO determine how sequences associated with database mappings are stored
110     // in the document
111     if (mp != null && mp.getTo() != null)
112     {
113       if (mp.getTo().getDatasetSequence() == null)
114       {
115         // TODO: fix this hinky sh!t
116         DatastoreItem dssync = dsReg.getDatastoreItemFor(mp.getTo());
117         if (dssync == null)
118         {
119           // sync the dataset sequence, if it hasn't been done already.
120           // TODO: ensure real dataset sequence corresponding to getTo is
121           // recovered
122           dssync = new Datasetsequence(
123                   datastore,
124                   mp.getTo(),
125                   (mp.getMappedWidth() == mp.getWidth()) ? sequence
126                           .getDictionary()
127                           : ((mp.getMappedWidth() == 3) ? uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA
128                                   : uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA),
129                   ds);
130         }
131         //
132         // TODO: NOW add a mapping between new dataset sequence and sequence
133         // associated with the database reference
134
135         // dna mappings only...
136         // new jalview.io.vamsas.Sequencemapping(datastore, mp, sequence, ds);
137
138       }
139
140     }
141     else
142     {
143       log.debug("Ignoring non-dataset sequence mapping.");
144     }
145   }
146
147   public void updateFromDoc()
148   {
149     DbRef vobj = (DbRef) this.vobj;
150     DBRefEntry jvobj = (DBRefEntry) this.jvobj;
151     jvobj.setAccessionId(vobj.getAccessionId());
152     jvobj.setSource(vobj.getSource());
153     jvobj.setVersion(vobj.getVersion());
154     // add new dbref
155     if (vobj.getMapCount() > 0)
156     {
157       // TODO: Jalview ignores all the other maps
158       if (vobj.getMapCount() > 1)
159       {
160         jalview.bin.Cache.log
161                 .debug("Ignoring additional mappings on DbRef: "
162                         + jvobj.getSource() + ":" + jvobj.getAccessionId());
163       }
164       jalview.datamodel.Mapping mp = new jalview.datamodel.Mapping(
165               parsemapType(vobj.getMap(0)));
166       if (jvobj.getMap() == null || !mp.equals(jvobj.getMap()))
167       {
168         jvobj.setMap(mp);
169       }
170     }
171   }
172
173   public void conflict()
174   {
175     DbRef vobj = (DbRef) this.vobj;
176     DBRefEntry jvobj = (DBRefEntry) this.jvobj;
177     jalview.bin.Cache.log.debug("Conflict in dbentry update for "
178             + vobj.getAccessionId() + vobj.getSource() + " "
179             + vobj.getVorbaId());
180     // TODO Auto-generated method stub
181
182   }
183
184   public void addFromDocument()
185   {
186     DbRef vobj = (DbRef) this.vobj;
187     DBRefEntry jvobj = (DBRefEntry) this.jvobj;
188     // add new dbref
189     sq.addDBRef(jvobj = new jalview.datamodel.DBRefEntry(vobj.getSource()
190             .toString(), vobj.getVersion().toString(), vobj
191             .getAccessionId().toString()));
192     if (vobj.getMapCount() > 0)
193     {
194       // TODO: Jalview ignores all the other maps
195       if (vobj.getMapCount() > 1)
196       {
197         jalview.bin.Cache.log
198                 .debug("Ignoring additional mappings on DbRef: "
199                         + jvobj.getSource() + ":" + jvobj.getAccessionId());
200       }
201       jalview.datamodel.Mapping mp = new jalview.datamodel.Mapping(
202               parsemapType(vobj.getMap(0)));
203       jvobj.setMap(mp);
204     }
205     // TODO: jalview ignores links and properties because it doesn't know what
206     // to do with them.
207
208     bindjvvobj(jvobj, vobj);
209   }
210
211   public void addToDocument()
212   {
213     DBRefEntry jvobj = (DBRefEntry) this.jvobj;
214     DbRef dbref = new DbRef();
215     bindjvvobj(jvobj, dbref);
216     dbref.setAccessionId(jvobj.getAccessionId());
217     dbref.setSource(jvobj.getSource());
218     dbref.setVersion(jvobj.getVersion());
219     sequence.addDbRef(dbref);
220     if (jvobj.getMap() != null)
221     {
222       jalview.datamodel.Mapping mp = jvobj.getMap();
223       if (mp.getMap() != null)
224       {
225         Map vMap = new Map();
226         initMapType(vMap, mp.getMap(), true);
227         dbref.addMap(vMap);
228         updateMapTo(mp);
229       }
230       else
231       {
232         jalview.bin.Cache.log.debug("Ignoring mapless DbRef.Map "
233                 + jvobj.getSrcAccString());
234       }
235     }
236   }
237
238 }