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