-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNVIdentifier.h
274 lines (218 loc) · 10.3 KB
/
SNVIdentifier.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
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
/*
SNVIdentifier.h
Author: Izaak Coleman
*/
#ifndef SNVIDENTIFIER_H
#define SNVIDENTIFIER_H
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <list>
#include <utility>
#include <memory>
#include "util_funcs.h"
#include "gsa.h"
// read_tag is used for elements of second GSA
// to determine which cancer specific reads
// derive from the same genomic locations.
struct read_tag{
int64_t read_id;
mutable int16_t offset;
mutable bool orientation;
unsigned char tissue_type;
read_tag (){};
read_tag(int64_t r, int16_t o, bool ori, char t):read_id(r), offset(o),
orientation(ori), tissue_type(t){};
};
struct read_tag_compare{
// Functor for read_tag comparison logic.
bool operator() (const read_tag &a, const read_tag &b) const {
// Within the set read_tags are:
// -- First sorted by tissue type TUMOUR = SWITCHED < HEALTHY
// -- Then sorted by read_id
return a.read_id < b.read_id;
}
};
using bpBlock = std::set<read_tag, read_tag_compare>;
struct vBlockComp{
bool operator() (std::shared_ptr<bpBlock const> const a, std::shared_ptr<bpBlock const> const b) {
if (a->size() < b->size()) return true;
if (a->size() > b->size()) return false;
else {
for(bpBlock::const_iterator a_it = a->cbegin(), b_it = b->cbegin();
a_it != a->cend(); ++a_it, ++b_it) {
if (a_it->read_id == b_it->read_id) continue;
return a_it->read_id < b_it->read_id;
}
}
return false; // same reads in blocks
}
};
class SNVIdentifier {
private:
GSA * gsa;
const char MIN_PHRED_QUAL;
// Read characters with a phred score < MIN_PHRED_QUAL will not contribute to
// the consensus sequence.
const int PRI_MSS;
// Blocks of suffixes covering the same genomic location in the coloured GSA,
// that are mostly cancer reads with a cancer read coverage of < PRI_MSS
// are not extracted.
int AUX_MSS;
// Blocks of suffixes covering the same genomic location in the second GSA
// that have a read coverage of < AUX_MSS are not extracted.
// Consensus sequence positions generated from < AUX_MSS reads are
// masked. Tumour consensus sequence positions with < AUX_MSS reads
// supporting the chosen consensus base are masked.
const int COVERAGE_UPPER_THRESHOLD;
// Blocks with a coverage > COVERAGE_UPPER_THRESHOLD are discarded.
int N_THREADS;
const int MAX_SNPS;
// Blocks with > MAX_SNPS number of low confidence postions
// are discarded.
const double ECONT;
const double ALLELIC_FREQ_OF_ERROR;
// Aligned positions with > 1 base with a frequency above
// ALLELIC_FREQ_OF_ERROR are considered low confidence positions and masked.
std::set<int64_t> CancerExtraction;
// Elements are indicies of cancer specific reads extracted from the
// coloured GSA
std::vector<std::shared_ptr<bpBlock> > SeedBlocks;
// Elements are initial break point blocks, generated from
// seedBreakPointBlocks(), each block consists of the the cancer specific
// reads covering single location.
int64_t backUpSearchStart(int64_t seed_index);
// The worker threads that seed break point blocks from the second gsa
// may begin block generation from the middle of block.
// backUpSearchStart() moves the start of block generation to the
// start of the block a thread may have landed in the middle of.
void extractCancerSpecificReads();
// Divides coloured GSA into even chunks and deploys threads
// with each thread computing extractGroupsWorker() over an allocated chunk.
void extractionWorker(int64_t to, int64_t from, std::set<int64_t> & threadExtr);
// Passes through allocated GSA chunk and extracts reads as cancer specific
// reads if:
// -- Suffixes of cancer specific reads form a block that covers the
// same genomic location, with the number of cancer reads >= GSA_MCT1
// -- The ratio of healthy / cancer reads in the block is <= ECONT
void seedBreakPointBlocks();
// Generates the seed break point blocks from the cancer specific reads
// in CancerExraction, storing result in SeedBlocks.
// The second GSA is firsly constructed. Again, RadixSA (Rajasekran, Nicolae
// 2014) is employed to generate a suffix array, which is then split
// into even chunks, each deployed on a thread and
// transformed with transformBlock() into a second GSA chunk.
// extractGroups() then generates seed blocks from second GSA.
void buildConsensusPairs(std::shared_ptr<bpBlock> * block,
std::shared_ptr<bpBlock> * end,
std::vector<consensus_pair> & threadWork);
// Builds consensus pairs out of an allocated group of seed break point
// blocks.
void writeConsensusPairs(std::string const & consensus_pairs, std::string const & fname);
void generateConsensusSequence(bool tissue, bpBlock const& block,
int & cns_offset, std::string & cns, std::string & qual);
// For a given break point block, function builds a consensus sequence,
// and quality string for tumour or healthy derived reads.
// And returns the alignment position of the consensus sequence (cns_offset)
void buildQualityString(std::string & qual, std::vector<int> const&
freq_matrix, std::string const& cns, bool tissue);
// Generates the consensus sequence quality string with various
// masking codes
void extractNonMutatedAlleles(bpBlock &block, consensus_pair &pair);
// Using the cancer consensus sequence extracts healthy reads
// covering the same location and inserts them into the block.
// This is done by searching for a 30 character sequence of the consensus
// sequence and extracting healthy reads sharing the sequence
// If search fails, the two regions flanking of the failed search
// sequence are searched.
int64_t binarySearch(std::string const & query);
// Binary search for query in coloured GSA, returns:
// -- failed search: -1
// -- success: index of matching suffix
int lcp(std::string const & l, std::string const & r, unsigned int mlr);
// optimized lcp computer. Optimization: Starts search lcp count and comparison
// mlr and l[mlr],r[mlr] respectively. As characters at index < mlr in
// suffixes of the GSA will be identical
bool lexCompare(std::string const & l, std::string const & r, unsigned int min_lr);
// Performs an optimized comparison, with same optimization as lcp()
int minVal(int a, int b);
bool extendBlock(int64_t seed_index, bpBlock
&block, bool orientation, int calibration);
// seed_index, the location where binarSearch() found a match to
// the query is used as the start point to gather healthy reads
// that also match the query towards the left and the right in the
// coloured GSA.
// Returns false if search failed to extract any healthy reads,
// true otherwise
bool getSuffixesFromLeft(int64_t seed_index,
bpBlock &block,
bool orientation, int calibration);
// called by extendBlock(). Returns false if failed to
// extract any healthy reads, true otherwise
bool getSuffixesFromRight(int64_t seed_index,
bpBlock &block,
bool orientation, int calibration);
// called by extendBlock(). Returns false if failed to
// extract any healthy reads, true otherwise
bool excessLowQuality(consensus_pair & pair);
// Returns true if number of low quality positions is >
// MAX_SNPS, false otherwise.
int convertOffset(read_tag const& tag);
// converts alignment offset between forward and reverse orientation,
// returning the converted offset
char rc(char c, int d);
void trimHealthyConsensus(consensus_pair & pair);
// Trims distal regions of the healthy consensus sequence
// up to the last consensus character that has a corresponding
// quality string value of 'T' or 'B', and succeeds a stretch
// of consensus characters that have a contigous stretch of 'T' or 'B'
// quality string values, starting from left and right ends.
void trimCancerConsensus(consensus_pair & pair);
// Trims the distal regions of the cancer consensus sequence,
// such that the ends of the cancer consensus sequence never extend
// beyond the healthy consensus sequence in the aligned pair.
void maskLowQualityPositions(consensus_pair & pair);
// In the aligned consensus pair, at a given aligned position,
// switches the cancer consensus sequence character
// with the healthy consensus sequence character,
// if the character in either one of the quality strings at the aligned
// position is not '-'.
bool noSNV(consensus_pair const & pair);
// Returns true if pair.mutated and pair.non_mutated do not contain
// a mismatch representative of an SNV.
int64_t computeLCP(std::string::const_iterator a, std::string::const_iterator b);
void buildVariantBlocks(int64_t const * dSA, int64_t const dSA_sz, int64_t * seed_idx, int64_t const
* const to, std::vector< std::pair<int64_t, int64_t> > const & bsa,
std::string const & concat, std::set<std::shared_ptr<bpBlock>, vBlockComp > &
twork);
read_tag constructReadTag(int64_t const * dSA, int64_t const dSA_sz,
std::vector<std::pair<int64_t, int64_t> > const & bsa,
std::string const & concat, int64_t const * ptr);
void xorSwap(int64_t *x, int64_t *y);
int64_t bubbleRemove(int64_t * const a, int64_t const sz, int64_t const
invalid);
void remove_short_suffixes(int64_t* &sa, int64_t &sa_sz, int64_t
min_suffix_length, std::string const & concat);
int64_t suffixLen(int64_t const i, std::string const & concat);
int assignBaseDisregardingPhred(int const pos, std::vector<read_tag> const & block,
int const max_offset);
std::pair<int64_t, int64_t> binarySearch(std::vector< std::pair<int64_t,
int64_t> > const & bsa, int64_t sa_pos);
void buildFastqAndTumourCNSData(std::list<consensus_pair> & consensus_pairs, std::string & fastq);
bool singleIndel(std::string const & cigar);
bool containsIndel(std::string const & tumour, std::string const & control);
public:
SNVIdentifier(GSA & gsa,
std::string const& basename,
char min_phred, int gsa1_mct, int gsa2_mct,
int coverage_upper_threshold,
int n_threads, int max_low_confidence_pos,
double econt, double allelic_freq_of_error);
std::vector<std::string> tumour_cns;
// DEBUG
void printAlignedBlock(bpBlock block);
void printExtractedCancerReads();
std::string addGaps(int ngaps);
};
#endif