| 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 |
package dr.app.tools; |
| 27 |
|
| 28 |
import dr.evolution.io.Importer; |
| 29 |
import dr.evolution.io.NexusImporter; |
| 30 |
import dr.evolution.tree.FlexibleNode; |
| 31 |
import dr.evolution.tree.FlexibleTree; |
| 32 |
import dr.evolution.tree.NodeRef; |
| 33 |
|
| 34 |
import java.io.*; |
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.Arrays; |
| 37 |
import java.util.HashMap; |
| 38 |
import java.util.HashSet; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class TaxaOriginTrait { |
| 45 |
|
| 46 |
private FlexibleTree[] trees; |
| 47 |
private String[] taxaNames; |
| 48 |
private String traitName; |
| 49 |
private String attributeName; |
| 50 |
private String fileNameRoot; |
| 51 |
|
| 52 |
private TaxaOriginTrait(String[] taxa, FlexibleTree[] trees, |
| 53 |
String attributeName, String fileNameRoot){ |
| 54 |
this.taxaNames = taxa; |
| 55 |
this.trees = trees; |
| 56 |
this.attributeName = attributeName; |
| 57 |
this.fileNameRoot = fileNameRoot; |
| 58 |
|
| 59 |
traitName = getTraitName(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
private FlexibleNode findCommonAncestor(FlexibleTree tree, FlexibleNode[] nodes){ |
| 64 |
HashSet<FlexibleNode> doneNodes = new HashSet<FlexibleNode>(); |
| 65 |
FlexibleNode currentParent = nodes[0]; |
| 66 |
for(FlexibleNode node: nodes){ |
| 67 |
doneNodes.add(node); |
| 68 |
boolean ancestorOfAllDoneNodes = false; |
| 69 |
currentParent = node; |
| 70 |
if(getTipSet(tree, currentParent).containsAll(doneNodes)){ |
| 71 |
ancestorOfAllDoneNodes=true; |
| 72 |
} |
| 73 |
while(!ancestorOfAllDoneNodes){ |
| 74 |
currentParent = (FlexibleNode)tree.getParent(currentParent); |
| 75 |
if(getTipSet(tree, currentParent).containsAll(doneNodes)){ |
| 76 |
ancestorOfAllDoneNodes=true; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
return currentParent; |
| 81 |
} |
| 82 |
|
| 83 |
private String getTraitName(){ |
| 84 |
FlexibleTree tree = trees[0]; |
| 85 |
|
| 86 |
String traitName = null; |
| 87 |
|
| 88 |
for(int i=0; i<tree.getExternalNodeCount(); i++){ |
| 89 |
NodeRef node = tree.getExternalNode(i); |
| 90 |
|
| 91 |
for(String taxaName : taxaNames){ |
| 92 |
if(tree.getNodeTaxon(node).getId().equals(taxaName)){ |
| 93 |
String attributeValue = (String)tree.getNodeAttribute(node, attributeName); |
| 94 |
|
| 95 |
if(traitName!=null && !traitName.equals(attributeValue)){ |
| 96 |
throw new RuntimeException("Not all taxa given have the same trait value"); |
| 97 |
} else { |
| 98 |
traitName = attributeValue; |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
return traitName; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
private boolean branchNode(FlexibleTree tree, FlexibleNode node){ |
| 114 |
return tree.getChildCount(node)==2; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
private FlexibleNode[] getTipsOfInterest(FlexibleTree tree){ |
| 120 |
HashSet<FlexibleNode> tempSet = new HashSet<FlexibleNode>(); |
| 121 |
|
| 122 |
for(String taxonName: taxaNames){ |
| 123 |
for(int i=0; i<tree.getExternalNodeCount(); i++){ |
| 124 |
if(tree.getNodeTaxon(tree.getExternalNode(i)).toString().equals(taxonName)){ |
| 125 |
tempSet.add((FlexibleNode)tree.getExternalNode(i)); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
return tempSet.toArray(new FlexibleNode[tempSet.size()]); |
| 130 |
} |
| 131 |
|
| 132 |
private HashMap<String,String> getIncomingJumpOrigins(FlexibleTree tree){ |
| 133 |
HashMap<String,String> out = new HashMap<String, String>(); |
| 134 |
|
| 135 |
FlexibleNode[] tips = getTipsOfInterest(tree); |
| 136 |
FlexibleNode mrca = findCommonAncestor(tree, tips); |
| 137 |
HashSet<FlexibleNode> taxaSet = new HashSet<FlexibleNode>(Arrays.asList(tips)); |
| 138 |
HashSet<FlexibleNode> tipSet = getTipSet(tree, mrca); |
| 139 |
if(!taxaSet.containsAll(tipSet)){ |
| 140 |
System.out.println("WARNING: mixed traits in a clade"); |
| 141 |
} |
| 142 |
if(!mrca.getAttribute(attributeName).equals(traitName)){ |
| 143 |
out.put(traitName,"Multiple"); |
| 144 |
System.out.println("Multiple origin found."); |
| 145 |
} else { |
| 146 |
boolean sameTrait = true; |
| 147 |
FlexibleNode currentParent = mrca; |
| 148 |
while(sameTrait){ |
| 149 |
currentParent = (FlexibleNode)tree.getParent(currentParent); |
| 150 |
if(currentParent==null){ |
| 151 |
out.put(traitName,"root"); |
| 152 |
break; |
| 153 |
} else { |
| 154 |
String parentTrait = (String)currentParent.getAttribute(attributeName); |
| 155 |
if(!parentTrait.equals(traitName)){ |
| 156 |
sameTrait = false; |
| 157 |
out.put(traitName,parentTrait); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
} |
| 164 |
return out; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
private HashSet<FlexibleNode> getTipSet(FlexibleTree tree, FlexibleNode node){ |
| 169 |
HashSet<FlexibleNode> out = new HashSet<FlexibleNode>(); |
| 170 |
if(tree.isExternal(node)){ |
| 171 |
out.add(node); |
| 172 |
return out; |
| 173 |
} else { |
| 174 |
for(int i=0; i<tree.getChildCount(node); i++){ |
| 175 |
out.addAll(getTipSet(tree, (FlexibleNode)tree.getChild(node, i))); |
| 176 |
} |
| 177 |
} |
| 178 |
return out; |
| 179 |
} |
| 180 |
|
| 181 |
private void tabulateOrigins(){ |
| 182 |
|
| 183 |
HashMap<String,Integer> countsMap = new HashMap<String, Integer>(); |
| 184 |
|
| 185 |
int count = 0; |
| 186 |
for(FlexibleTree currentTree: trees){ |
| 187 |
if(count % 1 == 0){ |
| 188 |
System.out.println("Doing tree "+count); |
| 189 |
} |
| 190 |
HashMap<String,String> results = getIncomingJumpOrigins(currentTree); |
| 191 |
for(String key: results.keySet()){ |
| 192 |
int oldCount = countsMap.containsKey(results.get(key)) ? countsMap.get(results.get(key)) : 0; |
| 193 |
countsMap.put(results.get(key), oldCount+1); |
| 194 |
} |
| 195 |
count++; |
| 196 |
} |
| 197 |
|
| 198 |
try{ |
| 199 |
|
| 200 |
BufferedWriter outWriter = new BufferedWriter(new FileWriter(fileNameRoot+".csv")); |
| 201 |
for(String key: countsMap.keySet()){ |
| 202 |
outWriter.write(key+","+countsMap.get(key)+"\n"); |
| 203 |
} |
| 204 |
outWriter.flush(); |
| 205 |
|
| 206 |
} catch(IOException e){ |
| 207 |
System.out.println("Failed to write to file"); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public static void main(String[] args){ |
| 221 |
try{ |
| 222 |
BufferedReader taxonReader = new BufferedReader(new FileReader(args[1])); |
| 223 |
HashSet<String> tempTaxa = new HashSet<String>(); |
| 224 |
String line; |
| 225 |
while((line = taxonReader.readLine())!=null){ |
| 226 |
tempTaxa.add(line); |
| 227 |
} |
| 228 |
String[] taxa = tempTaxa.toArray(new String[tempTaxa.size()]); |
| 229 |
|
| 230 |
NexusImporter importer = new NexusImporter(new FileReader(args[2])); |
| 231 |
importer.setSuppressWarnings(true); |
| 232 |
ArrayList<FlexibleTree> tempTrees = new ArrayList<FlexibleTree>(); |
| 233 |
int count = 0; |
| 234 |
while(importer.hasTree()){ |
| 235 |
if(count % 100 == 0){ |
| 236 |
System.out.println("Loaded "+count+" trees"); |
| 237 |
} |
| 238 |
tempTrees.add((FlexibleTree)importer.importNextTree()); |
| 239 |
count++; |
| 240 |
} |
| 241 |
FlexibleTree[] trees = tempTrees.toArray(new FlexibleTree[tempTrees.size()]); |
| 242 |
TaxaOriginTrait examiner = new TaxaOriginTrait(taxa,trees,args[0], |
| 243 |
args[3]); |
| 244 |
examiner.tabulateOrigins(); |
| 245 |
} catch(IOException e){ |
| 246 |
System.out.println("Failed to read files"); |
| 247 |
} catch(Importer.ImportException e){ |
| 248 |
System.out.println("Failed to import trees"); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
} |