debug and cyclefix for dbrefs with embedded sequences
[jalview.git] / src / jalview / io / vamsas / Dbref.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io.vamsas;
20
21 import jalview.datamodel.DBRefEntry;
22 import jalview.datamodel.SequenceI;
23 import uk.ac.vamsas.objects.core.DataSet;
24 import uk.ac.vamsas.objects.core.DbRef;
25 import uk.ac.vamsas.objects.core.Map;
26 import uk.ac.vamsas.objects.core.Sequence;
27 import uk.ac.vamsas.objects.core.SequenceType;
28 import jalview.io.VamsasAppDatastore;
29
30 public class Dbref extends Rangetype
31 {
32   jalview.datamodel.SequenceI sq = null;
33
34   uk.ac.vamsas.objects.core.Sequence sequence = null;
35
36   DataSet ds;
37
38   public Dbref(VamsasAppDatastore datastore, DBRefEntry dbentry,
39           jalview.datamodel.SequenceI sq2,
40           uk.ac.vamsas.objects.core.Sequence sequence2, DataSet dataset)
41   {
42     super(datastore, dbentry, DbRef.class);
43     // initialise object specific attributes
44     sq = sq2;
45     sequence = sequence2;
46     this.jvobj = dbentry;
47     ds = dataset;
48     // call the accessors
49     doSync();
50   }
51
52   public Dbref(VamsasAppDatastore datastore, DbRef ref, Sequence vdseq,
53           SequenceI dsseq)
54   {
55     super(datastore, ref, jalview.datamodel.DBRefEntry.class);
56     sequence = vdseq;
57     sq = dsseq;
58     ds = (DataSet) vdseq.getV_parent();
59     doJvUpdate();
60   }
61
62   public void updateToDoc()
63   {
64     DbRef dbref = (DbRef) this.vobj;
65     DBRefEntry jvobj = (DBRefEntry) this.jvobj;
66     dbref.setAccessionId(jvobj.getAccessionId());
67     dbref.setSource(jvobj.getSource());
68     dbref.setVersion(jvobj.getVersion());
69     if (jvobj.getMap() != null)
70     {
71       // Record mapping to external database coordinate system.
72       jalview.datamodel.Mapping mp = jvobj.getMap();
73       if (mp.getMap() != null)
74       {
75         Map vMap = null;
76         if (dbref.getMapCount() == 0)
77         {
78           vMap = new Map();
79           initMapType(vMap, mp.getMap(), true);
80           dbref.addMap(vMap);
81         }
82         else
83         {
84           // we just update the data anyway.
85           vMap = dbref.getMap(0);
86           initMapType(vMap, mp.getMap(), true);
87         }
88         updateMapTo(mp);
89       }
90     }
91     else
92     {
93       jalview.bin.Cache.log.debug("Ignoring mapless DbRef.Map "
94               + jvobj.getSrcAccString());
95     }
96
97   }
98
99   /**
100    * ugly hack to try to get the embedded sequences within a database reference
101    * to be stored in the document's dataset.
102    * 
103    * @param mp
104    */
105   private void updateMapTo(jalview.datamodel.Mapping mp)
106   {
107     log
108             .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 }