2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
20 package jalview.analysis;
\r
22 import jalview.datamodel.*;
\r
23 import jalview.util.*;
\r
26 public class AlignmentUtil {
\r
28 private AlignmentUtil() {
\r
31 public static int[][] percentIdentity2(AlignmentI align) {
\r
32 return percentIdentity2(align,0,align.getWidth()-1);
\r
35 public static int[][] percentIdentity2(AlignmentI align, int start, int end) {
\r
36 int [][] cons2 = new int[align.getWidth()][24];
\r
37 // Initialize the array
\r
38 for (int j=0;j<24;j++) {
\r
39 for (int i=0; i < align.getWidth();i++) {
\r
46 public static int getPixelHeight(int i, int j,int charHeight) {
\r
56 public static Vector substitution_rates (AlignmentI align, int start, int end) {
\r
58 Vector rates = new Vector();
\r
60 int len = (end-start+1);
\r
62 // Turn seqs into char arrays
\r
64 int[][] seqint = new int[align.getHeight()][len];
\r
67 for (int j = 0; j < align.getHeight(); j++) {
\r
69 SequenceI seq = align.getSequenceAt(j);
\r
71 for (int i = 0 ; i < len; i++) {
\r
72 char c = seq.getCharAt(start + i - 1);
\r
76 } else if (c == 'C') {
\r
78 } else if (c == 'T') {
\r
80 } else if (c == 'G') {
\r
91 // print_matrix(seqint,2,len); for (int j = 0; j < align.getHeight(); j++) {
\r
93 for (int j = 0; j < align.getHeight(); j++) {
\r
95 for (int k = 0; k < align.getHeight(); k++) {
\r
97 int counts[][] = new int[4][4];
\r
99 int tots[] = new int[4];
\r
101 int fulltots[] = new int[4];
\r
103 for (int i = 0 ; i < len; i++) {
\r
107 // System.out.println("Seq " + j + " " + k + " " + i + " " + seqint[j][i] + " " + seqint[k][i]);
\r
108 if (seqint[j][i] >= 0 &&
\r
109 seqint[k][i] >= 0) {
\r
110 counts[seqint[k][i]][seqint[j][i]]++;
\r
112 // print_matrix(counts,4,4);
\r
113 tots[seqint[j][i]]++;
\r
116 if (seqint[j][i] != -1) {
\r
117 fulltots[seqint[j][i]]++;
\r
125 System.out.println();
\r
127 System.out.println("Sequence " + align.getSequenceAt(j).getName() + " " + align.getSequenceAt(k).getName());
\r
129 System.out.println();
\r
130 print_matrix(counts,4,4);
\r
131 System.out.println();
\r
135 double[][] out = new double[4][4];// = constant_multiply_matrix(counts,1.0/tot,4,4);
\r
137 for (int i = 0; i < 4; i++) {
\r
138 for (int jj = 0; jj < 4; jj++) {
\r
139 out[i][jj] = (double)counts[i][jj]/tots[jj];
\r
143 print_matrix(out,4,4);
\r
144 System.out.println();
\r
147 System.out.print("RATES\t");
\r
148 System.out.print(align.getSequenceAt(j).getName() + "\t" + align.getSequenceAt(k).getName() + "\t");
\r
150 for (int i = 0; i < 4; i++) {
\r
151 for (int jj = 0; jj < 4; jj++) {
\r
152 Format.print(System.out,"%4.3f\t",out[i][jj]);
\r
155 System.out.println();
\r
157 for (int i = 0; i < 4; i++) {
\r
158 Format.print(System.out,"%4.3f\t",(double)fulltots[i]*1.0/fulltot);
\r
161 System.out.println();
\r
162 System.out.print("\nGC ");
\r
164 Format.print(System.out,"%4.3f\t",(double)(100*fulltots[1]+fulltots[3])/fulltot);
\r
166 System.out.print((fulltots[1]+fulltots[3]) + "\t" + fulltot);
\r
169 System.out.println();
\r
171 rates.addElement(out);
\r
179 public static double[][] constant_multiply_matrix(int[][] matrix, double c,int n, int m) {
\r
180 double[][] out = new double[n][m];
\r
182 for (int i = 0; i < n; i++) {
\r
183 for (int j = 0; j < m; j++) {
\r
185 out[i][j] = matrix[i][j]*c;
\r
192 public static void print_matrix(int[][] matrix, int n, int m) {
\r
195 for (int i = 0; i < n; i++) {
\r
196 for (int j = 0; j < m; j++) {
\r
198 System.out.print(matrix[i][j] + "\t");
\r
200 System.out.println();
\r
203 public static void print_matrix(double[][] matrix, int n, int m) {
\r
206 for (int i = 0; i < n; i++) {
\r
207 for (int j = 0; j < m; j++) {
\r
209 Format.print(System.out,"%4.3f\t",matrix[i][j]);
\r
212 System.out.println();
\r