86f27d1247c52208efec061bcb7a6786a891db9a
[jalview.git] / src / jalview / ext / archaeopteryx / LoadedTreeSequenceAssociation.java
1 package jalview.ext.archaeopteryx;
2
3 import jalview.analysis.SequenceIdMatcher;
4 import jalview.datamodel.SequenceI;
5 import jalview.ext.forester.DataConversions;
6 import jalview.ext.treeviewer.ExternalLoadedTreeAssociationI;
7 import jalview.util.MappingUtils;
8
9 import java.util.HashMap;
10 import java.util.Map;
11
12 import org.forester.phylogeny.Phylogeny;
13 import org.forester.phylogeny.PhylogenyNode;
14 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
15
16 public class LoadedTreeSequenceAssociation
17         implements ExternalLoadedTreeAssociationI
18 {
19   SequenceI[] alignSequences;
20
21   Phylogeny tree;
22
23   Map<SequenceI, PhylogenyNode> alignmentWithNodes;
24
25   Map<PhylogenyNode, SequenceI> nodesWithAlignment;
26
27   public LoadedTreeSequenceAssociation(SequenceI[] alignmentSequences,
28           Phylogeny aptxTree)
29   {
30     alignSequences = alignmentSequences;
31     tree = aptxTree;
32     alignmentWithNodes = new HashMap<>(alignSequences.length);
33     nodesWithAlignment = new HashMap<>(alignSequences.length);
34
35   }
36
37
38
39   /**
40    * Tries to match sequences from Jalview with tree nodes in Archaeopteryx and
41    * fills in the tree node with sequence data if a match is found.
42    * 
43    * Partially refactored from the old Jalview TreeModel
44    * associateLeavesToSequences method.
45    *
46    * @param seqs
47    * @param aptxTree
48    */
49   @Override
50   public void associateLeavesToSequences()
51
52   {
53     SequenceIdMatcher algnIds = new SequenceIdMatcher(alignSequences);
54     SequenceI nodeSequence;
55     String nodeSequenceName;
56
57     if (!tree.isEmpty())
58     {
59       for (final PhylogenyNodeIterator iter = tree.iteratorPreorder(); iter
60               .hasNext();)
61       {
62         PhylogenyNode treeNode = iter.next();
63         nodeSequenceName = treeNode.getName();
64
65         nodeSequence = algnIds.findIdMatch(nodeSequenceName);
66         if (nodeSequence != null)
67         {
68           org.forester.phylogeny.data.Sequence foresterNodeSeq = DataConversions
69                   .createForesterSequence(nodeSequence, true);
70           treeNode.getNodeData().setSequence(foresterNodeSeq);
71
72           MappingUtils.putWithDuplicationCheck(alignmentWithNodes,
73                   nodeSequence, treeNode);
74           MappingUtils.putWithDuplicationCheck(nodesWithAlignment, treeNode,
75                   nodeSequence);
76
77
78         }
79
80     }
81
82     }
83   }
84
85
86
87   public Map<SequenceI, PhylogenyNode> getAlignmentWithNodes()
88   {
89     return alignmentWithNodes;
90   }
91
92   public Map<PhylogenyNode, SequenceI> getNodesWithAlignment()
93   {
94     return nodesWithAlignment;
95   }
96
97   // {
98   // SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs);
99   //
100   // List<PhylogenyNode> leaves = aptxTree.getExternalNodes();
101   //
102   // int namesleft = seqs.length;
103   // SequenceI nodeSequence;
104   // String nodeSequenceName;
105   // List<SequenceI> one2many = new ArrayList<>();
106   // int countOne2Many = 0;
107   //
108   // for (PhylogenyNode treeNode : leaves)
109   // {
110   // nodeSequenceName = treeNode.getName();
111   // nodeSequence = null;
112   //
113   // if (namesleft > -1)
114   // {
115   // nodeSequence = algnIds.findIdMatch(nodeSequenceName);
116   // }
117   //
118   // if (nodeSequence != null)
119   // {
120   // org.forester.phylogeny.data.Sequence foresterNodeSeq =
121   // ForesterDataConversions.createForesterSequence(nodeSequence, true);
122   //
123   // treeNode.getNodeData().setSequence(foresterNodeSeq);
124   // if (one2many.contains(nodeSequence))
125   // {
126   // countOne2Many++;
127   // if (jalview.bin.Cache.log.isDebugEnabled())
128   // {
129   // jalview.bin.Cache.log.debug("One 2 many relationship for"
130   // +nodeSequence.getName());
131   // }
132   // }
133   // else
134   // {
135   // one2many.add(nodeSequence);
136   // namesleft--;
137   // }
138   // }
139   // else
140   // {
141   // treeNode.setCollapse(true); // collapse nodes that couldn't be connected
142   // // to a sequence
143   //
144   //
145   // // treeNode.setElement( new Sequence(nodeSequenceName,
146   // "THISISAPLACEHOLDER"));
147   // // treeNode.setPlaceholder(true);
148   // }
149   // }
150   // if (jalview.bin.Cache.log.isDebugEnabled() && countOne2Many > 0)
151   // {
152   // jalview.bin.Cache.log.debug("There were " + countOne2Many
153   // + "alignment sequence ids (out of" + one2many.size()
154   // + " unique ids) linked to two or more leaves.");
155   // }
156   // one2many.clear();
157   //
158   // }
159
160 }
161
162
163
164
165
166