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