File: io_support.hpp

package info (click to toggle)
spades 3.13.1%2Bdfsg-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 (178 lines) | stat: -rw-r--r-- 5,518 bytes parent folder | download | duplicates (2)
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
//***************************************************************************
//* Copyright (c) 2018 Saint Petersburg State University
//* All Rights Reserved
//* See file LICENSE for details.
//***************************************************************************

#pragma once

#include "assembly_graph/paths/bidirectional_path.hpp"
#include "assembly_graph/graph_support/contig_output.hpp"
#include "assembly_graph/components/connected_component.hpp"

namespace path_extend {
using namespace debruijn_graph;

struct ScaffoldInfo {
    std::string sequence;
    BidirectionalPath* path;
    std::string name;

    ScaffoldInfo(const std::string& sequence, BidirectionalPath* path) :
        sequence(sequence), path(path) { }

    size_t length() const {
        return sequence.length();
    }

    double coverage() const {
        return path->Coverage();
    }
};

typedef vector<ScaffoldInfo> ScaffoldStorage;

class ScaffoldSequenceMaker {
    const Graph &g_;
    const size_t k_;
public:
    ScaffoldSequenceMaker(const Graph& g) : g_(g), k_(g_.k()) {
    }

    string MakeSequence(const BidirectionalPath &scaffold) const;
};

//Finds common long edges in paths and joins them into
//Based on disjoint set union
class TranscriptToGeneJoiner {
private:
    const Graph &g_;
    size_t min_edge_len_; //minimal length for joining transcripts into a gene

    unordered_map<BidirectionalPath *, size_t> path_id_; //path ids
    std::vector<size_t> parents_; //node parents in
    std::vector<size_t> ranks_; //tree depth


    void MakeSet(size_t x);

    void JoinTrees(size_t x, size_t y);

    void Init(const PathContainer &paths);

    DECL_LOGGER("TranscriptToGeneJoiner");
public:
    TranscriptToGeneJoiner(const Graph &g, size_t min_edge_len): g_(g), min_edge_len_(min_edge_len) {}

    size_t FindTree(size_t x);

    size_t GetPathId(BidirectionalPath *path);

    void Construct(const PathContainer &paths);
};

class ContigNameGenerator {
public:
    virtual void Preprocess(const PathContainer& paths) = 0;

    virtual std::string MakeContigName(size_t index, const ScaffoldInfo &scaffold_info) = 0;

    virtual ~ContigNameGenerator() {
    }
};

class DefaultContigNameGenerator: public ContigNameGenerator {
public:
    void Preprocess(const PathContainer&) override {}

    std::string MakeContigName(size_t index, const ScaffoldInfo &scaffold_info) override {
        return io::MakeContigId(index, scaffold_info.length(), scaffold_info.coverage());
    }
};

class PlasmidContigNameGenerator: public ContigNameGenerator {
    const ConnectedComponentCounter &c_counter_;

public:
    PlasmidContigNameGenerator(const ConnectedComponentCounter &c_counter): c_counter_(c_counter) {}

    void Preprocess(const PathContainer&) override {}

    std::string MakeContigName(size_t index, const ScaffoldInfo &scaffold_info) override {
        return io::AddComponentId(io::MakeContigId(index, scaffold_info.length(), scaffold_info.coverage()),
                                  c_counter_.GetComponent(scaffold_info.path->Front()));
    }
};

class TranscriptNameGenerator: public ContigNameGenerator {
    TranscriptToGeneJoiner transcript_joiner_;

    std::unordered_map<size_t, size_t> isoform_num_;
    std::unordered_map<size_t, size_t> gene_ids_;
    size_t gene_num_;

public:
    TranscriptNameGenerator(const Graph &g, size_t min_edge_len = 300):
        transcript_joiner_(g, min_edge_len),
        isoform_num_(),
        gene_ids_(),
        gene_num_(0) {}

    void Clear() {
        isoform_num_.clear();
        gene_ids_.clear();
        gene_num_ = 0;
    }

    void Preprocess(const PathContainer& paths) override {
        Clear();
        transcript_joiner_.Construct(paths);
    }

    std::string MakeContigName(size_t index, const ScaffoldInfo &scaffold_info) override {
        size_t id = transcript_joiner_.GetPathId(scaffold_info.path);
        size_t parent_id = transcript_joiner_.FindTree(id);
        DEBUG("Path " << id << " Parent " << parent_id);
        if (gene_ids_.find(parent_id) == gene_ids_.end()) {
            gene_ids_[parent_id] = gene_num_;
            isoform_num_[parent_id] = 0;
            gene_num_++;
        }
        string contig_id = io::MakeRNAContigId(index,
                                               scaffold_info.length(),
                                               scaffold_info.coverage(),
                                               gene_ids_[parent_id],
                                               isoform_num_[parent_id]);
        isoform_num_[parent_id]++;
        return contig_id;
    }
};


inline std::shared_ptr<ContigNameGenerator> MakeContigNameGenerator(config::pipeline_type mode,
                                                                    const conj_graph_pack &gp) {
    std::shared_ptr<path_extend::ContigNameGenerator> name_generator;
    if (mode == config::pipeline_type::plasmid)
        name_generator = make_shared<PlasmidContigNameGenerator>(gp.components);
    else if (mode == config::pipeline_type::rna)
        name_generator = make_shared<TranscriptNameGenerator>(gp.g);
    else
        name_generator = make_shared<DefaultContigNameGenerator>();
    return name_generator;
}

class ScaffoldBreaker {
private:

    int min_overlap_;

    void SplitPath(const BidirectionalPath& path, PathContainer &result) const;

public:

    ScaffoldBreaker(int min_overlap): min_overlap_(min_overlap) {}

    void Break(const PathContainer &paths, PathContainer &result) const;
};

}