File: contigs_to_fastg.cpp

package info (click to toggle)
minia 3.2.1%2Bgit20200522.4960a99-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,012 kB
  • sloc: cpp: 1,376; sh: 397; python: 208; makefile: 8
file content (178 lines) | stat: -rw-r--r-- 4,960 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
/*
 *
 * taken from..
 *
 *  MEGAHIT
 *  Copyright (C) 2014 - 2015 The University of Hong Kong & L3 Bioinformatics Limited
 *
 * ..cheers!
 *  it's GPL but we're GPL too so we're good, right? guys.. right?
 *
 *  This program 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.
 *
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* contact: Dinghua Li <dhli@cs.hku.hk> */

#include <string>
#include <unordered_map>
#include <vector>
#include <cstdio>
#include <cassert>
#include <zlib.h>

#include "kseq.h"

using namespace std;

#ifndef KSEQ_INITED
    #define KSEQ_INITED
    KSEQ_INIT(gzFile, gzread)
#endif

char Comp(char c) {
    switch (c) {
    case 'A':
    case 'a':
        return 'T';

    case 'C':
    case 'c':
        return 'G';

    case 'G':
    case 'g':
        return 'C';

    case 'T':
    case 't':
        return 'A';

    default:
        assert(false);
    }
}

string RevComp(const string &s) {
    string ret;

    for (unsigned i = 0; i < s.length(); ++i) {
        ret.push_back(Comp(s[s.length() - 1 - i]));
    }

    return ret;
}

string NodeName(int i, int len, double mul, bool is_rc) {
    static char buf[10240];
    sprintf(buf, "NODE_%d_length_%d_cov_%0.2f_ID_%d", i, len, mul, i * 2 - 1);

    if (is_rc) return string(buf) + "'";
    else return string(buf);
}

int main(int argc, char **argv) {
    if (argc < 3) {
        fprintf(stderr, "Usage: %s <contigs.fasta> <kmer_size>\n", argv[0]);
        exit(1);
    }

    unsigned k = atoi(argv[2]) - 1; // megahit uses a different de bruijn definition as us, so it's k-1 for us
    gzFile fp = gzopen(argv[1], "r");
    if (fp == NULL)
    {
        fprintf(stderr,"cannot open file: %s ", argv[1]);
        exit(1);
    }
    kseq_t *seq = kseq_init(fp); // kseq to read files

    vector<string> ctgs;
    vector<double> muls;
    vector<string> node_names;
    vector<string> rev_node_names;
    unordered_map<string, vector<int> > start_kmer_to_id;
    bool has_muls = true;

    while (kseq_read(seq) >= 0) {
        if (seq->seq.l < k) {
            continue;
        }

        float mul;

        // Minia has a special output, actually similar to the one this tool outputs. Let's parse it in a convenient way
        for (int i = 0; i < seq->name.l; i++)
            if (seq->name.s[i] == '_') seq->name.s[i] = ' ';

        int osef;
        static char osef_s[100];
        int ret = sscanf(seq->name.s, "%s %d %s %d %s %f %s %d", &osef_s, &osef, &osef_s, &osef, &osef_s, &mul, &osef_s, &osef);
        
        if (ret != 8 && has_muls)
        {
            fprintf(stderr, "unexpected format string, disabling recording of coverage. Is this a Minia contig? Header of first seq: '%s %s'\n", seq->name.s, seq->comment.s);
            has_muls = false;

        }

        if (has_muls)
            muls.push_back(mul);

        ctgs.push_back(string(seq->seq.s));
    }

    for (int i = 0; i < (int)ctgs.size(); ++i) {
        start_kmer_to_id[ctgs[i].substr(0, k)].push_back(i + 1);
        start_kmer_to_id[RevComp(ctgs[i].substr(ctgs[i].length() - k))].push_back(-i - 1);
    }

    for (int i = 0; i < (int)ctgs.size(); ++i) {
        float mul = 0;
        if (has_muls)
            mul = muls[i];
        node_names.push_back(NodeName(i + 1, ctgs[i].length(), mul, false));
        rev_node_names.push_back(NodeName(i + 1, ctgs[i].length(), mul, true));
    }

    for (int i = 0; i < (int)ctgs.size(); ++i) {
        for (int dir = 0; dir < 2; ++dir) {
            string header = dir == 0 ? node_names[i] : rev_node_names[i];
            header = ">" + header;
            string s = dir == 0 ? ctgs[i] : RevComp(ctgs[i]);
            auto mit = start_kmer_to_id.find(s.substr(s.length() - k));

            if (mit != start_kmer_to_id.end()) {
                for (unsigned j = 0; j < mit->second.size(); ++j) {
                    if (j == 0) {
                        header += ":";
                    }
                    else {
                        header += ",";
                    }

                    if (mit->second[j] > 0) {
                        header += node_names[mit->second[j] - 1];
                    }
                    else {
                        header += rev_node_names[-mit->second[j] - 1];
                    }
                }
            }

            header += ";";
            printf("%s\n%s\n", header.c_str(), s.c_str());
        }
    }

    return 0;
}