Debian Codegraph @

src/dr/evolution/parsimony/TestParsimony.java

raw symbols SCIP
1 /*
2 * TestParsimony.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.evolution.parsimony;
27
28 import dr.evolution.alignment.*;
29 import dr.evolution.io.NexusImporter;
30 import dr.evolution.tree.*;
31 import dr.evolution.datatype.PairedDataType;
32
33 import java.io.FileReader;
34
35 /**
36 * @author rambaut
37 * Date: Jun 21, 2005
38 * Time: 11:41:32 PM
39 */
40 public class TestParsimony {
41
42 public final static String alignmentFileName = "ABLV.G.nuc";
43 public final static String treeFileName = "ABLV.G.tree";
44
45 public static void main(String[] args) throws Exception {
46
47 // read alignments
48 NexusImporter importer = new NexusImporter(new FileReader(alignmentFileName));
49 Alignment alignment = importer.importAlignment();
50 // alignment = new GapStrippedAlignment(alignment);
51
52 // read trees
53 Tree tree = null;
54 importer = new NexusImporter(new FileReader(treeFileName));
55 try {
56 tree = importer.importTree(alignment);
57 //((FlexibleTree)tree).resolveTree();
58
59 } catch (Exception e) {
60 System.err.println("Exception attempting to read tree: " + e.getMessage());
61 System.exit(0);
62 }
63
64 //testParsimony(new FitchParsimony(alignment, false), alignment, tree);
65 System.out.println("single:");
66 testParsimony(new SankoffParsimony(alignment), alignment, tree);
67 System.out.println("paired:");
68 testPairedSites(alignment, tree);
69 }
70
71 static void testPairedSites(SiteList siteList, Tree tree) {
72
73 ParsimonyCriterion singleParsimony = new SankoffParsimony(siteList);
74 double[] singleScores = singleParsimony.getSiteScores(tree);
75
76 PairedDataType pairedDataType = new PairedDataType(siteList.getDataType());
77 for (int i = 0; i < siteList.getSiteCount(); i++) {
78 if (singleScores[i] > 0) {
79 SimpleSiteList pairedSites = new SimpleSiteList(pairedDataType, siteList);
80
81 int[] siteIndices = new int[siteList.getSiteCount()];
82 int u = 0;
83
84 for (int j = i + 1; j < siteList.getSiteCount(); j++) {
85 if (singleScores[j] > 0) {
86
87 int[] pairedPattern = new int[siteList.getTaxonCount()];
88
89 int[] pattern1 = siteList.getSitePattern(i);
90 int[] pattern2 = siteList.getSitePattern(j);
91
92 for (int k = 0; k < pairedPattern.length; k ++) {
93 pairedPattern[k] = pairedDataType.getState(pattern1[k], pattern2[k]);
94 }
95 pairedSites.addPattern(pairedPattern);
96
97 siteIndices[u] = j;
98 u++;
99 }
100 }
101
102 //testParsimony(new FitchParsimony(pairedSites, false), pairedSites, tree);
103 ParsimonyCriterion parsimony = new SankoffParsimony(pairedSites);
104 double[] scores = parsimony.getSiteScores(tree);
105
106 for (int k = 0; k < pairedSites.getSiteCount(); k++) {
107 if (scores[k] > 1 /*&& isEpistatic(parsimony, pairedDataType, k, tree, tree.getRoot())*/) {
108 System.out.println("site {" + Integer.toString(i + 1) + ", " + Integer.toString(siteIndices[k] + 1) + "}: " + scores[k] + " steps");
109 showChanges(parsimony, k, tree, tree.getRoot());
110 }
111 }
112 }
113 }
114
115 }
116
117 static void testParsimony(ParsimonyCriterion parsimony, SiteList siteList, Tree tree) {
118 double[] scores = parsimony.getSiteScores(tree);
119
120 for (int i = 0; i < siteList.getSiteCount(); i++) {
121 if (scores[i] > 1) {
122 System.out.println("site" + i + " (" + scores[i] + " steps):");
123 showChanges(parsimony, i, tree, tree.getRoot());
124 }
125 }
126
127 }
128
129 static void showChanges(ParsimonyCriterion parsimony, int site, Tree tree, NodeRef node) {
130
131 if (node != tree.getRoot()) {
132 int stateB = parsimony.getStates(tree, node)[site];
133 int stateA = parsimony.getStates(tree, tree.getParent(node))[site];
134 if (stateA != stateB) {
135 if (tree.isExternal(node)) {
136 System.out.println("\tnode(" + tree.getParent(node).getNumber() + " -> " + tree.getNodeTaxon(node).getId() + "):\tstate" + "(" + stateA + " -> " + stateB + ")");
137 } else {
138 System.out.println("\tnode(" + tree.getParent(node).getNumber() + " -> " + node.getNumber() + "):\tstate" + "(" + stateA + " -> " + stateB + ")");
139 }
140 }
141 }
142 for (int j = 0; j < tree.getChildCount(node); j++) {
143 showChanges(parsimony, site, tree, tree.getChild(node, j));
144 }
145
146 }
147
148 }