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