Debian Codegraph @

easel/esl_swat.c

raw symbols SCIP
1 /* This module is unfinished; don't try to use it for anything! */
2
3
4 /* Standard Smith/Waterman sequence alignment
5 *
6 * Contents:
7 *
8 *
9 */
10 #include <esl_config.h>
11
12 #include "easel.h"
13 #include "esl_composition.h"
14 #include "esl_scorematrix.h"
15
16 #define eslSWAT_PROHIBIT -999999999
17
18 /* Function: esl_swat_Score()
19 * Incept: SRE, Fri Apr 13 16:40:15 2007 [Janelia]
20 *
21 * Purpose: Implements Smith/Waterman local sequence alignment, recovering
22 * only a score, not an alignment. Query sequence <x> of length <L>
23 * is aligned to subject sequence <y> of length <M>, using
24 * a scoring system composed of residue alignment scores in matrix
25 * <S>, a gap-open score <gop>, and a gap-extend score <gex>.
26 *
27 * A gap of $k$ residues is scored as <gop> $\times (k-1)$
28 * <gex>. That is, the gap-open score is applied to the
29 * first residue in the gap, and the gap-extend penalty is
30 * applied to each remaining residue. Additionally, both
31 * <gop> and <gex> should be negative numbers.
32 *
33 * Returns: <eslOK> on success, and the raw alignment score is returned in
34 * <ret_sc>.
35 *
36 * Throws: (no abnormal error conditions)
37 */
38 int
39 esl_swat_Score(ESL_DSQ *x, int L, ESL_DSQ *y, int M, ESL_SCOREMATRIX *S, int gop, int gex, int *ret_sc)
40 {
41 int status;
42 int i,j;
43 int **rowmem = NULL;
44 int *mc, *mp, *ixc, *ixp, *iyc, *iyp;
45 int maxsc;
46
47 /* DP lattice is organized in rows (0) 1..i..M with target running vertically;
48 * columns (0) 1..j..L with query running horizontally.
49 */
50
51 /* Allocation;
52 * we need two rows of length (L+1) for each of three matrices, M, IX, IY.
53 * in the future, optimize by providing this from the caller.
54 */
55 ESL_ALLOC(rowmem, sizeof(int *) * 6);
56 rowmem[0] = NULL;
57 ESL_ALLOC(rowmem[0], sizeof(int) * 6 * (L+1));
58 for (i = 1; i < 6; i++) rowmem[i] = rowmem[0] + i*(L+1);
59
60 /* Initializations.
61 */
62 rowmem[1][0] = 0;
63 rowmem[3][0] = eslSWAT_PROHIBIT;
64 rowmem[5][0] = eslSWAT_PROHIBIT;
65 for (j = 0; j <= L; j++) {
66 rowmem[0][j] = 0;
67 rowmem[2][j] = eslSWAT_PROHIBIT;
68 rowmem[4][j] = eslSWAT_PROHIBIT;
69 }
70
71 maxsc = 0;
72 for (i = 1; i <= M; i++) /* for each position in target... */
73 {
74 if (i%2) { mp = rowmem[0]; mc = rowmem[1]; ixp = rowmem[2]; ixc = rowmem[3]; iyp = rowmem[4]; iyc = rowmem[5]; }
75 else { mc = rowmem[0]; mp = rowmem[1]; ixc = rowmem[2]; ixp = rowmem[3]; iyc = rowmem[4]; iyp = rowmem[5]; }
76
77 for (j = 1; j <= L; j++) /* for each position in query... */
78 {
79 /* Match score at mc[j] aligns xj,yi. We can reach here from M (mp[j-1]), IX (ixp[j-1]), or IY (iyp[j-1]) */
80 mc[j] = 0;
81 if (mp[j-1] > mc[j]) mc[j] = mp[j-1];
82 if (ixp[j-1] > mc[j]) mc[j] = ixp[j-1];
83 if (iyp[j-1] > mc[j]) mc[j] = iyp[j-1];
84 mc[j] += S->s[x[j]][y[i]];
85
86 if (mc[j] > maxsc) maxsc = mc[j];
87
88 /* IX score at ixc[j] aligns xj to gap (horizontal move).
89 * We reach here from mc[j-1] (gap-open) or ixc[j-1] (gap-extend).
90 */
91 ixc[j] = mc[j-1] + gop;
92 if (ixc[j-1] + gex > ixc[j]) ixc[j] = ixc[j-1] + gex;
93
94 /* analogously for vertical move to iyc. */
95 iyc[j] = mp[j] + gop;
96 if (iyp[j] + gex > iyc[j]) iyc[j] = iyp[j] + gex;
97 }
98 }
99
100 *ret_sc = maxsc;
101 free(rowmem[0]);
102 free(rowmem);
103 return eslOK;
104
105 ERROR:
106 *ret_sc = 0;
107 if (rowmem != NULL) {
108 if (rowmem[0] != NULL) free(rowmem[0]);
109 free(rowmem);
110 }
111 return status;
112 }
113
114
115 /*****************************************************************
116 * Stats driver.
117 *****************************************************************/
118
119 /*
120 gcc -I. -L. -g -Wall -DeslSWAT_STATS -o stats esl_swat.c -leasel -lm
121 ./stats
122 */
123 #ifdef eslSWAT_STATS
124
125 #include "easel.h"
126 #include "esl_getopts.h"
127 #include "esl_fileparser.h"
128 #include "esl_scorematrix.h"
129 #include "esl_random.h"
130 #include "esl_randomseq.h"
131 #include "esl_histogram.h"
132
133 int
134 main(int argc, char **argv)
135 {
136 int status;
137 ESL_RANDOMNESS *r = esl_randomness_Create(0);
138 ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO);
139 ESL_SCOREMATRIX *S = NULL;
140 ESL_DSQ *x = NULL; /* iid query */
141 ESL_DSQ *y = NULL; /* iid target */
142 double lambda;
143 double bg[20]; /* iid background probabilities */
144 int L; /* query length */
145 int M; /* target length */
146 int nseq; /* number of target seqs to simulate */
147 int i;
148 int gop;
149 int gex;
150 char *mxfile = "PMX";
151 int raw_sc;
152
153 /* Configuration
154 */
155 L = 400; /* query length */
156 M = 400; /* target length */
157 nseq = 50000;
158 gop = -11;
159 gex = -1;
160 lambda = 0.3207;
161
162 ESL_ALLOC(x, sizeof(ESL_DSQ) * (L+2));
163 ESL_ALLOC(y, sizeof(ESL_DSQ) * (M+2));
164
165 /* Input an amino acid score matrix from a file. */
166 if (mxfile != NULL) {
167 ESL_FILEPARSER *efp = NULL;
168 if ( esl_fileparser_Open(mxfile, NULL, &efp) != eslOK) esl_fatal("failed to open score file %s", mxfile);
169 if ( esl_scorematrix_Read(efp, abc, &S) != eslOK) esl_fatal("failed to read matrix from %s", mxfile);
170 esl_fileparser_Close(efp);
171 } else { /* default = BLOSUM62 */
172 S = esl_scorematrix_Create(abc);
173 esl_scorematrix_Set("BLOSUM62", S);
174 }
175 esl_composition_BL62(bg);
176
177 esl_rsq_xIID(r, bg, 20, L, x);
178
179 for (i = 0; i < nseq; i++)
180 {
181 esl_rsq_xIID(r, bg, 20, M, y);
182 esl_swat_Score(x, L, y, M, S, gop, gex, &raw_sc);
183 printf("%d\n", raw_sc);
184 }
185
186 free(x);
187 free(y);
188 esl_scorematrix_Destroy(S);
189 esl_alphabet_Destroy(abc);
190 esl_randomness_Destroy(r);
191 exit(0);
192
193 ERROR:
194 exit(status);
195 }
196
197
198
199
200
201
202 #endif /*eslSWAT_STATS*/
203
204
205 /*****************************************************************
206 * Unit tests
207 *****************************************************************/
208
209 void
210 utest_Score(char *s1, char *s2, ESL_SCOREMATRIX *S, int gop, int gex, int expect_score)
211 {
212
213 }
214
215
216
217
218
219