2daa7ce2bd11fcd40dc46979aca9a04dddf600c9
[jalview.git] / src / jalview / ext / archaeopteryx / JalviewToAptxAssociation.java
1 package jalview.ext.archaeopteryx;
2
3 import jalview.analysis.SequenceIdMatcher;
4 import jalview.datamodel.SequenceI;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.forester.phylogeny.Phylogeny;
10 import org.forester.phylogeny.PhylogenyNode;
11
12 public class JalviewToAptxAssociation
13         implements ExternalLoadedTreeAssociationI<Phylogeny>
14 {
15   SequenceI[] alignSequences;
16
17   Phylogeny tree;
18
19   public JalviewToAptxAssociation(SequenceI[] alignmentSequences,
20           Phylogeny aptxTree)
21   {
22     alignSequences = alignmentSequences;
23     tree = aptxTree;
24
25   }
26
27
28
29   /**
30    * Tries to match sequences from Jalview with tree nodes in Archaeopteryx and
31    * fills in the tree node with sequence data if a match is found.
32    * 
33    * Partially refactored from the old Jalview TreeModel
34    * associateLeavesToSequences method.
35    *
36    * @param seqs
37    * @param aptxTree
38    */
39   @Override
40   public void associateLeavesToSequences(SequenceI[] seqs,
41           Phylogeny aptxTree)
42   {
43     SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs);
44
45     List<PhylogenyNode> leaves = aptxTree.getExternalNodes();
46
47     int namesleft = seqs.length;
48     SequenceI nodeSequence;
49     String nodeSequenceName;
50     List<SequenceI> one2many = new ArrayList<>();
51     // int countOne2Many = 0;
52
53     for (PhylogenyNode treeNode : leaves)
54     {
55       nodeSequenceName = treeNode.getName();
56       nodeSequence = null;
57
58       if (namesleft > -1)
59       {
60         nodeSequence = algnIds.findIdMatch(nodeSequenceName);
61       }
62
63       if (nodeSequence != null)
64       {
65
66         //treeNode.setElement(nodeSequence);
67         if (one2many.contains(nodeSequence))
68         {
69           // countOne2Many++;
70           if (jalview.bin.Cache.log.isDebugEnabled())
71           {
72             jalview.bin.Cache.log.debug("One 2 many relationship for"
73              +nodeSequence.getName());
74           }
75         }
76         else
77         {
78           one2many.add(nodeSequence);
79           namesleft--;
80         }
81       }
82       else
83       {
84        // treeNode.setElement( new Sequence(nodeSequenceName, "THISISAPLACEHOLDER"));
85         // treeNode.setPlaceholder(true);
86       }
87     }
88     // if (jalview.bin.Cache.log.isDebugEnabled() && countOne2Many>0) {
89     // jalview.bin.Cache.log.debug("There were "+countOne2Many+" alignment
90     // sequence ids (out of "+one2many.size()+" unique ids) linked to two or
91     // more leaves.");
92     // }
93     // one2many.clear();
94
95   }
96 }
97
98
99
100
101
102