Debian Codegraph @

src/dr/evolution/wrightfisher/SelectionMapping.java

raw symbols SCIP
1 /*
2 * SelectionMapping.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.wrightfisher;
27
28 import dr.evolution.alignment.Alignment;
29 import dr.evolution.alignment.SimpleAlignment;
30 import dr.evolution.datatype.AminoAcids;
31 import dr.evolution.datatype.Nucleotides;
32 import dr.evolution.sequence.Sequence;
33
34 import java.io.FileInputStream;
35 import java.io.FileWriter;
36 import java.io.PrintWriter;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Properties;
40
41 /**
42 * @author Alexei Drummond
43 *
44 * @version $Id: SelectionMapping.java,v 1.2 2005/04/20 21:26:19 rambaut Exp $
45 */
46 public class SelectionMapping {
47
48 public static void main(String[] args) throws java.io.IOException {
49
50 if (args.length != 1) throw new RuntimeException("Expect a file name containing parameters!");
51
52 FileInputStream fi = new FileInputStream(args[0]);
53 Properties props = new Properties();
54 props.load(fi);
55 fi.close();
56
57 int genomeLength = Integer.parseInt(props.getProperty("genomeLength", "1000"));
58 int populationSize = Integer.parseInt(props.getProperty("populationSize", "250"));
59 int replicates = Integer.parseInt(props.getProperty("replicates", "1000"));
60 double mu = Double.parseDouble(props.getProperty("mu", "1e-4"));
61 double alpha = Double.parseDouble(props.getProperty("alpha", "0.06"));
62 double pInv = Double.parseDouble(props.getProperty("pinv", "0.5"));
63 double beta = Double.parseDouble(props.getProperty("beta", "1"));
64 int stateSize = Integer.parseInt(props.getProperty("stateSize", "4"));
65 int n = Integer.parseInt(props.getProperty("sampleSize", "40"));
66 boolean randomFittest = Boolean.valueOf(props.getProperty("randomizeFittest", "false"));
67 boolean outputAlignments = Boolean.valueOf(props.getProperty("outputAlignments", "false"));
68 String alignmentFileName = props.getProperty("alignment.filename", "alignment.fasta");
69 String fileName = props.getProperty("output.filename", "out.txt");
70 int burninFactor = Integer.parseInt(props.getProperty("burninFactor", "10"));
71
72 PrintWriter writer = new PrintWriter(new FileWriter(fileName));
73
74 writer.println("// WMD v1.0");
75 writer.println("// genomeLength: " + genomeLength);
76 writer.println("// populationSize: " + populationSize);
77 writer.println("// alpha: " + alpha);
78 writer.println("// beta: " + beta);
79 writer.println("// replicates: " + replicates);
80 writer.println("// output.filename: " + fileName);
81 writer.println("// mu: " + mu);
82 writer.println("// outputAlignments: " + outputAlignments);
83 writer.println("// randomFittest: " + randomFittest);
84 writer.println("// alignment.filename: " + alignmentFileName);
85
86 int[] totalMutationalDensity = new int[200];
87 int[] totalLineages = new int[200];
88 int[] nMutants = new int[genomeLength*3];
89 double[] mrcaFitness = new double[1];
90
91 final int generations = populationSize;
92
93 ArrayList[] unfoldedSites = new ArrayList[populationSize+1];
94 for (int i = 0; i < populationSize+1; i++) {
95 unfoldedSites[i] = new ArrayList();
96 }
97
98 long start = System.currentTimeMillis();
99
100 int dnaSingles = 0;
101 int aaSingles = 0;
102 int dnaSegs = 0;
103 int aaSegs = 0;
104
105 for (int reps = 0; reps < replicates; reps++) {
106 // FitnessFunction f = null;
107 // if (alpha == 0.0) {
108 // f = new NeutralModel();
109 // }
110 FitnessFunction f = new CodonFitnessFunction(genomeLength, alpha, beta, pInv);
111 Population p = new Population(populationSize, genomeLength*3, new SimpleMutator(mu, stateSize), f, randomFittest);
112
113 Genome master = new SimpleGenome(genomeLength*3, f, randomFittest);
114
115 p = Population.forwardSimulation(p, burninFactor * populationSize);
116
117 int age = -1;
118 while (age == -1) {
119 p = Population.forwardSimulation(p, generations);
120 age = p.getAgeOfMRCA(mrcaFitness);
121 }
122 Population children = Population.forwardSimulation(p, 1);
123 writer.println(reps + "\t"+age + "\t" + p.getMeanParentFitness() + "\t" + mrcaFitness[0] + "\t" + p.getProportionAsFit(mrcaFitness[0]));
124
125 if (reps % 10 == 0) {
126 System.out.print(reps + "\t"+ age + "\t" + p.getMeanParentFitness() + "\t" + mrcaFitness[0] + "\t" + p.getProportionAsFit(mrcaFitness[0]));
127 long millis = System.currentTimeMillis() - start;
128 double seconds = millis / 1000.0;
129 if (reps != 0) {
130 double seconds2go = (replicates - reps) * seconds / reps;
131 System.out.println(" -- "+ (Math.round(seconds2go / 36) / 100.0) + " hours");
132 } else {
133 System.out.println();
134 }
135 }
136
137 //p.unfoldedSiteFrequencies(unfoldedSites);
138
139
140 // ************************************************************************************
141 // get the mutational density per lineage as a function of time
142 // ************************************************************************************
143
144 List<Integer>[] mutations = new ArrayList[200];
145 for (int i = 0; i < mutations.length; i++) {
146 mutations[i] = new ArrayList<Integer>(200);
147 }
148
149 int sampleSize = Math.min(n, populationSize);
150
151 p.getMutationDensity(sampleSize, mutations);
152
153 for (int i = 0; i < mutations.length; i++) {
154 for (int j = 0; j < mutations[i].size(); j++) {
155 totalMutationalDensity[i] += mutations[i].get(j);
156 }
157 totalLineages[i] += mutations[i].size();
158 }
159
160 // ************************************************************************************
161 // output alignments if requested
162 // ************************************************************************************
163
164 SimpleAlignment alignment = new SimpleAlignment();
165 SimpleAlignment aaAlignment = new SimpleAlignment();
166
167 alignment.setDataType(Nucleotides.INSTANCE);
168 aaAlignment.setDataType(AminoAcids.INSTANCE);
169
170 if (outputAlignments) {
171 PrintWriter pw = new PrintWriter(new FileWriter(alignmentFileName));
172
173
174 //pw.println(sampleSize + "\t" + p.getGenomeLength());
175 for (int i = 0; i < sampleSize; i++) {
176 pw.print(">sequence_");
177 if (i < 10) {
178 pw.print("0");
179 }
180 pw.println(""+i);
181 String dnaSequence = p.getGenome(i).getDNASequenceString();
182 String aaSequence = p.getGenome(i).getAminoAcidSequenceString();
183 pw.println(dnaSequence);
184 alignment.addSequence(new Sequence(dnaSequence));
185 aaAlignment.addSequence(new Sequence(aaSequence));
186 }
187 pw.println();
188 pw.close();
189 }
190
191 // ************************************************************************************
192 // calculate hamming distances from master sequence
193 // ************************************************************************************
194 for (int i = 0; i < sampleSize; i++) {
195 nMutants[master.hammingDistance(p.getGenome(i))] += 1;
196 }
197
198 dnaSingles += countSingletons(alignment);
199 aaSingles += countSingletons(aaAlignment);
200 dnaSegs += countSegregating(alignment);
201 aaSegs += countSegregating(aaAlignment);
202 }
203
204
205
206
207 for (int i = 0; i < unfoldedSites.length; i++) {
208 double meanFitness = 0.0;
209 double proportionNeutral = 0.0;
210 double proportionPositive = 0.0;
211 double proportionNegative = 0.0;
212 for (int j = 0; j < unfoldedSites[i].size(); j++) {
213 double relativeFitness = (Double) unfoldedSites[i].get(j);
214 meanFitness += relativeFitness;
215 if (relativeFitness == 1.0) {
216 proportionNeutral += 1.0;
217 } else if (relativeFitness > 1.0) {
218 proportionPositive += 1.0;
219 } else {
220 proportionNegative += 1.0;
221 }
222
223 }
224 meanFitness /= (double)unfoldedSites[i].size();
225 proportionNeutral /= (double)unfoldedSites[i].size();
226 proportionPositive /= (double)unfoldedSites[i].size();
227 proportionNegative /= (double)unfoldedSites[i].size();
228 writer.println(i + "\t" + unfoldedSites[i].size() +
229 "\t" + meanFitness + "\t" + proportionNeutral + "\t" + proportionPositive + "\t" + proportionNegative);
230
231 }
232
233
234 writer.println("--------------------");
235 writer.println("SINGLETON COUNTS");
236 writer.println("--------------------");
237 writer.println("dna singletons = " + dnaSingles);
238 writer.println("aa singletons = " + aaSingles);
239
240 int dnaNons = dnaSegs-dnaSingles;
241 int aaNons = aaSegs-aaSingles;
242
243 System.out.println("dna singletons = " + dnaSingles);
244 System.out.println("aa singletons = " + aaSingles);
245 System.out.println("dna segregating = " + dnaSegs);
246 System.out.println("aa segregating = " + aaSegs);
247 System.out.println("dna non-singles = " + dnaNons);
248 System.out.println("aa non-singles = " + aaNons);
249 System.out.println("ratio = " + ((double)dnaSingles/(double)aaSingles));
250 System.out.println("ratio(non) = " + ((double)dnaNons/(double)aaNons));
251
252 writer.println("--------------------");
253 writer.println("MUTATIONAL DENSITIES");
254 writer.println("--------------------");
255 for (int i = 0; i < 200; i++) {
256 writer.println(totalMutationalDensity[i] + "\t" + totalLineages[i]);
257 }
258
259 // ************************************************************************************
260 // output hamming distances
261 // ************************************************************************************
262 writer.println("--------------------");
263 writer.println("Hamming distance distribution");
264 writer.println("--------------------");
265 for (int i = 0; i < nMutants.length; i++) {
266 writer.println(i + "\t" + nMutants[i]);
267 }
268 writer.close();
269 }
270
271 public static int countSingletons(Alignment a) {
272
273 int count = 0;
274 int[] counts = new int[a.getDataType().getStateCount()];
275 for (int i = 0; i < a.getSiteCount(); i++) {
276 count += (isSingleton(a, i, counts)?1:0);
277 }
278
279 return count;
280 }
281
282 public static int countSegregating(Alignment a) {
283
284 int count = 0;
285 int[] counts = new int[a.getDataType().getStateCount()];
286 for (int i = 0; i < a.getSiteCount(); i++) {
287 count += (isSegregating(a, i, counts)?1:0);
288 }
289
290 return count;
291 }
292
293 public static boolean isSingleton(Alignment a, int j, int[] counts) {
294 for (int i = 0; i < counts.length; i++) {
295 counts[i] = 0;
296 }
297 for (int i = 0; i < a.getSequenceCount(); i++) {
298 int state = a.getState(i, j);
299 if (state >= 0 && state < counts.length) {
300 counts[a.getState(i, j)] += 1;
301 }
302 }
303 for(int count : counts) {
304 if( count == 1 ) return true;
305 }
306 return false;
307 }
308
309 public static boolean isSegregating(Alignment a, int j, int[] counts) {
310 for (int i = 0; i < counts.length; i++) {
311 counts[i] = 0;
312 }
313 int seqCount = a.getSequenceCount();
314 for (int i = 0; i < seqCount; i++) {
315 int state = a.getState(i, j);
316 if (state >= 0 && state < counts.length) {
317 counts[a.getState(i, j)] += 1;
318 }
319 }
320 for(int count : counts) {
321 if( count > 0 && count < seqCount ) return true;
322 }
323 return false;
324 }
325
326 }