| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 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 |
|
| 72 |
|
| 73 |
addDefect(STOP, i, j); |
| 74 |
totalReads += 1; |
| 75 |
} else if (codons.isGapState(state)) { |
| 76 |
|
| 77 |
|
| 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 |
|
| 84 |
} else { |
| 85 |
|
| 86 |
|
| 87 |
addDefect(INDEL, i, j); |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
} else { |
| 92 |
|
| 93 |
totalReads += 1; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 201 |
|
| 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 |
|
| 215 |
|
| 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 |
|
| 263 |
|
| 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 |
|
| 293 |
|
| 294 |
alignment = importer.parseDataBlock(taxonList); |
| 295 |
if (taxonList == null) { |
| 296 |
taxonList = alignment; |
| 297 |
} |
| 298 |
|
| 299 |
} else if (block == NexusImporter.TREES_BLOCK) { |
| 300 |
|
| 301 |
|
| 302 |
} else { |
| 303 |
|
| 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 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 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); |
| 422 |
for (int i=0; i<10; i++) { |
| 423 |
|
| 424 |
System.out.println(i+"\t" + defects.getSequenceCount(i) + "\t" + probTerm*sequenceCount); |
| 425 |
|
| 426 |
|
| 427 |
probTerm *= mean / (i+1); |
| 428 |
} |
| 429 |
|
| 430 |
System.out.println("Stops\tSequences\texpected"); |
| 431 |
probTerm = Math.exp(-meanS); |
| 432 |
for (int i=0; i<10; i++) { |
| 433 |
|
| 434 |
System.out.println(i+"\t" + defects.getStopSequenceCount(i) + "\t" + probTerm*sequenceCount); |
| 435 |
|
| 436 |
|
| 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 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 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 |
} |