-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCanopy.cpp
More file actions
143 lines (124 loc) · 4.39 KB
/
Copy pathCanopy.cpp
File metadata and controls
143 lines (124 loc) · 4.39 KB
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
/**
* Metagenomics Canopy Clustering Implementation
*
* Copyright (C) 2013, 2014 Piotr Dworzynski (piotr@cbs.dtu.dk), Technical University of Denmark
*
* This file is part of Metagenomics Canopy Clustering Implementation.
*
* Metagenomics Canopy Clustering Implementation 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, either version 2 of the License, or
* (at your option) any later version.
*
* Metagenomics Canopy Clustering Implementation 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 should have received a copy of the GNU General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Metagenomics Canopy Clustering Implementation v2
all copyrights lie with Falk Hildebrand (falk.hildebrand [ta] gmail dot com)
*/
#include "Canopy.hpp"
Canopy::Canopy(Point* center_to_copy, int deletedSmpls, bool replID):
ori_index(-1), center(new Point(center_to_copy, deletedSmpls)), neighbours(0), corrs(0){
if (replID) {
center->id = "!GENERATED!";
}
}
Canopy::Canopy(std::vector< Point*>& neighbours, int deletedSmpls):
ori_index(-1), center(nullptr), neighbours(neighbours), corrs(neighbours.size()){
find_and_set_center(deletedSmpls);
}
void Canopy::print2file(ofstream* out_file_memb, ofstream* out_file_pro,
const options& opt, int kk, int num_digits,bool guided) {
string output_cluster_prefix = opt.output_cluster_prefix;
if (!guided) {
//members
for (Point* p : this->neighbours) {
*out_file_memb << output_cluster_prefix << std::setw(num_digits) << std::setfill('0') << kk << "\t";
*out_file_memb << p->id;
*out_file_memb << "\n";
}
if (out_file_pro != NULL) { //classical canopy write out
//profile
*out_file_pro << output_cluster_prefix << std::setw(num_digits) << std::setfill('0') << kk;
for (int j = 0; j < this->center->num_data_samples; j++) {
*out_file_pro << "\t" << this->center->getData(j);
}
*out_file_pro << "\n";
}
}
else {//this is a post correlation analysis..
string curID = this->center->id;
//cerr << "Writing canopy " << curID << ", size = "<< neighbours.size() <<endl;
//vector<Point*> nei = this->neighbours;
//vector<PRECISIONT> corres = this->corrs.begin();
if (neighbours.size() != corrs.size()) {
cerr << curID << "\n";
cerr << "Found unequal neighbours(" << neighbours.size() << ") and corrs(" << corrs.size() << ") size\n";
exit(932);
}
size_t j = 0;
while ( j < neighbours.size() && j < corrs.size() ) {
Point* p = neighbours[j];
//PRECISIONT dist = get_distance_between_points(c->center, p);
*out_file_memb << curID << "\t";
*out_file_memb << p->id;
*out_file_memb << "\t" << corrs[j] << "\n";
j++;
}
//cerr << " wrote " << j << " genes out\n";
}
}
void Canopy::find_and_set_center(int deletedSmpls){
center = get_centroid_of_points(neighbours, deletedSmpls);
}
uint Canopy::cleanUp() {
uint clndGs = 0;
uint osize = neighbours.size();
for (size_t i = 0; i < neighbours.size(); i++) {
if (neighbours[i] == nullptr){
//hasnull = true;// break;
clndGs++;
}
}
if (!clndGs) { return clndGs; }
//clean up//
std::vector< Point*> neighbours2(osize- clndGs);
vector<PRECISIONT> corrs2(osize - clndGs);
uint i2 = 0;
for (size_t i = 0; i < neighbours.size(); i++) {
if (neighbours[i] != nullptr) {
//neighbours2.push_back(neighbours[i]);
//clndGs++;
neighbours2[i2] = neighbours[i];
corrs2[i2] = corrs[i];
i2++;
}
}
neighbours = neighbours2;
corrs = corrs2;
return clndGs;
}
std::ostream& operator<<(std::ostream& ost, const Canopy& c)
{
ost << ">>>>>>>>>>Canopy>>>>>>>>" << std::endl;
ost << "Center:" << std::endl;
if(c.center != NULL)
ost << *c.center;
else
ost << "===NONE===" << endl;
ost << "Neighbours: " << c.neighbours.size() << std::endl;
//BOOST_FOREACH(const Point* p, c.neighbours)
// ost << p->id << "\t";
//ost << std::endl;
ost << ">>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
return ost;
}
bool compare_canopy_ptrs_by_canopy_size(const shared_ptr<Canopy> a, const shared_ptr<Canopy> b){
return (a->neighbours.size() > b->neighbours.size());
}