File: statistics.hpp

package info (click to toggle)
spades 3.13.1+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 22,172 kB
  • sloc: cpp: 136,213; ansic: 48,218; python: 16,809; perl: 4,252; sh: 2,115; java: 890; makefile: 507; pascal: 348; xml: 303
file content (276 lines) | stat: -rw-r--r-- 7,782 bytes parent folder | download | duplicates (4)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//***************************************************************************
//* Copyright (c) 2015 Saint Petersburg State University
//* Copyright (c) 2011-2014 Saint Petersburg Academic University
//* All Rights Reserved
//* See file LICENSE for details.
//***************************************************************************

#pragma once

#include "utils/stl_utils.hpp"
#include "math/xmath.h"
#include "pipeline/config_struct.hpp"
#include "assembly_graph/paths/mapping_path.hpp"

#include <iostream>
#include <fstream>
#include <map>

namespace debruijn_graph {
namespace stats {

using namespace math;
using namespace omnigraph;

class AbstractStatCounter {
public:
    AbstractStatCounter() {
    }

    virtual ~AbstractStatCounter() {
    }

    virtual void Count() = 0;
    //protected:
    //  DECL_LOGGER("StatCounter")
};

class StatList : AbstractStatCounter {
private:
    vector<AbstractStatCounter *> to_count_;
public:
    StatList(vector<AbstractStatCounter *> to_count =
    vector<AbstractStatCounter *>()) :
            to_count_(to_count) {
    }

    virtual ~StatList() {
    }

    void AddStat(AbstractStatCounter *new_stat) {
        to_count_.push_back(new_stat);
    }

    const vector<AbstractStatCounter *> stats() {
        return to_count_;
    }

    virtual void Count() {
        for (size_t i = 0; i < to_count_.size(); i++) {
            to_count_[i]->Count();
        }
    }

    void DeleteStats() {
        for (size_t i = 0; i < to_count_.size(); i++)
            delete to_count_[i];
        to_count_.clear();
    }
};

template<class Graph>
class VertexEdgeStat : public AbstractStatCounter {
private:
    const Graph &graph_;
public:
    VertexEdgeStat(const Graph &graph) :
            graph_(graph) {
    }

    virtual ~VertexEdgeStat() {
    }

    size_t vertices() {
        return graph_.size();
    }

    size_t edges() {
        size_t edgeNumber = 0;
        size_t sum_edge_length = 0;
        for (auto iterator = graph_.ConstEdgeBegin(); !iterator.IsEnd();
             ++iterator) {
            edgeNumber++;
            //      if (graph_.coverage(*iterator) > 30) {
            sum_edge_length += graph_.length(*iterator);
            //      }
        }
        return edgeNumber;
    }

    size_t edge_length() {
        size_t sum_edge_length = 0;
        for (auto iterator = graph_.ConstEdgeBegin(); !iterator.IsEnd();
             ++iterator) {
            if (graph_.coverage(*iterator) > 30) {
                sum_edge_length += graph_.length(*iterator);
            }
        }
        return sum_edge_length;
    }

    virtual void Count() {
        INFO(
                "Vertex count=" << vertices() << "; Edge count=" << edges());
        INFO(
                "sum length of edges " << edge_length());
    }
};

template<class Graph>
class BlackEdgesStat : public AbstractStatCounter {
private:
    typedef typename Graph::EdgeId EdgeId;
    const Graph &graph_;
    Path<EdgeId> path1_;
    Path<EdgeId> path2_;
public:
    BlackEdgesStat(const Graph &graph, Path<EdgeId> path1, Path<EdgeId> path2) :
            graph_(graph), path1_(path1), path2_(path2) {
    }

    virtual ~BlackEdgesStat() {
    }

    virtual void Count() {
        size_t black_count = 0;
        size_t edge_count = 0;
        const vector <EdgeId> path_edges1 = path1_.sequence();
        const vector <EdgeId> path_edges2 = path2_.sequence();
        set <EdgeId> colored_edges;
        colored_edges.insert(path_edges1.begin(), path_edges1.end());
        colored_edges.insert(path_edges2.begin(), path_edges2.end());
        size_t sum_length = 0;
        for (auto it = graph_.ConstEdgeBegin(); !it.IsEnd(); ++it) {
            edge_count++;
            if (colored_edges.count(*it) == 0) {
                black_count++;
                sum_length += graph_.length(*it);
            }
        }
        if (edge_count > 0) {
            size_t total_genome_size = 0;
            for (const auto &chr: cfg::get().ds.reference_genome)
                total_genome_size += 2*chr.size();
            INFO("Error edges count: " << black_count << " which is " <<
                 100.0 * (double) black_count / (double) edge_count << "% of all edges");
            INFO("Total length of all black edges: " << sum_length << ". While double genome length is " <<
                 total_genome_size);
        } else {
            INFO("Error edges count: " << black_count << " which is 0% of all edges");
        }
    }
};

template<class Graph>
class NStat : public AbstractStatCounter {
private:
    typedef typename Graph::EdgeId EdgeId;
    const Graph &graph_;
    Path<EdgeId> path_;
    size_t perc_;
public:
    NStat(const Graph &graph, Path<EdgeId> path, size_t perc = 50) :
            graph_(graph), path_(path), perc_(perc) {
    }

    virtual ~NStat() {
    }

    virtual void Count() {
        vector <size_t> lengths;
        size_t sum_all = 0;
        for (size_t i = 0; i < path_.size(); i++) {
            lengths.push_back(graph_.length(path_[i]));
            sum_all += graph_.length(path_[i]);
        }
        sort(lengths.begin(), lengths.end());
        size_t sum = 0;
        size_t current = lengths.size();
        while (current > 0 && (double) sum < (double) perc_ * 0.01 * (double) sum_all) {
            current--;
            sum += lengths[current];
        }
        if (current < lengths.size())
            INFO("N" << perc_ << ": " << lengths[current]);
    }
};

template<class Graph>
class IsolatedEdgesStat : public AbstractStatCounter {
private:
    typedef typename Graph::EdgeId EdgeId;
    const Graph &graph_;
    set <EdgeId> black_edges_;
    vector <size_t> lengths;
public:
    IsolatedEdgesStat(const Graph &graph, Path<EdgeId> path1,
                      Path<EdgeId> path2) :
            graph_(graph) {
        for (auto it = graph.ConstEdgeBegin(); !it.IsEnd(); ++it) {
            black_edges_.insert(*it);
        }
        for (size_t i = 0; i < path1.size(); i++) {
            black_edges_.erase(path1[i]);
        }
        for (size_t i = 0; i < path2.size(); i++) {
            black_edges_.erase(path2[i]);
        }
    }

    virtual ~IsolatedEdgesStat() {
    }

    virtual void Count() {
        lengths.clear();
        for (auto it = graph_.ConstEdgeBegin(); !it.IsEnd(); ++it) {
            EdgeId edge = *it;
            if (graph_.IsDeadEnd(graph_.EdgeEnd(edge))
                && graph_.IsDeadStart(graph_.EdgeStart(edge))
                && black_edges_.count(edge) == 0) {
                lengths.push_back(graph_.length(edge));
            }
        }
        INFO("Isolated not black edges: " << lengths.size());
        WriteLengths(cfg::get().output_dir, "isolated_edges.txt");
    }

    void WriteLengths(string folder_name, string file_name) {
        ofstream os;
        os.open((folder_name + "/" + file_name).c_str());
        WriteLengths(os);
        os.close();
    }

    void WriteLengths(ostream &os) {
        sort(lengths.begin(), lengths.end());
        for (size_t i = 0; i < lengths.size(); i++) {
            os << lengths[i] << endl;
        }
    }
};

template<class Graph>
class SelfComplementStat : public AbstractStatCounter {
private:
    typedef typename Graph::EdgeId EdgeId;
    const Graph &graph_;
public:
    SelfComplementStat(const Graph &graph) :
            graph_(graph) {
    }

    virtual ~SelfComplementStat() {
    }

    virtual void Count() {
        size_t sc_number = 0;
        for (auto iterator = graph_.ConstEdgeBegin(); !iterator.IsEnd();
             ++iterator)
            if (graph_.conjugate(*iterator) == (*iterator))
                sc_number++;
        //    INFO("Self-complement count failed!!! ");
        INFO("Self-complement count=" << sc_number);
    }
};
}
}