segment.h 9.57 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 7 8 9
#include <iostream>
#include "fasta.h"
#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

#define EXTEND_D_ZONE 5
17

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

22 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
                                       stranf will be marked as STRAND_NOT_CONSISTEN */

26 27 28 29 30
#define DETECT_THRESHOLD 5      /* This threshold only affects the classification of unsegmented sequences.
                                   If the numbers of both V and J affectations are above this threshold,
                                   then the unsegmented sequence will be marked as AMBIGUOUS.
                                   Otherwise, if the number of either V or J affectations if above this threshold,
                                   then the unsegmented sequence will be marked as either ONLY V or ONLY J. */
31

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

34
#define BAD_EVALUE  1e10
35

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

39 40 41 42
#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). */

43

44

Mikaël Salson's avatar
Mikaël Salson committed
45
using namespace std;
Marc Duez's avatar
Marc Duez committed
46
using json = nlohmann::json;
Mikaël Salson's avatar
Mikaël Salson committed
47

48 49 50 51
enum SEGMENTED { NOT_PROCESSED,
		 TOTAL_SEG_AND_WINDOW,
                 SEG_PLUS, SEG_MINUS,
                 UNSEG_TOO_SHORT, UNSEG_STRAND_NOT_CONSISTENT,
52
		 UNSEG_TOO_FEW_ZERO,  UNSEG_TOO_FEW_J, UNSEG_TOO_FEW_V,
53
                 UNSEG_BAD_DELTA_MIN, UNSEG_AMBIGUOUS,
54
		 UNSEG_TOO_SHORT_FOR_WINDOW,
55

Mikaël Salson's avatar
Mikaël Salson committed
56
		 STATS_SIZE } ;
57 58 59

#define STATS_FIRST_UNSEG UNSEG_TOO_SHORT

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

69 70


71 72 73 74
/**
 * An alignment box (AlignBox) gather all parameters for a recombined gene segment (V, D, J, other D...)
 **/

75 76 77
class AlignBox
{
 public:
78 79
  string key;

80 81 82 83 84
  int del_left;
  int start;
  int end;
  int del_right;

85
  AlignBox(string key = "");
86
  string getSequence(string sequence);
87
  void addToJson(json &seg);
88 89

  /* Identifier, label and sequence of the reference sequence (the best one) */
90 91 92 93
  int ref_nb;
  string ref_label;
  string ref;

94 95
  /* Identifiers and scores of other possible reference sequence */
  vector<pair<int, int> > score;
96 97
};

98
ostream &operator<<(ostream &out, const AlignBox &box);
99

100
/**
101
 * Check whether there is an overlap between two boxes,
102 103 104
 * 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
105
 * @param *box_left, *box_right            the two boxes
106 107
 * @param segment_cost:                    the cost used by the dynamic programing
 *
108 109
 * @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
110 111 112 113 114
 *
 * @return                                 the N segment
 */

string check_and_resolve_overlap(string seq, int seq_begin, int seq_end,
115 116
                                 AlignBox *box_left, AlignBox *box_right,
                                 Cost segment_cost);
117

Mikaël Salson's avatar
Mikaël Salson committed
118 119 120
class Segmenter {
protected:
  string sequence;
121
  string sequence_or_rc;
122 123

  int JUNCTIONstart, JUNCTIONend;
Marc Duez's avatar
Marc Duez committed
124
  int CDR3start, CDR3end;
125

Mikaël Salson's avatar
Mikaël Salson committed
126
  bool reversed, segmented, dSegmented;
127
  int because;
Mikaël Salson's avatar
Mikaël Salson committed
128

129 130 131 132 133 134
  /**
   * Compares evalue_left, evalue_right and evalue against the provided threshold
   * @post some evalue is above the threshold ==> because is set to UNSEG_TOO_FEW_ZERO, UNSEG_TOO_FEW_V or UNSEG_TOO_FEW_J
   */
  void checkLeftRightEvaluesThreshold(double threshold, int strand);

Mikaël Salson's avatar
Mikaël Salson committed
135 136 137 138 139
  string removeChevauchement();
  bool finishSegmentation();
  bool finishSegmentationD();

 public:
140
  Germline *segmented_germline;
141
  string label;
Mikaël Salson's avatar
Mikaël Salson committed
142
  string code;
143 144
  string info;        // .vdj.fa header, fixed fields
  string info_extra;  // .vdj.fa header, other information, at the end of the header
145
  string seg_V, seg_N, seg_J, system;
146

147 148
  AlignBox *box_V, *box_D, *box_J;

149
  double evalue;
150 151
  double evalue_left;
  double evalue_right;
Mikaël Salson's avatar
Mikaël Salson committed
152 153 154
  string seg_N1, seg_D, seg_N2;
  Cost segment_cost;

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

Mathieu Giraud's avatar
Mathieu Giraud committed
157
  /* Queries */
Mikaël Salson's avatar
Mikaël Salson committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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



  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.
   */
  string getJunction(int l) const;

  /**
   * @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;

209 210 211 212 213 214 215 216 217
  /**
   * @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
218

219 220 221 222 223
  /**
   * @post status == SEG_PLUS || status == SEG_MINUS <==> isSegmented()
   */
  void setSegmentationStatus(int status);

Mikaël Salson's avatar
Mikaël Salson committed
224 225 226 227 228 229 230 231 232 233 234
  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
235
 private:
236
  KmerAffectAnalyser *kaa;
Mikaël Salson's avatar
Mikaël Salson committed
237 238 239 240
 protected:
  string affects;

 public:
241
  int score;
242 243
  int pvalue_left;
  int pvalue_right;
244
  KmerAffect before, after;
245 246

  KmerSegmenter();
Mikaël Salson's avatar
Mikaël Salson committed
247 248 249
  /**
   * Build a segmenter based on KmerSegmentation
   * @param seq: An object read from a FASTA/FASTQ file
250
   * @param germline: the germline
Mikaël Salson's avatar
Mikaël Salson committed
251
   */
252
  KmerSegmenter(Sequence seq, Germline *germline, double threshold = THRESHOLD_NB_EXPECTED, int multiplier=1);
Mathieu Giraud's avatar
Mathieu Giraud committed
253

254 255
  KmerSegmenter(const KmerSegmenter &seg);

Mathieu Giraud's avatar
Mathieu Giraud committed
256 257 258 259 260
  ~KmerSegmenter();

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

263
  string getInfoLineWithAffects() const;
Marc Duez's avatar
Marc Duez committed
264
  json toJson();
265

Mathieu Giraud's avatar
Mathieu Giraud committed
266
 private:
267 268
  void computeSegmentation(int strand, KmerAffect left, KmerAffect right,
                           double threshold, int multiplier);
Mikaël Salson's avatar
Mikaël Salson committed
269 270
};

271 272 273

class KmerMultiSegmenter
{
274 275
 private:
  double threshold_nb_expected;
276 277 278
 public:
  /**
   * @param seq: An object read from a FASTA/FASTQ file
279 280 281 282
   * @param multigermline: the multigerm
   * @param threshold: threshold of randomly expected segmentation
   */
  KmerMultiSegmenter(Sequence seq, MultiGermline *multigermline, ostream *out_unsegmented,
283
                     double threshold = THRESHOLD_NB_EXPECTED, int nb_reads_for_evalue = 1);
284

285 286
  ~KmerMultiSegmenter();

287
  KmerSegmenter *the_kseg;
288
  MultiGermline *multi_germline;
289 290 291
};


Mikaël Salson's avatar
Mikaël Salson committed
292 293 294
class FineSegmenter : public Segmenter
{
 public:
Mikaël Salson's avatar
Mikaël Salson committed
295 296 297
   vector<pair<int, int> > score_V;
   vector<pair<int, int> > score_D;
   vector<pair<int, int> > score_J;
298 299

   vector <AlignBox*> boxes ;
Mikaël Salson's avatar
Mikaël Salson committed
300
   
Mikaël Salson's avatar
Mikaël Salson committed
301 302 303
   /**
   * Build a fineSegmenter based on KmerSegmentation
   * @param seq: An object read from a FASTA/FASTQ file
304
   * @param germline: germline used
Mikaël Salson's avatar
Mikaël Salson committed
305
   */
306 307
   FineSegmenter(Sequence seq, Germline *germline, Cost segment_cost,
                 double threshold = THRESHOLD_NB_EXPECTED, int multiplier=1);
308 309 310

   ~FineSegmenter();

Mikaël Salson's avatar
Mikaël Salson committed
311 312
  /**
  * extend segmentation from VJ to VDJ
313
  * @param germline: germline used
Mikaël Salson's avatar
Mikaël Salson committed
314
  */
315
  void FineSegmentD(Germline *germline, bool several_D,
316
                    double threshold = THRESHOLD_NB_EXPECTED_D, int multiplier=1);
317 318

  bool FineSegmentD(Germline *germline,
319
                    AlignBox *box_Y, AlignBox *box_DD, AlignBox *box_Z,
320
                    int forbidden_id,
321
                    int extend_DD_on_Y, int extend_DD_on_Z,
322 323
                    double threshold = THRESHOLD_NB_EXPECTED_D, int multiplier=1);

Marc Duez's avatar
Marc Duez committed
324
  void findCDR3();
Mathieu Giraud's avatar
Mathieu Giraud committed
325

Marc Duez's avatar
Marc Duez committed
326
  json toJson();
Mikaël Salson's avatar
Mikaël Salson committed
327
  
Mikaël Salson's avatar
Mikaël Salson committed
328 329 330 331 332
};



#endif