forked from tfwillems/HipSTR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snp_bam_processor.cpp
179 lines (161 loc) · 8.26 KB
/
snp_bam_processor.cpp
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
#include <assert.h>
#include <time.h>
#include "snp_bam_processor.h"
#include "snp_phasing_quality.h"
#include "snp_tree.h"
void SNPBamProcessor::process_reads(std::vector< std::vector<BamTools::BamAlignment> >& paired_strs_by_rg,
std::vector< std::vector<BamTools::BamAlignment> >& mate_pairs_by_rg,
std::vector< std::vector<BamTools::BamAlignment> >& unpaired_strs_by_rg,
std::vector<std::string>& rg_names, Region& region,
std::string& ref_allele, std::string& chrom_seq, std::ostream& out){
// Only use specialized function for 10X genomics BAMs if flag has been set
if(bams_from_10x_){
process_10x_reads(paired_strs_by_rg, mate_pairs_by_rg, unpaired_strs_by_rg, rg_names, region, ref_allele, chrom_seq, out);
return;
}
locus_snp_phase_info_time_ = clock();
assert(paired_strs_by_rg.size() == mate_pairs_by_rg.size() && paired_strs_by_rg.size() == unpaired_strs_by_rg.size());
if (paired_strs_by_rg.size() == 0 && unpaired_strs_by_rg.size() == 0)
return;
std::vector< std::vector<BamTools::BamAlignment> > alignments(paired_strs_by_rg.size());
std::vector< std::vector<double> > log_p1s, log_p2s;
bool got_snp_info = false;
if (have_snp_vcf_){
std::vector<SNPTree*> snp_trees;
std::map<std::string, unsigned int> sample_indices;
if(create_snp_trees(region.chrom(), (region.start() > MAX_MATE_DIST ? region.start()-MAX_MATE_DIST : 1),
region.stop()+MAX_MATE_DIST, phased_snp_vcf_, sample_indices, snp_trees, logger())){
got_snp_info = true;
std::set<std::string> bad_samples, good_samples;
for (unsigned int i = 0; i < paired_strs_by_rg.size(); ++i){
if (sample_indices.find(rg_names[i]) != sample_indices.end()){
good_samples.insert(rg_names[i]);
std::vector<double> log_p1, log_p2;
SNPTree* snp_tree = snp_trees[sample_indices[rg_names[i]]];
calc_het_snp_factors(paired_strs_by_rg[i], mate_pairs_by_rg[i], base_quality_, snp_tree, log_p1, log_p2, match_count_, mismatch_count_);
calc_het_snp_factors(unpaired_strs_by_rg[i], base_quality_, snp_tree, log_p1, log_p2, match_count_, mismatch_count_);
log_p1s.push_back(log_p1); log_p2s.push_back(log_p2);
}
else {
std::vector<double> log_p1, log_p2;
for (unsigned int j = 0; j < paired_strs_by_rg[i].size()+unpaired_strs_by_rg[i].size(); ++j){
log_p1.push_back(0); log_p2.push_back(0); // Assign equal phasing LLs as no SNP info is available
}
log_p1s.push_back(log_p1); log_p2s.push_back(log_p2);
bad_samples.insert(rg_names[i]);
}
// Copy alignments
alignments[i].insert(alignments[i].end(), paired_strs_by_rg[i].begin(), paired_strs_by_rg[i].end());
alignments[i].insert(alignments[i].end(), unpaired_strs_by_rg[i].begin(), unpaired_strs_by_rg[i].end());
}
logger() << "Found VCF info for " << good_samples.size() << " out of " << good_samples.size()+bad_samples.size() << " samples with STR reads" << std::endl;
}
else
logger() << "Warning: Failed to construct SNP trees for " << region.chrom() << ":" << region.start() << "-" << region.stop() << std::endl;
destroy_snp_trees(snp_trees);
}
if (!got_snp_info){
for (unsigned int i = 0; i < paired_strs_by_rg.size(); i++){
// Copy alignments
alignments[i].insert(alignments[i].end(), paired_strs_by_rg[i].begin(), paired_strs_by_rg[i].end());
alignments[i].insert(alignments[i].end(), unpaired_strs_by_rg[i].begin(), unpaired_strs_by_rg[i].end());
// Assign equal phasing LLs as no SNP info is available
log_p1s.push_back(std::vector<double>(paired_strs_by_rg[i].size()+unpaired_strs_by_rg[i].size(), 0.0));
log_p2s.push_back(std::vector<double>(paired_strs_by_rg[i].size()+unpaired_strs_by_rg[i].size(), 0.0));
}
}
int phased_samples = 0, phased_reads = 0, total_reads = 0;
for (unsigned int i = 0; i < alignments.size(); i++){
bool sample_phased = false;
for (unsigned int j = 0; j < alignments[i].size(); j++){
sample_phased |= (log_p1s[i][j] != log_p2s[i][j]);
phased_reads += (log_p1s[i][j] != log_p2s[i][j]);
}
total_reads += alignments[i].size();
phased_samples += sample_phased;
}
logger() << "Phased SNPs add info for " << phased_reads << " out of " << total_reads << " reads"
<< " and " << phased_samples << " out of " << rg_names.size() << " samples" << std::endl;
locus_snp_phase_info_time_ = (clock() - locus_snp_phase_info_time_)/CLOCKS_PER_SEC;
total_snp_phase_info_time_ += locus_snp_phase_info_time_;
// Run any additional analyses using phasing probabilities
analyze_reads_and_phasing(alignments, log_p1s, log_p2s, rg_names, region, ref_allele, chrom_seq, 0);
}
int SNPBamProcessor::get_haplotype(BamTools::BamAlignment& aln){
if (!aln.HasTag(HAPLOTYPE_TAG))
return -1;
uint8_t haplotype;
if (!aln.GetTag(HAPLOTYPE_TAG, haplotype)){
char type;
aln.GetTagType(HAPLOTYPE_TAG, type);
printErrorAndDie("Failed to extract haplotype tag");
}
assert(haplotype == 1 || haplotype == 2);
return (int)haplotype;
}
/*
** Exploratory function for analyzing data from 10X Genomics BAMs
** These BAMs contain haplotype tags, which can be used in place of the physical-phasing + VCF approach
** used in the standard process_reads function
*/
void SNPBamProcessor::process_10x_reads(std::vector< std::vector<BamTools::BamAlignment> >& paired_strs_by_rg,
std::vector< std::vector<BamTools::BamAlignment> >& mate_pairs_by_rg,
std::vector< std::vector<BamTools::BamAlignment> >& unpaired_strs_by_rg,
std::vector<std::string>& rg_names, Region& region,
std::string& ref_allele, std::string& chrom_seq, std::ostream& out){
locus_snp_phase_info_time_ = clock();
assert(paired_strs_by_rg.size() == mate_pairs_by_rg.size() && paired_strs_by_rg.size() == unpaired_strs_by_rg.size());
if (paired_strs_by_rg.size() == 0 && unpaired_strs_by_rg.size() == 0)
return;
std::vector< std::vector<BamTools::BamAlignment> > alignments(paired_strs_by_rg.size());
std::vector< std::vector<double> > log_p1s, log_p2s;
int32_t phased_reads = 0, total_reads = 0;
for (unsigned int i = 0; i < paired_strs_by_rg.size(); i++){
// Copy alignments
alignments[i].insert(alignments[i].end(), paired_strs_by_rg[i].begin(), paired_strs_by_rg[i].end());
alignments[i].insert(alignments[i].end(), unpaired_strs_by_rg[i].begin(), unpaired_strs_by_rg[i].end());
log_p1s.push_back(std::vector<double>());
log_p2s.push_back(std::vector<double>());
for (unsigned int j = 0; j < paired_strs_by_rg[i].size(); j++){
total_reads++;
int haplotype_1 = get_haplotype(paired_strs_by_rg[i][j]);
int haplotype_2 = get_haplotype(mate_pairs_by_rg[i][j]);
// If the two mate pairs don't have the same haplotype index, it's possible that
// i) One of them is unmapped (and therefore has a -1)
// ii) They map to two different phase sets. This is essentially a phasing breakpoint and we
// probably want to avoid using phase information for these reads
int haplotype;
if (haplotype_1 != haplotype_2)
haplotype = -1;
else
haplotype = haplotype_1;
if (haplotype != -1){
phased_reads++;
log_p1s[i].push_back(haplotype == 1 ? FROM_HAP_LL : OTHER_HAP_LL);
log_p2s[i].push_back(haplotype == 2 ? FROM_HAP_LL : OTHER_HAP_LL);
}
else {
log_p1s[i].push_back(0.0);
log_p2s[i].push_back(0.0);
}
}
for (unsigned int j = 0; j < unpaired_strs_by_rg[i].size(); j++){
total_reads++;
int haplotype = get_haplotype(unpaired_strs_by_rg[i][j]);
if (haplotype != -1){
phased_reads++;
log_p1s[i].push_back(haplotype == 1 ? FROM_HAP_LL : OTHER_HAP_LL);
log_p2s[i].push_back(haplotype == 2 ? FROM_HAP_LL : OTHER_HAP_LL);
}
else {
log_p1s[i].push_back(0.0);
log_p2s[i].push_back(0.0);
}
}
}
logger() << "Phased SNPs add info for " << phased_reads << " out of " << total_reads << " reads" << std::endl;
locus_snp_phase_info_time_ = (clock() - locus_snp_phase_info_time_)/CLOCKS_PER_SEC;
total_snp_phase_info_time_ += locus_snp_phase_info_time_;
// Run any additional analyses using phasing probabilities
analyze_reads_and_phasing(alignments, log_p1s, log_p2s, rg_names, region, ref_allele, chrom_seq, 0);
}