| 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.evolution.alignment; |
| 27 |
|
| 28 |
import dr.evolution.datatype.DataType; |
| 29 |
import dr.evolution.sequence.Sequence; |
| 30 |
import dr.evolution.util.Taxon; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class Consensus { |
| 38 |
|
| 39 |
int[] counts; |
| 40 |
int[] consensus; |
| 41 |
int total; |
| 42 |
String name; |
| 43 |
DataType dataType = null; |
| 44 |
|
| 45 |
public Consensus(String name, Alignment alignment, boolean ignoreGaps) { |
| 46 |
|
| 47 |
this.name = name; |
| 48 |
dataType = alignment.getDataType(); |
| 49 |
|
| 50 |
int[][] frequencies = new int[alignment.getSiteCount()][dataType.getAmbiguousStateCount()]; |
| 51 |
for (int i = 0; i < alignment.getSequenceCount(); i++) { |
| 52 |
for (int j = 0; j < alignment.getSiteCount(); j++) { |
| 53 |
int state = alignment.getState(i, j); |
| 54 |
if (ignoreGaps) { |
| 55 |
if (state < dataType.getStateCount()) { |
| 56 |
frequencies[j][state] += 1; |
| 57 |
} |
| 58 |
} else { |
| 59 |
frequencies[j][state] += 1; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
counts = new int[alignment.getSiteCount()]; |
| 65 |
total = alignment.getSequenceCount(); |
| 66 |
consensus = new int[alignment.getSiteCount()]; |
| 67 |
for (int i = 0; i < alignment.getSiteCount(); i++) { |
| 68 |
|
| 69 |
int maxState = 0; |
| 70 |
int maxFreq = frequencies[i][0]; |
| 71 |
for (int j = 1; j < frequencies[i].length; j++) { |
| 72 |
int freq = frequencies[i][j]; |
| 73 |
if (freq > maxFreq) { |
| 74 |
maxState = j; |
| 75 |
maxFreq = freq; |
| 76 |
} |
| 77 |
} |
| 78 |
consensus[i] = maxState; |
| 79 |
counts[i] = maxFreq; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public double getReliability(int site) { |
| 89 |
return (double)counts[site]/(double)total; |
| 90 |
} |
| 91 |
|
| 92 |
public int getState(int site) { |
| 93 |
return consensus[site]; |
| 94 |
} |
| 95 |
|
| 96 |
public final Sequence getConsensusSequence() { |
| 97 |
StringBuffer buffer = new StringBuffer(); |
| 98 |
for (int i = 0; i < consensus.length; i++) { |
| 99 |
buffer.append(dataType.getChar(getState(i))); |
| 100 |
} |
| 101 |
Sequence sequence = new Sequence(new Taxon(name),buffer.toString()); |
| 102 |
sequence.setDataType(dataType); |
| 103 |
return sequence; |
| 104 |
} |
| 105 |
} |