1 /* Jalview - a java multiple alignment editor
\r
2 * Copyright (C) 1998 Michele Clamp
\r
4 * This program is free software; you can redistribute it and/or
\r
5 * modify it under the terms of the GNU General Public License
\r
6 * as published by the Free Software Foundation; either version 2
\r
7 * of the License, or (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License
\r
15 * along with this program; if not, write to the Free Software
\r
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
18 package jalview.analysis;
\r
20 import jalview.schemes.*;
\r
21 import jalview.datamodel.SequenceI;
\r
22 import jalview.util.*;
\r
23 import jalview.io.*;
\r
29 public class AlignSeq {
\r
54 public int seq1start;
\r
56 public int seq2start;
\r
61 public int maxscore;
\r
65 public static java.util.Hashtable dnaHash = new java.util.Hashtable();
\r
68 dnaHash.put("C", new Integer(0));
\r
69 dnaHash.put("T", new Integer(1));
\r
70 dnaHash.put("A", new Integer(2));
\r
71 dnaHash.put("G", new Integer(3));
\r
72 dnaHash.put("-", new Integer(4));
\r
75 static String dna[] = {"C","T","A","G","-"};
\r
76 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
81 int lookup[][] = ResidueProperties.getBLOSUM62();
\r
82 String intToStr[] = pep;
\r
89 public AlignSeq() {}
\r
91 public AlignSeq(SequenceI s1, SequenceI s2,String type) {
\r
92 rt = Runtime.getRuntime();
\r
93 SeqInit(s1,s2,type);
\r
96 public int getMaxScore() {
\r
100 public int getSeq2Start() {
\r
104 public int getSeq2End() {
\r
108 public int getSeq1Start() {
\r
112 public int getSeq1End() {
\r
116 public String getOutput() {
\r
120 public String getAStr1() {
\r
123 public String getAStr2() {
\r
126 public int [] getASeq1() {
\r
129 public int [] getASeq2() {
\r
132 public SequenceI getS1() {
\r
135 public SequenceI getS2() {
\r
139 public void SeqInit(SequenceI s1, SequenceI s2,String type) {
\r
140 s1str = extractGaps(".",s1.getSequence());
\r
141 s2str = extractGaps(".",s2.getSequence());
\r
142 s1str = extractGaps("-",s1str);
\r
143 s2str = extractGaps("-",s2str);
\r
144 s1str = extractGaps(" ",s1str);
\r
145 s2str = extractGaps(" ",s2str);
\r
152 if (type.equals("pep")) {
\r
153 lookup = ResidueProperties.getBLOSUM62();
\r
156 } else if (type.equals("dna")) {
\r
157 lookup = ResidueProperties.getDNA();
\r
161 output = output + ("Wrong type = dna or pep only");
\r
166 //System.out.println("lookuip " + rt.freeMemory() + " "+ rt.totalMemory());
\r
167 seq1 = new int[s1str.length()];
\r
168 //System.out.println("seq1 " + rt.freeMemory() +" " + rt.totalMemory());
\r
169 seq2 = new int[s2str.length()];
\r
170 //System.out.println("seq2 " + rt.freeMemory() + " " + rt.totalMemory());
\r
171 score = new int[s1str.length()][s2str.length()];
\r
172 //System.out.println("score " + rt.freeMemory() + " " + rt.totalMemory());
\r
173 E = new int[s1str.length()][s2str.length()];
\r
174 //System.out.println("E " + rt.freeMemory() + " " + rt.totalMemory());
\r
175 F = new int[s1str.length()][s2str.length()];
\r
176 traceback = new int[s1str.length()][s2str.length()];
\r
177 //System.out.println("F " + rt.freeMemory() + " " + rt.totalMemory());
\r
178 seq1 = stringToInt(s1str,type);
\r
179 //System.out.println("seq1 " + rt.freeMemory() + " " + rt.totalMemory());
\r
180 seq2 = stringToInt(s2str,type);
\r
181 //System.out.println("Seq2 " + rt.freeMemory() + " " + rt.totalMemory());
\r
183 // long tstart = System.currentTimeMillis();
\r
184 // calcScoreMatrix();
\r
185 //long tend = System.currentTimeMillis();
\r
187 //System.out.println("Time take to calculate score matrix = " + (tend-tstart) + " ms");
\r
190 // printScoreMatrix(score);
\r
191 //System.out.println();
\r
193 //printScoreMatrix(traceback);
\r
194 //System.out.println();
\r
196 // printScoreMatrix(E);
\r
197 //System.out.println();
\r
199 ///printScoreMatrix(F);
\r
200 //System.out.println();
\r
201 // tstart = System.currentTimeMillis();
\r
202 //traceAlignment();
\r
203 //tend = System.currentTimeMillis();
\r
204 //System.out.println("Time take to traceback alignment = " + (tend-tstart) + " ms");
\r
207 public void traceAlignment() {
\r
209 // Find the maximum score along the rhs or bottom row
\r
211 for (int i = 0; i < seq1.length; i++) {
\r
212 if (score[i][seq2.length - 1] > max ) {
\r
213 max = score[i][seq2.length - 1];
\r
215 maxj = seq2.length-1;
\r
218 for (int j = 0; j < seq2.length; j++) {
\r
219 if (score[seq1.length - 1][j] > max ) {
\r
220 max = score[seq1.length - 1][j];
\r
221 maxi = seq1.length-1;
\r
226 // System.out.println(maxi + " " + maxj + " " + score[maxi][maxj]);
\r
231 maxscore = score[i][j] / 10;
\r
236 aseq1 = new int[seq1.length + seq2.length];
\r
237 aseq2 = new int[seq1.length + seq2.length];
\r
239 count = seq1.length + seq2.length - 1;
\r
241 while (i>0 && j >0) {
\r
243 if (aseq1[count] != defInt && i >=0) {
\r
244 aseq1[count] = seq1[i];
\r
245 astr1 = intToStr[seq1[i]] + astr1;
\r
248 if (aseq2[count] != defInt && j > 0) {
\r
249 aseq2[count] = seq2[j];
\r
250 astr2 = intToStr[seq2[j]] + astr2;
\r
252 trace = findTrace(i,j);
\r
257 } else if (trace == 1) {
\r
259 aseq1[count] = defInt;
\r
260 astr1 = "-" + astr1.substring(1);
\r
261 } else if (trace == -1) {
\r
263 aseq2[count] = defInt;
\r
264 astr2 = "-" + astr2.substring(1);
\r
272 if (aseq1[count] != defInt) {
\r
273 aseq1[count] = seq1[i];
\r
274 astr1 = intToStr[seq1[i]] + astr1;
\r
277 if (aseq2[count] != defInt) {
\r
278 aseq2[count] = seq2[j];
\r
279 astr2 = intToStr[seq2[j]] + astr2;
\r
283 public void printAlignment() {
\r
284 // Find the biggest id length for formatting purposes
\r
285 int maxid = s1.getName().length();
\r
287 if (s2.getName().length() > maxid) {
\r
288 maxid = s2.getName().length();
\r
291 int len = 72 - maxid - 1;
\r
292 int nochunks = ((aseq1.length - count) / len) + 1;
\r
296 output = output + ("Score = " + score[maxi][maxj] + "\n");
\r
297 output = output + ("Length of alignment = " + (aseq1.length-count) + "\n");
\r
298 output = output + ("Sequence ");
\r
299 output = output + (new Format("%" + maxid + "s").form(s1.getName()));
\r
300 output = output + (" : " + seq1start + " - " + seq1end + " (Sequence length = " + s1str.length() + ")\n");
\r
301 output = output + ("Sequence ");
\r
302 output = output + (new Format("%" + maxid + "s").form(s2.getName()));
\r
303 output = output + (" : " + seq2start + " - " + seq2end + " (Sequence length = " + s2str.length() + ")\n\n");
\r
305 for (int j = 0; j < nochunks; j++) {
\r
306 // Print the first aligned sequence
\r
307 output = output + (new Format("%" + (maxid) + "s").form(s1.getName()) + " ");
\r
308 for (int i = 0; i < len ; i++) {
\r
310 if ((count + i + j*len) < aseq1.length) {
\r
311 output = output + (new Format("%s").form(intToStr[aseq1[count + i + j*len]]));
\r
315 output = output + ("\n");
\r
316 output = output + (new Format("%" + (maxid) + "s").form(" ") + " ");
\r
317 // Print out the matching chars
\r
318 for (int i = 0; i < len ; i++) {
\r
320 if ((count + i + j*len) < aseq1.length) {
\r
321 if ( intToStr[aseq1[count+i+j*len]].equals(intToStr[aseq2[count+i+j*len]]) && !intToStr[aseq1[count+i+j*len]].equals("-")) {
\r
323 output = output + ("|");
\r
324 } else if (type.equals("pep")) {
\r
325 if (ResidueProperties.getPAM250(intToStr[aseq1[count+i+j*len]],intToStr[aseq2[count+i+j*len]]) > 0) {
\r
326 output = output + (".");
\r
328 output = output + (" ");
\r
331 output = output + (" ");
\r
336 // Now print the second aligned sequence
\r
337 output = output + ("\n");
\r
338 output = output + (new Format("%" + (maxid) + "s").form(s2.getName()) + " " );
\r
339 for (int i = 0; i < len ; i++) {
\r
340 if ((count + i + j*len) < aseq1.length) {
\r
341 output = output + (new Format("%s").form(intToStr[aseq2[count + i + j*len]]));
\r
344 output = output + ("\n\n");
\r
346 pid = pid/(float)(aseq1.length-count)*100;
\r
347 output = output + (new Format("Percentage ID = %2.2f\n\n").form(pid));
\r
351 public void printScoreMatrix(int[][] mat) {
\r
352 int n = seq1.length;
\r
353 int m = seq2.length;
\r
355 for (int i = 0; i < n;i++) {
\r
356 // Print the top sequence
\r
358 Format.print(System.out,"%8s",s2str.substring(0,1));
\r
359 for (int jj = 1;jj < m; jj++) {
\r
360 Format.print(System.out,"%5s",s2str.substring(jj,jj+1));
\r
362 System.out.println();
\r
365 for (int j = 0;j < m; j++) {
\r
367 Format.print(System.out,"%3s",s1str.substring(i,i+1));
\r
369 Format.print(System.out,"%3d ",mat[i][j]/10);
\r
371 System.out.println();
\r
375 public int findTrace(int i,int j) {
\r
377 int max = score[i-1][j-1] + lookup[seq1[i]][seq2[j]] * 10;
\r
379 if (F[i][j] > max) {
\r
382 } else if (F[i][j] == max) {
\r
388 if (E[i][j] >= max) {
\r
391 } else if (E[i][j] == max) {
\r
401 public void calcScoreMatrix() {
\r
404 int n = seq1.length;
\r
405 int m = seq2.length;
\r
408 // top left hand element
\r
409 score[0][0] = lookup[seq1[0]][seq2[0]] * 10;
\r
410 E[0][0] = -gapExtend;
\r
413 // Calculate the top row first
\r
414 for (int j=1; j < m; j++) {
\r
415 // What should these values be? 0 maybe
\r
416 E[0][j] = max(score[0][j-1] - gapOpen,E[0][j-1] - gapExtend);
\r
417 F[0][j] = -gapExtend;
\r
419 score[0][j] = max( lookup[seq1[0]][seq2[j]] * 10 ,-gapOpen,-gapExtend);
\r
421 traceback[0][j] = 1;
\r
424 // Now do the left hand column
\r
425 for (int i=1; i < n; i++) {
\r
426 E[i][0] = -gapOpen;
\r
427 F[i][0] = max(score[i-1][0]-gapOpen,F[i-1][0]-gapExtend);
\r
429 score[i][0] = max( lookup[seq1[i]][seq2[0]] * 10 ,E[i][0],F[i][0]);
\r
430 traceback[i][0] = -1;
\r
433 // Now do all the other rows
\r
434 for (int i = 1; i < n; i++) {
\r
435 for (int j = 1; j < m; j++) {
\r
437 E[i][j] = max(score[i][j-1] - gapOpen, E[i][j-1] - gapExtend);
\r
438 F[i][j] = max(score[i-1][j] - gapOpen, F[i-1][j] - gapExtend);
\r
440 score[i][j] = max(score[i-1][j-1] + lookup[seq1[i]][seq2[j]]*10,
\r
443 traceback[i][j] = findTrace(i,j);
\r
448 public static String extractChars(String chars, String seq) {
\r
450 for (int i=0; i < chars.length(); i++) {
\r
451 String gap = chars.substring(i,i+1);
\r
452 out = extractGaps(gap,out);
\r
456 public static String extractGaps(String gapChar, String seq) {
\r
457 StringTokenizer str = new StringTokenizer(seq,gapChar);
\r
458 String newString = "";
\r
460 while (str.hasMoreTokens()) {
\r
461 newString = newString + str.nextToken();
\r
467 public int max(int i1, int i2, int i3) {
\r
478 public int max(int i1, int i2) {
\r
486 public int[] stringToInt(String s,String type) {
\r
487 int[] seq1 = new int[s.length()];
\r
489 for (int i = 0;i < s.length(); i++) {
\r
490 String ss = s.substring(i,i+1).toUpperCase();
\r
492 if (type.equals("pep")) {
\r
493 seq1[i] = ((Integer)ResidueProperties.aaHash.get(ss)).intValue();
\r
494 } else if (type.equals("dna")) {
\r
495 seq1[i] = ((Integer)dnaHash.get(ss)).intValue();
\r
497 if (seq1[i] > 23) {
\r
500 } catch (Exception e) {
\r
501 if (type.equals("dna")) {
\r
511 public static void displayMatrix(Graphics g, int[][] mat, int n, int m,int psize) {
\r
516 for (int i=0; i < n; i++) {
\r
517 for (int j=0; j < m; j++) {
\r
518 if (mat[i][j] >= max) {
\r
521 if (mat[i][j] <= min) {
\r
526 System.out.println(max + " " + min);
\r
527 for (int i=0; i < n; i++) {
\r
528 for (int j=0; j < m; j++) {
\r
531 // System.out.println(mat[i][j]);
\r
532 float score = (float)(mat[i][j] - min)/(float)(max-min);
\r
533 g.setColor(new Color(score,0,0));
\r
534 g.fillRect(x,y,psize,psize);
\r
535 // System.out.println(x + " " + y + " " + score);
\r