/* * 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 . * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.analysis.scoremodels; import jalview.api.analysis.DistanceModelI; import jalview.datamodel.AlignmentView; import jalview.util.Comparison; public class PairwiseDistanceModel implements DistanceModelI { PairwiseScoreModelI scoreModel; /** * Constructor given something to provide pairwise scores for residues * * @param sm */ public PairwiseDistanceModel(PairwiseScoreModelI sm) { scoreModel = sm; } /** * Returns a matrix of [i][j] values representing distances between pairs of * sequences */ @Override public float[][] findDistances(AlignmentView seqData) { String[] sequenceString = seqData .getSequenceStrings(Comparison.GAP_SPACE); int noseqs = sequenceString.length; float[][] distance = new float[noseqs][noseqs]; /* * calculate similarity scores for the upper half of the matrix * as [i, j] = the sum of pairwise scores of corresponding * positions of sequence[i] and sequence[j] */ float maxscore = 0; int end = sequenceString[0].length(); for (int i = 0; i < (noseqs - 1); i++) { for (int j = i; j < noseqs; j++) { float score = 0; for (int k = 0; k < end; k++) { try { score += scoreModel.getPairwiseScore( sequenceString[i].charAt(k), sequenceString[j].charAt(k)); } catch (Exception ex) { System.err.println("err creating " + getName() + " tree"); ex.printStackTrace(); } } distance[i][j] = score; if (score > maxscore) { maxscore = score; } } } /* * subtract similarity scores from the maximum value to * convert to a distance measure; also populate the lower * half of the result matrix with this value */ // FIXME this assumes the score matrix is symmetric - it may not be? for (int i = 0; i < (noseqs - 1); i++) { for (int j = i; j < noseqs; j++) { distance[i][j] = maxscore - distance[i][j]; distance[j][i] = distance[i][j]; } } return distance; } @Override public String getName() { return scoreModel.getName(); } @Override public boolean isDNA() { return scoreModel.isDNA(); } @Override public boolean isProtein() { return scoreModel.isProtein(); } public PairwiseScoreModelI getScoreModel() { return scoreModel; } }