The outage for Sunday 24th November has been cancelled.
Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
FNam2PDB.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file FNam2PDB.c
5 
6  \version V1.2
7  \date 07.07.14
8  \brief Extract a PDB code from a filename
9 
10  \copyright (c) Dr. Andrew C. R. Martin 1995
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 24.07.95 Original
50 - V1.1 26.07.95 Fixed a NULL to '\0'
51 - V1.2 07.07.14 Use bl prefix for functions By: CTP
52 
53 *************************************************************************/
54 /* Doxygen
55  -------
56  #GROUP Handling PDB Data
57  #SUBGROUP File IO
58  #FUNCTION blFNam2PDB()
59  Attempts to extract a PDB code from a filename
60 */
61 /************************************************************************/
62 /* Includes
63 */
64 #include <string.h>
65 #include <stdlib.h>
66 #include <ctype.h>
67 #include "macros.h"
68 
69 /************************************************************************/
70 /* Defines and macros
71 */
72 
73 /************************************************************************/
74 /* Globals
75 */
76 
77 /************************************************************************/
78 /* Prototypes
79 */
80 
81 /************************************************************************/
82 /*>char *blFNam2PDB(char *filename)
83  --------------------------------
84 *//**
85 
86  \param[in] *filename A PDB filename containing a PDB code
87  \return The PDB code (lower case)
88  NULL if memory allocation fails
89  XXXX if filename blank or NULL or if
90  unable to find PDB code
91 
92  This routine attempts to convert a filename stem to a PDB code.
93 
94  All the following inputs should produce the same output
95  of 1fbj:
96 
97 
98  $A:[PDB]PDB1FBJ.ENT
99  C:\P1FBJ.PDB
100  /pdb/p1fbj.pdb
101  /pdb/pdb1fbj.pdb
102  1fbj.pdb
103  1fbjL
104 
105  The routine first removes characters from the start of the filename up
106  to the last : ] / or \. It then searches for the following possible
107  patterns (where N is a digit and X is an alphanumeric)
108 
109 
110  pdbNXXX
111  pNXXX
112  NXXX
113  XXXX.pdb
114  XXXX.ent
115 
116  This should cover just about any filename which includes a legal PDB
117  code.
118 
119 - 24.07.95 Original By: ACRM
120 - 26.07.95 Corrected a NULL to '\0'
121 - 07.07.14 Use bl prefix for functions By: CTP
122 */
123 char *blFNam2PDB(char *filename)
124 {
125  int length,
126  pos;
127  char *temp = NULL,
128  *p,
129  *start;
130  static char pdbcode[8];
131 
132  /* Check real string given */
133  if(filename != NULL)
134  {
135  /* Check length not zero */
136  if((length = strlen(filename))!=0)
137  {
138  /* Make a temporary copy of the string */
139  if((temp = (char *)malloc((length+1)*sizeof(char)))==NULL)
140  return(NULL);
141  strcpy(temp,filename);
142 
143  /* Downcase the copy */
144  LOWER(temp);
145 
146  /* Step from the end of string to char after any : ] / or \ */
147  start = temp;
148  for(pos=length-1; pos>=0; pos--)
149  {
150  if((temp[pos] == ':') ||
151  (temp[pos] == ']') ||
152  (temp[pos] == '/') ||
153  (temp[pos] == '\\'))
154  {
155  start = temp+pos+1;
156  break;
157  }
158  }
159 
160  /* Search for `pdb' followed by a digit & 3 alphanumerics */
161  p=start;
162  for(;;)
163  {
164  if((p=strstr(p,"pdb"))!=NULL)
165  {
166  if(isdigit(p[3]) &&
167  isalnum(p[4]) &&
168  isalnum(p[5]) &&
169  isalnum(p[6]))
170  {
171  p[7] = '\0';
172  strcpy(pdbcode, p+3);
173  free(temp);
174  return(pdbcode);
175  }
176  else
177  {
178  p++;
179  }
180  }
181  else
182  {
183  break;
184  }
185  }
186 
187  /* Search for `p' followed by a digit & 3 alphanumerics */
188  p=start;
189  for(;;)
190  {
191  if((p=strstr(p,"p"))!=NULL)
192  {
193  if(isdigit(p[3]) &&
194  isalnum(p[4]) &&
195  isalnum(p[5]) &&
196  isalnum(p[6]))
197  {
198  p[7] = '\0';
199  strcpy(pdbcode, p+3);
200  free(temp);
201  return(pdbcode);
202  }
203  else
204  {
205  p++;
206  }
207  }
208  else
209  {
210  break;
211  }
212  }
213 
214  /* Search for digit followed by 3 alphanumerics */
215  for(p=start; *p!='\0'; p++)
216  {
217  if(isdigit(p[0]) &&
218  isalnum(p[1]) &&
219  isalnum(p[2]) &&
220  isalnum(p[3]))
221  {
222  p[4] = '\0';
223  strcpy(pdbcode, p);
224  free(temp);
225  return(pdbcode);
226  }
227  }
228 
229  /* Search for 4 characters before .pdb */
230  if((p=strstr(start,".pdb"))!=NULL)
231  {
232  p -= 4;
233  if(p>=start)
234  {
235  p[4] = '\0';
236  strcpy(pdbcode, p);
237  free(temp);
238  return(pdbcode);
239  }
240  }
241 
242  /* Search for 4 characters before .ent */
243  if((p=strstr(start,".ent"))!=NULL)
244  {
245  p -= 4;
246  if(p>=start)
247  {
248  p[4] = '\0';
249  strcpy(pdbcode, p);
250  free(temp);
251  return(pdbcode);
252  }
253  }
254 
255  /* Give up! */
256  strcpy(pdbcode,"XXXX");
257  free(temp);
258  return(pdbcode);
259  }
260  }
261 
262  strcpy(pdbcode,"XXXX");
263  return(pdbcode);
264 }
#define NULL
Definition: array2.c:99
char * blFNam2PDB(char *filename)
Definition: FNam2PDB.c:123
Useful macros.
#define LOWER(x)
Definition: macros.h:399