cluster-junctions.cpp 11 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-2019 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 "cluster-junctions.h"
25
#include "../lib/json.hpp"
Mikaël Salson's avatar
Mikaël Salson committed
26 27 28 29 30 31
#include<cstdlib>

  bool MySort(const pair<int, string>& lh, const pair<int, string>& rh){
    return lh.second>rh.second;
  }

32
comp_matrix::comp_matrix(list<pair <junction, size_t> > sc)
33
{
34 35
  sort_clones = sc;
  matrix_size = sort_clones.size();
36 37
  if (matrix_size > JUNCTION_LIMIT) matrix_size = JUNCTION_LIMIT;
}
Mikaël Salson's avatar
Mikaël Salson committed
38 39 40 41 42 43 44 45 46 47

void comp_matrix::compare(ostream &out, Cost cluster_cost)
{
  // DEBUG // out << "  DEBUT COMPARE JUNCTION" << endl ;
  //     clock_t start = clock();
   
  Cost compareCost = cluster_cost;
  out << "  Using cost " << compareCost << endl ;
  
  string j1, j2;
48
  m = alloc_matrix(sort_clones.size());
Mikaël Salson's avatar
Mikaël Salson committed
49 50 51 52 53
  
  int c=0;
  int c1=0;
  int c2=0;

54
  size_t i = 0;
55
  for (list <pair<junction,size_t> >::const_iterator it0 = sort_clones.begin();
56
    (it0 != sort_clones.end()) & (i<matrix_size); ++it0) 
Mikaël Salson's avatar
Mikaël Salson committed
57
    {
58
      i++;
Mikaël Salson's avatar
Mikaël Salson committed
59 60
      j1=it0->first;
      
61
      size_t k = 0;
62
      for (list <pair<junction,size_t> >::const_iterator it1 = it0;
63 64 65 66 67 68
        (it1 != sort_clones.end()) & (k<matrix_size); ++it1) 
        {
            k++;
            j2=it1->first;
            DynProg dp = DynProg(j1, j2, DynProg::Local, compareCost);
            int score=dp.compute();
69
            int distance = max(j1.size(), j2.size()) * compareCost.match - score;
70 71 72 73 74
            m[c2][c1]=distance;
            m[c1][c2]=distance;
            c1++;
            c++;
        }//fin it1
Mikaël Salson's avatar
Mikaël Salson committed
75 76 77 78 79 80 81 82
      c2++;
      c1=c2;
    }//fin it0

  // DEBUG // out << "  FIN COMPARE JUNCTION ("<<(clock()-start)/1000 <<"ms)" << endl ;
}

void comp_matrix::load(string file){
83 84

    
85
  m = alloc_matrix(sort_clones.size());
Mikaël Salson's avatar
Mikaël Salson committed
86

87
  char* tampon=(char*)malloc(sort_clones.size()*sizeof(char));
Mikaël Salson's avatar
Mikaël Salson committed
88
  ifstream in_comp(file.c_str());
89 90 91
  for(unsigned int i=0; i<sort_clones.size();i++){
    in_comp.read(tampon, sort_clones.size()*sizeof(char));
    for(unsigned int j=0;j<sort_clones.size(); j++){
Mikaël Salson's avatar
Mikaël Salson committed
92 93
      m[i][j]=tampon[j];
    }
Mikaël Salson's avatar
Mikaël Salson committed
94 95 96 97 98 99 100 101 102 103 104
  }
  free(tampon);

  in_comp.close();

}

void comp_matrix::save(string file){
  
  ofstream out_comp(file.c_str());
  
105 106
  for(unsigned int i=0; i<sort_clones.size();i++){
    out_comp.write((char *)m[i],sort_clones.size()*sizeof(char));
Mikaël Salson's avatar
Mikaël Salson committed
107 108 109 110 111 112 113
  }
  
  out_comp.close();
}
 

void comp_matrix::del(){
114
  for (unsigned int i=0;i<sort_clones.size();i++){
Mikaël Salson's avatar
Mikaël Salson committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	free(m[i]);
  }
  free(m);
}

list<list<junction> >  comp_matrix::cluster(string forced_edges, int w, ostream &out, 
					      int epsilon, int minPts)
{
  // out << "  eps: " << epsilon << " / minPts: " << minPts << endl ;
    
    
  map <string, map <string, bool> > graph ;
  
////////////////////////
//indexation des voisins
  map <string, list <string> > neighbor;
  string j1, j2;
  n_j=0;
  n_j2=0;
  int c=0;
  int c1=0;
  int c2=0;

138
  size_t i = 0;
139
  for (list <pair<junction,size_t> >::const_iterator ite = sort_clones.begin();
140
    (ite != sort_clones.end()) & (i<matrix_size); ++ite) 
Mikaël Salson's avatar
Mikaël Salson committed
141
    {
142
      i++;
Mikaël Salson's avatar
Mikaël Salson committed
143 144 145 146 147 148
      n_j++;
      list <string> voisins ;
      j1=ite->first;
      neighbor[j1]=voisins;
    }
  
149
  i = 0;
150
  for (list <pair<junction,size_t> >::const_iterator it0 = sort_clones.begin();
151 152
    (it0 != sort_clones.end()) & (i<matrix_size); ++it0) 
      {
153
      i++;
Mikaël Salson's avatar
Mikaël Salson committed
154
      j1=it0->first;
155
      int k=it0->second;
Mikaël Salson's avatar
Mikaël Salson committed
156 157 158
      count[j1]=k;
      n_j2+=k;
      
159
      size_t j = 0; 
160
      for (list <pair<junction,size_t> >::const_iterator it1 = sort_clones.begin();
Mikaël Salson's avatar
Mikaël Salson committed
161 162 163 164 165
	   (it1 != sort_clones.end()) & (j<matrix_size); ++it1) 
	{
	  j++;
	  j2=it1->first;
	  int distance = (int)m[c2][c1];
166
            
Mikaël Salson's avatar
Mikaël Salson committed
167
	  if (distance <= epsilon){
168
            neighbor[j1].push_back(j2);
Mikaël Salson's avatar
Mikaël Salson committed
169 170 171
	  }
	  c1++;
	  c++;
172
        }//fin it1
Mikaël Salson's avatar
Mikaël Salson committed
173 174 175
      c2++;
      c1=0;
    }//fin it0
Mikaël Salson's avatar
Mikaël Salson committed
176
    
Mikaël Salson's avatar
Mikaël Salson committed
177 178 179
  /////////////////////////
  //Forced - edges
  if (forced_edges.size())
Mikaël Salson's avatar
Mikaël Salson committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    {
      ifstream fe(forced_edges.c_str());
  
      if (!fe.is_open())
	{
	  out << "! forced_edges failed: " << endl ;
	}
      else
	{
	  out << "  Using forced edges in '" << forced_edges << "'" << endl ;
	  int nb_forced = 0 ;
	  while (fe.good())
	    {
	      string line ;
	      getline (fe, line);

	      int i = line.find(" ");
	      if (i != (int) string::npos)
		{
		  string junc1 = line.substr(0, i);
		  string junc2 = line.substr(i+1, i);

		  if ((junc1.size() != (size_t)w) || (junc2.size() != (size_t)w))
		    {
		      out <<  "   ! wrong junction size for " ;
		      out << junc1 << " (" << junc1.size() << ") " ;
		      out << junc2 << " (" << junc2.size() << ")" << endl ; 
		      continue ;
		    }

		  nb_forced++ ;
		  graph[junc1][junc2] = true ;
		  neighbor[junc1].push_back(junc2);
		  neighbor[junc2].push_back(junc1);
		}
	    }
	  out << "  <== " << nb_forced << " forced edges" << endl ;
	}
    }
  
////////////////////////
//DEBUT DBSCAN
  list <list < string > > cluster ;
  
   map <string, bool> visit ;
   map <string, bool> clust ;
   int nVoisins;
   
   int noise = 0;
   int nb_comp = 0 ;
230
   i = 0;
231
   for (list <pair<junction,size_t> >::const_iterator it0 = sort_clones.begin();
232
    (it0 != sort_clones.end()) & (i<matrix_size); ++it0) 
Mikaël Salson's avatar
Mikaël Salson committed
233
    {
234
      i++;
Mikaël Salson's avatar
Mikaël Salson committed
235 236 237 238 239 240 241 242 243 244 245 246
      j1=it0->first;
      if(visit[j1]==false && clust[j1]==false){
	
	visit[j1]=true;
	
	list <string> voisins1 ;
	voisins1=neighbor[j1];
	voisins1.sort();
	
	nVoisins=0;
	
	for (list<string>::iterator it1 = voisins1.begin();
Mikaël Salson's avatar
Mikaël Salson committed
247
	     it1 != voisins1.end(); ++it1 ) nVoisins+=count[*it1];
Mikaël Salson's avatar
Mikaël Salson committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	
	if (nVoisins<minPts){
	  //noise
	  noise++;
	}else{
	  
	  nb_comp++;
	  list< pair<int,string> > c;
	  c.push_back(make_pair(count[j1], j1));
	  clust[j1]=true;
	  
	  int flag=0;
	  list<string>::iterator it2 = voisins1.begin();
	  while(flag!=1){

	    string j2=*it2;
	    if (visit[j2]==false){
	      visit[j2]=true;
	      list <string> voisins2 ;
	      voisins2=neighbor[j2];
	      voisins2.sort();
	      
	      nVoisins=0;
	      for (list<string>::iterator it1 = voisins2.begin();it1 != voisins2.end();
Mikaël Salson's avatar
Mikaël Salson committed
272
		   ++it1 )nVoisins+=count[*it1];
Mikaël Salson's avatar
Mikaël Salson committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	      //si la junction possede assez de voisins
	      if (nVoisins>=minPts){
		//on ajoute ses voisins a la liste des voisins du cluster
		voisins1.merge(voisins2);
		voisins1.unique();
		
		//on replace l'iterateur au bon endroit sur la liste des voisins
		it2=voisins1.begin();
		while(j2.compare(*it2)!=0){
		  it2++;
		}
		
		//on ajoute la junction au cluster
		if (clust[j2]!=true){
		  clust[j2]=true;
		  c.push_back(make_pair(count[j2], j2));
		  j1=j2;
		}
	      }
	    }
	    it2++;
	    if ( it2 == voisins1.end() ) flag=1;
	    
	  }
	  
	  //on ajoute les voisins du cluster(qui ne sont pas deja clusterisé par d'autre cluster) dans le cluster
	  for (list<string>::iterator it1 = voisins1.begin();it1 != voisins1.end();++it1 ){
	    string j1=*it1;
	    if (clust[j1]==false){
	      clust[j1]=true;
	      c.push_back(make_pair(count[j1], j1));
	    }
	  }
	  
	  //avant d'ajouter le cluster a la liste on trie les junctions 
	  c.sort();
	  list< string > c2;
	  
	  for (list< pair<int,string> >::iterator c_it = c.begin(); c_it != c.end();
Mikaël Salson's avatar
Mikaël Salson committed
312
	       ++c_it ){
Mikaël Salson's avatar
Mikaël Salson committed
313
	    pair<int,string> pair=*c_it;
Mikaël Salson's avatar
Mikaël Salson committed
314
	    c2.push_back(pair.second);
Mikaël Salson's avatar
Mikaël Salson committed
315 316 317 318 319 320 321 322 323 324 325
	  }
	  c2.reverse();
	  cluster.push_back(c2);
	}
      }
    }//fin it0
    
    return cluster;
    
}

326
void comp_matrix::stat_cluster( list<list<junction> > cluster, ostream &out )
Mikaël Salson's avatar
Mikaël Salson committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
{
    int n_cluster=0;			//nombre de cluster
    int n_100cluster=0;			//nombre de cluster de taille superieur a 100
    int max_size=0;			//taille du plus gros cluster
    int n_jc=0;				//nombre de junction unique clusterisé
    float jc=0;				//nombre de junction unique clusterisé(%)
    int n_j2c=0;			//nombre de junction clusterisé
    float jc2=0;			//nombre de junction clusterisé(%)
    float moy=0;			//taille moyenne des clusters
    
    //recherche du plus gros cluster
   for (list <list <string> >::iterator it = cluster.begin();
    it != cluster.end(); ++it )
    {
      list<string> li=*it;
      n_cluster++;
      int size=0;
      
      for (list<string>::iterator it2 = li.begin();
      it2 != li.end(); ++it2 )
      {
	n_jc++;
	size +=count[*it2];
      }
      
      n_j2c+=size;
      
      if (size> max_size) max_size=size;
      
      if (size>=100) {
	n_100cluster++;
      }
      
    }

      if(n_j2c!=0)
	moy=n_j2c/n_cluster;
    
365 366 367
      return; 
      /* The following, if useful, should not be written to a hard-coded out/ directory */

Mikaël Salson's avatar
Mikaël Salson committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
      ofstream stat;
      stat.open("out/stat.txt");
      
      stat << n_cluster <<" ";		//nombre de cluster
      out << "    clusters 		: "<< n_cluster << endl ;
      
      stat << n_100cluster <<" ";	//nombre de cluster de taille > a 100
      out << "    clusters with size >= 100       : "<< n_100cluster << endl ;

      stat << max_size <<" ";		//taille du plus gros cluster		
      out << "    size of the biggest cluster 	: "<< max_size << endl ;
            
      stat << moy <<" ";			//taille moyenne des clusters		
      out << "    average size of clusters 	: "<< (int)moy << endl ;
                        
      stat << n_j <<" ";			//nombre de junction 
      out << "    junctions 		: "<< n_j << endl ;

      stat << n_jc <<" ";		//nombre de junction clusterisé
      if (n_j!=0) jc= ((n_jc*100)/n_j);
      stat << jc <<" ";
      out << "    clusterised junctions 	: "<< n_jc << " (" << jc << "%)" << endl ;

      stat << n_j2 <<" ";		//nombre d'occurrence
      out << "    occurrences 	        : "<< n_j2 << endl ;

      stat << n_j2c <<" ";		//nombre d'occurrence clusterisé
      if (n_j2!=0)  jc2= ((n_j2c*100)/n_j2);
      stat << jc2 <<" ";
      out << "    clusterised occurrences	: "<< n_j2c << " (" << jc2 << "%)" << endl ;
  
}
Mathieu Giraud's avatar
Mathieu Giraud committed
400

401
json comp_matrix::toJson(list<list<junction> > clusterList)
402
{
403
    json result;
404 405 406 407 408 409
    
    for (list <list <string> >::iterator it = clusterList.begin();
    it != clusterList.end(); ++it )
    {
      list<string> li=*it;
      
410
      if (li.size() > 1 ){ 
411
        json cluster;
412 413 414
        for (list<string>::iterator it2 = li.begin();
        it2 != li.end(); ++it2 )
        {
415
            cluster.push_back(*it2);
416
        }
417
        result.push_back(cluster);
418 419 420 421 422 423
      }
    }
    
    return result;
}

Mathieu Giraud's avatar
Mathieu Giraud committed
424 425 426 427 428 429 430
char **comp_matrix::alloc_matrix(size_t s) {
  char **matrix = (char**)malloc(s*sizeof(char*));
  for (size_t i=0;i<s;i++){
    matrix[i]=(char *)malloc(s*sizeof(char));
  }
  return matrix;
}