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;
37 public class Sequencemapping extends Rangetype
39 public Sequencemapping(VamsasAppDatastore datastore, SequenceMapping sequenceMapping)
42 Object mjvmapping = getvObj2jv(sequenceMapping);
47 if (sequenceMapping.isUpdated())
49 update((jalview.util.MapList) mjvmapping, sequenceMapping);
54 * create or update a vamsas sequence mapping corresponding to a jalview
55 * Mapping between two dataset sequences
62 public Sequencemapping(VamsasAppDatastore datastore, jalview.datamodel.Mapping mjvmapping, uk.ac.vamsas.objects.core.SequenceType from, uk.ac.vamsas.objects.core.DataSet ds)
65 SequenceMapping sequenceMapping = (SequenceMapping) getjv2vObj(mjvmapping);
66 if (sequenceMapping==null)
68 add(mjvmapping, from, ds);
70 if (from!=null && sequenceMapping.getLoc()!=from)
72 jalview.bin.Cache.log.warn("Probable IMPLEMENTATION ERROR: "+from+" doesn't match the local mapping sequence.");
74 if (ds!=null && sequenceMapping.is__stored_in_document() && sequenceMapping.getV_parent()!=ds)
76 jalview.bin.Cache.log.warn("Probable IMPLEMENTATION ERROR: "+ds+" doesn't match the parent of the bound sequence mapping object.");
78 if (sequenceMapping.isUpdated())
80 conflict(mjvmapping, sequenceMapping);
82 update(mjvmapping, sequenceMapping);
86 private void conflict(Mapping mjvmapping, SequenceMapping sequenceMapping)
88 System.err.println("Conflict in update of sequenceMapping "+sequenceMapping.getVorbaId());
90 private void add(Mapping mjvmapping, uk.ac.vamsas.objects.core.SequenceType from, DataSet ds)
92 SequenceI jvto = mjvmapping.getTo();
93 while (jvto.getDatasetSequence()!=null)
95 jvto = jvto.getDatasetSequence();
97 SequenceType to = (SequenceType) getjv2vObj(jvto);
100 jalview.bin.Cache.log.warn("NONFATAL - do a second update: Ignoring Forward Reference to seuqence not yet bound to vamsas seuqence object");
103 SequenceMapping sequenceMapping = new SequenceMapping();
104 sequenceMapping.setLoc(from);
105 sequenceMapping.setMap(to);
106 boolean dnaToProt=false,sense=false;
107 // ensure that we create a mapping with the correct sense
108 if (((Sequence) sequenceMapping.getLoc())
110 .equals(uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
112 if (((Sequence) sequenceMapping.getMap())
114 .equals(uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA))
120 if (((Sequence) sequenceMapping.getMap())
122 .equals(uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
131 jalview.bin.Cache.log.warn("Ignoring Mapping - don't support protein to protein mapping in vamsas document yet.");
136 // locate dataset for storage of SequenceMapping
139 ds = (DataSet) ((uk.ac.vamsas.client.Vobject) sequenceMapping.getLoc()).getV_parent();
141 ds = (DataSet) ((uk.ac.vamsas.client.Vobject) sequenceMapping.getMap()).getV_parent();
146 this.initMapType(sequenceMapping, mjvmapping.getMap(), true);
148 this.initMapType(sequenceMapping, mjvmapping.getMap().getInverse(), true);
150 ds.addSequenceMapping(sequenceMapping);
151 sequenceMapping.setProvenance(this.dummyProvenance("user defined coding region translation")); // TODO:
160 bindjvvobj(mjvmapping, sequenceMapping);
162 jalview.bin.Cache.log.debug("Successfully created mapping "+sequenceMapping.getVorbaId());
164 private void update(jalview.util.MapList mjvmapping, SequenceMapping sequenceMapping)
166 jalview.bin.Cache.log.error("Not implemented: Jalview Update Alcodon Mapping:TODO!");
168 private void update(jalview.datamodel.Mapping mjvmapping, SequenceMapping sequenceMapping)
170 jalview.bin.Cache.log.error("Not implemented: Jalview Update Sequence DBRef Mapping");
173 * limitations: Currently, jalview only deals with mappings between dataset
174 * sequences, and even then, only between those that map from DNA to Protein.
176 * @param sequenceMapping
178 private void add(SequenceMapping sequenceMapping)
181 SequenceI from=null,to=null;
182 boolean dnaToProt=false,sense=false;
183 Sequence sdloc=null, sdmap=null;
184 if (sequenceMapping.getLoc() instanceof AlignmentSequence)
186 sdloc = (Sequence) ((AlignmentSequence) sequenceMapping.getLoc()).getRefid();
188 sdloc = ((Sequence) sequenceMapping.getLoc());
190 if (sequenceMapping.getMap() instanceof AlignmentSequence)
192 sdmap = (Sequence) ((AlignmentSequence) sequenceMapping.getMap()).getRefid();
194 sdmap = ((Sequence) sequenceMapping.getMap());
196 if (sdloc==null || sdmap == null)
198 jalview.bin.Cache.log.info("Ignoring non sequence-sequence mapping");
201 mobj = this.getvObj2jv((Vobject) sdloc);
202 if (mobj instanceof SequenceI)
204 from = (SequenceI) mobj;
206 mobj = this.getvObj2jv((Vobject) sdmap);
207 if (mobj instanceof SequenceI)
209 to = (SequenceI) mobj;
211 if (from == null || to == null)
214 jalview.bin.Cache.log.error("Probable Vamsas implementation error : unbound dataset sequences involved in a mapping are being parsed!");
218 if (sdloc.getDictionary()
219 .equals(uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
221 if (sdmap.getDictionary()
222 .equals(uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA))
231 if (sdmap.getDictionary()
232 .equals(uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA))
238 // create mapping storage object and make each dataset alignment reference
240 jalview.datamodel.AlignmentI dsLoc = (jalview.datamodel.AlignmentI) getvObj2jv(sdloc.getV_parent());
241 jalview.datamodel.AlignmentI dsMap = (jalview.datamodel.AlignmentI) getvObj2jv(sdmap.getV_parent());
242 AlignedCodonFrame afc = new AlignedCodonFrame(0);
244 if (dsLoc!=null && dsLoc!=dsMap)
246 dsLoc.addCodonFrame(afc);
250 dsMap.addCodonFrame(afc);
252 // create and add the new mapping to (each) dataset's codonFrame
254 jalview.util.MapList mapping = null;
257 mapping = this.parsemapType(sequenceMapping, 1, 3); // invert sense
258 mapping = new jalview.util.MapList(mapping.getToRanges(), mapping.getFromRanges(), mapping.getToRatio(), mapping.getFromRatio());
259 afc.addMap(to, from, mapping);
261 mapping = this.parsemapType(sequenceMapping, 3, 1); // correct sense
262 afc.addMap(from, to, mapping);
264 bindjvvobj(mapping, sequenceMapping);
265 jalview.structure.StructureSelectionManager.getStructureSelectionManager().addMappings(new AlignedCodonFrame[] { afc });
266 // Try to link up any conjugate database references in the two sequences
267 // matchConjugateDBRefs(from, to, mapping);
268 // Try to propagate any dbrefs across this mapping.
272 * Complete any 'to' references in jalview.datamodel.Mapping objects associated with conjugate DBRefEntry under given mapping
273 * @param from sequence corresponding to from reference for sequence mapping
274 * @param to sequence correspondeing to to reference for sequence mapping
275 * @param smap maplist parsed in same sense as from and to
277 private void matchConjugateDBRefs(SequenceI from, SequenceI to,
278 jalview.util.MapList smap)
280 if (from.getDBRef()==null && to.getDBRef()==null)
282 if (jalview.bin.Cache.log.isDebugEnabled())
284 jalview.bin.Cache.log.debug("Not matching conjugate refs for "+from.getName()+" and "+to.getName());
288 if (jalview.bin.Cache.log.isDebugEnabled())
290 jalview.bin.Cache.log.debug("Matching conjugate refs for "+from.getName()+" and "+to.getName());
292 jalview.datamodel.DBRefEntry[] fdb = from.getDBRef();
293 jalview.datamodel.DBRefEntry[] tdb = new jalview.datamodel.DBRefEntry[to.getDBRef().length];
294 int tdblen = to.getDBRef().length;
295 System.arraycopy(to.getDBRef(), 0, tdb, 0, tdblen);
296 Vector matched = new Vector();
297 jalview.util.MapList smapI = smap.getInverse();
298 for (int f=0;f<fdb.length; f++)
300 jalview.datamodel.DBRefEntry fe = fdb[f];
301 jalview.datamodel.Mapping fmp = fe.getMap();
302 boolean fmpnnl = fmp!=null;
303 // if (fmpnnl && fmp.getTo()!=null)
305 // jalview.bin.Cache.log.debug("Not overwriting existing To reference in "+fe);
308 // smap from maps from fe.local to fe.map
309 boolean smapfromlocal2fe = (fmpnnl) ? smap.equals(fmp.getMap()) : false;
310 // smap from maps from fe.map to fe.local.
311 boolean smapfromfemap2local = (fmpnnl) ? smapI.equals(fmp.getMap()) : false;
312 for (int t=0; t<tdblen; t++)
314 jalview.datamodel.DBRefEntry te = tdb[t];
317 if (fe.getSource().equals(te.getSource())
318 && fe.getAccessionId().equals(te.getAccessionId()))
320 jalview.datamodel.Mapping tmp = te.getMap();
321 boolean tmpnnl = tmp!=null;
322 if (tmpnnl && tmp.getTo()!=null)
326 // smap to maps from te.local to te.map
327 boolean smaptolocal2tm = (tmpnnl) ? smap.equals(tmp.getMap()) : false;
328 // smap to maps from te.map to te.local
329 boolean smaptotemap2local = (tmpnnl) ? smapI.equals(fmp.getMap()) : false;
330 if (smapfromlocal2fe && smaptotemap2local)
332 // smap implies mapping from to to from
336 if (smapfromfemap2local && smaptolocal2tm) {