private int[][] getPwmByName(String tfbsName, ArrayList<RegulatoryElementPWM> pwms) { for (Iterator<RegulatoryElementPWM> iterator = pwms.iterator(); iterator.hasNext(); ) { RegulatoryElementPWM tfbsPWM = (RegulatoryElementPWM) iterator.next(); if (tfbsPWM.getName().equals(tfbsName)) { return tfbsPWM.getPwm(); } } return null; }
// TODO: faze it out - now I use Hash as above. Make sure that the score public ArrayList<RegulatoryElementPWM> getRegulatoryElementsPWMs() throws DataFormatException { String pwmLineRegEx1 = "[ACGTacgt][ \t]*\\|([ \t]*\\d+)+[ \t]*"; String pwmLineRegEx2 = "[ACGTacgt][ \t]*\\[([ \t]*\\d+)+[ \t]*\\][ \t]*"; ArrayList<RegulatoryElementPWM> pwms = new ArrayList<RegulatoryElementPWM>(); // *** Get file names of all matrixes final String[] matrixFileNames = new File(matrixFilesDir).list(); if (matrixFileNames == null) { throw new DataFormatException( "Patser Regulatory Element Service has no PSSMs associated with it."); } for (int i = 0; i < matrixFileNames.length; i++) { // Only read files with .matrix extension if (!matrixFileNames[i].endsWith(SystemVariables.getInstance().getString("pwm.extension"))) { continue; } RegulatoryElementPWM currPwmObj = new RegulatoryElementPWM(); String tfbsName = matrixFileNames[i].substring(0, matrixFileNames[i].length() - 7); currPwmObj.setName(tfbsName); BufferedReader bufferedReader = null; try { // for Mac bufferedReader = new BufferedReader(new FileReader(matrixFilesDir + "/" + matrixFileNames[i])); // bufferedReader = new BufferedReader(new FileReader(matrixFilesDir + matrixFileNames[i])); String line = null; int[][] pwmArr = null; int j = -1; while (null != (line = bufferedReader.readLine())) { if (line.matches(pwmLineRegEx1) || line.matches(pwmLineRegEx2)) { j++; if (j > 3) throw new DataFormatException( "Error parsing matrix file <" + tfbsName + ".matrix>. Unexpected line in the file. File should contain only one matrix."); line = line.replaceAll("[ACGTacgt\\|\\[\\]]", ""); // remove everything but the numbers StringTokenizer strTok = new StringTokenizer(line); int lengthOfPwm = strTok.countTokens(); if (pwmArr == null) { pwmArr = new int[lengthOfPwm][4]; } if (lengthOfPwm != pwmArr.length) throw new DataFormatException( "Error parsing matrix file <" + tfbsName + ".matrix>. Matrix is unbalanced."); int k = -1; while (strTok.hasMoreElements()) { k++; String token = strTok.nextToken(); pwmArr[k][j] = Integer.parseInt(token); } } } currPwmObj.setPwm(pwmArr); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (bufferedReader != null) bufferedReader.close(); } catch (IOException ex) { ex.printStackTrace(); } } pwms.add(currPwmObj); } // matrix for return pwms; }