| 1 |
/***************************************************************************** |
| 2 |
* discoSnp++: discovering polymorphism from raw unassembled NGS reads |
| 3 |
* A tool from the GATB (Genome Assembly Tool Box) |
| 4 |
* Copyright (C) 2014 INRIA |
| 5 |
* Authors: P.Peterlongo, E.Drezen |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Affero General Public License as |
| 9 |
* published by the Free Software Foundation, either version 3 of the |
| 10 |
* License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Affero General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Affero General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*****************************************************************************/ |
| 20 |
|
| 21 |
#ifndef _TOOL_BUBBLE_HPP_ |
| 22 |
#define _TOOL_BUBBLE_HPP_ |
| 23 |
|
| 24 |
/********************************************************************************/ |
| 25 |
#include <gatb/gatb_core.hpp> |
| 26 |
#include <string> |
| 27 |
/********************************************************************************/ |
| 28 |
|
| 29 |
/** We define string constants for command line options. */ |
| 30 |
#define STR_MAX_AMBIGOUS_INDELS "-max_ambigous_indel" |
| 31 |
#define STR_DISCOSNP_LOW_COMPLEXITY "-l" |
| 32 |
#define STR_DISCOSNP_AUTHORISED_BRANCHING "-b" |
| 33 |
#define STR_DISCOSNP_TRAVERSAL_UNITIG "-t" |
| 34 |
#define STR_DISCOSNP_TRAVERSAL_CONTIG "-T" |
| 35 |
#define STR_KISSNP2_COVERAGE_FILE_NAME "-coverage_file" |
| 36 |
#define STR_KISSNP2_DONT_OUTPUT_FIRST_COV "-dont_output_first_coverage" |
| 37 |
|
| 38 |
#define STR_MAX_INDEL_SIZE "-D" |
| 39 |
#define STR_MAX_POLYMORPHISM "-P" |
| 40 |
#define STR_MAX_SYMMETRICAL_CROSSROADS "-max_symmetrical_crossroads" |
| 41 |
#define STR_RADSEQ "-x" |
| 42 |
#define STR_MAX_TRUNCATED_PATH_LENGTH_DIFFERENCE "-max_truncated_path_length_difference" |
| 43 |
|
| 44 |
|
| 45 |
/********************************************************************************/ |
| 46 |
|
| 47 |
/** We define a structure holding all the information about a bubble. */ |
| 48 |
struct Bubble |
| 49 |
{ |
| 50 |
// Here is a description of the attributes defining a bubble. |
| 51 |
// |
| 52 |
// begin end |
| 53 |
// ------X X------ <-- branch1 (X is the common nucleotide between the 2 nodes) |
| 54 |
// ------Y Y------ <-- branch2 (Y is the common nucleotide between the 2 nodes) |
| 55 |
// |
| 56 |
// => the bubble is defined by two branches |
| 57 |
// |
| 58 |
// => a branch is defined by two nodes [begin,end] that overlap on one nucleotide |
| 59 |
// so the path length defined by these two nodes is (2*kmerSize - 1) |
| 60 |
Node begin[2]; |
| 61 |
Node end [2]; |
| 62 |
|
| 63 |
|
| 64 |
// A Bubble may represent a SNP or a deletion (currently). |
| 65 |
// SNP case: |
| 66 |
// ---A |
| 67 |
// A--- |
| 68 |
// and |
| 69 |
// ---T |
| 70 |
// T--- |
| 71 |
// We have 2 upper nodes (begin[0] and end[0]) overlaping by 1 nucleotides |
| 72 |
// We have 2 lower nodes (begin[1] and end[1]) overlaping by 1 nucleotides |
| 73 |
// Thus in the SNP case, size_overlap[0] == size_overlap[1] == 1 |
| 74 |
|
| 75 |
// In the DELETION case: |
| 76 |
// 123456 |
| 77 |
// 123IiiiY456 (I is the insertion here of length 5) |
| 78 |
// The output is : one path: |
| 79 |
// 1234 (k=4) |
| 80 |
// 3456 |
| 81 |
// other path: |
| 82 |
// 123I and Y456 |
| 83 |
// In this case we store |
| 84 |
// begin[0]=1234 end[0]=3456 size_overlap[0]=2 |
| 85 |
// and begin[1]=123I end[1]=Y456 size_overlap[1]=0 and we store the insertion removing hte first and last character (iii in this example). |
| 86 |
|
| 87 |
|
| 88 |
// strings storing central information coming after the first node of a path. |
| 89 |
std::string extended_string[2]; |
| 90 |
|
| 91 |
std::string polymorphism_type; |
| 92 |
|
| 93 |
// is this bubble of high complexity. |
| 94 |
bool high_complexity; |
| 95 |
|
| 96 |
/** indicates if the bubble passes the complexity filter (high complexity or don't care about complexity) */ |
| 97 |
bool acceptable_complexity; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
/** indicates when a predicted bubble is finished, if it is canonical */ |
| 102 |
bool isCanonical; |
| 103 |
|
| 104 |
/** indicates that a bubble had been closed. Maybe it was not dumped if the bubble was not canonical */ |
| 105 |
bool closed_bubble; |
| 106 |
|
| 107 |
/** in rad seq mode: is this bubble truncated **/ |
| 108 |
bool truncated; |
| 109 |
|
| 110 |
int type; // 0 = isolated SNP, 1 = isolated insertion, ... other to come |
| 111 |
|
| 112 |
|
| 113 |
int final_nb_polymorphism; // number of SNPs in a bubble, could be one (isolated SNP or an insertion) or more (an indel+n SNPs (n+1)) or n SNPs (n) |
| 114 |
|
| 115 |
// Index of the bubble |
| 116 |
size_t index; |
| 117 |
|
| 118 |
// Information about the bubble extension: 0=nothing, 1=left only, 2=right only, 3=both |
| 119 |
int where_to_extend; |
| 120 |
|
| 121 |
// Closing right nucleotide of the bubble |
| 122 |
Nucleotide closureRight; |
| 123 |
|
| 124 |
// Closing left nucleotide of the bubble |
| 125 |
Nucleotide closureLeft; |
| 126 |
|
| 127 |
// Unitig/Contig path on the right of the bubble. |
| 128 |
Path extensionRight; |
| 129 |
|
| 130 |
// Unitig/Contig path on the left of the bubble. |
| 131 |
Path extensionLeft; |
| 132 |
|
| 133 |
// Length of the right and left unitigs |
| 134 |
// - if the traversal is a simple traversal, then this length is equal to the length of the extension |
| 135 |
// - else (if the traversal is a monument traversal), then this length is equal to the starting position of the first bubble (if exist)) |
| 136 |
size_t divergenceLeft; |
| 137 |
size_t divergenceRight; |
| 138 |
|
| 139 |
// Sequence instances to be configured and then dumped in the output bank. |
| 140 |
Sequence seq1; |
| 141 |
Sequence seq2; |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
/********************************************************************************/ |
| 146 |
|
| 147 |
/** \brief class that tries to build a bubble from a starting node |
| 148 |
* |
| 149 |
* This class does all the job for expanding a bubble from a starting node if possible and |
| 150 |
* potentially extend it with right and left unitigs/contigs. |
| 151 |
* |
| 152 |
* This class is intended to be instantiated N times, one per thread. |
| 153 |
* |
| 154 |
* The starting node is provided to the operator() (so, one can see this class as a functor). |
| 155 |
*/ |
| 156 |
class BubbleFinder |
| 157 |
{ |
| 158 |
public: |
| 159 |
|
| 160 |
/** We define a structure gathering information during bubble detection. */ |
| 161 |
struct Stats |
| 162 |
{ |
| 163 |
Stats () : nb_bubbles(0), nb_bubbles_snp(0), nb_bubbles_snp_high(0), nb_bubbles_snp_low(0), nb_bubbles_del(0), nb_bubbles_del_high(0), nb_bubbles_del_low(0), nb_bubbles_snp_truncated(0) { memset (nb_where_to_extend_snp, 0, sizeof(nb_where_to_extend_snp)); memset (nb_where_to_extend_del, 0, sizeof(nb_where_to_extend_del)); } |
| 164 |
|
| 165 |
size_t nb_bubbles; |
| 166 |
|
| 167 |
size_t nb_bubbles_snp; |
| 168 |
size_t nb_bubbles_snp_high; |
| 169 |
size_t nb_bubbles_snp_low; |
| 170 |
size_t nb_bubbles_snp_truncated; |
| 171 |
size_t nb_where_to_extend_snp[4]; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
size_t nb_bubbles_del; |
| 176 |
size_t nb_bubbles_del_high; |
| 177 |
size_t nb_bubbles_del_low; |
| 178 |
size_t nb_where_to_extend_del[4]; |
| 179 |
}; |
| 180 |
|
| 181 |
/** Constructor. */ |
| 182 |
BubbleFinder (IProperties* props, const Graph& graph, Stats& stats); |
| 183 |
|
| 184 |
/** Copy constructor. It will be used when cloning N times the instance by the dispatcher |
| 185 |
* \param[in] bf : instance to be copied.*/ |
| 186 |
BubbleFinder (const BubbleFinder& bf); |
| 187 |
|
| 188 |
/** Destructor. */ |
| 189 |
~BubbleFinder (); |
| 190 |
|
| 191 |
/** Starting method that gets a node as argument and tries to build a bubble from it. |
| 192 |
* NOTE: defined as a template, because we can use either Node or BranchingNode instances |
| 193 |
* as starting nodes. See also 'start' method, which is template too, with too possible |
| 194 |
* specializations: one for Node, one for BranchingNode |
| 195 |
* \param[in] node : the starting node. */ |
| 196 |
template<typename T> |
| 197 |
void operator() (const T& node) |
| 198 |
{ |
| 199 |
/** We start the SNP in both directions (forward and reverse). */ |
| 200 |
start (bubble, node); |
| 201 |
start (bubble, graph.reverse(node)); |
| 202 |
} |
| 203 |
|
| 204 |
/** Get a properties object with the configuration of the finder. */ |
| 205 |
IProperties* getConfig () const; |
| 206 |
|
| 207 |
/** Constants */ |
| 208 |
static const char* STR_BFS_MAX_DEPTH; |
| 209 |
static const char* STR_BFS_MAX_BREADTH; |
| 210 |
protected: |
| 211 |
|
| 212 |
/** */ |
| 213 |
const Graph& graph; |
| 214 |
|
| 215 |
/** Statistics about the bubbles lookup. */ |
| 216 |
Stats& stats; |
| 217 |
|
| 218 |
/** Current Bubble instance built by this BubbleFinder instance. */ |
| 219 |
Bubble bubble; |
| 220 |
|
| 221 |
/** Shortcut attribute for the kmer size of the de Bruijn graph. */ |
| 222 |
size_t sizeKmer; |
| 223 |
|
| 224 |
/** Maximal number of polymorphism per bubble. Isolated = zero **/ |
| 225 |
int max_polymorphism; |
| 226 |
|
| 227 |
/** Max deletion size **/ |
| 228 |
int max_indel_size; |
| 229 |
|
| 230 |
/** Max indel size **/ |
| 231 |
int max_indel_ambiguity; |
| 232 |
|
| 233 |
bool accept_low; // Option set: do we accept low complexity bubbles |
| 234 |
bool accept_truncated_bubbles; //CHARLOTTE Option set: we accept truncated bubbles (for radseq sequencing) |
| 235 |
|
| 236 |
std::queue<std::pair<Node,std::string> > breadth_first_queue; |
| 237 |
|
| 238 |
|
| 239 |
int max_recursion_depth; |
| 240 |
int current_recursion_depth; |
| 241 |
|
| 242 |
int max_depth; // for unitigs/contigs extensions |
| 243 |
int max_breadth; // for unitigs/contigs extensions |
| 244 |
|
| 245 |
/* authorised_branching = |
| 246 |
* 0: branching forbidden in any path |
| 247 |
* 1: same branching on both path forbidden (i.e. 2 distinct nucleotides may be used in both paths for extension) |
| 248 |
* 2: no restriction on branching */ |
| 249 |
int authorised_branching; |
| 250 |
|
| 251 |
|
| 252 |
/** In b 2: maximaml number of symetrically branches traversed while walking the bubble**/ |
| 253 |
int max_sym_branches; |
| 254 |
|
| 255 |
/** RAD MODE: Longest accepted difference length between two paths of a truncated bubble */ |
| 256 |
int max_truncated_path_length_difference; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
/** Gives the kind of traversal to be done at left/right of the bubble. */ |
| 261 |
TraversalKind traversalKind; |
| 262 |
|
| 263 |
/** Output bank of the bubbles (as a pair of sequences). Note here: we use the IBank |
| 264 |
* interface here, and not a specific implementation (like BankFasta), so we could |
| 265 |
* deal with different kinds of banks. */ |
| 266 |
IBank* _outputBank; |
| 267 |
void setOutputBank (IBank* outputBank) { SP_SETATTR(outputBank); } |
| 268 |
|
| 269 |
/** We need a synchronizer for dumping the sequences into the output bank. */ |
| 270 |
ISynchronizer* _synchronizer; |
| 271 |
void setSynchronizer (ISynchronizer* synchronizer) { SP_SETATTR(synchronizer); } |
| 272 |
|
| 273 |
/** Terminator for marking branching nodes (used by the Traversal instance) */ |
| 274 |
BranchingTerminator* _terminator; |
| 275 |
void setTerminator (BranchingTerminator* terminator) { SP_SETATTR(terminator); } |
| 276 |
|
| 277 |
/** Used for computing unitigs or contigs (according to traversal kind choice) at the left |
| 278 |
* and right of the bubble. */ |
| 279 |
Traversal* _traversal; |
| 280 |
void setTraversal (Traversal* traversal) { SP_SETATTR(traversal); } |
| 281 |
|
| 282 |
/** Start a bubble detection from a given node. This node is mutated (its last nucleotide) in a |
| 283 |
* second node, and so this couple of nodes is set as the starting branch of a potential bubble. |
| 284 |
* NOTE: defined as template, with 2 specializations: one for Node, one for BranchingNode (see cpp file) |
| 285 |
* \param[in] node : the starting node. */ |
| 286 |
template<typename T> |
| 287 |
void start (Bubble& bubble, const T& node); |
| 288 |
|
| 289 |
/** Extension of a bubble given two nextNodes to be tested. |
| 290 |
* |
| 291 |
*/ |
| 292 |
bool expand_heart( |
| 293 |
const int nb_polymorphism, |
| 294 |
Node& nextNode1, |
| 295 |
Node& nextNode2, |
| 296 |
Node& node1, |
| 297 |
Node& node2, |
| 298 |
Node& previousNode1, |
| 299 |
Node& previousNode2, |
| 300 |
std::string local_extended_string1, |
| 301 |
std::string local_extended_string2, |
| 302 |
int sym_branches, |
| 303 |
int stack_size, |
| 304 |
const int dissyetrical_end_size |
| 305 |
); |
| 306 |
|
| 307 |
/** Extension of a single single node. Extension is non branching and stops after max_depth path. Returns true if the created path if smaller or equal to max_depth. |
| 308 |
* |
| 309 |
*/ |
| 310 |
bool expand_one_simple_path ( |
| 311 |
Node& node, // Node currently tested |
| 312 |
string& local_extended_string, // add nucleotides to this string |
| 313 |
const int max_depth, // maximal size of the path |
| 314 |
int & size_extension |
| 315 |
); |
| 316 |
|
| 317 |
/** Extension of a bubble by testing extensions from both branches of the bubble. |
| 318 |
* |
| 319 |
*/ |
| 320 |
bool expand (const int nb_polymorphism, |
| 321 |
Node node1, // In case of indels, this node is the real extended one, but we keep it at depth 1 |
| 322 |
Node node2, // In case of indels, this node is not extended (depth 1) |
| 323 |
Node previousNode1, |
| 324 |
Node previousNode2, |
| 325 |
std::string local_extended_string1, |
| 326 |
std::string local_extended_string2, |
| 327 |
int sym_branches, |
| 328 |
int stack_size); |
| 329 |
|
| 330 |
/** Extend the bubble to the left/right with a small assembly part of the de Bruijn graph. |
| 331 |
* \return true if the bubble has been extended, false otherwise. */ |
| 332 |
void extend (); |
| 333 |
|
| 334 |
/** Finish the bubble, ie output the pair of sequences in the output bank. |
| 335 |
* \param[in] bubble: bubble to be dumped in the output bank |
| 336 |
*/ |
| 337 |
void finish (const int dissyetrical_end_size); |
| 338 |
|
| 339 |
/** Check whether new node is similar to two previous nodes. |
| 340 |
* \param[in] previous : previous node |
| 341 |
* \param[in] current : current node |
| 342 |
* \param[in] next : next node |
| 343 |
* \return true if next node is different to current and previous nodes.*/ |
| 344 |
bool checkNodesDiff (Node& previous, Node& current, Node& next) const; |
| 345 |
|
| 346 |
/** Check whether the first kmer of the first path is smaller than the first kmer |
| 347 |
* of the revcomp(first path), this avoids repeated SNPs |
| 348 |
* \param[in] path : branch of a bubble. |
| 349 |
* \return set isCanonical to true if first path is lower than last reverse path. */ |
| 350 |
void checkPath (); |
| 351 |
|
| 352 |
/** Check bubble according to user choice for branching. |
| 353 |
* \param[in] node 1 : bubble branch last node |
| 354 |
* \param[in] node 2 : bubble branch last node |
| 355 |
* \return true if bubble is ok */ |
| 356 |
bool checkBranching (Node& node1, Node& node2, int & sym_branches) const; |
| 357 |
|
| 358 |
/** Check that indel bubbles respect the maximal size of the position ambiguity */ |
| 359 |
bool checkRepeatSize (string &extension1, string &extension2) const; |
| 360 |
|
| 361 |
/** Check complexity for a bubble. |
| 362 |
* \param[in] path1 : branch of the bubble |
| 363 |
* \param[in] path2 : branch of the bubble |
| 364 |
* set acceptable_complexity to true if the complexity is ok or if we accept low complexity bubbles. |
| 365 |
*/ |
| 366 |
void checkLowComplexity (); |
| 367 |
|
| 368 |
/** Fill a Sequence object for a given branch of a bubble. |
| 369 |
* \param[in] path : branch of the bubble. |
| 370 |
* \param[in] type : used to set the comment part of the sequence (likely 'higher' or 'lower') |
| 371 |
* \param[in] score : score for the bubble. |
| 372 |
* \param[in] where_to_extend : got from 'extend' method. |
| 373 |
* \param[in] seqIndex : index of the sequence (more exactly index for the pair of sequences) |
| 374 |
* \param[out] seq : sequence to be filled |
| 375 |
*/ |
| 376 |
void buildSequence (size_t pathIdx, const char* type, Sequence& seq, std::string polymorphism_comments); |
| 377 |
|
| 378 |
/** */ |
| 379 |
bool two_possible_extensions_on_one_path (Node& node) const; |
| 380 |
bool two_possible_extensions (Node node1, Node node2) const; |
| 381 |
private: |
| 382 |
bool recursive_indel_prediction( |
| 383 |
int extended_path_id, |
| 384 |
std::string tried_extension, |
| 385 |
Node current, |
| 386 |
size_t insert_size, |
| 387 |
const char end_insertion); |
| 388 |
void start_snp_prediction(); |
| 389 |
void start_indel_prediction(); |
| 390 |
}; |
| 391 |
|
| 392 |
#endif /* _TOOL_BUBBLE_HPP_ */ |
| 393 |
|