Debian Codegraph @

src/dr/oldevomodel/sitemodel/AlignmentScore.java

raw symbols SCIP
1 /*
2 * AlignmentScore.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.oldevomodel.sitemodel;
27
28 import dr.evolution.alignment.Alignment;
29 import dr.evolution.alignment.ExtractPairs;
30 import dr.evolution.alignment.GapUtils;
31 import dr.evolution.alignment.SitePatterns;
32 import dr.evolution.datatype.Nucleotides;
33 import dr.evolution.io.Importer;
34 import dr.evolution.io.NexusImporter;
35 import dr.oldevomodel.substmodel.FrequencyModel;
36 import dr.oldevomodel.substmodel.HKY;
37 import dr.oldevomodel.substmodel.SubstitutionModel;
38 import dr.inference.model.Parameter;
39 import dr.math.DifferentialEvolution;
40 import dr.math.MultivariateFunction;
41 import dr.math.UnivariateFunction;
42 import dr.math.UnivariateMinimum;
43
44 import java.io.FileReader;
45 import java.util.*;
46
47 /**
48 * @author alexei
49 * @version $Id: AlignmentScore.java,v 1.2 2005/04/11 11:29:37 alexei Exp $
50 */
51 public class AlignmentScore implements UnivariateFunction, MultivariateFunction {
52
53 SiteModel siteModel;
54 ScoreMatrix scoreMatrix;
55 SitePatterns sitePatterns;
56
57 public AlignmentScore(ScoreMatrix scoreMatrix, SitePatterns sitePatterns) {
58 this.scoreMatrix = scoreMatrix;
59 this.siteModel = scoreMatrix.siteModel;
60 this.sitePatterns = sitePatterns;
61 }
62
63 /**
64 * compute function value
65 *
66 * @param argument function argument (vector)
67 * @return function value
68 */
69 public double evaluate(double[] argument) {
70
71 double kappa = argument[0];
72 double time = argument[1];
73
74 ((HKY)((GammaSiteModel)siteModel).getSubstitutionModel()).setKappa(kappa);
75
76 scoreMatrix.setTime(time);
77
78 // must return negative logLikelihood because optimizer finds minimum
79 return -scoreMatrix.getScore(sitePatterns);
80 }
81
82 /**
83 * compute function value
84 *
85 * @param time
86 * @return function value
87 */
88 public double evaluate(double time) {
89 scoreMatrix.setTime(time);
90
91 // must return negative logLikelihood because optimizer finds minimum
92 return -scoreMatrix.getScore(sitePatterns);
93 }
94
95 /**
96 * get number of arguments
97 *
98 * @return number of arguments
99 */
100 public int getNumArguments() {
101 return 2;
102 }
103
104 /**
105 * get lower bound of argument
106 *
107 * @return lower bound
108 */
109 public double getLowerBound() {
110 return 0.0; //To change body of implemented methods use File | Settings | File Templates.
111 }
112
113 /**
114 * get upper bound of argument
115 *
116 * @return upper bound
117 */
118 public double getUpperBound() {
119 return 10.0; //To change body of implemented methods use File | Settings | File Templates.
120 }
121
122 /**
123 * get lower bound of argument n
124 *
125 * @param n argument number
126 * @return lower bound
127 */
128 public double getLowerBound(int n) {
129
130 return 0;
131 }
132
133 /**
134 * get upper bound of argument n
135 *
136 * @param n argument number
137 * @return upper bound
138 */
139 public double getUpperBound(int n) {
140 if (n == 0) return 100;
141 if (n == 1) return 100;
142 return 10;
143 }
144
145 public static double[] getAlignmentScore(ScoreMatrix scoreMatrix, SitePatterns sitePatterns) {
146
147 AlignmentScore alignmentScore = new AlignmentScore(scoreMatrix, sitePatterns);
148
149 double[] params = new double[alignmentScore.getNumArguments()];
150 DifferentialEvolution de = new DifferentialEvolution(params.length,params.length*10);
151 for (int i = 0; i < params.length; i++) params[i] = 0.5;
152
153 de.optimize(alignmentScore, params,1e-6, 1e-6);
154 double score = alignmentScore.evaluate(params);
155
156 double[] results = new double[params.length + 1];
157 System.arraycopy(params, 0, results, 0, params.length);
158 results[params.length] = score;
159
160 return results;
161 }
162
163 public static double getGeneticDistance(ScoreMatrix scoreMatrix, SitePatterns sitePatterns) {
164
165 AlignmentScore alignmentScore = new AlignmentScore(scoreMatrix, sitePatterns);
166
167 double[] params = new double[alignmentScore.getNumArguments()];
168 DifferentialEvolution de = new DifferentialEvolution(params.length,params.length*10);
169 for (int i = 0; i < params.length; i++) params[i] = 0.5;
170
171 de.optimize(alignmentScore, params,1e-6, 1e-6);
172 //double score = alignmentScore.evaluate(params);
173
174 return params[params.length-1];
175
176 }
177
178 public static double[] getFastAlignmentScore(ScoreMatrix scoreMatrix, SitePatterns sitePatterns) {
179
180 AlignmentScore alignmentScore = new AlignmentScore(scoreMatrix, sitePatterns);
181
182 UnivariateMinimum de = new UnivariateMinimum();
183
184 double time = de.optimize(alignmentScore, 1e-6);
185 double score = alignmentScore.evaluate(time);
186
187 return new double[] {time, score};
188 }
189
190
191 private static void printFrequencyTable(List<Integer> list) {
192 int max = 0;
193 int total = 0;
194 for (Integer size : list) {
195 if (size > max) max = size;
196 total += size;
197 }
198 int[] freqs = new int[max+1];
199 for (Integer size : list) {
200 freqs[size] += 1;
201 }
202
203 for (int i = 0; i < freqs.length; i++) {
204 System.out.println(i + "\t" + freqs[i]);
205 }
206 System.out.println("Total = " + total);
207 }
208
209 public static void main(String[] args) throws java.io.IOException, Importer.ImportException {
210
211 NexusImporter importer = new NexusImporter(new FileReader(args[0]));
212
213 Alignment alignment = importer.importAlignment();
214
215 ExtractPairs pairs = new ExtractPairs(alignment);
216
217 Parameter muParam = new Parameter.Default(1.0);
218 Parameter kappaParam = new Parameter.Default(1.0);
219 kappaParam.addBounds(new Parameter.DefaultBounds(100.0, 0.0, 1));
220 muParam.addBounds(new Parameter.DefaultBounds(1.0, 1.0, 1));
221
222 Parameter freqParam = new Parameter.Default(alignment.getStateFrequencies());
223
224 FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE,freqParam);
225
226 SubstitutionModel substModel = new HKY(kappaParam, freqModel);
227
228 SiteModel siteModel = new GammaSiteModel(substModel, muParam, null, 1, null);
229
230 ScoreMatrix scoreMatrix = new ScoreMatrix(siteModel, 0.1);
231
232
233 double threshold = 0.1;
234
235 List<PairDistance> pairDistances = new ArrayList<PairDistance>();
236 Set<Integer> sequencesUsed = new HashSet<Integer>();
237 List<Integer> allGaps = new ArrayList<Integer>();
238 for (int i = 0; i < alignment.getSequenceCount(); i++) {
239 for (int j = i+1; j < alignment.getSequenceCount(); j++) {
240 Alignment pairAlignment = pairs.getPairAlignment(i,j);
241 if (pairAlignment != null) {
242 SitePatterns patterns = new SitePatterns(pairAlignment);
243
244 double distance = getGeneticDistance(scoreMatrix, patterns);
245 if (distance < threshold) {
246 List gaps = new ArrayList();
247 GapUtils.getGapSizes(pairAlignment, gaps);
248 pairDistances.add(new PairDistance(i,j,distance, gaps, pairAlignment.getSiteCount()));
249 System.out.print(".");
250 } else {
251 System.out.print("*");
252 }
253 } else {
254 System.out.print("x");
255 }
256 }
257 System.out.println();
258 }
259
260 Collections.sort(pairDistances);
261 int totalLength = 0;
262 for (PairDistance pairDistance : pairDistances) {
263
264 Integer x = pairDistance.x;
265 Integer y = pairDistance.y;
266
267 if (!sequencesUsed.contains(x) && !sequencesUsed.contains(y)) {
268 allGaps.addAll(pairDistance.gaps);
269 sequencesUsed.add(x);
270 sequencesUsed.add(y);
271 System.out.println("Added pair (" + x + "," + y + ") d=" + pairDistance.distance + " L=" + pairDistance.alignmentLength);
272 totalLength += pairDistance.alignmentLength;
273 }
274 }
275
276 printFrequencyTable(allGaps);
277 System.out.println("total length=" + totalLength);
278 }
279 }