File: OpenAssemblerChooser.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 (286 lines) | stat: -rw-r--r-- 7,985 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 	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 "OpenAssemblerChooser.h"
#include "Chooser.h"

void OpenAssemblerChooser::updateMultiplicators(){
	m_singleEndMultiplicator=2.0;
	m_pairedEndMultiplicator=2.0;
}

void OpenAssemblerChooser::constructor(){
	updateMultiplicators();
}

int OpenAssemblerChooser::choose(ExtensionData*ed,Chooser*m_c,int minimumCoverage,
Parameters*parameters){

	/** filter invalid choices */
	set<int> invalidChoices;
	for(int i=0;i<(int)ed->m_enumerateChoices_outgoingEdges.size();i++){
		int coverageForI=ed->m_EXTENSION_coverages.at(i);
		Kmer key=ed->m_enumerateChoices_outgoingEdges[i];

		/** an invalid choice must not have paired reads */
		/** an invalid choice must not have single reads */
		if(ed->m_EXTENSION_pairedReadPositionsForVertices[key].size()==0 && ed->m_EXTENSION_readPositionsForVertices[key].size()==0){
			invalidChoices.insert(i);
			continue;
		}

		/* invalid if < minCoverage and all others >= 2*minCoverage */

		if(coverageForI<minimumCoverage){
			bool invalid=true;
			for(int j=0;j<(int)ed->m_enumerateChoices_outgoingEdges.size();j++){
				if(i==j)
					continue;
				int coverageForJ=ed->m_EXTENSION_coverages.at(j);
				if(coverageForJ<2*minimumCoverage){
					invalid=false;
					break;
				}
			}
			if(invalid){
				invalidChoices.insert(i);
				continue;
			}
		}

		/* invalid if < minCoverage/2 and all others >= minCoverage */
		if(coverageForI<minimumCoverage/2){
			bool invalid=true;
			for(int j=0;j<(int)ed->m_enumerateChoices_outgoingEdges.size();j++){
				if(i==j)
					continue;
				int coverageForJ=ed->m_EXTENSION_coverages.at(j);
				if(coverageForJ<minimumCoverage){
					invalid=false;
					break;
				}
			}
			if(invalid){
				invalidChoices.insert(i);
				continue;
			}
		}

		/* invalid if == 2 and all other are >= 10 */
		if(coverageForI == 2){
			bool invalid=true;
			for(int j=0;j<(int)ed->m_enumerateChoices_outgoingEdges.size();j++){
				if(i==j)
					continue;
				int coverageForJ=ed->m_EXTENSION_coverages.at(j);
				if(coverageForJ<10){
					invalid=false;
					break;
				}
			}
			if(invalid){
				invalidChoices.insert(i);
				continue;
			}
		}


	}

	/** make a list of battle victories for each choice */

	vector<set<int> > battleVictories;

	for(int i=0;i<(int)ed->m_enumerateChoices_outgoingEdges.size();i++){
		set<int> victories;
		battleVictories.push_back(victories);
	}

	chooseWithCoverage(ed,minimumCoverage,&battleVictories);

	int coverageWinner=getWinner(&battleVictories,ed->m_enumerateChoices_outgoingEdges.size());

	if(coverageWinner!=IMPOSSIBLE_CHOICE && invalidChoices.count(coverageWinner) == 0)
		return coverageWinner;

		
	int withPairedInformation=0;
	/** prepare data for the NovaEngine */
	vector<map<int,int> > novaData;
	for(int i=0;i<(int)ed->m_enumerateChoices_outgoingEdges.size();i++){
		Kmer key=ed->m_enumerateChoices_outgoingEdges[i];
		map<int,int> data;

		/** load single-end data */
/*
		if(ed->m_EXTENSION_readPositionsForVertices.count(key)>0){
			for(vector<int>::iterator j=ed->m_EXTENSION_readPositionsForVertices[key].begin();
				j!=ed->m_EXTENSION_readPositionsForVertices[key].end();j++){
				int val=*j;
				data[val]++;
			}
		}
*/
		/** load paired-end data and mate-pair data */
		if(ed->m_EXTENSION_pairedReadPositionsForVertices.count(key)>0){
			for(int j=0;j<(int)ed->m_EXTENSION_pairedReadPositionsForVertices[key].size();j++){
				if(j==0)
					withPairedInformation++;
				int value=ed->m_EXTENSION_pairedReadPositionsForVertices[key][j];
				data[value]++;
			}
		}

		novaData.push_back(data);
	}

	int novaChoice=IMPOSSIBLE_CHOICE;
	/** this is the powerful NovaEngine -- an assembly engine to surf de Bruijn DNA graphs */
	if(parameters->hasOption("-use-NovaEngine") && withPairedInformation>0){
		/** NovaData are ready, now call the NovaEngine */
		bool showNovaAlgorithm=parameters->hasOption("-show-NovaEngine");
		novaChoice=m_novaEngine.choose(&novaData,&invalidChoices,showNovaAlgorithm);

		if(novaChoice!=IMPOSSIBLE_CHOICE)
			return novaChoice;
	}


	m_c->chooseWithPairedReads(ed,m_pairedEndMultiplicator,&battleVictories,parameters);
	
	int pairedChoice=getWinner(&battleVictories,ed->m_enumerateChoices_outgoingEdges.size());

	battleVictories.clear();

	for(int i=0;i<(int)ed->m_enumerateChoices_outgoingEdges.size();i++){
		set<int> victories;
		battleVictories.push_back(victories);
	}

	if(pairedChoice!=IMPOSSIBLE_CHOICE){
		return pairedChoice;
	}else{
		/** TODO: verify it this really improves assemblies . */
		if(ed->m_EXTENSION_extension.size()>50000){
			if(!parameters->hasPairedReads()){
				return IMPOSSIBLE_CHOICE;
			}
		}

		// if both have paired reads and that is not enough for one of them to win, then abort
		int withPairedReads=0;
		
		for(int j=0;j<(int)ed->m_enumerateChoices_outgoingEdges.size();j++){
			Kmer key=ed->m_enumerateChoices_outgoingEdges[j];
			if(ed->m_EXTENSION_pairedReadPositionsForVertices[key].size()>0){
				withPairedReads++;
			}
		}
		if(withPairedReads!=0){
			return IMPOSSIBLE_CHOICE;
		}
	}

	map<int,int> CHOOSER_theMaxs;
	map<int,int> CHOOSER_theNumbers;
	map<int,int> CHOOSER_theSums;

	for(int i=0;i<(int)ed->m_enumerateChoices_outgoingEdges.size();i++){
		Kmer key=ed->m_enumerateChoices_outgoingEdges[i];
		int max=0;
		int n=0;
		int sum=0;
		if(ed->m_EXTENSION_readPositionsForVertices.count(key)>0){
			for(vector<int>::iterator j=ed->m_EXTENSION_readPositionsForVertices[key].begin();
				j!=ed->m_EXTENSION_readPositionsForVertices[key].end();j++){
				int val=*j;
				if(val>max){
					max=val;
				}
				n++;
				sum+=val;
			}
		}
		CHOOSER_theSums[i]=sum;
		CHOOSER_theNumbers[i]=n;
		CHOOSER_theMaxs[i]=max;
	}

	// win or lose with single-end reads
	for(int i=0;i<(int)ed->m_enumerateChoices_outgoingEdges.size();i++){
		if(CHOOSER_theMaxs[i]<5){
			continue;
		}

		for(int j=0;j<(int)ed->m_enumerateChoices_outgoingEdges.size();j++){
			if((CHOOSER_theMaxs[i] > m_singleEndMultiplicator*CHOOSER_theMaxs[j]) 
				&& (CHOOSER_theSums[i] > m_singleEndMultiplicator*CHOOSER_theSums[j]) 
				&& (CHOOSER_theNumbers[i] > m_singleEndMultiplicator*CHOOSER_theNumbers[j])
				){
				battleVictories[i].insert(j);
			}
		}
	}

	int finalWinner=getWinner(&battleVictories,ed->m_enumerateChoices_outgoingEdges.size());

	if(finalWinner!=IMPOSSIBLE_CHOICE){
		return finalWinner;
	}

	return IMPOSSIBLE_CHOICE;
}


int OpenAssemblerChooser::getWinner(vector<set<int> >*battleVictories,int choices){
	for(int winner=0;winner<(int)battleVictories->size();winner++){
		int wins=battleVictories->at(winner).size();
		if(wins+1==choices){
			return winner;
		}
	}
	return IMPOSSIBLE_CHOICE;
}

void OpenAssemblerChooser::chooseWithCoverage(ExtensionData*ed,int minCoverage,vector<set<int> >*battleVictories){
	for(int i=0;i<(int)ed->m_enumerateChoices_outgoingEdges.size();i++){

		int coverageForI=ed->m_EXTENSION_coverages.at(i);
		Kmer key=ed->m_enumerateChoices_outgoingEdges[i];

		for(int j=0;j<(int)ed->m_enumerateChoices_outgoingEdges.size();j++){
			if(i==j){
				continue;
			}
			int coverageForJ=ed->m_EXTENSION_coverages.at(j);

			if(coverageForI>=2*minCoverage && coverageForJ<=minCoverage/2){
				(*battleVictories)[i].insert(j);
			}

			if(coverageForI>=minCoverage && coverageForJ<=minCoverage/2){
				(*battleVictories)[i].insert(j);
			}
		}
	}
}