-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMapRead.h
265 lines (241 loc) · 7.9 KB
/
MapRead.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
#ifndef MAP_READ_H_
#define MAP_READ_H_
#include <math.h>
#include "MMIndex.h"
#include "Genome.h"
#include "Read.h"
#include "Options.h"
#include "CompareLists.h"
#include "Sorting.h"
#include "TupleOps.h"
#include "Clustering.h"
#include "AffineOneGapAlign.h"
#include "TupleOps.h"
#include "SparseDP.h"
#include "SparseDP_Forward.h"
#include "Chain.h"
#include "overload.h"
#include "LinearExtend.h"
#include "SplitClusters.h"
#include "Timing.h"
#include "ClusterRefine.h"
#include "IndelRefine.h"
#include "LocalRefineAlignment.h"
#include "Map_lowacc.h"
#include "Map_highacc.h"
#include <iostream>
#include <algorithm>
#include <iterator>
#include <ctime>
#include <cmath> // std::log
#include <sstream>
#include <thread>
#include <climits>
#include <map>
using namespace std;
class SortClusterBySize {
public:
bool operator()(const Cluster &a, const Cluster &b) {
return a.matches.size() > b.matches.size();
}
};
class SortAlignmentsByMatches {
public:
bool operator()(const SegAlignmentGroup a, const SegAlignmentGroup b) const {
return a.nm > b.nm;
}
};
void RankClustersByScore(vector<Cluster> &clusters) {
sort(clusters.begin(), clusters.end(), SortClusterBySize());
}
int SetStrand(Read &read, Genome &genome, const Options &opts, GenomePairs &matches) {
int nSame=0;
int nDifferent=0;
for (int m=0; m< matches.size(); m++) {
int chromIndex = genome.header.Find(matches[m].second.pos);
char *chrom=genome.seqs[chromIndex];
int chromPos = matches[m].second.pos - genome.header.pos[chromIndex];
GenomeTuple readTup, genomeTup;
StoreTuple(read.seq, matches[m].first.pos, opts.globalK, readTup);
StoreTuple(chrom, chromPos, opts.globalK, genomeTup);
if (readTup.t == genomeTup.t) {
nSame++;
}
else {
nDifferent++;
}
}
if (nSame > nDifferent) {
return 0;
}
else {
return 1;
}
}
template<typename T>
void SwapReadCoordinates(vector<T> &matches, GenomePos readLength, GenomePos kmer){
for (int i=0; i < matches.size(); i++) {
matches[i].first.pos = readLength - (matches[i].first.pos+ kmer);
}
}
// void ReverseClusterStrand(Read &read, Genome &genome, const Options &opts, vector<Cluster> &clusters) {
// for (int c = 0; c < clusters.size(); c++) {
// SwapStrand(read, opts, clusters[c].matches);
// clusters[c].strand = 1;
// }
// }
// void SetClusterStrand(Read &read, Genome &genome, const Options &opts,
// vector<Cluster> &clusters) {
// for (int c = 0; c < clusters.size(); c++) {
// clusters[c].strand = SetStrand(read, genome, opts, clusters[c].matches);
// if (clusters[c].strand == 1) {
// SwapStrand(read, opts, clusters[c].matches);
// }
// }
// }
void
SeparateMatchesByStrand(Read &read, Genome &genome, int k, vector<GenomePair> &allMatches, vector<GenomePair> &forMatches,
vector<GenomePair> &revMatches, string &baseName) {
//
// A value of 0 implies forward strand match.
//
vector<bool> strand(allMatches.size(), 0);
int nForward=0;
for (int i=0; i < allMatches.size(); i++) {
int readPos = allMatches[i].first.pos;
uint64_t refPos = allMatches[i].second.pos;
char *genomePtr = genome.GlobalIndexToSeq(refPos);
//
// Read and genome are identical, the match is in the forward strand
if (strncmp(&read.seq[readPos], genomePtr, k) == 0) {
nForward++;
}
else {
//
// The k-mers are not identical, but a match was stored between
// readPos and *genomePtr, therefore the match must be reverse.
//
strand[i] = true;
}
}
//
// Populate two lists, one for forward matches one for reverse.
//
forMatches.resize(nForward);
revMatches.resize(allMatches.size() - nForward);
int i = 0,r = 0,f = 0;
for (i = 0,r = 0,f = 0; i < allMatches.size(); i++) {
if (strand[i] == 0) {
forMatches[f] = allMatches[i];
f++;
}
else {
revMatches[r] = allMatches[i];
r++;
}
}
}
int
MapRead(const vector<float> & LookUpTable, Read &read, Genome &genome, vector<GenomeTuple> &genomemm, LocalIndex &glIndex, const Options &opts,
ostream *output, ostream *svsigstrm, Timing &timing, IndelRefineBuffers &indelRefineBuffers, pthread_mutex_t *semaphore=NULL) {
read.unaligned = 0;
string baseName = read.name;
for (int i=0; i < baseName.size(); i++) {
if (baseName[i] == '/') baseName[i] = '_';
if (baseName[i] == '|') baseName[i] = '_';
}
vector<GenomeTuple> readmm; // readmm stores minimizers
vector<pair<GenomeTuple, GenomeTuple> > allMatches, forMatches, revMatches;
timing.Start();
//
// Add pointers to seq that make code more readable.
//
char *readRC;
CreateRC(read.seq, read.length, readRC);
char *strands[2] = { read.seq, readRC };
if (opts.storeAll) {
Options allOpts = opts;
allOpts.globalW=1;
StoreMinimizers<GenomeTuple, Tuple>(read.seq, read.length, allOpts.globalK, allOpts.globalW, readmm, true);
// StoreMinimizers_noncanonical<GenomeTuple, Tuple>(read.seq, read.length, allOpts.globalK, allOpts.globalW, readmm, true);
}
else {
// Options partOpts = opts;
// partOpts.globalW = opts.globalW - 5;
StoreMinimizers<GenomeTuple, Tuple>(read.seq, read.length, opts.globalK, opts.globalW, readmm, true);
// StoreMinimizers_noncanonical<GenomeTuple, Tuple>(read.seq, read.length, opts.globalK, opts.globalW, readmm, true);
}
timing.Tick("Store minimizers");
sort(readmm.begin(), readmm.end()); //sort kmers in readmm(minimizers)
timing.Tick("Sort minimizers");
//
// Add matches between the read and the genome.
//
CompareLists<GenomeTuple, Tuple>(readmm, genomemm, allMatches, opts, true);
timing.Tick("CompareLists");
if (opts.dotPlot and opts.readname == read.name ) {
ofstream clust("all-matches.dots");
for (int m = 0; m < allMatches.size(); m++) {
clust << allMatches[m].first.pos << "\t" << allMatches[m].second.pos
<< "\t" << allMatches[m].first.pos + opts.globalK << "\t"
<< allMatches[m].second.pos+ opts.globalK << endl;
}
clust.close();
}
SeparateMatchesByStrand(read, genome, opts.globalK, allMatches, forMatches, revMatches, baseName);
allMatches.clear();
if (forMatches.size() == 0 and revMatches.size() == 0) {
read.unaligned = 1;
output_unaligned(read, opts, *output);
return 0;
}
if (opts.debug and opts.dotPlot and opts.readname == read.name ) {
ofstream fclust("for-matches_original.dots");
for (int m = 0; m < forMatches.size(); m++) {
fclust << forMatches[m].first.pos << "\t" << forMatches[m].second.pos << "\t" << opts.globalK + forMatches[m].first.pos << "\t"
<< forMatches[m].second.pos + opts.globalK << "\t" << m << endl;
}
fclust.close();
ofstream rclust("rev-matches_original.dots");
for (int m=0; m < revMatches.size(); m++) {
rclust << revMatches[m].first.pos << "\t" << revMatches[m].second.pos + opts.globalK << "\t" << opts.globalK + revMatches[m].first.pos << "\t"
<< revMatches[m].second.pos << "\t" << m << endl;
}
rclust.close();
ofstream rclustdiag("rev-matches_original.diag.dots");
for (int m=0; m < revMatches.size(); m++) {
rclustdiag << revMatches[m].first.pos - revMatches[m].second.pos << "\t" << opts.globalK + revMatches[m].first.pos << "\t"
<< revMatches[m].second.pos << "\t" << m << endl;
}
rclustdiag.close();
}
int rt = 0;
if (opts.bypassClustering) {
rt = MapRead_lowacc(forMatches, revMatches, LookUpTable, read, genome, genomemm, glIndex, opts, output, svsigstrm,
timing, indelRefineBuffers, strands, readRC, semaphore);
}
else {
rt = MapRead_highacc(forMatches, revMatches, LookUpTable, read, genome, genomemm, glIndex, opts, output, svsigstrm,
timing, indelRefineBuffers, strands, readRC, semaphore);
}
delete[] readRC;
return rt;
// /*
// if (semaphore != NULL ) {
// pthread_mutex_unlock(semaphore);
// }
// */
// //
// // Done with one read. Clean memory.
// //
// delete[] readRC;
// for (int a = 0; a < alignments.size(); a++) {
// for (int s = 0; s < alignments[a].SegAlignment.size(); s++) {
// delete alignments[a].SegAlignment[s];
// }
// }
// //read.Clear();
// if (alignments.size() > 0) return 1;
// return 0;
}
#endif