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