Clover icon

Coverage Report

  1. Project Clover database Mon Dec 1 2025 15:35:32 GMT
  2. Package jalview.analysis

File TreeBuilder.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
21% of files have more coverage

Code metrics

16
57
10
1
241
142
21
0.37
5.7
10
2.1

Classes

Class Line # Actions
TreeBuilder 42 57 21
0.7951807479.5%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.analysis;
22   
23    import jalview.api.analysis.ScoreModelI;
24    import jalview.api.analysis.SimilarityParamsI;
25    import jalview.datamodel.AlignmentAnnotation;
26    import jalview.datamodel.AlignmentView;
27    import jalview.datamodel.BinaryNode;
28    import jalview.datamodel.CigarArray;
29    import jalview.datamodel.SeqCigar;
30    import jalview.datamodel.SequenceI;
31    import jalview.datamodel.SequenceNode;
32    import jalview.util.Constants;
33    import jalview.viewmodel.AlignmentViewport;
34   
35   
36    import java.util.ArrayList;
37    import java.util.BitSet;
38    import java.util.HashMap;
39    import java.util.List;
40    import java.util.Vector;
41   
 
42    public abstract class TreeBuilder extends TreeEngine
43    {
44    public static final String AVERAGE_DISTANCE = "AV";
45   
46    public static final String NEIGHBOUR_JOINING = "NJ";
47   
48    protected SequenceI[] sequences;
49   
50    protected List<String> labels;
51   
52    // Map to store node pos : annotation details
53    // (additional details from annotation property)
54    protected HashMap<Integer, String> annotationDetails;
55   
56    public AlignmentView seqData;
57   
58    private AlignmentView seqStrings;
59   
60    /**
61    * Constructor
62    *
63    * @param av
64    * @param sm
65    * @param scoreParameters
66    */
 
67  1 toggle public TreeBuilder(AlignmentViewport av, ScoreModelI sm,
68    SimilarityParamsI scoreParameters)
69    {
70  1 int start, end;
71  1 boolean selview = av.getSelectionGroup() != null
72    && av.getSelectionGroup().getSize() >= 1;
73  1 seqStrings = av.getAlignmentView(selview);
74    // FIXME @RENIA - we should check that there are enough annotation tracks to
75    // build a tree!
76  1 if (!selview)// && !sm.isSecondaryStructure())
77    {
78  0 start = 0;
79  0 end = av.getAlignment().getWidth();
80  0 this.sequences = av.getAlignment().getSequencesArray();
81    }
82    else
83    {
84  1 start = av.getSelectionGroup().getStartRes();
85  1 end = av.getSelectionGroup().getEndRes() + 1;
86  1 this.sequences = av.getSelectionGroup()
87    .getSequencesInOrder(av.getAlignment());
88    }
89   
90  1 init(seqStrings, start, end);
91   
92  1 computeTree(sm, scoreParameters);
93    }
94   
 
95  1 toggle public SequenceI[] getSequences()
96    {
97  1 return sequences;
98    }
99   
100    /**
101    *
102    * @return true if tree has real distances
103    */
 
104  1 toggle public boolean hasDistances()
105    {
106  1 return true;
107    }
108   
109    /**
110    *
111    * @return true if tree has real bootstrap values
112    */
 
113  1 toggle public boolean hasBootstrap()
114    {
115  1 return false;
116    }
117   
 
118  1 toggle public boolean hasRootDistance()
119    {
120  1 return true;
121    }
122   
 
123  1 toggle public List<String> getLabels(){
124  1 return this.labels;
125    }
126   
127    ArrayList<AlignmentAnnotation> ssAnnotationForSeqs;
128   
129    /**
130    * Calculates the tree using the given score model and parameters, and the
131    * configured tree type
132    * <p>
133    * If the score model computes pairwise distance scores, then these are used
134    * directly to derive the tree
135    * <p>
136    * If the score model computes similarity scores, then the range of the scores
137    * is reversed to give a distance measure, and this is used to derive the tree
138    *
139    * @param sm
140    * @param scoreOptions
141    */
 
142  1 toggle protected void computeTree(ScoreModelI sm, SimilarityParamsI scoreOptions)
143    {
144  1 labels = new ArrayList<String>();
145  1 annotationDetails = new HashMap<Integer, String>();
146  1 ssAnnotationForSeqs = new ArrayList<AlignmentAnnotation>();
147  1 sequences = sm.expandSeqData(sequences, seqData, scoreOptions,
148    labels, ssAnnotationForSeqs, annotationDetails);
149  1 noseqs = sequences.length;
150   
151  1 distances = sm.findDistances(seqData, scoreOptions);
152   
153  1 makeLeaves();
154   
155  1 noClus = clusters.size();
156   
157  1 cluster();
158    }
159   
 
160  1 toggle protected void init(AlignmentView seqView, int start, int end)
161    {
162  1 this.node = new Vector<BinaryNode>();
163  1 if (seqView != null)
164    {
165  1 this.seqData = seqView;
166    }
167    else
168    {
169  0 SeqCigar[] seqs = new SeqCigar[sequences.length];
170  0 for (int i = 0; i < sequences.length; i++)
171    {
172  0 seqs[i] = new SeqCigar(sequences[i], start, end);
173    }
174  0 CigarArray sdata = new CigarArray(seqs);
175  0 sdata.addOperation(CigarArray.M, end - start + 1);
176  0 this.seqData = new AlignmentView(sdata, start);
177    }
178   
179    /*
180    * count the non-null sequences
181    */
182  1 noseqs = 0;
183   
184  1 done = new BitSet();
185   
186  1 for (SequenceI seq : sequences)
187    {
188  15 if (seq != null)
189    {
190  15 noseqs++;
191    }
192    }
193    }
194   
195    /**
196    * Start by making a cluster for each individual sequence
197    */
 
198  1 toggle void makeLeaves()
199    {
200  1 clusters = new Vector<BitSet>();
201   
202  19 for (int i = 0; i < noseqs; i++)
203    {
204  18 SequenceNode sn = new SequenceNode();
205   
206  18 sn.setElement(sequences[i]);
207   
208  18 if (labels.size() == noseqs)
209    {
210  18 sn.setLabel(labels.get(i));
211    }
212   
213  18 if (ssAnnotationForSeqs != null && !ssAnnotationForSeqs.isEmpty() && ssAnnotationForSeqs.get(i) != null)
214    {
215   
216  4 sn.setAlignmentAnnotation(ssAnnotationForSeqs.get(i));
217   
218    // Initialise AnnotationDetails if present for the annotation
219  4 String annotationData = annotationDetails.get(i);
220  4 if(annotationData != null && annotationData.length() > 0)
221    {
222  0 sn.setAnnotationDetails(annotationData);
223    }
224   
225    }
226   
227  18 sn.setName(sequences[i].getName());
228   
229  18 node.addElement(sn);
230  18 BitSet bs = new BitSet();
231  18 bs.set(i);
232  18 clusters.addElement(bs);
233    }
234    }
235   
 
236  1 toggle public AlignmentView getOriginalData()
237    {
238  1 return seqStrings;
239    }
240   
241    }