Debian Codegraph @

src/dr/evolution/alignment/AscertainedSitePatterns.java

raw symbols SCIP
1 /*
2 * AscertainedSitePatterns.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.util.TaxonList;
29 import dr.util.Citable;
30 import dr.util.Citation;
31 import dr.util.CommonCitations;
32
33 import java.util.*;
34
35 /**
36 * Package: AscertainedSitePatterns
37 * Description:
38 * <p/>
39 * <p/>
40 * Created by
41 * Alexander V. Alekseyenko ([email protected])
42 * Date: Mar 10, 2008
43 * Time: 12:50:36 PM
44 */
45 public class AscertainedSitePatterns extends SitePatterns implements Citable {
46
47 protected int[] includePatterns;
48 protected int[] excludePatterns;
49 protected int ascertainmentIncludeCount;
50 protected int ascertainmentExcludeCount;
51
52 /**
53 * Constructor
54 */
55 public AscertainedSitePatterns(Alignment alignment) {
56 super(alignment);
57 }
58
59 /**
60 * Constructor
61 */
62 public AscertainedSitePatterns(Alignment alignment, TaxonList taxa) {
63 super(alignment, taxa);
64 }
65
66 /**
67 * Constructor
68 */
69 public AscertainedSitePatterns(Alignment alignment, int from, int to, int every) {
70 super(alignment, from, to, every);
71 }
72
73 /**
74 * Constructor
75 */
76 public AscertainedSitePatterns(Alignment alignment, TaxonList taxa, int from, int to, int every) {
77 super(alignment, taxa, from, to, every);
78 }
79
80 /**
81 * Constructor
82 */
83 public AscertainedSitePatterns(SiteList siteList) {
84 super(siteList);
85 }
86
87 /**
88 * Constructor
89 */
90 public AscertainedSitePatterns(SiteList siteList, int from, int to, int every) {
91 super(siteList, from, to, every);
92 }
93
94 public AscertainedSitePatterns(Alignment alignment, TaxonList taxa, int from, int to, int every,
95 int includeFrom, int includeTo,
96 int excludeFrom, int excludeTo) {
97 super(alignment, taxa, from, to, every);
98 int[][] newPatterns = new int[patterns.length + (includeTo - includeFrom) + (excludeTo - excludeFrom)][];
99 double[] newWeights = new double[patterns.length + (includeTo - includeFrom) + (excludeTo - excludeFrom)];
100 for (int i = 0; i < patterns.length; ++i) {
101 newPatterns[i] = patterns[i];
102 newWeights[i] = weights[i];
103 }
104 patterns = newPatterns;
105 weights = newWeights;
106
107 if (includeTo - includeFrom >= 1)
108 includePatterns(includeFrom, includeTo, every);
109 if (excludeTo - excludeFrom >= 1)
110 excludePatterns(excludeFrom, excludeTo, every);
111
112 }
113
114 @Override
115 public Citation.Category getCategory() {
116 return Citation.Category.DATA_MODELS;
117 }
118
119 @Override
120 public String getDescription() {
121 return "Ascertained Site Patterns";
122 }
123
124 public List<Citation> getCitations() {
125 return Collections.singletonList(
126 CommonCitations.ALEKSEYENKO_2008
127 );
128 }
129
130 public int getIncludePatternCount() {
131 return ascertainmentIncludeCount;
132 }
133
134 public int[] getIncludePatternIndices() {
135 return includePatterns;
136 }
137
138 protected void includePatterns(int includeFrom, int includeTo, int every) {
139 if (includePatterns == null) {
140 includePatterns = new int[includeTo - includeFrom];
141 }
142 for (int i = includeFrom; i < includeTo; i += every) {
143 int[] pattern = siteList.getPattern(i);
144 int index = addAscertainmentPattern(pattern);
145 includePatterns[ascertainmentIncludeCount] = index;
146 ascertainmentIncludeCount += 1;
147 }
148 }
149
150 public int getExcludePatternCount() {
151 return ascertainmentExcludeCount;
152 }
153
154 public int[] getExcludePatternIndices() {
155 return excludePatterns;
156 }
157
158 protected void excludePatterns(int excludeFrom, int excludeTo, int every) {
159 if (excludePatterns == null)
160 excludePatterns = new int[excludeTo - excludeFrom];
161
162 for (int i = excludeFrom; i < excludeTo; i += every) {
163 int[] pattern = siteList.getPattern(i);
164 int index = addAscertainmentPattern(pattern);
165 weights[index] = 0.0; // Site is excluded, so set weight = 0
166 excludePatterns[ascertainmentExcludeCount] = index;
167 ascertainmentExcludeCount += 1;
168 }
169
170 }
171
172 public double getAscertainmentCorrection(double[] patternLogProbs) {
173 double excludeProb = 0, includeProb = 0, returnProb = 1.0;
174
175 int[] includeIndices = getIncludePatternIndices();
176 int[] excludeIndices = getExcludePatternIndices();
177 for (int i = 0; i < getIncludePatternCount(); i++) {
178 int index = includeIndices[i];
179 includeProb += Math.exp(patternLogProbs[index]);
180 }
181 for (int j = 0; j < getExcludePatternCount(); j++) {
182 int index = excludeIndices[j];
183 excludeProb += Math.exp(patternLogProbs[index]);
184 }
185 if (includeProb == 0.0) {
186 returnProb -= excludeProb;
187 } else if (excludeProb == 0.0) {
188 returnProb = includeProb;
189 } else {
190 returnProb = includeProb - excludeProb;
191 }
192 return Math.log(returnProb);
193 }
194
195 private int addAscertainmentPattern(int[] pattern) {
196 for (int i = 0; i < patternCount; i++) {
197 if (comparePatterns(patterns[i], pattern)) {
198 return i;
199 }
200 }
201 int index = patternCount;
202 patterns[index] = pattern;
203 weights[index] = 0.0; /* do not affect weight */
204 patternCount++;
205
206 return index;
207 }
208 }