Debian Codegraph @

src/dr/evolution/alignment/Defects.java

raw symbols SCIP
1 /*
2 * Defects.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.Codons;
29 import dr.evolution.datatype.Nucleotides;
30 import dr.evolution.io.Importer;
31 import dr.evolution.io.NexusImporter;
32 import dr.evolution.sequence.Sequence;
33 import dr.evolution.util.TaxonList;
34
35 import java.io.EOFException;
36 import java.io.FileReader;
37 import java.util.ArrayList;
38 import java.util.HashSet;
39 import java.util.Set;
40 import java.util.TreeSet;
41
42 /**
43 * @author Alexei Drummond
44 *
45 * @version $Id: Defects.java,v 1.6 2005/05/23 10:44:07 alexei Exp $
46 */
47 public class Defects {
48
49 private final ArrayList<Defect> defects = new ArrayList<Defect>();
50 private final Set<Integer> defectiveSequences = new HashSet<Integer>();
51 private final Set<Integer> defectiveSites = new HashSet<Integer>();
52 int sequenceCount = 0;
53 static final int STOP = -1;
54 static final int INDEL = -2;
55 int totalReads = 0;
56
57 public Defects(Alignment alignment) {
58
59 Codons codons = Codons.UNIVERSAL;
60 Nucleotides nucs = Nucleotides.INSTANCE;
61
62 Alignment codonAlignment = new ConvertAlignment(codons, alignment);
63
64 sequenceCount = codonAlignment.getSequenceCount();
65
66 for (int i = 0; i < codonAlignment.getSequenceCount(); i++) {
67 for (int j = 0; j < codonAlignment.getSiteCount(); j++) {
68 int state = codonAlignment.getState(i, j);
69
70 if (codons.isStopCodon(state)) {
71 //System.out.print("*");
72 // System.out.println("Found stop codon: " + triplet + " in sequence " + codonAlignment.getTaxonId(i) + " at site " + j);
73 addDefect(STOP, i, j);
74 totalReads += 1;
75 } else if (codons.isGapState(state)) {
76 //System.out.print("?");
77 // go back to original alignment and check
78 int start = j*3;
79 int state1 = alignment.getState(i,start);
80 int state2 = alignment.getState(i,start+1);
81 int state3 = alignment.getState(i,start+2);
82 if (nucs.isGapState(state1) && nucs.isGapState(state2) && nucs.isGapState(state3)) {
83 // real gap
84 } else {
85 // defect
86 // System.out.println("Found gap in sequence " + codonAlignment.getTaxonId(i) + " at site " + j);
87 addDefect(INDEL, i, j);
88 //totalReads += 1;
89 }
90
91 } else {
92 //System.out.print(" ");
93 totalReads += 1;
94 }
95 }
96 //System.out.println();
97 }
98 }
99
100 private void addDefect(int code, int sequence, int site) {
101 defects.add(new Defect(code, sequence, site));
102 defectiveSequences.add(sequence);
103 defectiveSites.add(site);
104 }
105
106 public int getDefectiveSequenceCount() {
107 return defectiveSequences.size();
108 }
109
110 public int getDefectiveSiteCount() {
111 return defectiveSites.size();
112 }
113
114 public int getDefectiveSites(int sequence) {
115 int count = 0;
116 for( Defect defect : defects ) {
117 if( defect.getSequence() == sequence ) {
118 count += 1;
119 }
120 }
121 return count;
122 }
123
124 public int getStopSites(int sequence) {
125 int count = 0;
126 for( Defect defect : defects ) {
127 if( defect.getSequence() == sequence && defect.isStop() ) {
128 count += 1;
129 }
130 }
131 return count;
132 }
133
134 public int getDefectiveSequences(int site) {
135 int count = 0;
136 for(Defect defect : defects) {
137 if( defect.getSequence() == site ) {
138 count += 1;
139 }
140 }
141 return count;
142 }
143
144 /**
145 * @return the total number of defects in the alignment
146 */
147 public int getDefectCount() {
148 return defects.size();
149 }
150
151 public int getStopCodonCount() {
152 int count = 0;
153 for( Defect defect : defects ) {
154 if( defect.isStop() ) {
155 count += 1;
156 }
157 }
158 return count;
159 }
160
161 public int getSequenceCount(int defectCount) {
162 int count = 0;
163 for (int i = 0; i < sequenceCount; i++) {
164 if (getDefectiveSites(i) == defectCount) {
165 count += 1;
166 }
167 }
168 return count;
169 }
170
171 /**
172 * @return the maximum number of defects found in any single sequence.
173 */
174 public int getMaxDefectiveSites() {
175
176 int maxDefects = 0;
177 for (int i = 0; i < sequenceCount; i++) {
178 int defects = getDefectiveSites(i);
179 if (defects > maxDefects) {
180 maxDefects = defects;
181 }
182 }
183 return maxDefects;
184 }
185
186 public int getMaxStopSites() {
187
188 int maxStops = 0;
189 for (int i = 0; i < sequenceCount; i++) {
190 int stops = getStopSites(i);
191 if (stops > maxStops) {
192 maxStops = stops;
193 }
194 }
195 return maxStops;
196 }
197
198
199 /**
200 * @param defectCount
201 * @return A set of integers representing the sequences with the given defect count.
202 */
203 public Set<Integer> getSequences(int defectCount) {
204 TreeSet<Integer> set = new TreeSet<Integer>();
205 for (int i = 0; i < sequenceCount; i++) {
206 if (getDefectiveSites(i) == defectCount) {
207 set.add(i);
208 }
209 }
210 return set;
211 }
212
213 /**
214 * @param stopCount
215 * @return A set of integers representing the sequences with the given defect count.
216 */
217 public Set<Integer> getSequencesByStopCount(int stopCount) {
218 TreeSet<Integer> set = new TreeSet<Integer>();
219 for (int i = 0; i < sequenceCount; i++) {
220 if (getStopSites(i) == stopCount) {
221 set.add(i);
222 }
223 }
224 return set;
225 }
226
227
228 public int getStopSequenceCount(int stopCount) {
229 int count = 0;
230 for (int i = 0; i < sequenceCount; i++) {
231 if (getStopSites(i) == stopCount) {
232 count += 1;
233 }
234 }
235 return count;
236 }
237
238 public int getTotalCodonsMinusGaps() { return totalReads; }
239
240
241 class Defect {
242
243 private int type = 0;
244 private int sequence = 0;
245 private int site = 0;
246
247 public Defect(int type, int sequence, int site) {
248 this.type = type;
249 this.sequence = sequence;
250 this.site = site;
251 }
252
253 public boolean isStop() { return type == STOP; }
254
255 public boolean isIndel() { return type == INDEL; }
256
257 public int getSequence() { return sequence; }
258 public int getSite() { return site; }
259 }
260
261 /**
262 * @param fileName
263 * @throws java.io.IOException
264 */
265 private static Alignment readNexusFile(String fileName) throws java.io.IOException {
266
267 Alignment alignment = null;
268 TaxonList taxonList = null;
269
270 try {
271 FileReader reader = new FileReader(fileName);
272
273 NexusImporter importer = new NexusImporter(reader);
274
275 boolean done = false;
276
277 while (!done) {
278 try {
279
280 NexusImporter.NexusBlock block = importer.findNextBlock();
281
282 if (block == NexusImporter.TAXA_BLOCK) {
283
284 if (taxonList != null) {
285 throw new NexusImporter.MissingBlockException("TAXA block already defined");
286 }
287
288 taxonList = importer.parseTaxaBlock();
289
290 } else if (block == NexusImporter.DATA_BLOCK) {
291
292 // A data block doesn't need a taxon block before it
293 // but if one exists then it will use it.
294 alignment = importer.parseDataBlock(taxonList);
295 if (taxonList == null) {
296 taxonList = alignment;
297 }
298
299 } else if (block == NexusImporter.TREES_BLOCK) {
300
301 // ignore tree block
302 } else {
303 // Ignore the block..
304 }
305
306 } catch (EOFException ex) {
307 done = true;
308 }
309 }
310
311 } catch (Importer.ImportException ime) {
312 System.err.println("Error reading alignment: " + ime);
313 }
314
315 return alignment;
316 }
317
318 private static int[][] generateTransversionMutants(int[] triplet) {
319 int[][] tvmutants = new int[6][3];
320
321 for (int index = 0; index < 6; index += 1) {
322 for (int pos = 0; pos < 3; pos++) {
323 tvmutants[index][pos] = triplet[pos];
324 }
325 }
326
327 int index = 0;
328 for (int pos = 0; pos < 3; pos++) {
329 if (triplet[pos] % 2 == 0) {
330 tvmutants[index++][pos] = 1;
331 tvmutants[index++][pos] = 3;
332 } else {
333 tvmutants[index++][pos] = 0;
334 tvmutants[index++][pos] = 2;
335 }
336 }
337 return tvmutants;
338 }
339
340 private static int[][] generateTransitionMutants(int[] triplet) {
341 int[][] timutants = new int[3][3];
342
343 for (int index = 0; index < 3; index += 1) {
344 for (int pos = 0; pos < 3; pos++) {
345 timutants[index][pos] = triplet[pos];
346 }
347 }
348
349 for (int pos = 0; pos < 3; pos++) {
350 switch (triplet[pos]) {
351 case 0: timutants[pos][pos] = 2; break;
352 case 1: timutants[pos][pos] = 3; break;
353 case 2: timutants[pos][pos] = 0; break;
354 case 3: timutants[pos][pos] = 1; break;
355 default: throw new IllegalArgumentException();
356 }
357 }
358 return timutants;
359 }
360
361 public static void main(String[] args) throws java.io.IOException {
362
363 String alignmentFileName = null;
364 if (args != null && args.length > 0) {
365 alignmentFileName = args[0];
366 }
367
368 Alignment alignment = readNexusFile(alignmentFileName);
369
370 Defects defects = new Defects(alignment);
371
372 int siteCount = alignment.getSiteCount() / 3;
373 int sequenceCount = alignment.getSequenceCount();
374 int totalSites = siteCount * sequenceCount;
375 int totalReads = defects.getTotalCodonsMinusGaps();
376
377 int defectiveSites = defects.getDefectiveSiteCount();
378 int defectiveSequences = defects.getDefectiveSequenceCount();
379 int totalDefects = defects.getDefectCount();
380 int totalStops = defects.getStopCodonCount();
381
382 double siteP = (double)defectiveSites/(double)siteCount;
383 double sequenceP = (double)defectiveSequences/(double)sequenceCount;
384 double totalP = (double)totalDefects/(double)totalReads;
385 double totalSP = (double)totalStops/(double)totalReads;
386
387 System.out.println("Matrix size=" + totalSites);
388 System.out.println("Non-gap codons=" + totalReads);
389
390 System.out.println(defectiveSequences + "/" + sequenceCount + "(" + sequenceP + ") defective sequences.");
391 System.out.println(defectiveSites + "/" + siteCount + "(" + siteP + ") defective sites.");
392 System.out.println(totalDefects + "/" + totalReads + "(" + totalP + ") defects.");
393 System.out.println(totalStops + "/" + totalReads + "(" + totalSP + ") stop codons.");
394
395 double mean = (double)totalDefects/(double)sequenceCount;
396 System.out.println(mean + " defects per sequence");
397
398 double meanS = (double)totalStops/(double)sequenceCount;
399 System.out.println(meanS + " stops per sequence");
400
401 int maxDefects = defects.getMaxDefectiveSites();
402 int maxStops = defects.getMaxStopSites();
403 System.out.println("defective sequences:");
404 //Set seqs = defects.getSequences(maxDefects);
405 //for (Iterator i = seqs.iterator(); i.hasNext();) {
406 // Integer index = (Integer)i.next();
407 // String name = alignment.getTaxonId(index.intValue());
408 // System.out.println(" " + name);
409 //}
410 for (int d = 1; d <= maxDefects; d++) {
411 Set<Integer> seqs = defects.getSequences(d);
412 for(Integer seq : seqs) {
413 String name = alignment.getTaxonId(seq);
414 System.out.println(d + " " + name);
415 }
416
417 }
418
419 System.out.println("Defects\tSequences\texpected");
420 double probTerm;
421 probTerm = Math.exp(-mean); // probability of 0
422 for (int i=0; i<10; i++) {
423
424 System.out.println(i+"\t" + defects.getSequenceCount(i) + "\t" + probTerm*sequenceCount);
425
426 // compute probability of n from prob. of n-1
427 probTerm *= mean / (i+1);
428 }
429
430 System.out.println("Stops\tSequences\texpected");
431 probTerm = Math.exp(-meanS); // probability of 0
432 for (int i=0; i<10; i++) {
433
434 System.out.println(i+"\t" + defects.getStopSequenceCount(i) + "\t" + probTerm*sequenceCount);
435
436 // compute probability of n from prob. of n-1
437 probTerm *= meanS / (i+1);
438 }
439
440 System.out.println("stop-codon sequences:");
441 for (int d = 1; d <= maxStops; d++) {
442 Set<Integer> seqs = defects.getSequencesByStopCount(d);
443 for(Integer index : seqs) {
444 String name = alignment.getTaxonId(index);
445 System.out.println(d + " " + name);
446 }
447
448 }
449
450
451 Consensus con = new Consensus("mode", alignment, true);
452 Sequence consensus = con.getConsensusSequence();
453 SimpleAlignment a = new SimpleAlignment();
454 a.addSequence(new Sequence(consensus));
455 ConvertAlignment ca = new ConvertAlignment(Codons.UNIVERSAL, a);
456
457 double[] pstop = new double[ca.getSiteCount()];
458 int[] counts = new int[10];
459 for (double kappa = 2.0; kappa <= 2.0; kappa += 1.0) {
460 for (int i = 0; i < ca.getSiteCount(); i++) {
461 int state = ca.getState(0,i);
462 if (Codons.UNIVERSAL.isStopCodon(state)) {
463 throw new RuntimeException("Consensus has a stop codon in it at position " + i + "!");
464 }
465 int[] triplet = Codons.UNIVERSAL.getTripletStates(state);
466 int[][] tvmutants = generateTransversionMutants(triplet);
467 int[][] timutants = generateTransitionMutants(triplet);
468
469 int tvStops = 0;
470 for (int j = 0; j < 6; j++) {
471 if (Codons.UNIVERSAL.isStopCodon(Codons.UNIVERSAL.getState(tvmutants[j][0],tvmutants[j][1],tvmutants[j][2]))) {
472 tvStops += 1;
473 }
474 }
475
476 int tiStops = 0;
477 for (int j = 0; j < 3; j++) {
478 if (Codons.UNIVERSAL.isStopCodon(Codons.UNIVERSAL.getState(timutants[j][0],timutants[j][1],timutants[j][2]))) {
479 tiStops += 1;
480 }
481 }
482
483 pstop[i] = (tvStops + kappa*tiStops) / (6.0 + kappa*3.0);
484 counts[tvStops+tiStops] += 1;
485 }
486 System.out.println("kappa = " + kappa + " pstop=" + dr.stats.DiscreteStatistics.mean(pstop));
487 }
488
489 System.out.println("stop-mutations\tcodons");
490 for (int i = 0; i < 10; i++) {
491 System.out.println(i+"\t" + counts[i]);
492 }
493
494 /*int count = 0;
495 int reps = 100000;
496 for (int i = 0; i < reps; i++) {
497 SimpleAlignment simpleAlignment = new SimpleAlignment();
498 simpleAlignment.addSequence(new Sequence(consensus));
499
500 //pick a random position
501 int pos = random.nextInt(simpleAlignment.getSiteCount());
502 int state = simpleAlignment.getState(0,pos);
503 int newState = random.nextInt(4);
504 while (newState == state) {
505 newState = random.nextInt(4);
506 }
507 simpleAlignment.setState(0,pos,newState);
508 defects = new Defects(simpleAlignment);
509 count += defects.getDefectCount();
510 }
511
512 double p = (double)count/(double)reps;
513 */
514
515 double p = dr.stats.DiscreteStatistics.mean(pstop);
516 double rate = totalSP * (1.0-p) / p;
517
518 System.out.println("Total inferred point-mutation rate = " + rate);
519 System.out.println("Total inferred point-mutations = " + (totalStops * (1-p) / p));
520 System.out.println("Proportion of point-mutations that produce premature stop codons = " + p);
521 }
522 }