Debian Codegraph @

src/dr/evolution/alignment/Consensus.java

raw symbols SCIP
1 /*
2 * Consensus.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.alignment;
27
28 import dr.evolution.datatype.DataType;
29 import dr.evolution.sequence.Sequence;
30 import dr.evolution.util.Taxon;
31
32 /**
33 * @author Alexei Drummond
34 *
35 * @version $Id: Consensus.java,v 1.6 2005/04/20 21:26:18 rambaut Exp $
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 * @param site
85 * @return the probability that a sequences in the original alignment has
86 * the same value as the consensus sequences at the given site.
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 }