2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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 java.util.Vector;
22 import jalview.datamodel.AlignedCodonFrame;
23 import jalview.datamodel.Mapping;
24 import jalview.datamodel.SequenceI;
25 import jalview.gui.Desktop;
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 a maplist
39 * object associated with a mapping in the Jalview model. We use the maplist
40 * object because these are referred to both in the Mapping object associated
41 * with a jalview.datamodel.DBRefEntry and in the array of
42 * jalview.datamodel.AlCodonFrame objects that Jalview uses to propagate
43 * sequence mapping position highlighting across the views.
48 public class Sequencemapping extends Rangetype
50 public Sequencemapping(VamsasAppDatastore datastore,
51 SequenceMapping sequenceMapping)
53 super(datastore, sequenceMapping, jalview.util.MapList.class);
57 private SequenceType from;
61 private Mapping mjvmapping;
64 * create or update a vamsas sequence mapping corresponding to a jalview
65 * Mapping between two dataset sequences
72 public Sequencemapping(VamsasAppDatastore datastore,
73 jalview.datamodel.Mapping mjvmapping,
74 uk.ac.vamsas.objects.core.SequenceType from,
75 uk.ac.vamsas.objects.core.DataSet ds)
77 super(datastore, mjvmapping.getMap(), SequenceMapping.class);
80 this.mjvmapping = mjvmapping;
86 * local check that extant mapping context is valid
88 public void validate()
91 SequenceMapping sequenceMapping = (SequenceMapping) vobj;
92 if (sequenceMapping == null)
96 if (from != null && sequenceMapping.getLoc() != from)
98 jalview.bin.Cache.log.warn("Probable IMPLEMENTATION ERROR: " + from
99 + " doesn't match the local mapping sequence.");
101 if (ds != null && sequenceMapping.is__stored_in_document()
102 && sequenceMapping.getV_parent() != ds)
104 jalview.bin.Cache.log
105 .warn("Probable IMPLEMENTATION ERROR: "
107 + " doesn't match the parent of the bound sequence mapping object.");
111 public void addToDocument()
113 add(mjvmapping, from, ds);
116 public void addFromDocument()
118 add((SequenceMapping) vobj);
121 public void conflict()
123 conflict(mjvmapping, (SequenceMapping) vobj);
127 public void updateToDoc()
129 update(mjvmapping, (SequenceMapping) vobj);
132 public void updateFromDoc()
134 update((SequenceMapping) vobj, (jalview.datamodel.Mapping) jvobj);
137 private void conflict(Mapping mjvmapping, SequenceMapping sequenceMapping)
139 System.err.println("Conflict in update of sequenceMapping "
140 + sequenceMapping.getVorbaId());
143 private void add(Mapping mjvmapping,
144 uk.ac.vamsas.objects.core.SequenceType from, DataSet ds)
146 SequenceI jvto = mjvmapping.getTo();
147 while (jvto.getDatasetSequence() != null)
149 jvto = jvto.getDatasetSequence();
151 SequenceType to = (SequenceType) getjv2vObj(jvto);
154 jalview.bin.Cache.log
155 .warn("FIXME NONFATAL - do a second update: Ignoring Forward Reference to seuqence not yet bound to vamsas seuqence object");
158 SequenceMapping sequenceMapping = new SequenceMapping();
159 sequenceMapping.setLoc(from);
160 sequenceMapping.setMap(to);
161 boolean dnaToProt = false, sense = false;
162 // ensure that we create a mapping with the correct sense
163 if (((Sequence) sequenceMapping.getLoc()).getDictionary().equals(
164 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
166 if (((Sequence) sequenceMapping.getMap()).getDictionary().equals(
167 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA))
175 if (((Sequence) sequenceMapping.getMap()).getDictionary().equals(
176 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
185 jalview.bin.Cache.log
186 .warn("Ignoring Mapping - don't support protein to protein mapping in vamsas document yet.");
191 // locate dataset for storage of SequenceMapping
194 ds = (DataSet) ((uk.ac.vamsas.client.Vobject) sequenceMapping
195 .getLoc()).getV_parent();
199 ds = (DataSet) ((uk.ac.vamsas.client.Vobject) sequenceMapping
200 .getMap()).getV_parent();
205 this.initMapType(sequenceMapping, mjvmapping.getMap(), true);
209 this.initMapType(sequenceMapping, mjvmapping.getMap().getInverse(),
212 ds.addSequenceMapping(sequenceMapping);
213 sequenceMapping.setProvenance(this
214 .dummyProvenance("user defined coding region translation")); // TODO:
223 bindjvvobj(mjvmapping.getMap(), sequenceMapping);
225 jalview.bin.Cache.log.debug("Successfully created mapping "
226 + sequenceMapping.getVorbaId());
229 // private void update(jalview.util.MapList mjvmapping,
230 // SequenceMapping sequenceMapping)
232 jalview.bin.Cache.log
233 .error("Not implemented: Jalview Update Alcodon Mapping:TODO!");
236 private void update(SequenceMapping sequenceMapping,
237 jalview.datamodel.Mapping mjvmapping)
239 jalview.bin.Cache.log
240 .error("Not implemented: Update DBRef Mapping from Jalview");
243 private void update(jalview.datamodel.Mapping mjvmapping,
244 SequenceMapping sequenceMapping)
246 jalview.bin.Cache.log
247 .error("Not implemented: Jalview Update Sequence DBRef Mapping");
251 * bind a SequenceMapping to a live AlCodonFrame element limitations:
252 * Currently, jalview only deals with mappings between dataset sequences, and
253 * even then, only between those that map from DNA to Protein.
255 * @param sequenceMapping
257 private void add(SequenceMapping sequenceMapping)
260 SequenceI from = null, to = null;
261 boolean dnaToProt = false, sense = false;
262 Sequence sdloc = null, sdmap = null;
263 if (sequenceMapping.getLoc() instanceof AlignmentSequence)
265 sdloc = (Sequence) ((AlignmentSequence) sequenceMapping.getLoc())
270 sdloc = ((Sequence) sequenceMapping.getLoc());
272 if (sequenceMapping.getMap() instanceof AlignmentSequence)
274 sdmap = (Sequence) ((AlignmentSequence) sequenceMapping.getMap())
279 sdmap = ((Sequence) sequenceMapping.getMap());
281 if (sdloc == null || sdmap == null)
283 jalview.bin.Cache.log.info("Ignoring non sequence-sequence mapping");
286 mobj = this.getvObj2jv((Vobject) sdloc);
287 if (mobj instanceof SequenceI)
289 from = (SequenceI) mobj;
291 mobj = this.getvObj2jv((Vobject) sdmap);
292 if (mobj instanceof SequenceI)
294 to = (SequenceI) mobj;
296 if (from == null || to == null)
299 jalview.bin.Cache.log
300 .error("Probable Vamsas implementation error : unbound dataset sequences involved in a mapping are being parsed!");
304 if (sdloc.getDictionary().equals(
305 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
307 if (sdmap.getDictionary().equals(
308 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA))
319 if (sdmap.getDictionary().equals(
320 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
326 // create mapping storage object and make each dataset alignment reference
328 jalview.datamodel.AlignmentI dsLoc = (jalview.datamodel.AlignmentI) getvObj2jv(sdloc
330 jalview.datamodel.AlignmentI dsMap = (jalview.datamodel.AlignmentI) getvObj2jv(sdmap
332 AlignedCodonFrame afc = new AlignedCodonFrame(0);
334 if (dsLoc != null && dsLoc != dsMap)
336 dsLoc.addCodonFrame(afc);
340 dsMap.addCodonFrame(afc);
342 // create and add the new mapping to (each) dataset's codonFrame
344 jalview.util.MapList mapping = null;
349 mapping = this.parsemapType(sequenceMapping, 1, 3); // invert sense
350 mapping = new jalview.util.MapList(mapping.getToRanges(),
351 mapping.getFromRanges(), mapping.getToRatio(),
352 mapping.getFromRatio());
353 afc.addMap(to, from, mapping);
357 mapping = this.parsemapType(sequenceMapping, 3, 1); // correct sense
358 afc.addMap(from, to, mapping);
363 mapping = this.parsemapType(sequenceMapping, 1, 1); // correct sense
364 afc.addMap(from, to, mapping);
366 bindjvvobj(mapping, sequenceMapping);
367 jalview.structure.StructureSelectionManager
368 .getStructureSelectionManager(Desktop.instance).addMappings(
369 new AlignedCodonFrame[]
371 // Try to link up any conjugate database references in the two sequences
372 // matchConjugateDBRefs(from, to, mapping);
373 // Try to propagate any dbrefs across this mapping.
378 * Complete any 'to' references in jalview.datamodel.Mapping objects
379 * associated with conjugate DBRefEntry under given mapping
382 * sequence corresponding to from reference for sequence mapping
384 * sequence correspondeing to to reference for sequence mapping
386 * maplist parsed in same sense as from and to
388 private void matchConjugateDBRefs(SequenceI from, SequenceI to,
389 jalview.util.MapList smap)
391 if (from.getDBRef() == null && to.getDBRef() == null)
393 if (jalview.bin.Cache.log.isDebugEnabled())
395 jalview.bin.Cache.log.debug("Not matching conjugate refs for "
396 + from.getName() + " and " + to.getName());
400 if (jalview.bin.Cache.log.isDebugEnabled())
402 jalview.bin.Cache.log.debug("Matching conjugate refs for "
403 + from.getName() + " and " + to.getName());
405 jalview.datamodel.DBRefEntry[] fdb = from.getDBRef();
406 jalview.datamodel.DBRefEntry[] tdb = new jalview.datamodel.DBRefEntry[to
408 int tdblen = to.getDBRef().length;
409 System.arraycopy(to.getDBRef(), 0, tdb, 0, tdblen);
410 Vector matched = new Vector();
411 jalview.util.MapList smapI = smap.getInverse();
412 for (int f = 0; f < fdb.length; f++)
414 jalview.datamodel.DBRefEntry fe = fdb[f];
415 jalview.datamodel.Mapping fmp = fe.getMap();
416 boolean fmpnnl = fmp != null;
417 // if (fmpnnl && fmp.getTo()!=null)
419 // jalview.bin.Cache.log.debug("Not overwriting existing To reference in
423 // smap from maps from fe.local to fe.map
424 boolean smapfromlocal2fe = (fmpnnl) ? smap.equals(fmp.getMap())
426 // smap from maps from fe.map to fe.local.
427 boolean smapfromfemap2local = (fmpnnl) ? smapI.equals(fmp.getMap())
429 for (int t = 0; t < tdblen; t++)
431 jalview.datamodel.DBRefEntry te = tdb[t];
434 if (fe.getSource().equals(te.getSource())
435 && fe.getAccessionId().equals(te.getAccessionId()))
437 jalview.datamodel.Mapping tmp = te.getMap();
438 boolean tmpnnl = tmp != null;
439 if (tmpnnl && tmp.getTo() != null)
443 // smap to maps from te.local to te.map
444 boolean smaptolocal2tm = (tmpnnl) ? smap.equals(tmp.getMap())
446 // smap to maps from te.map to te.local
447 boolean smaptotemap2local = (tmpnnl) ? smapI.equals(fmp
449 if (smapfromlocal2fe && smaptotemap2local)
451 // smap implies mapping from to to from
455 else if (smapfromfemap2local && smaptolocal2tm)