-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnucbase.hxx
402 lines (332 loc) · 10.5 KB
/
nucbase.hxx
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#ifndef NUCBASE_HXX
#define NUCBASE_HXX
#include "nucsequences.hpp"
#include "nucbase.hpp"
#include <stdexcept>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <omp.h>
using namespace std;
template <bool MAPNUM, bool MISMATCHES, bool SUBMATCHES, bool BWT>
bool NucBase::processDatabase(NucSequences & sequences, const vector<int> & columns, int mismatch, int submatch, bool absent, vector<int> &progress) const
{
bool output_open = true;
int ncol = columns.size();
int nseq = sequences.size();
vector<vector<int> > sums;
if(MAPNUM)
if(nseq > 1)
sums.resize(ncol, vector<int>(_nlines,0));
// We modify the labels with mismatches and submatches info
vector<string> newLabels(_labels);
vector<string>::iterator it;
for(it=newLabels.begin(); it<newLabels.end(); ++it)
{
ostringstream oss;
oss << *it;
if(absent)
oss << "_absent";
if(MISMATCHES)
oss << "_" << mismatch << "mm";
if(SUBMATCHES)
oss << "_" << submatch << "minblock";
// We modify the labels
*it = oss.str();
}
// We try to be multithread
#ifdef OMP_H
int numthreads = 1;
numthreads = max(min(omp_get_num_procs(), nseq), 1);
omp_set_num_threads(numthreads);
progress.resize(numthreads,0);
#endif
#ifdef OMP_H
#pragma omp parallel for
#endif
for(int j=0; j<nseq; ++j)
{
int thread = 0;
#ifdef OMP_H
thread = omp_get_thread_num();
#endif
// BWT
if(BWT)
{
#ifdef OMP_H
#pragma omp critical
#endif
sequences[j].bwt();
}
// We initialize the outputs
// array : 1st quarter: gff3, 2nd quarter: sense, 3rd quarter: antisense, 4th quarter: seq_mapnum
int noutputs = 3*ncol;
if(MAPNUM)
noutputs += ncol;
ofstream * output = new ofstream[noutputs];
for(int i=0; i<ncol; ++i)
{
ostringstream oss;
oss << _outputfolder << sequences[j].name() << "/" << sequences[j].name() << "_" << newLabels[columns[i]];
string name_gff3(oss.str());
name_gff3 += ".gff3";
output[i+0*ncol].open(name_gff3.c_str());
string name_sense(oss.str());
name_sense += "_sense.txt";
output[i+1*ncol].open(name_sense.c_str());
string name_antisense(oss.str());
name_antisense += "_antisense.txt";
output[i+2*ncol].open(name_antisense.c_str());
output_open &= output[i+0*ncol].is_open();
output_open &= output[i+1*ncol].is_open();
output_open &= output[i+2*ncol].is_open();
if(MAPNUM)
{
ostringstream oss;
oss << _outputfolder
<< sequences[j].name() << "/"
<< newLabels[columns[i]] << "_"
<< sequences[j].name();
string name_mapnum(oss.str());
name_mapnum += "_mapnum.txt";
output[i+3*ncol].open(name_mapnum.c_str());
output_open &= output[i+3*ncol].is_open();
output[i+3*ncol] << "labels\t" << sequences[j].name() << "_mapnum\t" << newLabels[columns[i]] << endl;
}
}
// We open the database file
ifstream input(_inputname.c_str());
if(input.is_open() && output_open)
{
string line;
string valone = "1";
// Line number
int l = 0;
// If the first line contains labels, we skip it
if(_labelled)
getline(input, line);
// We label the output file
labelOutputs(output, columns, newLabels, sequences[j].name());
// As long as we can read the file.
while(getline(input, line))
{
// We get and parse the line.
string word;
vector<string> words;
stringstream strstr(line);
while (getline(strstr, word, '\t'))
words.push_back(word);
// We get the additional info (if present)
const string & mapnum = words[_colmapnum];
const string & name = words[_colname];
const string & seq = words[0];
// We process the defined columns
for(int i=0; i<ncol; ++i)
{
string & val = columns[i]==0?valone:words[columns[i]];
// But only if they are present (!="0") in the corresponding database (==column)
if(val != "0")
{
// Sense
NucQuery sense;
sense.name(name);
sense.sequence(seq);
sense.sense(true);
// We look for the sense piRNA in the sequence.
sequences[j].search<SUBMATCHES,MISMATCHES,BWT>(sense, mismatch, submatch);
// We output the sense results (gff3)
writeOutput<true , SUBMATCHES>(output[i+0*ncol], sense, sequences[j], absent, val, mapnum);
// We output the sense results (tables)
writeOutput<false, SUBMATCHES>(output[i+1*ncol], sense, sequences[j], absent, val, mapnum);
// Antisense
NucQuery antisense;
antisense.name(name);
antisense.sequence(Nuc::complementary(seq));
antisense.sense(false);
// We look for the antisense piRNA in the sequence.
sequences[j].search<SUBMATCHES,MISMATCHES,BWT>(antisense, mismatch, submatch);
// We output the antisense results (gff3)
writeOutput<true , SUBMATCHES>(output[i+0*ncol], antisense, sequences[j], absent, val, mapnum);
// We output the antisense results (tables)
writeOutput<false, SUBMATCHES>(output[i+2*ncol], antisense, sequences[j], absent, val, mapnum);
if(MAPNUM)
{
int lsum = sense.count() + antisense.count();
if(SUBMATCHES)
{
NucQuery * elt = sense.next;
while(elt != 0)
{
lsum += elt->count();
elt = elt->next;
}
elt = antisense.next;
while(elt != 0)
{
lsum += elt->count();
elt = elt->next;
}
}
if(nseq > 1)
{
#ifdef OMP_H
#pragma omp atomic
#endif
sums[i][l] += lsum;
}
if((lsum>0) != absent)
output[i+3*ncol] << seq << "\t" << lsum << "\t" << val << endl;
}
}
}
if(MAPNUM)
++l;
++progress[thread];
}
input.close();
for(int i=0; i<noutputs; ++i)
output[i].close();
}
else
throw ios::failure( "ProcessDatabase : error opening database and/or results files !" );
delete [] output;
// Inverse BWT
if(BWT)
sequences[j].inverse_bwt();
}
if(MAPNUM)
{
if(nseq > 1)
{
for(int i=0; i<ncol; ++i)
{
vector<int> & sum = sums[i];
ostringstream oss;
oss << _outputfolder
<< newLabels[columns[i]] << "_"
<< nseq << "seqs";
string name_mapnum(oss.str());
name_mapnum += "_mapnum.txt";
ofstream output(name_mapnum.c_str());
output_open &= output.is_open();
ifstream input(_inputname.c_str());
if(input.is_open() && output_open)
{
string line;
string word;
int l = 0;
if(_labelled)
getline(input,line);
output << "labels\tmap_number\t" << newLabels[columns[i]] << endl;
while(getline(input, line))
{
vector<string> words;
stringstream strstr(line);
while (getline(strstr, word, '\t'))
words.push_back(word);
if((sum[l]>0) != absent)
output << words[0] << "\t" << sum[l] << "\t" << words[columns[i]] << endl;
++l;
}
}
}
}
}
return output_open;
}
template <bool GFF3, bool SUBMATCHES>
void NucBase::writeOutput(ofstream & out, NucQuery & query, NucSequence & sequence, const bool absent, const string & val, const string & mapnum) const
{
const string & seqname = sequence.name();
const string & queryname = query.name();
if(GFF3)
{
size_t querysize = query.sequence().size();
int count = query.count();
if(query.sense())
{
for(int i=0; i<count; ++i)
out << seqname << "\tNucBase\tpiRNA\t" << 1+query.position(i) << "\t" << query.position(i)+querysize
<< "\t.\t+\t.\tName=" << query.sequence() << ";Alias=" << queryname
//<< ";ID=" << info
<< endl;
}
else
{
for(int i=0; i<count; ++i)
out << seqname << "\tNucBase\tpiRNA\t" << 1+query.position(i) << "\t" << query.position(i)+querysize
<< "\t.\t-\t.\tName=" << query.sequence() << ";Alias=" << queryname
//<< ";ID=" << info
<< endl;
}
}
else
{
int count = query.count();
if(SUBMATCHES)
{
NucQuery * elt = query.next;
while(elt != 0)
{
count += elt->count();
elt = elt->next;
}
}
// If we have a match, we output it
if( (count > 0) != absent )
{
out << query.name() << "\t" << count << "\t" << val;
// Info is mapnum in "tables" format
if(_colmapnum > 0)
out << "\t" << mapnum << endl;
else
out << endl;
}
}
if(SUBMATCHES)
{
// If we have submatches, we go through the (short) list (and delete it at the same time)
NucQuery * elt = query.next;
bool empty = (query.next == 0) & (query.count() == 0);
while(elt != 0)
{
if(GFF3)
{
size_t querysize = elt->sequence().size();
int count = elt->count();
if(elt->sense())
{
for(int i=0; i<count; ++i)
out << seqname << "\tNucBase\tpiRNA\t" << 1+elt->position(i) << "\t" << elt->position(i)+querysize
<< "\t.\t+\t.\tName=" << elt->sequence() << ";Alias=" << queryname
//<< ";ID=" << info
<< endl;
}
else
{
for(int i=0; i<count; ++i)
out << seqname << "\tNucBase\tpiRNA\t" << 1+elt->position(i) << "\t" << elt->position(i)+querysize
<< "\t.\t-\t.\tName=" << elt->sequence() << ";Alias=" << queryname
//<< ";ID=" << info
<< endl;
}
elt = elt->next;
}
else
{
string seq = elt->sequence();
if(!elt->sense())
seq = Nuc::complementary(seq);
if( (elt->count() > 0) != absent )
out << seq << "\t" << elt->count() << endl;
elt = elt->next;
}
}
// We separate the submatches results (in "tables" format)
if(!GFF3)
if( empty == absent )
out << endl;
}
}
#endif