Debian Codegraph @

src/dr/oldevomodel/lineage/LineageSitePatterns.java

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