-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBriggs.cpp
527 lines (476 loc) · 18.2 KB
/
Briggs.cpp
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include "Briggs.h"
#include "mrand.h"
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <htslib/kstring.h>
#include <zlib.h>
#include <iostream>
int Biotin_ds_454Roche(char seq[], int L, double nv, double lambda, double delta_s, double delta_d, mrand_t *mr,int strand,int& C_to_T_counter,int& G_to_A_counter,int& C_to_T_counter_rev,int& G_to_A_counter_rev){
/*
Biotin_ds_454Roche - Simulates deamination events in a DNA sequence based on given parameters and returns binary value whether any deamination occurred (1 occurred, 0 unaltered).
@param seq: A character array representing the DNA sequence that will be modified by deamination. Alters sequence in place.
@param L: An integer representing the length of the DNA sequence.
@param nv: probability of introducing nicks in the DNA sequence.
@param lambda: A double parameter used in the geometric distribution to determine the length of overhangs.
@param delta_s: A double representing the probability threshold for a cytosine to be deaminated to uracil in the single-stranded regions (left overhang region).
@param delta_d: A double representing the probability threshold for a cytosine to be deaminated to uracil or a guanine to adenine in the middle region of the sequence.
@param mr: A pointer to an mrand_t struct used for generating random numbers.
@param strand: An integer representing the DNA strand orientation (0 for forward, 1 for reverse).
@param C_to_T_counter: integer tracking the number of C to T deamination events in the forward strand.
@param G_to_A_counter: integer tracking the number of G to A deamination events in the forward strand.
@param C_to_T_counter_rev: integer tracking the number of C to T deamination events in the reverse strand.
@param G_to_A_counter_rev: integer tracking the number of G to A deamination events in the reverse strand.
*/
double dtemp1;double dtemp2;
dtemp1 = mrand_pop(mr);
dtemp2 = mrand_pop(mr);
int IsDeam = 0;
//lengths of overhangs
assert(L<1024);
int left_overhang = 0;
int right_overhang = L-1;
char seq_intermediate[1024];
//Clear out intermediate sequence
memset(seq_intermediate, 0, sizeof seq_intermediate);
strcpy(seq_intermediate,seq);
// Determine left and right overhang lengths
while (left_overhang+right_overhang > L-2){
left_overhang = 0;
right_overhang = 0;
double uniform_prob_leftoverhang = dtemp1; // between 0 and 1
double uniform_prob_rightoverhang = dtemp2; // between 0 and 1
dtemp1 = mrand_pop(mr);
dtemp2 = mrand_pop(mr);
if (uniform_prob_leftoverhang > 0.5){
left_overhang = (int) Random_geometric_k(lambda,mr);
}
if (uniform_prob_rightoverhang > 0.5){
right_overhang = (int) Random_geometric_k(lambda,mr);
}
}
for (int i = 0; i<left_overhang; i++){
// left_overhang means left overhang (ss)
if (seq_intermediate[i] == 'C' || seq_intermediate[i] == 'c' ){
dtemp1 = mrand_pop(mr);
double uniformprob_ss_left = dtemp1;
if (uniformprob_ss_left < delta_s){
// performs the deamination
IsDeam = 1;
seq[i] = 'T';
if (i == 0&& strand == 0){C_to_T_counter++;}
else if(i == 0 && strand==1){C_to_T_counter_rev++;}
}
else{
// else copy the original sequence and continue
seq[i] = seq_intermediate[i];
}
}
else{
// else copy the original sequence and continue
seq[i] = seq_intermediate[i];
}
}
for (int i = 0; i < right_overhang; i++){
// 1 pos 3' (so last position in fragment)
// r means right overhang (ss)
if (seq_intermediate[L-i-1] == 'G' || seq_intermediate[L-i-1] == 'g'){
dtemp2 = mrand_pop(mr);
double uniformprob_ss_right = dtemp2;
if (uniformprob_ss_right < delta_s){
// performs the deamination from the 3' direction
IsDeam = 1;
seq[L-i-1] = 'A';
if (i == 0&& strand == 0){G_to_A_counter++;}
else if(i == 0 && strand==1){G_to_A_counter_rev++;}
}
else{
// else copy the end of original sequence and continue
seq[L-i-1] = seq_intermediate[L-i-1];
}
}
else{
// else copy the end of original sequence and continue
seq[L-i-1] = seq_intermediate[L-i-1];
}
}
dtemp1 = mrand_pop(mr);
double uniformprob_nick = dtemp1;
double prob_place_nick = nv/((L-left_overhang-right_overhang-1)*nv+1-nv); // P(m) probability of placing nick in the middle of the fragments
int pos_nick = left_overhang; //m
double cumd = prob_place_nick; // P(m)
while ((uniformprob_nick > cumd) && (pos_nick < L-right_overhang-1)){
// pos_nick cannot be larger than L-r-1, so once pos_nick is equal then we
// go out of this loop. So at that time p_nick = L-r-1. So positions within
// the full fragment
cumd += prob_place_nick;
pos_nick +=1;
}
for (int i = left_overhang; i < L-right_overhang; i++){
// The double strand part, the left and right hand overhang are probably cut, so only the midlle part of our DNA fragments (ds)
if ((seq_intermediate[i] == 'C' || seq_intermediate[i] == 'c') && i<=pos_nick){
dtemp1 = mrand_pop(mr);
double uniformprob_ds_left = dtemp1;
if (uniformprob_ds_left < delta_d){
// perform deamination
IsDeam = 1;
seq[i] = 'T';
if (i == 0 && strand == 0){
C_to_T_counter++;
}
else if(i == 0 && strand==1){
C_to_T_counter_rev++;
}
}
else{
// no alteration
seq[i] = seq_intermediate[i];
}
}
else if ((seq_intermediate[i] == 'G' || seq_intermediate[i] == 'g') && i>pos_nick){
dtemp2 = mrand_pop(mr);
double uniformprob_ds_right = dtemp2;
if (uniformprob_ds_right < delta_d){
// perform deamination
IsDeam = 1;
seq[i] = 'A';
if (i == 0){
if (i == 0 && strand==0){
G_to_A_counter++;
}
else if(i == 0 && strand==1){
G_to_A_counter_rev++;
}
}
}
else{
// no alteration
seq[i] = seq_intermediate[i];
}
}
else{
// no alteration
seq[i] = seq_intermediate[i];
}
}
// count number of deamination events
if (seq_intermediate[0] == 'C' && seq[0] == 'T'){
if (strand == 0){
C_to_T_counter++;
}
else if(strand==1){
C_to_T_counter_rev++;
}
}
if (seq_intermediate[L-1] == 'G' && seq[L-1] == 'A'){
if (strand == 0){
G_to_A_counter++;
}
else if(strand==1){
G_to_A_counter_rev++;
}
}
return IsDeam;
}
int Biotin_ds_454Roche_kstring(kstring_t* seq, double nv, double lambda, double delta_s, double delta_d, mrand_t* mr, int strand, int& C_to_T_counter, int& G_to_A_counter, int& C_to_T_counter_rev, int& G_to_A_counter_rev) {
/*
Biotin_ds_454Roche - Simulates deamination events in a DNA sequence based on given parameters and returns binary value whether any deamination occurred (1 occurred, 0 unaltered).
@param seq: A kstring representing the DNA sequence that will be modified by deamination.
@param nv: probability of introducing nicks in the DNA sequence.
@param lambda: A double parameter used in the geometric distribution to determine the length of overhangs.
@param delta_s: A double representing the probability threshold for a cytosine to be deaminated to uracil in the single-stranded regions (left overhang region).
@param delta_d: A double representing the probability threshold for a cytosine to be deaminated to uracil or a guanine to adenine in the middle region of the sequence.
@param mr: A pointer to an mrand_t struct used for generating random numbers.
@param strand: An integer representing the DNA strand orientation (0 for forward, 1 for reverse).
@param C_to_T_counter: integer tracking the number of C to T deamination events in the forward strand.
@param G_to_A_counter: integer tracking the number of G to A deamination events in the forward strand.
@param C_to_T_counter_rev: integer tracking the number of C to T deamination events in the reverse strand.
@param G_to_A_counter_rev: integer tracking the number of G to A deamination events in the reverse strand.
*/
int IsDeam = 0;
double dtemp1, dtemp2;
dtemp1 = mrand_pop(mr);
dtemp2 = mrand_pop(mr);
int left_overhang = 0;
int right_overhang = seq->l - 1;
// Determine left and right overhang lengths
while (left_overhang + right_overhang > seq->l - 2) {
left_overhang = 0;
right_overhang = 0;
double uniform_prob_leftoverhang = dtemp1; // between 0 and 1
double uniform_prob_rightoverhang = dtemp2; // between 0 and 1
dtemp1 = mrand_pop(mr);
dtemp2 = mrand_pop(mr);
if (uniform_prob_leftoverhang > 0.5) {
left_overhang = (int)Random_geometric_k(lambda, mr);
}
if (uniform_prob_rightoverhang > 0.5) {
right_overhang = (int)Random_geometric_k(lambda, mr);
}
}
// initialize intermediate kstrings to be altered
kstring_t seq_intermediate;
seq_intermediate.l = seq->l;
seq_intermediate.m = seq->l;
seq_intermediate.s = (char *)malloc((seq->l + 1) * sizeof(char)); // Allocate memory
strcpy(seq_intermediate.s, seq->s); // Copy seq->s to seq_intermediate.s
for (int i = 0; i < left_overhang; i++) {
if (seq_intermediate.s[i] == 'C' || seq_intermediate.s[i] == 'c') {
dtemp1 = mrand_pop(mr);
double uniformprob_ss_left = dtemp1;
if (uniformprob_ss_left < delta_s) {
// performs the deamination
IsDeam = 1;
seq->s[i] = 'T'; //X
if (i == 0 && strand == 0) {
C_to_T_counter++;
}
else if (i == 0 && strand == 1){
C_to_T_counter_rev++;
}
}
else {
// else copy the original sequence and continue
seq->s[i] = seq_intermediate.s[i];
}
}
else {
// else copy the original sequence and continue
seq->s[i] = seq_intermediate.s[i];
}
}
for (int i = 0; i < right_overhang; i++) {
if (seq_intermediate.s[seq->l - i - 1] == 'G' || seq_intermediate.s[seq->l - i - 1] == 'g') {
dtemp2 = mrand_pop(mr);
double uniformprob_ss_right = dtemp2;
if (uniformprob_ss_right < delta_s) {
// performs the deamination from the 3' direction
IsDeam = 1;
seq->s[seq->l-i-1] = 'A'; //Q
if (i == 0 && strand == 0) {
G_to_A_counter++;
}
else if (i == 0 && strand == 1) {
G_to_A_counter_rev++;
}
}
else{
// else copy the end of original sequence and continue
seq->s[seq->l-i-1] = seq_intermediate.s[seq->l-i-1];
}
}
else {
// else copy the end of original sequence and continue
seq->s[seq->l-i-1] = seq_intermediate.s[seq->l-i-1];
}
}
dtemp1 = mrand_pop(mr);
double uniformprob_nick = dtemp1;
double prob_place_nick = nv/((seq->l-left_overhang-right_overhang-1)*nv+1-nv); // P(m) probability of placing nick in the middle of the fragments
int pos_nick = left_overhang; //m
double cumd = prob_place_nick; // P(m)
while ((uniformprob_nick > cumd) && (pos_nick < seq->l-right_overhang-1)){
// pos_nick cannot be larger than L-r-1, so once pos_nick is equal then we
// go out of this loop. So at that time p_nick = L-r-1. So positions within
// the full fragment
cumd += prob_place_nick;
pos_nick +=1;
}
for (int i = left_overhang; i < seq->l-right_overhang; i++){
// The double strand part, the left and right hand overhang are probably cut, so only the midlle part of our DNA fragments (ds)
if ((seq_intermediate.s[i] == 'C' || seq_intermediate.s[i] == 'c') && i<=pos_nick){
dtemp1 = mrand_pop(mr);
double uniformprob_ds_left = dtemp1;
if (uniformprob_ds_left < delta_d){
// perform deamination
IsDeam = 1;
seq->s[i] = 'T';
if (i == 0 && strand == 0){
C_to_T_counter++;
}
else if(i == 0 && strand==1){
C_to_T_counter_rev++;
}
}
else{
// no alteration
seq->s[i] = seq_intermediate.s[i];
}
}
else if ((seq_intermediate.s[i] == 'G' || seq_intermediate.s[i] == 'g') && i>pos_nick){
dtemp2 = mrand_pop(mr);
double uniformprob_ds_right = dtemp2;
if (uniformprob_ds_right < delta_d){
// perform deamination
IsDeam = 1;
seq->s[i] = 'A';
if (i == 0){
if (i == 0 && strand==0){
G_to_A_counter++;
}
else if(i == 0 && strand==1){
G_to_A_counter_rev++;
}
}
}
else{
// no alteration
seq->s[i] = seq_intermediate.s[i];
}
}
else{
// no alteration
seq->s[i] = seq_intermediate.s[i];
}
}
// count number of deamination events
if (seq_intermediate.s[0] == 'C' && seq->s[0] == 'T'){
if (strand == 0){
C_to_T_counter++;
}
else if(strand==1){
C_to_T_counter_rev++;
}
}
if (seq_intermediate.s[seq->l-1] == 'G' && seq->s[seq->l-1] == 'A'){
if (strand == 0){
G_to_A_counter++;
}
else if(strand==1){
G_to_A_counter_rev++;
}
}
free(seq_intermediate.s);
seq_intermediate.s = NULL;
seq_intermediate.l = seq_intermediate.m = 0;
return IsDeam;
}
int PMD_Amplicon(kstring_t* seq, double nv, double lambda, double delta_s, double delta_d, mrand_t* mr){
/*
PMD_Amplicon - Simulates deamination events in a empirical DNA sequence based on given parameters and returns binary value whether any deamination occurred (1 occurred, 0 unaltered).
@param seq: A kstring representing the sequence read from an input fasta,fastq,bam file that will be modified by deamination.
@param nv: probability of introducing nicks in the DNA sequence.
@param lambda: A double parameter used in the geometric distribution to determine the length of overhangs.
@param delta_s: A double representing the probability threshold for a cytosine to be deaminated to uracil in the single-stranded regions (left overhang region).
@param delta_d: A double representing the probability threshold for a cytosine to be deaminated to uracil or a guanine to adenine in the middle region of the sequence.
@param mr: A pointer to an mrand_t struct used for generating random numbers.
*/
/*
In order to insert deamination, we assume the sequence reads fully span the original deamination molecule, such that we can model what would have been the single-stranded and
and double-stranded regions with the sequence read being created after blunt-end repair.
*/
int IsDeam = 0;
double dtemp1, dtemp2;
dtemp1 = mrand_pop(mr);
dtemp2 = mrand_pop(mr);
int left_overhang = 0;
int right_overhang = seq->l - 1;
while (left_overhang + right_overhang > seq->l - 2) {
left_overhang = 0;
right_overhang = 0;
double uniform_prob_leftoverhang = dtemp1; // between 0 and 1
double uniform_prob_rightoverhang = dtemp2; // between 0 and 1
dtemp1 = mrand_pop(mr);
dtemp2 = mrand_pop(mr);
if (uniform_prob_leftoverhang > 0.5) {
left_overhang = (int)Random_geometric_k(lambda, mr);
}
if (uniform_prob_rightoverhang > 0.5) {
right_overhang = (int)Random_geometric_k(lambda, mr);
}
}
kstring_t seq_intermediate;
seq_intermediate.l = seq->l;
seq_intermediate.m = seq->l;
seq_intermediate.s = (char *)malloc((seq->l + 1) * sizeof(char)); // Allocate memory
strcpy(seq_intermediate.s, seq->s); // Copy seq->s to seq_intermediate.s
for (int i = 0; i < left_overhang; i++) {
if (seq_intermediate.s[i] == 'C' || seq_intermediate.s[i] == 'c') {
dtemp1 = mrand_pop(mr);
double uniformprob_ss_left = dtemp1;
if (uniformprob_ss_left < delta_s) {
// performs the deamination
IsDeam = 1;
seq->s[i] = 'T'; //X
}
else {
// else copy the original sequence and continue
seq->s[i] = seq_intermediate.s[i];
}
}
else {
// else copy the original sequence and continue
seq->s[i] = seq_intermediate.s[i];
}
}
for (int i = 0; i < right_overhang; i++) {
if (seq_intermediate.s[seq->l - i - 1] == 'G' || seq_intermediate.s[seq->l - i - 1] == 'g') {
dtemp2 = mrand_pop(mr);
double uniformprob_ss_right = dtemp2;
if (uniformprob_ss_right < delta_s) {
// performs the deamination from the 3' direction
IsDeam = 1;
seq->s[seq->l-i-1] = 'A'; //Q
}
else {
// else copy the end of original sequence and continue
seq->s[seq->l-i-1] = seq_intermediate.s[seq->l-i-1];
}
}
else {
// else copy the end of original sequence and continue
seq->s[seq->l-i-1] = seq_intermediate.s[seq->l-i-1];
}
}
dtemp1 = mrand_pop(mr);
double uniformprob_nick = dtemp1;
double prob_place_nick = nv/((seq->l-left_overhang-right_overhang-1)*nv+1-nv); // P(m) probability of placing nick in the middle of the fragments
int pos_nick = left_overhang; //m
double cumd = prob_place_nick; // P(m)
while ((uniformprob_nick > cumd) && (pos_nick < seq->l-right_overhang-1)){
// pos_nick cannot be larger than L-r-1, so once pos_nick is equal then we
// go out of this loop. So at that time p_nick = L-r-1. So positions within
// the full fragment
cumd += prob_place_nick;
pos_nick +=1;
}
for (int i = left_overhang; i < seq->l-right_overhang; i++){
// The double strand part, the left and right hand overhang are probably cut, so only the midlle part of our DNA fragments (ds)
if ((seq_intermediate.s[i] == 'C' || seq_intermediate.s[i] == 'c') && i<=pos_nick){
dtemp1 = mrand_pop(mr);
double uniformprob_ds_left = dtemp1;
if (uniformprob_ds_left < delta_d){
// perform deamination
IsDeam = 1;
seq->s[i] = 'T';
}
else{
// no alteration
seq->s[i] = seq_intermediate.s[i];
}
}
else if ((seq_intermediate.s[i] == 'G' || seq_intermediate.s[i] == 'g') && i>pos_nick){
dtemp2 = mrand_pop(mr);
double uniformprob_ds_right = dtemp2;
if (uniformprob_ds_right < delta_d){
// perform deamination
IsDeam = 1;
seq->s[i] = 'A';
}
else{
// no alteration
seq->s[i] = seq_intermediate.s[i];
}
}
else{
// no alteration
seq->s[i] = seq_intermediate.s[i];
}
}
// Cleanup
free(seq_intermediate.s);
seq_intermediate.s = NULL;
seq_intermediate.l = seq_intermediate.m = 0;
return IsDeam;
}