1 /*************************************************************** 2 * 3 * This program is free software; you can redistribute it and/or 4 * modify it under the terms of the GNU General Public License 5 * as published by the Free Software Foundation; either version 2 6 * of the License, or (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program; if not, write to the Free Software 15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16 * 17 * @author: Copyright (C) Tim Carver 18 * 19 ***************************************************************/ 20 21 package org.emboss.jemboss.editor; 22 23 import java.io.*; 24 import java.util.StringTokenizer; 25 import java.util.Hashtable; 26 import java.util.Arrays; 27 import javax.swing.JOptionPane; 28 29 import org.apache.regexp.*; 30 import org.emboss.jemboss.JembossJarUtil; 31 32 /** 33 * 34 * Reads and hold a sequence scoring matrix 35 * 36 */ 37 public class Matrix 38 { 39 private int matrix[][]; 40 private int idimension; 41 private int jdimension; 42 43 private int i=0; 44 private int k=0; 45 46 /** hashtable of the residue positions in the table */ 47 private Hashtable residueMatrixPosition; 48 private Object[] keys = null; 49 //private String cons = ""; 50 private String matrixString = null; 51 private String matrixFileName = null; 52 53 public static final String DEFAULT_MATRIX = "EBLOSUM62"; 54 private String matrixFilesLocation; 55 private int locationType; 56 private static int LOCATION_JAR_ARCHIVE = 1; 57 private static int LOCATION_DATA_DIR = 2; 58 59 /** 60 * 61 * @param matrixFile matrix file 62 * 63 */ Matrix(File matrixFile)64 public Matrix(File matrixFile) 65 { 66 this.matrixFileName = matrixFile.getName(); 67 matrixRead(matrixFile); 68 } 69 70 /** 71 * 72 * @param matrixJar jar file containing scoring matrix 73 * @param matrixFileName matrix file 74 * 75 */ Matrix(String matrixFilesLocation, String matrixFileName)76 public Matrix(String matrixFilesLocation, String matrixFileName) 77 { 78 this.matrixFileName = matrixFileName; 79 this.matrixFilesLocation = matrixFilesLocation; 80 File mfl = new File(matrixFilesLocation); 81 if (mfl.isDirectory()){ 82 keys = mfl.list(new FilenameFilter(){ 83 public boolean accept(File dir, String name) { 84 if (name.startsWith("EPAM") || 85 name.startsWith("EBLOSUM") || 86 name.startsWith("EDNA")) 87 return true; 88 return false; 89 }}); 90 File m = new File(matrixFilesLocation+"/"+matrixFileName); 91 try { 92 matrixString = readFileAsString(m); 93 matrixReadString(matrixString); 94 locationType = LOCATION_DATA_DIR; 95 Arrays.sort(keys); 96 return; 97 } catch (IOException e) { 98 } 99 } 100 String matrixJar = matrixFilesLocation; 101 try 102 { 103 Hashtable matrixHash = (new JembossJarUtil(matrixJar)).getHash(); 104 keys = matrixHash.keySet().toArray(); 105 Arrays.sort(keys); 106 if(matrixHash.containsKey(matrixFileName)) 107 { 108 matrixString = new String((byte[])matrixHash.get(matrixFileName)); 109 matrixReadString(matrixString); 110 locationType = LOCATION_JAR_ARCHIVE; 111 } 112 else 113 System.err.println("Matrix file "+matrixFileName+ 114 " not found in jar file "+matrixJar); 115 } 116 catch(Exception exp) 117 { 118 JOptionPane.showMessageDialog(null, 119 "Failed to read "+matrixFileName+ 120 "\nfrom the matrix archive "+matrixJar, 121 "Missing matrix archive", 122 JOptionPane.ERROR_MESSAGE); 123 } 124 } 125 126 changeMatrix(String matrixFileName)127 public void changeMatrix(String matrixFileName){ 128 i = 0; 129 k = 0; 130 if (locationType == LOCATION_DATA_DIR){ 131 File m = new File(matrixFilesLocation+"/"+matrixFileName); 132 try { 133 matrixString = readFileAsString(m); 134 matrixReadString(matrixString); 135 } catch (IOException e) { 136 } 137 138 this.matrixFileName = matrixFileName; 139 140 return; 141 } 142 String matrixJar = matrixFilesLocation; 143 try 144 { 145 //TODO: shouldn't read the matrixHash each time 146 Hashtable matrixHash = (new JembossJarUtil(matrixJar)).getHash(); 147 if(matrixHash.containsKey(matrixFileName)) 148 { 149 matrixString = new String((byte[])matrixHash.get(matrixFileName)); 150 matrixReadString(matrixString); 151 } 152 else 153 System.err.println("Matrix file "+matrixFileName+ 154 " not found in jar file "+matrixJar); 155 } 156 catch(Exception exp) 157 { 158 JOptionPane.showMessageDialog(null, 159 "Failed to read "+matrixFileName+ 160 "\nfrom the matrix archive "+matrixJar, 161 "Missing matrix archive", 162 JOptionPane.ERROR_MESSAGE); 163 } 164 165 this.matrixFileName = matrixFileName; 166 } 167 168 readFileAsString(File source)169 final String readFileAsString(File source) throws IOException 170 { 171 final DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(source))); 172 final byte[] buffer = new byte[(int)source.length()]; 173 dis.readFully(buffer); 174 dis.close(); 175 return new String(buffer); 176 } 177 178 /** 179 * 180 * Get the scoring matrix as a 2 dimensional integer 181 * array 182 * @return 2 dimentional scoring matrix 183 * 184 */ getMatrix()185 public int[][] getMatrix() 186 { 187 return matrix; 188 } 189 190 /** 191 * 192 * Get the scoring matrix as text 193 * @return scoring matrix 194 * 195 */ getMatrixTable()196 public String getMatrixTable() 197 { 198 return matrixString; 199 } 200 201 /** 202 * 203 * Get the current scoring matrix name 204 * @return name of matrix 205 * 206 */ getCurrentMatrixName()207 public String getCurrentMatrixName() 208 { 209 int index = matrixFileName.lastIndexOf("/"); 210 if(index > -1) 211 return matrixFileName.substring(index+1); 212 return matrixFileName; 213 } 214 215 /** 216 * 217 * Scoring matrix filenames in jar file 218 * 219 */ getKeys()220 public Object[] getKeys() 221 { 222 return keys; 223 } 224 225 /** 226 * 227 * Scoring matrix names available 228 * 229 */ getKeyNames()230 public Object[] getKeyNames() 231 { 232 try 233 { 234 int nkeys = keys.length; 235 Object[] kname = new Object[nkeys]; 236 for(int i=0;i<nkeys;i++) 237 { 238 String k = (String)keys[i]; 239 if(k.indexOf("MANIFEST.MF") == -1) 240 { 241 int pos = k.indexOf("/")+1; 242 kname[i] = k.substring(pos); 243 } 244 } 245 return kname; 246 } 247 catch(NullPointerException npe) 248 { 249 JOptionPane.showMessageDialog(null, 250 "No matrix files found!", 251 "Matrix files missing", 252 JOptionPane.ERROR_MESSAGE); 253 return null; 254 } 255 } 256 257 258 /** 259 * 260 * Scoring matrix names available 261 * 262 */ getKeyNamesString()263 public String getKeyNamesString() 264 { 265 try 266 { 267 int nkeys = keys.length; 268 StringBuffer kname = new StringBuffer(); 269 for(int i=0;i<nkeys;i++) 270 { 271 String k = (String)keys[i]; 272 if(k.indexOf("MANIFEST.MF") == -1) 273 { 274 int pos = k.indexOf("/")+1; 275 kname.append(k.substring(pos)+"\n"); 276 } 277 } 278 return kname.toString(); 279 } 280 catch(NullPointerException npe) 281 { 282 JOptionPane.showMessageDialog(null, 283 "No matrix files found!", 284 "Matrix files missing", 285 JOptionPane.ERROR_MESSAGE); 286 return null; 287 } 288 } 289 290 291 /** 292 * 293 * Get the hashtable of the residue positions in the scoring table 294 * @return hashtable of the residue positions 295 * 296 */ getResidueMatrixPosition()297 public Hashtable getResidueMatrixPosition() 298 { 299 return residueMatrixPosition; 300 } 301 302 /** 303 * 304 * Get the residue position in the scoring table 305 * @param s residue 306 * @return position in scoring table 307 * 308 */ getMatrixIndex(String s)309 public int getMatrixIndex(String s) 310 { 311 s = s.toUpperCase(); 312 313 if(residueMatrixPosition==null) 314 return -1; 315 316 if(!residueMatrixPosition.containsKey(s)) 317 if(s.equals(".") || s.equals("-") 318 || s.equals("~")) 319 s = "X"; 320 321 if(!residueMatrixPosition.containsKey(s)) 322 return -1; 323 return ((Integer)residueMatrixPosition.get(s)).intValue(); 324 } 325 326 /** 327 * 328 * Get number of rows in the scoring table 329 * @return number of rows in the scoring table 330 * 331 */ getIDimension()332 public int getIDimension() 333 { 334 return idimension; 335 } 336 337 /** 338 * 339 * Get number of columns in the scoring table 340 * @return number of columns in the scoring table 341 * 342 */ getJDimension()343 public int getJDimension() 344 { 345 return jdimension; 346 } 347 348 /** 349 * 350 * Get regular expression for values in the scoring table 351 * @return RE 352 * 353 */ getRegularExpression()354 private RE getRegularExpression() 355 { 356 RE regexp = null; 357 358 try 359 { 360 RECompiler rec = new RECompiler(); 361 REProgram rep = rec.compile("[:digit:]"); 362 regexp = new RE(rep); 363 } 364 catch (RESyntaxException rese) 365 { 366 System.out.println("RESyntaxException "); 367 } 368 return regexp; 369 } 370 371 /** 372 * 373 * Count as a matrix row and number of columns 374 * @param line line from matrix file 375 * @param regexp regular expression for columns 376 * 377 */ matrixLineCount(String line, RE regexp)378 private void matrixLineCount(String line, RE regexp) 379 { 380 String delim = " :\t\n"; 381 if(!line.startsWith("#") && !line.equals("") 382 && regexp.match(line)) 383 { 384 jdimension = 0; 385 idimension++; 386 line = line.trim(); 387 StringTokenizer st = new StringTokenizer(line,delim); 388 while (st.hasMoreTokens()) 389 { 390 st.nextToken(); 391 jdimension++; 392 } 393 } 394 } 395 396 /** 397 * 398 * Parse a line from a scoring matrix file. Add to the 399 * matrix array the values in the table. 400 * @param line line from matrix file 401 * @param regexp regular expression for values 402 * 403 */ matrixLineParse(String line, RE regexp)404 private void matrixLineParse(String line, RE regexp) 405 { 406 String delim = " :\t\n"; 407 int j = 0; 408 409 if(!line.startsWith("#") && !line.equals("")) 410 { 411 line = line.trim(); 412 StringTokenizer st = new StringTokenizer(line,delim); 413 if(!regexp.match(line)) 414 { 415 while (st.hasMoreTokens()) 416 { 417 residueMatrixPosition.put(st.nextToken(),new Integer(k)); 418 k++; 419 } 420 } 421 else 422 { 423 st.nextToken(); 424 while (st.hasMoreTokens()) 425 { 426 String s = st.nextToken(); 427 matrix[i][j] = Integer.parseInt(s); 428 j++; 429 } 430 i++; 431 } 432 } 433 } 434 435 /** 436 * 437 * Read and parse a scoring matrix file 438 * @param matrixFile matrix file 439 * @return matrix 2-d integer array 440 * 441 */ matrixRead(File matrixFile)442 private int[][] matrixRead(File matrixFile) 443 { 444 //String delim = " :\t\n"; 445 String line = ""; 446 BufferedReader in; 447 residueMatrixPosition = new Hashtable(); 448 449 idimension = 1; 450 RE regexp = getRegularExpression(); 451 452 // determine dimensions of the matrix 453 try 454 { 455 in = new BufferedReader(new FileReader(matrixFile)); 456 while((line = in.readLine()) != null ) 457 matrixLineCount(line,regexp); 458 in.close(); 459 } 460 catch (IOException e) 461 { 462 System.out.println("Cannot read matrix file in!"); 463 } 464 465 // initialise matrix 466 matrix = new int[idimension][jdimension]; 467 468 // read the matrix 469 try 470 { 471 in = new BufferedReader(new FileReader(matrixFile)); 472 while((line = in.readLine()) != null ) 473 matrixLineParse(line,regexp); 474 in.close(); 475 } 476 catch (IOException e) 477 { 478 System.out.println("Cannot read matrix file in!"); 479 } 480 return matrix; 481 } 482 483 /** 484 * 485 * Read and parse a scoring matrix 486 * @param matrixString matrix as text 487 * @return matrix 2-d integer array 488 * 489 */ matrixReadString(String matrixString)490 private int[][] matrixReadString(String matrixString) 491 { 492 //String delim = " :\t\n"; 493 String line = ""; 494 BufferedReader in; 495 residueMatrixPosition = new Hashtable(); 496 497 idimension = 1; 498 RE regexp = getRegularExpression(); 499 500 // determine dimensions of the matrix 501 try 502 { 503 in = new BufferedReader(new StringReader(matrixString)); 504 while((line = in.readLine()) != null ) 505 matrixLineCount(line,regexp); 506 in.close(); 507 } 508 catch (IOException e) 509 { 510 System.out.println("Cannot read matrix file in!"); 511 } 512 513 // initialise matrix 514 matrix = new int[idimension][jdimension]; 515 516 // read the matrix 517 try 518 { 519 in = new BufferedReader(new StringReader(matrixString)); 520 while((line = in.readLine()) != null ) 521 matrixLineParse(line,regexp); 522 in.close(); 523 } 524 catch (IOException e) 525 { 526 System.out.println("Cannot read matrix file in!"); 527 } 528 return matrix; 529 } 530 531 } 532 533