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
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.
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.
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
19 package jalview.io.vamsas;
21 import java.util.Vector;
23 import jalview.datamodel.AlignedCodonFrame;
24 import jalview.datamodel.Mapping;
25 import jalview.datamodel.SequenceI;
26 import jalview.io.VamsasAppDatastore;
27 import uk.ac.vamsas.client.Vobject;
28 import uk.ac.vamsas.objects.core.AlignmentSequence;
29 import uk.ac.vamsas.objects.core.DataSet;
30 import uk.ac.vamsas.objects.core.Local;
31 import uk.ac.vamsas.objects.core.RangeType;
32 import uk.ac.vamsas.objects.core.Seg;
33 import uk.ac.vamsas.objects.core.Sequence;
34 import uk.ac.vamsas.objects.core.SequenceMapping;
35 import uk.ac.vamsas.objects.core.SequenceType;
38 * binds a vamsas sequence mapping object from the vamsas document to
39 * a maplist object associated with a mapping in the Jalview model.
40 * We use the maplist object because these are referred to both in
41 * the Mapping object associated with a jalview.datamodel.DBRefEntry
42 * and in the array of jalview.datamodel.AlCodonFrame objects that
43 * Jalview uses to propagate sequence mapping position highlighting
48 public class Sequencemapping extends Rangetype
50 public Sequencemapping(VamsasAppDatastore datastore,
51 SequenceMapping sequenceMapping)
53 super(datastore, sequenceMapping, jalview.util.MapList.class);
56 private SequenceType from;
58 private Mapping mjvmapping;
61 * create or update a vamsas sequence mapping corresponding to a jalview
62 * Mapping between two dataset sequences
69 public Sequencemapping(VamsasAppDatastore datastore,
70 jalview.datamodel.Mapping mjvmapping,
71 uk.ac.vamsas.objects.core.SequenceType from,
72 uk.ac.vamsas.objects.core.DataSet ds)
74 super(datastore, mjvmapping.getMap(), SequenceMapping.class);
77 this.mjvmapping = mjvmapping;
82 * local check that extant mapping context is valid
84 public void validate()
87 SequenceMapping sequenceMapping = (SequenceMapping) vobj;
88 if (sequenceMapping==null)
92 if (from != null && sequenceMapping.getLoc() != from)
94 jalview.bin.Cache.log.warn("Probable IMPLEMENTATION ERROR: " + from
95 + " doesn't match the local mapping sequence.");
97 if (ds != null && sequenceMapping.is__stored_in_document()
98 && sequenceMapping.getV_parent() != ds)
100 jalview.bin.Cache.log
101 .warn("Probable IMPLEMENTATION ERROR: "
103 + " doesn't match the parent of the bound sequence mapping object.");
107 public void addToDocument()
109 add(mjvmapping, from, ds);
112 public void addFromDocument()
114 add((SequenceMapping) vobj);
117 public void conflict()
119 conflict(mjvmapping, (SequenceMapping) vobj);
123 public void updateToDoc()
125 update(mjvmapping, (SequenceMapping) vobj);
127 public void updateFromDoc()
129 update((SequenceMapping) vobj, (jalview.datamodel.Mapping) jvobj);
132 private void conflict(Mapping mjvmapping, SequenceMapping sequenceMapping)
134 System.err.println("Conflict in update of sequenceMapping "
135 + sequenceMapping.getVorbaId());
138 private void add(Mapping mjvmapping,
139 uk.ac.vamsas.objects.core.SequenceType from, DataSet ds)
141 SequenceI jvto = mjvmapping.getTo();
142 while (jvto.getDatasetSequence() != null)
144 jvto = jvto.getDatasetSequence();
146 SequenceType to = (SequenceType) getjv2vObj(jvto);
149 jalview.bin.Cache.log
150 .warn("FIXME NONFATAL - do a second update: Ignoring Forward Reference to seuqence not yet bound to vamsas seuqence object");
153 SequenceMapping sequenceMapping = new SequenceMapping();
154 sequenceMapping.setLoc(from);
155 sequenceMapping.setMap(to);
156 boolean dnaToProt = false, sense = false;
157 // ensure that we create a mapping with the correct sense
158 if (((Sequence) sequenceMapping.getLoc()).getDictionary().equals(
159 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
161 if (((Sequence) sequenceMapping.getMap()).getDictionary().equals(
162 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA))
170 if (((Sequence) sequenceMapping.getMap()).getDictionary().equals(
171 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
180 jalview.bin.Cache.log
181 .warn("Ignoring Mapping - don't support protein to protein mapping in vamsas document yet.");
186 // locate dataset for storage of SequenceMapping
189 ds = (DataSet) ((uk.ac.vamsas.client.Vobject) sequenceMapping
190 .getLoc()).getV_parent();
194 ds = (DataSet) ((uk.ac.vamsas.client.Vobject) sequenceMapping
195 .getMap()).getV_parent();
200 this.initMapType(sequenceMapping, mjvmapping.getMap(), true);
204 this.initMapType(sequenceMapping, mjvmapping.getMap().getInverse(),
207 ds.addSequenceMapping(sequenceMapping);
208 sequenceMapping.setProvenance(this
209 .dummyProvenance("user defined coding region translation")); // TODO:
218 bindjvvobj(mjvmapping.getMap(), sequenceMapping);
220 jalview.bin.Cache.log.debug("Successfully created mapping "
221 + sequenceMapping.getVorbaId());
224 // private void update(jalview.util.MapList mjvmapping,
225 // SequenceMapping sequenceMapping)
227 jalview.bin.Cache.log
228 .error("Not implemented: Jalview Update Alcodon Mapping:TODO!");
231 private void update(SequenceMapping sequenceMapping,
232 jalview.datamodel.Mapping mjvmapping)
234 jalview.bin.Cache.log
235 .error("Not implemented: Update DBRef Mapping from Jalview");
237 private void update(jalview.datamodel.Mapping mjvmapping,
238 SequenceMapping sequenceMapping)
240 jalview.bin.Cache.log
241 .error("Not implemented: Jalview Update Sequence DBRef Mapping");
245 * bind a SequenceMapping to a live AlCodonFrame element
246 * limitations: Currently, jalview only deals with mappings between dataset
247 * sequences, and even then, only between those that map from DNA to Protein.
249 * @param sequenceMapping
251 private void add(SequenceMapping sequenceMapping)
254 SequenceI from = null, to = null;
255 boolean dnaToProt = false, sense = false;
256 Sequence sdloc = null, sdmap = null;
257 if (sequenceMapping.getLoc() instanceof AlignmentSequence)
259 sdloc = (Sequence) ((AlignmentSequence) sequenceMapping.getLoc())
264 sdloc = ((Sequence) sequenceMapping.getLoc());
266 if (sequenceMapping.getMap() instanceof AlignmentSequence)
268 sdmap = (Sequence) ((AlignmentSequence) sequenceMapping.getMap())
273 sdmap = ((Sequence) sequenceMapping.getMap());
275 if (sdloc == null || sdmap == null)
277 jalview.bin.Cache.log.info("Ignoring non sequence-sequence mapping");
280 mobj = this.getvObj2jv((Vobject) sdloc);
281 if (mobj instanceof SequenceI)
283 from = (SequenceI) mobj;
285 mobj = this.getvObj2jv((Vobject) sdmap);
286 if (mobj instanceof SequenceI)
288 to = (SequenceI) mobj;
290 if (from == null || to == null)
293 jalview.bin.Cache.log
294 .error("Probable Vamsas implementation error : unbound dataset sequences involved in a mapping are being parsed!");
298 if (sdloc.getDictionary().equals(
299 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
301 if (sdmap.getDictionary().equals(
302 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA))
313 if (sdmap.getDictionary().equals(
314 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
320 // create mapping storage object and make each dataset alignment reference
322 jalview.datamodel.AlignmentI dsLoc = (jalview.datamodel.AlignmentI) getvObj2jv(sdloc
324 jalview.datamodel.AlignmentI dsMap = (jalview.datamodel.AlignmentI) getvObj2jv(sdmap
326 AlignedCodonFrame afc = new AlignedCodonFrame(0);
328 if (dsLoc != null && dsLoc != dsMap)
330 dsLoc.addCodonFrame(afc);
334 dsMap.addCodonFrame(afc);
336 // create and add the new mapping to (each) dataset's codonFrame
338 jalview.util.MapList mapping = null;
343 mapping = this.parsemapType(sequenceMapping, 1, 3); // invert sense
344 mapping = new jalview.util.MapList(mapping.getToRanges(), mapping
345 .getFromRanges(), mapping.getToRatio(), mapping
347 afc.addMap(to, from, mapping);
351 mapping = this.parsemapType(sequenceMapping, 3, 1); // correct sense
352 afc.addMap(from, to, mapping);
355 mapping = this.parsemapType(sequenceMapping, 1, 1); // correct sense
356 afc.addMap(from, to, mapping);
358 bindjvvobj(mapping, sequenceMapping);
359 jalview.structure.StructureSelectionManager
360 .getStructureSelectionManager().addMappings(
361 new AlignedCodonFrame[]
363 // Try to link up any conjugate database references in the two sequences
364 // matchConjugateDBRefs(from, to, mapping);
365 // Try to propagate any dbrefs across this mapping.
370 * Complete any 'to' references in jalview.datamodel.Mapping objects
371 * associated with conjugate DBRefEntry under given mapping
374 * sequence corresponding to from reference for sequence
377 * sequence correspondeing to to reference for sequence mapping
379 * maplist parsed in same sense as from and to
381 private void matchConjugateDBRefs(SequenceI from, SequenceI to,
382 jalview.util.MapList smap)
384 if (from.getDBRef() == null && to.getDBRef() == null)
386 if (jalview.bin.Cache.log.isDebugEnabled())
388 jalview.bin.Cache.log.debug("Not matching conjugate refs for "
389 + from.getName() + " and " + to.getName());
393 if (jalview.bin.Cache.log.isDebugEnabled())
395 jalview.bin.Cache.log.debug("Matching conjugate refs for "
396 + from.getName() + " and " + to.getName());
398 jalview.datamodel.DBRefEntry[] fdb = from.getDBRef();
399 jalview.datamodel.DBRefEntry[] tdb = new jalview.datamodel.DBRefEntry[to
401 int tdblen = to.getDBRef().length;
402 System.arraycopy(to.getDBRef(), 0, tdb, 0, tdblen);
403 Vector matched = new Vector();
404 jalview.util.MapList smapI = smap.getInverse();
405 for (int f = 0; f < fdb.length; f++)
407 jalview.datamodel.DBRefEntry fe = fdb[f];
408 jalview.datamodel.Mapping fmp = fe.getMap();
409 boolean fmpnnl = fmp != null;
410 // if (fmpnnl && fmp.getTo()!=null)
412 // jalview.bin.Cache.log.debug("Not overwriting existing To reference in
416 // smap from maps from fe.local to fe.map
417 boolean smapfromlocal2fe = (fmpnnl) ? smap.equals(fmp.getMap())
419 // smap from maps from fe.map to fe.local.
420 boolean smapfromfemap2local = (fmpnnl) ? smapI.equals(fmp.getMap())
422 for (int t = 0; t < tdblen; t++)
424 jalview.datamodel.DBRefEntry te = tdb[t];
427 if (fe.getSource().equals(te.getSource())
428 && fe.getAccessionId().equals(te.getAccessionId()))
430 jalview.datamodel.Mapping tmp = te.getMap();
431 boolean tmpnnl = tmp != null;
432 if (tmpnnl && tmp.getTo() != null)
436 // smap to maps from te.local to te.map
437 boolean smaptolocal2tm = (tmpnnl) ? smap.equals(tmp.getMap())
439 // smap to maps from te.map to te.local
440 boolean smaptotemap2local = (tmpnnl) ? smapI.equals(fmp
442 if (smapfromlocal2fe && smaptotemap2local)
444 // smap implies mapping from to to from
448 else if (smapfromfemap2local && smaptolocal2tm)