| 1 |
package dr.evomodel.antigenic.phyloclustering.statistics; |
| 2 |
|
| 3 |
import dr.evolution.datatype.Nucleotides; |
| 4 |
|
| 5 |
import java.util.Iterator; |
| 6 |
import java.util.LinkedList; |
| 7 |
|
| 8 |
import dr.evolution.tree.NodeRef; |
| 9 |
import dr.evolution.tree.Tree; |
| 10 |
import dr.evolution.tree.TreeTrait; |
| 11 |
import dr.evolution.tree.TreeTraitProvider; |
| 12 |
import dr.evomodel.antigenic.phyloclustering.TreeClusteringVirusesPrior; |
| 13 |
import dr.evomodel.tree.TreeModel; |
| 14 |
import dr.xml.AbstractXMLObjectParser; |
| 15 |
import dr.xml.ElementRule; |
| 16 |
import dr.xml.XMLObject; |
| 17 |
import dr.xml.XMLObjectParser; |
| 18 |
import dr.xml.XMLParseException; |
| 19 |
import dr.xml.XMLSyntaxRule; |
| 20 |
|
| 21 |
public class MutationsTreeTrait implements TreeTraitProvider { |
| 22 |
|
| 23 |
protected Helper treeTraits = new Helper(); |
| 24 |
private TreeModel treeModel; |
| 25 |
private TreeClusteringVirusesPrior clusterPrior; |
| 26 |
public static final String MUTATIONS_TREETRAIT = "mutationsTreeTrait"; |
| 27 |
|
| 28 |
|
| 29 |
private String[] mutationString; |
| 30 |
|
| 31 |
private LinkedList<Integer>[] mutationList; |
| 32 |
private LinkedList<Integer>[] causalList; |
| 33 |
|
| 34 |
|
| 35 |
public MutationsTreeTrait(TreeModel treeModel_in, TreeClusteringVirusesPrior clusterPrior_in){ |
| 36 |
|
| 37 |
this.treeModel = treeModel_in; |
| 38 |
this.clusterPrior = clusterPrior_in; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
int numNodes = treeModel.getNodeCount(); |
| 45 |
|
| 46 |
String[] sequence = new String[numNodes]; |
| 47 |
|
| 48 |
|
| 49 |
String GENETIC_CODE_TABLES ="KNKNTTTTRSRSIIMIQHQHPPPPRRRRLLLLEDEDAAAAGGGGVVVV*Y*YSSSS*CWCLFLF"; |
| 50 |
|
| 51 |
int numCodons = clusterPrior.getNumSites(); |
| 52 |
|
| 53 |
for(int curIndex = 0; curIndex < numNodes; curIndex ++){ |
| 54 |
String ns = (String) treeModel.getNodeAttribute( treeModel.getNode(curIndex), "states"); |
| 55 |
|
| 56 |
ns = ns.substring(clusterPrior.getStartBase(), clusterPrior.getEndBase() ); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
String codonSequence = ""; |
| 65 |
for(int codon=0; codon< numCodons; codon++){ |
| 66 |
|
| 67 |
int nuc1 = Nucleotides.NUCLEOTIDE_STATES[ns.charAt(codon*3)]; |
| 68 |
int nuc2 = Nucleotides.NUCLEOTIDE_STATES[ns.charAt(codon*3+1)]; |
| 69 |
int nuc3 = Nucleotides.NUCLEOTIDE_STATES[ns.charAt(codon*3+2)]; |
| 70 |
|
| 71 |
int canonicalState = (nuc1 * 16) + (nuc2 * 4) + nuc3; |
| 72 |
|
| 73 |
codonSequence = codonSequence + GENETIC_CODE_TABLES.charAt(canonicalState); |
| 74 |
} |
| 75 |
|
| 76 |
sequence[curIndex] = codonSequence; |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
mutationString = new String[treeModel.getNodeCount()]; |
| 82 |
|
| 83 |
NodeRef cNode = treeModel.getRoot(); |
| 84 |
LinkedList<NodeRef> visitlist = new LinkedList<NodeRef>(); |
| 85 |
|
| 86 |
visitlist.add(cNode); |
| 87 |
|
| 88 |
int countProcessed=0; |
| 89 |
while(visitlist.size() > 0){ |
| 90 |
countProcessed++; |
| 91 |
|
| 92 |
if(treeModel.getParent(cNode) == null){ |
| 93 |
|
| 94 |
|
| 95 |
} |
| 96 |
else{ |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
mutationString[cNode.getNumber()] = "\""; |
| 102 |
String nodeState = sequence[cNode.getNumber()]; |
| 103 |
String parentState = sequence[treeModel.getParent(cNode).getNumber()]; |
| 104 |
|
| 105 |
int count = 0; |
| 106 |
for(int i=0; i < numCodons; i++){ |
| 107 |
if(nodeState.charAt(i) != parentState.charAt(i)){ |
| 108 |
count++; |
| 109 |
if(count>1){ |
| 110 |
|
| 111 |
mutationString[cNode.getNumber()] = mutationString[cNode.getNumber()] + ","; |
| 112 |
} |
| 113 |
|
| 114 |
mutationString[cNode.getNumber()] = mutationString[cNode.getNumber()] + (i+1); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
mutationString[cNode.getNumber()] = mutationString[cNode.getNumber()] + "\""; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
for(int childNum=0; childNum < treeModel.getChildCount(cNode); childNum++){ |
| 128 |
NodeRef node= treeModel.getChild(cNode,childNum); |
| 129 |
visitlist.add(node); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
visitlist.pop(); |
| 134 |
|
| 135 |
if(visitlist.size() > 0){ |
| 136 |
cNode = visitlist.getFirst(); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
treeTraits.addTrait(new TreeTrait.IA() { |
| 143 |
|
| 144 |
|
| 145 |
public String getTraitName() { |
| 146 |
return "mutations"; |
| 147 |
} |
| 148 |
|
| 149 |
public String getTraitString(Tree tree, NodeRef node) { |
| 150 |
if(tree != treeModel){ |
| 151 |
System.out.println("Something is wrong. Why is tree not equal to treeModel?"); |
| 152 |
System.exit(0); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
mutationList = clusterPrior.getMutationsPerNode(); |
| 159 |
causalList = clusterPrior.getCausativeStatesPerNode(); |
| 160 |
|
| 161 |
String nodeString = "\""; |
| 162 |
|
| 163 |
if(mutationList[node.getNumber()] != null){ |
| 164 |
Iterator itr = mutationList[node.getNumber()].iterator(); |
| 165 |
Iterator itr2 = causalList[node.getNumber()].iterator(); |
| 166 |
int count = 0; |
| 167 |
while(itr.hasNext()){ |
| 168 |
count++; |
| 169 |
if(count>1){ |
| 170 |
nodeString += ","; |
| 171 |
} |
| 172 |
int curMutation = ((Integer) itr.next()).intValue(); |
| 173 |
nodeString += curMutation; |
| 174 |
int curCausal = ((Integer) itr2.next()).intValue(); |
| 175 |
if(curCausal ==1){ |
| 176 |
nodeString += "*"; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
nodeString += "\""; |
| 181 |
|
| 182 |
return nodeString; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
public Intent getIntent() { |
| 187 |
|
| 188 |
return Intent.NODE; |
| 189 |
} |
| 190 |
|
| 191 |
public Class getTraitClass() { |
| 192 |
System.out.println("getTraitClass ran. Not expected. Quit now"); |
| 193 |
System.exit(0); |
| 194 |
return int[].class; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
public int[] getTrait(Tree tree, NodeRef node) { |
| 199 |
System.out.println("getTrait ran. Not expected. Quit now"); |
| 200 |
System.exit(0); |
| 201 |
return null; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
}); |
| 206 |
|
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public TreeTrait[] getTreeTraits() { |
| 213 |
return treeTraits.getTreeTraits(); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
public TreeTrait getTreeTrait(String key) { |
| 218 |
System.out.println("not expected to run getTreeTrait. Quit now"); |
| 219 |
System.exit(0); |
| 220 |
return treeTraits.getTreeTrait(key); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 227 |
|
| 228 |
|
| 229 |
public String getParserName() { |
| 230 |
return MUTATIONS_TREETRAIT; |
| 231 |
} |
| 232 |
|
| 233 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 234 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 235 |
TreeClusteringVirusesPrior clusterPrior = (TreeClusteringVirusesPrior) xo.getChild(TreeClusteringVirusesPrior.class); |
| 236 |
return new MutationsTreeTrait( treeModel, clusterPrior); |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public String getParserDescription() { |
| 245 |
return "Display node number in the tree."; |
| 246 |
} |
| 247 |
|
| 248 |
public Class getReturnType() { |
| 249 |
return MutationsTreeTrait.class; |
| 250 |
} |
| 251 |
|
| 252 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 253 |
return rules; |
| 254 |
} |
| 255 |
|
| 256 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 257 |
new ElementRule(TreeModel.class), |
| 258 |
new ElementRule(TreeClusteringVirusesPrior.class), |
| 259 |
}; |
| 260 |
}; |
| 261 |
|
| 262 |
|
| 263 |
} |