-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShiftPeak.cpp
196 lines (130 loc) · 4.32 KB
/
ShiftPeak.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
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <omp.h>
#include <vector>
#include <map>
#include <cstdlib>
#include <algorithm>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#define RANGE 100
using namespace std;
using namespace boost;
int AlignLen (string &cigar) {
char ch[5000];
int index = 0;
int aln_len = 0;
int S_num = 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{
ch[index] = cigar[i];
++index;
}
}
return aln_len;
}
int FindArgMax (string str, int start, int end) {
ifstream inf(str.c_str());
map<int,int> freq_map;
map<int,int>::iterator it;
while(inf){
string strInput;
getline(inf, strInput);
if(strInput.length() > 0){
vector<string> vec;
split(vec, strInput, is_any_of("\t"));
int pos = lexical_cast<int>(vec[3]);
if(vec[1] == "16") pos = pos + AlignLen(vec[5]) - 1; // For cage
//if(vec[1] == "0") pos = pos + AlignLen(vec[5]) - 1; // For PAS
it = freq_map.find(pos);
if(it == freq_map.end()) freq_map.insert(pair<int,int>(pos,1));
else ++(it->second);
}
}
inf.close();
int max = 0;
int arg_max = 0;
for(it = freq_map.begin(); it != freq_map.end(); ++it){
if(it->first > start - RANGE && it->first < end + RANGE && it->second > max){
max = it->second;
arg_max = it->first;
}
}
return arg_max;
}
int Shift (char *bam_file, string chro, int start, int end) {
char command[1000];
sprintf(command, "samtools view %s %s:%d-%d > SAMFILE", bam_file, chro.c_str(), start - RANGE, end + RANGE);
system(command);
int shift_peak = FindArgMax("SAMFILE", start, end);
string rm_file = "rm SAMFILE";
system(rm_file.c_str());
return shift_peak;
}
int main (int argc, char **argv) {
cerr << "ShiftPeak <ASPeak_results.txt> <sorted_bam_file> <output_file>" << endl;
ifstream inf(argv[1]);
char *bam_file = argv[2];
ofstream ouf(argv[3], ios::trunc);
while(inf){
string strInput;
getline(inf, strInput);
if(strInput.length() > 0){
vector<string> vec;
split(vec, strInput, is_any_of("\t"));
string chro = vec[0];
int start = lexical_cast<int>(vec[7]);
int end = lexical_cast<int>(vec[8]);
int shift_peak = Shift(bam_file, chro, start, end);
ouf << vec[0] << '\t' << vec[1] << '\t' << vec[2] << '\t' << shift_peak << endl;
}
}
inf.close(); ouf.close();
return 0;
}
/*int main (int argc, char **argv) {
//cerr << "ShiftPeak <cage_peak_maxdepth.txt> <sorted_bam_file> <output_file>" << endl;
cerr << "ShiftPeak <ASPeak_results.txt> <sorted_bam_file> <output_file>" << endl;
ifstream inf(argv[1]);
char *bam_file = argv[2];
ofstream ouf(argv[3], ios::trunc);
int line = 0;
string chro;
int gene_start, gene_end;
while(inf){
string strInput;
getline(inf, strInput);
++line;
if(strInput.length() > 0){
if(strInput[0] == '@'){
ouf << strInput << endl;
line = 0;
vector<string> vec;
split(vec, strInput, is_any_of("\t"));
chro = vec[1];
gene_start = lexical_cast<int>(vec[3]);
gene_end = lexical_cast<int>(vec[4]);
}
else if(line == 1) ouf << strInput << endl;
else{
vector<string> vec;
split(vec, strInput, is_any_of("\t"));
int pos = lexical_cast<int>(vec[0]);
//cout << chro << '\t' << pos << endl;
int shift_peak = Shift(bam_file, chro, gene_start, gene_end, pos);
ouf << shift_peak << '\t' << vec[1] << '\t' << vec[2] << endl;
}
}
}
inf.close(); ouf.close();
return 0;
}*/