-
Notifications
You must be signed in to change notification settings - Fork 1
/
LRPASPeak.cpp
194 lines (138 loc) · 4.44 KB
/
LRPASPeak.cpp
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
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
struct PEAK {
string chro;
//string orien;
//string name;
int pos;
};
int compare_peak_sort (const void *a, const void *b) {
if( (*(PEAK*) a).chro > (*(PEAK*) b).chro ) return 1;
else if ( (*(PEAK*) a).chro == (*(PEAK*) b).chro ){
if( (*(PEAK*) a).pos >= (*(PEAK*) b).pos ) return 1;
else return -1;
}
else return -1;
}
int compare_peak (PEAK a, PEAK b) {
if(a.chro > b.chro) return 1;
else if(a.chro == b.chro){
if(a.pos >= b.pos) return 1;
else return -1;
}
else return -1;
}
vector<PEAK> ReadInPeakAndSort (ifstream &inf) {
vector<PEAK> pk_vec;
while(inf){
string strInput;
getline(inf, strInput);
if(strInput.length() > 0){
vector<string> vec;
split(vec, strInput, is_any_of("\t"));
PEAK pk;
pk.chro = vec[0];
//pk.orien = vec[1];
//pk.name = vec[2];
//pk.pos = lexical_cast<int>(vec[3]);
pk.pos = lexical_cast<int>(vec[1]);
pk_vec.push_back(pk);
}
}
qsort(&pk_vec[0], pk_vec.size(), sizeof(PEAK), compare_peak_sort);
return pk_vec;
}
int AddLen (string &cigar, char orien) {
char ch[5000];
int index = 0;
int aln_len = 0;
int S_num = 0;
int left_S = 0;
int right_S = 0;
for(int i = 0; i < cigar.length(); ++i){
if(cigar[i] > 'A' && cigar[i] < 'Z'){
for(int j = index; j < 5000; ++j) ch[j] = '\0';
int number = atoi(ch);
index = 0;
if(cigar[i] == 'M' || cigar[i] == 'D' || cigar[i] == 'N') aln_len += number;
else if(cigar[i] == 'S' || cigar[i] == 'H'){
++S_num;
if(S_num == 1) left_S = number;
else if(S_num == 2) right_S = number;
else ;
}
}
else{
ch[index] = cigar[i];
++index;
}
}
int add_len = 0;
if(orien == '+') add_len = aln_len + right_S;
else if(orien == '-') add_len = -left_S;
else ;
return add_len;
}
int BinarySearch (vector<PEAK> &pk_vec, PEAK pk_query) {
int up = 0;
int down = pk_vec.size() - 1;
while(down - up > 1){
int middle = (up + down) / 2;
if(compare_peak(pk_vec[middle],pk_query) == 1) down = middle;
else up = middle;
}
return up;
}
int main (int argc, char **argv) {
cerr << "LRCAGEPeak <peaks.txt> <alignment.sam>" << endl;
cerr << "The <peaks.txt> file contains two columns: chro & pos" << endl;
ifstream pinf(argv[1]);
ifstream sinf(argv[2]);
vector<PEAK> pk_vec = ReadInPeakAndSort(pinf);
int count[pk_vec.size()];
for(int i = 0; i < pk_vec.size(); ++i) count[i] = 0;
while(sinf){
string strInput;
getline(sinf, strInput);
if(strInput.length() > 0){
vector<string> vec;
split(vec, strInput, is_any_of("\t"));
int end_point = 0;
if(vec[1] == "0"){
end_point = lexical_cast<int>(vec[3]) + AddLen(vec[5], '+');
}
else if(vec[1] == "16"){
end_point = lexical_cast<int>(vec[3]) + AddLen(vec[5], '-');
}
else ;
//cout << end_point << endl;
PEAK pk_query;
pk_query.chro = vec[2];
pk_query.pos = end_point;
int locate = BinarySearch(pk_vec, pk_query);
int dist_up = 10000;
int dist_down = 10000;
if(pk_query.chro == pk_vec[locate].chro) dist_up = abs(pk_query.pos - pk_vec[locate].pos);
if(locate < pk_vec.size() - 1 && pk_query.chro == pk_vec[locate + 1].chro) dist_down = abs(pk_query.pos - pk_vec[locate + 1].pos);
if(dist_up < 30 && dist_up < dist_down) ++count[locate];
else if(dist_down < 30 && dist_down < dist_up) ++count[locate + 1];
else ;
}
}
for(int i = 0; i < pk_vec.size(); ++i){
if(count[i] > 0) cout << pk_vec[i].chro << '\t' << pk_vec[i].pos << '\t' << count[i] << endl;
}
pk_vec.clear();
pinf.close(); sinf.close();
return 0;
}