-
Notifications
You must be signed in to change notification settings - Fork 4
/
1.find_position_bam.cpp
130 lines (109 loc) · 3.28 KB
/
1.find_position_bam.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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "api/BamReader.h"
using namespace std;
using namespace BamTools;
void usage() {
cout << "Digenome Toolkit - 1.find_position_bam" << endl << endl
<< "Usage: 1.find_position_bam [-p prefix] BAM_file" << endl << endl;
exit(0);
}
int main(int argc, char** argv) {
FILE *fsp = 0, *fep = 0;
int flag; // 0
char chrom[255]; // 1
int pos; // 2
int quality; // 3
char *cigar; // 4
size_t len = 0;
char chromcopy[255] = { 0 };
char fn[255];
int i;
int forward, reverse;
bool direction; // 0
bool leftflag;
const char *prefix = "";
int cnt = 0;
BamReader reader;
if (argc < 2 || argc > 4) usage();
for (i=1; i<argc; i++) {
if ((strcmp(argv[i], "-p") == 0) && (i != argc-1)) {
prefix = argv[++i];
} else {
if (!reader.IsOpen()) {
try {
reader.Open(argv[i]);
} catch (exception& e) {
cout << e.what();
throw;
}
} else {
usage();
}
}
}
if (!reader.IsOpen()) usage();
const SamHeader header = reader.GetHeader();
const RefVector references = reader.GetReferenceData();
BamAlignment al;
while(reader.GetNextAlignmentCore(al)) {
flag = al.AlignmentFlag;
if ((flag & 256) || (flag & 4) || (flag & 512) || (flag & 1024)) continue;
direction = ( (flag & 16) == 16);
if (al.RefID < references.size()) {
strcpy(chrom, references[al.RefID].RefName.c_str());
} else {
cout << "Warning: No reference found in header! Skipping..." << endl;
continue;
}
pos = al.Position+1;
quality = al.MapQuality;
if (quality == 0) continue;
if (strcmp(chromcopy, chrom) != 0) {
if (fsp) fclose(fsp);
if (fep) fclose(fep);
fn[0] = 0;
if (prefix != "") {
strcat(fn, prefix);
strcat(fn, "_");
}
strcat(fn, chrom);
strcat(fn, "_forward.txt");
fsp = fopen(fn, "w");
fn[0] = 0;
if (prefix != "") {
strcat(fn, prefix);
strcat(fn, "_");
}
strcat(fn, chrom);
strcat(fn, "_reverse.txt");
fep = fopen(fn, "w");
strcpy(chromcopy, chrom);
}
vector<CigarOp> cigars = al.CigarData;
forward = pos;
reverse = pos;
leftflag = true;
for (i = 0; i < cigars.size(); i++) {
if (cigars[i].Type == 'S' || cigars[i].Type == 'H') {
if (leftflag == false) break;
continue;
} else {
if (cigars[i].Type != 'I') reverse += cigars[i].Length;
leftflag = false;
}
}
if (direction == 0)
fprintf(fsp, "%d\n", forward);
else
fprintf(fep, "%d\n", reverse-1);
cnt++;
if (cnt % 100000 == 0)
printf("%d\n", cnt);
}
reader.Close();
if (fsp) fclose(fsp);
if (fep) fclose(fep);
}