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