-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupportByCAGEOutputPeak.cpp
198 lines (139 loc) · 4.65 KB
/
SupportByCAGEOutputPeak.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
195
196
197
198
#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 strand;
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.strand = vec[1];
pk.pos = lexical_cast<int>(vec[2]);
pk_vec.push_back(pk);
}
}
//qsort(&pk_vec[0], pk_vec.size(), sizeof(PEAK), compare_peak_sort);
return pk_vec;
}
pair<int,int> AddLen (string &cigar) {
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;
}
}
return pair<int,int>(-left_S, aln_len + right_S);
}
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 MatchPeak (vector<PEAK> &pk_vec, PEAK pk_query) {
int result = -1;
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 < dist_down && dist_up < 30 && pk_query.strand == pk_vec[locate].strand) result = locate;
else if(dist_down < 30 && pk_query.strand == pk_vec[locate + 1].strand) result = locate + 1;
else ;
return result;
}
int main (int argc, char **argv) {
cerr << "SupportByCAGEOutputPeak <cage_peak.txt> <sam_file>" << endl;
ifstream cageinf(argv[1]);
ifstream saminf(argv[2]);
vector<PEAK> cage_pkvec = ReadInPeakAndSort(cageinf);
while(saminf){
string strInput;
getline(saminf, strInput);
if(strInput.length() > 0){
vector<string> vec;
split(vec, strInput, is_any_of("\t"));
int end_point;
PEAK cage_query;
cage_query.chro = vec[2];
pair<int,int> add_len = AddLen(vec[5]);
if(vec[1] == "0"){
cage_query.strand = "+";
cage_query.pos = lexical_cast<int>(vec[3]) + add_len.first;
end_point = lexical_cast<int>(vec[3]) + add_len.second;
}
else if(vec[1] == "16"){
cage_query.strand = "-";
cage_query.pos = lexical_cast<int>(vec[3]) + add_len.second;
end_point = lexical_cast<int>(vec[3]) + add_len.first;
}
else ;
int cage_match = MatchPeak(cage_pkvec, cage_query);
if(cage_match >= 0){
cout << vec[0] << '\t';
cout << cage_pkvec[cage_match].chro << '\t' << cage_pkvec[cage_match].pos << '\t';
cout << vec[2] << '\t' << end_point << endl;
}
else cout << vec[0] << "\t*\t*\t" << vec[2] << '\t' << end_point << endl;
}
}
cage_pkvec.clear();
cageinf.close(); saminf.close();
return 0;
}