Skip to content

Commit 6a1ebc8

Browse files
committed
Reshape the TreeConstructor class
It contains static methods that communicate with forester for tree construction. Only distance and NJ supported for now.
1 parent bc79c9e commit 6a1ebc8

File tree

1 file changed

+19
-161
lines changed

1 file changed

+19
-161
lines changed

biojava-phylo/src/main/java/org/biojava/nbio/phylo/TreeConstructor.java

Lines changed: 19 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -18,187 +18,45 @@
1818
* http://www.biojava.org/
1919
*
2020
*/
21-
/*
22-
* To change this template, choose Tools | Templates
23-
* and open the template in the editor.
24-
*/
2521
package org.biojava.nbio.phylo;
2622

27-
import org.biojava.nbio.core.sequence.MultipleSequenceAlignment;
28-
import org.biojava.nbio.core.sequence.template.AbstractSequence;
29-
import org.biojava.nbio.core.sequence.template.Compound;
3023
import org.forester.evoinference.distance.NeighborJoining;
3124
import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
32-
import org.forester.evoinference.matrix.distance.DistanceMatrix;
33-
import org.forester.io.writers.PhylogenyWriter;
3425
import org.forester.phylogeny.Phylogeny;
3526
import org.slf4j.Logger;
3627
import org.slf4j.LoggerFactory;
3728

38-
import java.io.FileOutputStream;
39-
import java.io.PrintStream;
40-
import java.text.DecimalFormat;
41-
import java.util.ArrayList;
42-
import java.util.List;
43-
4429
/**
45-
* TreeConstructor uses the forester tree library to build phylogenetic trees.
30+
* The TreeConstructor uses the forester library to build different types of
31+
* phylogenetic trees.
4632
*
4733
* @author Scooter Willis
4834
* @author Aleix Lafita
4935
*
5036
*/
51-
public class TreeConstructor<C extends AbstractSequence<D>, D extends Compound>
52-
extends Thread {
37+
public class TreeConstructor {
5338

5439
private static final Logger logger = LoggerFactory
5540
.getLogger(TreeConstructor.class);
5641

57-
private TreeType treeType;
58-
private ScoreMatrixType scoreType;
59-
private List<NJTreeProgressListener> progessListenerVector = new ArrayList<NJTreeProgressListener>();
60-
private MultipleSequenceAlignment<C, D> multipleSequenceAlignment = new MultipleSequenceAlignment<C, D>();
61-
62-
public TreeConstructor(
63-
MultipleSequenceAlignment<C, D> multipleSequenceAlignment,
64-
TreeType _treeType,
65-
ScoreMatrixType _scoreType,
66-
NJTreeProgressListener _treeProgessListener) {
67-
68-
treeType = _treeType;
69-
scoreType = _scoreType;
70-
addProgessListener(_treeProgessListener);
71-
this.multipleSequenceAlignment = multipleSequenceAlignment;
72-
73-
}
74-
75-
public TreeConstructor(BasicSymmetricalDistanceMatrix _matrix,
76-
TreeType _treeType,
77-
TreeConstructionAlgorithm _treeConstructionAlgorithm,
78-
NJTreeProgressListener _treeProgessListener) {
79-
matrix = _matrix;
80-
copyDistanceMatrix = CheckTreeAccuracy.copyMatrix(matrix);
81-
treeType = _treeType;
82-
scoreType = _treeConstructionAlgorithm;
83-
addProgessListener(_treeProgessListener);
84-
85-
}
86-
87-
public void outputPhylipDistances(String fileName) throws Exception {
88-
DistanceMatrix distances = getDistanceMatrix();
89-
if (distances == null) {
90-
throw new Exception("distance matrix has not been calculated. "
91-
+ "Requires process() method to be called first");
92-
}
93-
FileOutputStream fo = new FileOutputStream(fileName);
94-
PrintStream dos = new PrintStream(fo);
95-
DecimalFormat df = new DecimalFormat();
96-
df.setMaximumFractionDigits(5);
97-
df.setMinimumFractionDigits(5);
98-
for (int row = 0; row < distances.getSize(); row++) {
99-
dos.print(distances.getIdentifier(row));
100-
for (int col = 0; col < distances.getSize(); col++) {
101-
dos.print(" " + df.format(distances.getValue(col, row)));
102-
}
103-
dos.println();
104-
}
105-
dos.close();
106-
fo.close();
107-
}
108-
109-
public DistanceMatrix getDistanceMatrix() {
110-
return copyDistanceMatrix;
111-
}
112-
113-
public void cancel() {
114-
// if (njtree != null) {
115-
// njtree.cancel();
116-
// }
117-
}
118-
119-
boolean verbose = false;
120-
Phylogeny p = null;
121-
BasicSymmetricalDistanceMatrix matrix = null;
122-
DistanceMatrix copyDistanceMatrix = null;
123-
124-
public void process() throws Exception {
125-
126-
if (matrix == null) {
127-
double[][] distances = DistanceCalculator.calculateDistanceMatrix(
128-
this, multipleSequenceAlignment, scoreType);
129-
matrix = new BasicSymmetricalDistanceMatrix(
130-
multipleSequenceAlignment.getSize());
131-
for (int i = 0; i < matrix.getSize(); i++) {
132-
matrix.setIdentifier(i, multipleSequenceAlignment
133-
.getAlignedSequence(i + 1).getAccession().getID());
134-
}
135-
for (int col = 0; col < matrix.getSize(); col++) {
136-
for (int row = 0; row < matrix.getSize(); row++) {
137-
matrix.setValue(col, row, distances[col][row]);
138-
139-
}
140-
}
141-
copyDistanceMatrix = CheckTreeAccuracy.copyMatrix(matrix);
142-
}
143-
144-
final List<Phylogeny> ps = new ArrayList<Phylogeny>();
145-
final NeighborJoining nj = NeighborJoining.createInstance(verbose);
146-
147-
ps.add(nj.execute(matrix));
148-
p = ps.get(0);
149-
150-
}
151-
152-
// public void getTreeAccuracy(){
153-
// CheckTreeAccuracy checkTreeAccuracy = new CheckTreeAccuracy();
154-
// checkTreeAccuracy.process(p,distanceMatrix );
155-
// }
156-
@Override
157-
public void run() {
158-
try {
159-
process();
160-
} catch (Exception e) {
161-
logger.error("Exception: ", e);
162-
}
163-
}
164-
165-
public String getNewickString(boolean simpleNewick,
166-
boolean writeDistanceToParent) throws Exception {
167-
final PhylogenyWriter w = new PhylogenyWriter();
168-
StringBuffer newickString = w.toNewHampshire(p, simpleNewick,
169-
writeDistanceToParent);
170-
return newickString.toString();
171-
}
172-
173-
public void addProgessListener(NJTreeProgressListener treeProgessListener) {
174-
if (treeProgessListener != null) {
175-
progessListenerVector.add(treeProgessListener);
176-
}
177-
}
42+
/** Prevent instantiation */
43+
private TreeConstructor() {}
17844

179-
public void removeProgessListener(NJTreeProgressListener treeProgessListener) {
180-
if (treeProgessListener != null) {
181-
progessListenerVector.remove(treeProgessListener);
182-
}
183-
}
45+
public static Phylogeny distanceTree(BasicSymmetricalDistanceMatrix distM,
46+
TreeConstructorType constructor) {
18447

185-
public void broadcastComplete() {
186-
for (NJTreeProgressListener treeProgressListener : progessListenerVector) {
187-
treeProgressListener.complete(this);
48+
Phylogeny p = null;
49+
switch (constructor) {
50+
case NJ:
51+
NeighborJoining nj = NeighborJoining.createInstance();
52+
p = nj.execute(distM);
53+
p.setType(TreeType.DISTANCE.name);
54+
break;
55+
default:
56+
logger.warn("Only NJ Tree Constructor Supported!");
57+
break;
18858
}
59+
logger.info("Tree Completed");
60+
return p;
18961
}
190-
191-
public void updateProgress(String state, int percentage) {
192-
for (NJTreeProgressListener treeProgressListener : progessListenerVector) {
193-
treeProgressListener.progress(this, state, percentage);
194-
}
195-
}
196-
197-
public void updateProgress(String state, int currentCount, int totalCount) {
198-
for (NJTreeProgressListener treeProgressListener : progessListenerVector) {
199-
treeProgressListener
200-
.progress(this, state, currentCount, totalCount);
201-
}
202-
}
203-
20462
}

0 commit comments

Comments
 (0)