File: embiep.c

package info (click to toggle)
emboss 6.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 571,248 kB
  • ctags: 39,971
  • sloc: ansic: 460,578; java: 29,439; perl: 13,573; sh: 12,740; makefile: 3,275; csh: 706; asm: 351; xml: 239; pascal: 237; modula3: 8
file content (772 lines) | stat: -rw-r--r-- 17,462 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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/* @source embiep *************************************************************
**
** Isoelectric point routines
**
** @author Copyright (c) 1999 Alan Bleasby
** @version $Revision: 1.27 $
** @modified $Date: 2013/06/29 22:38:19 $ by $Author: rice $
** @@
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library 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
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA  02110-1301,  USA.
**
******************************************************************************/


#include "ajlib.h"

#include "embiep.h"
#include "ajsys.h"
#include "ajfiledata.h"
#include "ajfileio.h"
#include "ajbase.h"

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>



#define PKFILE "Epk.dat"




/* @func embIepPkNew **********************************************************
**
** Create a pK array to hold amino acid pK data
**
** @return [double**] pK data
**
** @release 6.1.0
******************************************************************************/

double** embIepPkNew(void)
{
    double **pK = NULL;
    ajuint i;

    AJCNEW(pK,EMBIEPSIZE);

    for(i=0; i < EMBIEPSIZE;++i)
        AJCNEW(pK[i],3);

    return pK;
}




/* @func embIepPkNewFile ******************************************************
**
** Create a pK array and read the data from a file
**
** @param [u] pkfile [AjPFile] Amino acid pKa data file
** @param [w] pK [double***] Arrays of pK values
** @return [AjBool] True on success
**
** @release 6.6.0
******************************************************************************/

AjBool embIepPkNewFile(AjPFile pkfile, double ***pK)
{
    if(!*pK)
        *pK = embIepPkNew();

    embIepPkReadFile(*pK, pkfile); /* read pK values */
    
    return ajTrue;
}




/* @func embIepPkDel  *********************************************************
**
** Delete a pK array and read the data
**
** @param [w] pK [double ***] pKs
**
** @return [void]
**
** @release 6.1.0
******************************************************************************/

void embIepPkDel(double ***pK)
{
    ajuint i;

    for(i=0; i < EMBIEPSIZE; ++i)
        AJFREE((*pK)[i]);

    AJFREE(*pK);

    return;
}




/* @func embIepPhToHconc  *****************************************************
**
** Convert pH to hydrogen ion concontration
**
** @param [r] pH [double] pH
**
** @return [double] hydrogen ion concentrration
**
** @release 4.0.0
******************************************************************************/

double embIepPhToHconc(double pH)
{
    return pow(10.0,-pH);
}




/* @func embIepPhFromHconc ****************************************************
**
** Convert hydrogen ion concontration to pH
**
** @param [r] H [double] H
**
** @return [double] pH
**
** @release 4.0.0
******************************************************************************/

double embIepPhFromHconc(double H)
{
    return -log10(H);
}




/* @func embIepPkToK  *********************************************************
**
** Convert pK to dissociation constant
**
** @param [r] pK [double] pK
**
** @return [double] dissociation constant
**
** @release 1.0.0
******************************************************************************/

double embIepPkToK(double pK)
{
    return pow(10.0,-pK);
}




/* @func embIepPkFromK ********************************************************
**
** Convert dissociation constant to pK
**
** @param [r] K [double] K
**
** @return [double] pK
**
** @release 4.0.0
******************************************************************************/

double embIepPkFromK(double K)
{
    return -log10(K);
}




/* @func embIepPkRead  ********************************************************
**
** Read the pK values from Epk.dat
**
** @param [w] pK [double**] pK
**
** @return [void]
**
** @release 1.0.0
******************************************************************************/

void embIepPkRead(double **pK)
{
    AjPFile inf = NULL;

    inf = ajDatafileNewInNameC(PKFILE);

    embIepPkReadFile(pK, inf);

    ajFileClose(&inf);

    return;
}




/* @func embIepPkReadFile *****************************************************
**
** Read the pK values from Epk.dat
**
** @param [w] pK [double**] pK
** @param [w] pkfile [AjPFile] pK data file
**
** @return [void]
**
** @release 6.6.0
******************************************************************************/

void embIepPkReadFile(double **pK, AjPFile pkfile)
{
    AjPStr line;
    const char *p;
    double  amino    = 7.50;
    double  carboxyl = 3.55;
    double dval = 0.0;
    char ch;
    ajuint ich;
    ajuint i;
    ajuint j;

    if(!pkfile)
	ajFatal("%s file not found",PKFILE);

    for(j=0; j < 3; ++j)
        for(i=0; i < EMBIEPSIZE; ++i)
            pK[i][j] = 0.0;
    
    line = ajStrNew();

    while(ajReadline(pkfile,&line))
    {
	p = ajStrGetPtr(line);

	if(*p=='#' || *p=='!' || *p=='\n' || *p=='\r')
	    continue;

	if(ajStrPrefixCaseC(line,"Amino"))
	{
	    p = ajSysFuncStrtok(p," \t\n\r");
	    p = ajSysFuncStrtok(NULL," \t\n\r");
	    sscanf(p,"%lf",&amino);
	    continue;
	}

	if(ajStrPrefixCaseC(line,"Carboxyl"))
	{
	    p = ajSysFuncStrtok(p," \t\n\r");
	    p = ajSysFuncStrtok(NULL," \t\n\r");
	    sscanf(p,"%lf",&carboxyl);
	    continue;
	}

	p  = ajSysFuncStrtok(p," \t\n\r");
	ch = ajSysCastItoc(toupper((ajint)*p));
        ich = ajBasecodeToInt(ch);

	p  = ajSysFuncStrtok(NULL," \t\n\r");
        sscanf(p, "%lf", &pK[ich][0]);

        p  = ajSysFuncStrtok(NULL," \t\n\r");

	if(p && sscanf(p, "%lf", &dval))
            pK[ich][1] = dval;
        else
            pK[ich][1] = 0.0;

        p  = ajSysFuncStrtok(NULL," \t\n\r");

	if(p && sscanf(p, "%lf", &dval))
            pK[ich][2] = dval;
        else
            pK[ich][2] = 0.0;
    }

    pK[EMBIEPAMINO][0]    = amino;
    pK[EMBIEPAMINO][1]    = amino;
    pK[EMBIEPCARBOXYL][0] = carboxyl;
    pK[EMBIEPCARBOXYL][2] = carboxyl;

    ajStrDel(&line);

    return;
}




/* @func embIepCompC **********************************************************
**
** Calculate the amino acid composition of a protein sequence
**
** @param [r] s [const char *] protein sequence
** @param [r] amino [ajuint] number of amino termini
** @param [r] carboxyl [ajuint] number of carboxyl termini
** @param [r] sscount [ajuint] number of disulphide bridges
** @param [r] modlysine [ajuint] number of modified lysines
** @param [w] c [ajuint*] array of amino acid composition
** @param [w] resn [ajuint*] N-terminal amino acid
** @param [w] resc [ajuint*] C-terminal amino acid
**
** @return [void]
**
** @release 4.0.0
******************************************************************************/

void embIepCompC(const char *s, ajuint amino, ajuint carboxyl,
		 ajuint sscount, ajuint modlysine,
		 ajuint *c, ajuint *resn, ajuint *resc)
{
    ajint i;
    ajint j;
    const char *p;
    ajuint ires = 0;
    
    for(i=0;i<EMBIEPSIZE;++i)
	c[i]=0;

    p=s;
    *resn = ajBasecodeToInt(ajSysCastItoc(toupper((ajint)*p)));

    while(*p)
    {
        ires = ajBasecodeToInt(ajSysCastItoc(toupper((ajint)*p)));
	++c[ires];
	++p;
    }

    *resc = ires;

    if(c[1])				/* B = D or N use Dayhoff freq */
    {
	j = (int) (0.5 + ((float)c[1]) * 5.5 / 9.8);
	c[3] += j;
	c[13] += c[1] - j;
	ajDebug("embIepCompC B:%d => D:%d N:%d\n",
		c[1], j, c[1]-j);
	c[1] = 0;
    }

    if(c[25])				/* Z = E or Q use Dayhoff freq */
    {
	j = (int) (0.5 + ((float)c[25]) * 6.0 / 9.9);
	c[4] += j;
	c[16] += c[25] - j;
	ajDebug("embIepCompC Z:%d => E:%d Q:%d\n",
		c[25], j, c[25]-j);
	c[25] = 0;
    }

    c[EMBIEPAMINO]    = amino;
    c[EMBIEPCARBOXYL] = carboxyl;

    if (sscount > 0)
    {
	if(c[EMBIEPCYSTEINE] <  2*sscount)
	{
	    ajWarn("embIepCompC %d disulphides but only %d cysteines\n",
		   sscount, c[EMBIEPCYSTEINE]+2*sscount);
	    c[EMBIEPCYSTEINE] = 0;
	}
	else
	{
	    c[EMBIEPCYSTEINE] -= 2*sscount;
	}
    }

    if (modlysine > 0)
    {
	if(c[EMBIEPLYSINE] < modlysine)
	{
	    ajWarn("embIepCompC %d modified lysines but only %d lysines\n",
		   sscount, c[EMBIEPLYSINE]);
	    c[EMBIEPLYSINE] = 0;
	}
	else
	{
	    c[EMBIEPLYSINE] -= modlysine;
	}
    }

    return;
}




/* @func embIepCompS **********************************************************
**
** Calculate the amino acid composition of a protein sequence
**
** @param [r] str [const AjPStr] protein sequence
** @param [r] amino [ajuint] number of amino termini
** @param [r] carboxyl [ajuint] number of carboxyl termini
** @param [r] sscount [ajuint] number of disulphide bridges
** @param [r] modlysine [ajuint] number of modified lysines
** @param [w] c [ajuint *] Array of amino acid composition
** @param [w] resn [ajuint*] N-terminal amino acid
** @param [w] resc [ajuint*] C-terminal amino acid
**
** @return [void]
**
** @release 4.0.0
******************************************************************************/

void embIepCompS(const AjPStr str, ajuint amino, ajuint carboxyl,
		 ajuint sscount, ajuint modlysine,
		 ajuint *c, ajuint *resn, ajuint *resc)
{
    embIepCompC(MAJSTRGETPTR(str), amino, carboxyl, sscount, modlysine,
                c, resn, resc);

    return;
}




/* @func embIepCalcK  *********************************************************
**
** Calculate the dissociation constants
** Amino acids for which there is no entry in Epk.dat have K set to 0.0
**
** @param [w] K [double *] dissociation constants
** @param [w] pK [double **] pK values
**
** @return [void]
**
** @release 1.0.0
******************************************************************************/

void embIepCalcK(double *K, double **pK)
{
    ajint i;
    
    for(i=0;i<EMBIEPSIZE;++i)
	if(!pK[i][0])
	    K[i] = 0.0;
	else
	    K[i] = embIepPkToK(pK[i][0]);

    return;
}




/* @func embIepCalcKend *******************************************************
**
** Calculate the dissociation constants for the N and C termini
** Amino acids for which there is no entry in Epk.dat have K set to 0.0
**
** @param [w] K [double *] dissociation constants
** @param [w] pK [double **] pK values
** @param [r] resn [ajuint] N-terminal amino acid
** @param [r] resc [ajuint] N-terminal amino acid
**
** @return [void]
**
** @release 1.0.0
******************************************************************************/

void embIepCalcKend(double *K, double **pK, ajuint resn, ajuint resc)
{
    if(!pK[resn][1])
        K[EMBIEPAMINO] = embIepPkToK(pK[EMBIEPAMINO][0]);
    else
        K[EMBIEPAMINO] = embIepPkToK(pK[resn][1]);

    if(!pK[resc][2])
        K[EMBIEPCARBOXYL] = embIepPkToK(pK[EMBIEPCARBOXYL][0]);
    else
        K[EMBIEPCARBOXYL] = embIepPkToK(pK[resc][2]);

    return;
}




/* @func embIepGetProto  ******************************************************
**
** Calculate the number of H+ bound
** Amino acids for which there is no entry in Epk.dat have this set to 0.0
**
** @param [r] K [const double *] dissociation constants
** @param [r] c [const ajuint *] sequence composition
** @param [w] op [ajuint *] printout flags
** @param [r] H [double] hydrogen ion concentration
** @param [w] pro [double *] number of protons bound
**
** @return [void]
**
** @release 1.0.0
******************************************************************************/

void embIepGetProto(const double *K, const ajuint *c,
		    ajuint *op, double H, double *pro)
{
    ajint i;

    for(i=0;i<EMBIEPSIZE;++i)
	if(!K[i])
	{
	    pro[i]=0.0;
	    op[i]=0;
	}
	else
	{
	    if(!c[i])
		op[i]=0;
	    else
		op[i]=1;

	    pro[i]=(double)c[i] * (H/(H+K[i]));
	}

    return;
}




/* @func embIepGetCharge  *****************************************************
**
** Calculate the number of H+ bound
**
** @param [r] c [const ajuint *] sequence composition
** @param [r] pro [const double *] number of protons
** @param [w] total [double *] total protons
**
** @return [double] charge
**
** @release 1.0.0
******************************************************************************/

double embIepGetCharge(const ajuint *c, const double *pro, double *total)
{
    ajint i;
    double C;

    for(i=0,*total=0.0;i<EMBIEPSIZE;++i)
	*total += pro[i];

    C = (pro[10]+pro[17]+pro[7]+pro[EMBIEPAMINO]) - /* basic: KRH */
	((double)c[24]-pro[24] +                    /* acidic: Y */
	 (double)c[2]-pro[2] +                      /* acidic: C */
	 (double)c[3]-pro[3] +                      /* acidic: D */
	 (double)c[4]-pro[4] +                      /* acidic: E */
	 (double)c[EMBIEPCARBOXYL]-pro[EMBIEPCARBOXYL] );

    return C;
}




/* @func embIepPhConverge  ****************************************************
**
** Calculate the pH nearest the IEP or return 0.0 if one doesn't exist
**
** @param [r] c [const ajuint *] sequence composition
** @param [r] K [const double *] sequence dissociation constants
** @param [w] op [ajuint *] printout flags
** @param [w] pro [double *] number of protons
**
** @return [double] IEP or 0.0
**
** @release 1.0.0
******************************************************************************/

double embIepPhConverge(const ajuint *c, const double *K,
                        ajuint *op, double *pro)
{
    double sum = 0.0;
    double charge;
    double top;
    double mid;
    double bot;
    double H;
    double tph = 1.0;
    double bph = 14.0;

    H = embIepPhToHconc(tph);
    embIepGetProto(K,c,op,H,pro);
    top = embIepGetCharge(c,pro,&sum);
    H = embIepPhToHconc(bph);
    embIepGetProto(K,c,op,H,pro);
    bot = embIepGetCharge(c,pro,&sum);

    if((top>0.0 && bot>0.0) || (top<0.0 && bot<0.0))
	return 0.0;

    while(bph-tph>0.0001)
    {
	mid = ((bph-tph) / 2.0) + tph;
	H = embIepPhToHconc(mid);
	embIepGetProto(K,c,op,H,pro);
	charge = embIepGetCharge(c,pro,&sum);

	if(charge>0.0)
	{
	    tph = mid;
	    continue;
	}

	if(charge<0.0)
	{
	    bph = mid;
	    continue;
	}
	else
	    return mid;
    }

    return tph;
}




/* @func embIepIepC ***********************************************************
**
** Calculate the pH nearest the IEP.
**
** @param [r] s [const char *] sequence
** @param [r] amino [ajuint] number of N-termini
** @param [r] carboxyl [ajuint] number of C-termini
** @param [r] sscount [ajuint] number of disulphide bridges
** @param [r] modlysine [ajuint] number of modified lysines
** @param [w] pK [double **] pK values
** @param [w] iep [double *] IEP
** @param [r] termini [AjBool] use termini
**
** @return [AjBool] True if IEP exists
**
** @release 4.0.0
******************************************************************************/

AjBool embIepIepC(const char *s, ajuint amino, ajuint carboxyl,
		  ajuint sscount, ajuint modlysine,
		  double **pK, double *iep, AjBool termini)
{
    ajuint *c    = NULL;
    ajuint *op   = NULL;
    double *K   = NULL;
    double *pro = NULL;
    ajuint resn = 0;
    ajuint resc = 0;

    *iep = 0.0;

    AJCNEW(c,   EMBIEPSIZE);
    AJCNEW(op,  EMBIEPSIZE);
    AJCNEW(K,   EMBIEPSIZE);
    AJCNEW(pro, EMBIEPSIZE);


    embIepCalcK(K,pK);			/* Convert to dissoc consts */

    /* Get sequence composition */
    embIepCompC(s,amino,carboxyl,sscount, modlysine, c, &resn, &resc);

    if(!termini)
	c[EMBIEPAMINO] = c[EMBIEPCARBOXYL] = 0;
    else
        embIepCalcKend(K, pK, resn, resc);

    *iep = embIepPhConverge(c,K,op,pro);

    AJFREE(pro);
    AJFREE(K);
    AJFREE(op);
    AJFREE(c);

    if(!*iep)
	return ajFalse;

    return ajTrue;
}




/* @func embIepIepS ***********************************************************
**
** Calculate the pH nearest the IEP.
**
** @param [r] str [const AjPStr] sequence
** @param [r] amino [ajuint] number of N-termini
** @param [r] carboxyl [ajuint] number of C-termini
** @param [r] sscount [ajuint] number of disulphide bridges
** @param [r] modlysine [ajuint] number of modified lysines
** @param [w] pK [double **] pK values
** @param [w] iep [double *] IEP
** @param [r] termini [AjBool] use termini
**
** @return [AjBool] True if IEP exists
**
** @release 4.0.0
******************************************************************************/

AjBool embIepIepS(const AjPStr str, ajuint amino, ajuint carboxyl,
		  ajuint sscount, ajuint modlysine,
		  double **pK, double *iep, AjBool termini)
{
    return embIepIepC(ajStrGetPtr(str), amino, carboxyl, sscount, modlysine,
		      pK, iep, termini);
}




#ifdef AJ_COMPILE_DEPRECATED_BOOK
#endif




#ifdef AJ_COMPILE_DEPRECATED
/* @obsolete embIepComp
** @replace embIepCompC (1,2,3/1,2,0,0,3)
*/
__deprecated void  embIepComp(const char *s, ajint amino, ajint carboxyl,
                              ajint *c)
{
    embIepCompC(s, amino, carboxyl, 0, 0, c);

    return;
}




/* @obsolete embIepIEP
** @replace embIepIepC (1,2,3,4/1,2,0,0,3,4)
*/

__deprecated AjBool  embIepIEP(const char *s, ajint amino, ajint carboxyl,
                               double *pK, double *iep, AjBool termini)
{
    return embIepIepC(s, amino, carboxyl, 0, 0, pK, iep, termini);
}
#endif