File: clustering.h

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 (170 lines) | stat: -rw-r--r-- 5,150 bytes parent folder | download
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
#ifndef CLUSTERING_H
#define CLUSTERING_H
/*
 *  abundances.h
 *  cufflinks
 *
 *  Created by Cole Trapnell on 4/27/09.
 *  Copyright 2009 Cole Trapnell. All rights reserved.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// DON'T move this, or mystery compiler errors will result. Affects gcc >= 4.1
#include <boost/graph/vector_as_graph.hpp>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/visitors.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/connected_components.hpp>

#ifdef DEBUG
#include <boost/numeric/ublas/io.hpp>
#endif

#include <boost/version.hpp>

#if (BOOST_VERSION < 103800)
#include <boost/vector_property_map.hpp>
#else
#include <boost/property_map/vector_property_map.hpp>
#endif


#include "abundances.h"

using namespace boost;

typedef adjacency_list <vecS, vecS, undirectedS> AbundanceGraph;

struct ConnectByExonOverlap
{
	void operator()(const AbundanceGroup& cluster,
					AbundanceGraph& G);
};

struct ConnectByAnnotatedGeneId
{
	void operator()(const AbundanceGroup& cluster,
					AbundanceGraph& G);
};

struct ConnectByAnnotatedTssId
{
	void operator()(const AbundanceGroup& cluster,
					AbundanceGraph& G);
};

struct ConnectByAnnotatedProteinId
{
	void operator()(const AbundanceGroup& cluster,
					AbundanceGraph& G);
};

struct ConnectByStrand
{
	void operator()(const AbundanceGroup& cluster,
					AbundanceGraph& G);
};

// A "transcript cluster is a set of transcripts whose projections into the
// genome overlap on the same strand.  They may thus share fragment alignments,
// and so they need to be quantitated together.  After quantitation, they
// can be picked apart.
template<class cluster_policy>
void cluster_transcripts(const AbundanceGroup& transfrags,
						 vector<AbundanceGroup>& transfrags_by_cluster,
						 ublas::matrix<double>* new_gamma = NULL,
                         ublas::matrix<double>* new_iterated_count = NULL,
                         ublas::matrix<double>* new_count = NULL,
                         ublas::matrix<double>* new_fpkm = NULL,
                         ublas::matrix<double>* new_gamma_bootstrap = NULL)
{
	adjacency_list <vecS, vecS, undirectedS> G;
	
	transfrags_by_cluster.clear();
	
	cluster_policy cp;
	
	cp(transfrags, G);
	
	std::vector<int> component(num_vertices(G));
	connected_components(G, &component[0]);
	
	vector<vector<bool> > clusters(transfrags.abundances().size(), 
								   vector<bool>(transfrags.abundances().size(), false));
	
	vector<vector<size_t> > cluster_indices(transfrags.abundances().size());
	for (size_t i = 0; i < transfrags.abundances().size(); ++i)
	{
		clusters[component[i]][i] = true;
		cluster_indices[component[i]].push_back(i);
	}
	for (size_t i = 0; i < cluster_indices.size(); ++i)
	{
		if (cluster_indices[i].empty())
		{
			cluster_indices.resize(i);
			break;
		}
	}
	for (size_t i = 0; i < clusters.size(); ++i)
	{
		AbundanceGroup cluster;
		transfrags.filter_group(clusters[i], cluster);
		if (!cluster.abundances().empty())
			transfrags_by_cluster.push_back(cluster);
	}
	
	if (new_gamma != NULL)
	{
		const ublas::matrix<double>& trans_gamma_cov = transfrags.gamma_cov();
        const ublas::matrix<double>& trans_gamma_bootstrap_cov = transfrags.gamma_bootstrap_cov();
        const ublas::matrix<double>& trans_iterated_count_cov = transfrags.iterated_count_cov();
        const ublas::matrix<double>& trans_count_cov = transfrags.count_cov();
        const ublas::matrix<double>& trans_fpkm_cov = transfrags.fpkm_cov();
        
		ublas::matrix<double>& cov = *new_gamma;
        ublas::matrix<double>& boot_cov = *new_gamma_bootstrap;
        ublas::matrix<double>& iterated_count_cov = *new_iterated_count;
        ublas::matrix<double>& count_cov = *new_count;
        ublas::matrix<double>& fpkm_cov = *new_fpkm;
        
		// number of primary transcripts for this gene
		size_t num_pt = cluster_indices.size();
		cov = ublas::zero_matrix<double>(num_pt, num_pt);
        boot_cov = ublas::zero_matrix<double>(num_pt, num_pt);
        count_cov = ublas::zero_matrix<double>(num_pt, num_pt);
        iterated_count_cov = ublas::zero_matrix<double>(num_pt, num_pt);
        fpkm_cov = ublas::zero_matrix<double>(num_pt, num_pt);
		//cerr << "combined " << combined << endl;
		
		//cerr << "locus isoform gamma cov" << gamma_cov << endl;
		for (size_t L = 0; L < cluster_indices.size(); ++L)
		{
			const vector<size_t>& L_isos = cluster_indices[L];
			for (size_t K = 0; K < cluster_indices.size(); ++K)
			{
				const vector<size_t>& K_isos = cluster_indices[K];
				for (size_t l = 0; l < L_isos.size(); ++l)
				{
					for (size_t k = 0; k < K_isos.size(); ++k)
					{
						cov(L,K) += trans_gamma_cov(L_isos[l],K_isos[k]);
                        boot_cov(L,K) += trans_gamma_bootstrap_cov(L_isos[l],K_isos[k]);
                        count_cov(L,K) += trans_count_cov(L_isos[l],K_isos[k]);
                        iterated_count_cov(L,K) += trans_iterated_count_cov(L_isos[l],K_isos[k]);
                        fpkm_cov(L,K) += trans_fpkm_cov(L_isos[l],K_isos[k]);
					}
				}
			}
		}
	}
}

#endif