-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenInversions.cpp
More file actions
307 lines (276 loc) · 7.06 KB
/
ScreenInversions.cpp
File metadata and controls
307 lines (276 loc) · 7.06 KB
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
#include <iostream>
#include <string>
#include <sstream>
#include <map>
using namespace std;
#include "InversionAlign.h"
#include "FASTAReader.h"
#include "FASTASequence.h"
#include <semaphore.h>
class MappingSemaphores {
public:
sem_t reader;
sem_t writer;
sem_t unaligned;
sem_t hitCluster;
MappingSemaphores& operator=(MappingSemaphores &rhs) {
return *this;
}
void InitializeAll() {
sem_init(&reader, 0, 1);
sem_init(&writer, 0, 1);
sem_init(&unaligned, 0, 1);
sem_init(&hitCluster, 0, 1);
}
};
ifstream samIn;
vector<FASTASequence> genome;
map<string, FASTASequence*> chromMap;
MappingSemaphores semaphores;
ofstream tableOut;
int seqIndex;
bool isFofn, makeRC, makeDotplot, doSoftClip;
int window = 0;
int wordSize = 11;
void ScreenInversions() {
while (true) {
sem_wait(&semaphores.reader);
if (samIn.good() == false) {
sem_post(&semaphores.reader);
break;
}
string line;
getline(samIn, line);
if (line.size() == 0 or line[0] == '@') {
sem_post(&semaphores.reader);
if (line.size() == 0) {
break;
}
else {
continue;
}
}
//
// Finished reading line
//
sem_post(&semaphores.reader);
if (isFofn) {
ifstream localSam(line.c_str());
getline(localSam, line);
//
// Advance to the first alignment
//
while (line.size() > 0 and line[0] == '@') {
getline(localSam, line);
}
}
stringstream lineStrm(line);
string readName, chrom, cigar, tmpString, seq;
int flag, mapq, tmp, pos, tLen;
int pnext;
lineStrm >> readName >> flag >> chrom >> pos >> mapq >> cigar >> tmpString >> pnext >> tLen >> seq;
FASTASequence ref;
int additionalLength = 0;
cerr << "screening " << readName << endl;
if (chromMap.find(chrom) == chromMap.end()) {
cerr << "skipping:" << readName << " " << chrom << endl;
continue;
}
if (pos + window + tLen > chromMap[chrom]->length) {
int endWindow = chromMap[chrom]->length - pos - tLen;
additionalLength += endWindow;
}
else {
additionalLength += window;
}
if (pos - window < 0) {
additionalLength += pos - window;
pos = 0;
}
else {
additionalLength += window;
pos = pos - window;
}
ref.ReferenceSubstring(*chromMap[chrom], pos, tLen+additionalLength);
ref.CopyTitle("genome");
vector<int> lens;
vector<char> ops;
stringstream cigarStrm(cigar);
while (cigarStrm) {
int l;
char o;
if ( (cigarStrm >> l >> o).good() == false) {
break;
}
lens.push_back(l);
ops.push_back(o);
}
int i= 0;
int startOp = 0;
int clipFront = 0;
while (i < lens.size()) {
if (ops[i] == 'M' or ops[i] == 'I' or ops[i] == 'D') {
startOp = i;
break;
}
if (ops[i] == 'S') {
clipFront = lens[i];
}
i++;
}
int clipEnd = 0;
i = lens.size() - 1;
while (i > startOp) {
if (ops[i] == 'M' or ops[i] == 'I' or ops[i] == 'D') {
startOp = i;
break;
}
if (ops[i] == 'S') {
clipEnd = lens[i];
}
i--;
}
if (doSoftClip == false) {
clipFront = clipEnd = 0;
}
seq = seq.substr(clipFront, (seq.size() - clipEnd) - clipFront);
if (seq.size() == 0) {
continue;
}
FASTASequence query;
query.CopyTitle(readName);
query.seq = (Nucleotide*) seq.c_str();
query.length = seq.size();
query.deleteOnExit = false;
int res;
int invCoords[4], invCoordsRC[4];
if (makeRC) {
FASTASequence rcQuery;
query.MakeRC(rcQuery);
int forwardScore, reverseScore;
forwardScore = SDPAlignLite(query, ref, wordSize);
reverseScore = SDPAlignLite(rcQuery, ref, wordSize);
res = -1;
int resRC = -1;
float frac = 0.4;
if (forwardScore > frac * query.length) {
res = InversionAlign(query, ref, wordSize, invCoords, makeDotplot);
}
if (reverseScore > frac * query.length ) {
resRC = InversionAlign(rcQuery, ref, wordSize, invCoordsRC, makeDotplot);
}
if (reverseScore > frac * query.length) {
if (resRC > res and resRC > forwardScore) {
memcpy(invCoords, invCoordsRC, sizeof(int)*4);
res = resRC;
}
}
rcQuery.Free();
}
else {
res = InversionAlign(query, ref, wordSize, invCoords, makeDotplot);
}
if (res > 20) {
float frac = 0.01;
if (invCoords[0] > frac * query.length and invCoords[2] < (1-frac)*query.length) {
cout << readName << " " << res << endl;
sem_wait(&semaphores.writer);
tableOut << chrom << "\t" << pos + invCoords[2] << "\t" << pos + invCoords[3] + wordSize << "\t" << readName << "\t" << res << endl;
sem_post(&semaphores.writer);
}
}
++seqIndex;
if (seqIndex % 1000 ==0 ) {
cerr << "processed " << seqIndex << endl;
}
delete ref.title;
delete query.title;
}
}
int main(int argc, char* argv[]) {
stringstream helpStrm;
helpStrm << " Usage: screenInversions sam genome output " << endl;
helpStrm << " -j (int) Use n cores." << endl;
helpStrm << " -f Sam is fofn of sam files." << endl;
helpStrm << " -w (int) Expand reference by window." << endl;
helpStrm << " -r Allow reverse complement." << endl;
helpStrm << " -d Make dotplot" << endl;
helpStrm << " -k (int) Use k for matching ( k <= 15). " << endl;
helpStrm << " --noClip Do not clip" << endl;
string help = helpStrm.str();
if (argc < 4) {
cout << help;
exit(1);
}
string inFileName = argv[1];
string genomeFileName = argv[2];
string outputFasta = argv[3];
int argi = 4;
int nProc = 1;
isFofn = false;
makeRC = false;
makeDotplot = false;
doSoftClip = true;
window = 0;
while (argi < argc) {
if (strcmp(argv[argi], "-j") == 0) {
nProc = atoi(argv[++argi]);
}
else if (strcmp(argv[argi], "-f") == 0) {
isFofn = true;
}
else if (strcmp(argv[argi], "-r") == 0) {
makeRC = true;
}
else if (strcmp(argv[argi], "-d") == 0) {
makeDotplot = true;
}
else if (strcmp(argv[argi], "-w") == 0) {
window = atoi(argv[++argi]);
}
else if (strcmp(argv[argi], "--noClip") == 0) {
doSoftClip = false;
}
else if (strcmp(argv[argi], "-k") == 0) {
wordSize = atoi(argv[++argi]);
}
else {
cout << help << endl;
cout << "Error with option " << argv[argi] << endl;
exit(1);
}
++argi;
}
tableOut.open(outputFasta.c_str());
//
// Since many alignments will be performed against the genome,
// read it all at once, rather than seeking.
//
FASTAReader genomeReader;
genomeReader.Initialize(genomeFileName);
genomeReader.ReadAllSequences(genome);
int i;
for (i = 0; i < genome.size(); i++) {
chromMap[genome[i].title] = &genome[i];
}
samIn.open(inFileName.c_str());
int seqIndex = 0;
pthread_t *threads = new pthread_t[nProc];
pthread_attr_t *threadAttr = new pthread_attr_t[nProc];
// MappingSemaphores semaphores;
//
// When there are multiple processes running along, sometimes there
// are semaphores to worry about.
//
semaphores.InitializeAll();
int procIndex;
for (procIndex = 0; procIndex < nProc; procIndex++ ){
pthread_attr_init(&threadAttr[procIndex]);
}
for (procIndex = 0; procIndex < nProc; procIndex++) {
pthread_create(&threads[procIndex],&threadAttr[procIndex], (void* (*)(void*))ScreenInversions, NULL);
}
for (procIndex = 0; procIndex < nProc; procIndex++) {
pthread_join(threads[procIndex], NULL);
}
}