2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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.io.IOException;
21 import java.util.Enumeration;
22 import java.util.Hashtable;
23 import java.util.List;
24 import java.util.Vector;
26 import jalview.analysis.NJTree;
27 import jalview.bin.Cache;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.AlignmentView;
30 import jalview.datamodel.BinaryNode;
31 import jalview.datamodel.SeqCigar;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.datamodel.SequenceNode;
35 import jalview.gui.AlignViewport;
36 import jalview.gui.TreePanel;
37 import jalview.io.NewickFile;
38 import jalview.io.VamsasAppDatastore;
39 import uk.ac.vamsas.client.Vobject;
40 import uk.ac.vamsas.objects.core.AlignmentSequence;
41 import uk.ac.vamsas.objects.core.Entry;
42 import uk.ac.vamsas.objects.core.Input;
43 import uk.ac.vamsas.objects.core.Newick;
44 import uk.ac.vamsas.objects.core.Param;
45 import uk.ac.vamsas.objects.core.Provenance;
46 import uk.ac.vamsas.objects.core.Seg;
47 import uk.ac.vamsas.objects.core.Treenode;
48 import uk.ac.vamsas.objects.core.Vref;
50 public class Tree extends DatastoreItem
56 uk.ac.vamsas.objects.core.Tree tree;
58 uk.ac.vamsas.objects.core.Alignment alignment; // may be null => dataset or
61 private NewickFile ntree;
65 private AlignmentView inputData = null;
67 public static void updateFrom(VamsasAppDatastore datastore,
68 jalview.gui.AlignFrame alignFrame,
69 uk.ac.vamsas.objects.core.Tree vtree)
71 Tree toTree = new Tree(datastore, alignFrame, vtree);
74 public Tree(VamsasAppDatastore datastore,
75 jalview.gui.AlignFrame alignFrame,
76 uk.ac.vamsas.objects.core.Tree vtree)
78 super(datastore, vtree, TreePanel.class);
82 private NewickFile getNtree() throws IOException
84 return new jalview.io.NewickFile(tree.getNewick(0).getContent());
87 public Tree(VamsasAppDatastore datastore, TreePanel tp2, AlignmentI jal2,
88 uk.ac.vamsas.objects.core.Alignment alignment2)
90 super(datastore, tp2, uk.ac.vamsas.objects.core.Tree.class);
93 tp = (TreePanel) jvobj;
94 alignment = alignment2;
96 tree = (uk.ac.vamsas.objects.core.Tree) vobj;
103 * @see jalview.io.vamsas.DatastoreItem#addFromDocument()
106 public void addFromDocument()
108 tree = (uk.ac.vamsas.objects.core.Tree) vobj; // vtree;
109 TreePanel tp = (TreePanel) jvobj; // getvObj2jv(tree);
111 Object[] idata = recoverInputData(tree.getProvenance());
114 if (idata != null && idata[0] != null)
116 inputData = (AlignmentView) idata[0];
119 title = tree.getNewick(0).getTitle();
120 if (title == null || title.length() == 0)
122 title = tree.getTitle(); // hack!!!!
124 } catch (Exception e)
126 Cache.log.warn("Problems parsing treefile '"
127 + tree.getNewick(0).getContent() + "'", e);
134 * @see jalview.io.vamsas.DatastoreItem#conflict()
137 public void conflict()
140 .info("Update (with conflict) from vamsas document to alignment associated tree not implemented yet.");
146 * @see jalview.io.vamsas.DatastoreItem#update()
149 public void updateToDoc()
151 if (isModifiable(tree.getModifiable()))
153 // synchronize(); // update();
154 // verify any changes.
155 log.info("TODO: Update tree in document from jalview.");
160 log.info("TODO: Add the locally modified tree in Jalview as a new tree in document, leaving locked tree unchanged.");
167 * @see jalview.io.vamsas.DatastoreItem#updateFromDoc()
170 public void updateFromDoc()
172 // should probably just open a new tree panel in the same place as the old
174 // TODO: Tree.updateFromDoc
176 * TreePanel tp = (TreePanel) jvobj; // getvObj2jv(tree);
178 * // make a new tree Object[] idata =
179 * recoverInputData(tree.getProvenance()); try { if (idata != null &&
180 * idata[0] != null) { inputData = (AlignmentView) idata[0]; } ntree =
181 * getNtree(); title = tree.getNewick(0).getTitle(); if (title == null ||
182 * title.length() == 0) { title = tree.getTitle(); // hack!!!! } } catch
183 * (Exception e) { Cache.log.warn("Problems parsing treefile '" +
184 * tree.getNewick(0).getContent() + "'", e); }
186 log.debug("Update the local tree in jalview from the document.");
188 if (isModifiable(tree.getModifiable()))
190 // synchronize(); // update();
191 // verify any changes.
192 log.debug("Update tree in document from jalview.");
197 log.debug("Add modified jalview tree as new tree in document.");
202 * correctly creates provenance for trees calculated on an alignment by
209 private Provenance makeTreeProvenance(AlignmentI jal, TreePanel tp)
211 Cache.log.debug("Making Tree provenance for " + tp.getTitle());
212 Provenance prov = new Provenance();
213 prov.addEntry(new Entry());
214 prov.getEntry(0).setAction("imported " + tp.getTitle());
215 prov.getEntry(0).setUser(provEntry.getUser());
216 prov.getEntry(0).setApp(provEntry.getApp());
217 prov.getEntry(0).setDate(provEntry.getDate());
218 if (tp.getTree().hasOriginalSequenceData())
220 Input vInput = new Input();
221 // LATER: check to see if tree input data is contained in this alignment -
222 // or just correctly resolve the tree's seqData to the correct alignment
225 Vector alsqrefs = getjv2vObjs(findAlignmentSequences(jal,
226 tp.getTree().seqData.getSequences()));
227 Object[] alsqs = new Object[alsqrefs.size()];
228 alsqrefs.copyInto(alsqs);
229 vInput.setObjRef(alsqs);
230 // now create main provenance data
231 prov.getEntry(0).setAction("created " + tp.getTitle());
232 prov.getEntry(0).addInput(vInput);
233 // jalview's special input parameter for distance matrix calculations
234 vInput.setName("jalview:seqdist"); // TODO: settle on appropriate name.
235 prov.getEntry(0).addParam(new Param());
236 prov.getEntry(0).getParam(0).setName("treeType");
237 prov.getEntry(0).getParam(0).setType("utf8");
238 prov.getEntry(0).getParam(0).setContent("NJ"); // TODO: type of tree is a
240 int ranges[] = tp.getTree().seqData.getVisibleContigs();
241 // VisibleContigs are with respect to alignment coordinates. Still need
243 int start = tp.getTree().seqData.getAlignmentOrigin();
244 for (int r = 0; r < ranges.length; r += 2)
246 Seg visSeg = new Seg();
247 visSeg.setStart(1 + start + ranges[r]);
248 visSeg.setEnd(start + ranges[r + 1]);
249 visSeg.setInclusive(true);
250 vInput.addSeg(visSeg);
253 Cache.log.debug("Finished Tree provenance for " + tp.getTitle());
258 * look up SeqCigars in an existing alignment.
262 * @return vector of alignment sequences in order of SeqCigar array (but
263 * missing unfound seqcigars)
265 private Vector findAlignmentSequences(AlignmentI jal, SeqCigar[] sequences)
267 SeqCigar[] tseqs = new SeqCigar[sequences.length];
268 System.arraycopy(sequences, 0, tseqs, 0, sequences.length);
269 Vector alsq = new Vector();
270 List<SequenceI> jalsqs;
271 synchronized (jalsqs = jal.getSequences())
273 for (SequenceI asq : jalsqs)
275 for (int t = 0; t < sequences.length; t++)
278 && (tseqs[t].getRefSeq() == asq || tseqs[t].getRefSeq() == asq
279 .getDatasetSequence()))
280 // && tseqs[t].getStart()>=asq.getStart() &&
281 // tseqs[t].getEnd()<=asq.getEnd())
289 if (alsq.size() < sequences.length)
291 .warn("Not recovered all alignment sequences for given set of input sequence CIGARS");
297 * Update jalview newick representation with TreeNode map
300 * the treepanel that this tree is bound to.
302 public void UpdateSequenceTreeMap(TreePanel tp)
304 if (tp == null || tree == null)
306 Vector leaves = new Vector();
307 if (tp.getTree() == null)
309 Cache.log.warn("Not updating SequenceTreeMap for "
310 + tree.getVorbaId());
313 tp.getTree().findLeaves(tp.getTree().getTopNode(), leaves);
314 Treenode[] tn = tree.getTreenode(); // todo: select nodes for this
321 Treenode node = tn[i++];
322 BinaryNode mappednode = findNodeSpec(node.getNodespec(), leaves);
323 if (mappednode != null && mappednode instanceof SequenceNode)
325 SequenceNode leaf = (SequenceNode) mappednode;
326 // check if we can make the specified association
328 int vrf = 0, refv = 0;
329 while (jvseq == null && vrf < node.getVrefCount())
331 if (refv < node.getVref(vrf).getRefsCount())
333 Object noderef = node.getVref(vrf).getRefs(refv++);
334 if (noderef instanceof AlignmentSequence)
336 // we only make these kind of associations
337 jvseq = getvObj2jv((Vobject) noderef);
346 if (jvseq instanceof SequenceI)
348 leaf.setElement(jvseq);
349 leaf.setPlaceholder(false);
353 leaf.setPlaceholder(true);
354 leaf.setElement(new Sequence(leaf.getName(), "THISISAPLACEHLDER"));
360 // / TODO: refactor to vamsas :start
362 * construct treenode mappings for mapped sequences
368 public Treenode[] makeTreeNodes(NJTree ntree, Newick newick)
370 Vector leaves = new Vector();
371 ntree.findLeaves(ntree.getTopNode(), leaves);
372 Vector tnv = new Vector();
373 Enumeration l = leaves.elements();
374 Hashtable nodespecs = new Hashtable();
375 while (l.hasMoreElements())
377 jalview.datamodel.BinaryNode tnode = (jalview.datamodel.BinaryNode) l
379 if (tnode instanceof jalview.datamodel.SequenceNode)
381 if (!((jalview.datamodel.SequenceNode) tnode).isPlaceholder())
383 Object assocseq = ((jalview.datamodel.SequenceNode) tnode)
385 if (assocseq instanceof SequenceI)
387 Vobject vobj = this.getjv2vObj(assocseq);
390 Treenode node = new Treenode();
391 if (newick.isRegisterable())
393 this.cdoc.registerObject(newick);
394 node.addTreeId(newick);
396 node.setNodespec(makeNodeSpec(nodespecs, tnode));
397 node.setName(tnode.getName());
398 Vref vr = new Vref();
401 tnv.addElement(node);
405 System.err.println("WARNING: Unassociated treeNode "
406 + tnode.element().toString()
408 + ((tnode.getName() != null) ? " label "
409 + tnode.getName() : ""));
417 Treenode[] tn = new Treenode[tnv.size()];
421 return new Treenode[]
425 private String makeNodeSpec(Hashtable nodespecs,
426 jalview.datamodel.BinaryNode tnode)
428 String nname = new String(tnode.getName());
429 Integer nindx = (Integer) nodespecs.get(nname);
432 nindx = new Integer(1);
434 nname = nindx.toString() + " " + nname;
439 * call to match up Treenode specs to NJTree parsed from document object.
443 * as returned from NJTree.findLeaves( .., ..) ..
446 private jalview.datamodel.BinaryNode findNodeSpec(String nodespec,
450 String nspec = nodespec.substring(nodespec.indexOf(' ') + 1);
451 String oval = nodespec.substring(0, nodespec.indexOf(' '));
454 occurence = new Integer(oval).intValue();
455 } catch (Exception e)
457 System.err.println("Invalid nodespec '" + nodespec + "'");
460 jalview.datamodel.BinaryNode bn = null;
463 Enumeration en = leaves.elements();
464 while (en.hasMoreElements() && nocc < occurence)
466 bn = (jalview.datamodel.BinaryNode) en.nextElement();
467 if (bn instanceof jalview.datamodel.SequenceNode
468 && bn.getName().equals(nspec))
478 // todo: end refactor to vamsas library
480 * add jalview object to vamsas document
484 public void addToDocument()
486 tree = new uk.ac.vamsas.objects.core.Tree();
487 bindjvvobj(tp, tree);
488 tree.setTitle(tp.getTitle());
489 Newick newick = new Newick();
490 newick.setContent(tp.getTree().toString());
491 newick.setTitle(tp.getTitle());
492 tree.addNewick(newick);
493 tree.setProvenance(makeTreeProvenance(jal, tp));
494 tree.setTreenode(makeTreeNodes(tp.getTree(), newick));
496 alignment.addTree(tree);
500 * note: this function assumes that all sequence and alignment objects
501 * referenced in input data has already been associated with jalview objects.
505 * @return Object[] { AlignmentView, AlignmentI - reference alignment for
508 public Object[] recoverInputData(Provenance tp)
510 AlignViewport javport = null;
511 jalview.datamodel.AlignmentI jal = null;
512 jalview.datamodel.CigarArray view = null;
513 for (int pe = 0; pe < tp.getEntryCount(); pe++)
515 if (tp.getEntry(pe).getInputCount() > 0)
517 if (tp.getEntry(pe).getInputCount() > 1)
520 .warn("Ignoring additional input spec in provenance entry "
521 + tp.getEntry(pe).toString());
523 // LATER: deal sensibly with multiple inputs
524 Input vInput = tp.getEntry(pe).getInput(0);
525 // is this the whole alignment or a specific set of sequences ?
526 if (vInput.getObjRefCount() == 0)
528 if (tree.getV_parent() != null
529 && tree.getV_parent() instanceof uk.ac.vamsas.objects.core.Alignment)
531 javport = getViewport(tree.getV_parent());
532 jal = javport.getAlignment();
533 view = javport.getAlignment().getCompactAlignment();
538 // Explicit reference - to alignment, sequences or what.
539 if (vInput.getObjRefCount() == 1
540 && vInput.getObjRef(0) instanceof uk.ac.vamsas.objects.core.Alignment)
542 // recover an AlignmentView for the input data
543 javport = getViewport((Vobject) vInput.getObjRef(0));
544 jal = javport.getAlignment();
545 view = javport.getAlignment().getCompactAlignment();
547 else if (vInput.getObjRef(0) instanceof uk.ac.vamsas.objects.core.AlignmentSequence)
549 // recover an AlignmentView for the input data
550 javport = getViewport(((Vobject) vInput.getObjRef(0))
552 jal = javport.getAlignment();
553 jalview.datamodel.SequenceI[] seqs = new jalview.datamodel.SequenceI[vInput
555 for (int i = 0, iSize = vInput.getObjRefCount(); i < iSize; i++)
557 SequenceI seq = (SequenceI) getvObj2jv((Vobject) vInput
561 view = new jalview.datamodel.Alignment(seqs)
562 .getCompactAlignment();
566 int from = 1, to = jal.getWidth();
567 int offset = 0; // deleteRange modifies its frame of reference
568 for (int r = 0, s = vInput.getSegCount(); r < s; r++)
570 Seg visSeg = vInput.getSeg(r);
571 int se[] = getSegRange(visSeg, true); // jalview doesn't do
572 // bidirection alignments yet.
575 Cache.log.warn("Ignoring invalid segment in InputData spec.");
581 view.deleteRange(offset + from - 1, offset + se[0] - 2);
582 offset -= se[0] - from;
589 view.deleteRange(offset + from - 1, offset + to - 1); // final
596 { new AlignmentView(view), jal };
600 .debug("Returning null for input data recovery from provenance.");
604 private AlignViewport getViewport(Vobject v_parent)
606 if (v_parent instanceof uk.ac.vamsas.objects.core.Alignment)
609 .findViewport((uk.ac.vamsas.objects.core.Alignment) v_parent);
614 public NewickFile getNewickTree()
619 public String getTitle()
624 public AlignmentView getInputData()
629 public boolean isValidTree()
638 if (ntree.getTree() != null)
643 } catch (Exception e)
645 Cache.log.debug("Failed to parse newick tree string", e);