segment.cpp 25.2 KB
Newer Older
Mikaël Salson's avatar
Mikaël Salson committed
1
/*
2 3 4 5 6 7 8
  This file is part of Vidjil <http://www.vidjil.org>
  Copyright (C) 2011, 2012, 2013, 2014, 2015 by Bonsai bioinformatics 
  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

  "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/>
*/
Mikaël Salson's avatar
Mikaël Salson committed
23
#include <algorithm>    // std::sort
Mikaël Salson's avatar
Mikaël Salson committed
24 25 26 27
#include <cassert>
#include "segment.h"
#include "tools.h"
#include "affectanalyser.h"
Mikaël Salson's avatar
Mikaël Salson committed
28
#include <sstream>
Marc Duez's avatar
Marc Duez committed
29 30
#include <string>
#include <math.h>
Mikaël Salson's avatar
Mikaël Salson committed
31

Mathieu Giraud's avatar
Mathieu Giraud committed
32
Segmenter::~Segmenter() {}
Mikaël Salson's avatar
Mikaël Salson committed
33

Mathieu Giraud's avatar
Mathieu Giraud committed
34
Sequence Segmenter::getSequence() const {
Mikaël Salson's avatar
Mikaël Salson committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  Sequence s ;
  s.label_full = info ;
  s.label = label + " " + (reversed ? "-" : "+");
  s.sequence = revcomp(sequence, reversed);

  return s ;
}

string Segmenter::getJunction(int l) const {
  assert(isSegmented());

  int start = (getLeft() + getRight())/2 - l/2;
  
  if (start < 0 or start + l > (int)sequence.size())  // TODO: +l ou +l-1 ?
    return "" ;

  return getSequence().sequence.substr(start, l);
}

int Segmenter::getLeft() const {
Mathieu Giraud's avatar
Mathieu Giraud committed
55
  return Vend;
Mikaël Salson's avatar
Mikaël Salson committed
56 57 58
}
  
int Segmenter::getRight() const {
Mathieu Giraud's avatar
Mathieu Giraud committed
59
  return Jstart;
Mikaël Salson's avatar
Mikaël Salson committed
60 61 62
}

int Segmenter::getLeftD() const {
Mathieu Giraud's avatar
Mathieu Giraud committed
63
  return Dstart;
Mikaël Salson's avatar
Mikaël Salson committed
64 65 66
}
  
int Segmenter::getRightD() const {
Mathieu Giraud's avatar
Mathieu Giraud committed
67
  return Dend;
Mikaël Salson's avatar
Mikaël Salson committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81
}

bool Segmenter::isReverse() const {
  return reversed;
}

bool Segmenter::isSegmented() const {
  return segmented;
}

bool Segmenter::isDSegmented() const {
  return dSegmented;
}

82 83 84 85
bool KmerSegmenter::isDetected() const {
  return detected;
}

Mikaël Salson's avatar
Mikaël Salson committed
86 87 88 89 90 91 92 93
// Chevauchement

string Segmenter::removeChevauchement()
{
  assert(isSegmented());
  
  string chevauchement = "" ;

Mathieu Giraud's avatar
Mathieu Giraud committed
94
  if (Vend >= Jstart)
Mikaël Salson's avatar
Mikaël Salson committed
95
    {
Mathieu Giraud's avatar
Mathieu Giraud committed
96
      int middle = (Vend + Jstart) / 2 ;
97
      chevauchement = " !ov " + string_of_int (Vend - Jstart + 1);
Mathieu Giraud's avatar
Mathieu Giraud committed
98 99
      Vend = middle ;
      Jstart = middle+1 ;
Mikaël Salson's avatar
Mikaël Salson committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    }

  return chevauchement ;
}

// Prettyprint


bool Segmenter::finishSegmentation() 
{
  assert(isSegmented());
  
  string seq = getSequence().sequence;
    
Mathieu Giraud's avatar
Mathieu Giraud committed
114 115 116 117 118
  seg_V = seq.substr(0, Vend+1) ;
  seg_N = seq.substr(Vend+1, Jstart-Vend-1) ;  // Twice computed for FineSegmenter, but only once in KmerSegmenter !
  seg_J = seq.substr(Jstart) ;
  Dstart=0;
  Dend=0;
Mikaël Salson's avatar
Mikaël Salson committed
119 120 121 122 123 124 125 126 127 128 129 130 131

  info = "VJ \t" + string_of_int(FIRST_POS) + " " + info + " " + string_of_int(seq.size() - 1 + FIRST_POS) ;
  info += "\t" + code ;

  info = (reversed ? "- " : "+ ") + info ;

  return true ;
}

bool Segmenter::finishSegmentationD() 
{
  string seq = getSequence().sequence;

Mathieu Giraud's avatar
Mathieu Giraud committed
132 133
  seg_V = seq.substr(0, Vend+1) ; // From pos. 0 to Vend
  seg_J = seq.substr(Jstart) ;
Mikaël Salson's avatar
Mikaël Salson committed
134
  
Mathieu Giraud's avatar
Mathieu Giraud committed
135
  seg_D  = seq.substr(Dstart, Dend-Dstart+1) ; // From Dstart to Dend
Mikaël Salson's avatar
Mikaël Salson committed
136
  
Mathieu Giraud's avatar
Mathieu Giraud committed
137 138 139 140
  info = "VDJ \t0 " + string_of_int(Vend) +
		" " + string_of_int(Dstart) + 
		" " + string_of_int(Dend) +
		" " + string_of_int(Jstart) +
Mikaël Salson's avatar
Mikaël Salson committed
141 142 143 144 145 146 147 148 149
		" " + string_of_int(seq.size()-1+FIRST_POS) ;
		
  info += "\t" + code ;
  
  info = (reversed ? "- " : "+ ") + info ;

  return true ;
}

150 151 152 153 154 155 156 157 158 159 160 161
string Segmenter::getInfoLine() const
{
  string s = ">" ;

  s += label + " " ;
  s += (segmented ? "" : "! ") + info ;
  s += " " + info_extra ;
  s += " " + segmented_germline->code ;
  s += " " + string(segmented_mesg[because]) ;
  return s ;
}

Mikaël Salson's avatar
Mikaël Salson committed
162 163
ostream &operator<<(ostream &out, const Segmenter &s)
{
164
  out << s.getInfoLine() << endl;
Mikaël Salson's avatar
Mikaël Salson committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

  if (s.segmented)
    {
      out << s.seg_V << endl ;
      out << s.seg_N << endl ;
      out << s.seg_J << endl ;
    }
  else
    {
      out << s.sequence << endl ;
    }

  return out ;
}


// KmerSegmenter (Cheap)

183 184 185
KmerSegmenter::KmerSegmenter() { kaa = 0 ; }

KmerSegmenter::KmerSegmenter(Sequence seq, Germline *germline)
Mikaël Salson's avatar
Mikaël Salson committed
186 187 188 189
{
  label = seq.label ;
  sequence = seq.sequence ;
  info = "" ;
190
  info_extra = "seed";
Mikaël Salson's avatar
Mikaël Salson committed
191
  segmented = false;
192
  segmented_germline = germline ;
193
  reversed = false;
Mathieu Giraud's avatar
Mathieu Giraud committed
194
  Dend=0;
195 196 197
  because = 0 ; // Cause of unsegmentation
  score = 0 ;

198
  int s = (size_t)germline->index->getS() ;
Mikaël Salson's avatar
Mikaël Salson committed
199
  int length = sequence.length() ;
Mikaël Salson's avatar
Mikaël Salson committed
200

201
  if (length < s) 
Mikaël Salson's avatar
Mikaël Salson committed
202
    {
Mathieu Giraud's avatar
Mathieu Giraud committed
203 204
      because = UNSEG_TOO_SHORT;
      kaa = NULL;
205
      return ;
Mikaël Salson's avatar
Mikaël Salson committed
206 207
    }
 
208
  kaa = new KmerAffectAnalyser(*(germline->index), sequence);
Mikaël Salson's avatar
Mikaël Salson committed
209 210
  
  // Check strand consistency among the affectations.
Mikaël Salson's avatar
Mikaël Salson committed
211 212 213 214 215 216 217 218 219
  int strand;
  int nb_strand[2] = {0,0};     // In cell 0 we'll put the number of negative
                                // strand, while in cell 1 we'll put the
                                // positives
  for (int i = 0; i < kaa->count(); i++) { 
    KmerAffect it = kaa->getAffectation(i);
    if (! it.isAmbiguous() && ! it.isUnknown()) {
      strand = affect_strand(it.affect);
      nb_strand[(strand + 1) / 2] ++; // (strand+1) / 2 → 0 if strand == -1; 1 if strand == 1
Mikaël Salson's avatar
Mikaël Salson committed
220 221 222
    }
  }

223 224 225
  KmerAffect before, after;

  // Test on which strand we are, select the before and after KmerAffects
Mikaël Salson's avatar
Mikaël Salson committed
226
  if (nb_strand[0] == 0 && nb_strand[1] == 0) {
227
    because = UNSEG_TOO_FEW_ZERO ;
228
    return ;
Mikaël Salson's avatar
Mikaël Salson committed
229 230
  } else if (nb_strand[0] > RATIO_STRAND * nb_strand[1]) {
    strand = -1;
231 232
    before = KmerAffect(germline->affect_3, -1); 
    after = KmerAffect(germline->affect_5, -1);
Mikaël Salson's avatar
Mikaël Salson committed
233 234
  } else if (nb_strand[1] > RATIO_STRAND * nb_strand[0]) {
    strand = 1;
235 236
    before = KmerAffect(germline->affect_5, 1); 
    after = KmerAffect(germline->affect_3, 1);    
Mikaël Salson's avatar
Mikaël Salson committed
237 238 239
  } else {
    // Ambiguous information: we have positive and negative strands
    // and there is not enough difference to put them aparat.
240 241
    because = UNSEG_STRAND_NOT_CONSISTENT ;
    return ;
Mikaël Salson's avatar
Mikaël Salson committed
242 243
  }

244
  detected = false ;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  computeSegmentation(strand, before, after);

  if (! because)
    {
      // Now we check the delta between Vend and right
   
      if (Jstart - Vend < germline->delta_min)
	{
	  because = UNSEG_BAD_DELTA_MIN ;
	}

      if (Jstart - Vend > germline->delta_max)
	{
	  because = UNSEG_BAD_DELTA_MAX ;
	}
    } 
Mathieu Giraud's avatar
Mathieu Giraud committed
261

262
  if (!because)
Mathieu Giraud's avatar
Mathieu Giraud committed
263 264
    {
      // Yes, it is segmented
265
      segmented = true;
Mathieu Giraud's avatar
Mathieu Giraud committed
266 267 268 269
      reversed = (strand == -1); 
      because = reversed ? SEG_MINUS : SEG_PLUS ;

      info = string_of_int(Vend + FIRST_POS) + " " + string_of_int(Jstart + FIRST_POS)  ;
270 271
      // removeChevauchement is called once info was already computed: it is only to output info_extra
      info_extra += removeChevauchement();
Mathieu Giraud's avatar
Mathieu Giraud committed
272
      finishSegmentation();
273
      system = germline->code;
274
      return ;
Mathieu Giraud's avatar
Mathieu Giraud committed
275
    } 
276

277
 
Mathieu Giraud's avatar
Mathieu Giraud committed
278
}
Mikaël Salson's avatar
Mikaël Salson committed
279

Mathieu Giraud's avatar
Mathieu Giraud committed
280
KmerSegmenter::~KmerSegmenter() {
281 282
  if (kaa)
    delete kaa;
283 284
}

285
KmerMultiSegmenter::KmerMultiSegmenter(Sequence seq, MultiGermline *multigermline, ostream *out_unsegmented, double threshold)
286
{
287
  int best_score_seg = 0 ; // Best score, segmented sequences
288
  int best_score_unseg = 0 ; // Best score, unsegmented sequences
289
  the_kseg = NULL;
290
  multi_germline = multigermline;
291
  threshold_nb_expected = threshold;
292
  
293 294 295 296 297
  // Iterate over the germlines
  for (list<Germline*>::const_iterator it = multigermline->germlines.begin(); it != multigermline->germlines.end(); ++it)
    {
      Germline *germline = *it ;

298 299
      KmerSegmenter *kseg = new KmerSegmenter(seq, germline);
      bool keep_seg = false;
300

301 302 303 304
      if (out_unsegmented)
        {
          // Debug, display k-mer affectation and segmentation result for this germline
          *out_unsegmented << "#"
305 306
                           << left << setw(4) << kseg->segmented_germline->code << " "
                           << left << setw(20) << segmented_mesg[kseg->getSegmentationStatus()] << " ";
307

308
          *out_unsegmented << right << setw(3) << kseg->score << " ";
309
          
310 311
          if (kseg->getSegmentationStatus() != UNSEG_TOO_SHORT) 
            *out_unsegmented << kseg->getKmerAffectAnalyser()->toString();
312 313 314 315

          *out_unsegmented << endl ;
        }

316 317
      // Always remember the first kseg
      if (the_kseg == NULL)
318
        keep_seg = true;
319
      
320
      if (kseg->isSegmented())
321 322
        {
          // Yes, it is segmented
323
          // Should we keep the kseg ?
324
          if (kseg->score > best_score_seg)
325
            {
326
              keep_seg = true;
327
              best_score_seg = kseg->score ;
328
            }
329
        }
330 331 332 333 334 335 336 337 338 339 340 341
      else
        {
          // It is not segmented
          // Should we keep the kseg (with the unsegmentation cause) ?
            if (kseg->score > best_score_unseg)
            {              
              best_score_unseg = kseg->score ;
              if (!best_score_seg)
                keep_seg = true;
            }
        }
      
342
      if (keep_seg) {
343 344
        if (the_kseg)
          delete the_kseg;
345 346 347 348
        the_kseg = kseg;
      } else {
        delete kseg;
      }
349 350 351 352 353
    } // end for (Germlines)
  if (the_kseg->isSegmented() && getNbExpected() > threshold_nb_expected)
    the_kseg->setSegmentationStatus(UNSEG_NOISY);
}

354 355 356 357 358 359
double KmerSegmenter::getProbabilityAtLeastOrAbove(int at_least) const {

  // n: number of kmers in the sequence
  int n = getSequence().sequence.size() - getKmerAffectAnalyser()->getIndex().getS();
  float index_load = getKmerAffectAnalyser()->getIndex().getIndexLoad() ;

360
  double proba = 0;
361 362
  double probability_having_system = pow(index_load, at_least);
  double probability_not_having_system = pow(1 - index_load, n - at_least);
363
  for (int i=at_least; i<n; i++) {
364 365 366
    proba += nChoosek(n, i) * probability_having_system * probability_not_having_system;
    probability_having_system *= index_load;
    probability_not_having_system /= index_load;
367
  }
368 369 370 371 372 373

  return proba;
}

double KmerMultiSegmenter::getNbExpected() const {
  double proba = the_kseg->getProbabilityAtLeastOrAbove(the_kseg->score);
374
  return multi_germline->germlines.size() * proba;
375 376 377
}

KmerMultiSegmenter::~KmerMultiSegmenter() {
378 379
  if (the_kseg)
    delete the_kseg;
Mathieu Giraud's avatar
Mathieu Giraud committed
380 381
}

382 383
void KmerSegmenter::computeSegmentation(int strand, KmerAffect before, KmerAffect after) {
  // Try to segment, computing 'Vend' and 'Jstart'
384 385
  // If not segmented, put the cause of unsegmentation in 'because'

386
  affect_infos max;
Mikaël Salson's avatar
Mikaël Salson committed
387

388
  max = kaa->getMaximum(before, after); 
389 390 391 392 393

      // We labeled it detected if there were both enough affect_5 and enough affect_3
      detected = (max.nb_before_left + max.nb_before_right >= DETECT_THRESHOLD)
        && (max.nb_after_left + max.nb_after_right >= DETECT_THRESHOLD);
      
394
      if (! max.max_found) {
395 396
        if ((strand == 1 && max.nb_before_left == 0)
            || (strand == -1 && max.nb_after_right == 0)) 
397
          because = detected ? UNSEG_AMBIGUOUS : UNSEG_TOO_FEW_V ;
398 399
        else if ((strand == 1 && max.nb_after_right == 0)
                 || (strand == -1 && max.nb_before_left == 0))
Mikaël Salson's avatar
Mikaël Salson committed
400
	{
401
	  because = detected ? UNSEG_AMBIGUOUS : UNSEG_TOO_FEW_J ;
402
	} else 
403 404 405 406 407 408 409 410 411 412
          because = UNSEG_AMBIGUOUS; 
      } else {
        Vend = max.first_pos_max;
        Jstart = max.last_pos_max + 1;
        if (strand == -1) {
          int tmp = sequence.size() - Vend - 1;
          Vend = sequence.size() - Jstart - 1;
          Jstart = tmp;
        }
      }
Mikaël Salson's avatar
Mikaël Salson committed
413
  
414
    score = max.nb_before_left + max.nb_before_right + max.nb_after_left + max.nb_after_right;  
Mathieu Giraud's avatar
Mathieu Giraud committed
415
}
Mikaël Salson's avatar
Mikaël Salson committed
416

417
KmerAffectAnalyser *KmerSegmenter::getKmerAffectAnalyser() const {
Mathieu Giraud's avatar
Mathieu Giraud committed
418 419
  return kaa;
}
Mikaël Salson's avatar
Mikaël Salson committed
420

421
int Segmenter::getSegmentationStatus() const {
Mathieu Giraud's avatar
Mathieu Giraud committed
422
  return because;
Mikaël Salson's avatar
Mikaël Salson committed
423 424
}

425 426 427 428 429
void Segmenter::setSegmentationStatus(int status) {
  because = status;
  segmented = (status == SEG_PLUS || status == SEG_MINUS);
}

Mikaël Salson's avatar
Mikaël Salson committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
// FineSegmenter

void best_align(int overlap, string seq_left, string seq_right,
		string ref_left ,string ref_right, int *b_r, int *b_l, Cost segment_cost)
{
      int score_r[overlap+1];
      int score_l[overlap+1];
      
      //LEFT
      ref_left=string(ref_left.rbegin(), ref_left.rend());
      seq_left=string(seq_left.rbegin(), seq_left.rend()); 
      
      DynProg dp1 = DynProg(seq_left, ref_left,
			   DynProg::Local, segment_cost);
      score_l[0] = dp1.compute();
      dp1.backtrack();
      
      //RIGHT
      DynProg dp = DynProg(seq_right, ref_right,
			   DynProg::Local, segment_cost);
      score_r[0] = dp.compute();
      dp.backtrack();    
      
      for(int i=1; i<=overlap; i++){
	if(dp.best_i-i >0)
455
	score_r[i] = dp.B[dp.best_i-i][dp.best_j].score;
Mikaël Salson's avatar
Mikaël Salson committed
456 457 458
	else
	score_r[i] =0;
	if(dp1.best_i-i >0)
459
	score_l[i] = dp1.B[dp1.best_i-i][dp1.best_j].score;	
Mikaël Salson's avatar
Mikaël Salson committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	else
	score_l[i] =0;
      }
      int score=-1000000;
      int best_r=0;
      int best_l=0;

      for (int i=0; i<=overlap; i++){
	for (int j=overlap-i; j<=overlap; j++){
	  if ( ((score_r[i]+score_l[j]) == score) && (i+j < best_r+best_l )){
	    best_r=i;
	    best_l=j;
	    score=(score_r[i]+score_l[j]) ;
	  }
	  if ( (score_r[i]+score_l[j]) > score){
	    best_r=i;
	    best_l=j;
	    score=(score_r[i]+score_l[j]) ;
	  }
	}
      }
      *b_r=best_r;
      *b_l=best_l;
}

Mikaël Salson's avatar
Mikaël Salson committed
485 486 487 488 489
bool comp_pair (pair<int,int> i,pair<int,int> j)
{
  return ( i.first > j.first);
}

Mathieu Giraud's avatar
Mathieu Giraud committed
490
int align_against_collection(string &read, Fasta &rep, bool reverse_both, bool local, int *tag, 
Mikaël Salson's avatar
Mikaël Salson committed
491
			     int *del, int *del2, int *begin, int *length, vector<pair<int, int> > *score
Mikaël Salson's avatar
Mikaël Salson committed
492 493 494 495 496 497 498 499 500 501
			    , Cost segment_cost)
{
  
  int best_score = MINUS_INF ;
  int best_r = MINUS_INF ;
  int best_best_i = (int) string::npos ;
  int best_best_j = (int) string::npos ;
  int best_first_i = (int) string::npos ;
  int best_first_j = (int) string::npos ;
  string best_label = "" ;
Mikaël Salson's avatar
Mikaël Salson committed
502
  vector<pair<int, int> > score_r;
Mikaël Salson's avatar
Mikaël Salson committed
503 504 505 506 507 508 509 510 511 512 513

  DynProg::DynProgMode dpMode = DynProg::LocalEndWithSomeDeletions;
  if (local==true) dpMode = DynProg::Local;
  
  for (int r = 0 ; r < rep.size() ; r++)
    {
      DynProg dp = DynProg(read, rep.sequence(r),
			   dpMode, // DynProg::SemiGlobalTrans, 
			   segment_cost, // DNA
			   reverse_both, reverse_both);
      int score = dp.compute();
Mikaël Salson's avatar
Mikaël Salson committed
514
      
Mikaël Salson's avatar
Mikaël Salson committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528
      if (local==true){ 
	dp.backtrack();
      }
      
      if (score > best_score)
	{
	  best_score = score ;
	  best_best_i = dp.best_i ;
	  best_best_j = dp.best_j ;
	  best_first_i = dp.first_i ;
	  best_first_j = dp.first_j ;
	  best_r = r ;
	  best_label = rep.label(r) ;
	}
Mikaël Salson's avatar
Mikaël Salson committed
529 530
	
	score_r.push_back(make_pair(score, r));
Mathieu Giraud's avatar
Mathieu Giraud committed
531 532 533 534 535 536

	// #define DEBUG_SEGMENT      

#ifdef DEBUG_SEGMENT	
	cout << rep.label(r) << " " << score << " " << dp.best_i << endl ;
#endif
Mikaël Salson's avatar
Mikaël Salson committed
537 538

    }
Mikaël Salson's avatar
Mikaël Salson committed
539
    sort(score_r.begin(),score_r.end(),comp_pair);
Mikaël Salson's avatar
Mikaël Salson committed
540 541 542 543

  *del = reverse_both ? best_best_j : rep.sequence(best_r).size() - best_best_j - 1;
  *del2 = best_first_j;
  *begin = best_first_i;
Mathieu Giraud's avatar
Mathieu Giraud committed
544
  *tag = best_r ; 
Mikaël Salson's avatar
Mikaël Salson committed
545 546
  
  *length -= *del ;
Mikaël Salson's avatar
Mikaël Salson committed
547 548
  
  *score=score_r;
Mathieu Giraud's avatar
Mathieu Giraud committed
549 550 551 552 553 554

#ifdef DEBUG_SEGMENT	
  cout << "best: " << best_labels << " " << best_score ;
  cout << "del/del2/begin:" << (*del) << "/" << (*del2) << "/" << (*begin) << endl;
  cout << endl;
#endif
Mikaël Salson's avatar
Mikaël Salson committed
555 556 557 558 559 560 561 562 563
  
  return best_best_i ;
}

string format_del(int deletions)
{
  return deletions ? *"(" + string_of_int(deletions) + " del)" : "" ;
}

564
FineSegmenter::FineSegmenter(Sequence seq, Germline *germline, Cost segment_c)
Mikaël Salson's avatar
Mikaël Salson committed
565
{
566 567
  segmented = false;
  dSegmented = false;
568
  because = 0 ;
569
  segmented_germline = germline ;
570
  info_extra = "" ;
Mikaël Salson's avatar
Mikaël Salson committed
571 572
  label = seq.label ;
  sequence = seq.sequence ;
Mathieu Giraud's avatar
Mathieu Giraud committed
573
  Dend=0;
Mikaël Salson's avatar
Mikaël Salson committed
574 575
  segment_cost=segment_c;

576 577 578
  CDR3start = -1;
  CDR3end = -1;
  
Mikaël Salson's avatar
Mikaël Salson committed
579 580 581 582 583
  // TODO: factoriser tout cela, peut-etre en lancant deux segmenteurs, un +, un -, puis un qui chapote
  
  // Strand +
  
  int plus_score = 0 ;
Mathieu Giraud's avatar
Mathieu Giraud committed
584
  int tag_plus_V, tag_plus_J;
Mikaël Salson's avatar
Mikaël Salson committed
585 586 587 588
  int plus_length = 0 ;
  int del_plus_V, del_plus_J ;
  int del2=0;
  int beg=0;
Mikaël Salson's avatar
Mikaël Salson committed
589 590 591 592
  
  vector<pair<int, int> > score_plus_V;
  vector<pair<int, int> > score_plus_J;
  
593
  int plus_left = align_against_collection(sequence, germline->rep_5, false, false, &tag_plus_V, &del_plus_V, &del2, &beg, 
Mikaël Salson's avatar
Mikaël Salson committed
594 595
					   &plus_length, &score_plus_V
					   , segment_cost);
596
  int plus_right = align_against_collection(sequence, germline->rep_3, true, false, &tag_plus_J, &del_plus_J, &del2, &beg,
Mikaël Salson's avatar
Mikaël Salson committed
597 598
					    &plus_length, &score_plus_J
					    , segment_cost);
Mikaël Salson's avatar
Mikaël Salson committed
599 600
  plus_length += plus_right - plus_left ;

Mikaël Salson's avatar
Mikaël Salson committed
601 602
  plus_score=score_plus_V[0].first + score_plus_J[0].first ;
  
Mikaël Salson's avatar
Mikaël Salson committed
603 604 605
  // Strand -
  string rc = revcomp(sequence) ;
  int minus_score = 0 ;
Mathieu Giraud's avatar
Mathieu Giraud committed
606
  int tag_minus_V, tag_minus_J;
Mikaël Salson's avatar
Mikaël Salson committed
607 608
  int minus_length = 0 ;
  int del_minus_V, del_minus_J ;
Mikaël Salson's avatar
Mikaël Salson committed
609 610 611 612
  
  vector<pair<int, int> > score_minus_V;
  vector<pair<int, int> > score_minus_J;
  
613
  int minus_left = align_against_collection(rc, germline->rep_5, false, false, &tag_minus_V, &del_minus_V, &del2, &beg,
Mikaël Salson's avatar
Mikaël Salson committed
614 615
					    &minus_length, &score_minus_V
					    ,  segment_cost);
616
  int minus_right = align_against_collection(rc, germline->rep_3, true, false, &tag_minus_J, &del_minus_J, &del2, &beg,
Mikaël Salson's avatar
Mikaël Salson committed
617 618
					     &minus_length, &score_minus_J
					     , segment_cost);
Mikaël Salson's avatar
Mikaël Salson committed
619 620
  minus_length += minus_right - minus_left ;

Mikaël Salson's avatar
Mikaël Salson committed
621 622
  minus_score=score_minus_V[0].first + score_minus_J[0].first ;
  
Mikaël Salson's avatar
Mikaël Salson committed
623 624 625 626
  reversed = (minus_score > plus_score) ;

  if (!reversed)
    {
Mathieu Giraud's avatar
Mathieu Giraud committed
627 628 629 630
      Vend = plus_left ;
      Jstart = plus_right ;
      best_V = tag_plus_V ;
      best_J = tag_plus_J ;
Mikaël Salson's avatar
Mikaël Salson committed
631 632
      del_V = del_plus_V ;
      del_J = del_plus_J ;
Mikaël Salson's avatar
Mikaël Salson committed
633 634
      score_V=score_plus_V;
      score_J=score_plus_J;
Mikaël Salson's avatar
Mikaël Salson committed
635 636 637
    }
  else
    {
Mathieu Giraud's avatar
Mathieu Giraud committed
638 639 640 641
      Vend = minus_left ;
      Jstart = minus_right ;
      best_V = tag_minus_V ;
      best_J = tag_minus_J ;
Mikaël Salson's avatar
Mikaël Salson committed
642 643
      del_V = del_minus_V ;
      del_J = del_minus_J ;
Mikaël Salson's avatar
Mikaël Salson committed
644 645
      score_V=score_minus_V;
      score_J=score_minus_J;
Mikaël Salson's avatar
Mikaël Salson committed
646 647
    }

Mathieu Giraud's avatar
Mathieu Giraud committed
648
  segmented = (Vend != (int) string::npos) && (Jstart != (int) string::npos) && 
649
    (Jstart - Vend >= germline->delta_min) && (Jstart - Vend <= germline->delta_max);
Mikaël Salson's avatar
Mikaël Salson committed
650 651 652
    
  if (!segmented)
    {
653
      because = DONT_KNOW;
Mathieu Giraud's avatar
Mathieu Giraud committed
654
      info = " @" + string_of_int (Vend + FIRST_POS) + "  @" + string_of_int(Jstart + FIRST_POS) ;
655
      
656
      if (Jstart - Vend < germline->delta_min)
657 658 659 660
        {
          because = UNSEG_BAD_DELTA_MIN  ;
        }

661
      if (Jstart - Vend > germline->delta_max)
662 663 664 665 666 667 668 669 670 671 672 673 674 675
        {
          because = UNSEG_BAD_DELTA_MAX  ;
        }
        
      if (Vend == (int) string::npos) 
        {
          because = UNSEG_TOO_FEW_V ;
        }
      
      if (Jstart == (int) string::npos)
        {
          because = UNSEG_TOO_FEW_J ;
        }
      
Mikaël Salson's avatar
Mikaël Salson committed
676 677 678
      return ;
    }
    
679 680
    because = reversed ? SEG_MINUS : SEG_PLUS ;
    
Mikaël Salson's avatar
Mikaël Salson committed
681
    //overlap VJ
Mathieu Giraud's avatar
Mathieu Giraud committed
682
    if(Jstart-Vend <=0){
Mikaël Salson's avatar
Mikaël Salson committed
683
      int b_r, b_l;
Mathieu Giraud's avatar
Mathieu Giraud committed
684
      int overlap=Vend-Jstart+1;
Mikaël Salson's avatar
Mikaël Salson committed
685
      
Mathieu Giraud's avatar
Mathieu Giraud committed
686 687
      string seq_left = sequence.substr(0, Vend+1);
      string seq_right = sequence.substr(Jstart);
Mikaël Salson's avatar
Mikaël Salson committed
688 689

      best_align(overlap, seq_left, seq_right, 
690
		 germline->rep_5.sequence(best_V), germline->rep_3.sequence(best_J), &b_r,&b_l, segment_cost);
Mikaël Salson's avatar
Mikaël Salson committed
691
      // Trim V
Mathieu Giraud's avatar
Mathieu Giraud committed
692
      Vend -= b_l;
Mikaël Salson's avatar
Mikaël Salson committed
693 694 695
      del_V += b_l;

      // Trim J
Mathieu Giraud's avatar
Mathieu Giraud committed
696
      Jstart += b_r;
Mikaël Salson's avatar
Mikaël Salson committed
697
      del_J += b_r;
698
      if (Jstart>=(int) sequence.length())
Mathieu Giraud's avatar
Mathieu Giraud committed
699
	  Jstart=sequence.length()-1;
Mikaël Salson's avatar
Mikaël Salson committed
700 701 702
    }

    // string chevauchement = removeChevauchement();
Mikaël Salson's avatar
Mikaël Salson committed
703 704

    /// used only below, then recomputed in finishSegmentation() ;
Mathieu Giraud's avatar
Mathieu Giraud committed
705
    seg_N = revcomp(sequence, reversed).substr(Vend+1, Jstart-Vend-1); 
Mikaël Salson's avatar
Mikaël Salson committed
706

707
  code = germline->rep_5.label(best_V) +
Mikaël Salson's avatar
Mikaël Salson committed
708 709 710 711
    " "+ string_of_int(del_V) + 
    "/" + seg_N + 
    // chevauchement +
    "/" + string_of_int(del_J) +
712
    " " + germline->rep_3.label(best_J); 
Mikaël Salson's avatar
Mikaël Salson committed
713

Mikaël Salson's avatar
Mikaël Salson committed
714
    stringstream code_s;
715
   code_s<< germline->rep_5.label(best_V) <<
Mikaël Salson's avatar
Mikaël Salson committed
716 717 718 719
    " -" << string_of_int(del_V) << "/" 
    << seg_N.size()
    // chevauchement +
    << "/-" << string_of_int(del_J)
720
    <<" " << germline->rep_3.label(best_J);
Mikaël Salson's avatar
Mikaël Salson committed
721 722
    code_short=code_s.str();
    
723 724
  code_light = germline->rep_5.label(best_V) +
    "/ " + germline->rep_3.label(best_J); 
Mikaël Salson's avatar
Mikaël Salson committed
725 726

 
Mathieu Giraud's avatar
Mathieu Giraud committed
727
  info = string_of_int(Vend + FIRST_POS) + " " + string_of_int(Jstart + FIRST_POS) ;
Mikaël Salson's avatar
Mikaël Salson committed
728 729 730 731
  finishSegmentation();
}


732
void FineSegmenter::FineSegmentD(Germline *germline){
Mikaël Salson's avatar
Mikaël Salson committed
733 734 735 736
  
  if (segmented){
    
    int end = (int) string::npos ;
Mathieu Giraud's avatar
Mathieu Giraud committed
737
    int tag_D;
Mikaël Salson's avatar
Mikaël Salson committed
738 739 740 741
    int length = 0 ;
    int begin = 0;
    
    // Create a zone where to look for D, adding at most EXTEND_D_ZONE nucleotides at each side
Mathieu Giraud's avatar
Mathieu Giraud committed
742
    int l = Vend - EXTEND_D_ZONE;
Mikaël Salson's avatar
Mikaël Salson committed
743 744 745
    if (l<0) 
      l=0 ;

Mathieu Giraud's avatar
Mathieu Giraud committed
746
    int r = Jstart + EXTEND_D_ZONE;
Mikaël Salson's avatar
Mikaël Salson committed
747 748 749 750 751 752 753

    if (r > (int)getSequence().sequence.length()) 
      r = getSequence().sequence.length();
      
    string str = getSequence().sequence.substr(l, r-l);

    // Align
754
    end = align_against_collection(str, germline->rep_4, false, true, &tag_D, &del_D_right, &del_D_left, &begin,
Mikaël Salson's avatar
Mikaël Salson committed
755 756
				&length, &score_D, segment_cost);
    
Mathieu Giraud's avatar
Mathieu Giraud committed
757
    best_D = tag_D;
Mikaël Salson's avatar
Mikaël Salson committed
758
    
Mathieu Giraud's avatar
Mathieu Giraud committed
759 760
    Dstart = l + begin;
    Dend = l + end;
Mikaël Salson's avatar
Mikaël Salson committed
761 762
	
    string seq = getSequence().sequence;
763 764 765 766 767 768 769 770 771 772


    // recompute remaining length for D
    length = germline->rep_4.sequence(best_D).length() - del_D_right - del_D_left;

    if (length < MIN_D_LENGTH)
      return ;


    dSegmented=true;
Mikaël Salson's avatar
Mikaël Salson committed
773 774
    
    //overlap VD
Mathieu Giraud's avatar
Mathieu Giraud committed
775
    if(Dstart-Vend <=0){
Mikaël Salson's avatar
Mikaël Salson committed
776
      int b_r, b_l;
Mathieu Giraud's avatar
Mathieu Giraud committed
777 778 779
      int overlap=Vend-Dstart+1;
      string seq_left = seq.substr(0, Vend+1);
      string seq_right = seq.substr(Dstart, Dend-Dstart+1);
Mikaël Salson's avatar
Mikaël Salson committed
780 781

      best_align(overlap, seq_left, seq_right, 
782
		 germline->rep_5.sequence(best_V), germline->rep_4.sequence(best_D), &b_r,&b_l, segment_cost);
Mikaël Salson's avatar
Mikaël Salson committed
783 784

      // Trim V
Mathieu Giraud's avatar
Mathieu Giraud committed
785
      Vend -= b_l;
Mikaël Salson's avatar
Mikaël Salson committed
786 787 788
      del_V += b_l;

      // Trim D
Mathieu Giraud's avatar
Mathieu Giraud committed
789
      Dstart += b_r;
Mikaël Salson's avatar
Mikaël Salson committed
790
    }
Mathieu Giraud's avatar
Mathieu Giraud committed
791
    seg_N1 = seq.substr(Vend+1, Dstart-Vend-1) ; // From Vend+1 to Dstart-1
Mikaël Salson's avatar
Mikaël Salson committed
792 793
    
    //overlap DJ
Mathieu Giraud's avatar
Mathieu Giraud committed
794
    if(Jstart-Dend <=0){
Mikaël Salson's avatar
Mikaël Salson committed
795
      int b_r, b_l;
Mathieu Giraud's avatar
Mathieu Giraud committed
796 797 798
      int overlap=Dend-Jstart+1;
      string seq_right = seq.substr(Dstart, Dend-Dstart+1);
      string seq_left = seq.substr(Jstart, seq.length()-Jstart);
Mikaël Salson's avatar
Mikaël Salson committed
799 800

      best_align(overlap, seq_left, seq_right, 
801
		 germline->rep_4.sequence(best_D), germline->rep_3.sequence(best_J), &b_r,&b_l, segment_cost);
Mikaël Salson's avatar
Mikaël Salson committed
802 803

      // Trim D
Mathieu Giraud's avatar
Mathieu Giraud committed
804
      Dend -= b_l;
Mikaël Salson's avatar
Mikaël Salson committed
805 806

      // Trim J
Mathieu Giraud's avatar
Mathieu Giraud committed
807
      Jstart += b_r;
Mikaël Salson's avatar
Mikaël Salson committed
808 809 810
      del_J += b_r;

    }
Mathieu Giraud's avatar
Mathieu Giraud committed
811
    seg_N2 = seq.substr(Dend+1, Jstart-Dend-1) ; // From Dend+1 to right-1
812
    code = germline->rep_5.label(best_V) +
Mikaël Salson's avatar
Mikaël Salson committed
813 814 815 816
    " "+ string_of_int(del_V) + 
    "/" + seg_N1 + 
    
    "/" + string_of_int(del_D_left) +
817
    " " + germline->rep_4.label(best_D) +
Mikaël Salson's avatar
Mikaël Salson committed
818 819 820 821
    " " + string_of_int(del_D_right) +
    
    "/" + seg_N2 +
    "/" + string_of_int(del_J) +
822
    " " + germline->rep_3.label(best_J); 
Mikaël Salson's avatar
Mikaël Salson committed
823

Mikaël Salson's avatar
Mikaël Salson committed
824
    stringstream code_s;
825
    code_s << germline->rep_5.label(best_V) 
Mikaël Salson's avatar
Mikaël Salson committed
826 827 828 829
    << " -" << string_of_int(del_V) << "/" 
    << seg_N1.size()
    
    << "/-" << string_of_int(del_D_left) 
830
    << " " << germline->rep_4.label(best_D) 
Mikaël Salson's avatar
Mikaël Salson committed
831 832 833 834
    << " -" << string_of_int(del_D_right) << "/"
    
    << seg_N2.size()
    << "/-" << string_of_int(del_J) 
835
    << " " << germline->rep_3.label(best_J);
Mikaël Salson's avatar
Mikaël Salson committed
836 837 838
    code_short=code_s.str();
    
    
839 840 841
    code_light = germline->rep_5.label(best_V) +
    "/ " + germline->rep_4.label(best_D) +
    "/ " + germline->rep_3.label(best_J); 
Mikaël Salson's avatar
Mikaël Salson committed
842 843 844 845
    
    finishSegmentationD();
  }
}
Mikaël Salson's avatar
Mikaël Salson committed
846

Marc Duez's avatar
Marc Duez committed
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
void FineSegmenter::findCDR3(){
    string str = getSequence().sequence;
    
    list<string> codon_start;
    codon_start.push_back("TGT");
    codon_start.push_back("TGC");
    
    list<string> codon_end;
    codon_end.push_back("TTT");
    codon_end.push_back("TTC");
    codon_end.push_back("TGG");
    
    list<int> p_start;
    list<int> p_end;

    size_t loc;
    std::list<string>::const_iterator it;
    for (it = codon_start.begin(); it != codon_start.end(); ++it) {//filter 1 : start codon must be in V
        loc = 0;
866
        while ( loc != string::npos && loc < (size_t)Vend){
Marc Duez's avatar
Marc Duez committed
867
            loc = str.find(*it, loc+3);
868
            if (loc != string::npos && loc < (size_t)Vend) {
Marc Duez's avatar
Marc Duez committed
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
                p_start.push_front(loc);
            }
        }
    }

    for (it = codon_end.begin(); it != codon_end.end(); ++it) {//filter 2 : end codon must be in J
        loc = Jstart;
        while ( loc != string::npos){
            loc = str.find(*it, loc+3);
            if (loc != string::npos) {
                p_end.push_back(loc);
            }
        }
    }

884 885
    CDR3start = -1;
    CDR3end = -1;
Marc Duez's avatar
Marc Duez committed
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
    
    std::list<int>::const_iterator it1;
    for (it1 = p_start.begin(); it1 != p_start.end(); ++it1) {
        
        std::list<int>::const_iterator it2;
        for (it2 = p_end.begin(); it2 != p_end.end(); ++it2) {
            
            if ( (*it2-*it1)%3 == 0){       //filter 3 : start/stop codon must be seprated by a multiple of 3
                
                if ( fabs((*it2-*it1)-36 ) < fabs((CDR3end-CDR3start)-36) ){ //filter 4 : cdr3 length must be close to 12 AA
                    CDR3start = *it1;
                    CDR3end = *it2;
                }
            }
        }
    }
    
}

905
void FineSegmenter::toJsonList(JsonList *seg){
Tatiana Rocher's avatar
Tatiana Rocher committed
906

Mathieu Giraud's avatar
Mathieu Giraud committed
907
  if (isSegmented()) {
908 909 910
    seg->add("5", segmented_germline->rep_5.label(best_V));
    seg->add("5start", 0);
    seg->add("5end", Vend);
Mathieu Giraud's avatar
Mathieu Giraud committed
911 912
    
    if (score_D.size()>0){
913 914 915
      seg->add("4", segmented_germline->rep_4.label(best_D));
      seg->add("4start", Dstart);
      seg->add("4end", Dend);
Mikaël Salson's avatar
Mikaël Salson committed
916
    }
Mathieu Giraud's avatar
Mathieu Giraud committed
917
    
918 919
    seg->add("3", segmented_germline->rep_3.label(best_J));
    seg->add("3start", Jstart);
920 921 922

    if (CDR3start >= 0)
      {
Marc Duez's avatar
Marc Duez committed
923 924 925 926
    JsonList *json_cdr;
    json_cdr=new JsonList();
    json_cdr->add("start", CDR3start);
    json_cdr->add("stop", CDR3end);
927
    seg->add("cdr3", *json_cdr);
928
      }
Tatiana Rocher's avatar
Tatiana Rocher committed
929

930 931
  }
}
932

933 934
void KmerSegmenter::toJsonList(JsonList *seg)
{
935 936 937 938 939 940
    int sequenceSize = sequence.size();

    JsonList *json_affectValues;
    json_affectValues=new JsonList();
    json_affectValues->add("start", 0);
    json_affectValues->add("stop", sequenceSize); 
941 942
    json_affectValues->add("seq", getKmerAffectAnalyser()->toStringValues());
    seg->add("affectValues", *json_affectValues);
Tatiana Rocher's avatar
Tatiana Rocher committed
943 944
      

945
    JsonList *json_affectSigns;
946 947 948
    json_affectSigns=new JsonList();
    json_affectSigns->add("start", 0);
    json_affectSigns->add("stop", sequenceSize); 
949 950
    json_affectSigns->add("seq", getKmerAffectAnalyser()->toStringSigns());
    seg->add("affectSigns", *json_affectSigns);
951 952 953

    delete json_affectValues;
    delete json_affectSigns;
Mikaël Salson's avatar
Mikaël Salson committed
954 955 956
}