Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
454 lines (393 loc) · 14 KB
/
Copy pathmain.cpp
File metadata and controls
454 lines (393 loc) · 14 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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <map>
#include <limits>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <utility>
#include <cmath>
#include <algorithm>
#include "lib/gtf-cpp/gtf.h"
#include "lib/pdqsort/pdqsort.h"
#include "lib/fasta-cpp/fasta.h"
#include "introns.h"
// 3' PWM
static introns::Matrix pwm_lod_3p;
// 5' PWM
static introns::Matrix pwm_lod_5p;
// B' PWM
static introns::Matrix pwm_lod_Bp;
// parse a pwm file and return the pwm
// this also applies the `score' function to the values to make a lod
static introns::Matrix parse_pwm(const std::string&);
// parser-only operation
static int get_introns(const std::string& gtffile,
const std::string& fastafile,
std::vector<introns::Intron>& outputintrons);
// scores and normalizes introns
static void score_and_normalize_introns(std::vector<introns::Intron>& introns);
static void output_introns_fasta(std::vector<introns::Intron>& introns);
static void output_introns_gtf(std::vector<introns::Intron>& introns);
static void usage(char* executable) {
std::fprintf(stderr,
"Usage:\n"
"\t1: %s --parse [gtf] [fasta]\n"
"\t2: %s [3' pwm] [5' pwm] [B' pwm] [gtf] [fasta]\n"
"\n\t\t1: Parse a FASTA file with a GTF file.\n"
"\n\t\t2: Take a 3' PWM, 5' PWM, and B' PWM and predict U12 introns from"
"\n\t\t the given GTF and FASTA files.\n",
executable, executable);
}
int main(int argc, char** argv) {
std::string pwmfilename_3p,
pwmfilename_5p,
pwmfilename_Bp,
gtffilename,
fastafilename,
outputfilename;
bool parseronly = false;
if (argc == 1) {
usage(argv[0]);
return 1;
}
if (argc > 1) {
parseronly = (0 == std::strcmp(argv[1], "--parse"));
if (!parseronly) {
pwmfilename_3p = argv[1];
}
}
if (argc > 2) {
if (parseronly) {
gtffilename = argv[2];
} else {
pwmfilename_5p = argv[2];
}
}
if (argc > 3) {
if (parseronly) {
fastafilename = argv[3];
} else {
pwmfilename_Bp = argv[3];
}
}
if (!parseronly) {
if (argc > 4) {
gtffilename = argv[4];
}
if (argc > 5) {
fastafilename = argv[5];
}
}
if ((parseronly && argc < 5) || (!parseronly && argc < 6)) {
usage(argv[0]);
return 1;
}
// do parser-only operation
if (parseronly) {
std::vector<introns::Intron> introns;
int ret = get_introns(gtffilename, fastafilename, introns);
if (ret == 0) {
output_introns_fasta(introns);
output_introns_gtf(introns);
}
return 0;
}
pwm_lod_3p = parse_pwm(pwmfilename_3p);
pwm_lod_5p = parse_pwm(pwmfilename_5p);
pwm_lod_Bp = parse_pwm(pwmfilename_Bp);
std::cout << "-> Loaded PWMs\n";
std::vector<introns::Intron> introns;
// get the introns
int ret = get_introns(gtffilename, fastafilename, introns);
if (ret != 0) return ret;
score_and_normalize_introns(introns);
std::cout << "-> Scored introns\n";
output_introns_fasta(introns);
output_introns_gtf(introns);
return 0;
}
// get introns
static int get_introns(const std::string& gtffile,
const std::string& fastafile,
std::vector<introns::Intron>& outputintrons) {
// open the GTF file
GTFFile gtf;
gtf.setfilename(gtffile);
try {
gtf.load_filter([](const auto& seq) {
return seq.feature == "exon";
});
} catch (const GTFError& e) {
std::cerr << e.what() << '\n';
return 1;
}
std::vector<GTFSequence>& tmpexons = gtf.getall();
// sort exons by start
pdqsort(tmpexons.begin(), tmpexons.end(), [](GTFSequence& a, GTFSequence& b) {
return a.start < b.start;
});
// split the exons into a map:
/*
* {
* gene_id: {
* transcript_id: {
* { exons, introns }
* }
* }
* }
*/
std::map<std::string, // gene_id
std::map<std::string, // transcript_id
std::pair<std::vector<GTFSequence>, // exons
std::vector<introns::Intron>>>> // introns
exons_by_transcript_by_gene;
for (auto& exon : tmpexons) {
exons_by_transcript_by_gene
[exon.attributes["gene_id"]]
[exon.attributes["transcript_id"]].first.push_back(exon);
}
// clean up memory as needed
for (auto& [gene_id, tmap] : exons_by_transcript_by_gene) {
for (auto& [tid, emap] : tmap) {
emap.first.shrink_to_fit();
}
}
std::cout << "-> Loaded " << tmpexons.size() << " exons\n";
FASTAFile fasta;
if (!fasta.open(fastafile)) {
std::cerr << "Error opening fasta file: " << fastafile << '\n';
return 1;
}
outputintrons.clear();
std::string tmpstr;
int tcount = 0;
for (auto& [gene_id, exons_by_transcript] : exons_by_transcript_by_gene) {
for (auto& [transcript_id, pair] : exons_by_transcript) {
if (tcount++ == 10) goto done;
std::cerr << "gene_id: " << gene_id << "; transcript " << transcript_id << '\n';
auto& [exons, _introns] = pair;
for (std::size_t i = 0; i < exons.size() - 1; i++) {
// cut out overlapped exons
if (exons[i].end > exons[i+1].start) continue;
try {
// 3nt from upstream
// 4nt from downstream
tmpstr = fasta.get_sequence(exons[i].end - 2, exons[i+1].start + 3, true);
// 5' is the first 14 chars, 3' is the last 18
// the full sequence is everything minus the first 3 and last 4
_introns.push_back({
exons[i].seqname, // sequence
tmpstr.substr(0, 14), // 5'
tmpstr.substr(tmpstr.size() - 18), // 3'
"", // B'
tmpstr.substr(3, tmpstr.size() - 7), // whole intron
exons[i].end+1, exons[i+1].start-1, // start and end
transcript_id,
gene_id,
{ transcript_id }, // set the list of tids
.strand = exons[i].strand, // negative?
});
// if this is negative, reverse + complement the strands
if (_introns.back().strand == '-') {
_introns.back() = !(_introns.back());
}
} catch(const std::runtime_error& e) {
std::cerr << "Error: " << e.what() << '\n';
return 1;
}
}
}
}
done:
// keep a map of gene id to transcript id to duplicate counts
std::map<std::string, std::map<std::string, unsigned long long>> dupecounts;
for (auto& [gene_id, exons_by_transcript] : exons_by_transcript_by_gene) {
for (auto& [transcript_id, pair] : exons_by_transcript) {
auto& [exons, _introns] = pair;
for (auto& intron : _introns) {
for (auto& [ts_id, pair2] : exons_by_transcript) {
auto& [exons2, _introns2] = pair2;
for (auto& other : _introns2) {
if (&other == &intron) continue;
if (!other.keep_in_output) continue;
if (std::abs((long long)intron.start - (long long)other.start) <= 20L && std::abs((long long)intron.end - (long long)other.end) <= 20L) {
intron.keep_in_output = true;
dupecounts[gene_id][ts_id]++;
if (std::find(other.all_transcripts.begin(),
other.all_transcripts.end(),
transcript_id)
== other.all_transcripts.end()) {
other.all_transcripts.push_back(transcript_id);
}
} else {
// if they overlap still, we need to check if one is
// really big and throw it out if it's huge
if (intron.start < other.end && intron.end > other.start) {
if (intron.full_sequence.size() > other.full_sequence.size()) {
intron.keep_in_output = false;
} else if (other.full_sequence.size() > intron.full_sequence.size()) {
other.keep_in_output = false;
}
}
}
}
}
}
}
}
for (auto& [gene_id, exons_by_transcript] : exons_by_transcript_by_gene) {
for (auto& [transcript_id, pair] : exons_by_transcript) {
auto& [exons, _introns] = pair;
for (auto& intron : _introns) {
if (intron.keep_in_output) {
outputintrons.push_back(intron);
}
}
}
}
outputintrons.shrink_to_fit();
std::cout << "-> Loaded " << outputintrons.size() << " introns\n";
std::time_t t = std::time(nullptr);
char mbstr[100];
std::strftime(mbstr, 100, "duplicatestats-%Y%m%d%H%M.csv", std::localtime(&t));
std::ofstream outfile(mbstr);
if (!outfile) {
std::cerr << "Error opening " << mbstr << " for writing!\n"
<< "Continuing without duplicate stats output.\n";
} else {
outfile << "\"gene_id\",\"transcript_id\",\"duplicates\"\r\n";
for (auto& [gid, ts] : dupecounts) {
for (auto& [tid, dupes] : ts) {
outfile << '"' << gid << "\",\"" << tid << "\"," << dupes << "\r\n";
}
}
outfile.close();
std::cout << "-> Wrote duplicate stats to " << mbstr << '\n';
}
return 0;
}
// scores and normalizes introns
static void score_and_normalize_introns(std::vector<introns::Intron>& introns) {
for (auto& intron : introns) {
double sum = 0.0;
for (std::size_t i = 0; i < intron.five_prime.size(); i++) {
sum += pwm_lod_5p[intron.five_prime[i]][i];
}
for (std::size_t i = 0; i < intron.three_prime.size(); i++) {
sum += pwm_lod_3p[intron.three_prime[i]][i];
}
intron.score = sum;
}
double min = std::numeric_limits<double>::max(),
max = std::numeric_limits<double>::lowest();
for (auto& i : introns) {
if (i.score > max) max = i.score;
if (i.score < min) min = i.score;
}
for (auto& i : introns) {
i.score_normalized = introns::normalize(i.score, min, max);
}
}
// parse a pwm from a file
// this also applies the `score' function to the values to make a lod
static introns::Matrix parse_pwm(const std::string& pwmfilename) {
introns::Matrix _pwm{
{'G', {}},
{'T', {}},
{'A', {}},
{'C', {}},
};
std::ifstream pwmfile(pwmfilename);
if (!pwmfile) {
throw std::runtime_error("Error opening file: " + pwmfilename);
}
char nt;
std::stringstream ss;
std::string line;
double tmp;
int count;
while (std::getline(pwmfile, line)) {
count = 0;
ss = std::stringstream(line);
while (ss) {
ss >> tmp;
if (ss) {
count++;
if(count == 1) {
nt = 'A';
} else if (count == 2) {
nt = 'C';
} else if (count == 3) {
nt = 'G';
} else if (count == 4) {
nt = 'T';
}
_pwm[nt].push_back(introns::log_odds(tmp));
}
}
}
pwmfile.close();
return _pwm;
}
static void output_introns_fasta(std::vector<introns::Intron>& introns) {
std::time_t t = std::time(nullptr);
char mbstr[100];
std::strftime(mbstr, 100, "introns-%Y%m%d%H%M.fa", std::localtime(&t));
std::ofstream outfile(mbstr);
if (!outfile) {
throw std::runtime_error("Error opening file for writing: " + std::string(mbstr));
return;
}
std::cout << "-> Writing sequences to " << mbstr << '\n';
for (auto& intron : introns) {
outfile << '>' << intron.gene_id
<< ',' << intron.transcript_id
<< ',' << intron.start
<< '-' << intron.end << "\r\n";
// print the intron 60 characters at a time
std::size_t start = 0;
std::string tmp = "";
while (start < intron.full_sequence.size()) {
tmp = intron.full_sequence.substr(start, 60);
outfile << tmp << "\r\n";
start += 60;
}
}
outfile.close();
}
static void output_introns_gtf(std::vector<introns::Intron>& introns) {
std::time_t t = std::time(nullptr);
char mbstr[100];
std::strftime(mbstr, 100, "introns-%Y%m%d%H%M.gtf", std::localtime(&t));
GTFFile outfile(mbstr);
if (!outfile.open_for_writing(false)) {
throw std::runtime_error("Error opening file for writing: " + std::string(mbstr));
return;
}
std::cout << "-> Writing sequence annotations to " << mbstr << '\n';
std::size_t id = 0;
GTFSequence tmpseq;
for (auto& intron : introns) {
tmpseq = {
std::to_string(id++),
"intron-util",
"intron_CNS",
intron.start,
intron.end,
(DBL_EQ(intron.score_normalized, 0.0) ? '.' : intron.score_normalized),
intron.strand,
-1,
{
{ "gene_id", intron.gene_id },
{ "transcript_id", intron.transcript_id },
},
};
outfile << tmpseq;
}
outfile.close();
}