2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
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.
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.
21 package jalview.io.vamsas;
23 import jalview.datamodel.DBRefEntry;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.io.VamsasAppDatastore;
28 import java.util.List;
30 import uk.ac.vamsas.objects.core.DataSet;
31 import uk.ac.vamsas.objects.core.DbRef;
32 import uk.ac.vamsas.objects.core.Sequence;
35 * synchronize a vamsas dataset sequence with a jalview dataset sequence. This
36 * class deals with all sequence features and database references associated
37 * with the jalview sequence.
42 public class Datasetsequence extends DatastoreItem
46 private DataSet dataset;
48 // private AlignmentI jvdset;
50 public Datasetsequence(VamsasAppDatastore vamsasAppDatastore,
51 SequenceI sq, String theDict, DataSet theDataset)
53 super(vamsasAppDatastore, sq, uk.ac.vamsas.objects.core.Sequence.class);
54 this.dataset = theDataset;
55 // this.jvdset = jvdset;
60 public Datasetsequence(VamsasAppDatastore vamsasAppDatastore,
63 super(vamsasAppDatastore, vdseq, SequenceI.class);
68 public void addFromDocument()
70 Sequence vseq = (Sequence) vobj;
71 SequenceI dsseq = new jalview.datamodel.Sequence(vseq.getName(),
72 vseq.getSequence(), (int) vseq.getStart(), (int) vseq.getEnd());
73 dsseq.setDescription(vseq.getDescription());
74 bindjvvobj(dsseq, vseq);
75 dsseq.setVamsasId(vseq.getVorbaId().getId());
81 public void updateFromDoc()
83 Sequence sq = (Sequence) vobj;
84 SequenceI sequence = (SequenceI) jvobj;
85 if (!sequence.getSequenceAsString().equals(sq.getSequence()))
88 "Potential Client Error ! - mismatch of dataset sequence: and jalview internal dataset sequence.");
92 // verify and update principal attributes.
93 if (sequence.getDescription() != null
94 && (sequence.getDescription() == null || !sequence
95 .getDescription().equals(sq.getDescription())))
97 sequence.setDescription(sq.getDescription());
100 if (sequence.getSequence() == null
101 || !sequence.getSequenceAsString().equals(sq.getSequence()))
103 if (sequence.getStart() != sq.getStart()
104 || sequence.getEnd() != sq.getEnd())
106 // update modified sequence.
107 sequence.setSequence(sq.getSequence());
108 sequence.setStart((int) sq.getStart());
109 sequence.setEnd((int) sq.getEnd());
113 if (!sequence.getName().equals(sq.getName()))
115 sequence.setName(sq.getName());
118 modified |= updateJvDbRefs();
119 // updateJvFeatures();
124 * private void updateJvFeatures() { Sequence vsq = (Sequence) vobj;
126 * // add or update any new features/references on dataset sequence if
127 * (vsq.getgetSequenceFeatures() != null) { int sfSize =
128 * sq.getSequenceFeatures().length;
130 * for (int sf = 0; sf < sfSize; sf++) { new
131 * jalview.io.vamsas.Sequencefeature(datastore,
132 * (jalview.datamodel.SequenceFeature) sq .getSequenceFeatures()[sf], dataset,
133 * (Sequence) vobj); } } }
135 private boolean updateSqFeatures()
137 boolean changed = false;
138 SequenceI sq = (SequenceI) jvobj;
140 // add or update any new features/references on dataset sequence
141 List<SequenceFeature> sfs = sq.getSequenceFeatures();
142 for (SequenceFeature sf : sfs)
144 changed |= new jalview.io.vamsas.Sequencefeature(datastore, sf,
145 dataset, (Sequence) vobj).docWasUpdated();
152 public void addToDocument()
154 SequenceI sq = (SequenceI) jvobj;
155 Sequence sequence = new Sequence();
156 bindjvvobj(sq, sequence);
157 sq.setVamsasId(sequence.getVorbaId().getId());
158 sequence.setSequence(sq.getSequenceAsString());
159 sequence.setDictionary(dict);
160 sequence.setName(sq.getName());
161 sequence.setStart(sq.getStart());
162 sequence.setEnd(sq.getEnd());
163 sequence.setDescription(sq.getDescription());
164 dataset.addSequence(sequence);
167 updateDbRefs();// sq,(Sequence) vobj, dataset);
171 * sync database references from jv to document
173 * @return true if document was modified
175 private boolean updateDbRefs()
177 boolean modifiedthedoc = false;
178 SequenceI sq = (SequenceI) jvobj;
180 if (sq.getDatasetSequence() == null && sq.getDBRefs() != null)
182 // only sync database references for dataset sequences
183 List<DBRefEntry> entries = sq.getDBRefs();
184 // jalview.datamodel.DBRefEntry dbentry;
185 for (int db = 0, dn = entries.size(); db < dn; db++)
187 modifiedthedoc |= new jalview.io.vamsas.Dbref(datastore,
189 entries.get(db), sq, (Sequence) vobj, dataset)
195 return modifiedthedoc;
199 * sync database references from document to jv sequence
201 * @return true if local sequence refs were modified
203 private boolean updateJvDbRefs()
205 boolean modifiedtheseq = false;
206 SequenceI sq = (SequenceI) jvobj;
207 Sequence vsq = (Sequence) vobj;
208 if (vsq.getDbRefCount() > 0)
210 // only sync database references for dataset sequences
211 DbRef[] entries = vsq.getDbRef();
213 for (int db = 0; db < entries.length; db++)
215 modifiedtheseq |= new jalview.io.vamsas.Dbref(datastore,
217 entries[db], vsq, sq).jvWasUpdated();
220 return modifiedtheseq;
224 public void conflict()
227 "Conflict in dataset sequence update to document. Overwriting document");
228 // TODO: could try to import from document data to jalview first. and then
232 boolean modified = false;
235 public void updateToDoc()
237 SequenceI sq = (SequenceI) jvobj;
238 Sequence sequence = (Sequence) vobj;
239 // verify and update principal attributes.
240 if (sequence.getDescription() != null
241 && (sequence.getDescription() == null || !sequence
242 .getDescription().equals(sq.getDescription())))
244 sequence.setDescription(sq.getDescription());
247 if (sequence.getSequence() == null
248 || !sequence.getSequence().equals(sq.getSequenceAsString()))
250 if (sequence.getStart() != sq.getStart()
251 || sequence.getEnd() != sq.getEnd())
253 // update modified sequence.
254 sequence.setSequence(sq.getSequenceAsString());
255 sequence.setStart(sq.getStart());
256 sequence.setEnd(sq.getEnd());
260 if (!dict.equals(sequence.getDictionary()))
262 sequence.setDictionary(dict);
265 if (!sequence.getName().equals(sq.getName()))
267 sequence.setName(sq.getName());
270 modified |= updateDbRefs();
271 modified |= updateSqFeatures();
275 * (probably could just do vobj.isModified(), but..)
277 * @return true if document's dataset sequence was modified
279 public boolean getModified()