1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/**
* Title: ProAlign<p>
* Description: <p>
* Copyright: Copyright (c) Ari Loytynoja<p>
* License: GNU GENERAL PUBLIC LICENSE<p>
* @see http://www.gnu.org/copyleft/gpl.html
* Company: ULB<p>
* @author Ari Loytynoja
* @version 1.0
*/
package proalign;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.ArrayList;
class TreeReader {
float[] distance;
boolean isUnRooted = false;
TreeReader(){
}
// Read a tree from a file and remove branch lengths
//
String readFile(String filename) {
String tree = new String();
ProAlign.log("TreeReader: "+filename);
try {
InFile in = new InFile(filename);
String row = new String();
while((row = in.readLine()) != null) {
tree += row;
}
} catch (Exception e) {}
return tree;
}
// Take a tree and divide it into two subtrees.
// Notice that clustalw root is trifurcating
//
String[] divideTree(String tree) {
// System.out.println("tree "+tree+"\n");
String[] trees = new String[2];
distance = new float[2];
trees[0] = "";
if(tree.endsWith(";")) {
tree = tree.substring(0,tree.length()-1); // remove last ';'
}
tree = tree.substring(1,tree.length()-1); // remove first & last '('
if(tree.charAt(0)!='(') { // only one taxon
String tmp = tree.substring(0,tree.indexOf(","));
trees[0] = tmp.substring(0,tmp.indexOf(":"));
distance[0] = new Float(tmp.substring(tmp.indexOf(":")+1)).floatValue();
boolean trifurc = false;
int open = 0;
for(int j = tree.indexOf(",")+1; j<tree.length(); j++) {
if(tree.charAt(j)=='(') { open++; }
else if(tree.charAt(j)==')') { open--; }
if(open==0 && tree.substring(j).indexOf(",")>0) {
trifurc = true;
}
}
// correction for trifurcating root
if(trifurc) {
isUnRooted = true;
trees[1] = "("+tree.substring(tree.indexOf(",")+1)+")";
distance[0] = distance[0]/2f;
distance[1] = distance[0];
} else {
trees[1] = tree.substring(tree.indexOf(",")+1,tree.lastIndexOf(":"));
tmp = tree.substring(tree.lastIndexOf(":")+1);
distance[1] = new Float(tmp).floatValue();
}
// System.out.println("1o: "+trees[0]+" "+distance[0]+"\n");
// System.out.println("2o: "+trees[1]+" "+distance[1]+"\n");
} else {
int open = 0;
for(int i=0; i<tree.length(); i++) {
// count parentheses that are "open"
if(tree.charAt(i)=='(') { open++; }
else if(tree.charAt(i)==')') { open--; }
trees[0] += ""+tree.charAt(i);
if(open<=0) {
String tmp = tree.substring(i+2,tree.indexOf(",",i+2));
distance[0] = new Float(tmp).floatValue();
boolean trifurc = false;
open = 0;
for(int j = tree.indexOf(",",i+2)+1; j<tree.length(); j++) {
if(tree.charAt(j)=='(') { open++; }
else if(tree.charAt(j)==')') { open--; }
if(open==0 && tree.substring(j).indexOf(",")>0) {
trifurc = true;
}
}
// correction for trifurcating root
if(trifurc) {
isUnRooted = true;
trees[1] = "("+tree.substring(tree.indexOf(",",i+2)+1)+")";
distance[0] = distance[0]/2f;
distance[1] = distance[0];
// System.out.println("1: "+trees[0]+" "+distance[0]+"\n");
// System.out.println("2a: "+trees[1]+" "+distance[1]+"\n");
} else {
tmp = tree.substring(tree.indexOf(",",i+2)+1);
trees[1] = tmp.substring(0,tmp.lastIndexOf(":"));
tmp = tmp.substring(tmp.lastIndexOf(":")+1);
distance[1] = new Float(tmp).floatValue();
// System.out.println("1: "+trees[0]+" "+distance[0]+"\n");
// System.out.println("2b: "+trees[1]+" "+distance[1]+"\n");
}
break;
}
}
}
//System.out.println("0: "+trees[0]+" ["+distance[0]+"]");
//System.out.println("1: "+trees[1]+" ["+distance[1]+"]");
return trees;
}
String[] getAllNodes(String filepath) {
ArrayList nl = new ArrayList();
TreeReader tr = this;
String tree = this.readFile(filepath);
// if(ProAlign.DEBUG) {
// ProAlign.log.println(" reading a list of all the nodes.");
// }
//System.out.println(clustalw);
String[] trees = tr.divideTree(tree);
tr.loopThroughNodes(trees[0],tr,nl);
tr.loopThroughNodes(trees[1],tr,nl);
String[] nodes = new String[nl.size()];
for(int i=0; i<nl.size(); i++) {
nodes[i] = (String) nl.get(i);
}
return nodes;
}
void loopThroughNodes(String tree, TreeReader tr, ArrayList nl) {
if(tree.indexOf(",")>0) {
String[] trees = tr.divideTree(tree);
tr.loopThroughNodes(trees[0],tr,nl);
tr.loopThroughNodes(trees[1],tr,nl);
} else {
nl.add(tree);
//System.out.println(nl.size()+" "+tree);
}
}
// ---DEBUGGING ONLY -->
void loopTreeTest(String tree, TreeReader tr) {
if(tree.indexOf(",")>0) {
String[] trees = tr.divideTree(tree);
tr.loopTreeTest(trees[0],tr);
tr.loopTreeTest(trees[1],tr);
} else {
//System.out.println("l "+tree);
}
}
public static void main(String[] args) {
TreeReader tr = new TreeReader();
String tree = tr.readFile(args[0]);
String[] trees = tr.divideTree(tree);
tr.loopTreeTest(trees[0],tr);
tr.loopTreeTest(trees[1],tr);
System.out.println("OK");
String[] nodes = tr.getAllNodes(args[0]);
for(int i=0; i<nodes.length; i++) {
System.out.println(nodes[i]);
}
}
// <--DEBUGGING ONLY ---
}
|