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.AlignedCodonFrame;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.Mapping;
26 import jalview.datamodel.SequenceI;
27 import jalview.gui.Desktop;
28 import jalview.io.VamsasAppDatastore;
30 import java.util.Vector;
32 import uk.ac.vamsas.objects.core.AlignmentSequence;
33 import uk.ac.vamsas.objects.core.DataSet;
34 import uk.ac.vamsas.objects.core.Sequence;
35 import uk.ac.vamsas.objects.core.SequenceMapping;
36 import uk.ac.vamsas.objects.core.SequenceType;
39 * binds a vamsas sequence mapping object from the vamsas document to a maplist
40 * object associated with a mapping in the Jalview model. We use the maplist
41 * object because these are referred to both in the Mapping object associated
42 * with a jalview.datamodel.DBRefEntry and in the array of
43 * jalview.datamodel.AlCodonFrame objects that Jalview uses to propagate
44 * sequence mapping position highlighting across the views.
49 public class Sequencemapping extends Rangetype
51 public Sequencemapping(VamsasAppDatastore datastore,
52 SequenceMapping sequenceMapping)
54 super(datastore, sequenceMapping, jalview.util.MapList.class);
58 private SequenceType from;
62 private Mapping mjvmapping;
65 * create or update a vamsas sequence mapping corresponding to a jalview
66 * Mapping between two dataset sequences
73 public Sequencemapping(VamsasAppDatastore datastore,
74 jalview.datamodel.Mapping mjvmapping,
75 uk.ac.vamsas.objects.core.SequenceType from,
76 uk.ac.vamsas.objects.core.DataSet ds)
78 super(datastore, mjvmapping.getMap(), SequenceMapping.class);
81 this.mjvmapping = mjvmapping;
87 * local check that extant mapping context is valid
89 public void validate()
92 SequenceMapping sequenceMapping = (SequenceMapping) vobj;
93 if (sequenceMapping == null)
97 if (from != null && sequenceMapping.getLoc() != from)
99 jalview.bin.Cache.log.warn("Probable IMPLEMENTATION ERROR: " + from
100 + " doesn't match the local mapping sequence.");
102 if (ds != null && sequenceMapping.is__stored_in_document()
103 && sequenceMapping.getV_parent() != ds)
105 jalview.bin.Cache.log
106 .warn("Probable IMPLEMENTATION ERROR: "
108 + " doesn't match the parent of the bound sequence mapping object.");
112 public void addToDocument()
114 add(mjvmapping, from, ds);
117 public void addFromDocument()
119 add((SequenceMapping) vobj);
122 public void conflict()
124 conflict(mjvmapping, (SequenceMapping) vobj);
128 public void updateToDoc()
130 update(mjvmapping, (SequenceMapping) vobj);
133 public void updateFromDoc()
135 update((SequenceMapping) vobj, (jalview.datamodel.Mapping) jvobj);
138 private void conflict(Mapping mjvmapping, SequenceMapping sequenceMapping)
140 System.err.println("Conflict in update of sequenceMapping "
141 + sequenceMapping.getVorbaId());
144 private void add(Mapping mjvmapping,
145 uk.ac.vamsas.objects.core.SequenceType from, DataSet ds)
147 SequenceI jvto = mjvmapping.getTo();
148 while (jvto.getDatasetSequence() != null)
150 jvto = jvto.getDatasetSequence();
152 SequenceType to = (SequenceType) getjv2vObj(jvto);
155 jalview.bin.Cache.log
156 .warn("FIXME NONFATAL - do a second update: Ignoring Forward Reference to seuqence not yet bound to vamsas seuqence object");
159 SequenceMapping sequenceMapping = new SequenceMapping();
160 sequenceMapping.setLoc(from);
161 sequenceMapping.setMap(to);
162 boolean dnaToProt = false, sense = false;
163 // ensure that we create a mapping with the correct sense
164 if (((Sequence) sequenceMapping.getLoc()).getDictionary().equals(
165 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
167 if (((Sequence) sequenceMapping.getMap()).getDictionary().equals(
168 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA))
176 if (((Sequence) sequenceMapping.getMap()).getDictionary().equals(
177 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
186 jalview.bin.Cache.log
187 .warn("Ignoring Mapping - don't support protein to protein mapping in vamsas document yet.");
192 // locate dataset for storage of SequenceMapping
195 ds = (DataSet) ((uk.ac.vamsas.client.Vobject) sequenceMapping
196 .getLoc()).getV_parent();
200 ds = (DataSet) ((uk.ac.vamsas.client.Vobject) sequenceMapping
201 .getMap()).getV_parent();
206 this.initMapType(sequenceMapping, mjvmapping.getMap(), true);
210 this.initMapType(sequenceMapping, mjvmapping.getMap().getInverse(),
213 ds.addSequenceMapping(sequenceMapping);
214 sequenceMapping.setProvenance(this
215 .dummyProvenance("user defined coding region translation")); // TODO:
224 bindjvvobj(mjvmapping.getMap(), sequenceMapping);
226 jalview.bin.Cache.log.debug("Successfully created mapping "
227 + sequenceMapping.getVorbaId());
230 // private void update(jalview.util.MapList mjvmapping,
231 // SequenceMapping sequenceMapping)
233 jalview.bin.Cache.log
234 .error("Not implemented: Jalview Update Alcodon Mapping:TODO!");
237 private void update(SequenceMapping sequenceMapping,
238 jalview.datamodel.Mapping mjvmapping)
240 jalview.bin.Cache.log
241 .error("Not implemented: Update DBRef Mapping from Jalview");
244 private void update(jalview.datamodel.Mapping mjvmapping,
245 SequenceMapping sequenceMapping)
247 jalview.bin.Cache.log
248 .error("Not implemented: Jalview Update Sequence DBRef Mapping");
252 * bind a SequenceMapping to a live AlCodonFrame element limitations:
253 * Currently, jalview only deals with mappings between dataset sequences, and
254 * even then, only between those that map from DNA to Protein.
256 * @param sequenceMapping
258 private void add(SequenceMapping sequenceMapping)
261 SequenceI from = null, to = null;
262 boolean dnaToProt = false, sense = false;
263 Sequence sdloc = null, sdmap = null;
264 if (sequenceMapping.getLoc() instanceof AlignmentSequence)
266 sdloc = (Sequence) ((AlignmentSequence) sequenceMapping.getLoc())
271 sdloc = ((Sequence) sequenceMapping.getLoc());
273 if (sequenceMapping.getMap() instanceof AlignmentSequence)
275 sdmap = (Sequence) ((AlignmentSequence) sequenceMapping.getMap())
280 sdmap = ((Sequence) sequenceMapping.getMap());
282 if (sdloc == null || sdmap == null)
284 jalview.bin.Cache.log.info("Ignoring non sequence-sequence mapping");
287 mobj = this.getvObj2jv(sdloc);
288 if (mobj instanceof SequenceI)
290 from = (SequenceI) mobj;
292 mobj = this.getvObj2jv(sdmap);
293 if (mobj instanceof SequenceI)
295 to = (SequenceI) mobj;
297 if (from == null || to == null)
300 jalview.bin.Cache.log
301 .error("Probable Vamsas implementation error : unbound dataset sequences involved in a mapping are being parsed!");
305 if (sdloc.getDictionary().equals(
306 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
308 if (sdmap.getDictionary().equals(
309 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA))
320 if (sdmap.getDictionary().equals(
321 uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
327 // create mapping storage object and make each dataset alignment reference
329 AlignmentI dsLoc = (AlignmentI) getvObj2jv(sdloc.getV_parent());
330 AlignmentI dsMap = (AlignmentI) getvObj2jv(sdmap.getV_parent());
331 AlignedCodonFrame acf = new AlignedCodonFrame();
333 if (dsLoc != null && dsLoc != dsMap)
335 dsLoc.addCodonFrame(acf);
339 dsMap.addCodonFrame(acf);
341 // create and add the new mapping to (each) dataset's codonFrame
343 jalview.util.MapList mapping = null;
348 mapping = this.parsemapType(sequenceMapping, 1, 3); // invert sense
349 mapping = new jalview.util.MapList(mapping.getToRanges(),
350 mapping.getFromRanges(), mapping.getToRatio(),
351 mapping.getFromRatio());
352 acf.addMap(to, from, mapping);
356 mapping = this.parsemapType(sequenceMapping, 3, 1); // correct sense
357 acf.addMap(from, to, mapping);
362 mapping = this.parsemapType(sequenceMapping, 1, 1); // correct sense
363 acf.addMap(from, to, mapping);
365 bindjvvobj(mapping, sequenceMapping);
366 jalview.structure.StructureSelectionManager
367 .getStructureSelectionManager(Desktop.instance).addMapping(acf);
368 // Try to link up any conjugate database references in the two sequences
369 // matchConjugateDBRefs(from, to, mapping);
370 // Try to propagate any dbrefs across this mapping.
375 * Complete any 'to' references in jalview.datamodel.Mapping objects
376 * associated with conjugate DBRefEntry under given mapping
379 * sequence corresponding to from reference for sequence mapping
381 * sequence correspondeing to to reference for sequence mapping
383 * maplist parsed in same sense as from and to
385 private void matchConjugateDBRefs(SequenceI from, SequenceI to,
386 jalview.util.MapList smap)
388 if (from.getDBRef() == null && to.getDBRef() == null)
390 if (jalview.bin.Cache.log.isDebugEnabled())
392 jalview.bin.Cache.log.debug("Not matching conjugate refs for "
393 + from.getName() + " and " + to.getName());
397 if (jalview.bin.Cache.log.isDebugEnabled())
399 jalview.bin.Cache.log.debug("Matching conjugate refs for "
400 + from.getName() + " and " + to.getName());
402 jalview.datamodel.DBRefEntry[] fdb = from.getDBRef();
403 jalview.datamodel.DBRefEntry[] tdb = new jalview.datamodel.DBRefEntry[to
405 int tdblen = to.getDBRef().length;
406 System.arraycopy(to.getDBRef(), 0, tdb, 0, tdblen);
407 Vector matched = new Vector();
408 jalview.util.MapList smapI = smap.getInverse();
409 for (int f = 0; f < fdb.length; f++)
411 jalview.datamodel.DBRefEntry fe = fdb[f];
412 jalview.datamodel.Mapping fmp = fe.getMap();
413 boolean fmpnnl = fmp != null;
414 // if (fmpnnl && fmp.getTo()!=null)
416 // jalview.bin.Cache.log.debug("Not overwriting existing To reference in
420 // smap from maps from fe.local to fe.map
421 boolean smapfromlocal2fe = (fmpnnl) ? smap.equals(fmp.getMap())
423 // smap from maps from fe.map to fe.local.
424 boolean smapfromfemap2local = (fmpnnl) ? smapI.equals(fmp.getMap())
426 for (int t = 0; t < tdblen; t++)
428 jalview.datamodel.DBRefEntry te = tdb[t];
431 if (fe.getSource().equals(te.getSource())
432 && fe.getAccessionId().equals(te.getAccessionId()))
434 jalview.datamodel.Mapping tmp = te.getMap();
435 boolean tmpnnl = tmp != null;
436 if (tmpnnl && tmp.getTo() != null)
440 // smap to maps from te.local to te.map
441 boolean smaptolocal2tm = (tmpnnl) ? smap.equals(tmp.getMap())
443 // smap to maps from te.map to te.local
444 boolean smaptotemap2local = (tmpnnl) ? smapI.equals(fmp
446 if (smapfromlocal2fe && smaptotemap2local)
448 // smap implies mapping from to to from
452 else if (smapfromfemap2local && smaptolocal2tm)