File: BubbleTool.cpp

package info (click to toggle)
ray 2.3.1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,008 kB
  • sloc: cpp: 49,973; sh: 339; makefile: 281; python: 168
file content (262 lines) | stat: -rw-r--r-- 6,691 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 	Ray
    Copyright (C) 2010, 2011, 2012 Sébastien Boisvert

	http://DeNovoAssembler.SourceForge.Net/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3 of the License.

    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 General Public License for more details.

    You have received a copy of the GNU General Public License
    along with this program (gpl-3.0.txt).  
	see <http://www.gnu.org/licenses/>

*/

#include "BubbleTool.h"

#include <code/Mock/common_functions.h>

#include <assert.h>
#include <map>
#include <set>
#include <iostream>
using namespace std;

void BubbleTool::printStuff(Kmer root,vector<vector<Kmer> >*trees,
map<Kmer,int>*coverages){
	int m_wordSize=m_parameters->getWordSize();
	cout<<"Trees="<<trees->size()<<endl;
	cout<<"root="<<root.idToWord(m_wordSize,m_parameters->getColorSpaceMode())<<endl;
	cout<<"digraph{"<<endl;
	map<Kmer,set<Kmer> > printedEdges;
	
	for(map<Kmer ,int>::iterator i=coverages->begin();i!=coverages->end();i++){
		Kmer kmer=i->first;
		cout<<kmer.idToWord(m_wordSize,m_parameters->getColorSpaceMode())<<" [label=\""<<kmer.idToWord(m_wordSize,m_parameters->getColorSpaceMode())<<" "<<i->second<<"\"]"<<endl;
	}
	for(int j=0;j<(int)trees->size();j++){
		for(int i=0;i<(int)trees->at(j).size();i+=2){
			Kmer a=trees->at(j).at(i+0);
			#ifdef CONFIG_ASSERT
			assert(i+1<(int)trees->at(j).size());
			#endif
			Kmer b=trees->at(j).at(i+1);
			if(printedEdges.count(a)>0 && printedEdges[a].count(b)>0){
				continue;
			}
			cout<<a.idToWord(m_wordSize,m_parameters->getColorSpaceMode())<<" -> "<<b.idToWord(m_wordSize,m_parameters->getColorSpaceMode())<<endl;
			printedEdges[a].insert(b);
		}
	}
	cout<<"}"<<endl;
}

/**
 *
 */
bool BubbleTool::isGenuineBubble(Kmer root,vector<vector<Kmer > >*trees,
map<Kmer ,int>*coverages,int repeatCoverage){
	#ifdef NO_BUBBLES
	return false;
	#endif

	if((*coverages)[root]>= repeatCoverage){
		return false;
	}

	int m_wordSize=m_parameters->getWordSize();
	#ifdef CONFIG_ASSERT
	for(int i=0;i<(int)trees->size();i++){
		for(int j=0;j<(int)trees->at(i).size();j+=2){
			Kmer a=trees->at(i).at(j+0);
			Kmer b=trees->at(i).at(j+1);
			string as=a.idToWord(m_wordSize,m_parameters->getColorSpaceMode());
			string bs=b.idToWord(m_wordSize,m_parameters->getColorSpaceMode());
			assert(as.substr(1,m_wordSize-1)==bs.substr(0,m_wordSize-1));
		}
	}
	#endif

	if(m_parameters->debugBubbles()){
		printStuff(root,trees,coverages);
	}

	if(trees->size()!=2){
		return false;// we don'T support that right now ! triploid stuff are awesome.
	}

	// given the word size
	// check that they join.
	//
	// substitution SNP is d=0
	// del is 1, 2, or 3

	map<Kmer ,int> coveringNumber;

	Kmer target;
	bool foundTarget=false;
	for(int j=0;j<(int)trees->size();j++){
		for(int i=0;i<(int)trees->at(j).size();i+=2){
			Kmer a=trees->at(j).at(i+1);
			#ifdef CONFIG_ASSERT
			if(coverages->count(a)==0){
				cout<<a.idToWord(m_parameters->getWordSize(),m_parameters->getColorSpaceMode())<<" has no coverage."<<endl;
			}
			assert(coverages->count(a)>0);
			#endif

			coveringNumber[a]++;
			if(!foundTarget && coveringNumber[a]==2){
				foundTarget=true;
				target=a;
				break;
			}
		}
	}

	if(!foundTarget){
		if(m_parameters->debugBubbles()){
			cout<<"Target not found."<<endl;
		}
		return false;
	}

	if((*coverages)[target]>= repeatCoverage){
		return false;
	}

	#ifdef CONFIG_ASSERT
	assert(coverages->count(root)>0);
	assert(coverages->count(target)>0);
	#endif
	#ifdef CONFIG_ASSERT
	int rootCoverage=(*coverages)[root];
	int targetCoverage=(*coverages)[target];
	assert(rootCoverage>0);
	assert(targetCoverage>0);
	#endif

	vector<map<Kmer ,Kmer > > parents;

	for(int j=0;j<(int)trees->size();j++){
		map<Kmer ,Kmer > aVector;
		parents.push_back(aVector);
		for(int i=0;i<(int)trees->at(j).size();i+=2){
			Kmer a=trees->at(j).at(i+0);
			Kmer b=trees->at(j).at(i+1);
			parents[j][b]=a;
		}
	}

	vector<vector<int> > observedValues;
	/*
 *
 *  BUBBLE is below
 *
 *    *  ----  * -------*  --------*
 *      \                          /
 *        ---- * --------* ------ *
 *
 */
	// accumulate observed values
	// and stop when encountering
	for(int j=0;j<(int)trees->size();j++){
		vector<int> aVector;
		observedValues.push_back(aVector);
		set<Kmer > visited;
		
		Kmer startingPoint=trees->at(j).at(0);
		Kmer current=target;

		while(current!=startingPoint){
			if(visited.count(current)>0){
				return false;
			}
			visited.insert(current);
			Kmer theParent=parents[j][current];
			int coverageValue=(*coverages)[theParent];

			observedValues[j].push_back(coverageValue);
			current=theParent;
		}
	}

	if(m_parameters->debugBubbles()){
		cout<<"O1="<<observedValues[0].size()<<" O2="<<observedValues[1].size()<<endl;
	}

	int sum1=0;
	for(int i=0;i<(int)observedValues[0].size();i++){
		sum1+=observedValues[0][i];
	}

	if(m_parameters->debugBubbles()){
		cout<<"O1Values= ";
		for(int i=0;i<(int)observedValues[0].size();i++){
			cout<<observedValues[0][i]<<" ";
		}
		cout<<endl;
	}

	int sum2=0;
	for(int i=0;i<(int)observedValues[1].size();i++){
		sum2+=observedValues[1][i];
	}
	
	if(m_parameters->debugBubbles()){
		cout<<"O2Values= ";
		for(int i=0;i<(int)observedValues[1].size();i++){
			cout<<observedValues[1][i]<<" ";
		}
		cout<<endl;
	}

	if((int)observedValues[0].size()<2*m_parameters->getWordSize()
	&& (int)observedValues[1].size()<2*m_parameters->getWordSize()){
		if(sum1>sum2){
			m_choice=trees->at(0).at(1);
		}else if(sum2>sum1){
			m_choice=trees->at(1).at(1);

		// this will not happen often
		}else if(sum1==sum2){
			// take the shortest, if any
			if(observedValues[0].size()<observedValues[1].size()){
				m_choice=trees->at(0).at(1);
			}else if(observedValues[1].size()<observedValues[0].size()){
				m_choice=trees->at(1).at(1);
			// same length and same sum, won't happen very often anyway
			}else{
				m_choice=trees->at(0).at(1);
			}
		}
		
		if(m_parameters->debugBubbles()){
			cout<<"This is a genuine bubble"<<endl;
			cout<<"root="<<root.idToWord(m_wordSize,m_parameters->getColorSpaceMode())<<" target="<<target.idToWord(m_wordSize,m_parameters->getColorSpaceMode())<<endl;
		}

		return true;
	}

	if(m_parameters->debugBubbles()){
		cout<<"False at last"<<endl;
	}

	return false;
}

Kmer BubbleTool::getTraversalStartingPoint(){
	return m_choice;
}

void BubbleTool::constructor(Parameters*parameters){
	m_parameters=parameters;
}