Debian Codegraph @

src/dr/evolution/alignment/Patterns.java

raw symbols SCIP
1 /*
2 * Patterns.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.DataType;
29 import dr.evolution.util.Taxon;
30 import dr.evolution.util.TaxonList;
31
32 import java.util.HashSet;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Set;
36
37 /**
38 * A concrete implementation of PatternList. Patterns can be added and
39 * removed from the list individually or in bulk from an alignment.
40 *
41 * @author Andrew Rambaut
42 * @author Alexei Drummond
43 * @version $Id: Patterns.java,v 1.10 2005/07/08 11:27:53 rambaut Exp $
44 */
45 public class Patterns implements PatternList {
46
47 public static final int COUNT_INCREMENT = 100;
48
49 /**
50 * number of patterns
51 */
52 protected int patternCount = 0;
53
54 /**
55 * length of patterns
56 */
57 protected int patternLength = 0;
58
59 /**
60 * weights of each pattern
61 */
62 protected double[] weights = new double[COUNT_INCREMENT];
63
64 /**
65 * site patterns [pattern][taxon]
66 */
67 protected int[][] patterns = new int[COUNT_INCREMENT][];
68
69 protected DataType dataType = null;
70
71 protected TaxonList taxonList = null;
72
73 /**
74 * Constructor
75 */
76 public Patterns(DataType dataType) {
77 this.dataType = dataType;
78 }
79
80 /**
81 * Constructor
82 */
83 public Patterns(DataType dataType, TaxonList taxonList) {
84 this.dataType = dataType;
85 this.taxonList = taxonList;
86 patternLength = taxonList.getTaxonCount();
87 }
88
89 /**
90 * Constructor
91 */
92 public Patterns(SiteList siteList) {
93 addPatterns(siteList, 0, 0, 1);
94 }
95
96 /**
97 * Constructor
98 */
99 public Patterns(List<SiteList> siteLists) {
100 for (SiteList siteList : siteLists) {
101 addPatterns(siteList, 0, 0, 1);
102 }
103 }
104
105 /**
106 * Constructor
107 */
108 public Patterns(SiteList siteList, int from, int to, int every) {
109 addPatterns(siteList, from, to, every);
110 }
111
112 /**
113 * Constructor
114 */
115 public Patterns(SiteList siteList, int from, int to, int every, int subSet, int subSetCount) {
116 addPatterns(siteList, from, to, every);
117 subSetPatterns(subSet, subSetCount);
118 }
119
120 /**
121 * Constructor
122 */
123 public Patterns(PatternList patternList) {
124 addPatterns(patternList);
125 }
126
127 /**
128 * Constructor
129 */
130 public Patterns(PatternList patternList, int subSet, int subSetCount) {
131 addPatterns(patternList);
132 subSetPatterns(subSet, subSetCount);
133 }
134
135 private void subSetPatterns(int subSet, int subSetCount) {
136 if (subSetCount > 0) {
137 // if we are using subSetCount then cut it down to only the subset we want...
138 int div = patternCount / subSetCount;
139 int rem = patternCount % subSetCount;
140
141 int start = 0;
142 for (int i = 0; i < subSet; i++) {
143 start += div + (i < rem ? 1 : 0);
144 }
145
146 int newPatternCount = div;
147 if (subSet < rem) {
148 newPatternCount++;
149 }
150
151 int[][] newPatterns = new int[newPatternCount][];
152 double[] newWeights = new double[newPatternCount];
153 for (int i = 0; i < newPatternCount; i++) {
154 newPatterns[i] = patterns[start + i];
155 newWeights[i] = weights[start + i];
156 }
157 patterns = newPatterns;
158 weights = newWeights;
159
160 patternCount = newPatternCount;
161 }
162 }
163
164 /**
165 * adds patterns to the list from a SiteList
166 */
167 public void addPatterns(SiteList siteList, int from, int to, int every) {
168
169 if (siteList == null) {
170 return;
171 }
172
173 if (taxonList == null) {
174 taxonList = siteList;
175 patternLength = taxonList.getTaxonCount();
176 }
177
178 if (dataType == null) {
179 dataType = siteList.getDataType();
180 } else if (dataType != siteList.getDataType()) {
181 throw new IllegalArgumentException("Patterns' existing DataType does not match that of added SiteList");
182 }
183
184 if (from < 0)
185 from = 0;
186
187 if (to <= 0)
188 to = siteList.getSiteCount() - 1;
189
190 if (every <= 0)
191 every = 1;
192
193 for (int i = from; i <= to; i += every) {
194 int[] pattern = siteList.getSitePattern(i);
195
196 // don't add patterns that are all gaps or all ambiguous
197 if (pattern != null && (!isInvariant(pattern) ||
198 (!isGapped(pattern) &&
199 !isAmbiguous(pattern) &&
200 !isUnknown(pattern)))) {
201
202 addPattern(pattern, 1.0);
203 }
204 }
205 areUnique = siteList.areUnique();
206 }
207
208 /**
209 * adds patterns to the list from a SiteList
210 */
211 public void addPatterns(PatternList patternList) {
212
213 if (patternList == null) {
214 return;
215 }
216
217 if (taxonList == null) {
218 taxonList = patternList;
219 patternLength = taxonList.getTaxonCount();
220 }
221
222 if (dataType == null) {
223 dataType = patternList.getDataType();
224 } else if (dataType != patternList.getDataType()) {
225 throw new IllegalArgumentException("Patterns' existing DataType does not match that of added PatternList");
226 }
227
228 for (int i = 0; i < patternList.getPatternCount(); i++) {
229 int[] pattern = patternList.getPattern(i);
230
231 // don't add patterns that are all gaps or all ambiguous
232 if (!isInvariant(pattern) ||
233 (!isGapped(pattern) &&
234 !isAmbiguous(pattern) &&
235 !isUnknown(pattern))) {
236
237 addPattern(pattern, patternList.getPatternWeight(i));
238 }
239 }
240 areUnique = patternList.areUnique();
241 }
242
243 /**
244 * adds a pattern to the pattern list with a default weight of 1
245 */
246 public void addPattern(int[] pattern) {
247 addPattern(pattern, 1.0);
248 }
249
250 /**
251 * adds a pattern to the pattern list
252 */
253 public void addPattern(int[] pattern, double weight) {
254
255 if (patternLength == 0) {
256 patternLength = pattern.length;
257 }
258
259 if (patternLength != 0 && pattern.length != patternLength) {
260 throw new IllegalArgumentException("Added pattern's length (" + pattern.length + ") does not match those of existing patterns (" + patternLength + ")");
261 }
262
263 for (int i = 0; i < patternCount; i++) {
264
265 if (comparePatterns(patterns[i], pattern)) {
266
267 weights[i] += weight;
268 return;
269 }
270 }
271
272 if (patternCount == patterns.length) {
273 int[][] newPatterns = new int[patternCount + COUNT_INCREMENT][];
274 double[] newWeights = new double[patternCount + COUNT_INCREMENT];
275 for (int i = 0; i < patternCount; i++) {
276 newPatterns[i] = patterns[i];
277 newWeights[i] = weights[i];
278 }
279 patterns = newPatterns;
280 weights = newWeights;
281 }
282
283 patterns[patternCount] = pattern;
284 weights[patternCount] = weight;
285 patternCount++;
286 }
287
288 /**
289 * removes a pattern from the pattern list
290 */
291 public void removePattern(int[] pattern) {
292
293 int index = -1;
294 for (int i = 0; i < patternCount; i++) {
295
296 if (comparePatterns(patterns[i], pattern)) {
297 index = i;
298 break;
299 }
300 }
301
302 if (index == -1) throw new IllegalArgumentException("Pattern not found");
303
304 weights[index] -= 1;
305 if (weights[index] == 0 && patternCount > 1) {
306 patterns[index] = patterns[patternCount - 1];
307 patterns[patternCount - 1] = null;
308 weights[index] = weights[patternCount - 1];
309 patternCount--;
310 }
311
312 }
313
314 /**
315 * removes all patterns from the pattern list
316 */
317 public void removeAllPatterns() {
318 patternCount = 0;
319 for (int i = 0; i < patterns.length; i++) patterns[i] = null;
320 }
321
322 /**
323 * @return true if the pattern has one or more gaps
324 */
325 protected boolean isGapped(int[] pattern) {
326 int len = pattern.length;
327
328 for (int i = 0; i < len; i++) {
329 if (getDataType().isGapState(pattern[i])) {
330 return true;
331 }
332 }
333 return false;
334 }
335
336 /**
337 * @return true if the pattern has one or more ambiguous states
338 */
339 protected boolean isAmbiguous(int[] pattern) {
340 int len = pattern.length;
341
342 for (int i = 0; i < len; i++) {
343 if (getDataType().isAmbiguousState(pattern[i])) {
344 return true;
345 }
346 }
347 return false;
348 }
349
350 /**
351 * @return true if the pattern is invariant
352 */
353 protected boolean isUnknown(int[] pattern) {
354 int len = pattern.length;
355
356 for (int i = 0; i < len; i++) {
357 if (getDataType().isUnknownState(pattern[i])) {
358 return true;
359 }
360 }
361 return false;
362 }
363
364 /**
365 * @return true if the pattern is invariant
366 */
367 protected static boolean isInvariant(int[] pattern) {
368 int len = pattern.length;
369
370 int state = pattern[0];
371 for (int i = 1; i < len; i++) {
372 if (pattern[i] != state) {
373 return false;
374 }
375 }
376
377 return true;
378 }
379
380 /**
381 * compares two patterns
382 *
383 * @return true if they are identical
384 */
385 protected boolean comparePatterns(int[] pattern1, int[] pattern2) {
386
387 int len = pattern1.length;
388 for (int i = 0; i < len; i++) {
389 if (pattern1[i] != pattern2[i]) {
390 return false;
391 }
392 }
393
394 return true;
395 }
396
397 // **************************************************************
398 // PatternList IMPLEMENTATION
399 // **************************************************************
400
401 /**
402 * @return number of patterns
403 */
404 public int getPatternCount() {
405 return patternCount;
406 }
407
408 /**
409 * @return number of states for this siteList
410 */
411 public int getStateCount() {
412 return dataType.getStateCount();
413 }
414
415 /**
416 * Gets the length of the pattern strings which will usually be the
417 * same as the number of taxa
418 *
419 * @return the length of patterns
420 */
421 public int getPatternLength() {
422 return patternLength;
423 }
424
425 /**
426 * Gets the pattern as an array of state numbers (one per sequences)
427 *
428 * @return the pattern at patternIndex
429 */
430 public int[] getPattern(int patternIndex) {
431 return patterns[patternIndex];
432 }
433
434 @Override
435 public double[][] getUncertainPattern(int patternIndex) {
436 throw new UnsupportedOperationException("uncertain patterns not implemented yet");
437 }
438
439 /**
440 * @return state at (taxonIndex, patternIndex)
441 */
442 public int getPatternState(int taxonIndex, int patternIndex) {
443 return patterns[patternIndex][taxonIndex];
444 }
445
446 @Override
447 public double[] getUncertainPatternState(int taxonIndex, int patternIndex) {
448 throw new UnsupportedOperationException("uncertain patterns not implemented yet");
449 }
450
451 /**
452 * Gets the weight of a site pattern
453 */
454 public double getPatternWeight(int patternIndex) {
455 return weights[patternIndex];
456 }
457
458 /**
459 * @return the array of pattern weights
460 */
461 public double[] getPatternWeights() {
462 double[] w = new double[weights.length];
463 for (int i = 0; i < weights.length; i++) w[i] = weights[i];
464 return w;
465 }
466
467 /**
468 * @return the DataType of this siteList
469 */
470 public DataType getDataType() {
471 return dataType;
472 }
473
474 /**
475 * @return the frequency of each state
476 */
477 public double[] getStateFrequencies() {
478 return PatternList.Utils.empiricalStateFrequencies(this);
479 }
480
481 @Override
482 public boolean areUnique() {
483 return areUnique;
484 }
485
486 @Override
487 public boolean areUncertain() {
488 return false;
489 }
490
491 // **************************************************************
492 // TaxonList IMPLEMENTATION
493 // **************************************************************
494
495 /**
496 * @return a count of the number of taxa in the list.
497 */
498 public int getTaxonCount() {
499 if (taxonList == null) throw new RuntimeException("Patterns has no TaxonList");
500 return taxonList.getTaxonCount();
501 }
502
503 /**
504 * @return the ith taxon.
505 */
506 public Taxon getTaxon(int taxonIndex) {
507 if (taxonList == null) throw new RuntimeException("Patterns has no TaxonList");
508 return taxonList.getTaxon(taxonIndex);
509 }
510
511 /**
512 * @return the ID of the ith taxon.
513 */
514 public String getTaxonId(int taxonIndex) {
515 if (taxonList == null) throw new RuntimeException("Patterns has no TaxonList");
516 return taxonList.getTaxonId(taxonIndex);
517 }
518
519 /**
520 * returns the index of the taxon with the given id.
521 */
522 public int getTaxonIndex(String id) {
523 if (taxonList == null) throw new RuntimeException("Patterns has no TaxonList");
524 return taxonList.getTaxonIndex(id);
525 }
526
527 /**
528 * returns the index of the given taxon.
529 */
530 public int getTaxonIndex(Taxon taxon) {
531 if (taxonList == null) throw new RuntimeException("Patterns has no TaxonList");
532 return taxonList.getTaxonIndex(taxon);
533 }
534
535 public List<Taxon> asList() {
536 if (taxonList == null) throw new RuntimeException("Patterns has no TaxonList");
537 return taxonList.asList();
538 }
539
540 public Iterator<Taxon> iterator() {
541 if (taxonList == null) throw new RuntimeException("Patterns has no TaxonList");
542 return taxonList.iterator();
543 }
544
545
546 /**
547 * @param taxonIndex the index of the taxon whose attribute is being fetched.
548 * @param name the name of the attribute of interest.
549 * @return an object representing the named attributed for the given taxon.
550 */
551 public Object getTaxonAttribute(int taxonIndex, String name) {
552 if (taxonList == null) throw new RuntimeException("Patterns has no TaxonList");
553 return taxonList.getTaxonAttribute(taxonIndex, name);
554 }
555
556 // **************************************************************
557 // Identifiable IMPLEMENTATION
558 // **************************************************************
559
560 protected String id = null;
561
562 /**
563 * @return the id.
564 */
565 public String getId() {
566 return id;
567 }
568
569 /**
570 * Sets the id.
571 */
572 public void setId(String id) {
573 this.id = id;
574 }
575
576 // ========= Mask =========
577 // indexes to mask sth., e.g. taxon index whose state is unknown character in microsatellite
578 protected Set<Integer> maskSet = new HashSet<Integer>();
579
580 // no duplication, if duplicate, not add
581 public boolean addMask(int index) {
582 return maskSet.add(index);
583 }
584
585 public boolean isMasked(int index) {
586 return maskSet.contains(index);
587 }
588
589 public boolean hasMask() {
590 return maskSet.size() > 0;
591 }
592
593 public void clearMask() {
594 maskSet.clear();
595 }
596
597 public Set<Integer> getMaskSet() {
598 return maskSet;
599 }
600
601 /**
602 * @return the ith taxon not masked.
603 */
604 public Taxon getTaxonMasked(int taxonIndex) {
605 if (taxonList == null) throw new RuntimeException("Patterns has no TaxonList");
606 if (isMasked(taxonIndex)) {
607 return null;
608 }
609 return taxonList.getTaxon(taxonIndex);
610 }
611
612 private boolean areUnique = true;
613 }