-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetReadEnds.cpp
89 lines (65 loc) · 2.05 KB
/
GetReadEnds.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
#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;
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 main (int argc, char **argv) {
cerr << "GetReadEnds <alignment.sam>" << endl;
ifstream inf(argv[1]);
while(inf){
string strInput;
getline(inf, strInput);
if(strInput.length() > 0){
vector<string> vec;
split(vec, strInput, is_any_of("\t"));
int begin_point, end_point;
pair<int,int> add_len = AddLen(vec[5]);
if(vec[1] == "0"){
begin_point = lexical_cast<int>(vec[3]) + add_len.first;
end_point = lexical_cast<int>(vec[3]) + add_len.second;
}
else if(vec[1] == "16"){
begin_point = lexical_cast<int>(vec[3]) + add_len.second;
end_point = lexical_cast<int>(vec[3]) + add_len.first;
}
else ;
cout << vec[0] << '\t' << vec[2] << '\t' << begin_point << '\t' << end_point << endl;
}
}
inf.close();
return 0;
}