File: embpdb.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 (1030 lines) | stat: -rw-r--r-- 28,303 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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
/* @source embpdb *************************************************************
**
** Algorithms for handling protein structural data.
** For use with the Atom, Chain and Pdb objects defined in ajpdb.h
** Also for use with Hetent, Het, Vdwres, Vdwall, Cmap and Pdbtosp objects 
** (also in ajpdb.h).
** 
** @author CopyrightCopyright (c) 2004 Jon Ison
** @version $Revision: 1.24 $
** @modified $Date: 2012/07/14 14:52:40 $ 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 "embpdb.h"
#include "ajpdb.h"
#include "ajdomain.h"
#include "ajlist.h"

#include <math.h>




/* ======================================================================= */
/* ============================ private data ============================= */
/* ======================================================================= */




/* ======================================================================= */
/* ================= Prototypes for private functions ==================== */
/* ======================================================================= */




/* ======================================================================= */
/* ========================== private functions ========================== */
/* ======================================================================= */




/* ======================================================================= */
/* =========================== constructors ============================== */
/* ======================================================================= */




/* @section Constructors ****************************************************
**
** All constructors return a pointer to a new instance. It is the 
** responsibility of the user to first destroy any previous instance. The 
** target pointer does not need to be initialised to NULL, but it is good 
** programming practice to do so anyway.
**
****************************************************************************/




/* ======================================================================= */
/* =========================== destructors =============================== */
/* ======================================================================= */




/* @section Structure Destructors *******************************************
**
** All destructor functions receive the address of the instance to be
** deleted.  The original pointer is set to NULL so is ready for re-use.
**
****************************************************************************/




/* ======================================================================= */
/* ============================ Assignments ============================== */
/* ======================================================================= */




/* @section Assignments *****************************************************
**
** These functions overwrite the instance provided as the first argument
** A NULL value is always acceptable so these functions are often used to
** create a new instance by assignment.
**
****************************************************************************/




/* ======================================================================= */
/* ============================= Modifiers =============================== */
/* ======================================================================= */




/* @section Modifiers *******************************************************
**
** These functions use the contents of an instance and update them.
**
****************************************************************************/




/* ======================================================================= */
/* ========================== Operators ===================================*/
/* ======================================================================= */




/* @section Operators *******************************************************
**
** These functions use the contents of an instance but do not make any 
** changes.
**
****************************************************************************/




/* @func embPdbidToSp *********************************************************
**
** Read a pdb identifier code and writes the equivalent swissprot identifier
** code.  Relies on list of Pdbtosp objects sorted by PDB code, which is 
** usually obtained by a call to ajPdbtospReadAllNew.
** 
** @param [r] pdb  [const AjPStr]   Pdb  identifier code
** @param [w] spr  [AjPStr*]  Swissprot identifier code
** @param [r] list [const AjPList]  Sorted list of Pdbtosp objects
**
** @return [AjBool]  True if a swissprot identifier code was found
**                   for the Pdb code.
**
** @release 2.9.0
** @@
****************************************************************************/

AjBool embPdbidToSp(const AjPStr pdb, AjPStr *spr, const AjPList list)
{
    AjPPdbtosp *arr = NULL;  /* Array derived from list */
    ajint dim = 0;           /* Size of array */
    ajint idx = 0;           /* Index into array for the Pdb code */

    
    if(!pdb || !list)
    {
	ajWarn("Bad args passed to embPdbidToSp");

	return ajFalse;
    }
    

    dim = (ajuint) ajListToarray(list, (void ***) &(arr));

    if(!dim)
    {
	ajWarn("Empty list passed to embPdbidToSp");

	return ajFalse;
    }


    if( (idx = ajPdbtospArrFindPdbid(arr, dim, pdb))==-1)
	return ajFalse;

    ajStrAssignS(spr, arr[idx]->Spr[0]);

    return ajTrue;
}




/* @func embPdbidToAcc ********************************************************
**
** Read a pdb identifier code and writes the equivalent accession number.
** Relies on list of Pdbtosp objects sorted by PDB code, which is usually 
** obtained by a call to ajPdbtospReadAllNew.
** 
** @param [r] pdb  [const AjPStr]   Pdb  identifier code
** @param [w] acc  [AjPStr*]  Accession number
** @param [r] list [const AjPList]  Sorted list of Pdbtosp objects
**
** @return [AjBool]  True if a swissprot identifier code was found for the
**                   Pdb code.
**
** @release 2.9.0
** @@
****************************************************************************/

AjBool embPdbidToAcc(const AjPStr pdb, AjPStr *acc, const AjPList list)
{
    AjPPdbtosp *arr = NULL;  /* Array derived from list */
    AjPPdbtosp *arrfree = NULL;
    ajint dim = 0;           /* Size of array */
    ajint idx = 0;           /* Index into array for the Pdb code */

    
    if(!pdb || !list)
    {
	ajWarn("Bad args passed to embPdbidToAcc");

	return ajFalse;
    }
    

    dim = (ajuint) ajListToarray(list, (void ***) &(arr));

    if(!dim)
    {
	ajWarn("Empty list passed to embPdbidToAcc");

	return ajFalse;
    }


    if( (idx = ajPdbtospArrFindPdbid(arr, dim, pdb))==-1)
    {
        arrfree = (AjPPdbtosp*) arr;
	AJFREE(arrfree);

	return ajFalse;
    }

    ajStrAssignS(acc, arr[idx]->Acc[0]);
    arrfree = (AjPPdbtosp*) arr;
    AJFREE(arrfree);

    return ajTrue;
}




/* @func  embPdbidToScop ******************************************************
**
** Writes a list of scop identifier codes for the domains that a Pdb object
** contains.  The domain data is taken from a list of scop objects.
**
** @param [r] pdb             [const AjPPdb]   Pointer to pdb object
** @param [r] list_allscop    [const AjPList]  Pointer to SCOP list of SCOP 
**                                       classification objects  
** @param [w] list_pdbscopids [AjPList*] Pointer to list of scop domain ids
**                                       in the current pdb object
** 
** @return [AjBool] ajTrue on success
**
** @release 2.9.0
** @@
****************************************************************************/

AjBool embPdbidToScop(const AjPPdb pdb, const AjPList list_allscop,
		      AjPList *list_pdbscopids)
{
  
    AjIList iter    = NULL; /* List iterator for SCOP classification list */
    AjPScop ptr     = NULL;
    AjPStr tmpPdbId = NULL;
    AjPStr tmpDomId = NULL;
    ajint found     = 0;

    iter=ajListIterNewread(list_allscop);


    while((ptr=(AjPScop)ajListIterGet(iter)))
    {
	ajStrAssignS(&tmpPdbId, ptr->Pdb);
	ajStrFmtLower(&tmpPdbId);

	if(ajStrMatchS(pdb->Pdb, tmpPdbId))
	{
	    ajStrAssignS(&tmpDomId, ptr->Entry);
	    ajStrFmtLower(&tmpDomId);
	    ajListPushAppend(*list_pdbscopids, tmpDomId);
	    tmpDomId = NULL;
	    found = 1;
	}
    }

    ajListIterDel(&iter);
    ajStrDel(&tmpPdbId);
    ajStrDel(&tmpDomId);
  
    if(found==1) 
	return ajTrue;

    return ajFalse;
}




/* @func embAtomInContact *****************************************************
**
** Determines whether two atoms are in physical contact  
**
** @param [r] atm1   [const AjPAtom]     Atom 1 object
** @param [r] atm2   [const AjPAtom]     Atom 1 object
** @param [r] thresh [float]       Threshold contact distance
** @param [r] vdw    [const AjPVdwall]   Vdwall object
**
** Contact between two atoms is defined as when the van der Waals surface of 
** the first atom comes within the threshold contact distance (thresh) of 
** the van der Waals surface of the second atom.
**
** @return [AjBool] True if the two atoms form contact
**
** @release 2.9.0
** @@
**
****************************************************************************/

AjBool embAtomInContact(const AjPAtom atm1, const AjPAtom atm2, float thresh,
			const AjPVdwall vdw)
{
    float val  = 0.0;
    float val1 = 0.0;



    /* Check args */
    if(!atm1 || !atm2 || !vdw)
    {
	ajWarn("Bad args passed to embAtomInContact");

	return ajFalse;
    }
    
    
    val=((atm1->X -  atm2->X) * (atm1->X -  atm2->X)) +
	((atm1->Y -  atm2->Y) * (atm1->Y -  atm2->Y)) +
	    ((atm1->Z -  atm2->Z) * (atm1->Z -  atm2->Z));


    /*  This calculation uses square root 
    if((sqrt(val) - embVdwRad(atm1, vdw) -
	embVdwRad(atm2, vdw)) <= thresh)
	return ajTrue;
	*/


    /* Same calculation avoiding square root */
    val1 = embVdwRad(atm1, vdw) + embVdwRad(atm2, vdw) + thresh;
    
    if(val <= (val1*val1))
	return ajTrue;


    return ajFalse;
}




/* @func embAtomDistance ******************************************************
**
** Returns the distance (Angstroms) between two atoms.
**
** @param [r] atm1   [const AjPAtom]     Atom 1 object
** @param [r] atm2   [const AjPAtom]     Atom 1 object
** @param [r] vdw    [const AjPVdwall]   Vdwall object
**
** Returns the distance (Angstroms) between the van der Waals surface of two
** atoms.
**
** @return [float] Distance (Angstroms) between two atoms.
**
** @release 2.9.0
** @@
**
****************************************************************************/

float embAtomDistance(const AjPAtom atm1, const AjPAtom atm2,
		      const AjPVdwall vdw)
{
    float val  = 0.0;
    float val1 = 0.0;

    
    val=((atm1->X -  atm2->X) * (atm1->X -  atm2->X)) +
	((atm1->Y -  atm2->Y) * (atm1->Y -  atm2->Y)) +
	    ((atm1->Z -  atm2->Z) * (atm1->Z -  atm2->Z));


    /*  This calculation uses square root */
    val1= (float) (sqrt(val) - embVdwRad(atm1, vdw) - embVdwRad(atm2, vdw));
        
    return val1;
} 




/* ======================================================================= */
/* ============================== Casts ===================================*/
/* ======================================================================= */




/* @section Casts ***********************************************************
**
** These functions examine the contents of an instance and return some
** derived information. Some of them provide access to the internal
** components of an instance. They are provided for programming convenience
** but should be used with caution.
**
****************************************************************************/




/* ======================================================================= */
/* =========================== Reporters ==================================*/
/* ======================================================================= */




/* @section Reporters *******************************************************
**
** These functions return the contents of an instance but do not make any 
** changes.
**
****************************************************************************/




/* @func embVdwRad ************************************************************
**
** Returns the van der Waals radius of an atom. Returns 1.2 as default.
**
** @param [r] atm    [const AjPAtom]     Atom object
** @param [r] vdw    [const AjPVdwall]   Vdwall object
**
** @return [float] van der Waals radius of the atom
**
** @release 2.9.0
** @@
**
****************************************************************************/

float embVdwRad(const AjPAtom atm, const AjPVdwall vdw)
{
    ajuint x = 0U;
    ajuint y = 0U;
    
    for(x = 0U; x < vdw->N; x++)
	for(y = 0; y < vdw->Res[x]->N; y++)
	    if(ajStrMatchS(atm->Atm, vdw->Res[x]->Atm[y]))
		return vdw->Res[x]->Rad[y];
    
    return (float) 1.2;
}




/* @func embPdbToIdx **********************************************************
**
** Reads a Pdb object and writes an integer which gives the index into the 
** protein sequence for a residue with a specified pdb residue number and a 
** specified chain number.
** 
** @param [w] idx [ajint*] Residue number (index into sequence)
** @param [r] pdb [const AjPPdb] Pdb object
** @param [r] res [const AjPStr] Residue number (PDB numbering)
** @param [r] chn [ajuint] Chain number
**
** @return [AjBool] True on succcess (res was found in pdb object)
**
** @release 2.9.0
** @@
****************************************************************************/
AjBool embPdbToIdx(ajint *idx, const AjPPdb pdb, const AjPStr res, ajuint chn)
{
    AjIList  iter = NULL;
    AjPResidue  residue  = NULL;
    
    
    if(!pdb || !(res) || !(idx))
    {
	ajWarn("Bad arg's passed to embPdbToIdx");

	return ajFalse;
    }
    
    if((chn > pdb->Nchn) || (!pdb->Chains) || (chn<1))
    {
	ajWarn("Bad arg's passed to embPdbToIdx");

	return ajFalse;
    }
    

    /* Initialise the iterator */
    iter=ajListIterNewread(pdb->Chains[chn-1]->Residues);


    /* Iterate through the list of residues */
    while((residue = (AjPResidue)ajListIterGet(iter)))
    {
	if(residue->Chn!=chn)
	    continue;
	
	/*
	** Hard-coded to work on model 1
	** Continue / break if a non-protein residue is found or model no. !=1
	*/
	if(residue->Mod!=1)
	    break;

	/* if(residue->Type!='P') 
	    continue; */

	/* If we have found the residue */
	if(ajStrMatchS(res, residue->Pdb))
	{
	    ajListIterDel(&iter);		
	    *idx = residue->Idx;

	    return ajTrue;
	}
    }
        
    ajWarn("Residue number not found in embPdbToIdx");
    ajListIterDel(&iter);		

    return ajFalse;
}




/* ======================================================================= */
/* ========================== Input & Output ============================= */
/* ======================================================================= */




/* @section Input & output **************************************************
**
** These functions are used for formatted input and output to file.    
**
****************************************************************************/




/* ======================================================================= */
/* ======================== Miscellaneous =================================*/
/* ======================================================================= */




/* @section Miscellaneous ***************************************************
**
** These functions may have diverse functions that do not fit into the other
** categories. 
**
****************************************************************************/




/* @func embPdbListHeterogens *************************************************
** 
** Function to create a list of arrays of Atom objects for ligands in the 
** current Pdb object (a single array for each ligand).  An array of int's
** giving the number of Atom objects in each array, is also written. The
** number of ligands is also written.
** 
** @param [r] pdb             [const AjPPdb]   Pointer to pdb object 
** @param [w] list_heterogens [AjPList*] Pointer to list of heterogen Atom
**                                       arrays 
** @param [w] siz_heterogens  [AjPInt*]  Pointer to integer array of sizes
**                                       (number of Atom objects in each 
**                                       array).
** @param [w] nhet            [ajint*]   Number of arrays in the list that
**                                       was written. 
** @param [u] logfile         [AjPFile]  Log file for error messages
**
** @return [AjBool] ajTrue on success
**
** @release 2.9.0
** @@ 
****************************************************************************/

AjBool embPdbListHeterogens(const AjPPdb pdb, AjPList *list_heterogens, 
			    AjPInt *siz_heterogens, ajint *nhet, 
			    AjPFile logfile)
{ 
    /*
    ** NOTE: EVERYTHING IN THE CLEAN PDB FILES IS CURRENTLY CHAIN
    ** ASSOCIATED!  THIS WILL BE CHANGED IN FUTURE
    */
    AjIList iter  = NULL;	/* Iterator for atoms in current pdb object */
    AjPAtom hetat = NULL;		/* Pointer to current Atom object */
    ajuint i = 0U;		/* Counter for chains */
    ajint prev_gpn = -10000; 	/* Group number of atom object from
				   previous iteration */
    AjPList GrpAtmList = NULL; 	/* List to hold atoms from the current 
				   group */
    AjPAtom *AtmArray  = NULL;	/* Array of atom objects */
    ajint n=0;			/* number of elements in AtmArray */
    ajint grp_count = 0;		/* No. of groups */
    ajint arr_count = 0;          /* Index for siz_heterogens */
    
    /* Check args */
    if((pdb==NULL)||(list_heterogens==NULL)||(siz_heterogens==NULL))
    {
        ajWarn("Bad args passed to embPdbListHeterogens\n");

        return ajFalse;
    }
  
    if((!(*list_heterogens))||(!(*siz_heterogens)))
    {
        ajWarn("Bad args passed to embPdbListHeterogens\n");

        return ajFalse;
    }
  
    if(pdb->Ngp>0)
        ajFmtPrintF(logfile, "\tNGP:%d\n", pdb->Ngp);
  
    if(pdb->Nchn>0)
    {      
        for(i = 0U; i < pdb->Nchn; i++) 
        {
            prev_gpn=-100000;	   /* Reset prev_gpn for each chain */
            /* initialise iterator for pdb->Chains[i]->Atoms */
            iter=ajListIterNewread(pdb->Chains[i]->Atoms);

            /* Iterate through list of Atom objects */
            while((hetat=(AjPAtom)ajListIterGet(iter)))
            {		
                /* check for type  */
                if(hetat->Type != 'H')
                    continue;

                /* TEST FOR A NEW GROUP */
                if(prev_gpn != hetat->Gpn) 
                {
                    grp_count++;

                    if(GrpAtmList)
                    {
                        n=(ajuint) (ajListToarray(GrpAtmList, (void ***) &AtmArray));
                        ajListPushAppend(*list_heterogens, AtmArray);
                        /*
                        ** So that ajListToarray doesn't try and free the
                        ** non-NULL pointer
                        */
                        AtmArray=NULL;
                        ajIntPut(siz_heterogens, arr_count, n);
                        (*nhet)++;
                        ajListFree(&GrpAtmList);
                        GrpAtmList=NULL;
                        arr_count++;
                    }		    		    

                    GrpAtmList=ajListNew();
                    prev_gpn=hetat->Gpn;
                } /* End of new group loop */

                ajListPushAppend(GrpAtmList, (AjPAtom) hetat);
            } /* End of list iteration loop */

            /* Free list iterator */
            ajListIterDel(&iter);
	    
        } /* End of chain for loop */

        if(GrpAtmList)
        {
            n=(ajuint) (ajListToarray(GrpAtmList, (void ***) &AtmArray));
            ajListPushAppend(*list_heterogens, AtmArray);
            /*
            ** So that ajListToarray doesn't try and free the non-NULL
            ** pointer
            */
            AtmArray=NULL; 
            ajIntPut(siz_heterogens, arr_count, n);
            (*nhet)++;
            ajListFree(&GrpAtmList);
            GrpAtmList=NULL;
        }
	
        GrpAtmList = NULL;
        prev_gpn   = -10000;  

    } /* End of chain loop */
  
  
    return ajTrue;
}




/* @func embPdbResidueIndexI **************************************************
**
** Reads a Pdb object and writes an integer array which gives the index into 
** the protein sequence for structured residues (residues for which electron
** density was determined) for a given chain. The array length is of course
** equal to the number of structured residues. 
**
** @param [r] pdb [const AjPPdb] Pdb object
** @param [r] chn [ajuint] Chain number
** @param [w] idx [AjPInt*] Index array
**
** @return [AjBool] True on succcess
**
** @release 3.0.0
** @@
****************************************************************************/

AjBool embPdbResidueIndexI(const AjPPdb pdb, ajuint chn, AjPInt *idx)
{
    AjIList  iter = NULL;
    AjPResidue  res  = NULL;
/*    ajint this_rn = 0;
    ajint last_rn = -1000; */
    ajint resn    = 0;  
    
    
    if(!pdb || !(*idx))
    {
	ajWarn("Bad arg's passed to embPdbResidueIndexI");

	return ajFalse;
    }
    
    if((chn > pdb->Nchn) || (!pdb->Chains))
    {
	ajWarn("Bad arg's passed to embPdbResidueIndexI");

	return ajFalse;
    }
    

    /* Initialise the iterator */
    iter=ajListIterNewread(pdb->Chains[chn-1]->Residues);


    /* Iterate through the list of residues */
    while((res=(AjPResidue)ajListIterGet(iter)))
    {
	if(res->Chn!=chn)
	    continue;
	
	/* Hard-coded to work on model 1 */
	/* Continue / break if a non-protein residue is found or 
	   model no. !=1 */
	if(res->Mod!=1)
	    break;
	/*
	   if(res->Type!='P') 
	   continue; */


	/* If we are onto a new residue */
	/*
	this_rn=res->Idx;
	if(this_rn!=last_rn)
	{
	    ajIntPut(&(*idx), resn++, res->Idx);
	    last_rn=this_rn;
	}*/

	ajIntPut(&(*idx), resn++, res->Idx);

    }
        
    if(resn==0)
    {
	ajWarn("Chain not found in embPdbResidueIndexI");
	ajListIterDel(&iter);		
	return ajFalse;
    }
    	
    ajListIterDel(&iter);		

    return ajTrue;
}




/* @func embPdbResidueIndexC **************************************************
**
** Reads a Pdb object and writes an integer array which gives the index into 
** the protein sequence for structured residues (residues for which electron
** density was determined) for a given chain.  The array length is of course 
** equal to the number of structured residues. 
**
** @param [r] pdb [const AjPPdb]  Pdb object
** @param [r] chn [char]  Chain identifier
** @param [w] idx [AjPInt*] Index array
**
** @return [AjBool] True on succcess
**
** @release 3.0.0
** @@
****************************************************************************/

AjBool embPdbResidueIndexC(const AjPPdb pdb, char chn, AjPInt *idx)
{
    ajuint chnn = 0U;
    
    if(!ajPdbChnidToNum(chn, pdb, &chnn))
    {
	ajWarn("Chain not found in embPdbResidueIndexC");

	return ajFalse;
    }
    
    if(!embPdbResidueIndexI(pdb, chnn, idx))
	return ajFalse;

    return ajTrue;
}




/* @func embPdbResidueIndexICA ************************************************
**
** Reads a Pdb object and writes an integer array which gives the index into 
** the protein sequence for structured residues (residues for which electron
** density was determined) for a given chain, EXCLUDING those residues for 
** which CA atoms are missing. The array length is of course equal to the 
** number of structured residues. 
**
** @param [r] pdb  [const AjPPdb] Pdb object
** @param [r] chn  [ajuint] Chain number
** @param [w] idx  [AjPUint*] Index array
** @param [w] nres [ajint*] Array length 
**
** @return [AjBool] True on succcess
**
** @release 3.0.0
** @@
****************************************************************************/

AjBool embPdbResidueIndexICA(const AjPPdb pdb,
                             ajuint chn, AjPUint *idx, ajint *nres)
{
    AjIList iter  = NULL;
    AjPAtom atm   = NULL;
    ajint this_rn = 0;
    ajint last_rn = -1000;
    ajint resn    = 0;     /* Sequential count of residues */
    
    if(!pdb || !(*idx))
    {
	ajWarn("Bad arg's passed to embPdbResidueIndexICA");

	return ajFalse;
    }
    
    if((chn > pdb->Nchn) || (!pdb->Chains))
    {
	ajWarn("Bad arg's passed to embPdbResidueIndexICA");

	return ajFalse;
    }
    

    /* Initialise the iterator */
    iter=ajListIterNewread(pdb->Chains[chn-1]->Atoms);


    /* Iterate through the list of atoms */
    while((atm=(AjPAtom)ajListIterGet(iter)))
    {
	if(atm->Chn!=chn)
	    continue;
	
	/*
	** Hard-coded to work on model 1
	** Continue / break if a non-protein atom is found or model no. !=1
	*/
	if(atm->Mod!=1)
	    break;

	if(atm->Type!='P') 
	    continue;

	/* If we are onto a new residue */
	this_rn=atm->Idx;

	if(this_rn!=last_rn && ajStrMatchC(atm->Atm,  "CA"))
	{
	    ajUintPut(&(*idx), resn++, atm->Idx);
	    last_rn=this_rn;
	}
    }

        
    if(resn==0)
    {
	ajWarn("Chain not found in embPdbResidueIndexICA");
	ajListIterDel(&iter);		

	return ajFalse;
    }
    	
    *nres=resn;
    
    ajListIterDel(&iter);		

    return ajTrue;
}




/* @func embPdbResidueIndexCCA ************************************************
**
** Reads a Pdb object and writes an integer array which gives the index into 
** the protein sequence for structured residues (residues for which electron
** density was determined) for a given chain, EXCLUDING those residues for 
** which CA atoms are missing. The array length is of course equal to the 
** number of structured residues. 
**
** @param [r] pdb [const AjPPdb]  Pdb object
** @param [r] chn [char]    Chain identifier
** @param [w] idx [AjPUint*] Index array
** @param [w] nres [ajint*] Array length 
**
** @return [AjBool] True on succcess
**
** @release 3.0.0
** @@
****************************************************************************/

AjBool embPdbResidueIndexCCA(const AjPPdb pdb, char chn,
			     AjPUint *idx, ajint *nres)
{
    ajuint chnn = 0U;
    
    if(!ajPdbChnidToNum(chn, pdb, &chnn))
    {
	ajWarn("Chain not found in embPdbResidueIndexCCA");

	return ajFalse;
    }
    
    if(!embPdbResidueIndexICA(pdb, chnn, idx, nres))
	return ajFalse;


    return ajTrue;
}




/* @func embStrideToThree *****************************************************
**
** Reads a string that contains an 8-state STRIDE secondary structure 
** assignment and writes a string with the corresponding 3-state assignment.
** The 8 states used in STRIDE are 'H' (alpha helix), 'G' (3-10 helix), 
** 'I' (Pi-helix), 'E' (extended conformation), 'B' or 'b' (isolated bridge),
** 'T' (turn) or 'C' (coil, i.e. none of the above).  The 3 states used
** are 'H' (STRIDE 'H', 'G' or 'I'), 'E' (STRIDE 'E', 'B' or 'b') and 'C'
** (STRIDE 'T' or 'C'). The string is allocated if necessary.
**
** @param [w] to [AjPStr*]  String to write
** @param [r] from [const AjPStr]  String to read
**
** @return [AjBool] True on succcess
**
** @release 2.9.0
** @@
****************************************************************************/

AjBool       embStrideToThree(AjPStr *to, const AjPStr from)
{
    if(!from)
    {
	ajWarn("Bad args passed to embStrideToThree");

	return ajFalse;
    }
    else
	ajStrAssignS(to, from);

    ajStrExchangeKK(to, 'G', 'H');
    ajStrExchangeKK(to, 'I', 'H');
    ajStrExchangeKK(to, 'B', 'E');
    ajStrExchangeKK(to, 'b', 'E');
    ajStrExchangeKK(to, 'T', 'C');

    return ajTrue;
}