Debian Codegraph @

src/dr/evolution/alignment/ExtractPairs.java

raw symbols SCIP
1 /*
2 * ExtractPairs.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.sequence.Sequence;
30
31 /**
32 * @author Alexei Drummond
33 *
34 * @version $Id: ExtractPairs.java,v 1.3 2005/04/20 21:26:18 rambaut Exp $
35 */
36 public class ExtractPairs {
37
38 Alignment alignment;
39
40 public ExtractPairs(Alignment alignment) {
41
42 this.alignment = alignment;
43 }
44
45 public Alignment getPairAlignment(int x, int y) {
46
47 SimpleAlignment pairAlignment = new SimpleAlignment();
48
49 StringBuffer sequence0 = new StringBuffer();
50 StringBuffer sequence1 = new StringBuffer();
51
52 DataType dataType = alignment.getDataType();
53 int stateCount = dataType.getStateCount();
54
55 for (int i = 0; i < alignment.getSiteCount(); i++) {
56
57 int s0 = alignment.getState(x,i);
58 int s1 = alignment.getState(y,i);
59
60 char c0 = dataType.getChar(s0);
61 char c1 = dataType.getChar(s1);
62
63 if (s0 < stateCount || s1 < stateCount) {
64 sequence0.append(c0);
65 sequence1.append(c1);
66 }
67 }
68
69 // trim hanging ends on left
70 int left = 0;
71 while (
72 (dataType.getState(sequence0.charAt(left)) >= stateCount) ||
73 (dataType.getState(sequence1.charAt(left)) >= stateCount)) {
74 left += 1;
75 }
76
77 // trim hanging ends on right
78 int right = sequence0.length()-1;
79 while (
80 (dataType.getState(sequence0.charAt(right)) >= stateCount) ||
81 (dataType.getState(sequence1.charAt(right)) >= stateCount)) {
82 right -= 1;
83 }
84
85 if (right < left) return null;
86
87 String sequenceString0 = sequence0.substring(left,right+1);
88 String sequenceString1 = sequence1.substring(left,right+1);
89
90 pairAlignment.addSequence(new Sequence(alignment.getTaxon(x),sequenceString0));
91 pairAlignment.addSequence(new Sequence(alignment.getTaxon(y),sequenceString1));
92
93 return pairAlignment;
94 }
95
96 }