File: graphlocation.cpp

package info (click to toggle)
bandage 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,684 kB
  • sloc: cpp: 45,359; sh: 491; makefile: 12
file content (205 lines) | stat: -rw-r--r-- 5,127 bytes parent folder | download
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//Copyright 2017 Ryan Wick

//This file is part of Bandage.

//Bandage is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//Bandage is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Bandage.  If not, see <http://www.gnu.org/licenses/>.


#include "graphlocation.h"

#include "debruijnnode.h"
#include "assemblygraph.h"
#include "../program/globals.h"



GraphLocation::GraphLocation() :
    m_node(0), m_position(0)
{

}

GraphLocation::GraphLocation(DeBruijnNode * node, int position) :
    m_node(node), m_position(position)
{
}


GraphLocation GraphLocation::startOfNode(DeBruijnNode * node)
{
    GraphLocation location(node, 1);
    if (location.isValid())
        return location;
    else
        return GraphLocation::null();
}


GraphLocation GraphLocation::endOfNode(DeBruijnNode * node)
{
    if (node == 0)
        return GraphLocation::null();

    int pos = node->getLength();
    GraphLocation location(node, pos);
    if (location.isValid())
        return location;
    else
        return GraphLocation::null();
}


GraphLocation GraphLocation::null()
{
    return GraphLocation(0, 0);
}

bool GraphLocation::isValid() const
{
    if (isNull())
        return false;
    if (m_position < 1)
        return false;
    return m_position <= m_node->getLength();
}


bool GraphLocation::isNull() const
{
    return (m_node == 0 || m_position == 0);
}


GraphLocation GraphLocation::reverseComplementLocation() const
{
    int newPos = m_node->getLength() - m_position + 1;
    GraphLocation newLocation(m_node->getReverseComplement(), newPos);

    //For Velvet graphs, the reverse complement location is shifted by the k-mer
    //size and may not even be on the same node!
    if (g_assemblyGraph->m_graphFileType == LAST_GRAPH)
        newLocation.moveLocation(-g_assemblyGraph->m_kmer + 1);

    if (newLocation.isValid())
        return newLocation;
    else
        return GraphLocation::null();
}



void GraphLocation::moveLocation(int change)
{
    if (change > 0)
        moveForward(change);
    else if (change < 0)
        moveBackward(-change);
}


void GraphLocation::moveForward(int change)
{
    //See if there are enough bases left in this node to move by the
    //required amount.  If so, we're done!
    int basesLeftInNode = m_node->getLength() - m_position;
    if (change <= basesLeftInNode)
    {
        m_position += change;
        return;
    }

    //If there aren't enough bases left, then we recursively try with the
    //next nodes.
    std::vector<DeBruijnNode *> downstreamNodes = m_node->getDownstreamNodes();
    for (size_t i = 0; i < downstreamNodes.size(); ++i)
    {
        DeBruijnNode * node = downstreamNodes[i];
        GraphLocation nextNodeLocation = GraphLocation::startOfNode(node);
        nextNodeLocation.moveForward(change - basesLeftInNode - 1);

        if (nextNodeLocation.isValid())
        {
            m_node = nextNodeLocation.getNode();
            m_position = nextNodeLocation.getPosition();
            return;
        }
    }

    //If the code got here, then we failed to move and we make this a null
    //position.
    m_node = 0;
    m_position = 0;
    return;
}

void GraphLocation::moveBackward(int change)
{
    //See if there are enough bases left in this node to move by the
    //required amount.  If so, we're done!
    int basesLeftInNode = m_position - 1;
    if (change <= basesLeftInNode)
    {
        m_position -= change;
        return;
    }

    //If there aren't enough bases left, then we recursively try with the
    //next nodes.
    std::vector<DeBruijnNode *> upstreamNodes = m_node->getUpstreamNodes();
    for (size_t i = 0; i < upstreamNodes.size(); ++i)
    {
        DeBruijnNode * node = upstreamNodes[i];
        GraphLocation nextNodeLocation = GraphLocation::endOfNode(node);
        nextNodeLocation.moveBackward(change - basesLeftInNode - 1);

        if (nextNodeLocation.isValid())
        {
            m_node = nextNodeLocation.getNode();
            m_position = nextNodeLocation.getPosition();
            return;
        }
    }

    //If the code got here, then we failed to move and we make this a null
    //position.
    m_node = 0;
    m_position = 0;
    return;
}


char GraphLocation::getBase() const
{
    if (!isValid())
        return '\0';
    else
        return m_node->getBaseAt(m_position - 1);
}



bool GraphLocation::isAtStartOfNode() const
{
    return (isValid() && m_position == 1);
}

bool GraphLocation::isAtEndOfNode() const
{
    return (isValid() && m_position == m_node->getLength());
}

bool GraphLocation::operator==(GraphLocation const &other) const
{
    return (m_node == other.m_node && m_position == other.m_position);
}