/*
* Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
* Copyright (C) $$Year-Rel$$ The Jalview Authors
*
* This file is part of Jalview.
*
* Jalview 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 3
* of the License, or (at your option) any later version.
*
* Jalview 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 Jalview. If not, see
* Unmapped characters (not in the alphabet) get an index of -1. *
* Mappings are added automatically for lower case symbols (for non case * sensitive scoring), unless they are explicitly present in the alphabet (are * scored separately in the score matrix). * * @param alphabet * @return */ static short[] buildSymbolIndex(char[] alphabet) { short[] index = new short[MAX_ASCII + 1]; Arrays.fill(index, UNMAPPED); short pos = 0; for (char c : alphabet) { if (c <= MAX_ASCII) { index[c] = pos; } /* * also map lower-case character (unless separately mapped) */ if (c >= 'A' && c <= 'Z') { short lowerCase = (short) (c + ('a' - 'A')); if (index[lowerCase] == UNMAPPED) { index[lowerCase] = pos; } } pos++; } return index; } @Override public String getName() { return name; } @Override public boolean isDNA() { return !peptide; } @Override public boolean isProtein() { return peptide; } @Override public float[][] getMatrix() { return matrix; } /** * Returns the pairwise score for substituting c with d, or zero if c or d is * an unscored or unexpected character */ @Override public float getPairwiseScore(char c, char d) { if (c > MAX_ASCII) { System.err.println(String.format(BAD_ASCII_ERROR, c)); return 0; } if (d > MAX_ASCII) { System.err.println(String.format(BAD_ASCII_ERROR, d)); return 0; } int cIndex = symbolIndex[c]; int dIndex = symbolIndex[d]; if (cIndex != UNMAPPED && dIndex != UNMAPPED) { return matrix[cIndex][dIndex]; } return 0; } /** * pretty print the matrix */ @Override public String toString() { return outputMatrix(false); } /** * Print the score matrix, optionally formatted as html, with the alphabet symbols as column headings and at the start of each row * @param html * @return */ public String outputMatrix(boolean html) { StringBuilder sb = new StringBuilder(512); /* * heading row with alphabet */ if (html) { sb.append("
" : ""); } for (char sym : symbols) { if (html) { sb.append(" | ").append(sym).append(" | "); } else { sb.append("\t").append(sym); } } sb.append(html ? "
---|---|
"); } sb.append(c1).append(html ? " | " : ""); for (char c2 : symbols) { sb.append(html ? "" : "\t") .append(matrix[symbolIndex[c1]][symbolIndex[c2]]) .append(html ? " | " : ""); } sb.append(html ? "