-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBySampleStratifier.h
86 lines (66 loc) · 3.18 KB
/
BySampleStratifier.h
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
/* BySampleStratifier.h
*
* Collect stats while stratifing over samples
*
* Because the stratifier will need to be able to create
* new stats collectors of the correct type, this class
* is defined as a template. User should instanciate this class
* using the desired stats collector type
*/
#ifndef BYSAMPLESTRATIFIER_H
#define BYSAMPLESTRATIFIER_H
#pragma once
#include "AbstractStatCollector.h"
namespace VcfStatsAlive {
template <class CollectorT>
class BySampleStratifier : public AbstractStatCollector {
protected:
virtual void processVariantImpl(bcf_hdr_t* hdr, bcf1_t *var) override {
bcf1_t* subset_rec; // temporary bcf record to hold subsetted record
for(int sample_idx = 0; sample_idx < hdr->n[BCF_DT_SAMPLE]; sample_idx++) {
auto* sample = hdr->id[BCF_DT_SAMPLE] + sample_idx;
int imap = sample_idx;
subset_rec = bcf_dup(var);
bcf_subset(hdr, subset_rec, 1, &imap);
m_collectors[sample->key]->processVariant(m_subset_hdrs[sample_names[sample_idx]], subset_rec);
bcf_destroy(subset_rec);
}
}
virtual void appendJsonImpl(json_t * jsonRootObj) override {
for(auto& sample : m_collectors) {
json_t *j_sample = json_object();
sample.second->appendJson(j_sample);
json_object_set_new(jsonRootObj, sample.first.c_str(), j_sample);
}
}
public:
BySampleStratifier(bcf_hdr_t* hdr) : AbstractStatCollector() {
auto sample_iter = hdr->id[BCF_DT_SAMPLE];
auto sample_iter_end = hdr->id[BCF_DT_SAMPLE] + hdr->n[BCF_DT_SAMPLE];
sample_count = hdr->n[BCF_DT_SAMPLE];
sample_names = new char*[hdr->n[BCF_DT_SAMPLE]];
for(int sample_idx = 0; sample_idx < hdr->n[BCF_DT_SAMPLE]; sample_idx++) {
int imap = sample_idx;
sample_names[sample_idx] = strdup(hdr->id[BCF_DT_SAMPLE][sample_idx].key);
std::cerr<<"Sample ["<<sample_names[sample_idx]<<"] seen; creating collector"<<std::endl;
m_collectors[sample_names[sample_idx]] = new CollectorT();
m_subset_hdrs[sample_names[sample_idx]] = bcf_hdr_subset(hdr, 1, hdr->samples + sample_idx, &imap);
}
}
virtual ~BySampleStratifier() {
// release collectors for each sample
for(auto& sample : m_collectors) delete sample.second;
// release bcf_hdr_t objects for each sample
for(auto& sample : m_subset_hdrs) bcf_hdr_destroy(sample.second);
// release sample names and pointer array
for(int i=0; i<sample_count; i++) free(sample_names[i]);
delete [] sample_names;
}
private:
std::map<std::string, CollectorT*> m_collectors;
int sample_count;
char ** sample_names;
std::map<std::string, bcf_hdr_t*> m_subset_hdrs;
};
}
#endif