Debian Codegraph @

src/dr/evolution/alignment/SitePatterns.java

raw symbols SCIP
1 /*
2 * SitePatterns.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.AminoAcids;
29 import dr.evolution.datatype.Codons;
30 import dr.evolution.datatype.DataType;
31 import dr.evolution.datatype.Nucleotides;
32 import dr.evolution.util.Taxon;
33 import dr.evolution.util.TaxonList;
34 import dr.inference.model.Parameter;
35 import dr.inference.model.Variable;
36
37 import java.util.*;
38
39 /**
40 * Stores a set of site patterns. This differs from the simple Patterns
41 * class because it stores the pattern index for each site. Thus it has
42 * a connection to a single alignment.
43 *
44 * @author Andrew Rambaut
45 * @author Alexei Drummond
46 * @version $Id: SitePatterns.java,v 1.47 2005/06/22 16:44:17 rambaut Exp $
47 */
48 public class SitePatterns implements SiteList, dr.util.XHTMLable {
49
50 /**
51 * the source alignment
52 */
53 protected SiteList siteList = null;
54
55 /**
56 * number of sites
57 */
58 protected int siteCount = 0;
59
60 /**
61 * number of patterns
62 */
63 protected int patternCount = 0;
64
65 /**
66 * length of site patterns
67 */
68 protected int patternLength = 0;
69
70 /**
71 * site -> site pattern
72 */
73 protected int[] sitePatternIndices;
74
75 /**
76 * count of invariant patterns
77 */
78 protected int invariantCount;
79
80 /**
81 * weights of each site pattern
82 */
83 protected double[] weights;
84
85 /**
86 * site patterns [site pattern][taxon]
87 */
88 protected int[][] patterns;
89
90 protected double[][][] uncertainPatterns;
91
92 protected int from, to, every;
93
94 protected boolean strip = true; // Strip out completely ambiguous sites
95
96 protected boolean unique = true; // Compress into weighted list of unique patterns
97
98 private boolean uncertainSites = false;
99
100 /**
101 * Constructor
102 */
103 public SitePatterns(Alignment alignment) {
104 this(alignment, null, 0, 0, 1);
105 }
106
107 /**
108 * Constructor
109 */
110 public SitePatterns(Alignment alignment, TaxonList taxa) {
111 this(alignment, taxa, 0, 0, 1);
112 }
113
114 /**
115 * Constructor
116 */
117 public SitePatterns(Alignment alignment, int from, int to, int every) {
118 this(alignment, null, from, to, every);
119 }
120
121 // /**
122 // * Constructor for dnds
123 // */
124 // public SitePatterns(Alignment alignment, int from, int to, int every, boolean unique) {
125 // this(alignment, null, from, to, every, unique);
126 // }
127
128 /**
129 * Constructor
130 */
131
132 public SitePatterns(Alignment alignment, TaxonList taxa, int from, int to, int every) {
133 this(alignment,taxa,from,to,every,true);
134 }
135
136 public SitePatterns(Alignment alignment, TaxonList taxa, int from, int to, int every, boolean strip) {
137 this(alignment, taxa, from, to, every, strip, true);
138 }
139
140 public SitePatterns(Alignment alignment, TaxonList taxa, int from, int to, int every, boolean strip, boolean unique) {
141 this(alignment, taxa, from, to, every, strip, unique, null);
142 }
143
144 public SitePatterns(Alignment alignment, TaxonList taxa, int from, int to, int every, boolean strip, boolean unique, int[] constantSiteCounts) {
145 if (taxa != null) {
146 SimpleAlignment a = new SimpleAlignment();
147
148 for (int i = 0; i < alignment.getSequenceCount(); i++) {
149 if (taxa.getTaxonIndex(alignment.getTaxonId(i)) != -1) {
150 a.addSequence(alignment.getSequence(i));
151 }
152 }
153
154 alignment = a;
155 }
156 this.strip = strip;
157 this.unique = unique;
158
159 setPatterns(alignment, from, to, every, constantSiteCounts);
160 }
161
162 /**
163 * Constructor
164 */
165 public SitePatterns(SiteList siteList) {
166 this(siteList, -1, -1, 1, true, true);
167 }
168
169 /**
170 * Constructor
171 */
172 public SitePatterns(SiteList siteList, int from, int to, int every) {
173 this(siteList, from, to, every, true, true);
174 }
175
176 /**
177 * Constructor
178 */
179 public SitePatterns(SiteList siteList, int from, int to, int every, boolean strip) {
180 this(siteList, from, to, every, strip, true);
181 }
182
183 /**
184 * Constructor
185 */
186 public SitePatterns(SiteList siteList, int from, int to, int every, boolean strip, boolean unique) {
187 this.strip = strip;
188 this.unique = unique;
189 setPatterns(siteList, from, to, every, null);
190 }
191
192 /**
193 * Constructor
194 */
195 public SitePatterns(SiteList siteList, boolean[] mask) {
196 this(siteList, mask, true, true);
197 }
198
199 /**
200 * Constructor
201 */
202 public SitePatterns(SiteList siteList, boolean[] mask, boolean strip) {
203 this(siteList, mask, strip, true);
204 }
205
206 /**
207 * Constructor
208 */
209 public SitePatterns(SiteList siteList, boolean[] mask, boolean strip, boolean unique) {
210 this.strip = strip;
211 this.unique = unique;
212 setPatterns(siteList, mask);
213 }
214
215 public SiteList getSiteList() {
216 return siteList;
217 }
218
219 public int getFrom() {
220 return from;
221 }
222
223 public int getTo() {
224 return to;
225 }
226
227 public int getEvery() {
228 return every;
229 }
230
231 /**
232 * sets up pattern list using an alignment
233 */
234 public void setPatterns(SiteList siteList, int from, int to, int every, int[] constantSiteCounts) {
235
236 this.siteList = siteList;
237 this.from = from;
238 this.to = to;
239 this.every = every;
240
241 if (siteList == null) {
242 return;
243 }
244
245 if (from <= -1)
246 from = 0;
247
248 if (to <= -1)
249 to = siteList.getSiteCount() - 1;
250
251 if (every <= 0)
252 every = 1;
253
254 siteCount = ((to - from) / every) + 1;
255
256 patternCount = 0;
257
258 patterns = new int[siteCount][];
259
260 sitePatternIndices = new int[siteCount];
261 weights = new double[siteCount];
262
263 invariantCount = 0;
264
265 uncertainSites = siteList.areUncertain();
266
267 if (uncertainSites) {
268 uncertainPatterns = new double[siteCount][][];
269 }
270
271 if (constantSiteCounts != null) {
272 if (constantSiteCounts.length != siteList.getStateCount()) {
273 throw new IllegalArgumentException("Constant site count array length doesn't equal the number of states");
274 }
275 for (int i = 0; i < siteList.getStateCount(); i++) {
276 int[] pattern = new int[siteList.getPatternLength()];
277 for (int j = 0; j < siteList.getPatternLength(); j++) {
278 pattern[j] = i;
279 }
280 addPattern(pattern, constantSiteCounts[i], null);
281 }
282 }
283
284 int site = 0;
285
286 for (int i = from; i <= to; i += every) {
287 int[] pattern = siteList.getSitePattern(i);
288 double[][] probs = null;
289
290 if (uncertainSites) {
291 probs = siteList.getUncertainSitePattern(i);
292 }
293
294 if (!strip || !isInvariant(pattern) ||
295 (!isGapped(pattern) &&
296 !isAmbiguous(pattern) &&
297 !isUnknown(pattern))) {
298
299 sitePatternIndices[site] = addPattern(pattern, probs);
300
301 } else {
302 sitePatternIndices[site] = -1;
303 }
304 site++;
305 }
306 }
307
308 /**
309 * sets up pattern list using an alignment
310 */
311 public void setPatterns(SiteList siteList, boolean[] mask) {
312
313 this.siteList = siteList;
314 if (siteList == null) {
315 return;
316 }
317
318 from = 0;
319 to = siteList.getSiteCount() - 1;
320 every = 1;
321
322 siteCount = siteList.getSiteCount();
323
324 patternCount = 0;
325
326 patterns = new int[siteCount][];
327
328 sitePatternIndices = new int[siteCount];
329 weights = new double[siteCount];
330
331 uncertainSites = siteList.areUncertain();
332
333 if (uncertainSites) {
334 uncertainPatterns = new double[siteCount][][];
335 }
336
337 invariantCount = 0;
338 int[] pattern;
339
340 int site = 0;
341
342 for (int i = from; i <= to; i += every) {
343 pattern = siteList.getSitePattern(i);
344
345 if (mask[i]) {
346 if (!strip || !isInvariant(pattern) ||
347 (!isGapped(pattern) &&
348 !isAmbiguous(pattern) &&
349 !isUnknown(pattern))
350 ) {
351
352 double[][] probs = null;
353 if (uncertainSites) {
354 probs = siteList.getUncertainSitePattern(i);
355 }
356 sitePatternIndices[site] = addPattern(pattern, probs);
357
358 } else {
359 sitePatternIndices[site] = -1;
360 }
361 site++;
362 }
363 }
364 }
365
366
367 /**
368 * adds a pattern to the pattern list
369 *
370 * @return the index of the pattern in the pattern list
371 */
372 private int addPattern(int[] pattern, double[][] uncertainty) {
373 return addPattern(pattern, 1, uncertainty);
374 }
375
376 /**
377 * adds a pattern to the pattern list with the given weight
378 *
379 * @return the index of the pattern in the pattern list
380 */
381 private int addPattern(int[] pattern, int weight, double[][] uncertainty) {
382
383 for (int i = 0; i < patternCount; i++) {
384
385 if (unique && comparePatterns(patterns[i], pattern)) {
386
387 weights[i] += weight;
388 return i;
389 }
390 }
391
392 if (isInvariant(pattern)) {
393 invariantCount += weight;
394 }
395
396 int index = patternCount;
397 patterns[index] = pattern;
398 weights[index] = weight;
399
400 if (uncertainSites) {
401
402 if (uncertainty == null) {
403 uncertainPatterns[index] = new double[pattern.length][];
404 for (int taxon = 0; taxon < pattern.length; ++taxon) {
405 double[] prob = new double[getDataType().getStateCount()];
406 int[] possibleStates = getDataType().getStates(pattern[taxon]);
407 for (int state : possibleStates) {
408 prob[state] = 1.0;
409 }
410 uncertainPatterns[index][taxon] = prob;
411 }
412 } else {
413 uncertainPatterns[index] = uncertainty;
414 }
415 }
416
417 patternCount++;
418
419 return index;
420 }
421
422 /**
423 * @return true if the pattern is invariant
424 */
425 private boolean isGapped(int[] pattern) {
426 int len = pattern.length;
427
428 for (int i = 0; i < len; i++) {
429 if (getDataType().isGapState(pattern[i])) {
430 return true;
431 }
432 }
433 return false;
434 }
435
436 /**
437 * @return true if the pattern is invariant
438 */
439 private boolean isAmbiguous(int[] pattern) {
440 int len = pattern.length;
441
442 for (int i = 0; i < len; i++) {
443 if (getDataType().isAmbiguousState(pattern[i])) {
444 return true;
445 }
446 }
447 return false;
448 }
449
450 /**
451 * @return true if the pattern is invariant
452 */
453 private boolean isUnknown(int[] pattern) {
454 int len = pattern.length;
455
456 for (int i = 0; i < len; i++) {
457 if (getDataType().isUnknownState(pattern[i])) {
458 return true;
459 }
460 }
461 return false;
462 }
463
464 /**
465 * @return true if the pattern is invariant
466 */
467 private boolean isInvariant(int[] pattern) {
468 int len = pattern.length;
469
470 int state = pattern[0];
471 for (int i = 1; i < len; i++) {
472 if (pattern[i] != state) {
473 return false;
474 }
475 }
476
477 return true;
478 }
479
480 /**
481 * compares two patterns
482 *
483 * @return true if they are identical
484 */
485 protected boolean comparePatterns(int[] pattern1, int[] pattern2) {
486
487 int len = pattern1.length;
488 for (int i = 0; i < len; i++) {
489 if (pattern1[i] != pattern2[i]) {
490 return false;
491 }
492 }
493
494 return true;
495 }
496
497 /**
498 * @return number of invariant sites (these will be first in the list).
499 */
500 public int getInvariantCount() {
501 return invariantCount;
502 }
503
504 // **************************************************************
505 // SiteList IMPLEMENTATION
506 // **************************************************************
507
508 /**
509 * @return number of sites
510 */
511 public int getSiteCount() {
512 return siteCount;
513 }
514
515 /**
516 * Gets the pattern of site as an array of state numbers (one per sequence)
517 *
518 * @return the site pattern at siteIndex
519 */
520 public int[] getSitePattern(int siteIndex) {
521 final int sitePatternIndice = sitePatternIndices[siteIndex];
522 return sitePatternIndice >= 0 ? patterns[sitePatternIndice] : null;
523 }
524
525 @Override
526 public double[][] getUncertainSitePattern(int siteIndex) {
527 throw new UnsupportedOperationException("getUncertainSitePattern not implemented yet");
528 }
529
530 /**
531 * Gets the pattern index at a particular site
532 *
533 * @return the patternIndex
534 */
535 public int getPatternIndex(int siteIndex) {
536 return sitePatternIndices[siteIndex];
537 }
538
539 /**
540 * @return the sequence state at (taxon, site)
541 */
542 public int getState(int taxonIndex, int siteIndex) {
543 final int sitePatternIndice = sitePatternIndices[siteIndex];
544 // is that right?
545 return sitePatternIndice >= 0 ? patterns[sitePatternIndice][taxonIndex] : getDataType().getGapState();
546 }
547
548 @Override
549 public double[] getUncertainState(int taxonIndex, int siteIndex) {
550 throw new UnsupportedOperationException("getUncertainState not implemented yet");
551 }
552
553 // **************************************************************
554 // PatternList IMPLEMENTATION
555 // **************************************************************
556
557 /**
558 * @return number of patterns
559 */
560 public int getPatternCount() {
561 return patternCount;
562 }
563
564 /**
565 * @return number of states for this siteList
566 */
567 public int getStateCount() {
568 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
569 return siteList.getStateCount();
570 }
571
572 /**
573 * Gets the length of the pattern strings which will usually be the
574 * same as the number of taxa
575 *
576 * @return the length of patterns
577 */
578 public int getPatternLength() {
579 return getTaxonCount();
580 }
581
582 /**
583 * Gets the pattern as an array of state numbers (one per sequence)
584 *
585 * @return the pattern at patternIndex
586 */
587 public int[] getPattern(int patternIndex) {
588 return patterns[patternIndex];
589 }
590
591 @Override
592 public double[][] getUncertainPattern(int patternIndex) {
593 throw new UnsupportedOperationException("getUncertainPattern not implemented yet");
594 }
595
596 /**
597 * @return state at (taxonIndex, patternIndex)
598 */
599 public int getPatternState(int taxonIndex, int patternIndex) {
600 return patterns[patternIndex][taxonIndex];
601 }
602
603 @Override
604 public double[] getUncertainPatternState(int taxonIndex, int patternIndex) {
605 return uncertainPatterns[patternIndex][taxonIndex];
606 }
607
608 /**
609 * Gets the weight of a site pattern
610 */
611 public double getPatternWeight(int patternIndex) {
612 return weights[patternIndex];
613 }
614
615 /**
616 * @return the array of pattern weights
617 */
618 public double[] getPatternWeights() {
619 return weights;
620 }
621
622 /**
623 * @return the DataType of this siteList
624 */
625 public DataType getDataType() {
626 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
627 return siteList.getDataType();
628 }
629
630 /**
631 * @return the frequency of each state
632 */
633 public double[] getStateFrequencies() {
634 return PatternList.Utils.empiricalStateFrequencies(this);
635 }
636
637 @Override
638 public boolean areUnique() {
639 return unique;
640 }
641
642 @Override
643 public boolean areUncertain() {
644 return uncertainSites;
645 }
646
647 // **************************************************************
648 // TaxonList IMPLEMENTATION
649 // **************************************************************
650
651 /**
652 * @return a count of the number of taxa in the list.
653 */
654 public int getTaxonCount() {
655 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
656 return siteList.getTaxonCount();
657 }
658
659 /**
660 * @return the ith taxon.
661 */
662 public Taxon getTaxon(int taxonIndex) {
663 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
664 return siteList.getTaxon(taxonIndex);
665 }
666
667 /**
668 * @return the ID of the ith taxon.
669 */
670 public String getTaxonId(int taxonIndex) {
671 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
672 return siteList.getTaxonId(taxonIndex);
673 }
674
675 /**
676 * returns the index of the taxon with the given id.
677 */
678 public int getTaxonIndex(String id) {
679 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
680 return siteList.getTaxonIndex(id);
681 }
682
683 /**
684 * returns the index of the given taxon.
685 */
686 public int getTaxonIndex(Taxon taxon) {
687 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
688 return siteList.getTaxonIndex(taxon);
689 }
690
691 public List<Taxon> asList() {
692 List<Taxon> taxa = new ArrayList<Taxon>();
693 for (int i = 0, n = getTaxonCount(); i < n; i++) {
694 taxa.add(getTaxon(i));
695 }
696 return taxa;
697 }
698
699 public Iterator<Taxon> iterator() {
700 return new Iterator<Taxon>() {
701 private int index = -1;
702
703 public boolean hasNext() {
704 return index < getTaxonCount() - 1;
705 }
706
707 public Taxon next() {
708 index ++;
709 return getTaxon(index);
710 }
711
712 public void remove() { /* do nothing */ }
713 };
714 }
715
716 /**
717 * @param taxonIndex the index of the taxon whose attribute is being fetched.
718 * @param name the name of the attribute of interest.
719 * @return an object representing the named attributed for the given taxon.
720 */
721 public Object getTaxonAttribute(int taxonIndex, String name) {
722 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
723 return siteList.getTaxonAttribute(taxonIndex, name);
724 }
725
726 // **************************************************************
727 // Identifiable IMPLEMENTATION
728 // **************************************************************
729
730 protected String id = null;
731
732 /**
733 * @return the id.
734 */
735 public String getId() {
736 return id;
737 }
738
739 /**
740 * Sets the id.
741 */
742 public void setId(String id) {
743 this.id = id;
744 }
745
746 // **************************************************************
747 // XHTMLable IMPLEMENTATION
748 // **************************************************************
749
750 public String toXHTML() {
751 String xhtml = "<p><em>Pattern List</em> pattern count = ";
752 xhtml += getPatternCount();
753 xhtml += " invariant count = ";
754 xhtml += getInvariantCount();
755 xhtml += "</p>";
756
757 xhtml += "<pre>";
758
759 int count, state;
760 int type = getDataType().getType();
761
762 count = getPatternCount();
763
764 int length, maxLength = 0;
765 for (int i = 0; i < count; i++) {
766 length = Integer.toString((int) getPatternWeight(i)).length();
767 if (length > maxLength)
768 maxLength = length;
769 }
770
771 for (int i = 0; i < count; i++) {
772 length = Integer.toString(i + 1).length();
773 for (int j = length; j < maxLength; j++)
774 xhtml += " ";
775 xhtml += Integer.toString(i + 1) + ": ";
776
777 length = Integer.toString((int) getPatternWeight(i)).length();
778 xhtml += Integer.toString((int) getPatternWeight(i));
779 for (int j = length; j <= maxLength; j++)
780 xhtml += " ";
781
782 for (int j = 0; j < getTaxonCount(); j++) {
783 state = getPatternState(j, i);
784
785 if (type == DataType.NUCLEOTIDES) {
786 xhtml += Nucleotides.INSTANCE.getChar(state) + " ";
787 } else if (type == DataType.CODONS) {
788 xhtml += Codons.UNIVERSAL.getTriplet(state) + " ";
789 } else {
790 xhtml += AminoAcids.INSTANCE.getChar(state) + " ";
791 }
792 }
793 xhtml += "\n";
794 }
795 xhtml += "</pre>";
796 return xhtml;
797 }
798 }