segment.h 4.94 KB
Newer Older
Mikaël Salson's avatar
Mikaël Salson committed
1 2 3 4 5 6 7 8
#ifndef SEGMENT_H
#define SEGMENT_H

#include <string>
#include <iostream>
#include "fasta.h"
#include "dynprog.h"
#include "tools.h"
9
#include "germline.h"
Mikaël Salson's avatar
Mikaël Salson committed
10 11
#include "kmerstore.h"
#include "kmeraffect.h"
Mathieu Giraud's avatar
Mathieu Giraud committed
12 13
#include "affectanalyser.h"
#include "json.h"
Mikaël Salson's avatar
Mikaël Salson committed
14 15

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

20 21 22 23
#define AMBIGUOUS_THRESHOLD 4   /* If the number of both V and J affectations
                                   is above this threshold, then a non-segmented
                                   sequence will be labeled as AMBIGUOUS */

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

Mikaël Salson's avatar
Mikaël Salson committed
26 27 28 29
using namespace std;

enum SEGMENTED { DONT_KNOW, SEG_PLUS, SEG_MINUS, UNSEG_TOO_SHORT, UNSEG_STRAND_NOT_CONSISTENT, 
		 UNSEG_TOO_FEW_ZERO,  UNSEG_TOO_FEW_V, UNSEG_TOO_FEW_J, 
30
		 UNSEG_BAD_DELTA_MIN, UNSEG_BAD_DELTA_MAX, UNSEG_AMBIGUOUS,
Mikaël Salson's avatar
Mikaël Salson committed
31 32 33 34
		 TOTAL_SEG_AND_WINDOW, 
		 TOTAL_SEG_BUT_TOO_SHORT_FOR_THE_WINDOW,
		 STATS_SIZE } ;
const char* const segmented_mesg[] = { "?", "SEG_+", "SEG_-", "UNSEG too short", "UNSEG strand",  
Mikaël Salson's avatar
Mikaël Salson committed
35
				       "UNSEG too few (zero)", "UNSEG too few V", "UNSEG too few J",
36
				       "UNSEG < delta_min", "UNSEG > delta_max", "UNSEG ambiguous",
Mikaël Salson's avatar
Mikaël Salson committed
37 38 39
                                       "= SEG, with window",
                                       "= SEG, but no window",
                                      } ;
Mikaël Salson's avatar
Mikaël Salson committed
40 41 42 43 44

class Segmenter {
protected:
  string label;
  string sequence;
Mathieu Giraud's avatar
Mathieu Giraud committed
45 46
  int Vend, Jstart;
  int Dstart, Dend;
Mikaël Salson's avatar
Mikaël Salson committed
47 48 49 50 51 52 53 54
  bool reversed, segmented, dSegmented;

  string removeChevauchement();
  bool finishSegmentation();
  bool finishSegmentationD();

 public:
  string code;
Mikaël Salson's avatar
Mikaël Salson committed
55
  string code_short;
Mikaël Salson's avatar
Mikaël Salson committed
56
  string code_light;
57 58
  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
59 60
  int best_V, best_J ;
  int del_V, del_D_left, del_D_right, del_J ;
61
  string seg_V, seg_N, seg_J, system;
62

Mikaël Salson's avatar
Mikaël Salson committed
63 64 65 66
  int best_D;
  string seg_N1, seg_D, seg_N2;
  Cost segment_cost;

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

Mathieu Giraud's avatar
Mathieu Giraud committed
69
  /* Queries */
Mikaël Salson's avatar
Mikaël Salson committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132



  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;


  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
133 134
 private:
  int because;                  
135
  KmerAffectAnalyser *kaa;
Mikaël Salson's avatar
Mikaël Salson committed
136 137 138 139
 protected:
  string affects;

 public:
140 141
  Germline *segmented_germline;
  
Mikaël Salson's avatar
Mikaël Salson committed
142 143 144
  /**
   * Build a segmenter based on KmerSegmentation
   * @param seq: An object read from a FASTA/FASTQ file
145
   * @param multigermline: the multigermline
Mikaël Salson's avatar
Mikaël Salson committed
146
   */
147
  KmerSegmenter(Sequence seq, MultiGermline *multigermline);
Mathieu Giraud's avatar
Mathieu Giraud committed
148 149 150 151 152 153

  ~KmerSegmenter();

  /**
   * @return the KmerAffectAnalyser of the current sequence.
   */
154
  KmerAffectAnalyser *getKmerAffectAnalyser() const;
Mathieu Giraud's avatar
Mathieu Giraud committed
155 156 157 158 159 160 161 162 163 164

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

 private:
165
  void computeSegmentation(int strand, Germline* germline);
Mikaël Salson's avatar
Mikaël Salson committed
166 167 168 169 170
};

class FineSegmenter : public Segmenter
{
 public:
171
   int because;
Mikaël Salson's avatar
Mikaël Salson committed
172 173 174 175
   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
176 177 178
   /**
   * Build a fineSegmenter based on KmerSegmentation
   * @param seq: An object read from a FASTA/FASTQ file
179
   * @param germline: germline used
Mikaël Salson's avatar
Mikaël Salson committed
180
   */
181
  FineSegmenter(Sequence seq, Germline *germline, Cost segment_cost);
Mikaël Salson's avatar
Mikaël Salson committed
182 183 184
  
  /**
  * extend segmentation from VJ to VDJ
185
  * @param germline: germline used
Mikaël Salson's avatar
Mikaël Salson committed
186
  */
187
  void FineSegmentD(Germline *germline);
Mathieu Giraud's avatar
Mathieu Giraud committed
188

189
  JsonList toJsonList(Germline *germline);
Mikaël Salson's avatar
Mikaël Salson committed
190
  
Mikaël Salson's avatar
Mikaël Salson committed
191 192 193 194 195
};



#endif