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