File: sv_tree.cpp

package info (click to toggle)
vt 0.57721%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 21,680 kB
  • sloc: cpp: 51,772; makefile: 348; sh: 206
file content (342 lines) | stat: -rw-r--r-- 7,009 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* The MIT License
   Copyright (c) 2014 Adrian Tan <atks@umich.edu>
   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:
   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

#include "sv_tree.h"

/**
 * Constructor.
 */
SVNode::SVNode()
{
    this->parent = NULL;
    this->type = {0,0,0};
    this->depth = 0;
    this->count = 0;
    this->mcount = 0;
};

/**
 * Constructor.
 */
SVNode::SVNode(const char* type)
{
    this->parent = NULL;
    this->type = {0,0,0};
    this->depth = 0;
    this->count = 0;
    this->mcount = 0;
    kputs(type, &this->type);
};

/**
 * Constructor.
 */
SVNode::SVNode(const char* type, int32_t depth)
{
    this->parent = NULL;
    this->type = {0,0,0};
    this->depth = depth;
    this->count = 0;
    this->mcount = 0;
    kputs(type, &this->type);
};

/**
 * Destructor.
 */
SVNode::~SVNode()
{
    for (int32_t i=0; i<children.size(); ++i)
    {
        delete children[i];
    }
    children.clear();
    this->parent = NULL;

    if (type.m) free(type.s);
};

/**
 * Set depth.
 */
void SVNode::set_depth(int32_t depth)
{
    this->depth = depth;
};

/**
 * Increment count.
 */
void SVNode::increment_count()
{
    ++count;

    if (parent!=NULL)
    {
        parent->increment_count();
    }
};

/**
 * Increment mcount.
 */
void SVNode::increment_mcount()
{
    ++mcount;

    if (parent!=NULL)
    {
        parent->increment_mcount();
    }
};

/**
 * Clear values.
 */
void SVNode::clear()
{
    if (type.m) free(type.s);
    count = 0;
};

/**
 * Print values.
 */
void SVNode::print()
{
    for (int32_t i=0; i<depth; ++i) std::cerr << "\t";

    std::cerr << sv_type2string(type.s) << " (" << count << ")\n";

    for (int32_t i=0; i<children.size(); ++i)
    {
        children[i]->print();
    }
};

/**
 *  For translating reserved keywords.
 */
std::string SVNode::sv_type2string(char* sv_type)
{
    if (!strcmp(sv_type,"TRA"))
    {
        return "translocation";
    }
    else if (!strcmp(sv_type,"DEL"))
    {
        return "deletion";
    }
    else if (!strcmp(sv_type,"INS"))
    {
        return "insertion";
    }
    else if (!strcmp(sv_type,"INV"))
    {
        return "inversion";
    }
    else if (!strcmp(sv_type,"ME"))
    {
        return "mobile element";
    }
    else if (!strcmp(sv_type,"MT"))
    {
        return "numt";
    }
    else if (!strcmp(sv_type,"DUP"))
    {
        return "duplication";
    }
    else if (!strcmp(sv_type,"TANDEM"))
    {
        return "tandem repeats";
    }
    else if (!strcmp(sv_type,"CNV"))
    {
        return "copy number variation";
    }
    else
    {
        return sv_type;
    }
};

/**
 * Constructor.
 */
SVTree::SVTree()
{
    root = new SVNode("root", 0);
    max_depth = 0;
    mixed_sv_count = 0;
    m = kh_init(xdict);

    this->add("<TRA>");
    this->add("<DEL>");
    this->add("<INS>");
    this->add("<DUP>");
    this->add("<INV>");
    this->add("<CNV>");
    this->add("<DUP:TANDEM>");
    this->add("<DEL:ME>");
    this->add("<INS:ME>");
    this->add("<INS:MT>");
    this->add("<INS:ME:ALU>");
    this->add("<INS:ME:LINE1>");
    this->add("<INS:ME:SVA>");
//    this->add("<RSCLIP>");
//    this->add("<LSCLIP>");
};

/**
 * Destructor.
 */
SVTree::~SVTree()
{
    if (root)
    {
        delete root;
    }
    root = NULL;

   // kh_destroy(xdict, m);
};

/**
 * Adds a new tag, returns true if successful.
 */
bool SVTree::add(const char* sv_type)
{
    //update hash
    khiter_t k;
    int32_t ret = 0;

    if ((k=kh_get(xdict, m, sv_type))==kh_end(m))
    {
        std::vector<std::string> vec;
        split(vec, "<:>", sv_type);

        SVNode* cnode = root;
        for (size_t i=0; i<vec.size(); ++i)
        {
            bool found_type = false;
            for (size_t j=0; j<cnode->children.size(); ++j)
            {
                if (!strcmp(cnode->children[j]->type.s, vec[i].c_str()))
                {
                    cnode = cnode->children[j];
                    found_type = true;
                    break;
                }
            }

            if (!found_type)
            {
                max_depth = i+1>max_depth?i+1:max_depth;
                SVNode* newnode = new SVNode(vec[i].c_str(), i+1);
                char* new_sv_type = strdup(sv_type);
                k = kh_put(xdict, m, new_sv_type, &ret);
                if (!ret)
                {
                    //already present
                    free(new_sv_type);
                }
                kh_value(m, k) = newnode;
                cnode->children.push_back(newnode);
                newnode->parent = cnode;
                cnode = newnode;
            }
        }

        return true;
    }

    return false;
}

/**
 * Observes and update the count of a new tag.
 */
void SVTree::count(Variant& variant)
{
    khiter_t k;
    int32_t ret = 0;
    bool mixed_sv = false;

    for (size_t i=0; i<variant.alleles.size(); ++i)
    {
        const char* sv_type = variant.alleles[i].sv_type.c_str();
        if ((k=kh_get(xdict, m, sv_type))==kh_end(m))
        {
            this->add(sv_type);
            k=kh_get(xdict, m, sv_type);
        }

        if (variant.alleles[i].sv_type!=variant.alleles[0].sv_type)
        {
            mixed_sv = true;
        }
    }

    if (mixed_sv)
    {
        ++mixed_sv_count;
    }
    else
    {
        if (variant.alleles.size()==1)
        {
            kh_value(m, k)->increment_count();
        }
        else
        {
            kh_value(m, k)->increment_mcount();
        }
    }
};

/**
 * Enumerates the children in a depth first order.
 */
std::vector<SVNode*> SVTree::enumerate_dfs()
{
    std::vector<SVNode*> s;
    enumerate_dfs(s, root);
    return s;
};

/**
 * Helper function for enumerating the children in a depth first order.
 */
void SVTree::enumerate_dfs(std::vector<SVNode*>& s, SVNode* node)
{
    s.push_back(node);
    for (size_t i=0; i<node->children.size(); ++i)
    {
        enumerate_dfs(s, node->children[i]);
    }
};

/**
 * Print this tree.
 */
void SVTree::print()
{
    root->print();
};