similarityMatrix.cpp 5.39 KB
Newer Older
Mikaël Salson's avatar
Mikaël Salson committed
1
/*
2
  This file is part of Vidjil <http://www.vidjil.org>
Mathieu Giraud's avatar
Mathieu Giraud committed
3
  Copyright (C) 2011-2020 by VidjilNet consortium and Bonsai bioinformatics
4 5 6 7 8
  at CRIStAL (UMR CNRS 9189, Université Lille) and Inria Lille
  Contributors: 
      Mathieu Giraud <mathieu.giraud@vidjil.org>
      Mikaël Salson <mikael.salson@vidjil.org>
      Marc Duez <marc.duez@vidjil.org>
Mikaël Salson's avatar
Mikaël Salson committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

  "Vidjil" 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.

  "Vidjil" 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 "Vidjil". If not, see <http://www.gnu.org/licenses/>
*/

#include "similarityMatrix.h"
25
#include "../lib/json.hpp"
Mikaël Salson's avatar
Mikaël Salson committed
26 27
#include <cassert>
#include <limits>
28
#include <math.h>
Mikaël Salson's avatar
Mikaël Salson committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

SimilarityMatrix::SimilarityMatrix(int n):n(n),labels(n),descriptions(n) {
  assert(n >= 0);
  scores = new float*[n];
  for (int i=0; i < n; i++) {
    scores[i] = new float[n];
    for (int j = 0; j < n; j++) 
      scores[i][j] = 0;
  }
  maxV = -1;
  minV = numeric_limits<float>::infinity();
}

SimilarityMatrix::~SimilarityMatrix() {
  for (int i=0; i < n; i++) {
    delete [] scores[i];
  } 
  delete [] scores;
}

// Queries
string SimilarityMatrix::description(int i) const{
  assert(i >= 0 && i < n);
  return descriptions[i];
}

string SimilarityMatrix::label(int i) const {
  assert(i >= 0 && i < n);
  return labels[i];
}

float SimilarityMatrix::max() const {
  return maxV;
}

float SimilarityMatrix::min() const {
  return minV;
}

int SimilarityMatrix::size() const {
  return n;
}

float SimilarityMatrix::operator()(int i, int j) const{
  assert(i >= 0 && i < n && j >= 0 && j < n);
  return scores[i][j];
}

  // Commands
void SimilarityMatrix::setDescription(int i, string description) {
  assert(i >= 0 && i < n);
  descriptions[i] = description;
}

void SimilarityMatrix::setLabel(int i, string label) {
  assert(i >= 0 && i < n);
  labels[i] = label;
}

void SimilarityMatrix::setScore(int i, int j, float score) {
  // cout << "i = " << i << ", j = " << j << ", n = " << n << endl;
  assert(i >= 0 && i < n && j >= 0 && j < n);
  scores[i][j] = score;
  if (i != j) {
    if (score < minV) {
      minV = score;
    }
    if (score > maxV) {
      maxV = score;
    }
  }
}

Mikaël Salson's avatar
Mikaël Salson committed
102
OutputSimilarityMatrix::OutputSimilarityMatrix(SimilarityMatrix &m, float sim, int max_display):matrix(m), sim(sim), max_display(max_display){}
Mikaël Salson's avatar
Mikaël Salson committed
103 104 105 106 107 108


float OutputSimilarityMatrix::similarity() const {
  return sim;
}

Mikaël Salson's avatar
Mikaël Salson committed
109 110 111 112 113
int OutputSimilarityMatrix::maxDisplayed() const {
  return max_display;
}

RawOutputSimilarityMatrix::RawOutputSimilarityMatrix(SimilarityMatrix &m, float sim, int max_display) : OutputSimilarityMatrix(m, sim, max_display) {}
Mikaël Salson's avatar
Mikaël Salson committed
114

115
JsonOutputSimilarityMatrix::JsonOutputSimilarityMatrix(SimilarityMatrix &m, float sim, int max_display) : OutputSimilarityMatrix(m, sim, max_display) {}
Mikaël Salson's avatar
Mikaël Salson committed
116

117
JsonOutputWindowsMatrix::JsonOutputWindowsMatrix(SimilarityMatrix &m, float sim, int max_display) : OutputSimilarityMatrix(m, sim, max_display) {}
Mikaël Salson's avatar
Mikaël Salson committed
118 119 120

ostream &operator<<(ostream &out, const RawOutputSimilarityMatrix &outputMatrix) {
  SimilarityMatrix &matrix = outputMatrix.matrix;
121
  out << right;
Mikaël Salson's avatar
Mikaël Salson committed
122 123 124
  out << "    | " ;

  for (int num = 0; num <  matrix.size(); num++) 
Mikaël Salson's avatar
Mikaël Salson committed
125
    if (num <= outputMatrix.maxDisplayed())
Mikaël Salson's avatar
Mikaël Salson committed
126 127 128 129 130
      out << setw(4) << matrix.label(num) << " " ;

  out << endl ;

  for (int num = 0; num <  matrix.size(); num++) {
Mikaël Salson's avatar
Mikaël Salson committed
131
    if (num <= outputMatrix.maxDisplayed())
Mikaël Salson's avatar
Mikaël Salson committed
132 133 134 135 136 137 138 139
      out << setw(3) << matrix.label(num) << " | " ;
    

    for (int num_num = 0; num_num < matrix.size(); num_num++ )
      {
        
        if (num_num < num)
          {
Mikaël Salson's avatar
Mikaël Salson committed
140
            if ((num <= outputMatrix.maxDisplayed()) && (num_num <= outputMatrix.maxDisplayed()))
Mikaël Salson's avatar
Mikaël Salson committed
141 142 143 144
              out << "     " ;
            continue ;
          }
        
Mikaël Salson's avatar
Mikaël Salson committed
145
        if (num_num <= outputMatrix.maxDisplayed())
Mikaël Salson's avatar
Mikaël Salson committed
146 147 148 149 150 151 152 153 154 155 156
          {
            out << setw(4) << (int)matrix(num, num_num) ;
	    
            if ((matrix(num, num_num) >= outputMatrix.similarity()) 
                && (num_num > num)) 
              out << "!"  ;
            else
              out << " " ;
          }
      }

Mikaël Salson's avatar
Mikaël Salson committed
157
    if (num <= outputMatrix.maxDisplayed())
Mikaël Salson's avatar
Mikaël Salson committed
158 159 160 161 162 163 164 165 166 167
      {
        out << "   " << matrix.description(num) ;
        out << endl ;	  
      }
  }


  return out;
}

168 169
/*Export a similarity matrix, for the edit distance distribution & DBSCAN algorithm
*/
170
json &operator<<(json &out, const JsonOutputSimilarityMatrix &outputMatrix) {
171 172

    SimilarityMatrix &matrix = outputMatrix.matrix;
173 174 175
    
    out = json::array();
    
176
    for (int i = 0; i < matrix.size(); i++) {
177
        out[i] = json::array();
178
        for (int j = 0; j < matrix.size(); j++) {
179
            out[i][j] = fabs(matrix(i,j));
180 181 182 183 184 185 186
        }
    }
    return out;
}

/* Export Levenshtein distances matrix, for the edit distance distribution & DBSCAN algorithm
*/
187
json &operator<<(json &out, const JsonOutputWindowsMatrix &outputMatrix) {
188 189

    SimilarityMatrix &matrix = outputMatrix.matrix;
190
    
191
    out = json::array();
192
    
193
    for (int i = 0; i < matrix.size(); i++) {
194
        out[i] = json::array();
195
        for (int j = 0; j < matrix.size(); j++) {
196
            out[i][j] = fabs(matrix(i,j));
197 198 199 200
        }
    }
    return out;
}