-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFindIndepentSet_v2.h
189 lines (157 loc) · 4.48 KB
/
FindIndepentSet_v2.h
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
// NO BARCODE FILTERING, NODES ARE SELECTED AT RANDOM TO ADD TO THE UNIQUE SET
class Network{
public:
Network();
~Network();
vector<string> PutBarcodes; //Keep the putative barcodes
unsigned long int NofPutBarcodes;
vector <string> CommonSet;
vector< vector <double> > Features;
bool RetrieveUniqueNodes(string);
void PrintUniquePutBarcodes(const char*);
static string RevComp(string);
void PrintEditDistanceDistribution(char *fname);
private:
int CalculateEditDistance(string, string, unsigned int **d);
bool printLock;
unsigned int* editDistDistribution;
unsigned int*** d;
};
Network::Network(){
editDistDistribution = new unsigned int[SeqLen+1];
for (int i=0; i<=SeqLen; ++i)
editDistDistribution[i] = 0;
//Create matrices for all edit distance computations
int maxThreads = omp_get_max_threads();
d = new unsigned int**[maxThreads];
for (int k=0; k < maxThreads; ++k ) {
d[k] = new unsigned int*[SeqLen+1+2*padding];
for (int i=0;i<=SeqLen+2*padding;++i) {
d[k][i] = new unsigned int[SeqLen+1];
}
d[k][0][0] = 0;
for(int x = 1; x <= SeqLen+2*padding; ++x) d[k][x][0] = 0;
for(int x = 1; x <= SeqLen; ++x) d[k][0][x] = x;
}
printLock = false;
}
Network::~Network() {
delete[] editDistDistribution;
int maxThreads = omp_get_max_threads();
for (int k=0; k < maxThreads; ++k ) {
for (int i=0;i<=SeqLen;++i) {
delete[] d[k][i];
}
delete[] d[k];
}
delete[] d;
}
string Network::RevComp(string s){
int z;
string s_revcom;
for(z=(s.length()-1);z>=0;--z){
switch (s[z]){
case 'A' : { s_revcom.append("T"); break; }
case 'C' : { s_revcom.append("G"); break; }
case 'G' : { s_revcom.append("C"); break; }
case 'T' : { s_revcom.append("A"); break; }
}
}
return s_revcom;
}
int Network::CalculateEditDistance(string s1, string s2, unsigned int **d){
int x,y;
int cost = 0;
string s2_revcom;
const int len=SeqLen;
if (d == NULL) {
fprintf(stdout,"Something went very wrong\n");
return 0;
}
for(x = 1; x <= len+2*padding; ++x)
for(y = 1; y <= len; ++y) {
if (x<=padding || x>len+padding) //Wildcard!
cost = 0;
else
cost = (s1[x - padding - 1] == s2[y - 1] ? 0 : 1);
d[x][y] = std::min( std::min(d[x - 1][y] + 1,d[x][y - 1] + 1),d[x - 1][y - 1] + cost );
}
//Find min for sub-global alignment
int min_ed = 1000;
for (int i=len+2*padding ; i>len-1;--i) {
if (d[i][len] < min_ed) {
min_ed = d[i][len];
}
}
if(min_ed > EditDistanceThreshold){
s2_revcom=Network::RevComp(s2);
for(x = 1; x <= len+2*padding; ++x)
for(y = 1; y <= len; ++y) {
if (x<=padding || x>len+padding) //Wildcard!
cost = 0;
else
cost = (s1[x - padding - 1] == s2_revcom[y - 1] ? 0 : 1);
d[x][y] = std::min( std::min(d[x - 1][y] + 1,d[x][y - 1] + 1),d[x - 1][y - 1] + cost );
}
for (int i=len+2*padding ; i>len-1;--i) {
if (d[i][len] < min_ed) {
min_ed = d[i][len];
}
}
}
return min_ed;
}
bool Network::RetrieveUniqueNodes(string putbarcode){
long int k;
bool addtoset=true;
bool done=false;
unsigned int* localDistribution = new unsigned int[SeqLen+1];
for (int i=0; i<=SeqLen; ++i) localDistribution[i] = 0;
#pragma omp parallel for default(shared) schedule(dynamic,200)
for(k=0;k<CommonSet.size();++k){ // Make sure if the last selected node is not connected to already present nodes in the common set
int id = omp_get_thread_num();
//fprintf(stdout,"%d ",id);
if(!done){
unsigned int editdist = CalculateEditDistance(putbarcode,CommonSet[k],d[id]); // Check reverse complement also
++localDistribution[editdist];
if(editdist<=EditDistanceThreshold){
#pragma omp critical
{
addtoset=false;
done=true;
}
}
}
}
if(addtoset) {
CommonSet.push_back(putbarcode); //Add it to the set if it not connected to the previously added members
printLock = false;
//Also add the distribution parameters
for (int i=0; i<SeqLen; ++i) {
editDistDistribution[i] += localDistribution[i];
}
}
delete[] localDistribution;
if(CommonSet.size()%500==0 && !(printLock)) {
cout << CommonSet.size() << " Unique Barcodes Selected" << endl;
printLock = true;
}
return addtoset;
}
void Network::PrintUniquePutBarcodes(const char *fname){
int i=0;
ofstream outf(fname);
for(i=0;i<CommonSet.size();i++){
outf << CommonSet[i] << endl;
}
outf.close();
}
void Network::PrintEditDistanceDistribution(char *fname)
{
// Print Edit Distance Distribution
ofstream outf2(fname);
for(int ed=0;ed<SeqLen;ed++){
outf2 << ed << '\t' << editDistDistribution[ed] << endl;
}
outf2.close();
}