-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdna_loop.c
More file actions
238 lines (230 loc) · 8.36 KB
/
dna_loop.c
File metadata and controls
238 lines (230 loc) · 8.36 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
/*
* dna_loop.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 <math.h>
#include "prim.h"
#include "dna.h"
#include "dna_pair.h"
#define DB_PAIR if(DB[116])
#define DB_PAIRLO if(DB[117])
/****************************************************************************
* Score all possible "loop" overlaps of sequence sS[len] and return the
* "offset" corresponding to the highest scoring;
* The actual score is returned via *scPR.
* The pairing-parameter data structure holds the value of the smallest
* allowable loop and the minimum amount of overlap to consider.
* Returned offset is the position of sequence (3') end relative to the (5')
* begining; if these two align together, offset = 0.
* For the sequence TAACGGCCGTTAGG, offset = -2 (with min_loop 4)
* 5'> TAAC-GG
* |||| :
* 3'< GGATTG-CC
*/
int ScoreLoopOverlapI(char *sS,int len,PPARS *ppPO,REAL *scPR)
{
int i,f,s,m,n,find,best;
REAL scR,bestR;
DB_PAIR DB_PrI(">> ScoreLoopOverlapI len=%d (loop=%d over=%d)\n",len,
ppPO->min_loop,ppPO->min_word);
/***
* m = number of alignments to compare
*/
m = 2 * (len - ppPO->min_loop - (2 * ppPO->min_word)) + 1;
find = len - ppPO->min_loop - (2 * ppPO->min_word);
bestR = -TOO_BIG_R;
best = 0;
DB_PAIR DB_PrI("+ m=%d find=%d\n",m,find);
for(i=0;i<m;i++)
{
f = MAX_NUM(0,find-i);
s = MAX_NUM(0,i-find);
n = (len - MAX_NUM(f,s) - ppPO->min_loop)/2;
DB_PAIR DB_PrI("+ M[%d] n=%d [%d][%d]\n",i,n,f,len-s-1);
scR = SeqLoopScoreR(sS, len, f, len-s-1, n, ppPO);
if(scR > bestR)
{
bestR = scR;
best = find-i;
DB_PAIR DB_PrI("+ Best=%d score=%4.2f\n",best,bestR);
}
}
*scPR = bestR;
DB_PAIR DB_PrI("<< ScoreLoopOverlapI %d (%4.2f)\n",best,bestR);
return(best);
}
/***************************************************************************
* Compare seq subset to itself to evaluate loop overlap
* sS is sequence [len]
* fi & si are first & second starting indices, for compare of n base stretch
*/
REAL SeqLoopScoreR(char *sS,int len,int fi,int si,int n,PPARS *ppPO)
{
REAL scR;
int i,mat,con,m;
DB_PAIRLO DB_PrI(">> SeqLoopScoreR len=%d fi=%d si=%d n=%d\n",len,fi,si,n);
scR = 0.0;
mat = con = 0;
switch(ppPO->alg)
{
case ALGO_MATCH:
for(i=0;i<n;i++)
{
switch(sS[fi+i])
{
case 'A': case 'a':
if( (sS[si-i]=='T') || (sS[si-i]=='t') )
mat++;
break;
case 'C': case 'c':
if( (sS[si-i]=='G') || (sS[si-i]=='g') )
mat++;
break;
case 'G': case 'g':
if( (sS[si-i]=='C') || (sS[si-i]=='c') )
mat++;
break;
case 'T': case 't':
if( (sS[si-i]=='A') || (sS[si-i]=='a') )
mat++;
break;
default:
printf("Bogus base [%d]|%c|\n",fi+i,sS[fi+i]);
ERR("SeqLoopScoreR","Bad seq"); return(-TOO_BIG_R);
}
}
scR = RNUM(mat);
break;
case ALGO_CONT:
for(i=0;i<n;i++)
{
m = 0;
/**
DB_PAIRLO DB_PrI("+ comp[%d][%d] ",fi+i,si-i);
DB_PAIRLO DB_PrI(" %c %c",sS[fi+i],sS[si-i]);
**/
switch(sS[fi+i])
{
case 'A': case 'a':
if( (sS[si-i]=='T') || (sS[si-i]=='t') )
m++;
break;
case 'C': case 'c':
if( (sS[si-i]=='G') || (sS[si-i]=='g') )
m++;
break;
case 'G': case 'g':
if( (sS[si-i]=='C') || (sS[si-i]=='c') )
m++;
break;
case 'T': case 't':
if( (sS[si-i]=='A') || (sS[si-i]=='a') )
m++;
break;
default:
printf("Bogus base [%d]|%c|\n",fi+i,sS[fi+i]);
ERR("SeqLoopScoreR","Bad seq"); return(-TOO_BIG_R);
}
if(m)
{
con++;
}
else
{
con = 0;
}
mat = MAX_NUM(mat,con);
/**
DB_PAIRLO DB_PrI(" m=%d con=%d mat=%d\n",m,con,mat);
**/
}
scR = RNUM(mat);
break;
case ALGO_BMATCH:
for(i=0;i<n;i++)
{
/***
* Current position
*/
con = FALSE;
switch(toupper(INT(sS[fi+i])))
{
case 'A': if(sS[si-i]=='T'){con++;} break;
case 'C': if(sS[si-i]=='G'){con++;} break;
case 'G': if(sS[si-i]=='C'){con++;} break;
case 'T': if(sS[si-i]=='A'){con++;} break;
default:
printf("Bogus base [%d]|%c|\n",fi+i,sS[fi+i]);
ERR("SeqLoopScoreR","Bad seq"); return(-TOO_BIG_R);
}
if(con == FALSE)
{
continue;
}
mat += 2;
/***
* Previous position; away from hairpin loop itself.
* Check if within seq dims
*/
if( ((fi+i-1)>=0) && ((si-i+1)<len) )
{
con = FALSE;
switch(toupper(INT(sS[fi+i-1])))
{
case 'A': if(sS[si-i+1]=='T'){con++;} break;
case 'C': if(sS[si-i+1]=='G'){con++;} break;
case 'G': if(sS[si-i+1]=='C'){con++;} break;
case 'T': if(sS[si-i+1]=='A'){con++;} break;
default:
printf("Bogus base [%d]|%c|\n",fi+i-1,sS[fi+i-1]);
ERR("SeqLoopScoreR","Bad seq"); return(-TOO_BIG_R);
}
if(con)
mat++;
}
/***
* Next position is closer to the hairpin loop; ignore if in
* the loop via check against number to compare, n
*/
if((i+1)<n)
{
con = FALSE;
switch(toupper(INT(sS[fi+i+1])))
{
case 'A': if(sS[si-i-1]=='T'){con++;} break;
case 'C': if(sS[si-i-1]=='G'){con++;} break;
case 'G': if(sS[si-i-1]=='C'){con++;} break;
case 'T': if(sS[si-i-1]=='A'){con++;} break;
default:
printf("Bogus base [%d]|%c|\n",fi+i+1,sS[fi+i+1]);
ERR("SeqLoopScoreR","Bad seq"); return(-TOO_BIG_R);
}
if(con)
mat++;
}
}
scR = RNUM(mat)/4.0;
break;
default:
scR = -TOO_BIG_R;
}
DB_PAIRLO DB_PrI("<< SeqLoopScoreR %4.2f\n",scR);
return(scR);
}