Debian Codegraph @

src/dr/app/tools/TaxaOriginTrait.java

raw symbols SCIP
1 /*
2 * TaxaOriginTrait.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * Takes trees from BranchJumpPlotter and a text file listing taxaNames; assumes that the sets of these that have the same
42 * discrete trait form clades. Gives the posterior distribution of the trait that was the origin of the
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 * Arguments:
214 * 0: Trait name
215 * 1: File name for list of taxaNames of interest
216 * 2: File name for trees (output from BranchJumpPlotter)
217 * 3: Output file name root
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 }