X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FAlignmentUtil.java;h=a2e1c71b167baba40e6b3706d291f2e9093e983a;hb=1627d5f04287be852b799ba51bf2b6ded9acecda;hp=21928663596b1b5aaab19f49afaeb811c8d07cb0;hpb=6f62124bf8277dd898f43f75e81bbde027fb2ebc;p=jalview.git diff --git a/src/jalview/analysis/AlignmentUtil.java b/src/jalview/analysis/AlignmentUtil.java index 2192866..a2e1c71 100755 --- a/src/jalview/analysis/AlignmentUtil.java +++ b/src/jalview/analysis/AlignmentUtil.java @@ -1,198 +1,205 @@ +/* +* Jalview - A Sequence Alignment Editor and Viewer +* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +*/ package jalview.analysis; import jalview.datamodel.*; -import jalview.util.*; -import java.util.*; -public class AlignmentUtil { +import jalview.util.*; - private AlignmentUtil() { - } +import java.util.*; - public static int[][] percentIdentity2(AlignmentI align) { - return percentIdentity2(align,0,align.getWidth()-1); - } - public static int[][] percentIdentity2(AlignmentI align, int start, int end) { - int [][] cons2 = new int[align.getWidth()][24]; - // Initialize the array - for (int j=0;j<24;j++) { - for (int i=0; i < align.getWidth();i++) { - cons2[i][j] = 0; - } +public class AlignmentUtil { + private AlignmentUtil() { } - return cons2; - } - public static int getPixelHeight(int i, int j,int charHeight) { - int h=0; - while (i < j) { - h += charHeight; - i++; + public static int[][] percentIdentity2(AlignmentI align) { + return percentIdentity2(align, 0, align.getWidth() - 1); } - return h; - } - - - public static Vector substitution_rates (AlignmentI align, int start, int end) { - - Vector rates = new Vector(); - - int len = (end-start+1); - - // Turn seqs into char arrays - - int[][] seqint = new int[align.getHeight()][len]; - - - for (int j = 0; j < align.getHeight(); j++) { - - SequenceI seq = align.getSequenceAt(j); - - for (int i = 0 ; i < len; i++) { - char c = seq.getCharAt(start + i - 1); - - if (c == 'A') { - seqint[j][i] = 0; - } else if (c == 'C') { - seqint[j][i] = 1; - } else if (c == 'T') { - seqint[j][i] = 2; - } else if (c == 'G') { - seqint[j][i] = 3; - } else { - seqint[j][i] = -1; - } - - } - - } - - - // print_matrix(seqint,2,len); for (int j = 0; j < align.getHeight(); j++) { - - for (int j = 0; j < align.getHeight(); j++) { - - for (int k = 0; k < align.getHeight(); k++) { - - int counts[][] = new int[4][4]; - int tot = 0; - int tots[] = new int[4]; - int fulltot = 0; - int fulltots[] = new int[4]; - - for (int i = 0 ; i < len; i++) { - - if (k != j) { - // System.out.println("Seq " + j + " " + k + " " + i + " " + seqint[j][i] + " " + seqint[k][i]); - if (seqint[j][i] >= 0 && - seqint[k][i] >= 0) { - counts[seqint[k][i]][seqint[j][i]]++; + public static int[][] percentIdentity2(AlignmentI align, int start, int end) { + int[][] cons2 = new int[align.getWidth()][24]; - // print_matrix(counts,4,4); - tots[seqint[j][i]]++; - tot++; - } - if (seqint[j][i] != -1) { - fulltots[seqint[j][i]]++; - fulltot++; - } - } - } + // Initialize the array + for (int j = 0; j < 24; j++) { + for (int i = 0; i < align.getWidth(); i++) { + cons2[i][j] = 0; + } + } - if (k != j) { - - System.out.println(); - - System.out.println("Sequence " + align.getSequenceAt(j).getName() + " " + align.getSequenceAt(k).getName()); - - System.out.println(); - print_matrix(counts,4,4); - System.out.println(); - - - - double[][] out = new double[4][4];// = constant_multiply_matrix(counts,1.0/tot,4,4); - - for (int i = 0; i < 4; i++) { - for (int jj = 0; jj < 4; jj++) { - out[i][jj] = (double)counts[i][jj]/tots[jj]; - } - } - - print_matrix(out,4,4); - System.out.println(); - - - System.out.print("RATES\t"); - System.out.print(align.getSequenceAt(j).getName() + "\t" + align.getSequenceAt(k).getName() + "\t"); - - for (int i = 0; i < 4; i++) { - for (int jj = 0; jj < 4; jj++) { - Format.print(System.out,"%4.3f\t",out[i][jj]); - } - } - System.out.println(); - - for (int i = 0; i < 4; i++) { - Format.print(System.out,"%4.3f\t",(double)fulltots[i]*1.0/fulltot); - } - - System.out.println(); - System.out.print("\nGC "); - - Format.print(System.out,"%4.3f\t",(double)(100*fulltots[1]+fulltots[3])/fulltot); - - System.out.print((fulltots[1]+fulltots[3]) + "\t" + fulltot); + return cons2; + } + public static int getPixelHeight(int i, int j, int charHeight) { + int h = 0; - System.out.println(); + while (i < j) { + h += charHeight; + i++; + } - rates.addElement(out); - } + return h; + } - } - } - return rates; + public static Vector substitution_rates(AlignmentI align, int start, int end) { + Vector rates = new Vector(); + + int len = (end - start + 1); + + // Turn seqs into char arrays + int[][] seqint = new int[align.getHeight()][len]; + + for (int j = 0; j < align.getHeight(); j++) { + SequenceI seq = align.getSequenceAt(j); + + for (int i = 0; i < len; i++) { + char c = seq.getCharAt((start + i) - 1); + + if (c == 'A') { + seqint[j][i] = 0; + } else if (c == 'C') { + seqint[j][i] = 1; + } else if (c == 'T') { + seqint[j][i] = 2; + } else if (c == 'G') { + seqint[j][i] = 3; + } else { + seqint[j][i] = -1; + } + } + } + + // print_matrix(seqint,2,len); for (int j = 0; j < align.getHeight(); j++) { + for (int j = 0; j < align.getHeight(); j++) { + for (int k = 0; k < align.getHeight(); k++) { + int[][] counts = new int[4][4]; + int tot = 0; + int[] tots = new int[4]; + int fulltot = 0; + int[] fulltots = new int[4]; + + for (int i = 0; i < len; i++) { + if (k != j) { + // System.out.println("Seq " + j + " " + k + " " + i + " " + seqint[j][i] + " " + seqint[k][i]); + if ((seqint[j][i] >= 0) && (seqint[k][i] >= 0)) { + counts[seqint[k][i]][seqint[j][i]]++; + + // print_matrix(counts,4,4); + tots[seqint[j][i]]++; + tot++; + } + + if (seqint[j][i] != -1) { + fulltots[seqint[j][i]]++; + fulltot++; + } + } + } + + if (k != j) { + System.out.println(); + + System.out.println("Sequence " + + align.getSequenceAt(j).getName() + " " + + align.getSequenceAt(k).getName()); + + System.out.println(); + print_matrix(counts, 4, 4); + System.out.println(); + + double[][] out = new double[4][4]; // = constant_multiply_matrix(counts,1.0/tot,4,4); + + for (int i = 0; i < 4; i++) { + for (int jj = 0; jj < 4; jj++) { + out[i][jj] = (double) counts[i][jj] / tots[jj]; + } + } + + print_matrix(out, 4, 4); + System.out.println(); + + System.out.print("RATES\t"); + System.out.print(align.getSequenceAt(j).getName() + "\t" + + align.getSequenceAt(k).getName() + "\t"); + + for (int i = 0; i < 4; i++) { + for (int jj = 0; jj < 4; jj++) { + Format.print(System.out, "%4.3f\t", out[i][jj]); + } + } + + System.out.println(); + + for (int i = 0; i < 4; i++) { + Format.print(System.out, "%4.3f\t", + ((double) fulltots[i] * 1.0) / fulltot); + } + + System.out.println(); + System.out.print("\nGC "); + + Format.print(System.out, "%4.3f\t", + (double) ((100 * fulltots[1]) + fulltots[3]) / fulltot); + + System.out.print((fulltots[1] + fulltots[3]) + "\t" + + fulltot); + + System.out.println(); + + rates.addElement(out); + } + } + } + + return rates; } - public static double[][] constant_multiply_matrix(int[][] matrix, double c,int n, int m) { - double[][] out = new double[n][m]; + public static double[][] constant_multiply_matrix(int[][] matrix, double c, + int n, int m) { + double[][] out = new double[n][m]; - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + out[i][j] = matrix[i][j] * c; + } + } - out[i][j] = matrix[i][j]*c; - } - } - return out; + return out; } - public static void print_matrix(int[][] matrix, int n, int m) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + System.out.print(matrix[i][j] + "\t"); + } - - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { - - System.out.print(matrix[i][j] + "\t"); - } - System.out.println(); - } + System.out.println(); + } } - public static void print_matrix(double[][] matrix, int n, int m) { - - - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { - Format.print(System.out,"%4.3f\t",matrix[i][j]); + public static void print_matrix(double[][] matrix, int n, int m) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + Format.print(System.out, "%4.3f\t", matrix[i][j]); + } - } - System.out.println(); - } + System.out.println(); + } } - - }