Mise à jour terminée. Pour connaître les apports de la version 13.8.4 par rapport à notre ancienne version vous pouvez lire les "Release Notes" suivantes :
https://about.gitlab.com/releases/2021/02/11/security-release-gitlab-13-8-4-released/
https://about.gitlab.com/releases/2021/02/05/gitlab-13-8-3-released/

segment.h 7.96 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
#define DETECT_THRESHOLD 5      /* If the number of both V and J affectations
27 28
                                   is above this threshold, then the sequence,
                                   if it not segmented, will be marked as AMBIGUOUS */
29

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

32
#define BAD_EVALUE  1e10
33

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


38

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

42 43 44 45
enum SEGMENTED { NOT_PROCESSED,
		 TOTAL_SEG_AND_WINDOW,
                 SEG_PLUS, SEG_MINUS,
                 UNSEG_TOO_SHORT, UNSEG_STRAND_NOT_CONSISTENT,
46
		 UNSEG_TOO_FEW_ZERO,  UNSEG_TOO_FEW_J, UNSEG_TOO_FEW_V,
47
                 UNSEG_BAD_DELTA_MIN, UNSEG_AMBIGUOUS,
48
		 UNSEG_TOO_SHORT_FOR_WINDOW,
49

Mikaël Salson's avatar
Mikaël Salson committed
50
		 STATS_SIZE } ;
51 52 53 54
const char* const segmented_mesg[] = { "?",
                                       "SEG",
                                       "SEG_+", "SEG_-",
                                       "UNSEG too short", "UNSEG strand",
55
				       "UNSEG too few V/J", "UNSEG only V", "UNSEG only J",
56
				       "UNSEG < delta_min", "UNSEG ambiguous",
57
                                       "UNSEG too short w",
Mikaël Salson's avatar
Mikaël Salson committed
58
                                      } ;
Mikaël Salson's avatar
Mikaël Salson committed
59

60 61 62 63 64 65 66 67



 /**
  * Find the best split point when there may be a overlap between a left and a right region
  * @param overlap: the length of the overlap
  * @param seq_left, seq_right: the two sequences that overlap
  * @param ref_left, ref_right: the two reference sequences
68
  * @param pos_seq_left, pos_seq_right: the initial end/start position of two overlaping sequences
69
  * @param segment_cost: dp cost
70 71 72 73
  *
  * @post  trim_ref_left (at the end of ref_left) and trim_ref_right (at the beginning of ref_right)
  *        are set to the best number of nucleotides to trim in order to remove the overlap
  *        pos_seq_left and pos_seq_right are shifted by the good number of nucleotides (relatively to seq_{left,right})
74 75 76 77
  */

void best_overlap_split(int overlap, string seq_left, string seq_right,
                        string ref_left, string ref_right,
78 79
                        int *pos_seq_left, int *pos_seq_right,
                        int *trim_ref_left, int *trim_ref_right, Cost segment_cost);
80

Mikaël Salson's avatar
Mikaël Salson committed
81 82 83
class Segmenter {
protected:
  string sequence;
Mathieu Giraud's avatar
Mathieu Giraud committed
84 85
  int Vend, Jstart;
  int Dstart, Dend;
Marc Duez's avatar
Marc Duez committed
86
  int CDR3start, CDR3end;
Mikaël Salson's avatar
Mikaël Salson committed
87
  bool reversed, segmented, dSegmented;
88
  int because;
Mikaël Salson's avatar
Mikaël Salson committed
89

90 91 92 93 94 95
  /**
   * 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
96 97 98 99 100
  string removeChevauchement();
  bool finishSegmentation();
  bool finishSegmentationD();

 public:
101
  Germline *segmented_germline;
102
  string label;
Mikaël Salson's avatar
Mikaël Salson committed
103
  string code;
Mikaël Salson's avatar
Mikaël Salson committed
104
  string code_short;
Mikaël Salson's avatar
Mikaël Salson committed
105
  string code_light;
106 107
  string info;        // .vdj.fa header, fixed fields
  string info_extra;  // .vdj.fa header, other information, at the end of the header
Mikaël Salson's avatar
Mikaël Salson committed
108 109
  int best_V, best_J ;
  int del_V, del_D_left, del_D_right, del_J ;
110
  string seg_V, seg_N, seg_J, system;
111

Mikaël Salson's avatar
Mikaël Salson committed
112
  int best_D;
113
  double evalue;
114 115
  double evalue_left;
  double evalue_right;
Mikaël Salson's avatar
Mikaël Salson committed
116 117 118
  string seg_N1, seg_D, seg_N2;
  Cost segment_cost;

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

Mathieu Giraud's avatar
Mathieu Giraud committed
121
  /* Queries */
Mikaël Salson's avatar
Mikaël Salson committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172



  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;

173 174 175 176 177 178 179 180 181
  /**
   * @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
182

183 184 185 186 187
  /**
   * @post status == SEG_PLUS || status == SEG_MINUS <==> isSegmented()
   */
  void setSegmentationStatus(int status);

Mikaël Salson's avatar
Mikaël Salson committed
188 189 190 191 192 193 194 195 196 197 198
  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
199
 private:
200
  KmerAffectAnalyser *kaa;
Mikaël Salson's avatar
Mikaël Salson committed
201 202 203 204
 protected:
  string affects;

 public:
205
  int score;
206 207
  int pvalue_left;
  int pvalue_right;
208
  KmerAffect before, after;
209 210

  KmerSegmenter();
Mikaël Salson's avatar
Mikaël Salson committed
211 212 213
  /**
   * Build a segmenter based on KmerSegmentation
   * @param seq: An object read from a FASTA/FASTQ file
214
   * @param germline: the germline
Mikaël Salson's avatar
Mikaël Salson committed
215
   */
216
  KmerSegmenter(Sequence seq, Germline *germline, double threshold = THRESHOLD_NB_EXPECTED, int multiplier=1);
Mathieu Giraud's avatar
Mathieu Giraud committed
217

218 219
  KmerSegmenter(const KmerSegmenter &seg);

Mathieu Giraud's avatar
Mathieu Giraud committed
220 221 222 223 224
  ~KmerSegmenter();

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

227
  string getInfoLineWithAffects() const;
Marc Duez's avatar
Marc Duez committed
228
  json toJson();
229

Mathieu Giraud's avatar
Mathieu Giraud committed
230
 private:
231 232
  void computeSegmentation(int strand, KmerAffect left, KmerAffect right,
                           double threshold, int multiplier);
Mikaël Salson's avatar
Mikaël Salson committed
233 234
};

235 236 237

class KmerMultiSegmenter
{
238 239
 private:
  double threshold_nb_expected;
240 241 242
 public:
  /**
   * @param seq: An object read from a FASTA/FASTQ file
243 244 245 246
   * @param multigermline: the multigerm
   * @param threshold: threshold of randomly expected segmentation
   */
  KmerMultiSegmenter(Sequence seq, MultiGermline *multigermline, ostream *out_unsegmented,
247
                     double threshold = THRESHOLD_NB_EXPECTED, int nb_reads_for_evalue = 1);
248

249 250
  ~KmerMultiSegmenter();

251
  KmerSegmenter *the_kseg;
252
  MultiGermline *multi_germline;
253 254 255
};


Mikaël Salson's avatar
Mikaël Salson committed
256 257 258
class FineSegmenter : public Segmenter
{
 public:
Mikaël Salson's avatar
Mikaël Salson committed
259 260 261 262
   vector<pair<int, int> > score_V;
   vector<pair<int, int> > score_D;
   vector<pair<int, int> > score_J;
   
Mikaël Salson's avatar
Mikaël Salson committed
263 264 265
   /**
   * Build a fineSegmenter based on KmerSegmentation
   * @param seq: An object read from a FASTA/FASTQ file
266
   * @param germline: germline used
Mikaël Salson's avatar
Mikaël Salson committed
267
   */
268 269
   FineSegmenter(Sequence seq, Germline *germline, Cost segment_cost,
                 double threshold = THRESHOLD_NB_EXPECTED, int multiplier=1);
Mikaël Salson's avatar
Mikaël Salson committed
270 271 272
  
  /**
  * extend segmentation from VJ to VDJ
273
  * @param germline: germline used
Mikaël Salson's avatar
Mikaël Salson committed
274
  */
275 276
  void FineSegmentD(Germline *germline,
                    double threshold = THRESHOLD_NB_EXPECTED_D, int multiplier=1);
Marc Duez's avatar
Marc Duez committed
277
  void findCDR3();
Mathieu Giraud's avatar
Mathieu Giraud committed
278

Marc Duez's avatar
Marc Duez committed
279
  json toJson();
Mikaël Salson's avatar
Mikaël Salson committed
280
  
Mikaël Salson's avatar
Mikaël Salson committed
281 282 283 284 285
};



#endif