File: findkm.c

package info (click to toggle)
emboss 6.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 571,544 kB
  • sloc: ansic: 460,579; java: 29,439; perl: 13,573; sh: 12,754; makefile: 3,283; csh: 706; asm: 351; xml: 239; pascal: 237; modula3: 8
file content (399 lines) | stat: -rw-r--r-- 9,777 bytes parent folder | download | duplicates (7)
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
/* @source findkm.c
**
** @author Copyright (C) Sinead O'Leary (soleary@hgmp.mrc.ac.uk),
** David Martin (david.martin@biotek.uio.no)
**
** Application to calculate the Michaelis Menton Constants (Km) of different
** enzymes and their substrates.
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
******************************************************************************/

#include "emboss.h"
#include <math.h>
#include <stdlib.h>
#include <limits.h>




static float findkm_summation(const float *arr, ajint number);
static float findkm_multisum (const float *arr1, const float *arr2,
			      ajint number);
static float findkm_findmax(const float *arr1, ajint number);
static float findkm_findmin(const float *arr1, ajint number);




/* @prog findkm ***************************************************************
**
** Find Km and Vmax for an enzyme reaction by a Hanes/Woolf plot
**
******************************************************************************/

int main(int argc, char **argv)
{
    AjPFile infile = NULL;
    AjPFile outfile = NULL;
    AjPStr line;
    AjPGraph graphLB = NULL;
    AjPGraphdata xygraph  = NULL;
    AjPGraphdata xygraph2 = NULL;
    AjBool doplot;

    ajint N=0;

    float *xdata = NULL;
    float *ydata = NULL;
    float *V = NULL;
    float *S = NULL;

    float a;
    float b;
    float upperXlimit;
    float upperYlimit;

    float A;
    float B;
    float C;
    float D;
    float xmin;
    float xmax;
    float ymin;
    float ymax;
    float xmin2;
    float xmax2;
    float ymin2;
    float ymax2;

    float Vmax;
    float Km;
    float cutx;
    float cuty;

    float amin = 0.;
    float amax = 0.;
    float bmin = 0.;
    float bmax = 0.;


    embInit("findkm", argc, argv);

    infile  = ajAcdGetInfile("infile");
    outfile = ajAcdGetOutfile ("outfile");
    doplot  = ajAcdGetBoolean("plot");
    graphLB = ajAcdGetGraphxy("graphLB");
    line = ajStrNew();


    /* Determine N by reading infile */

    while(ajReadlineTrim(infile, &line))
        if(ajStrGetLen(line) >0)
	    N++;


    /* only allocate memory to the arrays */

    AJCNEW(xdata, N);
    AJCNEW(ydata, N);
    AJCNEW(S, N);
    AJCNEW(V, N);

    ajFileSeek(infile, 0L, 0);

    N=0;
    while(ajReadlineTrim(infile, &line))
    {
	if(ajStrGetLen(line) > 0)
        {
            sscanf(ajStrGetPtr(line),"%f %f",&S[N],&V[N]);
            if(S[N] > 0.0 && V[N] > 0.0)
            {
                xdata[N] = S[N];
                ydata[N] = S[N]/V[N];
		N++;
            }
        }
    }


    /* find the max and min values for the graph parameters*/
    xmin = (float)0.5*findkm_findmin(xdata, N);
    xmax = (float)1.5*findkm_findmax(xdata, N);
    ymin = (float)0.5*findkm_findmin(ydata, N);
    ymax = (float)1.5*findkm_findmax(ydata, N);

    xmin2 = (float)0.5*findkm_findmin(S, N);
    xmax2 = (float)1.5*findkm_findmax(S, N);
    ymin2 = (float)0.5*findkm_findmin(V, N);
    ymax2 = (float)1.5*findkm_findmax(V, N);



    /*
    ** In case the casted ints turn out to be same number on the axis,
    ** make the max number larger than the min so graph can be seen.
    */

    if((ajint)xmax == (ajint)xmin)
        ++xmax;
    if((ajint)ymax == (ajint)ymin)
        ++ymax;


    if((ajint)xmax2 == (ajint)xmin2)
        ++xmax2;
    if((ajint)ymax2 == (ajint)ymin2)
        ++ymax2;



    /*
    ** Gaussian Elimination for Best-fit curve plotting and
    ** calculating Km and Vmax
    */

    A = findkm_summation(xdata, N);
    B = findkm_summation(ydata, N);

    C = findkm_multisum(xdata, ydata, N);
    D = findkm_multisum(xdata, xdata, N);


    /*
    ** To find the best fit line, Least Squares Fit:    y =ax +b;
    ** Two Simultaneous equations, REARRANGE FOR b
    **
    ** findkm_summation(ydata, N) - findkm_summation(xdata,N)*a - N*b =0;
    ** b = (findkm_summation(ydata,N) - findkm_summation(xdata,N)*a) /  N;
    ** b = (B - A*a)/ N;
    **
    ** C - D*a - A*((B - A*a)/ N) =0;
    ** C - D*a - A*B/N + A*A*a/N =0;
    ** C - A*B/N = D*a - A*A*a/N;
    */

    /* REARRANGE FOR a */

    a = (N*C - A*B)/ (N*D - A*A);
    b = (B - A*a)/ N;

    /*
    ** Equation of Line - Lineweaver burk eqn
    ** 1/V = (Km/Vmax)*(1/S) + 1/Vmax;
    */


    Vmax = 1/a;
    Km = b/a;

    cutx = -1/Km;
    cuty = Km/Vmax;

    /* set limits for last point on graph */

    upperXlimit = findkm_findmax(xdata,N)+3;
    upperYlimit = (upperXlimit)*a + b;

    ajFmtPrintF(outfile, "---Hanes Woolf Plot Calculations---\n");
    ajFmtPrintF(outfile, "Slope of best fit line is a = %.2f\n", a);
    ajFmtPrintF(outfile,"Coefficient in Eqn of line y = ma +b is b "
		"= %.2f\n", b);

    ajFmtPrintF(outfile, "Where line cuts x axis = (%.2f, 0)\n", cutx);
    ajFmtPrintF(outfile, "Where line cuts y axis = (0, %.2f)\n", cuty);
    ajFmtPrintF(outfile, "Limit-point of graph for plot = (%.2f, %.2f)\n\n",
		upperXlimit, upperYlimit);
    ajFmtPrintF(outfile, "Vmax = %.2f, Km = %f\n",Vmax, Km);

    /* draw graphs */

    if(doplot)
    {
	xygraph = ajGraphdataNewI(N);
	ajGraphdataAddXY(xygraph, S, V);
	ajGraphDataAdd(graphLB, xygraph);
	ajGraphdataSetTitleC(xygraph, "Michaelis Menten Plot");
	ajGraphdataSetXlabelC(xygraph, "[S]");
	ajGraphdataSetYlabelC(xygraph, "V");

	ajGraphxySetXstartF(graphLB, 0.0);
	ajGraphxySetXendF(graphLB, xmax2);
	ajGraphxySetYstartF(graphLB, 0.0);
	ajGraphxySetYendF(graphLB, ymax2);
	ajGraphxySetXrangeII(graphLB, (ajint)0.0, (ajint)xmax2);
	ajGraphxySetYrangeII(graphLB, (ajint)0.0, (ajint)ymax2);
	ajGraphdataAddposLine(xygraph, 0.0, 0.0, S[0], V[0], (ajint)BLACK);
	ajGraphxyShowPointsCircle(graphLB, ajTrue);
	ajGraphdataSetMinmax(xygraph,0.0,xmax2,0.0,ymax2);


	ajGraphicsCalcRange(S,N,&amin,&amax);
	ajGraphicsCalcRange(V,N,&bmin,&bmax);
	ajGraphdataSetTruescale(xygraph,amin,amax,bmin,bmax);
	ajGraphdataSetTypeC(xygraph,"2D Plot Float");

	xygraph2 = ajGraphdataNewI(N);
	ajGraphdataAddXY(xygraph2, xdata, ydata);
	ajGraphDataAdd(graphLB, xygraph2);

	ajGraphdataSetTitleC(xygraph2, "Hanes Woolf Plot");
	ajGraphdataSetXlabelC(xygraph2, "[S]");
	ajGraphdataSetYlabelC(xygraph2, "[S]/V");

	ajGraphxySetXstartF(graphLB, cutx);
	ajGraphxySetXendF(graphLB, upperXlimit);
	ajGraphxySetYstartF(graphLB, 0.0);
	ajGraphxySetYendF(graphLB, upperYlimit);
	ajGraphxySetXrangeII(graphLB, (ajint)cutx, (ajint)upperXlimit);
	ajGraphxySetYrangeII(graphLB, (ajint)0.0, (ajint)upperYlimit);

	ajGraphxyShowPointsCircle(graphLB, ajTrue);
	ajGraphdataSetMinmax(xygraph2, cutx,upperXlimit,0.0,upperYlimit);
	ajGraphicsCalcRange(xdata,N,&amin,&amax);
	ajGraphicsCalcRange(ydata,N,&bmin,&bmax);
	ajGraphdataSetTruescale(xygraph2,amin,amax,bmin,bmax);
	ajGraphdataSetTypeC(xygraph2,"2D Plot");



	ajGraphSetTitleC(graphLB,"FindKm");
	ajGraphxySetflagOverlay(graphLB,ajFalse);
	ajGraphxyDisplay(graphLB, ajTrue);
    }

    AJFREE(xdata);
    AJFREE(ydata);

    AJFREE(S);
    AJFREE(V);

    ajFileClose(&infile);
    ajFileClose(&outfile);

    ajGraphxyDel(&graphLB);
    ajStrDel(&line);

    embExit();

    return 0;
}




/* @funcstatic findkm_summation ***********************************************
**
** Undocumented.
**
** @param [r] arr [const float*] Undocumented
** @param [r] number [ajint] Array size
** @return [float] Undocumented
** @@
******************************************************************************/


static float findkm_summation(const float *arr, ajint number)
{
    ajint i;
    float sum=0;

    for(i = 0; i < number; i++)
        sum += arr[i];

    return sum;
}




/* @funcstatic findkm_multisum ************************************************
**
** Undocumented.
**
** @param [r] arr1 [const float*] Undocumented
** @param [r] arr2 [const float*] Undocumented
** @param [r] number [ajint] Array size
** @return [float] Undocumented
** @@
******************************************************************************/

static float findkm_multisum(const float *arr1, const float *arr2,
			     ajint number)
{
    ajint i;
    float sum = 0;

    for(i = 0; i < number; i++)
        sum += arr1[i]*arr2[i];

    return sum;
}




/* @funcstatic findkm_findmax *************************************************
**
** Undocumented.
**
** @param [r] arr [const float*] Undocumented
** @param [r] number [ajint] Array size
** @return [float] Undocumented
** @@
******************************************************************************/

static float findkm_findmax(const float *arr, ajint number)
{
    ajint i;
    float max;

    max = arr[0];

    for(i=1; i<number; ++i)
        if(arr[i] > max)
            max = arr[i];

    return max;
}




/* @funcstatic findkm_findmin *************************************************
**
** Undocumented.
**
** @param [r] arr [const float*] Undocumented
** @param [r] number [ajint] Array size
** @return [float] Undocumented
** @@
******************************************************************************/

static float findkm_findmin(const float *arr, ajint number)
{
    ajint i;
    float min;

    min = arr[0];

    for(i=1; i<number; ++i)
        if(arr[i] < min)
            min = arr[i];

    return min;
}