segment.h 10.3 KB
Newer Older
Mikaël Salson's avatar
Mikaël Salson committed
1 2 3 4
#ifndef SEGMENT_H
#define SEGMENT_H

#include <string>
5
#include <fstream>
Mikaël Salson's avatar
Mikaël Salson committed
6
#include <iostream>
7
#include "bioreader.hpp"
Mikaël Salson's avatar
Mikaël Salson committed
8 9
#include "dynprog.h"
#include "tools.h"
10
#include "germline.h"
Mikaël Salson's avatar
Mikaël Salson committed
11 12
#include "kmerstore.h"
#include "kmeraffect.h"
Mathieu Giraud's avatar
Mathieu Giraud committed
13
#include "affectanalyser.h"
Marc Duez's avatar
Marc Duez committed
14
#include "../lib/json.hpp"
Mikaël Salson's avatar
Mikaël Salson committed
15

16 17
// #define DEBUG_EVALUE

Mikaël Salson's avatar
Mikaël Salson committed
18
#define EXTEND_D_ZONE 5
19

20
#define RATIO_STRAND 2          /* The ratio between the affectations in one
Mikaël Salson's avatar
Mikaël Salson committed
21 22
                                   strand and the other, to safely attribute a
                                   segment to a given strand */
Mikaël Salson's avatar
Mikaël Salson committed
23

24 25
#define DETECT_THRESHOLD_STRAND 5   /* If the number of total affectations
                                       is above this threshold, then a sequence with no clearly attributed
26
                                       strand will be marked as STRAND_NOT_CONSISTENT */
27

Mathieu Giraud's avatar
Mathieu Giraud committed
28 29
#define JSON_REMEMBER_BEST  4   /* The number of V/D/J predictions to keep  */

30
#define BAD_EVALUE  1e10
31

32
#define THRESHOLD_NB_EXPECTED 1.0 /* Threshold of the accepted expected value for number of found k-mers */
33
#define THRESHOLD_NB_EXPECTED_D  .05 /* e-value threshold, D-REGION */
34

35 36 37 38
#define BOTTOM_TRIANGLE_SHIFT  20   /* Should equal to (max allowed 'k-band') + (max allowed number of V/J deletions) - (min size to recognize facing J/V)
                                       As we need ~10 bp to recognize the facing V/J, this value should be large enough to handle V/J deletions until ~30 bp,
                                       (and even larger V/J deletions if there is a large facing J or V in the read). */

39

40

Mikaël Salson's avatar
Mikaël Salson committed
41
using namespace std;
Marc Duez's avatar
Marc Duez committed
42
using json = nlohmann::json;
Mikaël Salson's avatar
Mikaël Salson committed
43

44 45 46 47
enum SEGMENTED { NOT_PROCESSED,
		 TOTAL_SEG_AND_WINDOW,
                 SEG_PLUS, SEG_MINUS,
                 UNSEG_TOO_SHORT, UNSEG_STRAND_NOT_CONSISTENT,
48
		 UNSEG_TOO_FEW_ZERO,  UNSEG_ONLY_V, UNSEG_ONLY_J,
49
                 UNSEG_BAD_DELTA_MIN, UNSEG_AMBIGUOUS,
50
		 UNSEG_TOO_SHORT_FOR_WINDOW,
51

Mikaël Salson's avatar
Mikaël Salson committed
52
		 STATS_SIZE } ;
53 54 55

#define STATS_FIRST_UNSEG UNSEG_TOO_SHORT

56 57 58 59
const char* const segmented_mesg[] = { "?",
                                       "SEG",
                                       "SEG_+", "SEG_-",
                                       "UNSEG too short", "UNSEG strand",
60
				       "UNSEG too few V/J", "UNSEG only V/5'", "UNSEG only J/3'",
61
				       "UNSEG < delta_min", "UNSEG ambiguous",
62
                                       "UNSEG too short w",
Mikaël Salson's avatar
Mikaël Salson committed
63
                                      } ;
Mikaël Salson's avatar
Mikaël Salson committed
64

65 66


67 68 69 70
/**
 * An alignment box (AlignBox) gather all parameters for a recombined gene segment (V, D, J, other D...)
 **/

71 72 73
class AlignBox
{
 public:
74
  string key;
75
  string color;
76

77 78 79 80 81
  int del_left;
  int start;
  int end;
  int del_right;

82
  AlignBox(string key = "", string color="");
83
  string getSequence(string sequence);
84
  void addToJson(json &seg);
85

86 87 88 89 90
  /**
   * Returns 'V', 'D', 'J', or possibly '5', '4', '3', '?', depending on the ref_label and on the key
   */
  char getInitial();

91 92 93 94 95
  /**
   * Returns the length
   */
  int getLength();

96 97 98 99 100 101 102 103 104 105 106 107
  /**
   * Returns the position in the reference string corresponding to the position in the read
   * Preliminary implementation, only works for the start of V and J boxes
   */
  int posInRef(int i);

  /**
   * Format the reference string in a given range position, possibly adding the ANSI colors
   * where there is the alignment
   */
  string refToString(int from, int to);

108
  /* Identifier, label and sequence of the reference sequence (the best one) */
109 110 111 112
  int ref_nb;
  string ref_label;
  string ref;

113 114 115
  /* Marked position, for Cys104 and Phe118/Trp118 */
  int marked_pos;

116 117
  /* Identifiers and scores of other possible reference sequence */
  vector<pair<int, int> > score;
118 119
};

120
ostream &operator<<(ostream &out, const AlignBox &box);
121

122
/**
123
 * Check whether there is an overlap between two boxes,
124 125 126
 * If this is the case, fix this overlap (finding the best split point), and update segmentation accordingly
 * @param seq:                             the read
 * @param seq_begin, seq_end:              the positions to consider on 'seq' for the two sequences that may overlap
127
 * @param *box_left, *box_right            the two boxes
128 129
 * @param segment_cost:                    the cost used by the dynamic programing
 *
130 131
 * @post  box_left->del_left and box_right->del_right are set to the best number of nucleotides to trim in order to remove the overlap.
 *        box_left->end and box_right->start are shifted by the good number of nucleotides
132 133 134 135 136
 *
 * @return                                 the N segment
 */

string check_and_resolve_overlap(string seq, int seq_begin, int seq_end,
137 138
                                 AlignBox *box_left, AlignBox *box_right,
                                 Cost segment_cost);
139

Mikaël Salson's avatar
Mikaël Salson committed
140 141 142
class Segmenter {
protected:
  string sequence;
143
  string sequence_or_rc;
144

145
  // JUNCTIONstart/end and CDR3start/end are 1-based
146
  int JUNCTIONstart, JUNCTIONend;
147
  string JUNCTIONaa;
148
  bool JUNCTIONproductive;
149

Marc Duez's avatar
Marc Duez committed
150
  int CDR3start, CDR3end;
151 152
  string CDR3nuc;
  string CDR3aa;
153

Mikaël Salson's avatar
Mikaël Salson committed
154
  bool reversed, segmented, dSegmented;
155
  int because;
Mikaël Salson's avatar
Mikaël Salson committed
156

157 158
  /**
   * Compares evalue_left, evalue_right and evalue against the provided threshold
159
   * @post some evalue is above the threshold ==> because is set to UNSEG_TOO_FEW_ZERO, UNSEG_ONLY_J or UNSEG_ONLY_V
160 161 162
   */
  void checkLeftRightEvaluesThreshold(double threshold, int strand);

Mikaël Salson's avatar
Mikaël Salson committed
163 164 165 166 167
  string removeChevauchement();
  bool finishSegmentation();
  bool finishSegmentationD();

 public:
168
  Germline *segmented_germline;
169
  string label;
Mikaël Salson's avatar
Mikaël Salson committed
170
  string code;
171 172
  string info;        // .vdj.fa header, fixed fields
  string info_extra;  // .vdj.fa header, other information, at the end of the header
173
  string seg_V, seg_N, seg_J, system;
174

175 176
  AlignBox *box_V, *box_D, *box_J;

177
  double evalue;
178 179
  double evalue_left;
  double evalue_right;
Mikaël Salson's avatar
Mikaël Salson committed
180 181 182
  string seg_N1, seg_D, seg_N2;
  Cost segment_cost;

Mathieu Giraud's avatar
Mathieu Giraud committed
183
  virtual ~Segmenter();
Mikaël Salson's avatar
Mikaël Salson committed
184

Mathieu Giraud's avatar
Mathieu Giraud committed
185
  /* Queries */
Mikaël Salson's avatar
Mikaël Salson committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199



  Sequence getSequence() const ;

  /**
   * @param l: length around the junction
   * @return the string centered on the junction (ie. at position
   *         (getLeft() + getRight())/2).
   *         The string has length l unless the original string 
   *         is not long enough.
   *         The junction is revcomp-ed if the original string comes from reverse
   *         strand.
   */
200
  string getJunction(int l, int shift=0) const;
Mikaël Salson's avatar
Mikaël Salson committed
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 230 231 232 233 234 235 236

  /**
   * @return the left position (on forward strand) of the segmentation.
   */
  int getLeft() const;
  
  /**
   * @return the right position (on forward strand) of the segmentation
   */
  int getRight() const;
  
  /**
   * @return the left position (on forward strand) of the D segmentation.
   */
  int getLeftD() const;
  
  /**
   * @return the right position (on forward strand) of the D segmentation
   */
  int getRightD() const;

  /**
   * @return true iff the string comes from reverse strand
   */
  bool isReverse() const;

  /**
   * @return true iff the sequence has been successfully segmented
   */
  bool isSegmented() const;
  
  /**
   * @return true if a D gene was found in the N region
   */
  bool isDSegmented() const;

237 238 239 240 241 242 243 244 245
  /**
   * @return the status of the segmentation. Tells if the Sequence has been segmented
   *         of if it has not, what the reason is.
   * @assert getSegmentationStatus() == SEG_PLUS || getSegmentationStatus() == SEG_MINUS
   *         <==> isSegmented()
   */
  int getSegmentationStatus() const;

  string getInfoLine() const;
Mikaël Salson's avatar
Mikaël Salson committed
246

247 248 249 250 251
  /**
   * @post status == SEG_PLUS || status == SEG_MINUS <==> isSegmented()
   */
  void setSegmentationStatus(int status);

Mikaël Salson's avatar
Mikaël Salson committed
252 253 254 255 256 257 258 259 260 261 262
  friend ostream &operator<<(ostream &out, const Segmenter &s);
};



ostream &operator<<(ostream &out, const Segmenter &s);



class KmerSegmenter : public Segmenter
{
Mathieu Giraud's avatar
Mathieu Giraud committed
263
 private:
264
  KmerAffectAnalyser *kaa;
Mikaël Salson's avatar
Mikaël Salson committed
265 266 267 268
 protected:
  string affects;

 public:
269
  int score;
270 271
  int pvalue_left;
  int pvalue_right;
272
  KmerAffect before, after;
273 274

  KmerSegmenter();
Mikaël Salson's avatar
Mikaël Salson committed
275 276 277
  /**
   * Build a segmenter based on KmerSegmentation
   * @param seq: An object read from a FASTA/FASTQ file
278
   * @param germline: the germline
Mikaël Salson's avatar
Mikaël Salson committed
279
   */
280
  KmerSegmenter(Sequence seq, Germline *germline, double threshold = THRESHOLD_NB_EXPECTED, double multiplier=1.0);
Mathieu Giraud's avatar
Mathieu Giraud committed
281

282 283
  KmerSegmenter(const KmerSegmenter &seg);

Mathieu Giraud's avatar
Mathieu Giraud committed
284 285 286 287 288
  ~KmerSegmenter();

  /**
   * @return the KmerAffectAnalyser of the current sequence.
   */
289
  KmerAffectAnalyser *getKmerAffectAnalyser() const;
Mathieu Giraud's avatar
Mathieu Giraud committed
290

291
  string getInfoLineWithAffects() const;
Marc Duez's avatar
Marc Duez committed
292
  json toJson();
293

Mathieu Giraud's avatar
Mathieu Giraud committed
294
 private:
295
  void computeSegmentation(int strand, KmerAffect left, KmerAffect right,
296
                           double threshold, double multiplier);
Mikaël Salson's avatar
Mikaël Salson committed
297 298
};

299 300 301

class KmerMultiSegmenter
{
302 303
 private:
  double threshold_nb_expected;
304 305 306
 public:
  /**
   * @param seq: An object read from a FASTA/FASTQ file
307 308 309 310
   * @param multigermline: the multigerm
   * @param threshold: threshold of randomly expected segmentation
   */
  KmerMultiSegmenter(Sequence seq, MultiGermline *multigermline, ostream *out_unsegmented,
311
                     double threshold = THRESHOLD_NB_EXPECTED, int nb_reads_for_evalue = 1);
312

313 314
  ~KmerMultiSegmenter();

315
  KmerSegmenter *the_kseg;
316
  MultiGermline *multi_germline;
317 318 319
};


Mikaël Salson's avatar
Mikaël Salson committed
320 321 322
class FineSegmenter : public Segmenter
{
 public:
Mikaël Salson's avatar
Mikaël Salson committed
323 324 325
   vector<pair<int, int> > score_V;
   vector<pair<int, int> > score_D;
   vector<pair<int, int> > score_J;
326 327

   vector <AlignBox*> boxes ;
Mikaël Salson's avatar
Mikaël Salson committed
328
   
Mikaël Salson's avatar
Mikaël Salson committed
329 330 331
   /**
   * Build a fineSegmenter based on KmerSegmentation
   * @param seq: An object read from a FASTA/FASTQ file
332
   * @param germline: germline used
Mikaël Salson's avatar
Mikaël Salson committed
333
   */
334
   FineSegmenter(Sequence seq, Germline *germline, Cost segment_cost,
335
                 double threshold = THRESHOLD_NB_EXPECTED, double multiplier=1.0);
336 337 338

   ~FineSegmenter();

Mikaël Salson's avatar
Mikaël Salson committed
339 340
  /**
  * extend segmentation from VJ to VDJ
341
  * @param germline: germline used
Mikaël Salson's avatar
Mikaël Salson committed
342
  */
343
  void FineSegmentD(Germline *germline, bool several_D,
344
                    double threshold = THRESHOLD_NB_EXPECTED_D, double multiplier=1.0);
345 346

  bool FineSegmentD(Germline *germline,
347
                    AlignBox *box_Y, AlignBox *box_DD, AlignBox *box_Z,
348
                    int forbidden_id,
349
                    int extend_DD_on_Y, int extend_DD_on_Z,
350
                    double threshold = THRESHOLD_NB_EXPECTED_D, double multiplier=1.0);
351

Mathieu Giraud's avatar
Mathieu Giraud committed
352 353 354 355
  /**
   * find JUNCTION/CDR3, by using marked Cys104 and Phe118/Trp118 positions
   * in the germline V and J genes and the backtrack of the DP matrix
   */
Marc Duez's avatar
Marc Duez committed
356
  void findCDR3();
Mathieu Giraud's avatar
Mathieu Giraud committed
357

Marc Duez's avatar
Marc Duez committed
358
  json toJson();
Mikaël Salson's avatar
Mikaël Salson committed
359
  
Mikaël Salson's avatar
Mikaël Salson committed
360 361 362
};


363
void align_against_collection(string &read, BioReader &rep, int forbidden_rep_id,
364 365
                              bool reverse_ref, bool reverse_both, bool local,
                              AlignBox *box, Cost segment_cost);
Mikaël Salson's avatar
Mikaël Salson committed
366 367

#endif