-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdna_pp.c
580 lines (568 loc) · 17.6 KB
/
dna_pp.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
* dna_pp.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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "prim.h"
#include "dna.h"
#include "dna_pair.h"
#define DB_PP if(DB[115])
/************************************************************************ ppp
* Create PPARS data structure
*/
PPARS *CreatePparsPO()
{
PPARS *pparPO;
if(!(pparPO = (PPARS *)ALLOC(1,sizeof(PPARS))))
{
return(NULL);
}
pparPO->ID = PPARS_ID;
InitPpars(pparPO);
return(pparPO);
}
/************************************************************************
* Frees up PPARS data structure
*/
int DestroyPparsI(PPARS *pparPO)
{
VALIDATE(pparPO,PPARS_ID);
FREE(pparPO);
return(TRUE);
}
/***********************************************************************
* Initialize PPARS data structure vars
*/
void InitPpars(PPARS *pparPO)
{
VALIDATE(pparPO,PPARS_ID);
INIT_S(pparPO->parfile);
pparPO->alg = DEF_ALGO;
pparPO->ctype = DEF_CTYPE;
pparPO->rval = DEF_RVAL;
pparPO->min_word = DEF_MIN_WORD;
pparPO->min_loop = DEF_MIN_LOOP;
pparPO->cl3 = DEF_CLAMP3;
pparPO->cl5 = DEF_CLAMP5;
pparPO->thresh = DEF_C_THRESH;
INIT_S(pparPO->msfile);
SetPair_parIdentMatch(pparPO);
pparPO->do_ham = FALSE;
return;
}
/***********************************************************************
* Set pair-wise match weights for identity check
*/
void SetPair_parIdentMatch(PPARS *pparPO)
{
VALIDATE(pparPO,PPARS_ID);
pparPO->mscore[0] = 1.0; /* AA */
pparPO->mscore[1] = 0.0; /* AC */
pparPO->mscore[2] = 0.0; /* AG */
pparPO->mscore[3] = 0.0; /* AT */
pparPO->mscore[4] = 0.0; /* CA */
pparPO->mscore[5] = 1.0; /* CC */
pparPO->mscore[6] = 0.0; /* CG */
pparPO->mscore[7] = 0.0; /* CT */
pparPO->mscore[8] = 0.0; /* GA */
pparPO->mscore[9] = 0.0; /* GC */
pparPO->mscore[10] = 1.0; /* GG */
pparPO->mscore[11] = 0.0; /* GT */
pparPO->mscore[12] = 0.0; /* TA */
pparPO->mscore[13] = 0.0; /* TC */
pparPO->mscore[14] = 0.0; /* TG */
pparPO->mscore[15] = 1.0; /* TT */
pparPO->parset = PP_PSET_ID;
SetPair_parMaxScores(pparPO);
}
/***********************************************************************
* Set pair-wise match / mismatch weights for default complement match
*/
void SetPair_parCompMatch(PPARS *pparPO)
{
VALIDATE(pparPO,PPARS_ID);
pparPO->mscore[0] = 0.0; /* AA */
pparPO->mscore[1] = 0.0; /* AC */
pparPO->mscore[2] = 0.0; /* AG */
pparPO->mscore[3] = 1.0; /* AT */
pparPO->mscore[4] = 0.0; /* CA */
pparPO->mscore[5] = 0.0; /* CC */
pparPO->mscore[6] = 1.0; /* CG */
pparPO->mscore[7] = 0.0; /* CT */
pparPO->mscore[8] = 0.0; /* GA */
pparPO->mscore[9] = 1.0; /* GC */
pparPO->mscore[10] = 0.0; /* GG */
pparPO->mscore[11] = 0.0; /* GT */
pparPO->mscore[12] = 1.0; /* TA */
pparPO->mscore[13] = 0.0; /* TC */
pparPO->mscore[14] = 0.0; /* TG */
pparPO->mscore[15] = 0.0; /* TT */
pparPO->parset = PP_PSET_COM;
SetPair_parMaxScores(pparPO);
}
/***********************************************************************
* Set Thermodynamic pair-wise match / mismatch weights
* 1/20/03: Values are -dG from Nic Peyret's "averaging" of missmatchs
*/
void SetPair_parThermoWmatch(PPARS *pparPO)
{
VALIDATE(pparPO,PPARS_ID);
pparPO->mscore[0] = -0.5; /* AA */
pparPO->mscore[1] = -0.85; /* AC */
pparPO->mscore[2] = -0.1; /* AG */
pparPO->mscore[3] = 1.1; /* AT */
pparPO->mscore[4] = -0.85; /* CA */
pparPO->mscore[5] = -0.95; /* CC */
pparPO->mscore[6] = 1.7; /* CG */
pparPO->mscore[7] = -0.7; /* CT */
pparPO->mscore[8] = -0.1; /* GA */
pparPO->mscore[9] = 1.7; /* GC */
pparPO->mscore[10] = -0.15; /* GG */
pparPO->mscore[11] = -0.05; /* GT */
pparPO->mscore[12] = 1.1; /* TA */
pparPO->mscore[13] = -0.7; /* TC */
pparPO->mscore[14] = -0.05; /* TG */
pparPO->mscore[15] = -0.45; /* TT */
pparPO->parset = PP_PSET_TH;
SetPair_parMaxScores(pparPO);
}
/***********************************************************************
* Set GC=3 AT=2 pair-wise match weights
*/
void SetPair_parS3W2Wmatch(PPARS *pparPO)
{
VALIDATE(pparPO,PPARS_ID);
pparPO->mscore[0] = 0.0; /* AA */
pparPO->mscore[1] = 0.0; /* AC */
pparPO->mscore[2] = 0.0; /* AG */
pparPO->mscore[3] = 2.0; /* AT */
pparPO->mscore[4] = 0.0; /* CA */
pparPO->mscore[5] = 0.0; /* CC */
pparPO->mscore[6] = 3.0; /* CG */
pparPO->mscore[7] = 0.0; /* CT */
pparPO->mscore[8] = 0.0; /* GA */
pparPO->mscore[9] = 3.0; /* GC */
pparPO->mscore[10] = 0.0; /* GG */
pparPO->mscore[11] = 0.0; /* GT */
pparPO->mscore[12] = 2.0; /* TA */
pparPO->mscore[13] = 0.0; /* TC */
pparPO->mscore[14] = 0.0; /* TG */
pparPO->mscore[15] = 0.0; /* TT */
pparPO->parset = PP_PSET_TH;
SetPair_parMaxScores(pparPO);
}
/***************************************************************************
* Return match score for pair of chars
*/
int GetPair_parMatchScoreI(PPARS *pparPO, char f, char s, REAL *scPR)
{
int pind;
VALIDATE(pparPO,PPARS_ID);
pind = SeqBasePairIndexI(f,s);
if(IS_BOG(pind))
{
return(FALSE);
}
if(scPR)
{
*scPR = pparPO->mscore[pind];
}
return(TRUE);
}
/***************************************************************************
* Load pair-wise pairing parameters
* Returns the TRUE if all parameters are loaded; FALSE if problem
*/
int LoadPairingParsI(char *inS,PPARS *pparPO)
{
int i,prob;
char bufS[DEF_BS],pairS[DEF_BS];
FILE *fPF;
REAL pR, pvalsR[16];
DB_PP DB_PrI(">> LoadPairingParsI |%s|\n",inS);
if(!(fPF=OpenUFilePF(inS,"r",NULL)))
{
DB_PP DB_PrI("<< LoadPairingParsI FALSE\n");
return(FALSE);
}
/***
* Init parameters to bogus values
*/
for(i=0;i<PP_IND_NUM;i++)
{
pvalsR[i] = BAD_R;
}
/***
* Load data values with assumed format XX Pairing
* where XX is the sequence pair & XX is a pairing score value
*/
DB_PP DB_PrI("+ Loading data values\n");
prob = 0;
while(fgets(bufS,LINEGRAB,fPF) != NULL)
{
if(COM_LINE(bufS))
{ continue; }
pR = BAD_R;
INIT_S(pairS);
sscanf(bufS,"%s %lf",pairS,&pR);
if(NO_S(pairS))
{ continue; }
Upperize(pairS);
/***
* Either code-pair keys or specific init keywords
*/
i = SeqPairIndexI(pairS);
if(BAD_REAL(pR) || IS_BOG(i))
{
PROBLINE;
printf("Bad / unrecognized data line:\n");
fputs(bufS,stdout);
prob++; break;
}
pvalsR[i] = pR;
}
FILECLOSE(fPF);
if(prob)
{
return(FALSE);
}
/***
* Check all parameters and complain if any missed
*/
for(i=0;i<PP_IND_NUM;i++)
{
if( BAD_REAL(pvalsR[i]) )
{
PROBLINE;
pairS[0] = DNAIndexBaseC(i/4);
pairS[1] = DNAIndexBaseC(i%4);
pairS[2] = '\0';
printf("Failed to get score value for pairing %s\n",pairS);
DB_PP DB_PrI("<< LoadPairingParsI FALSE\n");
return(FALSE);
}
}
/***
* Finally copy into data structure
*/
for(i=0;i<PP_IND_NUM;i++)
{
pparPO->mscore[i] = pvalsR[i];
}
strcpy(pparPO->parfile,inS);
SetPair_parMaxScores(pparPO);
DB_PP DB_PrI("<< LoadPairingParsI TRUE\n");
return(TRUE);
}
/***********************************************************************
* Set max values of score for each base type
*/
void SetPair_parMaxScores(PPARS *pparPO)
{
VALIDATE(pparPO,PPARS_ID);
/* A-X */
pparPO->mA = MAX_NUM(pparPO->mscore[0],pparPO->mscore[1]);
pparPO->mA = MAX_NUM(pparPO->mscore[2],pparPO->mA);
pparPO->mA = MAX_NUM(pparPO->mscore[3],pparPO->mA);
/* C-X */
pparPO->mC = MAX_NUM(pparPO->mscore[4],pparPO->mscore[5]);
pparPO->mC = MAX_NUM(pparPO->mscore[6],pparPO->mC);
pparPO->mC = MAX_NUM(pparPO->mscore[7],pparPO->mC);
/* G-X */
pparPO->mG = MAX_NUM(pparPO->mscore[8],pparPO->mscore[9]);
pparPO->mG = MAX_NUM(pparPO->mscore[10],pparPO->mG);
pparPO->mG = MAX_NUM(pparPO->mscore[11],pparPO->mG);
/* T-X */
pparPO->mT = MAX_NUM(pparPO->mscore[12],pparPO->mscore[13]);
pparPO->mT = MAX_NUM(pparPO->mscore[14],pparPO->mT);
pparPO->mT = MAX_NUM(pparPO->mscore[15],pparPO->mT);
}
/****************************************************************************
* Set pair-wise comparision algorithm
*/
int SetPparsAlgI(PPARS *pparPO,int alg)
{
VALIDATE(pparPO,PPARS_ID);
switch(alg)
{
case ALGO_BMATCH:
case ALGO_CONT:
case ALGO_CON3:
case ALGO_CON5:
case ALGO_MATCH:
pparPO->alg = alg;
return(TRUE);
}
return(FALSE);
}
/**************************************************************************/
int GetPparsAlgI(PPARS *pparPO)
{
VALIDATE(pparPO,PPARS_ID);
return(pparPO->alg);
}
/***************************************************************************/
void FillPparsAlgTagString(int alg,char *bufS)
{
switch(alg)
{
case ALGO_BMATCH: sprintf(bufS,"SCB"); break;
case ALGO_CONT: sprintf(bufS,"SCON"); break;
case ALGO_MATCH: sprintf(bufS,"SMAT"); break;
case ALGO_CON3: sprintf(bufS,"SCO3"); break;
case ALGO_CON5: sprintf(bufS,"SCO5"); break;
default: sprintf(bufS,">>>BOGUS<<<"); break;
}
}
/**************************************************************************/
void FillPparsAlgString(int alg,char *bufS)
{
switch(alg)
{
case ALGO_MATCH:
sprintf(bufS,"Max matching"); break;
case ALGO_BMATCH:
sprintf(bufS,"Block-weighted matching"); break;
case ALGO_CONT:
sprintf(bufS,"Max contiguous stretch"); break;
case ALGO_CON3:
sprintf(bufS,"Max contiguous stretch @ 3' end"); break;
case ALGO_CON5:
sprintf(bufS,"Max contiguous stretch @ 5' end"); break;
default:
sprintf(bufS,"??? DEFAULT ???"); break;
}
}
/**************************************************************************
* Set return-value type
*/
int SetPparsRvalI(PPARS *pparPO,int type)
{
VALIDATE(pparPO,PPARS_ID);
switch(type)
{
case PP_R_MAX:
case PP_R_TOT:
case PP_R_NUM:
case PP_R_EFR:
pparPO->rval = type;
return(TRUE);
}
return(FALSE);
}
/**************************************************************************/
int GetPparsRvalI(PPARS *pparPO)
{
VALIDATE(pparPO,PPARS_ID);
return(pparPO->rval);
}
/**************************************************************************/
void FillPparsRvalString(int rval,char *bufS)
{
switch(rval)
{
case PP_R_MAX: sprintf(bufS,"Maximum value"); break;
case PP_R_TOT: sprintf(bufS,"Total value (at least thresh)"); break;
case PP_R_NUM: sprintf(bufS,"Alignment count (at least thresh)");break;
case PP_R_EFR: sprintf(bufS,"Expect/Find"); break;
default: sprintf(bufS,">>>BOGUS<<<"); break;
}
}
/**************************************************************************
* Set comparision type
*/
int SetPparsCtypeI(PPARS *pparPO,int type)
{
VALIDATE(pparPO,PPARS_ID);
switch(type)
{
case PP_COM_SIM:
case PP_COM_COM:
case PP_COM_LOOP:
case PP_COM_PCOM:
pparPO->ctype = type;
return(TRUE);
}
return(FALSE);
}
/**************************************************************************/
int GetPparsCtypeI(PPARS *pparPO)
{
VALIDATE(pparPO,PPARS_ID);
return(pparPO->ctype);
}
/**************************************************************************/
void FillPparsCtypeString(int ctype,char *bufS)
{
switch(ctype)
{
case PP_COM_SIM: sprintf(bufS,"Similarity"); break;
case PP_COM_COM: sprintf(bufS,"Complimentarity"); break;
case PP_COM_LOOP: sprintf(bufS,"Hairpin Loop"); break;
case PP_COM_PCOM: sprintf(bufS,"Parallel Comp"); break;
default: sprintf(bufS,">>>BOGUS<<<"); break;
}
}
/*************************************************************************/
int SetPparsThreshI(PPARS *pparPO,REAL tR)
{
VALIDATE(pparPO,PPARS_ID);
pparPO->thresh = tR;
return(TRUE);
}
/*************************************************************************/
REAL GetPparsThreshR(PPARS *pparPO)
{
VALIDATE(pparPO,PPARS_ID);
return(pparPO->thresh);
}
/*************************************************************************/
REAL MaxSeqCompareScoreR(PPARS *pparPO,char *seqPC,int len)
{
int i;
REAL scR;
VALIDATE(pparPO,PPARS_ID);
scR = 0.0;
for(i=0;i<len;i++)
{
switch(TOUPPER(seqPC[i]))
{
case 'A': scR += pparPO->mA; break;
case 'C': scR += pparPO->mC; break;
case 'G': scR += pparPO->mG; break;
case 'T': scR += pparPO->mT; break;
}
}
return(scR);
}
/****************************************************************************
* Set pair-wise minimum word size for comparision
*/
int SetPparsWordMinI(PPARS *pparPO,int v)
{
VALIDATE(pparPO,PPARS_ID);
if(v > 0) {
pparPO->min_word = v;
return(TRUE);
}
return(FALSE);
}
/***************************************************************************/
int SetPparsHammingI(PPARS *pparPO,int v)
{
VALIDATE(pparPO,PPARS_ID);
pparPO->do_ham = v;
return(TRUE);
}
/****************************************************************************
* Set clamp value
*/
int SetPparsClampsI(PPARS *pparPO,int v3,int v5)
{
VALIDATE(pparPO,PPARS_ID);
pparPO->cl3 = v3;
pparPO->cl5 = v5;
return(TRUE);
}
/****************************************************************************
* Set pair-wise loop minimum for loop evaluations
*/
int SetPparsLoopMinI(PPARS *pparPO,int v)
{
VALIDATE(pparPO,PPARS_ID);
if(v > 0)
{
pparPO->min_loop = v;
return(TRUE);
}
return(FALSE);
}
/****************************************************************************
* Dump PPARS data structure vars
*/
void DumpPpars(PPARS *pparPO,FILE *oPF)
{
char pairS[DEF_BS];
VALIDATE(pparPO,PPARS_ID);
HAND_NFILE(oPF);
fprintf(oPF,"######################################################\n");
fprintf(oPF,"# Parameters for Sequence Comparison \n");
fprintf(oPF,"######################################################\n");
fprintf(oPF,"# Comparison: ");
FillPparsCtypeString(pparPO->ctype,pairS);
fprintf(oPF,"%s\n",pairS);
fprintf(oPF,"# Algorithm: ");
FillPparsAlgString(pparPO->alg,pairS);
fprintf(oPF,"%s\n",pairS);
if(pparPO->do_ham) {
fprintf(oPF,"# Hamming distance (no align)\n");
}
else {
fprintf(oPF,"# Non-gap align (not Hamming)\n");
}
fprintf(oPF,"# Return value: ");
FillPparsRvalString(pparPO->rval,pairS);
fprintf(oPF,"%s\n",pairS);
fprintf(oPF,"# Threshold: %3.2f\n",pparPO->thresh);
fprintf(oPF,"# Min word size: %d\n",pparPO->min_word);
fprintf(oPF,"# Min loop size: %d\n",pparPO->min_loop);
fprintf(oPF,"# Clamp 3': %d\n",pparPO->cl3);
fprintf(oPF,"# Clamp 5': %d\n",pparPO->cl5);
DumpPparsWeights(pparPO,oPF);
fprintf(oPF,"#\n");
}
/****************************************************************************/
void DumpPparsWeights(PPARS *pparPO,FILE *oPF)
{
int i;
char pairS[DEF_BS];
VALIDATE(pparPO,PPARS_ID);
HAND_NFILE(oPF);
fprintf(oPF,"# Pair weightings\n");
if(!NO_S(pparPO->parfile))
{
fprintf(oPF,"# From parameter file: %s\n",pparPO->parfile);
}
else
{
switch(pparPO->parset)
{
case PP_PSET_ID:
fprintf(oPF,"# Base identity\n");
break;
case PP_PSET_COM:
fprintf(oPF,"# Base complement\n");
break;
case PP_PSET_TH:
fprintf(oPF,"# Thermodynamic defaults\n");
break;
default:
printf("Bogus parset = %d\n",pparPO->parset);
ERR("DumpPparsWeights","Bad parset code");
}
}
for(i=0;i<PP_IND_NUM;i++)
{
pairS[0] = DNAIndexBaseC(i/4);
pairS[1] = DNAIndexBaseC(i%4);
pairS[2] = '\0';
fprintf(oPF,"# %s = %4.3f\n",pairS,pparPO->mscore[i]);
}
}