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 java.io.IOException;
24 import java.util.Enumeration;
25 import java.util.Hashtable;
26 import java.util.List;
27 import java.util.Vector;
29 import jalview.analysis.NJTree;
30 import jalview.bin.Cache;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.AlignmentView;
33 import jalview.datamodel.BinaryNode;
34 import jalview.datamodel.SeqCigar;
35 import jalview.datamodel.Sequence;
36 import jalview.datamodel.SequenceI;
37 import jalview.datamodel.SequenceNode;
38 import jalview.gui.TreePanel;
39 import jalview.io.NewickFile;
40 import jalview.io.VamsasAppDatastore;
41 import jalview.viewmodel.AlignmentViewport;
42 import uk.ac.vamsas.client.Vobject;
43 import uk.ac.vamsas.objects.core.AlignmentSequence;
44 import uk.ac.vamsas.objects.core.Entry;
45 import uk.ac.vamsas.objects.core.Input;
46 import uk.ac.vamsas.objects.core.Newick;
47 import uk.ac.vamsas.objects.core.Param;
48 import uk.ac.vamsas.objects.core.Provenance;
49 import uk.ac.vamsas.objects.core.Seg;
50 import uk.ac.vamsas.objects.core.Treenode;
51 import uk.ac.vamsas.objects.core.Vref;
53 public class Tree extends DatastoreItem
59 uk.ac.vamsas.objects.core.Tree tree;
61 uk.ac.vamsas.objects.core.Alignment alignment; // may be null => dataset or
64 private NewickFile ntree;
68 private AlignmentView inputData = null;
70 public static void updateFrom(VamsasAppDatastore datastore,
71 jalview.gui.AlignFrame alignFrame,
72 uk.ac.vamsas.objects.core.Tree vtree)
74 Tree toTree = new Tree(datastore, alignFrame, vtree);
77 public Tree(VamsasAppDatastore datastore,
78 jalview.gui.AlignFrame alignFrame,
79 uk.ac.vamsas.objects.core.Tree vtree)
81 super(datastore, vtree, TreePanel.class);
85 private NewickFile getNtree() throws IOException
87 return new jalview.io.NewickFile(tree.getNewick(0).getContent());
90 public Tree(VamsasAppDatastore datastore, TreePanel tp2, AlignmentI jal2,
91 uk.ac.vamsas.objects.core.Alignment alignment2)
93 super(datastore, tp2, uk.ac.vamsas.objects.core.Tree.class);
96 tp = (TreePanel) jvobj;
97 alignment = alignment2;
99 tree = (uk.ac.vamsas.objects.core.Tree) vobj;
106 * @see jalview.io.vamsas.DatastoreItem#addFromDocument()
109 public void addFromDocument()
111 tree = (uk.ac.vamsas.objects.core.Tree) vobj; // vtree;
112 TreePanel tp = (TreePanel) jvobj; // getvObj2jv(tree);
114 Object[] idata = recoverInputData(tree.getProvenance());
117 if (idata != null && idata[0] != null)
119 inputData = (AlignmentView) idata[0];
122 title = tree.getNewick(0).getTitle();
123 if (title == null || title.length() == 0)
125 title = tree.getTitle(); // hack!!!!
127 } catch (Exception e)
129 Cache.log.warn("Problems parsing treefile '"
130 + tree.getNewick(0).getContent() + "'", e);
137 * @see jalview.io.vamsas.DatastoreItem#conflict()
140 public void conflict()
143 .info("Update (with conflict) from vamsas document to alignment associated tree not implemented yet.");
149 * @see jalview.io.vamsas.DatastoreItem#update()
152 public void updateToDoc()
154 if (isModifiable(tree.getModifiable()))
156 // synchronize(); // update();
157 // verify any changes.
158 log.info("TODO: Update tree in document from jalview.");
163 log.info("TODO: Add the locally modified tree in Jalview as a new tree in document, leaving locked tree unchanged.");
170 * @see jalview.io.vamsas.DatastoreItem#updateFromDoc()
173 public void updateFromDoc()
175 // should probably just open a new tree panel in the same place as the old
177 // TODO: Tree.updateFromDoc
179 * TreePanel tp = (TreePanel) jvobj; // getvObj2jv(tree);
181 * // make a new tree Object[] idata =
182 * recoverInputData(tree.getProvenance()); try { if (idata != null &&
183 * idata[0] != null) { inputData = (AlignmentView) idata[0]; } ntree =
184 * getNtree(); title = tree.getNewick(0).getTitle(); if (title == null ||
185 * title.length() == 0) { title = tree.getTitle(); // hack!!!! } } catch
186 * (Exception e) { Cache.log.warn("Problems parsing treefile '" +
187 * tree.getNewick(0).getContent() + "'", e); }
189 log.debug("Update the local tree in jalview from the document.");
191 if (isModifiable(tree.getModifiable()))
193 // synchronize(); // update();
194 // verify any changes.
195 log.debug("Update tree in document from jalview.");
200 log.debug("Add modified jalview tree as new tree in document.");
205 * correctly creates provenance for trees calculated on an alignment by
212 private Provenance makeTreeProvenance(AlignmentI jal, TreePanel tp)
214 Cache.log.debug("Making Tree provenance for " + tp.getTitle());
215 Provenance prov = new Provenance();
216 prov.addEntry(new Entry());
217 prov.getEntry(0).setAction("imported " + tp.getTitle());
218 prov.getEntry(0).setUser(provEntry.getUser());
219 prov.getEntry(0).setApp(provEntry.getApp());
220 prov.getEntry(0).setDate(provEntry.getDate());
221 if (tp.getTree().hasOriginalSequenceData())
223 Input vInput = new Input();
224 // LATER: check to see if tree input data is contained in this alignment -
225 // or just correctly resolve the tree's seqData to the correct alignment
228 Vector alsqrefs = getjv2vObjs(findAlignmentSequences(jal,
229 tp.getTree().seqData.getSequences()));
230 Object[] alsqs = new Object[alsqrefs.size()];
231 alsqrefs.copyInto(alsqs);
232 vInput.setObjRef(alsqs);
233 // now create main provenance data
234 prov.getEntry(0).setAction("created " + tp.getTitle());
235 prov.getEntry(0).addInput(vInput);
236 // jalview's special input parameter for distance matrix calculations
237 vInput.setName("jalview:seqdist"); // TODO: settle on appropriate name.
238 prov.getEntry(0).addParam(new Param());
239 prov.getEntry(0).getParam(0).setName("treeType");
240 prov.getEntry(0).getParam(0).setType("utf8");
241 prov.getEntry(0).getParam(0).setContent("NJ"); // TODO: type of tree is a
243 int ranges[] = tp.getTree().seqData.getVisibleContigs();
244 // VisibleContigs are with respect to alignment coordinates. Still need
246 int start = tp.getTree().seqData.getAlignmentOrigin();
247 for (int r = 0; r < ranges.length; r += 2)
249 Seg visSeg = new Seg();
250 visSeg.setStart(1 + start + ranges[r]);
251 visSeg.setEnd(start + ranges[r + 1]);
252 visSeg.setInclusive(true);
253 vInput.addSeg(visSeg);
256 Cache.log.debug("Finished Tree provenance for " + tp.getTitle());
261 * look up SeqCigars in an existing alignment.
265 * @return vector of alignment sequences in order of SeqCigar array (but
266 * missing unfound seqcigars)
268 private Vector findAlignmentSequences(AlignmentI jal, SeqCigar[] sequences)
270 SeqCigar[] tseqs = new SeqCigar[sequences.length];
271 System.arraycopy(sequences, 0, tseqs, 0, sequences.length);
272 Vector alsq = new Vector();
273 List<SequenceI> jalsqs;
274 synchronized (jalsqs = jal.getSequences())
276 for (SequenceI asq : jalsqs)
278 for (int t = 0; t < sequences.length; t++)
281 && (tseqs[t].getRefSeq() == asq || tseqs[t].getRefSeq() == asq
282 .getDatasetSequence()))
283 // && tseqs[t].getStart()>=asq.getStart() &&
284 // tseqs[t].getEnd()<=asq.getEnd())
292 if (alsq.size() < sequences.length)
294 .warn("Not recovered all alignment sequences for given set of input sequence CIGARS");
300 * Update jalview newick representation with TreeNode map
303 * the treepanel that this tree is bound to.
305 public void UpdateSequenceTreeMap(TreePanel tp)
307 if (tp == null || tree == null)
309 Vector leaves = new Vector();
310 if (tp.getTree() == null)
312 Cache.log.warn("Not updating SequenceTreeMap for "
313 + tree.getVorbaId());
316 tp.getTree().findLeaves(tp.getTree().getTopNode(), leaves);
317 Treenode[] tn = tree.getTreenode(); // todo: select nodes for this
324 Treenode node = tn[i++];
325 BinaryNode mappednode = findNodeSpec(node.getNodespec(), leaves);
326 if (mappednode != null && mappednode instanceof SequenceNode)
328 SequenceNode leaf = (SequenceNode) mappednode;
329 // check if we can make the specified association
331 int vrf = 0, refv = 0;
332 while (jvseq == null && vrf < node.getVrefCount())
334 if (refv < node.getVref(vrf).getRefsCount())
336 Object noderef = node.getVref(vrf).getRefs(refv++);
337 if (noderef instanceof AlignmentSequence)
339 // we only make these kind of associations
340 jvseq = getvObj2jv((Vobject) noderef);
349 if (jvseq instanceof SequenceI)
351 leaf.setElement(jvseq);
352 leaf.setPlaceholder(false);
356 leaf.setPlaceholder(true);
357 leaf.setElement(new Sequence(leaf.getName(), "THISISAPLACEHLDER"));
363 // / TODO: refactor to vamsas :start
365 * construct treenode mappings for mapped sequences
371 public Treenode[] makeTreeNodes(NJTree ntree, Newick newick)
373 Vector leaves = new Vector();
374 ntree.findLeaves(ntree.getTopNode(), leaves);
375 Vector tnv = new Vector();
376 Enumeration l = leaves.elements();
377 Hashtable nodespecs = new Hashtable();
378 while (l.hasMoreElements())
380 jalview.datamodel.BinaryNode tnode = (jalview.datamodel.BinaryNode) l
382 if (tnode instanceof jalview.datamodel.SequenceNode)
384 if (!((jalview.datamodel.SequenceNode) tnode).isPlaceholder())
386 Object assocseq = ((jalview.datamodel.SequenceNode) tnode)
388 if (assocseq instanceof SequenceI)
390 Vobject vobj = this.getjv2vObj(assocseq);
393 Treenode node = new Treenode();
394 if (newick.isRegisterable())
396 this.cdoc.registerObject(newick);
397 node.addTreeId(newick);
399 node.setNodespec(makeNodeSpec(nodespecs, tnode));
400 node.setName(tnode.getName());
401 Vref vr = new Vref();
404 tnv.addElement(node);
408 System.err.println("WARNING: Unassociated treeNode "
409 + tnode.element().toString()
411 + ((tnode.getName() != null) ? " label "
412 + tnode.getName() : ""));
420 Treenode[] tn = new Treenode[tnv.size()];
424 return new Treenode[]
428 private String makeNodeSpec(Hashtable nodespecs,
429 jalview.datamodel.BinaryNode tnode)
431 String nname = new String(tnode.getName());
432 Integer nindx = (Integer) nodespecs.get(nname);
435 nindx = new Integer(1);
437 nname = nindx.toString() + " " + nname;
442 * call to match up Treenode specs to NJTree parsed from document object.
446 * as returned from NJTree.findLeaves( .., ..) ..
449 private jalview.datamodel.BinaryNode findNodeSpec(String nodespec,
453 String nspec = nodespec.substring(nodespec.indexOf(' ') + 1);
454 String oval = nodespec.substring(0, nodespec.indexOf(' '));
457 occurence = new Integer(oval).intValue();
458 } catch (Exception e)
460 System.err.println("Invalid nodespec '" + nodespec + "'");
463 jalview.datamodel.BinaryNode bn = null;
466 Enumeration en = leaves.elements();
467 while (en.hasMoreElements() && nocc < occurence)
469 bn = (jalview.datamodel.BinaryNode) en.nextElement();
470 if (bn instanceof jalview.datamodel.SequenceNode
471 && bn.getName().equals(nspec))
481 // todo: end refactor to vamsas library
483 * add jalview object to vamsas document
487 public void addToDocument()
489 tree = new uk.ac.vamsas.objects.core.Tree();
490 bindjvvobj(tp, tree);
491 tree.setTitle(tp.getTitle());
492 Newick newick = new Newick();
493 newick.setContent(tp.getTree().toString());
494 newick.setTitle(tp.getTitle());
495 tree.addNewick(newick);
496 tree.setProvenance(makeTreeProvenance(jal, tp));
497 tree.setTreenode(makeTreeNodes(tp.getTree(), newick));
499 alignment.addTree(tree);
503 * note: this function assumes that all sequence and alignment objects
504 * referenced in input data has already been associated with jalview objects.
508 * @return Object[] { AlignmentView, AlignmentI - reference alignment for
511 public Object[] recoverInputData(Provenance tp)
513 AlignmentViewport javport = null;
514 jalview.datamodel.AlignmentI jal = null;
515 jalview.datamodel.CigarArray view = null;
516 for (int pe = 0; pe < tp.getEntryCount(); pe++)
518 if (tp.getEntry(pe).getInputCount() > 0)
520 if (tp.getEntry(pe).getInputCount() > 1)
523 .warn("Ignoring additional input spec in provenance entry "
524 + tp.getEntry(pe).toString());
526 // LATER: deal sensibly with multiple inputs
527 Input vInput = tp.getEntry(pe).getInput(0);
528 // is this the whole alignment or a specific set of sequences ?
529 if (vInput.getObjRefCount() == 0)
531 if (tree.getV_parent() != null
532 && tree.getV_parent() instanceof uk.ac.vamsas.objects.core.Alignment)
534 javport = getViewport(tree.getV_parent());
535 jal = javport.getAlignment();
536 view = javport.getAlignment().getCompactAlignment();
541 // Explicit reference - to alignment, sequences or what.
542 if (vInput.getObjRefCount() == 1
543 && vInput.getObjRef(0) instanceof uk.ac.vamsas.objects.core.Alignment)
545 // recover an AlignmentView for the input data
546 javport = getViewport((Vobject) vInput.getObjRef(0));
547 jal = javport.getAlignment();
548 view = javport.getAlignment().getCompactAlignment();
550 else if (vInput.getObjRef(0) instanceof uk.ac.vamsas.objects.core.AlignmentSequence)
552 // recover an AlignmentView for the input data
553 javport = getViewport(((Vobject) vInput.getObjRef(0))
555 jal = javport.getAlignment();
556 jalview.datamodel.SequenceI[] seqs = new jalview.datamodel.SequenceI[vInput
558 for (int i = 0, iSize = vInput.getObjRefCount(); i < iSize; i++)
560 SequenceI seq = (SequenceI) getvObj2jv((Vobject) vInput
564 view = new jalview.datamodel.Alignment(seqs)
565 .getCompactAlignment();
569 int from = 1, to = jal.getWidth();
570 int offset = 0; // deleteRange modifies its frame of reference
571 for (int r = 0, s = vInput.getSegCount(); r < s; r++)
573 Seg visSeg = vInput.getSeg(r);
574 int se[] = getSegRange(visSeg, true); // jalview doesn't do
575 // bidirection alignments yet.
578 Cache.log.warn("Ignoring invalid segment in InputData spec.");
584 view.deleteRange(offset + from - 1, offset + se[0] - 2);
585 offset -= se[0] - from;
592 view.deleteRange(offset + from - 1, offset + to - 1); // final
599 { new AlignmentView(view), jal };
603 .debug("Returning null for input data recovery from provenance.");
607 private AlignmentViewport getViewport(Vobject v_parent)
609 if (v_parent instanceof uk.ac.vamsas.objects.core.Alignment)
612 .findViewport((uk.ac.vamsas.objects.core.Alignment) v_parent);
617 public NewickFile getNewickTree()
622 public String getTitle()
627 public AlignmentView getInputData()
632 public boolean isValidTree()
641 if (ntree.getTree() != null)
646 } catch (Exception e)
648 Cache.log.debug("Failed to parse newick tree string", e);