compare-all.cpp 4.28 KB
Newer Older
Mikaël Salson's avatar
Mikaël Salson committed
1
/*
2
  This file is part of Vidjil <http://www.vidjil.org>
3
  Copyright (C) 2011-2017 by 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 25 26

  "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 "compare-all.h"
#include "tools.h"

27
SimilarityMatrix compare_all(list <Sequence> sequences,
Mikaël Salson's avatar
Mikaël Salson committed
28 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
                             list <string> sequence_names)
{
  SimilarityMatrix matrix(sequences.size());

  if (!sequences.size())
    {
      return matrix ;
    }

  int num = 0 ;

  for (list <Sequence>::const_iterator it1 = sequences.begin();
       it1 != sequences.end(); ++it1 )
  num = 0 ;

  list<string>::const_iterator itLabel = sequence_names.begin();
  for (list <Sequence>::const_iterator it1 = sequences.begin();
       it1 != sequences.end(); ++it1 )
    {
      string seq1 = it1 -> sequence ;

      if (sequence_names.empty())
        matrix.setLabel(num, string_of_int(num+1));
      else {
        matrix.setLabel(num, *itLabel);
        itLabel++;
      }
      
      int num_num = -1 ;

      for (list <Sequence>::const_iterator it2 = sequences.begin();
	   it2 != sequences.end(); ++it2 )
	{
	  if (++num_num < num)
	    {
	      continue ;
	    }

	  string seq2 = it2 -> sequence ;

	  DynProg dp = DynProg(seq1, seq2, DynProg::Local, IdentityDirty);
  
	  int score = dp.compute() ;
	  float identity = identity_percent(score);

          matrix.setScore(num, num_num, identity);
74
          matrix.setScore(num_num, num, identity);
Mikaël Salson's avatar
Mikaël Salson committed
75 76 77 78 79 80 81 82 83
	  
        }
    
      matrix.setDescription(num++, it1->label);
    }  

  return matrix;
}

84 85 86 87 88 89 90 91 92 93
/*Function which permit to compare all windows in windowsStorage*/
SimilarityMatrix compare_windows(WindowsStorage &windowsStorage, const Cost theCost, int nb_clones) {

    //Creation of a nb_clones matrix
    SimilarityMatrix matrix(nb_clones);

    if (windowsStorage.size() == 0)
        return matrix;

    //Creation of a pair list, which contains junctions/windows (index) and the number of occurences of this (value)
94
    list<pair <junction, size_t> > sortedList = windowsStorage.getSortedList();
95 96 97 98 99 100

    //Positions of the array
    int positionIt1 = 0;
    int positionIt2 = 0;

    //Process of the junctions (windows)
101
    for (list<pair <junction, size_t> >:: const_iterator it1 = sortedList.begin();
102 103 104 105 106 107
        it1 != sortedList.end(); ++it1) {

        //We save the name of the junction
        string win1 = it1->first;

        //We compute & save only clones it the position is lower than the number of clones we want
108
        if (positionIt1 >= nb_clones) break;
109 110

        //Process of the second junctions
111
        for (list<pair <junction, size_t> >:: const_iterator it2 = sortedList.begin();
112 113 114 115 116 117 118
        it2 != sortedList.end(); ++it2) {

            string win2 = it2->first;

            //We compute only distances when first position is lower than the second
            if (positionIt1 < positionIt2) {

119
                if (positionIt2 < nb_clones) {
120 121 122 123 124 125 126 127 128 129 130 131 132

                //Compute all the windows, globally
                DynProg dp = DynProg(win1, win2, DynProg::Global, theCost);

                //Compute the score by dynamic programming
                float score = dp.compute();

                //To debug
                /*dp.backtrack();
                cout << dp.str_back;*/

                //Put the score at position1 / position2 of the array
                matrix.setScore(positionIt1, positionIt2, score);
133
                matrix.setScore(positionIt2, positionIt1, score);
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

                }

                else break;

            }

            positionIt2++;
        }

        positionIt1++;
        positionIt2 = 0;

    }

    return matrix;
}