-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGenomicRegionStore.cc
61 lines (45 loc) · 1.59 KB
/
GenomicRegionStore.cc
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
#include "GenomicRegionStore.h"
using namespace BamstatsAlive;
GenomicRegionStore::GenomicRegionStore(const std::string& regionJson) {
json_t *root;
json_error_t error;
root = json_loads(regionJson.c_str(), 0, &error);
if(!root) throw new InvalidJsonStringException;
if(!json_is_array(root)) {
json_decref(root);
throw new JsonRootNotArrayException;
}
for(int i=0; i<json_array_size(root); i++) {
json_t *arrayItem, *jvChr, *jvStartPos, *jvEndPos;
arrayItem = json_array_get(root, i);
if(!json_is_object(arrayItem)) {
json_decref(root);
throw new ArrayItemsNotObjectException;
}
jvChr = json_object_get(arrayItem, "chr");
jvStartPos = json_object_get(arrayItem, "start");
jvEndPos = json_object_get(arrayItem, "end");
if(!json_is_string(jvChr) || !json_is_integer(jvStartPos) || !json_is_integer(jvEndPos)) {
json_decref(root);
throw new UnexpectedFieldDataTypeException;
}
const char *chr = json_string_value(jvChr);
int32_t startPos = json_integer_value(jvStartPos);
int32_t endPos = json_integer_value(jvEndPos);
struct _regionT newRegion(chr, startPos, endPos);
_regions.push_back(newRegion);
}
json_decref(root);
}
const GenomicRegionStore::GenomicRegionT& GenomicRegionStore::kRegionNotFound() {
static GenomicRegionT notfound("", 0, 0);
return notfound;
}
const GenomicRegionStore::GenomicRegionT& GenomicRegionStore::locateRegion(const char *chrom, int32_t pos) {
GenomicRegionVec::iterator it;
for(it = _regions.begin(); it != _regions.end(); it++) {
if(it->contains(chrom, pos))
return *it;
}
return GenomicRegionStore::kRegionNotFound();
}