File: ScaffoldingAlgorithm.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 (483 lines) | stat: -rw-r--r-- 13,826 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 	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 "ScaffoldingAlgorithm.h"

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

ScaffoldingAlgorithm::ScaffoldingAlgorithm(){
}

void ScaffoldingAlgorithm::setVertices(vector<ScaffoldingVertex>*vertices){
	m_vertices=vertices;
}

void ScaffoldingAlgorithm::setEdges(vector<ScaffoldingEdge>*edges){
	m_edges=edges;
}

/* TODO: use pointers or references to store ScaffoldingEdge objects */
void ScaffoldingAlgorithm::solve(
	vector<vector<PathHandle> >*scaffoldContigs,
	vector<vector<char> >*scaffoldStrands,
	vector<vector<int> >*scaffoldGaps
		){

	cout<<"Welcome to GreedySolver v1.0"<<endl;

	map<PathHandle,map<PathHandle,int> > validCounts;

	vector<vector<uint64_t> > megaLinks;

	cout<<m_vertices->size()<<" vertices; "<<m_edges->size()<<" edges"<<endl;

	int threshold=500;

	int large=0;
	for(int i=0;i<(int)m_vertices->size();i++){
		if(m_vertices->at(i).getLength() >= threshold){
			large++;
		}
		m_lengths[m_vertices->at(i).getName()]=m_vertices->at(i).getLength();
	}

	cout<<large<<" contigs >= "<<threshold<<endl;

	priority_queue<ScaffoldingEdge> bagOfChoices;

	for(int i=0;i<(int)m_edges->size();i++){
		bagOfChoices.push((*m_edges)[i]);
	}

	m_numberOfEdges=0;

	while(!bagOfChoices.empty()){
		ScaffoldingEdge edge=bagOfChoices.top();
		
		//cout<<"Want to add"<<endl;
		//edge.print();

		if(!hasConflict(&edge)){
			addEdge(&edge);
		}else{
			/* solve conflict */
		}

		bagOfChoices.pop();
	}

	cout<<"Edges in solution: "<<m_numberOfEdges<<endl;

	cout<<endl;

	cout<<"Conflict statistics:"<<endl;

	for(map<int,map<int,int> >::iterator i=m_conflicts.begin();i!=m_conflicts.end();i++){
		int sum=0;
		for(map<int,int>::iterator j=i->second.begin();j!=i->second.end();j++){
			sum+=j->second;
		}
		cout<<i->first<<"	"<<sum<<endl;
	}


	/* beginning of code to move */

	/* input: ScaffoldingVertex objects and ScaffoldingEdge objects */
	/** filter edges with greedy algorithm */
	/** populate megaLinks with accepted edges */

	for(map<PathHandle,map<PathHandle,ScaffoldingEdge> >::iterator i=m_addedEdges.begin();i!=m_addedEdges.end();i++){
		for(map<PathHandle,ScaffoldingEdge>::iterator j=i->second.begin();j!=i->second.end();j++){
			PathHandle first=i->first;
			PathHandle second=j->first;

			if(second < first){
				continue;
			}

			ScaffoldingEdge edge=j->second;
		
			/**
			 * TODO This code is not clean. char should not be stored as a
			 * uint64_t.
			 */
			vector<uint64_t> megaLink;
			PathHandle leftContig=edge.getLeftContig();
			megaLink.push_back(leftContig.getValue());
			char leftStrand=edge.getLeftStrand();
			megaLink.push_back(leftStrand);
			PathHandle rightContig=edge.getRightContig();
			megaLink.push_back(rightContig.getValue());
			char rightStrand=edge.getRightStrand();
			megaLink.push_back(rightStrand);
			int average=edge.getGapSize();
			megaLink.push_back(average);
			megaLinks.push_back(megaLink);

			validCounts[leftContig][rightContig]++;
			validCounts[rightContig][leftContig]++;
		}
	}

	// create the graph
	set<PathHandle> vertices;

	// TODO: fix this code, PathHandle should not be used to store a char
	map<PathHandle,map<char,vector<vector<uint64_t> > > > parents;
	map<PathHandle,map<char,vector<vector<uint64_t> > > > children;

	for(int i=0;i<(int)megaLinks.size();i++){
		PathHandle leftContig=megaLinks[i][0];
		char leftStrand=megaLinks[i][1];
		PathHandle rightContig=megaLinks[i][2];
		char rightStrand=megaLinks[i][3];
		int distance=megaLinks[i][4];
		char otherLeftStrand='F';
		vertices.insert(leftContig);
		vertices.insert(rightContig);
		if(leftStrand=='F')
			otherLeftStrand='R';
		char otherRightStrand='F';
		if(rightStrand=='F')
			otherRightStrand='R';

		children[leftContig][leftStrand].push_back(megaLinks[i]);
		parents[rightContig][rightStrand].push_back(megaLinks[i]);

		vector<uint64_t> reverseLink;
		reverseLink.push_back(rightContig.getValue());
		reverseLink.push_back(otherRightStrand);
		reverseLink.push_back(leftContig.getValue());
		reverseLink.push_back(otherLeftStrand);
		reverseLink.push_back(distance);

		children[rightContig][otherRightStrand].push_back(reverseLink);
		parents[leftContig][otherLeftStrand].push_back(reverseLink);
	}

	// add colors to the graph
	map<PathHandle,int> colors;
	map<int,vector<PathHandle> > colorMap;
	int i=0;
	for(set<PathHandle>::iterator j=vertices.begin();j!=vertices.end();j++){
		colors[*j]=i;
		colorMap[i].push_back(*j);
		i++;
	}
	
	// do some color merging.
	for(set<PathHandle>::iterator j=vertices.begin();j!=vertices.end();j++){
		PathHandle vertex=*j;
		char state='F';
		if(children.count(vertex)>0&&children[vertex].count(state)>0
			&&children[vertex][state].size()==1){
			PathHandle childVertex=children[vertex][state][0][2];
			char childState=children[vertex][state][0][3];
			if(parents[childVertex][childState].size()==1
			&&validCounts[vertex][childVertex]==1){
				int currentColor=colors[vertex];
				int childColor=colors[childVertex];
				if(currentColor!=childColor){

					for(int i=0;i<(int)colorMap[childColor].size();i++){
						PathHandle otherVertex=colorMap[childColor][i];
						colors[otherVertex]=currentColor;
						colorMap[currentColor].push_back(otherVertex);
					}
					colorMap.erase(childColor);
				}
			}
		}
		state='R';
		if(children.count(vertex)>0&&children[vertex].count(state)>0
			&&children[vertex][state].size()==1){
			PathHandle childVertex=children[vertex][state][0][2];
			char childState=children[vertex][state][0][3];
			if(parents[childVertex][childState].size()==1
			&& validCounts[vertex][childVertex]==1){
				int currentColor=colors[vertex];
				int childColor=colors[childVertex];
				if(currentColor!=childColor){
					for(int i=0;i<(int)colorMap[childColor].size();i++){
						PathHandle otherVertex=colorMap[childColor][i];
						colors[otherVertex]=currentColor;
						colorMap[currentColor].push_back(otherVertex);
					}
					colorMap.erase(childColor);
				}
			}
		}
	}

	// extract scaffolds
	set<int>completedColours;
	for(set<PathHandle>::iterator j=vertices.begin();j!=vertices.end();j++){
		PathHandle vertex=*j;
		extractScaffolds('F',&colors,vertex,&parents,&children,&completedColours,scaffoldContigs,scaffoldStrands,scaffoldGaps);
		extractScaffolds('R',&colors,vertex,&parents,&children,&completedColours,scaffoldContigs,scaffoldStrands,scaffoldGaps);
	}



	// add unscaffolded stuff.
	for(int i=0;i<(int)m_vertices->size();i++){
		PathHandle contig=m_vertices->at(i).getName();
		if(colors.count(contig)==0){
			vector<PathHandle> contigs;
			vector<char> strands;
			vector<int> gaps;
			contigs.push_back(contig);
			strands.push_back('F');
			scaffoldContigs->push_back(contigs);
			scaffoldStrands->push_back(strands);
			scaffoldGaps->push_back(gaps);
		}
	}

	/* END OF CODE TO MOVE */

	LargeCount total=0;

	int numberOfLarge=0;
	for(int i=0;i<(int)scaffoldContigs->size();i++){
		int scaffoldLength=0;
		for(int j=0;j<(int)scaffoldContigs->at(i).size();j++){
			scaffoldLength+=m_lengths[scaffoldContigs->at(i)[j]];
		}

		for(int j=0;j<(int)scaffoldGaps->at(i).size();j++){
			scaffoldLength+=scaffoldGaps->at(i)[j];
		}

		if(scaffoldLength>=threshold){
			numberOfLarge++;
		}
		total+=scaffoldLength;
	}

	cout<<scaffoldContigs->size()<<" scaffolds, "<<numberOfLarge<<" >= 500"<<endl;
	cout<<"Total: "<<total<<" nucleotides"<<endl;

}

void ScaffoldingAlgorithm::addEdge(ScaffoldingEdge*edge){
	//cout<<"Adding edge "<<endl;
	//edge->print();
	PathHandle leftContig=edge->getLeftContig();
	PathHandle rightContig=edge->getRightContig();

	m_addedEdges[leftContig][rightContig]=*edge;
	m_addedEdges[rightContig][leftContig]=*edge;
	m_numberOfEdges++;
}

bool ScaffoldingAlgorithm::hasConflict(ScaffoldingEdge*edge){
	PathHandle leftContig=edge->getLeftContig();
	PathHandle rightContig=edge->getRightContig();

	if(hasConflictWithContig(edge,leftContig))
		return true;

	if(hasConflictWithContig(edge,rightContig))
		return true;

	return false;

}

bool ScaffoldingAlgorithm::hasConflictWithContig(ScaffoldingEdge*edge,PathHandle contig){
	/* the contig is now connected */
	if(m_addedEdges.count(contig) == 0)
		return false;

	/* check if there is a conflit between existing edges */
	for(map<PathHandle,ScaffoldingEdge>::iterator i=m_addedEdges[contig].begin();
			i!=m_addedEdges[contig].end();i++){
		ScaffoldingEdge otherEdge=i->second;
		
		if(hasConflictWithEdge(edge,&otherEdge))
			return true;
	}

	return false;
}

bool ScaffoldingAlgorithm::hasConflictWithEdge(ScaffoldingEdge*edgeToBeAdded,ScaffoldingEdge*alreadyAcceptedEdge){
	PathHandle edge1LeftContig=edgeToBeAdded->getLeftContig();
	PathHandle edge1RightContig=edgeToBeAdded->getRightContig();
	PathHandle edge2LeftContig=alreadyAcceptedEdge->getLeftContig();
	PathHandle edge2RightContig=alreadyAcceptedEdge->getRightContig();

	if(edge1LeftContig == edge2LeftContig){
		if(hasConflictWithEdgeAroundContig(edgeToBeAdded,alreadyAcceptedEdge,edge1LeftContig)){
			return true;
		}
	}

	if(edge1LeftContig == edge2RightContig){
		if(hasConflictWithEdgeAroundContig(edgeToBeAdded,alreadyAcceptedEdge,edge1LeftContig)){
			return true;
		}
	}

	if(edge1RightContig== edge2LeftContig){
		if(hasConflictWithEdgeAroundContig(edgeToBeAdded,alreadyAcceptedEdge,edge1RightContig)){
			return true;
		}
	}

	if(edge1RightContig== edge2RightContig){
		if(hasConflictWithEdgeAroundContig(edgeToBeAdded,alreadyAcceptedEdge,edge1RightContig)){
			return true;
		}
	}

	return false;
}

bool ScaffoldingAlgorithm::hasConflictWithEdgeAroundContig(ScaffoldingEdge*edgeToBeAdded,ScaffoldingEdge*alreadyAcceptedEdge,PathHandle contigToCheck){

	int edge1ContigSide=edgeToBeAdded->getSide(contigToCheck);
	char edge1ContigStrand=edgeToBeAdded->getStrand(contigToCheck);

	int edge2ContigSide=alreadyAcceptedEdge->getSide(contigToCheck);
	char edge2ContigStrand=alreadyAcceptedEdge->getStrand(contigToCheck);

	if(edge1ContigStrand == edge2ContigStrand){
		if(edge1ContigSide == edge2ContigSide){
			cout<<"Conflict for contig "<<contigToCheck<<" Length= "<<m_lengths[contigToCheck]<<endl;
			cout<<"Edge to be added:"<<endl;
			edgeToBeAdded->print();
			cout<<"Already added edge:"<<endl;
			alreadyAcceptedEdge->print();
			m_conflicts[edgeToBeAdded->getPriority()][alreadyAcceptedEdge->getPriority()]++;
			return true;
		}
	}else{
		ScaffoldingEdge reverseEdge=alreadyAcceptedEdge->getReverseEdge();

		int edge2ContigSideReverse=reverseEdge.getSide(contigToCheck);
	
		#ifdef CONFIG_ASSERT
		char edge2ContigStrandReverse=reverseEdge.getStrand(contigToCheck);
		assert(edge2ContigStrandReverse == edge1ContigStrand);
		#endif

		if(edge1ContigSide == edge2ContigSideReverse){
			cout<<"Conflict (reverse) for contig "<<contigToCheck<<" Length= "<<m_lengths[contigToCheck]<<endl;
			cout<<"Edge to be added:"<<endl;
			edgeToBeAdded->print();
			cout<<"Already added edge:"<<endl;
			alreadyAcceptedEdge->print();
			m_conflicts[edgeToBeAdded->getPriority()][alreadyAcceptedEdge->getPriority()]++;
			return true;
		}
	}

	return false;
}

void ScaffoldingAlgorithm::extractScaffolds(char state,map<PathHandle,int>*colors,PathHandle vertex,
	map<PathHandle,map<char,vector<vector<uint64_t> > > >*parents,
	map<PathHandle,map<char,vector<vector<uint64_t> > > >*children,set<int>*completedColours,

	vector<vector<PathHandle> >*scaffoldContigs,
	vector<vector<char> >*scaffoldStrands,
	vector<vector<int> >*scaffoldGaps
){
	vector<PathHandle> contigs;
	vector<char> strands;
	vector<int> gaps;
	bool skip=false;
	int currentColor=(*colors)[vertex];
	if((*completedColours).count(currentColor)>0)
		return;

	if((*parents).count(vertex)>0&&(*parents)[vertex].count(state)>0){
		for(int i=0;i<(int)(*parents)[vertex][state].size();i++){
			#ifdef CONFIG_ASSERT
			assert(0<(*parents)[vertex][state][i].size());
			#endif
			PathHandle parent=(*parents)[vertex][state][i][0];
			int parentColor=(*colors)[parent];
			if(parentColor==currentColor){
				skip=true;
				break;
			}
		}
	}
	if(skip)
		return;

	(*completedColours).insert(currentColor);
	bool done=false;
	while(!done){
		contigs.push_back(vertex);
		strands.push_back(state);
		if((*children).count(vertex)>0&&(*children)[vertex].count(state)>0){
			bool found=false;
			for(int i=0;i<(int)(*children)[vertex][state].size();i++){
				#ifdef CONFIG_ASSERT
				assert(2<(*children)[vertex][state][i].size());
				#endif

				PathHandle childVertex=(*children)[vertex][state][i][2];
				int childColor=(*colors)[childVertex];
				if(childColor==currentColor){
					#ifdef CONFIG_ASSERT
					assert(3<(*children)[vertex][state][i].size());
					#endif

					char childState=(*children)[vertex][state][i][3];

					#ifdef CONFIG_ASSERT
					assert(4<(*children)[vertex][state][i].size());
					#endif

					int gap=(*children)[vertex][state][i][4];
					gaps.push_back(gap);

					vertex=childVertex;
					state=childState;
					found=true;
					break;
				}
			}
			if(!found){
				done=true;
			}
		}else{
			done=true;
		}
	}
	scaffoldContigs->push_back(contigs);
	scaffoldStrands->push_back(strands);
	scaffoldGaps->push_back(gaps);
	return;
}