-
Notifications
You must be signed in to change notification settings - Fork 2
/
dna_char.c
547 lines (534 loc) · 14.3 KB
/
dna_char.c
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* dna_char.c
*
* Copyright 2019 Ryan Koehler, VerdAscend Sciences, ryan@verdascend.com
*
* The programs and source code of the vertools collection are free software.
* They are distributed in the hope that they will be useful,
* WITHOUT ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE.
*
* Permission is granted for research, educational, and possibly commercial use
* and modification as long as 1) Code and any derived works are not
* redistributed for any fee, and 2) Proper credit is given to the authors.
* If you wish to include this software in a product, or use it commercially,
* please contact the authors.
*
* See https://www.verdascend.com/ for more
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "prim.h"
#include "dna.h"
/***************************************************************************
* TRUE if in standard DNA alphabet
* BOGUS if degenerate DNA code
* FALSE otherwise
*/
int GoodDNABaseI(char c)
{
switch(c)
{
/***
* Standard ones = TRUE;
*/
case 'A': case 'a':
case 'C': case 'c':
case 'G': case 'g':
case 'T': case 't':
return(TRUE);
/***
* Non-standard degenerate ones = BOGUS;
*/
case 'N': case 'n':
case 'B': case 'b':
case 'D': case 'd':
case 'H': case 'h':
case 'V': case 'v':
case 'S': case 's':
case 'W': case 'w':
case 'Y': case 'y':
case 'K': case 'k':
case 'M': case 'm':
case 'R': case 'r':
return(BOGUS);
}
return(FALSE);
}
/***************************************************************************
* TRUE if c and s are complimentary bases in DNA alphabet
*/
int GoodDNACompBasesI(char c,char s)
{
int g;
g = FALSE;
switch(c)
{
case 'A': case 'a':
switch(s)
{
case 'T': case 't': g=TRUE;
}
break;
case 'C': case 'c':
switch(s)
{
case 'G': case 'g': g=TRUE;
}
break;
case 'G': case 'g':
switch(s)
{
case 'C': case 'c': g=TRUE;
}
break;
case 'T': case 't':
switch(s)
{
case 'A': case 'a': g=TRUE;
}
break;
}
return(g);
}
/***************************************************************************
* Returns integer index for DNA alphabet character; BOGUS if not in libarary
*/
int DNABaseIndexI(char seqC)
{
switch(seqC)
{
case 'A': case 'a': return(DNA_A);
case 'C': case 'c': return(DNA_C);
case 'G': case 'g': return(DNA_G);
case 'T': case 't': return(DNA_T);
case 'N': case 'n': return(DNA_N);
}
return(BOGUS);
}
/***************************************************************************
* Returns char in DNA alphabet for integer index; NULL if out of bounds
*/
char DNAIndexBaseC(int ind)
{
switch(ind)
{
case DNA_A: return('A');
case DNA_C: return('C');
case DNA_G: return('G');
case DNA_T: return('T');
case DNA_N: return('N');
}
return(0);
}
/***************************************************************************
* Returns the sequence pair index; i.e. alphabetic order
* If any unrecognized sequence, retruns BOGUS
*/
int SeqPairIndexI(char *seqS)
{
return(SeqBasePairIndexI(seqS[0],seqS[1]));
}
/***************************************************************************
* Returns the sequence pair index; i.e. alphabetic order
* If any unrecognized sequence, retruns BOGUS
*/
int SeqBasePairIndexI(char c1, char c2)
{
switch(c1)
{
case 'A': case 'a':
switch(c2)
{
case 'A': case 'a': return(0);
case 'C': case 'c': return(1);
case 'G': case 'g': return(2);
case 'T': case 't': return(3);
}
break;
case 'C': case 'c':
switch(c2)
{
case 'A': case 'a': return(4);
case 'C': case 'c': return(5);
case 'G': case 'g': return(6);
case 'T': case 't': return(7);
}
break;
case 'G': case 'g':
switch(c2)
{
case 'A': case 'a': return(8);
case 'C': case 'c': return(9);
case 'G': case 'g': return(10);
case 'T': case 't': return(11);
}
break;
case 'T': case 't':
switch(c2)
{
case 'A': case 'a': return(12);
case 'C': case 'c': return(13);
case 'G': case 'g': return(14);
case 'T': case 't': return(15);
}
break;
}
return(BOGUS);
}
/***************************************************************************
* Returns compliment for DNA alphabet; 0 if not in alphabet
*/
char CompDNABaseC(char c)
{
switch(c)
{
case 'A': return('T');
case 'a': return('t');
case 'C': return('G');
case 'c': return('g');
case 'G': return('C');
case 'g': return('c');
case 'T': return('A');
case 't': return('a');
/***
* Degen and brackets
*/
case 'N': return('N');
case 'n': return('n');
case '[': return(']');
case ']': return('[');
case 'S': return('S'); /* = [C/G] => [G/C] = S */
case 's': return('s');
case 'W': return('W'); /* = [A/T] => [T/A] = W */
case 'w': return('w');
case 'M': return('K'); /* = [A/C] => [T/G] = K */
case 'm': return('k');
case 'K': return('M'); /* = [G/T] => [C/A] = M */
case 'k': return('m');
case 'R': return('Y'); /* = [A/G] => [T/C] = Y */
case 'r': return('y');
case 'Y': return('R'); /* = [C/T] => [G/A] = R */
case 'y': return('r');
/***
* Triple degenerate
*/
case 'B': return('V'); /* = [C/G/T] => [G/C/A] = V */
case 'b': return('v');
case 'D': return('H'); /* = [A/G/T] => [T/C/A] = H */
case 'd': return('h');
case 'H': return('D'); /* = [T/C/A] => [A/G/T] = D */
case 'h': return('d');
case 'V': return('B'); /* = [A/C/G] => [T/G/C] = B */
case 'v': return('b');
default: return(c);
}
}
/***************************************************************************
* Returns complimentary DNA sequence 5'>-->3'
* compS may be the same string as seqS
*/
int CompDNASeqI(char *seqS,int len,char *compS)
{
int i;
char uC,dC;
for(i=0;i<(len/2);i++)
{
uC = seqS[i];
dC = seqS[len-i-1];
compS[i] = CompDNABaseC(dC);
compS[len-i-1] = CompDNABaseC(uC);
}
/***
* If odd length, compliment middle base
*/
if(len%2)
{
compS[len/2] = CompDNABaseC(seqS[len/2]);
}
return(i);
}
/***************************************************************************
* Returns inverse DNA sequence of the one passed (both assumed 5'>-->3')
*/
int InverseDNASeqI(char *seqS,int len,char *compS)
{
int i;
for(i=0;i<len;i++)
{
switch(seqS[i])
{
case '[':
case ']':
compS[i] = seqS[i]; break;
default:
compS[i] = CompDNABaseC(seqS[i]);
}
}
return(i);
}
/***************************************************************************
* Returns the reverse DNA seqeucne of the one passed (i.e. 3'>-->5')
* Lenght of seqS is len, compS should be at least as big
*/
int ReverseDNASeqI(char *seqS,int len,char *compS)
{
int i,j;
char fC,sC;
j = len-1;
for(i=0;i<((len+1)/2);i++)
{
fC = seqS[i];
sC = seqS[j];
switch(sC)
{
case '[': compS[i] = ']'; break;
case ']': compS[i] = '['; break;
default:
compS[i] = sC;
}
switch(fC)
{
case '[': compS[j--] = ']'; break;
case ']': compS[j--] = '['; break;
default:
compS[j--] = fC;
}
}
return(len);
}
/***************************************************************************
* Fills seqS[len] with a random sequence
* If fracsPI is non-null, there should be 4 numbers corresponding to
* percent (0-100) for ACGT (that should sum to 100)
*/
int RandomDNASeqI(char *seqS, int len, int *fracsPI)
{
int i,j,r;
static int set = FALSE;
static char baseperS[100];
/***
* Initialize the fraction array
*/
if(set != TRUE)
{
if(fracsPI)
{
r = 0;
for(i=0;i<4;i++)
{ r += fracsPI[i]; }
if( r != 100 )
{
printf("SHAM SNAPPED! r=%d\n",r);
ERR("RandomDNASeqI","?");
}
r = 0;
for(i=0;i<4;i++)
{
for(j=0;j<fracsPI[i];j++)
{ baseperS[r++] = DNAIndexBaseC(i); }
}
}
else
{
for(i=0;i<100;i++)
{
switch(i/25)
{
case 0: baseperS[i] = 'A'; break;
case 1: baseperS[i] = 'C'; break;
case 2: baseperS[i] = 'G'; break;
case 3: baseperS[i] = 'T'; break;
default:
printf("SHAM SNAPPED! i=%d i/25=%d\n",i,i/25);
ERR("RandomDNASeqI","?");
}
}
}
set = TRUE;
}
/*
for(i=0;i<100;i++)
{
printf("X %2d = %c\n",i,baseperS[i]);
}
*/
/**
* Now fill the sequence
*/
for(i=0;i<len;i++)
{
r = RandI(100);
seqS[i] = baseperS[r];
}
return(i);
}
/***************************************************************************
* Randomly tweaks sequence of len with tweak changes
*/
int RandTweakDNASeqI(char *seqS,int len,int tweak)
{
int i,r,b;
REAL fR,rR;
LIMIT_NUM(tweak,1,len);
for(i=0;i<len;i++)
{
fR = RNUM(tweak)/RNUM(len-i);
rR = RandR(1.0);
if(fR > rR)
{
b = DNABaseIndexI(seqS[i]);
r = RandI(4);
while(r==b)
{
r = RandI(4);
}
seqS[i] = DNAIndexBaseC(r);
tweak--;
}
if(tweak==0)
break;
}
return(i);
}
/****************************************************************************
* Count the number of non-standard bases in the passed string
*/
int CountNonStandBasesI(char *seqS,int len)
{
int i,n;
n = 0;
for(i=0;i<len;i++)
{
switch(seqS[i])
{
case 'a':
case 'A':
case 'c':
case 'C':
case 'g':
case 'G':
case 't':
case 'T':
break;
default:
if(isgraph(INT(seqS[i])))
n++;
}
}
return(n);
}
/****************************************************************************
* Extract a subsequence from seq and put it into subS
*/
int ExtractForOrRevSubSeqI(char *seqS,int slen,int st,int en,int dir,char *subS)
{
int i,n;
char c;
n = en-st;
LIMIT_NUM(n,0,slen);
for(i=0;i<n;i++)
{
if(dir==REVERSE)
c = CompDNABaseC(seqS[en-i-1]);
else
c = seqS[st+i];
subS[i] = c;
}
subS[n]='\0';
return(n);
}
/***********************************************************************
* Reduce sequence to only bases "fir" to "las"
* If rre is true, reference relative the end rather than start (backwards)
*/
int GetReducedSeqI(char *seqS,int len,int fir,int las,int rre,char *newS)
{
int i,j,start,end;
if(rre) {
start=len-las;
end=len-fir+1;
}
else {
start=fir-1;
end=las;
}
LIMIT_NUM(start,0,len);
LIMIT_NUM(end,0,len);
i = start;
j = 0;
while(i<end)
{
newS[j++] = seqS[i++];
}
newS[j] = '\0';
return(j);
}
/**************************************************************************
* Converts any non ACGT to N and all to uppercase
* If ols is TRUE, one-letter-SNP codes are used for SNPs
* If mlc is TRUE, mask-lower-case i.e. set lowercase to N's
*/
int CleanUpSeqI(char *inS, int slen, char *outS, int ols, int mlc)
{
int i,j,s,nn,mn;
char snpS[DEF_BS+1];
i = j = 0;
while(i<slen)
{
/***
* Masking lowercase?
*/
if( (mlc) && (islower(INT(inS[i]))) ) {
inS[i] = 'N';
}
switch(inS[i])
{
case 'a': case 'A':
case 'c': case 'C':
case 'g': case 'G':
case 't': case 'T':
outS[j++] = TOUPPER(inS[i++]);
break;
case '[':
s = mn = nn = 0;
while( (inS[i] != ']') && (i<slen) && (s<DEF_BS) )
{
snpS[s++] = inS[i++];
}
snpS[s] = '\0';
s = CollapseSnpStringI(snpS,snpS);
i++;
/***
* If simple single-base, two-allele SNP and one-letter
*/
if( (ols) && (strlen(snpS)==1) ) {
outS[j++] = snpS[0];
}
else {
while(mn>0)
{
outS[j++] = 'N';
mn--;
}
}
break;
case '/':
case '*':
i++;
break;
default:
if(isalpha(INT(inS[i]))) {
outS[j++] = 'N';
}
else {
outS[j++] = '?';
}
i++;
}
}
outS[j] = '\0';
return(j);
}