cluster-junctions.cpp 10.9 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 27 28 29 30

  "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"
#include<cstdlib>

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

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

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;
47
  m = alloc_matrix(sort_clones.size());
Mikaël Salson's avatar
Mikaël Salson committed
48 49 50 51 52
  
  int c=0;
  int c1=0;
  int c2=0;

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

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

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

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

86
  char* tampon=(char*)malloc(sort_clones.size()*sizeof(char));
Mikaël Salson's avatar
Mikaël Salson committed
87
  ifstream in_comp(file.c_str());
88 89 90
  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
91 92
      m[i][j]=tampon[j];
    }
Mikaël Salson's avatar
Mikaël Salson committed
93 94 95 96 97 98 99 100 101 102 103
  }
  free(tampon);

  in_comp.close();

}

void comp_matrix::save(string file){
  
  ofstream out_comp(file.c_str());
  
104 105
  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
106 107 108 109 110 111 112
  }
  
  out_comp.close();
}
 

void comp_matrix::del(){
113
  for (unsigned int i=0;i<sort_clones.size();i++){
Mikaël Salson's avatar
Mikaël Salson committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	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;

137
  size_t i = 0;
138
  for (list <pair<junction,size_t> >::const_iterator ite = sort_clones.begin();
139
    (ite != sort_clones.end()) & (i<matrix_size); ++ite) 
Mikaël Salson's avatar
Mikaël Salson committed
140
    {
141
      i++;
Mikaël Salson's avatar
Mikaël Salson committed
142 143 144 145 146 147
      n_j++;
      list <string> voisins ;
      j1=ite->first;
      neighbor[j1]=voisins;
    }
  
148
  i = 0;
149
  for (list <pair<junction,size_t> >::const_iterator it0 = sort_clones.begin();
150 151
    (it0 != sort_clones.end()) & (i<matrix_size); ++it0) 
      {
152
      i++;
Mikaël Salson's avatar
Mikaël Salson committed
153
      j1=it0->first;
154
      int k=it0->second;
Mikaël Salson's avatar
Mikaël Salson committed
155 156 157
      count[j1]=k;
      n_j2+=k;
      
158
      size_t j = 0; 
159
      for (list <pair<junction,size_t> >::const_iterator it1 = sort_clones.begin();
Mikaël Salson's avatar
Mikaël Salson committed
160 161 162 163 164
	   (it1 != sort_clones.end()) & (j<matrix_size); ++it1) 
	{
	  j++;
	  j2=it1->first;
	  int distance = (int)m[c2][c1];
165
            
Mikaël Salson's avatar
Mikaël Salson committed
166
	  if (distance <= epsilon){
167
            neighbor[j1].push_back(j2);
Mikaël Salson's avatar
Mikaël Salson committed
168 169 170
	  }
	  c1++;
	  c++;
171
        }//fin it1
Mikaël Salson's avatar
Mikaël Salson committed
172 173 174
      c2++;
      c1=0;
    }//fin it0
Mikaël Salson's avatar
Mikaël Salson committed
175
    
Mikaël Salson's avatar
Mikaël Salson committed
176 177 178
  /////////////////////////
  //Forced - edges
  if (forced_edges.size())
Mikaël Salson's avatar
Mikaël Salson committed
179 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
    {
      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 ;
229
   i = 0;
230
   for (list <pair<junction,size_t> >::const_iterator it0 = sort_clones.begin();
231
    (it0 != sort_clones.end()) & (i<matrix_size); ++it0) 
Mikaël Salson's avatar
Mikaël Salson committed
232
    {
233
      i++;
Mikaël Salson's avatar
Mikaël Salson committed
234 235 236 237 238 239 240 241 242 243 244 245
      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
246
	     it1 != voisins1.end(); ++it1 ) nVoisins+=count[*it1];
Mikaël Salson's avatar
Mikaël Salson committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	
	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
271
		   ++it1 )nVoisins+=count[*it1];
Mikaël Salson's avatar
Mikaël Salson committed
272 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
	      //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
311
	       ++c_it ){
Mikaël Salson's avatar
Mikaël Salson committed
312
	    pair<int,string> pair=*c_it;
Mikaël Salson's avatar
Mikaël Salson committed
313
	    c2.push_back(pair.second);
Mikaël Salson's avatar
Mikaël Salson committed
314 315 316 317 318 319 320 321 322 323 324
	  }
	  c2.reverse();
	  cluster.push_back(c2);
	}
      }
    }//fin it0
    
    return cluster;
    
}

325
void comp_matrix::stat_cluster( list<list<junction> > cluster, ostream &out )
Mikaël Salson's avatar
Mikaël Salson committed
326 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
{
    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;
    
364 365 366
      return; 
      /* The following, if useful, should not be written to a hard-coded out/ directory */

Mikaël Salson's avatar
Mikaël Salson committed
367 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
      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
399

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

Mathieu Giraud's avatar
Mathieu Giraud committed
423 424 425 426 427 428 429
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;
}