-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbed_utils.cpp
56 lines (51 loc) · 1.8 KB
/
bed_utils.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
#include "bed_utils.hpp"
bool track_comparator(Track a, Track b) {
if (a.chrom[3] == b.chrom[3]) {
return a.begin < b.end ;
} else {
return a.chrom[3] < b.chrom[3] ;
}
}
string Track::get_name() const {
return svtype + "@" + chrom + "_" + std::to_string(begin) + "_" + std::to_string(end) ;
}
std::vector<Track> load_tracks_from_file(string path) {
lprint({"Parsing BED file:", path, ".."});
std::ifstream bed_file(path) ;
std::string line ;
int i = 0 ;
std::vector<Track> tracks ;
unordered_map<string, int> header ;
while (std::getline(bed_file, line)) {
istringstream iss(line) ;
if (i == 0) {
if (line[0] != '#') {
lprint({"BED header not present (first line doesn't begin with #). Aborting.."}, 2);
}
vector<string> tokens{istream_iterator<string>{iss}, istream_iterator<string>{}} ;
for (int i = 0; i < tokens.size(); i++) {
header[tokens[i]] = i ;
}
i += 1 ;
} else {
vector<string> tokens{istream_iterator<string>{iss}, istream_iterator<string>{}} ;
Track track ;
track.chrom = tokens[0] ;
track.begin = std::stoi(tokens[1]) ;
track.end = std::stoi(tokens[2]) ;
track.svtype = tokens[3] ;
track.svlen = std::stoi(tokens[4]) ;
tracks.push_back(track) ;
}
}
lprint({"Loaded", to_string(tracks.size()), "tracks."});
return tracks ;
}
std::unordered_map<Track, int> load_tracks_from_file_as_dict(string path) {
auto _tracks = load_tracks_from_file(path) ;
std::unordered_map<Track, int> tracks ;
for (auto track = _tracks.begin(); track != _tracks.end(); track++) {
tracks[*track] = 1 ;
}
return tracks ;
}