merge from 2_4_Release branch
[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.DbRef;
24 import uk.ac.vamsas.objects.core.Map;
25 import uk.ac.vamsas.objects.core.Sequence;
26 import jalview.io.VamsasAppDatastore;
27
28 public class Dbref extends Rangetype
29 {
30   jalview.datamodel.SequenceI sq = null;
31
32   uk.ac.vamsas.objects.core.Sequence sequence = null;
33
34   DbRef dbref = null;
35
36   DBRefEntry dbentry = null;
37
38   public Dbref(VamsasAppDatastore datastore, DBRefEntry dbentry,
39           jalview.datamodel.SequenceI sq2,
40           uk.ac.vamsas.objects.core.Sequence sequence2)
41   {
42     super(datastore);
43     dbref = (DbRef) getjv2vObj(dbentry);
44     sq = sq2;
45     sequence = sequence2;
46     this.dbentry = dbentry;
47     if (dbref == null)
48     {
49       add();
50     }
51     else
52     {
53       if (dbref.isUpdated())
54       {
55         conflict();
56       }
57       else
58       {
59         update();
60       }
61
62     }
63
64   }
65
66   public Dbref(VamsasAppDatastore datastore, DbRef ref, Sequence vdseq,
67           SequenceI dsseq)
68   {
69     super(datastore);
70     dbref = ref;
71     sequence = vdseq;
72     sq = dsseq;
73     dbentry = (jalview.datamodel.DBRefEntry) getvObj2jv(dbref);
74     if (dbentry == null)
75     {
76       addFromDocument();
77     }
78     else
79     {
80       if (dbref.isUpdated())
81       {
82         update();
83       }
84     }
85   }
86
87   private void update()
88   {
89     // TODO: verify and update dbrefs in vamsas document
90     // there will be trouble when a dataset sequence is modified to
91     // contain more residues than were originally referenced - we must
92     // then make a number of dataset sequence entries - this info is already
93     // stored
94     jalview.bin.Cache.log
95             .debug("TODO verify update of dataset sequence database references.");
96   }
97
98   private void conflict()
99   {
100     jalview.bin.Cache.log.debug("Conflict in dbentry update for "
101             + dbref.getAccessionId() + dbref.getSource() + " "
102             + dbref.getVorbaId());
103     // TODO Auto-generated method stub
104
105   }
106
107   private void addFromDocument()
108   {
109     // add new dbref
110     sq.addDBRef(dbentry = new jalview.datamodel.DBRefEntry(dbref
111             .getSource().toString(), dbref.getVersion().toString(), dbref
112             .getAccessionId().toString()));
113     if (dbref.getMapCount() > 0)
114     {
115       // TODO: Jalview ignores all the other maps
116       if (dbref.getMapCount() > 1)
117       {
118         jalview.bin.Cache.log
119                 .debug("Ignoring additional mappings on DbRef: "
120                         + dbentry.getSource() + ":"
121                         + dbentry.getAccessionId());
122       }
123       jalview.datamodel.Mapping mp = new jalview.datamodel.Mapping(
124               parsemapType(dbref.getMap(0)));
125       dbentry.setMap(mp);
126     }
127     // TODO: jalview ignores links and properties because it doesn't know what
128     // to do with them.
129
130     bindjvvobj(dbentry, dbref);
131   }
132
133   private void add()
134   {
135     DbRef dbref = new DbRef();
136     bindjvvobj(dbentry, dbref);
137     dbref.setAccessionId(dbentry.getAccessionId());
138     dbref.setSource(dbentry.getSource());
139     dbref.setVersion(dbentry.getVersion());
140     if (dbentry.getMap() != null)
141     {
142       jalview.datamodel.Mapping mp = dbentry.getMap();
143       if (mp.getMap() != null)
144       {
145         Map vMap = new Map();
146         initMapType(vMap, mp.getMap(), true);
147         dbref.addMap(vMap);
148       }
149       else
150       {
151         jalview.bin.Cache.log.debug("Ignoring mapless DbRef.Map "
152                 + dbentry.getSrcAccString());
153       }
154     }
155     sequence.addDbRef(dbref);
156   }
157
158 }