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.schemes.*;
\r
23 import jalview.datamodel.SequenceI;
\r
24 import jalview.util.*;
\r
25 import jalview.io.*;
\r
31 public class AlignSeq {
\r
56 public int seq1start;
\r
58 public int seq2start;
\r
63 public int maxscore;
\r
67 public static java.util.Hashtable dnaHash = new java.util.Hashtable();
\r
70 dnaHash.put("C", new Integer(0));
\r
71 dnaHash.put("T", new Integer(1));
\r
72 dnaHash.put("A", new Integer(2));
\r
73 dnaHash.put("G", new Integer(3));
\r
74 dnaHash.put("-", new Integer(4));
\r
77 static String dna[] = {"C","T","A","G","-"};
\r
78 static String pep[] = {"A","R","N","D","C","Q","E","G","H","I","L","K","M","F","P","S","T","W","Y","V","B","Z","X","-"};
\r
83 int lookup[][] = ResidueProperties.getBLOSUM62();
\r
84 String intToStr[] = pep;
\r
91 public AlignSeq() {}
\r
93 public AlignSeq(SequenceI s1, SequenceI s2,String type) {
\r
94 rt = Runtime.getRuntime();
\r
95 SeqInit(s1,s2,type);
\r
98 public int getMaxScore() {
\r
102 public int getSeq2Start() {
\r
106 public int getSeq2End() {
\r
110 public int getSeq1Start() {
\r
114 public int getSeq1End() {
\r
118 public String getOutput() {
\r
122 public String getAStr1() {
\r
125 public String getAStr2() {
\r
128 public int [] getASeq1() {
\r
131 public int [] getASeq2() {
\r
134 public SequenceI getS1() {
\r
137 public SequenceI getS2() {
\r
141 public void SeqInit(SequenceI s1, SequenceI s2,String type) {
\r
142 s1str = extractGaps(".",s1.getSequence());
\r
143 s2str = extractGaps(".",s2.getSequence());
\r
144 s1str = extractGaps("-",s1str);
\r
145 s2str = extractGaps("-",s2str);
\r
146 s1str = extractGaps(" ",s1str);
\r
147 s2str = extractGaps(" ",s2str);
\r
154 if (type.equals("pep")) {
\r
155 lookup = ResidueProperties.getBLOSUM62();
\r
158 } else if (type.equals("dna")) {
\r
159 lookup = ResidueProperties.getDNA();
\r
163 output = output + ("Wrong type = dna or pep only");
\r
168 //System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
\r
169 seq1 = new int[s1str.length()];
\r
170 //System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
\r
171 seq2 = new int[s2str.length()];
\r
172 //System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
\r
173 score = new int[s1str.length()][s2str.length()];
\r
174 //System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
\r
175 E = new int[s1str.length()][s2str.length()];
\r
176 //System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
\r
177 F = new int[s1str.length()][s2str.length()];
\r
178 traceback = new int[s1str.length()][s2str.length()];
\r
179 //System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
\r
180 seq1 = stringToInt(s1str,type);
\r
181 //System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
\r
182 seq2 = stringToInt(s2str,type);
\r
183 //System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
\r
185 // long tstart = System.currentTimeMillis();
\r
186 // calcScoreMatrix();
\r
187 //long tend = System.currentTimeMillis();
\r
189 //System.out.println("Time take to calculate score matrix = " + (tend-tstart) + " ms");
\r
192 // printScoreMatrix(score);
\r
193 //System.out.println();
\r
195 //printScoreMatrix(traceback);
\r
196 //System.out.println();
\r
198 // printScoreMatrix(E);
\r
199 //System.out.println();
\r
201 ///printScoreMatrix(F);
\r
202 //System.out.println();
\r
203 // tstart = System.currentTimeMillis();
\r
204 //traceAlignment();
\r
205 //tend = System.currentTimeMillis();
\r
206 //System.out.println("Time take to traceback alignment = " + (tend-tstart) + " ms");
\r
209 public void traceAlignment() {
\r
211 // Find the maximum score along the rhs or bottom row
\r
213 for (int i = 0; i < seq1.length; i++) {
\r
214 if (score[i][seq2.length - 1] > max ) {
\r
215 max = score[i][seq2.length - 1];
\r
217 maxj = seq2.length-1;
\r
220 for (int j = 0; j < seq2.length; j++) {
\r
221 if (score[seq1.length - 1][j] > max ) {
\r
222 max = score[seq1.length - 1][j];
\r
223 maxi = seq1.length-1;
\r
228 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
\r
233 maxscore = score[i][j] / 10;
\r
238 aseq1 = new int[seq1.length + seq2.length];
\r
239 aseq2 = new int[seq1.length + seq2.length];
\r
241 count = seq1.length + seq2.length - 1;
\r
243 while (i>0 && j >0) {
\r
245 if (aseq1[count] != defInt && i >=0) {
\r
246 aseq1[count] = seq1[i];
\r
247 astr1 = intToStr[seq1[i]] + astr1;
\r
250 if (aseq2[count] != defInt && j > 0) {
\r
251 aseq2[count] = seq2[j];
\r
252 astr2 = intToStr[seq2[j]] + astr2;
\r
254 trace = findTrace(i,j);
\r
259 } else if (trace == 1) {
\r
261 aseq1[count] = defInt;
\r
262 astr1 = "-" + astr1.substring(1);
\r
263 } else if (trace == -1) {
\r
265 aseq2[count] = defInt;
\r
266 astr2 = "-" + astr2.substring(1);
\r
274 if (aseq1[count] != defInt) {
\r
275 aseq1[count] = seq1[i];
\r
276 astr1 = intToStr[seq1[i]] + astr1;
\r
279 if (aseq2[count] != defInt) {
\r
280 aseq2[count] = seq2[j];
\r
281 astr2 = intToStr[seq2[j]] + astr2;
\r
285 public void printAlignment() {
\r
286 // Find the biggest id length for formatting purposes
\r
287 int maxid = s1.getName().length();
\r
289 if (s2.getName().length() > maxid) {
\r
290 maxid = s2.getName().length();
\r
293 int len = 72 - maxid - 1;
\r
294 int nochunks = ((aseq1.length - count) / len) + 1;
\r
298 output = output + ("Score = " + score[maxi][maxj] + "\n");
\r
299 output = output + ("Length of alignment = " + (aseq1.length-count) + "\n");
\r
300 output = output + ("Sequence ");
\r
301 output = output + (new Format("%" + maxid + "s").form(s1.getName()));
\r
302 output = output + (" : " + seq1start + " - " + seq1end + " (Sequence length = " + s1str.length() + ")\n");
\r
303 output = output + ("Sequence ");
\r
304 output = output + (new Format("%" + maxid + "s").form(s2.getName()));
\r
305 output = output + (" : " + seq2start + " - " + seq2end + " (Sequence length = " + s2str.length() + ")\n\n");
\r
307 for (int j = 0; j < nochunks; j++) {
\r
308 // Print the first aligned sequence
\r
309 output = output + (new Format("%" + (maxid) + "s").form(s1.getName()) + " ");
\r
310 for (int i = 0; i < len ; i++) {
\r
312 if ((count + i + j*len) < aseq1.length) {
\r
313 output = output + (new Format("%s").form(intToStr[aseq1[count + i + j*len]]));
\r
317 output = output + ("\n");
\r
318 output = output + (new Format("%" + (maxid) + "s").form(" ") + " ");
\r
319 // Print out the matching chars
\r
320 for (int i = 0; i < len ; i++) {
\r
322 if ((count + i + j*len) < aseq1.length) {
\r
323 if ( intToStr[aseq1[count+i+j*len]].equals(intToStr[aseq2[count+i+j*len]]) && !intToStr[aseq1[count+i+j*len]].equals("-")) {
\r
325 output = output + ("|");
\r
326 } else if (type.equals("pep")) {
\r
327 if (ResidueProperties.getPAM250(intToStr[aseq1[count+i+j*len]],intToStr[aseq2[count+i+j*len]]) > 0) {
\r
328 output = output + (".");
\r
330 output = output + (" ");
\r
333 output = output + (" ");
\r
338 // Now print the second aligned sequence
\r
339 output = output + ("\n");
\r
340 output = output + (new Format("%" + (maxid) + "s").form(s2.getName()) + " " );
\r
341 for (int i = 0; i < len ; i++) {
\r
342 if ((count + i + j*len) < aseq1.length) {
\r
343 output = output + (new Format("%s").form(intToStr[aseq2[count + i + j*len]]));
\r
346 output = output + ("\n\n");
\r
348 pid = pid/(float)(aseq1.length-count)*100;
\r
349 output = output + (new Format("Percentage ID = %2.2f\n\n").form(pid));
\r
353 public void printScoreMatrix(int[][] mat) {
\r
354 int n = seq1.length;
\r
355 int m = seq2.length;
\r
357 for (int i = 0; i < n;i++) {
\r
358 // Print the top sequence
\r
360 Format.print(System.out,"%8s",s2str.substring(0,1));
\r
361 for (int jj = 1;jj < m; jj++) {
\r
362 Format.print(System.out,"%5s",s2str.substring(jj,jj+1));
\r
364 System.out.println();
\r
367 for (int j = 0;j < m; j++) {
\r
369 Format.print(System.out,"%3s",s1str.substring(i,i+1));
\r
371 Format.print(System.out,"%3d ",mat[i][j]/10);
\r
373 System.out.println();
\r
377 public int findTrace(int i,int j) {
\r
379 int max = score[i-1][j-1] + lookup[seq1[i]][seq2[j]] * 10;
\r
381 if (F[i][j] > max) {
\r
384 } else if (F[i][j] == max) {
\r
390 if (E[i][j] >= max) {
\r
393 } else if (E[i][j] == max) {
\r
403 public void calcScoreMatrix() {
\r
406 int n = seq1.length;
\r
407 int m = seq2.length;
\r
410 // top left hand element
\r
411 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
\r
412 E[0][0] = -gapExtend;
\r
415 // Calculate the top row first
\r
416 for (int j=1; j < m; j++) {
\r
417 // What should these values be? 0 maybe
\r
418 E[0][j] = max(score[0][j-1] - gapOpen,E[0][j-1] - gapExtend);
\r
419 F[0][j] = -gapExtend;
\r
421 score[0][j] = max( lookup[seq1[0]][seq2[j]] * 10 ,-gapOpen,-gapExtend);
\r
423 traceback[0][j] = 1;
\r
426 // Now do the left hand column
\r
427 for (int i=1; i < n; i++) {
\r
428 E[i][0] = -gapOpen;
\r
429 F[i][0] = max(score[i-1][0]-gapOpen,F[i-1][0]-gapExtend);
\r
431 score[i][0] = max( lookup[seq1[i]][seq2[0]] * 10 ,E[i][0],F[i][0]);
\r
432 traceback[i][0] = -1;
\r
435 // Now do all the other rows
\r
436 for (int i = 1; i < n; i++) {
\r
437 for (int j = 1; j < m; j++) {
\r
439 E[i][j] = max(score[i][j-1] - gapOpen, E[i][j-1] - gapExtend);
\r
440 F[i][j] = max(score[i-1][j] - gapOpen, F[i-1][j] - gapExtend);
\r
442 score[i][j] = max(score[i-1][j-1] + lookup[seq1[i]][seq2[j]]*10,
\r
445 traceback[i][j] = findTrace(i,j);
\r
450 public static String extractChars(String chars, String seq) {
\r
452 for (int i=0; i < chars.length(); i++) {
\r
453 String gap = chars.substring(i,i+1);
\r
454 out = extractGaps(gap,out);
\r
458 public static String extractGaps(String gapChar, String seq) {
\r
459 StringTokenizer str = new StringTokenizer(seq,gapChar);
\r
460 String newString = "";
\r
462 while (str.hasMoreTokens()) {
\r
463 newString = newString + str.nextToken();
\r
469 public int max(int i1, int i2, int i3) {
\r
480 public int max(int i1, int i2) {
\r
488 public int[] stringToInt(String s,String type) {
\r
489 int[] seq1 = new int[s.length()];
\r
491 for (int i = 0;i < s.length(); i++) {
\r
492 String ss = s.substring(i,i+1).toUpperCase();
\r
494 if (type.equals("pep")) {
\r
495 seq1[i] = ((Integer)ResidueProperties.aaHash.get(ss)).intValue();
\r
496 } else if (type.equals("dna")) {
\r
497 seq1[i] = ((Integer)dnaHash.get(ss)).intValue();
\r
499 if (seq1[i] > 23) {
\r
502 } catch (Exception e) {
\r
503 if (type.equals("dna")) {
\r
513 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,int psize) {
\r
518 for (int i=0; i < n; i++) {
\r
519 for (int j=0; j < m; j++) {
\r
520 if (mat[i][j] >= max) {
\r
523 if (mat[i][j] <= min) {
\r
528 System.out.println(max + " " + min);
\r
529 for (int i=0; i < n; i++) {
\r
530 for (int j=0; j < m; j++) {
\r
533 // System.out.println(mat[i][j]);
\r
534 float score = (float)(mat[i][j] - min)/(float)(max-min);
\r
535 g.setColor(new Color(score,0,0));
\r
536 g.fillRect(x,y,psize,psize);
\r
537 // System.out.println(x + " " + y + " " + score);
\r