This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: g++ BUG?



Thanks!  Now I need ONLY ONE more piece of advice.  I successfully compile
code with g++ originating from 1) old gcc originally on dec-alpha and 
2) newly installed gcc-3.0.1 ... and get drastically different results.
Method 1 yields working code with usable results; method 2 yields working
code with unusable results.  I have included 4 files to help you quickly
see the problem:

	read_pdb.c    - source
	Test.pdb      - data
	LOG_gcc-2.x   - result with old g++ (A)
	LOG_gcc-3.x   - result with new g++ (B)

The goal is to read and print ATOM records in a PDB file (formatted ascii
file of atomic coordinates).  

Simply,   %g++ read_pdb.c
          %a.out Test.pdb

That's it.  So, LOG_gcc-2.x shows success and is almost a mirror of the
Test.pdb.  As you will see, LOG_gcc-3.x does not.  I am convinced that
this is just ONE fundamental problem I do not understand (e.g. building
gcc-3.0.1 wrong, or incorrectly assuming g++ defaults that have changed,
or ...?).

Can you help?


Mark Wall
HHMI/UT Southwestern Med. Ctr.
/*
**
**	25.6.2001	Mark Wall
**	C++
**
**	"read_pdb_file()" will return a structure containing all the
**	protein coordinate data in a PDB file.  
**
**
**		An example use of this function is included in main(), and
**		all includes, defines, typedefs and function prototypes  
**		should go in a common header file when used in a larger 
**		program.
**
*/

//#include "read_pdb.h"


#define PDB_INDEX  1
#define RECORD_MAX  90
#define FIELD_MAX 12

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>

typedef struct {
	int    num_of_atoms;	// total number of residues
	int    num_of_res;	// total number of residues
	int    *res_num_list;	// array (#res x 1) residue number
	char   **res_seq_list;	// array (#res x 1) residue type
	int    *atom_num;	// array (#atom x 1) atom number
	char   **atom_id;	// array (#atom x 1) atom label
	char   **res_id;	// array (#atom x 1) residue type
	char   *chain_id;	// array (#atom x 1) chain identifier
	int    *res_num;	// array (#atom x 1) residue number
	double *x;		// array (#atom x 1) x coordinate
	double *y;		// array (#atom x 1) y coordinate
	double *z;		// array (#atom x 1) z coordinate
	double *occ;		// array (#atom x 1) atomic occupancy
	double *b;		// array (#atom x 1) temperature factor
} coord_struct;

void read_pdb_file(coord_struct &pdb, char *pdb_file_name);


main (int argc, char *argv[]) {
	coord_struct  pdb;
	int    i;
	
	if (argc < PDB_INDEX+1) {
		std::cout <<"Usage: prompt> "<<argv[0]<<" file.pdb\n";
		exit(1);
	}
	read_pdb_file(pdb, argv[PDB_INDEX]);
	for (i=0; i<pdb.num_of_atoms; ++i) 
		std::cout <<"ATOM   "
		     <<setiosflags(std::ios::right)<<std::setw(4)<<pdb.atom_num[i]<<"  "
		     <<setiosflags(std::ios::left)<<std::setw(4)<<pdb.atom_id[i]
		     <<setiosflags(std::ios::left)<<std::setw(4)<<pdb.res_id[i]
		     <<std::setw(1)<<pdb.chain_id[i]
		     <<setiosflags(std::ios::right)<<std::setw(4)<<pdb.res_num[i]<<"    "
		     <<setiosflags(std::ios::fixed)
		     <<std::setprecision(3)<<std::setw(8)<<pdb.x[i]
		     <<std::setprecision(3)<<std::setw(8)<<pdb.y[i]
		     <<std::setprecision(3)<<std::setw(8)<<pdb.z[i]
		     <<std::setprecision(2)<<std::setw(6)<<pdb.occ[i]
		     <<std::setprecision(2)<<std::setw(6)<<pdb.b[i]<< std::endl;

//	for (i=0; i<pdb.num_of_res; ++i)
//		std::cout << std::setw(5) << pdb.res_seq_list[i]
//		     << std::setw(4) << pdb.res_num_list[i] << std::endl;
		     
	return(0);
}


/*--------------------------------------------*/
/*  Read protein coordinate data in pdb file  */
/*--------------------------------------------*/
void read_pdb_file(coord_struct &pdb, char *pdb_file_name)
{
	std::ifstream      pdb_file;		 // stream name for input PDB file
	char          dummy[RECORD_MAX], // temporary C string for reading fields
		      tmp_id[FIELD_MAX]; // temporary C string, checks for "CA"
	register int  i,		 // fast access iterator
		      j,		 // residue iterator when reading all atoms
		      last;		 // control for residue iterator j

	pdb.num_of_atoms=0;
	pdb.num_of_res=0;
	
	/*-----------------------------------------------*/
	/*   Read number of protein atoms and residues   */
	/*-----------------------------------------------*/
	pdb_file.open(pdb_file_name,std::ios::in);
	if (pdb_file.bad()) {
		std::cerr <<"Error: Unable to open "<<pdb_file_name<<std::endl;
		exit (2);
	}
	while ((pdb_file >> dummy)) {
		if (!std::strncmp(dummy,"ATOM",4)) {
			++pdb.num_of_atoms;
			pdb_file >> dummy >> tmp_id;
			if (!std::strncmp(tmp_id,"CA",2 ))
				++pdb.num_of_res;
		}
		pdb_file.getline(dummy,RECORD_MAX);
	}
	pdb_file.close();

	if (pdb.num_of_atoms==0 || pdb.num_of_res==0) {
		std::cerr << "No usable residues in PDB file.\n";
		exit (1);
	}

	/*--------------------------------------------------*/
	/*  Read all atoms after allocating memory for,     */  // change later to
	/*    the data structure,                           */  // fill a Coord_class
	/*--------------------------------------------------*/
	pdb.atom_num     = new int   [pdb.num_of_atoms];
	pdb.atom_id      = new char *[pdb.num_of_atoms];
	pdb.res_id       = new char *[pdb.num_of_atoms];
	for (i=0; i<pdb.num_of_atoms; ++i) {
		pdb.atom_id[i]   = new char [5];
		pdb.res_id[i]    = new char [5];
	}
	pdb.chain_id     = new char  [pdb.num_of_atoms+1];
	pdb.res_num_list = new int   [pdb.num_of_res];
	pdb.res_seq_list = new char *[pdb.num_of_res];
	for (i=0; i<pdb.num_of_res; ++i)
		pdb.res_seq_list[i] = new char [5];

	pdb.res_num = new int    [pdb.num_of_atoms];
	pdb.x       = new double [pdb.num_of_atoms];
	pdb.y       = new double [pdb.num_of_atoms];
	pdb.z       = new double [pdb.num_of_atoms];
	pdb.occ     = new double [pdb.num_of_atoms];
	pdb.b       = new double [pdb.num_of_atoms];

	i=j=last=0;
	pdb_file.open(pdb_file_name,std::ios::in);
	while (pdb_file >> dummy) {
		if (std::strncmp(dummy,"ATOM",4)) {
			pdb_file.getline(dummy,RECORD_MAX);
		}
		else {
			pdb_file.ignore(3);
			pdb_file.get(dummy,5); pdb.atom_num[i]=std::atoi(dummy);
			pdb_file.ignore(2);
			pdb_file.get(pdb.atom_id[i],5);
			pdb_file.get(pdb.res_id[i],5);
			pdb_file.get(pdb.chain_id[i]);
			pdb_file.get(dummy,5); pdb.res_num[i]=std::atoi(dummy);
			pdb_file.ignore(4);
			pdb_file.get(dummy,9); pdb.x[i] = std::atof(dummy);
			pdb_file.get(dummy,9); pdb.y[i] = std::atof(dummy);
			pdb_file.get(dummy,9); pdb.z[i] = std::atof(dummy);
			pdb_file.get(dummy,7); pdb.occ[i]=std::atof(dummy);
			pdb_file.get(dummy,7); pdb.b[i] = std::atof(dummy);
			pdb_file.getline(dummy,RECORD_MAX);
			if (pdb.res_num[i] != last) {
				std::strcpy(pdb.res_seq_list[j],pdb.res_id[i]);
				pdb.res_num_list[j] = last = pdb.res_num[i];
				j++;
			}
			i++;
		}
	}
	pdb_file.close();	

	return;
}
REMARK  Sample PDB file
ATOM      1  N   ASN   584      23.122 -19.077  26.733  1.00 31.16   7
ATOM      2  CA  ASN   584      22.622 -18.771  28.061  1.00 30.76   6
ATOM      3  CB  ASN   584      22.530 -20.045  28.903  1.00 34.93   6
ATOM      4  CG  ASN   584      21.772 -21.148  28.199  1.00 39.18   6
ATOM      5  OD1 ASN   584      22.281 -21.767  27.264  1.00 44.16   8
ATOM      6  ND2 ASN   584      20.542 -21.390  28.630  1.00 42.18   7
ATOM      7  C   ASN   584      23.510 -17.762  28.766  1.00 29.00   6
ATOM      8  O   ASN   584      24.710 -17.674  28.505  1.00 28.15   8
ATOM      9  N   VAL   585      22.900 -16.993  29.657  1.00 25.36   7
ATOM     10  CA  VAL   585      23.617 -15.995  30.426  1.00 25.81   6
ATOM     11  CB  VAL   585      23.175 -14.568  30.056  1.00 25.80   6
ATOM     12  CG1 VAL   585      23.913 -13.555  30.921  1.00 26.63   6
ATOM     13  CG2 VAL   585      23.451 -14.306  28.588  1.00 25.54   6
ATOM     14  C   VAL   585      23.294 -16.253  31.886  1.00 25.88   6
ATOM     15  O   VAL   585      22.128 -16.310  32.269  1.00 26.24   8
ATOM     16  N   ASP   586      24.333 -16.431  32.691  1.00 26.29   7
ATOM     17  CA  ASP   586      24.169 -16.690  34.115  1.00 26.17   6
ATOM     18  CB  ASP   586      24.539 -18.138  34.432  1.00 28.05   6
ATOM     19  CG  ASP   586      23.583 -19.132  33.817  1.00 29.70   6
ATOM     20  OD1 ASP   586      22.457 -19.274  34.337  1.00 31.93   8
ATOM     21  OD2 ASP   586      23.953 -19.769  32.808  1.00 32.92   8
ATOM     22  C   ASP   586      25.068 -15.758  34.903  1.00 25.83   6
ATOM     23  O   ASP   586      26.263 -15.658  34.626  1.00 27.22   8
ATOM     24  N   LEU   587      24.493 -15.073  35.885  1.00 24.23   7
ATOM     25  CA  LEU   587      25.261 -14.157  36.712  1.00 23.49   6
ATOM     26  CB  LEU   587      25.328 -12.774  36.052  1.00 23.03   6
ATOM     27  CG  LEU   587      24.014 -12.042  35.767  1.00 24.15   6
ATOM     28  CD1 LEU   587      23.468 -11.416  37.042  1.00 23.09   6
ATOM     29  CD2 LEU   587      24.263 -10.959  34.723  1.00 25.39   6
ATOM     30  C   LEU   587      24.667 -14.040  38.107  1.00 22.87   6
ATOM     31  O   LEU   587      23.540 -14.475  38.359  1.00 23.13   8
END
ATOM      1  N   ASN   584      23.122 -19.077  26.733  1.00 31.16
ATOM      2  CA  ASN   584      22.622 -18.771  28.061  1.00 30.76
ATOM      3  CB  ASN   584      22.530 -20.045  28.903  1.00 34.93
ATOM      4  CG  ASN   584      21.772 -21.148  28.199  1.00 39.18
ATOM      5  OD1 ASN   584      22.281 -21.767  27.264  1.00 44.16
ATOM      6  ND2 ASN   584      20.542 -21.390  28.630  1.00 42.18
ATOM      7  C   ASN   584      23.510 -17.762  28.766  1.00 29.00
ATOM      8  O   ASN   584      24.710 -17.674  28.505  1.00 28.15
ATOM      9  N   VAL   585      22.900 -16.993  29.657  1.00 25.36
ATOM     10  CA  VAL   585      23.617 -15.995  30.426  1.00 25.81
ATOM     11  CB  VAL   585      23.175 -14.568  30.056  1.00 25.80
ATOM     12  CG1 VAL   585      23.913 -13.555  30.921  1.00 26.63
ATOM     13  CG2 VAL   585      23.451 -14.306  28.588  1.00 25.54
ATOM     14  C   VAL   585      23.294 -16.253  31.886  1.00 25.88
ATOM     15  O   VAL   585      22.128 -16.310  32.269  1.00 26.24
ATOM     16  N   ASP   586      24.333 -16.431  32.691  1.00 26.29
ATOM     17  CA  ASP   586      24.169 -16.690  34.115  1.00 26.17
ATOM     18  CB  ASP   586      24.539 -18.138  34.432  1.00 28.05
ATOM     19  CG  ASP   586      23.583 -19.132  33.817  1.00 29.70
ATOM     20  OD1 ASP   586      22.457 -19.274  34.337  1.00 31.93
ATOM     21  OD2 ASP   586      23.953 -19.769  32.808  1.00 32.92
ATOM     22  C   ASP   586      25.068 -15.758  34.903  1.00 25.83
ATOM     23  O   ASP   586      26.263 -15.658  34.626  1.00 27.22
ATOM     24  N   LEU   587      24.493 -15.073  35.885  1.00 24.23
ATOM     25  CA  LEU   587      25.261 -14.157  36.712  1.00 23.49
ATOM     26  CB  LEU   587      25.328 -12.774  36.052  1.00 23.03
ATOM     27  CG  LEU   587      24.014 -12.042  35.767  1.00 24.15
ATOM     28  CD1 LEU   587      23.468 -11.416  37.042  1.00 23.09
ATOM     29  CD2 LEU   587      24.263 -10.959  34.723  1.00 25.39
ATOM     30  C   LEU   587      24.667 -14.040  38.107  1.00 22.87
ATOM     31  O   LEU   587      23.540 -14.475  38.359  1.00 23.13

LOG_gcc-3.x


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]