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.bin.Cache;
24 import jalview.datamodel.AlignedCodonFrame;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.Mapping;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.Desktop;
30 import jalview.io.VamsasAppDatastore;
31 import jalview.structure.StructureSelectionManager;
32 import jalview.util.MapList;
34 import java.util.Vector;
36 import uk.ac.vamsas.objects.core.AlignmentSequence;
37 import uk.ac.vamsas.objects.core.DataSet;
38 import uk.ac.vamsas.objects.core.Sequence;
39 import uk.ac.vamsas.objects.core.SequenceMapping;
40 import uk.ac.vamsas.objects.core.SequenceType;
41 import uk.ac.vamsas.objects.utils.SymbolDictionary;
44 * binds a vamsas sequence mapping object from the vamsas document to a maplist
45 * object associated with a mapping in the Jalview model. We use the maplist
46 * object because these are referred to both in the Mapping object associated
47 * with a DBRefEntry and in the array of jalview.datamodel.AlCodonFrame objects
48 * that Jalview uses to propagate sequence mapping position highlighting across
54 public class Sequencemapping extends Rangetype
56 public Sequencemapping(VamsasAppDatastore datastore,
57 SequenceMapping sequenceMapping)
59 super(datastore, sequenceMapping, MapList.class);
63 private SequenceType from;
67 private Mapping mjvmapping;
70 * create or update a vamsas sequence mapping corresponding to a jalview
71 * Mapping between two dataset sequences
78 public Sequencemapping(VamsasAppDatastore datastore,
80 uk.ac.vamsas.objects.core.SequenceType from,
81 uk.ac.vamsas.objects.core.DataSet ds)
83 super(datastore, mjvmapping.getMap(), SequenceMapping.class);
86 this.mjvmapping = mjvmapping;
92 * local check that extant mapping context is valid
94 public void validate()
97 SequenceMapping sequenceMapping = (SequenceMapping) vobj;
98 if (sequenceMapping == null)
102 if (from != null && sequenceMapping.getLoc() != from)
104 Cache.log.warn("Probable IMPLEMENTATION ERROR: " + from
105 + " doesn't match the local mapping sequence.");
107 if (ds != null && sequenceMapping.is__stored_in_document()
108 && sequenceMapping.getV_parent() != ds)
111 .warn("Probable IMPLEMENTATION ERROR: "
113 + " doesn't match the parent of the bound sequence mapping object.");
117 public void addToDocument()
119 add(mjvmapping, from, ds);
122 public void addFromDocument()
124 add((SequenceMapping) vobj);
127 public void conflict()
129 conflict(mjvmapping, (SequenceMapping) vobj);
133 public void updateToDoc()
135 update(mjvmapping, (SequenceMapping) vobj);
138 public void updateFromDoc()
140 update((SequenceMapping) vobj, (Mapping) jvobj);
143 private void conflict(Mapping mjvmapping, SequenceMapping sequenceMapping)
145 System.err.println("Conflict in update of sequenceMapping "
146 + sequenceMapping.getVorbaId());
149 private void add(Mapping mjvmapping,
150 uk.ac.vamsas.objects.core.SequenceType from, DataSet ds)
152 SequenceI jvto = mjvmapping.getTo();
153 while (jvto.getDatasetSequence() != null)
155 jvto = jvto.getDatasetSequence();
157 SequenceType to = (SequenceType) getjv2vObj(jvto);
161 .warn("FIXME NONFATAL - do a second update: Ignoring Forward Reference to seuqence not yet bound to vamsas seuqence object");
164 SequenceMapping sequenceMapping = new SequenceMapping();
165 sequenceMapping.setLoc(from);
166 sequenceMapping.setMap(to);
167 boolean dnaToProt = false, sense = false;
168 // ensure that we create a mapping with the correct sense
169 if (((Sequence) sequenceMapping.getLoc()).getDictionary().equals(
170 SymbolDictionary.STANDARD_NA))
172 if (((Sequence) sequenceMapping.getMap()).getDictionary().equals(
173 SymbolDictionary.STANDARD_AA))
181 if (((Sequence) sequenceMapping.getMap()).getDictionary().equals(
182 SymbolDictionary.STANDARD_NA))
192 .warn("Ignoring Mapping - don't support protein to protein mapping in vamsas document yet.");
197 // locate dataset for storage of SequenceMapping
200 ds = (DataSet) ((uk.ac.vamsas.client.Vobject) sequenceMapping
201 .getLoc()).getV_parent();
205 ds = (DataSet) ((uk.ac.vamsas.client.Vobject) sequenceMapping
206 .getMap()).getV_parent();
211 this.initMapType(sequenceMapping, mjvmapping.getMap(), true);
215 this.initMapType(sequenceMapping, mjvmapping.getMap().getInverse(),
218 ds.addSequenceMapping(sequenceMapping);
219 sequenceMapping.setProvenance(this
220 .dummyProvenance("user defined coding region translation")); // TODO:
229 bindjvvobj(mjvmapping.getMap(), sequenceMapping);
231 Cache.log.debug("Successfully created mapping "
232 + sequenceMapping.getVorbaId());
235 // private void update(MapList mjvmapping,
236 // SequenceMapping sequenceMapping)
239 .error("Not implemented: Jalview Update Alcodon Mapping:TODO!");
242 private void update(SequenceMapping sequenceMapping,
246 .error("Not implemented: Update DBRef Mapping from Jalview");
249 private void update(Mapping mjvmapping,
250 SequenceMapping sequenceMapping)
253 .error("Not implemented: Jalview Update Sequence DBRef Mapping");
257 * bind a SequenceMapping to a live AlCodonFrame element limitations:
258 * Currently, jalview only deals with mappings between dataset sequences, and
259 * even then, only between those that map from DNA to Protein.
261 * @param sequenceMapping
263 private void add(SequenceMapping sequenceMapping)
266 SequenceI from = null, to = null;
267 boolean dnaToProt = false, sense = false;
268 Sequence sdloc = null, sdmap = null;
269 if (sequenceMapping.getLoc() instanceof AlignmentSequence)
271 sdloc = (Sequence) ((AlignmentSequence) sequenceMapping.getLoc())
276 sdloc = ((Sequence) sequenceMapping.getLoc());
278 if (sequenceMapping.getMap() instanceof AlignmentSequence)
280 sdmap = (Sequence) ((AlignmentSequence) sequenceMapping.getMap())
285 sdmap = ((Sequence) sequenceMapping.getMap());
287 if (sdloc == null || sdmap == null)
289 Cache.log.info("Ignoring non sequence-sequence mapping");
292 mobj = this.getvObj2jv(sdloc);
293 if (mobj instanceof SequenceI)
295 from = (SequenceI) mobj;
297 mobj = this.getvObj2jv(sdmap);
298 if (mobj instanceof SequenceI)
300 to = (SequenceI) mobj;
302 if (from == null || to == null)
306 .error("Probable Vamsas implementation error : unbound dataset sequences involved in a mapping are being parsed!");
310 if (sdloc.getDictionary().equals(
311 SymbolDictionary.STANDARD_NA))
313 if (sdmap.getDictionary().equals(
314 SymbolDictionary.STANDARD_AA))
325 if (sdmap.getDictionary().equals(
326 SymbolDictionary.STANDARD_NA))
332 // create mapping storage object and make each dataset alignment reference
334 AlignmentI dsLoc = (AlignmentI) getvObj2jv(sdloc.getV_parent());
335 AlignmentI dsMap = (AlignmentI) getvObj2jv(sdmap.getV_parent());
336 AlignedCodonFrame acf = new AlignedCodonFrame();
338 if (dsLoc != null && dsLoc != dsMap)
340 dsLoc.addCodonFrame(acf);
344 dsMap.addCodonFrame(acf);
346 // create and add the new mapping to (each) dataset's codonFrame
348 MapList mapping = null;
353 mapping = this.parsemapType(sequenceMapping, 1, 3); // invert sense
354 mapping = new MapList(mapping.getToRanges(),
355 mapping.getFromRanges(), mapping.getToRatio(),
356 mapping.getFromRatio());
357 acf.addMap(to, from, mapping);
361 mapping = this.parsemapType(sequenceMapping, 3, 1); // correct sense
362 acf.addMap(from, to, mapping);
367 mapping = this.parsemapType(sequenceMapping, 1, 1); // correct sense
368 acf.addMap(from, to, mapping);
370 bindjvvobj(mapping, sequenceMapping);
371 StructureSelectionManager
372 .getStructureSelectionManager(Desktop.instance).addMapping(acf);
373 // Try to link up any conjugate database references in the two sequences
374 // matchConjugateDBRefs(from, to, mapping);
375 // Try to propagate any dbrefs across this mapping.
380 * Complete any 'to' references in Mapping objects associated with conjugate
381 * DBRefEntry under given mapping
384 * sequence corresponding to from reference for sequence mapping
386 * sequence correspondeing to to reference for sequence mapping
388 * maplist parsed in same sense as from and to
390 private void matchConjugateDBRefs(SequenceI from, SequenceI to,
393 if (from.getDBRef() == null && to.getDBRef() == null)
395 if (Cache.log.isDebugEnabled())
397 Cache.log.debug("Not matching conjugate refs for "
398 + from.getName() + " and " + to.getName());
402 if (Cache.log.isDebugEnabled())
404 Cache.log.debug("Matching conjugate refs for "
405 + from.getName() + " and " + to.getName());
407 DBRefEntry[] fdb = from.getDBRef();
408 DBRefEntry[] tdb = new DBRefEntry[to
410 int tdblen = to.getDBRef().length;
411 System.arraycopy(to.getDBRef(), 0, tdb, 0, tdblen);
412 Vector matched = new Vector();
413 MapList smapI = smap.getInverse();
414 for (int f = 0; f < fdb.length; f++)
416 DBRefEntry fe = fdb[f];
417 Mapping fmp = fe.getMap();
418 boolean fmpnnl = fmp != null;
419 // if (fmpnnl && fmp.getTo()!=null)
421 // Cache.log.debug("Not overwriting existing To reference in
425 // smap from maps from fe.local to fe.map
426 boolean smapfromlocal2fe = (fmpnnl) ? smap.equals(fmp.getMap())
428 // smap from maps from fe.map to fe.local.
429 boolean smapfromfemap2local = (fmpnnl) ? smapI.equals(fmp.getMap())
431 for (int t = 0; t < tdblen; t++)
433 DBRefEntry te = tdb[t];
436 if (fe.getSource().equals(te.getSource())
437 && fe.getAccessionId().equals(te.getAccessionId()))
439 Mapping tmp = te.getMap();
440 boolean tmpnnl = tmp != null;
441 if (tmpnnl && tmp.getTo() != null)
445 // smap to maps from te.local to te.map
446 boolean smaptolocal2tm = (tmpnnl) ? smap.equals(tmp.getMap())
448 // smap to maps from te.map to te.local
449 boolean smaptotemap2local = (tmpnnl) ? smapI.equals(fmp
451 if (smapfromlocal2fe && smaptotemap2local)
453 // smap implies mapping from to to from
457 else if (smapfromfemap2local && smaptolocal2tm)