2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.io.vamsas;
20 import jalview.datamodel.DBRefEntry;
21 import jalview.datamodel.SequenceI;
22 import jalview.io.VamsasAppDatastore;
23 import uk.ac.vamsas.objects.core.DataSet;
24 import uk.ac.vamsas.objects.core.DbRef;
25 import uk.ac.vamsas.objects.core.Sequence;
28 * synchronize a vamsas dataset sequence with a jalview dataset sequence. This
29 * class deals with all sequence features and database references associated
30 * with the jalview sequence.
35 public class Datasetsequence extends DatastoreItem
39 private DataSet dataset;
41 // private AlignmentI jvdset;
43 public Datasetsequence(VamsasAppDatastore vamsasAppDatastore,
44 SequenceI sq, String dict, DataSet dataset)
46 super(vamsasAppDatastore, sq, uk.ac.vamsas.objects.core.Sequence.class);
47 this.dataset = dataset;
48 // this.jvdset = jvdset;
53 public Datasetsequence(VamsasAppDatastore vamsasAppDatastore,
56 super(vamsasAppDatastore, vdseq, SequenceI.class);
60 public void addFromDocument()
62 Sequence vseq = (Sequence) vobj;
63 SequenceI dsseq = new jalview.datamodel.Sequence(vseq.getName(),
64 vseq.getSequence(), (int) vseq.getStart(), (int) vseq.getEnd());
65 dsseq.setDescription(vseq.getDescription());
66 bindjvvobj(dsseq, vseq);
67 dsseq.setVamsasId(vseq.getVorbaId().getId());
72 public void updateFromDoc()
74 Sequence sq = (Sequence) vobj;
75 SequenceI sequence = (SequenceI) jvobj;
76 if (!sequence.getSequenceAsString().equals(sq.getSequence()))
78 log.warn("Potential Client Error ! - mismatch of dataset sequence: and jalview internal dataset sequence.");
82 // verify and update principal attributes.
83 if (sequence.getDescription() != null
84 && (sequence.getDescription() == null || !sequence
85 .getDescription().equals(sq.getDescription())))
87 sequence.setDescription(sq.getDescription());
90 if (sequence.getSequence() == null
91 || !sequence.getSequenceAsString().equals(sq.getSequence()))
93 if (sequence.getStart() != sq.getStart()
94 || sequence.getEnd() != sq.getEnd())
96 // update modified sequence.
97 sequence.setSequence(sq.getSequence());
98 sequence.setStart((int) sq.getStart());
99 sequence.setEnd((int) sq.getEnd());
103 if (!sequence.getName().equals(sq.getName()))
105 sequence.setName(sq.getName());
108 modified |= updateJvDbRefs();
109 // updateJvFeatures();
114 * private void updateJvFeatures() { Sequence vsq = (Sequence) vobj;
116 * // add or update any new features/references on dataset sequence if
117 * (vsq.getgetSequenceFeatures() != null) { int sfSize =
118 * sq.getSequenceFeatures().length;
120 * for (int sf = 0; sf < sfSize; sf++) { new
121 * jalview.io.vamsas.Sequencefeature(datastore,
122 * (jalview.datamodel.SequenceFeature) sq .getSequenceFeatures()[sf], dataset,
123 * (Sequence) vobj); } } }
125 private boolean updateSqFeatures()
127 boolean modified = false;
128 SequenceI sq = (SequenceI) jvobj;
130 // add or update any new features/references on dataset sequence
131 if (sq.getSequenceFeatures() != null)
133 int sfSize = sq.getSequenceFeatures().length;
135 for (int sf = 0; sf < sfSize; sf++)
137 modified |= new jalview.io.vamsas.Sequencefeature(datastore,
138 (jalview.datamodel.SequenceFeature) sq
139 .getSequenceFeatures()[sf], dataset,
140 (Sequence) vobj).docWasUpdated();
146 public void addToDocument()
148 SequenceI sq = (SequenceI) jvobj;
149 Sequence sequence = new Sequence();
150 bindjvvobj(sq, sequence);
151 sq.setVamsasId(sequence.getVorbaId().getId());
152 sequence.setSequence(sq.getSequenceAsString());
153 sequence.setDictionary(dict);
154 sequence.setName(sq.getName());
155 sequence.setStart(sq.getStart());
156 sequence.setEnd(sq.getEnd());
157 sequence.setDescription(sq.getDescription());
158 dataset.addSequence(sequence);
161 updateDbRefs();// sq,(Sequence) vobj, dataset);
165 * sync database references from jv to document
167 * @return true if document was modified
169 private boolean updateDbRefs()
171 boolean modifiedthedoc = false;
172 SequenceI sq = (SequenceI) jvobj;
174 if (sq.getDatasetSequence() == null && sq.getDBRef() != null)
176 // only sync database references for dataset sequences
177 DBRefEntry[] entries = sq.getDBRef();
178 // jalview.datamodel.DBRefEntry dbentry;
179 for (int db = 0; db < entries.length; db++)
181 modifiedthedoc |= new jalview.io.vamsas.Dbref(datastore,
183 entries[db], sq, (Sequence) vobj, dataset).docWasUpdated();
188 return modifiedthedoc;
192 * sync database references from document to jv sequence
194 * @return true if local sequence refs were modified
196 private boolean updateJvDbRefs()
198 boolean modifiedtheseq = false;
199 SequenceI sq = (SequenceI) jvobj;
200 Sequence vsq = (Sequence) vobj;
201 if (vsq.getDbRefCount() > 0)
203 // only sync database references for dataset sequences
204 DbRef[] entries = vsq.getDbRef();
206 for (int db = 0; db < entries.length; db++)
208 modifiedtheseq |= new jalview.io.vamsas.Dbref(datastore,
210 entries[db], vsq, sq).jvWasUpdated();
213 return modifiedtheseq;
216 public void conflict()
218 log.warn("Conflict in dataset sequence update to document. Overwriting document");
219 // TODO: could try to import from document data to jalview first. and then
223 boolean modified = false;
225 public void updateToDoc()
227 SequenceI sq = (SequenceI) jvobj;
228 Sequence sequence = (Sequence) vobj;
229 // verify and update principal attributes.
230 if (sequence.getDescription() != null
231 && (sequence.getDescription() == null || !sequence
232 .getDescription().equals(sq.getDescription())))
234 sequence.setDescription(sq.getDescription());
237 if (sequence.getSequence() == null
238 || !sequence.getSequence().equals(sq.getSequenceAsString()))
240 if (sequence.getStart() != sq.getStart()
241 || sequence.getEnd() != sq.getEnd())
243 // update modified sequence.
244 sequence.setSequence(sq.getSequenceAsString());
245 sequence.setStart(sq.getStart());
246 sequence.setEnd(sq.getEnd());
250 if (!dict.equals(sequence.getDictionary()))
252 sequence.setDictionary(dict);
255 if (!sequence.getName().equals(sq.getName()))
257 sequence.setName(sq.getName());
260 modified |= updateDbRefs();
261 modified |= updateSqFeatures();
265 * (probably could just do vobj.isModified(), but..)
267 * @return true if document's dataset sequence was modified
269 public boolean getModified()