File: pdb_ripper_2.cpp

package info (click to toggle)
mustang 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 932 kB
  • ctags: 415
  • sloc: cpp: 7,870; makefile: 91; sh: 18
file content (464 lines) | stat: -rw-r--r-- 13,148 bytes parent folder | download | duplicates (2)
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
/*******************************************************************************
 * Copyright (c) 2005, Arun S Konagurthu, The University of Melbourne.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification are permitted provided that the following conditions are met:
 * 
 * (1) Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * (2) Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * (3) Neither the name of the University of Melbourne nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESSINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/
#include <iostream>
using std::cin; 	using std::cout; 	using std::cerr; 
using std::flush; 	using std::endl; 	using std::ios;
using std::fixed;


#include <iomanip>
using std::setprecision ; using std::setw ; 

#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using std::ifstream ;

#include "macros.h"
#include "globals.h"
#include "pdb_ripper.h"
#include "alloc_routines.h"
#include "init_routines.h"
int CHECK_NCHAINS_IN_PDB( char *fname )
{
	int NCHAINS = 0 ;
	int chain_flag = OFF ;
	int chain_id = -1;
	char buffer[90];
	ifstream pdb( fname , ios::in ) ;
	if( !pdb )
	{
       		cerr << "Pdb File Error: Structure " << fname << " does not exist in the path specified.\n" ; 
		pdb.close();
       		exit(1) ;
       	}
	while ( !pdb.eof() )
	{
		pdb.getline( buffer , 90 ) ;
		if( pdb.eof() || 
		    (buffer[0] == 'E' && buffer[1] == 'N' && buffer[2] == 'D' && buffer[3] == ' ') 
		  ) break ;

		if( buffer[0] == 'A' && buffer[1] == 'T' && buffer[2] == 'O' && buffer[3] == 'M' && chain_flag == OFF )
		{
			chain_flag = ON ;
			chain_id = buffer[ 21 ] ;
		}
			
		if( chain_flag == ON && buffer[0] == 'T' && buffer[1] == 'E' && 
		    buffer[2] == 'R' && buffer[3] == ' ' /*&& buffer[ 21 ] == chain_id */
		  )
		{
			chain_flag = OFF ;
			NCHAINS++ ;
		}
	}
	return NCHAINS ;
}
int CHECK_NRESIDUES_IN_PDB( char *fname )
{
	int NRESIDUES = 0 ;
	char buffer[90];
	ifstream pdb( fname , ios::in ) ;
	if( !pdb )
	{
       		cerr << "Pdb File Error: Structure " << fname << " does not exist in the path specified.\n" ; 
		pdb.close();
       		exit(1) ;
       	}
	char prev_res_num[8] = "" ;
	char curr_res_num[8] = "" ;
	while ( !pdb.eof() )
	{
		pdb.getline( buffer , 90 ) ;
		
		
		if( pdb.eof() || 
		    (buffer[0] == 'E' && buffer[1] == 'N' && buffer[2] == 'D' && buffer[3] == ' ' )
		  ) break ;

		if( buffer[0] == 'T' && buffer[1] == 'E' && buffer[2] == 'R' && buffer[3] == ' ' )
			break ;
		
		if( buffer[0] == 'A' && buffer[ 1 ] == 'T' && buffer[ 2 ] == 'O' && buffer[ 3 ] == 'M' )
			if( ( buffer[13] == 'N' && buffer[14] == ' ' ) ||
			    ( buffer[13] == 'C' && buffer[14] == 'A' ) ||
			    ( buffer[13] == 'C' && buffer[14] == ' ' ) ||
			    ( buffer[13] == 'O' && buffer[14] == ' ' ) )
			{
				for( int i = 22 , j = 0 ; i < 28 ; i++ , j++ ) curr_res_num[j] = buffer[i] ;
				curr_res_num[6] = '\0' ;
				if( strcmp(curr_res_num, prev_res_num) ) NRESIDUES++ ;
			}
		strcpy( prev_res_num , curr_res_num ) ;
		
	}
	return NRESIDUES ;
}

void PARSE_PDB_STRUCTURE( char *fname , int Sindx ) 
{
	if(!meditate) 
		cout << "Parsing the PDB file: \033[35m" << setw(15) << struct_names[Sindx] << "\033[0m";
	
	PROT_SIZES[Sindx] = 0 ;
	//char strbuffer[4][90];
	char buffer[90] ;
	char tempstr[20] = "" ;
	char temp_name[4] ;
	char temp_num[10] ;
	float temp_coord[3] ;
	float temp_occupancy ;
	int index = 0 ;
	//char prev_num[9] = "-1";
	int atom_handle = 0 ;

	ifstream pdb( fname , ios::in ) ;
	if( !pdb )
	{
		cerr << "Pdb File Error: Structure " << fname << " does not exist in the path specified.\n" ; 
		pdb.close();
		exit(1) ;
	}

	while ( !pdb.eof() )
	{
		pdb.getline( buffer , 90 ) ;
		if ( (buffer[0] == 'T' && buffer[1] == 'E' && buffer[2] == 'R')  ) break ;
		if ( (buffer[0] != 'A' || buffer[1] != 'T' || buffer[2] != 'O' || buffer[3] != 'M')  ) continue ;
		else if( 
				( buffer[13] == 'N' && buffer[14] == ' ' ) || 
				( buffer[13] == 'C' && buffer[14] == 'A' ) || 
				( buffer[13] == 'C' && buffer[14] == ' ' ) || 
				( buffer[13] == 'O' && buffer[14] == ' ' )  
		       )
		{
			     if( buffer[13] == 'N' && buffer[14] == ' ' ) atom_handle = 0 ;
			else if( buffer[13] == 'C' && buffer[14] == 'A' ) atom_handle = 1 ;
			else if( buffer[13] == 'C' && buffer[14] == ' ' ) atom_handle = 2 ;
			else if( buffer[13] == 'O' && buffer[14] == ' ' ) atom_handle = 3 ;


			
			for ( int i = 17 ,  j = 0 ; i < 20 ; i++ )
			{
				tempstr[ j++ ] = buffer[ i ] ;
				if ( i == (20 - 1) )
					tempstr[ j ] = '\0' ;
			}
			sscanf( tempstr , "%s" , temp_name ) ;
			
			for ( int i = 22 ,  j = 0 ; i < 28 ; i++ )
			{
				tempstr[ j++ ] = buffer[ i ] ;
				if( i == (28 - 1) )
					tempstr[ j ] = '\0' ;
			}
			//sscanf( tempstr , "%d" , &temp_num ) ;
			strcpy( temp_num , tempstr) ;
			
			for ( int i = 54 ,  j = 0 ; i < 60 ; i++ )
			{
				tempstr[ j++ ] = buffer[ i ] ;
				if( i == (60 - 1) )
					tempstr[ j ] = '\0' ;
			}
			//sscanf( tempstr , "%d" , &temp_num ) ;
			sscanf( tempstr , "%f" , &temp_occupancy ) ;


			//X coord of  CA 
			for ( int i = 30 ,  j = 0 ; i < 38 ; i++ )
			{
				tempstr[ j++ ] = buffer[ i ] ;
				if( i == (38 - 1) )
					tempstr[ j ] = '\0' ;
			}
			sscanf( tempstr , "%f" , &temp_coord[0] ) ;

			//Y coord of  CA
			for ( int i = 38 ,  j = 0 ; i < 46 ; i++ )
			{
				tempstr[ j++ ] = buffer[ i ] ;
				if( i == (46 - 1) )
					tempstr[ j ] = '\0' ;
			}
			sscanf( tempstr , "%f" , &temp_coord[1] ) ;

			//Z coord of  CA
			for ( int i = 46 ,  j = 0 ; i < 54 ; i++ )
			{
				tempstr[ j++ ] = buffer[ i ] ;
				if( i == (54 - 1) )
					tempstr[ j ] = '\0' ;
			}
			sscanf( tempstr , "%f" , &temp_coord[2] ) ;
		
			if( index != 0 && (!strcmp( PROT[Sindx][index-1].res_num , temp_num )) )
			{
				if( temp_occupancy > PROT[Sindx][index-1].backbone_occupancies[atom_handle]+float_epsilon )
				{
					if( atom_handle == 0 )
					{
						for( int i = 0 ; i < 3 ; i++ )
							PROT[Sindx][index-1].N_coords[i]  = temp_coord[i] ;
					}
					else if( atom_handle == 1 )
					{
						for( int i = 0 ; i < 3 ; i++ )
							PROT[Sindx][index-1].CA_coords[i]  = temp_coord[i] ;
					}	
					else if( atom_handle == 2 )
					{
						for( int i = 0 ; i < 3 ; i++ )
							PROT[Sindx][index-1].C_coords[i]  = temp_coord[i] ;
					}
					else if( atom_handle == 3 )
					{
						for( int i = 0 ; i < 3 ; i++ )
							PROT[Sindx][index-1].O_coords[i]  = temp_coord[i] ;
					}
					PROT[Sindx][index-1].backbone_occupancies[atom_handle] = temp_occupancy ;
				}
			}
			else
			{

				//fillin in the residue number(with insertion code) as it appears in PDB
				strcpy( PROT[Sindx][index].res_num , temp_num ) ;
				PROT[Sindx][ index ].res_indx = index ;
			
				// associating single letter code for this residue
				for( int i = 0 ; i < 23 ; i++ )
				{
					if( !strcmp(res_names[i].three_letter_code , temp_name) )
					{
						PROT[Sindx][index].res_name = res_names[i].single_letter_code ;
						break;
					}
				}
				if( atom_handle == 0 )
				{
					for( int i = 0 ; i < 3 ; i++ )
						PROT[Sindx][index].N_coords[i]  = temp_coord[i] ;
				}
			        else if( atom_handle == 1 )
				{
					for( int i = 0 ; i < 3 ; i++ )
						PROT[Sindx][index].CA_coords[i]  = temp_coord[i] ;
				}	
				else if( atom_handle == 2 )
				{
					for( int i = 0 ; i < 3 ; i++ )
						PROT[Sindx][index].C_coords[i]  = temp_coord[i] ;
				}
				else if( atom_handle == 3 )
				{
					for( int i = 0 ; i < 3 ; i++ )
						PROT[Sindx][index].O_coords[i]  = temp_coord[i] ;
				}
				index++ ;
				PROT_SIZES[Sindx]++ ;
				//cout << index << endl ;
			}
		}
	}

		
	if(!meditate)
	{
		cout << "  (...contains \033[1m" << setw(4) << PROT_SIZES[Sindx] << "\033[0m residues.)";
		cout << "  [ \033[32;4mOK\033[0m ]\n" ;

	}
	
}

/*..........................................................................*/
void PARSE_ENTIRE_PDB_STRUCTURE( char *fname , int Sindx ) 
{
	char strbuffer[90];
	char tempstr[20] = "" ;
	char temp_atom_name[6] ;
	char temp_res_name[4] ;
	char temp_res_num[6] ;
	float temp_coord[3] ;
	float temp_occupancy;
	float temp_bfactor;
	int index = 0 ;
	char prev_res_name[6]="";
	struct Complete_pdb_info *pdb_trav = NULL ;
	ifstream pdb( fname , ios::in ) ;
	if ( !pdb )
	{
		cerr << "\nError in opening the PDB:" <<  fname << endl;
		exit( 1 ) ;
	}
	while ( !pdb.eof() )
	{
		pdb.getline( strbuffer , 90 ) ;
		//cout << (int)strbuffer[0] << endl ;
		if ( strbuffer[ 0 ] == 'T' && strbuffer[ 1 ] == 'E' && strbuffer[ 2 ] == 'R' )
			break ;
		if ( strbuffer[ 0 ] != 'A' || strbuffer[ 1 ] != 'T' || strbuffer[ 2 ] != 'O' || strbuffer[ 3 ] != 'M' )
			continue;
		
		if( index == 0 && COMPLETE_PDBs[Sindx][index] == NULL )
		{
			COMPLETE_PDBs[Sindx][index] =  new struct Complete_pdb_info ;
			pdb_trav = COMPLETE_PDBs[Sindx][index] ;
			pdb_trav -> link =  NULL ;
			for ( int i = 22 ,  j = 0 ; i < 27 ; i++ )
			{
				prev_res_name[ j++ ] = strbuffer[ i ] ;
				if( i == (27 - 1) )
					prev_res_name[ j ] = '\0' ;
			}
			PDB_SIZES[Sindx]++ ;
		}
		else
		{
			for ( int i = 22 ,  j = 0 ; i < 27 ; i++ )
			{
				tempstr[ j++ ] = strbuffer[ i ] ;
				if( i == (27 - 1) )
					tempstr[ j ] = '\0' ;
			}
			if( strcmp( prev_res_name , tempstr ) == 0 ) // same residue
			{
				pdb_trav -> link = new struct Complete_pdb_info ;
				pdb_trav = pdb_trav -> link ;
				pdb_trav -> link =  NULL ;
			}
			else // not same
			{
				index++ ;
				PDB_SIZES[Sindx]++ ;
				//if( index > PROT_SIZES[Sindx] ) // HERE!!!use if statement on PROT_SIZE to execute a breakpoint
				//	cout << index << " " << strbuffer << endl ;
				//cout << index << endl ;
				COMPLETE_PDBs[Sindx][index] =  new struct Complete_pdb_info ;
				pdb_trav = COMPLETE_PDBs[Sindx][index] ;
				pdb_trav -> link =  NULL ;
				for ( int i = 22 ,  j = 0 ; i < 27 ; i++ )
				{
					prev_res_name[ j++ ] = strbuffer[ i ] ;
					if( i == (27 - 1) )
						prev_res_name[ j ] = '\0' ;
				}

			}

		}
		// fill in pdb_info into pdb_trav

		//atom_name
		for ( int i = 12 ,  j = 0 ; i < 17 ; i++ )
		{
			tempstr[ j++ ] = strbuffer[ i ] ;
			if ( i == (17 - 1) )
				tempstr[ j ] = '\0' ;
		}
		strcpy(temp_atom_name, tempstr ) ;
			
		//residue	
		for ( int i = 17 ,  j = 0 ; i < 20 ; i++ )
		{
			tempstr[ j++ ] = strbuffer[ i ] ;
			if ( i == (20 - 1) )
				tempstr[ j ] = '\0' ;
		}
		sscanf( tempstr , "%s" , temp_res_name ) ;
		//residue_num
		
		for ( int i = 22 ,  j = 0 ; i < 27 ; i++ )
		{
			tempstr[ j++ ] = strbuffer[ i ] ;
			if( i == (27 - 1) )
				tempstr[ j ] = '\0' ;
		}
		sscanf( tempstr , "%s" , temp_res_num ) ;
		//X coord of CA
		for ( int i = 30 ,  j = 0 ; i < 38 ; i++ )
		{
			tempstr[ j++ ] = strbuffer[ i ] ;
			if( i == (38 - 1) )
				tempstr[ j ] = '\0' ;
		}
		sscanf( tempstr , "%f" , &temp_coord[0] ) ;

		//Y coord of CA
		for ( int i = 38 ,  j = 0 ; i < 46 ; i++ )
		{
			tempstr[ j++ ] = strbuffer[ i ] ;
			if( i == (46 - 1) )
				tempstr[ j ] = '\0' ;
		}
		sscanf( tempstr , "%f" , &temp_coord[1] ) ;

		//Z coord of CA
		for ( int i = 46 ,  j = 0 ; i < 54 ; i++ )
		{
			tempstr[ j++ ] = strbuffer[ i ] ;
			if( i == (54 - 1) )
				tempstr[ j ] = '\0' ;
		}
		sscanf( tempstr , "%f" , &temp_coord[2] ) ;
		//occupancy
		for ( int i = 54 ,  j = 0 ; i < 60 ; i++ )
		{
			tempstr[ j++ ] = strbuffer[ i ] ;
			if( i == (60 - 1) )
				tempstr[ j ] = '\0' ;
		}
		sscanf( tempstr , "%f" , &temp_occupancy ) ;
		//B-factor
		for ( int i = 60 ,  j = 0 ; i < 66 ; i++ )
		{
			tempstr[ j++ ] = strbuffer[ i ] ;
			if( i == (66 - 1) )
				tempstr[ j ] = '\0' ;
		}
		sscanf( tempstr , "%f" , &temp_bfactor ) ;


		//finally fillin the pdb_info datastructs
		strcpy( pdb_trav -> atom_name , temp_atom_name );
		strcpy( pdb_trav -> residue , temp_res_name );
		strcpy( pdb_trav -> residue_num , temp_res_num );

		for( int i = 0 ; i < 3 ; i++ )
			pdb_trav -> coords[i] = temp_coord[i] ;
		
		pdb_trav -> occupancy = temp_occupancy ;
		pdb_trav -> B_factor = temp_bfactor ;
	}
	pdb.close() ;
}