refactored to propery separate addToDocument,addFromDocument, updateToDocument, updat...
[jalview.git] / src / jalview / io / vamsas / Dbref.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 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 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    * ugly hack to try to get the embedded sequences within a database reference to be stored in the document's dataset.
99    * @param mp
100    */
101   private void updateMapTo(jalview.datamodel.Mapping mp)
102   {
103     log.info("Performing updateMapTo remove this message when we know what we're doing.");
104     // TODO determine how sequences associated with database mappings are stored in the document
105     if (mp!=null && mp.getTo() != null)
106     {
107       if (mp.getTo().getDatasetSequence() == null)
108       {
109         // sync the dataset sequence, if it hasn't been done already.
110         Datasetsequence dssync = new Datasetsequence(
111                 datastore, mp.getTo(),
112                 (mp.getMappedWidth() == mp.getWidth()) ? sequence
113                         .getDictionary()
114                         : ((mp.getMappedWidth() == 3) ? uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA
115                                 : uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA),
116                 ds);
117         // add a mapping between new dataset sequence and sequence associated with the database reference
118         
119       } else {
120         log.debug("Ignoring non-dataset sequence mapping.");
121       }
122     }
123   }
124   public void updateFromDoc()
125   {
126     DbRef vobj = (DbRef) this.vobj;
127     DBRefEntry jvobj = (DBRefEntry) this.jvobj;
128     jvobj.setAccessionId(vobj.getAccessionId());
129     jvobj.setSource(vobj.getSource());
130     jvobj.setVersion(vobj.getVersion());
131     // add new dbref
132     if (vobj.getMapCount() > 0)
133     {
134       // TODO: Jalview ignores all the other maps
135       if (vobj.getMapCount() > 1)
136       {
137         jalview.bin.Cache.log
138                 .debug("Ignoring additional mappings on DbRef: "
139                         + jvobj.getSource() + ":" + jvobj.getAccessionId());
140       }
141       jalview.datamodel.Mapping mp = new jalview.datamodel.Mapping(
142               parsemapType(vobj.getMap(0)));
143       if (jvobj.getMap()==null || !mp.equals(jvobj.getMap()))
144       {
145         jvobj.setMap(mp);
146       }
147     }
148   }
149   
150   public void conflict()
151   {
152     DbRef vobj = (DbRef) this.vobj;
153     DBRefEntry jvobj = (DBRefEntry) this.jvobj;
154     jalview.bin.Cache.log.debug("Conflict in dbentry update for "
155             + vobj.getAccessionId() + vobj.getSource() + " "
156             + vobj.getVorbaId());
157     // TODO Auto-generated method stub
158
159   }
160
161   public void addFromDocument()
162   {
163     DbRef vobj = (DbRef) this.vobj;
164     DBRefEntry jvobj = (DBRefEntry) this.jvobj;
165     // add new dbref
166     sq.addDBRef(jvobj = new jalview.datamodel.DBRefEntry(vobj.getSource()
167             .toString(), vobj.getVersion().toString(), vobj
168             .getAccessionId().toString()));
169     if (vobj.getMapCount() > 0)
170     {
171       // TODO: Jalview ignores all the other maps
172       if (vobj.getMapCount() > 1)
173       {
174         jalview.bin.Cache.log
175                 .debug("Ignoring additional mappings on DbRef: "
176                         + jvobj.getSource() + ":" + jvobj.getAccessionId());
177       }
178       jalview.datamodel.Mapping mp = new jalview.datamodel.Mapping(
179               parsemapType(vobj.getMap(0)));
180       jvobj.setMap(mp);
181     }
182     // TODO: jalview ignores links and properties because it doesn't know what
183     // to do with them.
184
185     bindjvvobj(jvobj, vobj);
186   }
187
188   public void addToDocument()
189   {
190     DBRefEntry jvobj = (DBRefEntry) this.jvobj;
191     DbRef dbref = new DbRef();
192     bindjvvobj(jvobj, dbref);
193     dbref.setAccessionId(jvobj.getAccessionId());
194     dbref.setSource(jvobj.getSource());
195     dbref.setVersion(jvobj.getVersion());
196     if (jvobj.getMap() != null)
197     {
198       jalview.datamodel.Mapping mp = jvobj.getMap();
199       if (mp.getMap() != null)
200       {
201         Map vMap = new Map();
202         initMapType(vMap, mp.getMap(), true);
203         dbref.addMap(vMap);
204       }
205       else
206       {
207         jalview.bin.Cache.log.debug("Ignoring mapless DbRef.Map "
208                 + jvobj.getSrcAccString());
209       }
210     }
211     sequence.addDbRef(dbref);
212   }
213
214 }