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