-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-coverage.cpp
More file actions
executable file
·166 lines (146 loc) · 4.59 KB
/
read-coverage.cpp
File metadata and controls
executable file
·166 lines (146 loc) · 4.59 KB
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
/*
============================================================================
Name : read-coverage.cpp
Author : Francesca Nadalin
Version :
Description : Calculate the distribution of reads on genomic locations
============================================================================
*/
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <map>
#include <sys/time.h>
#include <sys/resource.h>
#include "Sam.h"
#include "Bed.h"
#define RESERVE_BLOCK 100
int main(int argc, char * argv[]) {
clock_t time1 = clock();
if (argc < 4)
{
std::cout << "Usage: <bedfile> <bamfile> <outfile>" << std::endl;
std::cout << "N.B.: both files must be sorted by coordinate and 0-based" << std::endl;
std::cout << "Output file: BED format with number of reads in the 4th column" << std::endl;
return 1;
}
std::string bed_file(argv[1]);
std::string bam_file(argv[2]);
std::string out_file(argv[3]);
std::ifstream bed;
bed.open(bed_file.c_str());
if (not bed.is_open())
{
std::cerr << "Error while opening file " << bed_file << " for reading" << std::endl;
}
std::map<std::string, std::vector<Bed> > beds; // key: reference, value: vector of BED entries with reference = key
std::map<std::string, std::vector<size_t> > read_count; // key: reference, value: vector of the number of reads whose center falls in the BED entry pointed by key in beds
for (std::string line; std::getline(bed, line); )
{
Bed B(line);
if (beds.count(B.ref()) == 0)
{
std::vector<Bed> v; v.reserve(RESERVE_BLOCK);
std::vector<size_t> w; w.reserve(RESERVE_BLOCK);
beds.insert(std::pair<std::string, std::vector<Bed> >(B.ref(), v));
read_count.insert(std::pair<std::string, std::vector<size_t> >(B.ref(), w));
}
if (beds.find(B.ref())->second.capacity() == beds.find(B.ref())->second.size())
{
beds.find(B.ref())->second.reserve(beds.find(B.ref())->second.capacity() + RESERVE_BLOCK);
read_count.find(B.ref())->second.reserve(read_count.find(B.ref())->second.capacity() + RESERVE_BLOCK);
}
beds.find(B.ref())->second.push_back(B);
read_count.find(B.ref())->second.push_back(0);
}
bed.close();
Sam S(bam_file);
if (not S.is_open())
{
std::cerr << "Error while opening file " << bam_file << " for reading" << std::endl;
}
std::map< std::string, std::vector<Bed> >::iterator b = beds.begin();
std::map< std::string, std::vector<size_t> >::iterator r = read_count.begin();
size_t i = 0;
size_t n = 0;
while (S.read())
{
if (S.is_mapped())
{
// std::cout << "reading sam..." << std::endl;
// center of the read
size_t mid = S.start() + ((size_t) S.length()/2);
// std::cout << "ref: " << S.ref() << " start: " << S.start() << " length: " << S.length() << " mid: " << mid << std::endl;
// find the reference of the read in the bam file
if (S.ref().compare(b->first) != 0)
{
if (beds.count(S.ref()) > 0)
{
b = beds.find(S.ref());
r = read_count.find(S.ref());
}
i = 0;
}
// std::cout << "bed ref: " << b->first << std::endl;
if (S.ref().compare(b->first) == 0)
{
// find the bed entry that contains the read
while (i < b->second.size() and b->second.at(i).end() <= mid)
{
// std::cout << "bed end: " << b->second.at(i).end() << std::endl;
++i;
}
if (i < b->second.size() and b->second.at(i).start() <= mid)
{
// bed interval found!!
// std::cout << "bed end: " << b->second.at(i).end() << std::endl;
// std::cout << "bed start: " << b->second.at(i).start() << std::endl;
// std::cout << "bed interval found!!" << std::endl;
++(r->second.at(i));
}
}
}
++n;
if (n%100000 == 0)
{
std::cout << ".";
std::cout.flush();
if (n%10000000 == 0)
{
std::cout << std::endl;
}
}
}
// print
std::ofstream out;
out.open(out_file.c_str());
if (not out.is_open())
{
std::cerr << "Error while opening file " << out_file << " for writing" << std::endl;
}
b = beds.begin();
r = read_count.begin();
while (b != beds.end() and r != read_count.end())
{
for (size_t i = 0; i < b->second.size(); ++i)
{
out << b->second.at(i).ref() << " "
<< b->second.at(i).start() << " "
<< b->second.at(i).end() << " "
<< b->second.at(i).name() << " "
<< b->second.at(i).score() << " "
<< r->second.at(i) << std::endl;
}
++b;
++r;
}
out.close();
double time2 = clock();
std::cout << std::endl;
std::cout << "Wall time: " << (double) (time2 - time1) / CLOCKS_PER_SEC << " s" << std::endl;
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
std::cout << "RSS: " << usage.ru_maxrss << std::endl;
return 0;
}