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