-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbed_tools.cpp
More file actions
167 lines (144 loc) · 5.5 KB
/
Copy pathbed_tools.cpp
File metadata and controls
167 lines (144 loc) · 5.5 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
166
167
#include "bed_tools.hpp"
#include <iostream>
bed_entry::bed_entry(std::string chr, long start, long end, std::string id, int score, char strand): bio_entry(chr, start, end, strand, id) {
m_score = score;
}
bed_entry::bed_entry(bio_entry entry, int score): bio_entry(entry) {
m_score = score;
}
bed_entry::bed_entry(): bio_entry() {
m_score = 0;
}
bed_entry::~bed_entry() {
}
std::string bed_entry::getString() const {
return m_chr + "\t" + std::to_string(m_start) + "\t" + std::to_string(m_end) + "\t" + m_id + "\t" + std::to_string(m_score) + "\t" + std::string(1, m_strand);
}
bed_file::bed_file(std::string filename, open_type type) : bio_file(filename, type) {
}
std::unique_ptr <bio_entry> bed_file::readLine() {
bed_entry entry;
if(remainToRead()) {
std::vector <std::string> values = readBioLine('\t');
try {
std::string chr = values.at(0);
long start = std::stol(values.at(1));
long end = std::stol(values.at(2)); // minimally defined bed entry
std::string name;
int score(0);
char strand;
try {
name = values.at(3);
} catch(const std::out_of_range) {
name = ".";
}
try {
score = std::stoi(values.at(4));
} catch(std::logic_error) { // catch both score undefined (= . so no conversion) and no score col
score = 0;
}
try {
strand = values.at(5)[0];
} catch(const std::out_of_range) {
strand = '+';
} // these cols are optionnal
entry = bed_entry(chr, start, end, name, score, strand);
}
catch(const std::out_of_range)
{
std::cout << "not enough elements in line" << std::endl;
throw;
}
}
return std::make_unique <bed_entry>(entry);
}
void bed_file::apply_intersect(bio_file& file, bool stranded, id_status status) {
std::vector <intersect_results> results(intersect(file, stranded, status));
clear();
for(const auto& entry: results) {
appendEntry(std::make_unique <bed_entry> (bed_entry(entry.result, 1000)));
}
}
AOE_entry::AOE_entry(std::string chr, long start, long end, std::string id, int score, char strand, long zero): bed_entry(chr, start, end, id, score, strand) {
m_zero = zero;
}
AOE_entry::AOE_entry(bio_entry entry, int score, long zero): bed_entry(entry, score) {
m_zero = zero;
}
AOE_entry::AOE_entry(): bed_entry() {
m_zero = 0;
}
int AOE_entry::getZero() {
return m_zero;
}
std::string AOE_entry::getString() const {
return m_chr + "\t" + std::to_string(m_start) + "\t" + std::to_string(m_end) + "\t" + m_id + "\t" + std::to_string(m_score) + "\t" + std::string(1, m_strand) + "\t" + std::to_string(m_zero);
}
AOE_file::AOE_file(std::string filename, open_type type): bio_file(filename, type) {
}
std::unique_ptr <bio_entry> AOE_file::readLine() {
AOE_entry entry;
if(remainToRead()) {
std::vector <std::string> values = readBioLine('\t');
try {
char strand = '\0';
if(values.at(5)[0] == 'L') {
strand = '+';
} else if(values.at(5)[0] == 'R') {
strand = '-';
} else if(values.at(5)[0] == '+' || values.at(5)[0] == '-') {
strand = values.at(5)[0];
} else {
throw std::domain_error("strand incorrectly defined : not a correct value : " + std::string(1, values.at(5)[0]));
} // correct strand info for AOE files
int score(0);
try {
score = std::stoi(values.at(4));
} catch(std::invalid_argument) {
score = 0;
}
entry = AOE_entry(values.at(0), std::stol(values.at(1)), std::stol(values.at(2)), values.at(3), score, strand, std::stol(values.at(6)));
}
catch(const std::out_of_range)
{
std::cout << "not enough elements in line" << std::endl;
throw;
}
}
return std::make_unique <AOE_entry>(entry);
}
long AOE_entry::getRelativePos(long pos) const {
long returned(pos - m_zero);
if(m_strand == '-') {
returned = returned * -1;
}
return returned;
}
// std::unique_ptr<bio_entry> AOE_entry::intersect(const bio_entry& entry, bool stranded) {
// bio_entry intersected_entry (*bio_entry::intersect(entry, stranded)); // copy the value pointed
// if(!(intersected_entry == bio_entry())) {
// return std::make_unique <AOE_entry> (AOE_entry(intersected_entry, 1000, m_zero));
// } else {
// return std::make_unique <AOE_entry> (AOE_entry());
// }
// }
void bed_file::appendVector(std::vector <std::shared_ptr <bio_entry>>& entries) {
for(const auto& entry: entries) {
bed_entry b_entry((*entry), 1000);
appendEntry(std::make_unique <bed_entry>(b_entry));
}
}
void AOE_file::apply_intersect(bio_file& file, bool stranded, id_status status) {
std::vector <intersect_results> results(intersect(file, stranded, status));
std::vector <std::unique_ptr <bio_entry>> entries;
for(const auto& entry: results) {
entries.push_back(std::make_unique <AOE_entry> (AOE_entry(entry.result, dynamic_cast <AOE_entry*>(entry.source) -> getScore(), dynamic_cast <AOE_entry*>(entry.source) -> getZero())));
}
clear();
for(auto &entry: entries) {
appendEntry(std::move(entry));
}
}
int bed_entry::getScore() const {
return m_score;
}