-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdbg_query.cpp
81 lines (72 loc) · 1.85 KB
/
dbg_query.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
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
//-----------------------------------------------
// Copyright 2013 Wellcome Trust Sanger Institute
// Written by Jared Simpson (js18@sanger.ac.uk)
// Released under the GPL
//-----------------------------------------------
//
// DBGQuery - API for querying properties of a
// de Bruijn graph encoded as an FM-index
//
#include <stdio.h>
#include "dbg_query.h"
//
bool DBGQuery::isVertex(const FMIndex* index, const std::string& s)
{
return index->count(s) > 0 || index->count(reverseComplement(s)) > 0;
}
//
bool DBGQuery::isSuffixNeighbor(const FMIndex* index, const std::string& s, char b)
{
// Make the neighbor string
std::string t = s.substr(1) + b;
return isVertex(index, t);
}
//
bool DBGQuery::isPrefixNeighbor(const FMIndex* index, const std::string& s, char b)
{
// Make the neighbor string
std::string t = b + s.substr(0, s.size() - 1);
return isVertex(index, t);
}
//
std::string DBGQuery::getSuffixNeighbors(const FMIndex* index, const std::string& s)
{
std::string out;
for(size_t i = 0; i < 4; ++i)
{
char b = "ACGT"[i];
if(isSuffixNeighbor(index, s, b))
out.append(1, b);
}
return out;
}
//
std::string DBGQuery::getPrefixNeighbors(const FMIndex* index, const std::string& s)
{
std::string out;
for(size_t i = 0; i < 4; ++i)
{
char b = "ACGT"[i];
if(isPrefixNeighbor(index, s, b))
out.append(1, b);
}
return out;
}
//
std::pair<std::string, size_t>
DBGQuery::extractSubstringAndIndex(
const FMIndex* index, size_t idx, size_t len)
{
std::string out;
out.reserve(len);
while(out.length() < len)
{
char b = index->getChar(idx);
if(b == EOF)
break;
out.push_back(b);
idx = index->LF(idx);
}
std::reverse(out.begin(), out.end());
return std::make_pair(out, idx);
}