/***************************************************************************** * discoSnp++: discovering polymorphism from raw unassembled NGS reads * A tool from the GATB (Genome Assembly Tool Box) * Copyright (C) 2014 INRIA * Authors: P.Peterlongo, E.Drezen * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . *****************************************************************************/ #include #include #include using namespace std; #define DEBUG(a) // a const char* BubbleFinder::STR_BFS_MAX_DEPTH = "-bfs-max-depth"; const char* BubbleFinder::STR_BFS_MAX_BREADTH = "-bfs-max-breadth"; /********************************************************************* ** METHOD : ** PURPOSE : ** INPUT : ** OUTPUT : ** RETURN : ** REMARKS : *********************************************************************/ BubbleFinder::BubbleFinder (IProperties* props, const Graph& graph, Stats& stats) : graph(graph), stats(stats), _terminator(0), _outputBank(0), _traversal(0), _synchronizer(0) { assert (props != 0); /** We retrieve the kmer size. */ sizeKmer = graph.getKmerSize(); /** We set attributes according to user choice. */ accept_low = props->get (STR_DISCOSNP_LOW_COMPLEXITY) != 0; authorised_branching = props->getInt (STR_DISCOSNP_AUTHORISED_BRANCHING); max_indel_size = props->getInt (STR_MAX_INDEL_SIZE); max_indel_ambiguity = props->getInt (STR_MAX_AMBIGOUS_INDELS); max_polymorphism = props->getInt (STR_MAX_POLYMORPHISM); max_sym_branches = props->getInt (STR_MAX_SYMMETRICAL_CROSSROADS); accept_truncated_bubbles = props->get (STR_RADSEQ) != 0; max_truncated_path_length_difference = props->getInt (STR_MAX_TRUNCATED_PATH_LENGTH_DIFFERENCE); max_depth = props->getInt (STR_BFS_MAX_DEPTH); max_recursion_depth=1000; // TODO: parameter? max_breadth = props->getInt (STR_BFS_MAX_BREADTH); /** We set the traversal kind. */ traversalKind = TRAVERSAL_NONE; if (props->get(STR_DISCOSNP_TRAVERSAL_UNITIG) != 0) { traversalKind = TRAVERSAL_UNITIG; } if (props->get(STR_DISCOSNP_TRAVERSAL_CONTIG) != 0) { traversalKind = TRAVERSAL_CONTIG; } /** We set the name of the output file. */ stringstream ss; ss << props->getStr(STR_URI_OUTPUT);// << "_k_" << sizeKmer << "_c_" << graph.getInfo().getInt("abundance"); // ss << "_D_"< successors = graph.successors (node); if(successors.size() != 1) return Node(~0); // depth 1 for (int d=2; d>1)&3; } void clear_queue_pair( std::queue > &q ) { std::queue > empty; std::swap( q, empty ); } /********************************************************************* ** METHOD : ** PURPOSE : ** INPUT : ** OUTPUT : ** RETURN : ** REMARKS : breadth first search (queue already stored in the bubbleFinder class. Then limit the queue size. Thus early possible bubbles are tested even if longer insers are too complex to be treated. *********************************************************************/ void BubbleFinder::start_indel_prediction(){ if (max_indel_size==0) return; // no need to try to find indels bubble.type=1; // Consider a deletion in the upper path (avance on the lower) and then try the opposite Node current; int found_del_size=max_indel_size+10; // No indel found for now (we could also use MAXINT) for(int extended_path_id=0;extended_path_id<2;extended_path_id++){ // nb_snp_start++; // cout<<"nb_snp_start "<(current, string(""))); while(!breadth_first_queue.empty()){ // TODO maybe we could stop the whole breadth first search in case a bubble was found at a found_del_size and the current insert_size is <= found_del_size-2 DEBUG((cout<<"queue size "<max_recursion_depth){ clear_queue_pair(breadth_first_queue); break; // This bubble is too complex, we stop. } pair element=breadth_first_queue.front(); breadth_first_queue.pop(); DEBUG((cout<<"and now queue size "< found_del_size) { continue; } if (end_insertion == graph.toString(current)[sizeKmer-1] ){ DEBUG((cout<<"start an INDEL detection "< successors = graph.successors (current); /** No branching authorized in the insertion mode. */ if (successors.size()>1 && authorised_branching==0) { clear_queue_pair(breadth_first_queue); break; // ...and stop } /** checks if a successor with the good starting letter (the one potentially closing the indel) exists */ bool exists; Node successor = graph.successor(current,(Nucleotide)NT2int(end_insertion),exists); if(exists) breadth_first_queue.push(pair(successor,tried_extension+end_insertion)); /** then checks for the other possible extensions */ for (size_t successor_id=0; successor_id(successors[successor_id], tried_extension+graph.toString(successors[successor_id])[sizeKmer-1])); } } } } /********************************************************************* ** METHOD : ** PURPOSE : ** INPUT : ** OUTPUT : ** RETURN : ** REMARKS : *********************************************************************/ template<> void BubbleFinder::start (Bubble& bubble, const BranchingNode& node) { this->bubble=bubble; DEBUG ((cout << "[BubbleSNPFinder::start] BRANCHING NODE " << graph.toString(node) << endl)); DEBUG ((cout << "[BubbleSNPFinder::start] bubble.isCanonical " << bubble.isCanonical << endl)); /** We compute the successors of the node. */ GraphVector successors = graph.successors((Node&)node); DEBUG((cout << "successor size"< s1 and b1 -> s2 and b2 -> s1 and b2 -> s2), then // we need to construct the bubble s1... s2... from only one of the two branching nodes b1 or b2. // We chose the smallest from all the possible starting nodes to start a bubble. GraphVector predecessors = graph.predecessors (successors[i]); if (predecessors.size()>1) { for (size_t k=0; k successors; for (int depth=0;depth1) return false; // right branching, returns false if (graph.indegree(node)>1) return false; // left branching, returns false. if (successors.size()==0) return true; // path ends, return true node = successors[0]; local_extended_string+=ascii(graph.getNT(successors[0],sizeKmer-1)); size_extension++; } // the loop stops one step latter, meaning that if we arrive here, the path if of length max_depth+1, longer than max_depth. return false; } /********************************************************************* ** METHOD : ** PURPOSE : ** INPUT : ** OUTPUT : ** RETURN : True if expended, else return false ** REMARKS : *********************************************************************/ bool BubbleFinder::expand ( const int nb_polymorphism, Node node1, // Node currently tested Node node2, // Node currently tested Node previousNode1, // node before the one tested Node previousNode2, // node before the one tested string local_extended_string1, string local_extended_string2, int sym_branches, // number of symmetrically branchings traversed (used in b 2 mode) int stack_size ) { DEBUG((cout<<" *"< > successors; while (true){ if (checkBranching(node1,node2, sym_branches) == false) return false; // no possibility to continue successors = graph.successors (node1, node2); // get next two nodes if (successors.size() != 1) break; // no more iterative programming if (successors[0].first == successors[0].second) { break; // ended bubble } bool checkPrevious = checkNodesDiff (previousNode1, node1, successors[0].first) && checkNodesDiff (previousNode2, node2, successors[0].second); if (!checkPrevious){return false;} local_extended_string1+=ascii(graph.getNT(successors[0].first,sizeKmer-1)); // extend upper sequence with new character local_extended_string2+=ascii(graph.getNT(successors[0].second,sizeKmer-1));// extend lower sequence with new character previousNode1 = node1; // change currents and previous nodes previousNode2 = node2; // change currents and previous nodes node1 = successors[0].first; // change currents and previous nodes node2 = successors[0].second; // change currents and previous nodes } /**************** END OPTIMIZATION *****************************************/ /****************************************************************************/ /** We may have to stop the extension according to the branching mode. */ if (checkBranching(node1,node2, sym_branches) == false) { return false; } bool dumped_bubble=false; /*************************************************/ /** RADseq: We check if the bubble is truncated **/ /*************************************************/ int equal_zone_lenght=3; // TODO: make a parameter please. const int suc1=graph.successors(node1).size(); const int suc2=graph.successors(node2).size(); if(accept_truncated_bubbles && (suc1==0 || suc2==0)) // If end of at least the two paths and if one asks for truncated bubbles. { //We check that the last 3 nt are similar on the two truncated paths for (int i=0;i > successors = graph.successors (node1, node2); DEBUG((cout<<"successors size "< successors1 = graph.successors (node1); GraphVector < Node > successors2 = graph.successors (node2); /** We loop over the successors of the two nodes found with distinct extending nucleotides. */ for (size_t i1=0; i1 successors = graph.successors (bubble.end[0]); GraphVector predecessors = graph.predecessors (bubble.begin[0]); /** We need to reset branching nodes between extensions in case of overlapping extensions. */ _terminator->reset (); /** If unique, we keep the left/right extensions. */ if (successors.size()==1) { /** We compute right extension of the node. */ closureRight = graph.getNT (successors [0], sizeKmer-1); _traversal->traverse (successors[0], DIR_OUTCOMING, bubble.extensionRight); bubble.divergenceRight = _traversal->getBubbles().empty() ? bubble.extensionRight.size() : _traversal->getBubbles()[0].first; } if (predecessors.size()==1) { /** We compute left extension of the node. */ closureLeft = graph.getNT (predecessors[0], 0); Node rev_pred = graph.reverse(predecessors[0]); _traversal->traverse (rev_pred, DIR_OUTCOMING, bubble.extensionLeft); bubble.divergenceLeft = _traversal->getBubbles().empty() ? bubble.extensionLeft.size() : _traversal->getBubbles()[0].first; } } /** We return a code value according to left/right extensions status. */ if (closureLeft==NUCL_UNKNOWN && closureRight==NUCL_UNKNOWN) { bubble.where_to_extend = 0; } else if (closureLeft!=NUCL_UNKNOWN && closureRight==NUCL_UNKNOWN) { bubble.where_to_extend = 1; } else if (closureLeft==NUCL_UNKNOWN && closureRight!=NUCL_UNKNOWN) { bubble.where_to_extend = 2; } else if (closureLeft!=NUCL_UNKNOWN && closureRight!=NUCL_UNKNOWN) { bubble.where_to_extend = 3; } bubble.closureLeft = closureLeft; bubble.closureRight = closureRight; } /********************************************************************* ** METHOD : ** PURPOSE : ** INPUT : ** OUTPUT : ** RETURN : ** REMARKS : *********************************************************************/ void BubbleFinder::finish (const int disymetrical_end_size) { /** We build two Sequence objects from the information of the bubble. */ /** We set the bubble index. NOTE: We have to make sure it is protected against concurrent * accesses since we may be called here from different threads. */ if (bubble.type==0) __sync_add_and_fetch (&(stats.nb_bubbles_snp), 1); if (bubble.type==1) __sync_add_and_fetch (&(stats.nb_bubbles_del), 1); bubble.index = __sync_add_and_fetch (&(stats.nb_bubbles), 1); /** compute the bubble paths */ string path_0 = graph.toString (bubble.begin[0])+bubble.extended_string[0]; string path_1 = graph.toString (bubble.begin[1])+bubble.extended_string[1]; stringstream comment; if ( bubble.polymorphism_type=="SNP" ){ int polymorphism_id=1; for (unsigned int i=0;i1) { comment << ","; } comment<<"P_" << polymorphism_id << ":" << i << "_" << path_0[i] << "/" << path_1[i]; polymorphism_id++; } } } if ( bubble.polymorphism_type=="INDEL" ){ // if disymetrical_end_size>0: path0 has been extended more than path1 // if disymetrical_end_size<0: path1 has been extended more than path0 int p0=path_0.length(),p1=path_1.length(); int bes0=bubble.extended_string[0].length(), bes1=bubble.extended_string[1].length(); if (disymetrical_end_size>0){ p0-=disymetrical_end_size; bes0-=disymetrical_end_size; } if (disymetrical_end_size<0){ p1+=disymetrical_end_size; bes1+=disymetrical_end_size; } const int insert_size = p0insert (bubble.seq1); _outputBank->insert (bubble.seq2); /** Stats update (in concurrent access protection block). */ // stats.nb_bubbles++; if (bubble.type==0){ stats.nb_where_to_extend_snp[bubble.where_to_extend] ++; if (bubble.high_complexity) { stats.nb_bubbles_snp_high++; } else { stats.nb_bubbles_snp_low++; } if (bubble.truncated) { stats.nb_bubbles_snp_truncated++; } } if (bubble.type==1){ stats.nb_where_to_extend_del[bubble.where_to_extend] ++; if (bubble.high_complexity) { stats.nb_bubbles_del_high++; } else { stats.nb_bubbles_del_low++; } } } } /********************************************************************* ** METHOD : ** PURPOSE : ** INPUT : ** OUTPUT : ** RETURN : ** REMARKS : *********************************************************************/ bool BubbleFinder::two_possible_extensions_on_one_path (Node& node) const { return graph.indegree(node)>1 || graph.outdegree(node)>1; } /********************************************************************* ** METHOD : ** PURPOSE : ** INPUT : ** OUTPUT : ** RETURN : ** REMARKS : *********************************************************************/ bool BubbleFinder::two_possible_extensions (Node node1, Node node2) const { return graph.successorsEdge (node1, node2).size() >= 2 || graph.successorsEdge (graph.reverse (node1),graph.reverse (node2)).size() >= 2; } /********************************************************************* ** METHOD : ** PURPOSE : ** INPUT : ** OUTPUT : ** RETURN : ** REMARKS : *********************************************************************/ void BubbleFinder::buildSequence ( size_t pathIdx, const char* type, Sequence& seq, string polymorphism_comments) { stringstream commentStream; /** We build the comment for the sequence. */ commentStream << bubble.polymorphism_type << "_" << type << "_path_" << bubble.index << "|" << polymorphism_comments << "|" << (bubble.high_complexity ? "high" : "low")<< "|nb_pol_" <1 ? (bubble.extensionRight.size()+1) : 0); } if (traversalKind == TRAVERSAL_CONTIG) { commentStream << "|left_unitig_length_"; commentStream << (bubble.where_to_extend%2==1 ? (bubble.divergenceLeft +1) : 0); commentStream << "|right_unitig_length_"; commentStream << (bubble.where_to_extend>1 ? (bubble.divergenceRight+1) : 0); commentStream << "|left_contig_length_"; commentStream << (bubble.where_to_extend%2==1 ? (bubble.extensionLeft.size() +1) : 0); commentStream << "|right_contig_length_"; commentStream << (bubble.where_to_extend>1 ? (bubble.extensionRight.size()+1) : 0); } /** We assign the comment of the sequence. */ seq.setComment (commentStream.str()); size_t lenLeft = bubble.extensionLeft.size (); size_t lenRight = bubble.extensionRight.size (); size_t len = sizeKmer + bubble.extended_string[pathIdx].length(); if (bubble.closureLeft != NUCL_UNKNOWN) { len += 1 + lenLeft; } if (bubble.closureRight != NUCL_UNKNOWN) { len += 1 + lenRight; } /** We resize the sequence data if needed. Note: +1 for ending '\0' * NOTE: we use resize if we need more space, setSize otherwise. */ if (seq.getData().size() < len+1) { seq.getData().resize (len+1); } else { seq.getData().setSize (len+1); } char* output = seq.getDataBuffer(); /** We add the left extension if any. Note that we use lower case for extensions. */ if (bubble.closureLeft != NUCL_UNKNOWN) { for (size_t i=0; i 0 central string is empty **/ for (size_t i=0; i CCTG -> CTGG -> TGGG -> GGGA ---------------------> extended string is GGG (size k-1) * ACCT -> CCTX -> CTXX -> TXXG -> XXGG -> XGGG -> GGGA ------> extended string is XXGGG (size k-1+size insert * * Extreme case: * ACCTGGGA * ACCT|GGGA|GGGA * * ACCT->CCTG->CTGG->TGGG->GGGA->GGAX ------------------------------> extended string is empty (size 0 = k-1-ambiguity => ambiguity=k-1) * ACCT->CCTG->CTGG->TGGG->GGGA->GGAG->GAGG->AGGG->GGGA->GAAX ------> extended string is AGGG (size k = k-1-ambiguity+size_ins = k-1-(k-1)+k => insertion of length k **/ const int size_repeat = sizeKmer-2-min(extension1.length(), extension2.length()); if (size_repeat>max_indel_ambiguity) { return false; } return true; } /********************************************************************* ** METHOD : BubbleFinder::checkBranching ** PURPOSE : Checks the branching properties of a couple of nodes. If authorised_branching==0: no branching is authorized in any of the two nodes. ** If authorised_branching==1: no symetrical branching authorized from the two nodes (ie. N1-->A and N1-->B and N2-->A and N2-->B not authorized) ** INPUT : ** OUTPUT : ** RETURN : ** REMARKS : *********************************************************************/ bool BubbleFinder::checkBranching (Node& node1, Node& node2, int & sym_branches) const { // stop the extension if authorised_branching==0 (not branching in any path) and any of the two paths is branching if (authorised_branching==0 && (two_possible_extensions_on_one_path(node1) || two_possible_extensions_on_one_path(node2))) { return false; } const bool two_extensions = two_possible_extensions (node1, node2); // stop the extension if authorised_branching==1 (not branching in both path) and both the two paths are branching if (authorised_branching==1 && two_extensions) { return false; } // stop the extension if authorised_branching=2 and too much nrabching crossroads were traversed. if (authorised_branching==2 && two_extensions){ if (sym_branches==max_sym_branches) {return false;} // We saw already too much symmetrically branching crossreads sym_branches++; } return true; } /********************************************************************* ** METHOD : ** PURPOSE : ** INPUT : ** OUTPUT : ** RETURN : ** REMARKS : *********************************************************************/ void BubbleFinder::checkLowComplexity () { bubble.acceptable_complexity=true; string path1 = graph.toString (bubble.begin[0]).substr(0, sizeKmer-1) + graph.toString (bubble.end[0]); string path2 = graph.toString (bubble.begin[1]).substr(0, sizeKmer-1) + graph.toString (bubble.end[1]); /** We compute the low complexity score of the two paths. */ bubble.high_complexity = filterLowComplexity2Paths (path1, path2); if (accept_low) return; // the complexity is acceptable for this bubble anyway. bubble.acceptable_complexity=bubble.high_complexity; DEBUG((cout<<"check low "<add (0, "config", ""); props->add (1, "kmer_size", "%d", sizeKmer); props->add (1, "auth_branch", "%d", authorised_branching); props->add (1, "max_indel_size", "%d", max_indel_size); props->add (1, "max_polymorphism", "%d", max_polymorphism); props->add (1, "low", "%d", accept_low); props-> add (1, "rad", "%d", accept_truncated_bubbles); props->add (1, "traversal", "%s", toString (traversalKind).c_str()); if (accept_truncated_bubbles){ props->add(1, "radseq mode", ""); props->add(2, "max_truncated_path_length_difference", "%d", accept_truncated_bubbles); } return props; } /********************************************************************************/ /******************************** SNPs ******************************************/ /********************************************************************************/