JAL-2805 added static method for logging duplications when using map.put
[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   // SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs);
81   //
82   // List<PhylogenyNode> leaves = aptxTree.getExternalNodes();
83   //
84   // int namesleft = seqs.length;
85   // SequenceI nodeSequence;
86   // String nodeSequenceName;
87   // List<SequenceI> one2many = new ArrayList<>();
88   // int countOne2Many = 0;
89   //
90   // for (PhylogenyNode treeNode : leaves)
91   // {
92   // nodeSequenceName = treeNode.getName();
93   // nodeSequence = null;
94   //
95   // if (namesleft > -1)
96   // {
97   // nodeSequence = algnIds.findIdMatch(nodeSequenceName);
98   // }
99   //
100   // if (nodeSequence != null)
101   // {
102   // org.forester.phylogeny.data.Sequence foresterNodeSeq =
103   // ForesterDataConversions.createForesterSequence(nodeSequence, true);
104   //
105   // treeNode.getNodeData().setSequence(foresterNodeSeq);
106   // if (one2many.contains(nodeSequence))
107   // {
108   // countOne2Many++;
109   // if (jalview.bin.Cache.log.isDebugEnabled())
110   // {
111   // jalview.bin.Cache.log.debug("One 2 many relationship for"
112   // +nodeSequence.getName());
113   // }
114   // }
115   // else
116   // {
117   // one2many.add(nodeSequence);
118   // namesleft--;
119   // }
120   // }
121   // else
122   // {
123   // treeNode.setCollapse(true); // collapse nodes that couldn't be connected
124   // // to a sequence
125   //
126   //
127   // // treeNode.setElement( new Sequence(nodeSequenceName,
128   // "THISISAPLACEHOLDER"));
129   // // treeNode.setPlaceholder(true);
130   // }
131   // }
132   // if (jalview.bin.Cache.log.isDebugEnabled() && countOne2Many > 0)
133   // {
134   // jalview.bin.Cache.log.debug("There were " + countOne2Many
135   // + "alignment sequence ids (out of" + one2many.size()
136   // + " unique ids) linked to two or more leaves.");
137   // }
138   // one2many.clear();
139   //
140   // }
141
142 }
143
144
145
146
147
148