File: matching_merge.cpp

package info (click to toggle)
cufflinks 1.3.0-2
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 3,864 kB
  • sloc: cpp: 48,999; ansic: 12,297; sh: 3,381; python: 432; makefile: 209
file content (103 lines) | stat: -rw-r--r-- 2,438 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
/*
 *  matching_merge.cpp
 *  cufflinks
 *
 *  Created by Cole Trapnell on 6/1/10.
 *  Copyright 2010 Cole Trapnell. All rights reserved.
 *
 */

#include "matching_merge.h"

using namespace std;
using namespace boost;

void find_path(const DAG& bundle_dag,
			   const adjacency_list<>& TC,
			   const DAGNode& source,
			   const DAGNode& target,
			   vector<DAGNode>& path)
{
	if (source == target)
		return;
	bool done = false;
	DAGNode curr = source;
	while(!done)
	{
		graph_traits<DAG>::adjacency_iterator i, iend;
		for (tie(i,iend) = adjacent_vertices(curr, bundle_dag); i != iend; ++i)
		{
			DAGNode I = *i;
			pair<adjacency_list<>::edge_descriptor, bool> p;
			p = edge(I, target, TC);
			if (p.second)
			{
				path.push_back(*i);
				curr = *i;
				break;
			}
			if (*i == target)
			{
				path.push_back(*i);
				done = true;
				break;
			}
		}
	}
}

void extend_chains_to_paths(const DAG& bundle_dag,
							vector<vector<DAGNode> >& chains,
							adjacency_list<>& TC,
							DAGNode source,
							DAGNode sink,
							vector<vector<DAGNode> >& paths)
{	
	//Extend each chain to a path
	for(size_t c = 0; c < chains.size(); ++c)
	{
		vector<DAGNode>& chain = chains[c];
		assert (!chain.empty());
		reverse(chain.begin(), chain.end());
		vector<DAGNode> path;
		find_path(bundle_dag, TC, source, chain[0], path);
		for (size_t n = 1; n < chain.size(); ++n)
		{
			assert (path.back() == chain[n - 1]);
			DAGNode last = chain[n-1];
			DAGNode next = chain[n];
			find_path(bundle_dag, TC, last, next, path);
		}
		find_path(bundle_dag, TC, chain.back(), sink, path);
		assert (path.back() == sink);
		path.pop_back();
		paths.push_back(path);
	}
}

void make_scaffolds_from_paths(DAG& bundle_dag,
							   const vector<vector<DAGNode> >& paths,
							   vector<Scaffold>& scaffolds)
{
	HitsForNodeMap hits_for_node = get(vertex_name, bundle_dag);
	for (size_t p = 0; p < paths.size(); ++p)
	{
		const vector<DAGNode>& path = paths[p];
		
		vector<Scaffold> path_alignments;
		for (size_t m = 0; m < path.size(); ++m)
		{
			//fprintf(stderr, "%d ", scaff_id);
			path_alignments.push_back(*(hits_for_node[path[m]]));
		}
		//fprintf(stderr,"\n");
		//fprintf(stderr, "\tMerging path %d into scaffold\n", p);
		Scaffold s(path_alignments);
		
		//fprintf(stderr, "PATH %d\n-------------------------------------\n",s.get_id());
		scaffolds.push_back(s);
	}
	sort(scaffolds.begin(), scaffolds.end(), scaff_lt);
}