Debian Codegraph @

src/parse_gff.cpp

raw symbols SCIP
1 #include "parse_gff.h"
2
3 gff3_feature::gff3_feature(std::string line) {
4 int ctr = 0;
5 std::stringstream line_stream;
6 std::string cell;
7 line_stream << line;
8 while (std::getline(line_stream, cell, '\t')) {
9 switch (ctr) {
10 case 0: // Name
11 this->seqid = cell;
12 break;
13 case 1:
14 this->source = cell;
15 break;
16 case 2:
17 this->type = cell;
18 break;
19 case 3:
20 this->start = atoi(cell.c_str());
21 break;
22 case 4:
23 this->end = atoi(cell.c_str());
24 break;
25 case 5:
26 this->score = atof(cell.c_str());
27 break;
28 case 6:
29 this->strand = cell[0];
30 break;
31 case 7:
32 this->phase = atoi(cell.c_str());
33 break;
34 case 8:
35 this->set_attributes(cell);
36 break;
37 }
38 ctr++;
39 }
40 if (ctr < 9) std::cerr << "GFF file is not in GFF3 file format!" << std::endl;
41 line_stream.clear();
42 }
43
44 int gff3_feature::print() {
45 std::cerr << seqid << "\t" << source << "\t" << type << "\t" << start << "\t"
46 << end << "\t" << score << "\t" << strand << "\t" << phase << "\t";
47 std::map<std::string, std::string>::iterator it;
48 for (it = attributes.begin(); it != attributes.end(); it++) {
49 std::cerr << it->first << ": " << it->second << "; ";
50 }
51 std::cerr << std::endl;
52 return 0;
53 }
54
55 std::string gff3_feature::get_attribute(std::string key) {
56 std::string val;
57 if (attributes.find(key) != attributes.end()) {
58 val = attributes[key];
59 }
60 return val;
61 }
62
63 int gff3_feature::set_attributes(std::string attr) {
64 std::string key, val;
65 std::regex exp("[^;]+");
66 std::regex_iterator<std::string::iterator> it(attr.begin(), attr.end(), exp);
67 std::regex_iterator<std::string::iterator> rend;
68 std::string delimiter = "=";
69 while (it != rend) {
70 key = it->str().substr(0, it->str().find(delimiter));
71 val = it->str().substr(it->str().find(delimiter) + 1, it->str().length());
72 if (!key.empty() && !val.empty()) {
73 this->attributes[key] = val;
74 }
75 ++it;
76 }
77 return 0;
78 }
79
80 uint64_t gff3_feature::get_start() { return start; }
81
82 uint64_t gff3_feature::get_end() { return end; }
83
84 int gff3_feature::get_phase() { return phase; }
85
86 std::string gff3_feature::get_type() { return type; }
87
88 char gff3_feature::get_strand() { return strand; }
89
90 int64_t gff3_feature::get_edit_position() {
91 int64_t edit_pos = -1;
92 std::map<std::string, std::string>::iterator it;
93 for (it = attributes.begin(); it != attributes.end(); it++) {
94 if (it->first.compare(EDIT_POSITION) == 0) {
95 edit_pos = stoi(it->second);
96 break;
97 }
98 }
99 return edit_pos;
100 }
101
102 std::string gff3_feature::get_edit_sequence() {
103 std::string edit_seq = "";
104 std::map<std::string, std::string>::iterator it;
105 for (it = attributes.begin(); it != attributes.end(); it++) {
106 if (it->first.compare(EDIT_SEQUENCE) == 0) {
107 edit_seq = it->second;
108 break;
109 }
110 }
111 return edit_seq;
112 }
113
114 int gff3::print() {
115 std::vector<gff3_feature>::iterator it;
116 for (it = features.begin(); it != features.end(); it++) {
117 it->print();
118 }
119 return 0;
120 }
121
122 std::vector<gff3_feature> gff3::get_features() { return features; }
123
124 gff3::gff3() { this->is_empty = true; }
125
126 gff3::gff3(std::string path) {
127 this->is_empty = true;
128 this->read_file(path);
129 }
130
131 int gff3::read_file(std::string path) {
132 std::ifstream fin = std::ifstream(path);
133 if (!fin) {
134 std::cerr << "GFF file does not exist at " << path << std::endl;
135 return -1;
136 }
137 std::string line;
138 while (std::getline(fin, line)) {
139 if (line[0] == '#') // Avoid comments in GFF file
140 continue;
141 if(!line.empty())
142 features.push_back(gff3_feature(line));
143 }
144 if(!features.empty()){
145 this->is_empty = false;
146 } else {
147 std::cerr << "GFF file is empty!" << std::endl;
148 }
149
150 return 0;
151 }
152
153 std::vector<gff3_feature> gff3::query_features(uint64_t pos, std::string type) {
154 std::vector<gff3_feature>::iterator it;
155 std::vector<gff3_feature> res;
156 for (it = features.begin(); it != features.end(); it++) {
157 if (it->get_type() != type) continue;
158 if (pos >= it->get_start() && pos <= it->get_end()) {
159 res.push_back(*it);
160 }
161 }
162 return res;
163 }
164
165 int gff3::get_count() { return features.size(); }
166
167 bool gff3::empty() { return is_empty; }