Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
GetPDBChainLabels.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file GetPDBChainLabels.c
5 
6  \version V1.13
7  \date 26.03.15
8  \brief
9 
10  \copyright (c) UCL / Dr. Andrew C. R. Martin 1992-2015
11  \author Dr. Andrew C. R. Martin
12  \par
13  Institute of Structural & Molecular Biology,
14  University College London,
15  Gower Street,
16  London.
17  WC1E 6BT.
18  \par
19  andrew@bioinf.org.uk
20  andrew.martin@ucl.ac.uk
21 
22 **************************************************************************
23 
24  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
25  according to the conditions laid out in the accompanying file
26  COPYING.DOC.
27 
28  The code may be modified as required, but any modifications must be
29  documented so that the person responsible can be identified.
30 
31  The code may not be sold commercially or included as part of a
32  commercial product except as described in the file COPYING.DOC.
33 
34 **************************************************************************
35 
36  Description:
37  ============
38 
39 
40 **************************************************************************
41 
42  Usage:
43  ======
44 
45 **************************************************************************
46 
47  Revision History:
48  =================
49 - V1.0 22.02.94 Original release
50 - V1.1 23.05.94 Added FindNextChainPDB()
51 - V1.2 05.10.94 KillSidechain() uses BOOL rather than int
52 - V1.3 24.07.95 Added TermPDB()
53 - V1.4 25.07.95 Added GetPDBChainLabels()
54 - V1.5 26.09.95 Fixed bug in TermPDB()
55 - V1.6 12.10.95 Added DupePDB(), CopyPDBCoords()
56 - V1.7 23.10.95 Moved FindResidueSpec() to ParseRes.c
57 - V1.8 10.01.96 Added ExtractZonePDB()
58 - V1.9 14.03.96 Added FindAtomInRes()
59 - V1.10 08.10.99 Initialised some variables
60 - V1.11 25.03.14 Deprecated GetPDBChainLabels() and added replacement
61  function blGetPDBChainLabels() By: CTP
62 - V1.12 31.07.14 Moved GetPDBChainLabels() to dperecated.c By: CTP
63 - V1.13 26.03.15 blGetPDBChainLabels() no longer assumes all instances
64  of one label occur together By: ACRM
65 
66 *************************************************************************/
67 /* Doxygen
68  -------
69  #GROUP Handling PDB Data
70  #SUBGROUP Extracting data
71  #FUNCTION blGetPDBChainLabels()
72  Scans a PDB linked list for chain names. Allocates memory for an
73  array of strings containing these labels which is returned together
74  with the number of chains found.
75 */
76 /************************************************************************/
77 /* Includes
78 */
79 #include <stdlib.h>
80 
81 #include "pdb.h"
82 #include "macros.h"
83 
84 /************************************************************************/
85 /* Defines and macros
86 */
87 
88 /************************************************************************/
89 /* Globals
90 */
91 
92 /************************************************************************/
93 /* Prototypes
94 */
95 
96 
97 /************************************************************************/
98 /*>char **blGetPDBChainLabels(PDB *pdb, int *nChains)
99  --------------------------------------------------
100 *//**
101 
102  \param[in] *pdb PDB linked list
103  \param[out] nChains Number of chains found.
104  \return Allocated array of strings containing chain
105  labels. NULL if unable to allocate memory.
106 
107  Scans a PDB linked list for chain names. Allocates memory for an
108  array of strings containing these labels which is returned together
109  with the number of chains found.
110 
111  N.B. You must free the allocated memory for both the array of chains
112  and for each individual chain label when you've finished with it!
113 
114 - 25.03.14 Original based on GetPDBChainLabels(). By: CTP
115 - 26.03.15 No longer assumes chain labels do not appear again later
116  (i.e. chains L,H,Y,L only returns L,H,Y) By: ACRM
117 */
118 char **blGetPDBChainLabels(PDB *pdb, int *nChains)
119 {
120  char **chains = NULL,
121  prevChain[8];
122  PDB *p;
123 
124  /* Initialize previous chain and zero chain count */
125  prevChain[0] = '\0';
126  *nChains = 0;
127 
128  /* Just return if linked list is NULL */
129  if(pdb == NULL)
130  return(NULL);
131 
132  /* Allocate memory for first chain */
133  if((chains = (char **)malloc(sizeof(char *)))==NULL)
134  return(NULL);
135  if((chains[0] = (char *)malloc(8 * sizeof(char)))==NULL)
136  return(NULL);
137  *nChains = 1;
138 
139  /* Copy in the first chain */
140  strncpy(chains[0], pdb->chain, 8);
141  strncpy(prevChain, pdb->chain, 8);
142 
143  /* Run through the pdb linked list */
144  for(p=pdb; p!=NULL; NEXT(p))
145  {
146  /* If chain label has changed */
147  if(!CHAINMATCH(p->chain, prevChain))
148  {
149  BOOL found = FALSE;
150  int chainNum;
151 
152  strncpy(prevChain, p->chain, 8);
153 
154  /* See if this chain has appeared before */
155  for(chainNum=0; chainNum<(*nChains); chainNum++)
156  {
157  if(CHAINMATCH(p->chain, chains[chainNum]))
158  {
159  found = TRUE;
160  break;
161  }
162  }
163  /* If it hasn't appeared before, allocate memory to store it
164  and copy in the new label
165  */
166  if(!found)
167  {
168  if((chains =
169  (char **)realloc(chains, (*nChains + 1)*sizeof(char *)))
170  == NULL)
171  return(NULL);
172 
173  if((chains[*nChains] =
174  (char *)malloc(8 * sizeof(char))) == NULL)
175  return(NULL);
176 
177  strncpy(chains[*nChains],p->chain, 8);
178  (*nChains)++;
179  }
180  }
181  }
182 
183  return(chains);
184 }
Include file for PDB routines.
short BOOL
Definition: SysDefs.h:64
#define NULL
Definition: array2.c:99
Definition: pdb.h:298
#define FALSE
Definition: macros.h:223
#define NEXT(x)
Definition: macros.h:249
Useful macros.
#define TRUE
Definition: macros.h:219
#define CHAINMATCH(chain1, chain2)
Definition: pdb.h:495
char chain[blMAXCHAINLABEL]
Definition: pdb.h:321
char ** blGetPDBChainLabels(PDB *pdb, int *nChains)