/*
FILE:     ndb_cifparse_main.C
*/
/*
VERSION:  3.101
*/
/*
DATE:     8/27/2004
*/
/*
  Comments and Questions to: sw-help@rcsb.rutgers.edu
*/
/*
COPYRIGHT 1999-2004 Rutgers - The State University of New Jersey

This software is provided WITHOUT WARRANTY OF MERCHANTABILITY OR
FITNESS FOR A PARTICULAR PURPOSE OR ANY OTHER WARRANTY, EXPRESS OR
IMPLIED.  RUTGERS MAKE NO REPRESENTATION OR WARRANTY THAT THE
SOFTWARE WILL NOT INFRINGE ANY PATENT, COPYRIGHT OR OTHER
PROPRIETARY RIGHT.

The user of this software shall indemnify, hold harmless and defend
Rutgers, its governors, trustees, officers, employees, students,
agents and the authors against any and all claims, suits,
losses, liabilities, damages, costs, fees, and expenses including
reasonable attorneys' fees resulting from or arising out of the
use of this software.  This indemnification shall include, but is
not limited to, any and all claims alleging products liability.
*/
/*
               PDB SOFTWARE LICENSE AGREEMENT

BY CLICKING THE ACCEPTANCE BUTTON OR INSTALLING OR USING 
THIS "SOFTWARE, THE INDIVIDUAL OR ENTITY LICENSING THE  
SOFTWARE ("LICENSEE") IS CONSENTING TO BE BOUND BY AND IS 
BECOMING A PARTY TO THIS AGREEMENT.  IF LICENSEE DOES NOT 
AGREE TO ALL OF THE TERMS OF THIS AGREEMENT
THE LICENSEE MUST NOT INSTALL OR USE THE SOFTWARE.

1. LICENSE AGREEMENT

This is a license between you ("Licensee") and the Protein Data Bank (PDB) 
at Rutgers, The State University of New Jersey (hereafter referred to 
as "RUTGERS").   The software is owned by RUTGERS and protected by 
copyright laws, and some elements are protected by laws governing 
trademarks, trade dress and trade secrets, and may be protected by 
patent laws. 

2. LICENSE GRANT

RUTGERS grants you, and you hereby accept, non-exclusive, royalty-free 
perpetual license to install, use, modify, prepare derivative works, 
incorporate into other computer software, and distribute in binary 
and source code format, or any derivative work thereof, together with 
any associated media, printed materials, and on-line or electronic 
documentation (if any) provided by RUTGERS (collectively, the "SOFTWARE"), 
subject to the following terms and conditions: (i) any distribution 
of the SOFTWARE shall bind the receiver to the terms and conditions 
of this Agreement; (ii) any distribution of the SOFTWARE in modified 
form shall clearly state that the SOFTWARE has been modified from 
the version originally obtained from RUTGERS.  

2. COPYRIGHT; RETENTION OF RIGHTS.  

The above license grant is conditioned on the following: (i) you must 
reproduce all copyright notices and other proprietary notices on any 
copies of the SOFTWARE and you must not remove such notices; (ii) in 
the event you compile the SOFTWARE, you will include the copyright 
notice with the binary in such a manner as to allow it to be easily 
viewable; (iii) if you incorporate the SOFTWARE into other code, you 
must provide notice that the code contains the SOFTWARE and include 
a copy of the copyright notices and other proprietary notices.  All 
copies of the SOFTWARE shall be subject to the terms of this Agreement.  

3. NO MAINTENANCE OR SUPPORT; TREATMENT OF ENHANCEMENTS 

RUTGERS is under no obligation whatsoever to: (i) provide maintenance 
or support for the SOFTWARE; or (ii) to notify you of bug fixes, patches, 
or upgrades to the features, functionality or performance of the 
SOFTWARE ("Enhancements") (if any), whether developed by RUTGERS 
or third parties.  If, in its sole discretion, RUTGERS makes an 
Enhancement available to you and RUTGERS does not separately enter 
into a written license agreement with you relating to such bug fix, 
patch or upgrade, then it shall be deemed incorporated into the SOFTWARE 
and subject to this Agreement. You are under no obligation whatsoever 
to provide any Enhancements to RUTGERS or the public that you may 
develop over time; however, if you choose to provide your Enhancements 
to RUTGERS, or if you choose to otherwise publish or distribute your 
Enhancements, in source code form without contemporaneously requiring 
end users or RUTGERS to enter into a separate written license agreement 
for such Enhancements, then you hereby grant RUTGERS a non-exclusive,
royalty-free perpetual license to install, use, modify, prepare
derivative works, incorporate into the SOFTWARE or other computer
software, distribute, and sublicense your Enhancements or derivative
works thereof, in binary and source code form.

4. FEES.  There is no license fee for the SOFTWARE.  If Licensee
wishes to receive the SOFTWARE on media, there may be a small charge
for the media and for shipping and handling.  Licensee is
responsible for any and all taxes.

5. TERMINATION.  Without prejudice to any other rights, Licensor
may terminate this Agreement if Licensee breaches any of its terms
and conditions.  Upon termination, Licensee shall destroy all
copies of the SOFTWARE.

6. PROPRIETARY RIGHTS.  Title, ownership rights, and intellectual
property rights in the Product shall remain with RUTGERS.  Licensee 
acknowledges such ownership and intellectual property rights and will 
not take any action to jeopardize, limit or interfere in any manner 
with RUTGERS' ownership of or rights with respect to the SOFTWARE.  
The SOFTWARE is protected by copyright and other intellectual 
property laws and by international treaties.  Title and related 
rights in the content accessed through the SOFTWARE is the property 
of the applicable content owner and is protected by applicable law.  
The license granted under this Agreement gives Licensee no rights to such
content.

7. DISCLAIMER OF WARRANTY.  THE SOFTWARE IS PROVIDED FREE OF 
CHARGE, AND, THEREFORE, ON AN "AS IS" BASIS, WITHOUT WARRANTY OF 
ANY KIND, INCLUDING WITHOUT LIMITATION THE WARRANTIES THAT IT 
IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE 
OR NON-INFRINGING.  THE ENTIRE RISK AS TO THE QUALITY AND 
PERFORMANCE OF THE SOFTWARE IS BORNE BY LICENSEE.  SHOULD THE 
SOFTWARE PROVE DEFECTIVE IN ANY RESPECT, THE LICENSEE AND NOT 
LICENSOR ASSUMES THE ENTIRE COST OF ANY SERVICE AND REPAIR.  
THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF 
THIS AGREEMENT.  NO USE OF THE PRODUCT IS AUTHORIZED HEREUNDER 
EXCEPT UNDER THIS DISCLAIMER.

8. LIMITATION OF LIABILITY.  TO THE MAXIMUM EXTENT PERMITTED BY
APPLICABLE LAW,  IN NO EVENT WILL LICENSOR BE LIABLE FOR ANY 
INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 
OUT OF THE USE OF OR INABILITY TO USE THE SOFTWARE, INCLUDING, 
WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, WORK 
STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL 
OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF ADVISED OF THE
POSSIBILITY THEREOF. 
*/
/* 
  PURPOSE:    DDL 2.1 compliant CIF data file access library test program...

*/


#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "ndb_cifparse.h"
#include "ndb_misclib.h"

#define MAX_ID2_LEN  10
#define MAX_ID1_LEN  10

void ndb_usage(const char *prog_name);
void build_new_datablocks(CifHandle *cif);
void test_write_file(CifHandle *cif,const char *fname);
int test_read_file(CifHandle *cif,const char *fname);
void test_build_block_write_block(CifHandle *cif,const char *fname);
void test_remove_block(CifHandle *cif,int iblock);
void test_print_all_datablocks(CifHandle *cif);
void build_new_datablock_from_pdb_file(CifHandle *cif);
int get_category_index(CifHandle *cif,int blockIndex, const char *categoryName);
int get_column_index(CifHandle *cif,int blockIndex, int categoryIndex, const char *columnName);
void  pr_coords_for_component_from_cif(CifHandle *cif,const char *cifName, const char *blockId, const char *compId);
void test_write_file_pretty(CifHandle *cif,const char *fname);

CifHandle NdbCif;


int main(int argc, char *argv[])
{
    int level=10, i, itest,ret;
    char *prog_name, *fname=NULL,*fname2=NULL, *oname=NULL, *dbname=NULL, *compid=NULL;
    char *fname_default = "test.cif";
    char *oname_default = "test.ocif";
    prog_name = argv[0];

    ndb_open_log(prog_name,"cifparse.log");
/*  
 *   Process command line arguments ... 
 */

    for (i=1; i < argc; i++) {
	if ( argv[i][0] == '-' ) {
	    if (strcmp(argv[i],"-f") == 0 ) { /*input file */
		i++;
		fname= argv[i];  
	    }
	    else if (strcmp(argv[i],"-f2") == 0 ) { /*input file2 */
		i++;
		fname2= argv[i];  
	    }
	    else if (strcmp(argv[i],"-o") == 0 ) { /*output file */
		i++;
		oname= argv[i];  
	    }
	    else if (strcmp(argv[i],"-v") == 0 ) {  /* debug level */
		i++;
		level = atoi(argv[i]);  
	    }
	    else if (strcmp(argv[i],"-t") == 0 ) {  /* test # */
		i++;
		itest = atoi(argv[i]);  
	    }
	    else if (strcmp(argv[i],"-db") == 0 ) { /* dblock name */
		i++;
		dbname= argv[i];  
	    }
	    else if (strcmp(argv[i],"-compid") == 0 ) { /* component id */
		i++;
		compid= argv[i];  
	    } else {
	      ndb_usage(prog_name);
	      exit(1);
	    }
	} else {
	    ndb_usage(prog_name);
	    exit(1);
	}
    }
    if (fname == NULL) fname = fname_default;
    if (oname == NULL) oname = oname_default;


    ndb_set_debug_level(level);
  


    ndb_log_message(NDB_MSG_DEBUG4,"Starting CIFparse test program");
    ndb_log_message(NDB_MSG_DEBUG4,"Current number of data blocks is %d",
		    ndb_cif_count_datablock(&NdbCif));
    ndb_log_message(NDB_MSG_DEBUG4,"Current data block is %d",
		    ndb_cif_current_datablock(&NdbCif));
    switch (itest) 
	{
	case 1:
	  ndb_cif_init(&NdbCif);
	  test_read_file(&NdbCif,fname);
	  test_write_file(&NdbCif,oname);
	  ndb_cif_close(&NdbCif);
	  break;

	case 2:
	  ndb_cif_init(&NdbCif);
	  test_build_block_write_block(&NdbCif,oname);
	  ndb_cif_close(&NdbCif);
	  break;

	case 3:
	  ndb_cif_init(&NdbCif);
	  build_new_datablocks(&NdbCif);
	  ndb_cif_print_datablocks(&NdbCif,stdout);
	  ndb_cif_rewind_datablock(&NdbCif);
	  ndb_cif_print_datablocks(&NdbCif,stdout);
	  ndb_cif_close(&NdbCif);
	  break;

	case 4:
	  ndb_cif_init(&NdbCif);
	  build_new_datablocks(&NdbCif);
	  ndb_cif_move_datablock_by_id(&NdbCif,1);
	  test_write_file(&NdbCif,oname);
	  ndb_cif_close(&NdbCif);
	  break;
	case 5:
	  ndb_cif_init(&NdbCif);
	  test_read_file(&NdbCif,fname);
	  test_print_all_datablocks(&NdbCif);
	  ndb_cif_close(&NdbCif);
	  break;
	case 6:
	  for (i=0; i < 10; i++) {
	    fprintf(stderr,"Beginning read/write %d cycle on file %s\n",i+1,fname);
	    ndb_cif_init(&NdbCif);
	    test_read_file(&NdbCif,fname);
	    test_write_file(&NdbCif,oname);
	    ndb_cif_close(&NdbCif);
	  }
	  break;
	case 7:
	  pr_coords_for_component_from_cif(&NdbCif,fname,dbname, compid);
	  break;
	case 8:
	  ndb_cif_init(&NdbCif);
	  test_read_file(&NdbCif,fname);
	  test_write_file_pretty(&NdbCif,oname);
	  ndb_cif_close(&NdbCif);

	  break;

	case 9:
	  ndb_cif_init(&NdbCif);
	  ret=test_read_file(&NdbCif,fname);
	  ndb_cif_close(&NdbCif);
	  if (ret==0) 
	    fprintf(stderr,"Parser error in file %s\n",fname);
	  else
		 fprintf(stderr,"%d Datablock(s) in file%s\n",ret,fname);
	  
	  ndb_cif_init(&NdbCif);
	  ret=test_read_file(&NdbCif,fname2);
	  ndb_cif_close(&NdbCif);
	  if (ret==0) 
	    fprintf(stderr,"Parser error in file %s\n",fname2);
	  else
		 fprintf(stderr,"%d Datablock(s) in file %s\n",ret,fname2);

	  break;

	  
	}
    
    ndb_log_message(NDB_MSG_DEBUG4,"All done ... ");
    ndb_close_log();
}

void build_new_datablocks(CifHandle *cif)
{
    int nblocks=10, ncats=10, nrows=100, ncols=8;
    int iblock, icat, irow, icol;
    char block[20], category[20],keyword[20],value[20];

    for (iblock=1; iblock<=nblocks; iblock++) {
	sprintf(block,"block_%d",iblock);
	ndb_cif_new_datablock(cif,block);
	ndb_log_message(NDB_MSG_DEBUG4,"Building block %d of %d",
			ndb_cif_current_datablock(cif),
			ndb_cif_count_datablock(cif));
	for (icat=1; icat <= ncats; icat++) {
	    sprintf(category,"category_%d",icat);
	    ndb_cif_new_category(cif,category);
	    for (icol=1; icol <= ncols; icol++) {
		sprintf(keyword,"keyword_%d",icol);
		ndb_cif_put_item_keyword(cif,keyword);
	    }
	    for (irow=1; irow <= nrows; irow++) {
		ndb_cif_new_row(cif);
		for (icol=1; icol <= ncols; icol++) {
		    sprintf(value,"Value[%3d,%3d]",irow,icol);
		    ndb_cif_put_item_value(cif,icol,value);
		}
	    }
	}
    }
}


void ndb_usage(const char *prog_name)
{
    fprintf(stdout,"Usage:  %s\n",prog_name);   
    fprintf(stdout,"                       [-f   input CIF1 ]\n"); 
    fprintf(stdout,"                       [-f2   input CIF2 ]\n"); 
    fprintf(stdout,"                       [-o   output CIF ]\n"); 
    fprintf(stdout,"                       [-t   test number (1-6)]\n"); 
    fprintf(stdout,"                       [-v   debug level (1-10)]\n"); 
    fprintf(stdout,"                       [-db   data block name\n"); 
    fprintf(stdout,"                       [-compid component id\n"); 
}


int test_read_file(CifHandle *cif,const char *fname)
{
    FILE *fp;
    int iret;
/*
 *  Read file  ... 
 */
    if ((fp = fopen(fname,"r")) == NULL ) {
	ndb_log_message(NDB_MSG_DEBUG4,"Unable to open file %s",fname);
	exit(1);
    }
    iret = ndb_cif_read_file(cif,fp);
    ndb_log_message(NDB_MSG_DEBUG4,"Read CIF file %s and returned %d datablock(s)", 
		    fname,iret);
    ndb_log_message(NDB_MSG_DEBUG4,"Current number of data blocks is %d",
		    ndb_cif_count_datablock(cif));
    ndb_log_message(NDB_MSG_DEBUG4,"Current data block is %d",
		    ndb_cif_current_datablock(cif));
    fclose(fp);
	 return iret;
}



void test_write_file(CifHandle *cif,const char *fname)
{
    FILE *fp;
/* 
 *   Write file
 */
    ndb_cif_rewind_file(cif);
    if ((fp = fopen(fname,"w")) == NULL ) {
	ndb_log_message(NDB_MSG_DEBUG4,"Unable to open %s",fname);
	exit(1);
    }
    ndb_cif_write_file(cif,fp);
    ndb_log_message(NDB_MSG_DEBUG4,"Wrote CIF file %s",fname);
    fclose(fp);
    ndb_log_message(NDB_MSG_DEBUG4,"Current number of data blocks is %d",
		    ndb_cif_count_datablock(cif));
    ndb_log_message(NDB_MSG_DEBUG4,"Current data block is %d",
		    ndb_cif_current_datablock(cif));

}

void test_write_file_pretty(CifHandle *cif,const char *fname)
{
    FILE *fp;
/* 
 *   Write file
 */
    ndb_cif_rewind_file(cif);
    if ((fp = fopen(fname,"w")) == NULL ) {
	ndb_log_message(NDB_MSG_DEBUG4,"Unable to open %s",fname);
	exit(1);
    }
    ndb_cif_write_file_pr(cif,fp);
    ndb_log_message(NDB_MSG_DEBUG4,"Wrote CIF file %s",fname);
    fclose(fp);
    ndb_log_message(NDB_MSG_DEBUG4,"Current number of data blocks is %d",
		    ndb_cif_count_datablock(cif));
    ndb_log_message(NDB_MSG_DEBUG4,"Current data block is %d",
		    ndb_cif_current_datablock(cif));

}


void test_build_block_write_block(CifHandle *cif,const char *fname)
{
    FILE *fp;
    ndb_log_message(NDB_MSG_DEBUG4,"Building data blocks");
    build_new_datablocks(cif);
    ndb_cif_rewind_datablock(cif);
    ndb_log_message(NDB_MSG_DEBUG4,"Current number of data blocks is %d",
		    ndb_cif_count_datablock(cif));
    ndb_log_message(NDB_MSG_DEBUG4,"Current data block is %d",
		    ndb_cif_current_datablock(cif));

    ndb_log_message(NDB_MSG_DEBUG4,"Writing new data blocks");
    if ((fp = fopen(fname,"w")) == NULL ) {
	ndb_log_message(NDB_MSG_DEBUG4,"Unable to open file %s",fname);
	exit(1);
    }
    ndb_cif_write_file(cif,fp);
    ndb_log_message(NDB_MSG_DEBUG4,"Wrote CIF file %s",fname);
    fclose(fp);
}


void test_remove_block(CifHandle *cif,int iblock)
{
    int nblocks;
    ndb_log_message(NDB_MSG_DEBUG4,"Current number of data blocks is %d",
		    ndb_cif_count_datablock(cif));
    ndb_log_message(NDB_MSG_DEBUG4,"Current data block is %d",
		    ndb_cif_current_datablock(cif));

    nblocks = ndb_cif_count_datablock(cif);
    if (iblock < nblocks ) {
	ndb_log_message(NDB_MSG_DEBUG4,"Removing data block %d ",iblock);
	ndb_cif_remove_datablock_by_id(cif,iblock);
	ndb_log_message(NDB_MSG_DEBUG4,"Current number of data blocks is %d",
			ndb_cif_count_datablock(cif));
	ndb_log_message(NDB_MSG_DEBUG4,"Current data block is %d",
			ndb_cif_current_datablock(cif));
    }
}


void test_print_all_datablocks(CifHandle *cif)
{
    int datablockId;

    ndb_cif_rewind_datablock(cif);

    datablockId = ndb_cif_rewind_datablock(cif);
    do {
	ndb_cif_pretty_print_datablock(cif,stdout);
	datablockId = ndb_cif_next_datablock(cif);
    } while (datablockId);

}

void  pr_coords_for_component_from_cif(CifHandle *cif,const char *cifName, const char *blockId, const char *compId)
/*
 *  Example: function which reads coordinates for component 'compid' from the 
 *           atom_site category of data block 'blockId.
 *
 */
{
  int i, stat, iblock;
  FILE *fp=NULL;

  int iCat, irow, nRow;
  int col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12;
  int col2a,col4a,col5a,col6a;

  char atom_id[MAX_ID2_LEN+1];
  char name[MAX_ID2_LEN+1];
  char nameAuth[MAX_ID2_LEN+1];
  char type[MAX_ID2_LEN+1];
  char compid[MAX_ID2_LEN+1];
  char compidAuth[MAX_ID2_LEN+1];
  char asymid[MAX_ID1_LEN+1];
  char asymidAuth[MAX_ID1_LEN+1];
  char seqid[MAX_ID1_LEN+1];
  char seqidAuth[MAX_ID1_LEN+1];
  char altid[MAX_ID1_LEN+1];
  double x,y,z,occ,biso;


  ndb_cif_init(cif);
  ndb_log_message(NDB_MSG_INFO,"Reading CIF file %s",cifName);
  if ((fp = fopen(cifName, "r")) == NULL) {
    ndb_log_message(NDB_MSG_INFO,"Unable to open %s",cifName);
    return;
  }
  stat = ndb_cif_read_file(cif,fp);
  fclose(fp);
  ndb_log_message(NDB_MSG_INFO,"Read %d datablocks from %s [status = %d]",
		  ndb_cif_count_datablock(cif),cifName,stat);

  /*
   *  Check for target data block.
   */

  iblock = -1;
  for (i=0; i< (*cif).numDatablock; i++) {
    if (!strcmp(blockId,(*cif).datablocks[i].datablockName)) {
      iblock = i;
      break;
    }
  }

  if (iblock == -1 ) {
    ndb_log_message(NDB_MSG_ERR,"Target data block %s is not in file %s",
		    blockId,cifName);
    return;
  }

  /*******************************************************************************************
   *                 Load atom information and coordinates... 
   *        Get category id for category "atom_site" in this datablock..
   */

  iCat = get_category_index(cif,iblock,"atom_site");
  if (iCat == -1) {
    ndb_log_message(NDB_MSG_ERR,"Target category %s is not in data block %d",
		    "atom_site",iblock);
    return;
  } else {
    ndb_log_message(NDB_MSG_INFO,"Read %d rows in category %d",
		    (*cif).datablocks[iblock].categories[iCat].numRow,
		    iCat);

    col1   = get_column_index(cif,iblock,iCat,"id");
    col2   = get_column_index(cif,iblock,iCat,"label_atom_id");
    col3   = get_column_index(cif,iblock,iCat,"type_symbol");
    col4   = get_column_index(cif,iblock,iCat,"label_comp_id");
    col5   = get_column_index(cif,iblock,iCat,"label_asym_id");
    col6   = get_column_index(cif,iblock,iCat,"label_seq_id");
    col7   = get_column_index(cif,iblock,iCat,"label_alt_id");
    col8   = get_column_index(cif,iblock,iCat,"cartn_x");
    col9   = get_column_index(cif,iblock,iCat,"cartn_y");
    col10  = get_column_index(cif,iblock,iCat,"cartn_z");
    col11  = get_column_index(cif,iblock,iCat,"occupancy");
    col12  = get_column_index(cif,iblock,iCat,"B_iso_or_equiv");
    col2a   = get_column_index(cif,iblock,iCat,"auth_atom_id");
    col4a   = get_column_index(cif,iblock,iCat,"auth_comp_id");
    col5a   = get_column_index(cif,iblock,iCat,"auth_asym_id");
    col6a   = get_column_index(cif,iblock,iCat,"auth_seq_id");

    nRow=0;
    for (i=0; i < (*cif).datablocks[iblock].categories[iCat].numRow; i++) {
      if (!strcmp((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col4],compId)) {
	nRow++;
      }
    }


    if (nRow > 0 ) {
      for (i=0; i < (*cif).datablocks[iblock].categories[iCat].numRow; i++) {
	if (!strcmp((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col4],compId)) {

	  if ((col1 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col1] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col1]) > 0)) {
	    strncpy(atom_id,
	      (*cif).datablocks[iblock].categories[iCat].rows[i].columns[col1],MAX_ID2_LEN);
	  }

	  if ((col2 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col2] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col2]) > 0)) {
	    strncpy(name,(*cif).datablocks[iblock].categories[iCat].rows[i].columns[col2],MAX_ID2_LEN);
	  }

	  if ((col2a != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col2a] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col2a]) > 0)) {
	    strncpy(nameAuth,(*cif).datablocks[iblock].categories[iCat].rows[i].columns[col2a],MAX_ID2_LEN);
	  }


	  if ((col3 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col3] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col3]) > 0)) {
	    strncpy(type,(*cif).datablocks[iblock].categories[iCat].rows[i].columns[col3],2);
	  }
		
	  if ((col4 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col4] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col4]) > 0)) {
	    strncpy(compid,(*cif).datablocks[iblock].categories[iCat].rows[i].columns[col4],MAX_ID2_LEN);
	  }
	  if ((col4a != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col4a] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col4a]) > 0)) {
	    strncpy(compidAuth,(*cif).datablocks[iblock].categories[iCat].rows[i].columns[col4a],MAX_ID2_LEN);
	  }

	  if ((col5 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col5] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col5]) > 0)) {
	    strncpy(asymid,(*cif).datablocks[iblock].categories[iCat].rows[i].columns[col5],MAX_ID1_LEN);
	  }

	  if ((col5a != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col5a] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col5a]) > 0)) {
	    strncpy(asymidAuth,(*cif).datablocks[iblock].categories[iCat].rows[i].columns[col5a],MAX_ID1_LEN);
	  }

	  if ((col6 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col6] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col6]) > 0)) {
	    strncpy(seqid,(*cif).datablocks[iblock].categories[iCat].rows[i].columns[col6],MAX_ID1_LEN);
	  }

	  if ((col6a != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col6a] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col6a]) > 0)) {
	    strncpy(seqidAuth,(*cif).datablocks[iblock].categories[iCat].rows[i].columns[col6a],MAX_ID1_LEN);
	  }

	  if ((col7 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col7] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col7]) > 0)) {
	    strncpy(altid,(*cif).datablocks[iblock].categories[iCat].rows[i].columns[col7],MAX_ID1_LEN);
	  }

	  if ((col8 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col8] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col8]) > 0)) {
	    x = atof((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col8]);
	  }

	  if ((col9 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col9] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col9]) > 0)) {
	    y = atof((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col9]);
	  }

	  if ((col10 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col10] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col10]) > 0)) {
	    z = atof((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col10]);
	  }

	  if ((col11 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col11] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col11]) > 0)) {
	    occ = atof((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col11]);
	  }

	  if ((col12 != -1) && 
	      ((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col12] != NULL) && 
	      (strlen((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col12]) > 0)) {
	    biso = atof((*cif).datablocks[iblock].categories[iCat].rows[i].columns[col12]);
	  }
	  irow++;
	  printf("%-5s %-5s %-5s %-5s %-5s %8.3f %8.3f %8.3f %8.3f %8.3f\n",
		 atom_id,name,type,compid,seqid,x,y,z,occ,biso);

	}
      }	
    } 
  }
  ndb_cif_close(cif);
}



int get_column_index(CifHandle *cif,int blockIndex, int categoryIndex, const char *columnName)
  /*
   *  Return the index of for 'columnName' in the data block and category 
   *  specified by 'blockIndex' and 'categoryIndex'.
   */
{
    int icol, k;
    icol = -1;
    for (k=0; k < (*cif).datablocks[blockIndex].categories[categoryIndex].numCol; k++) {
        if (!strcmp(columnName,(*cif).datablocks[blockIndex].categories[categoryIndex].colNames[k])) {
            icol = k;
            break;
        }
    }
    return icol;
}

int get_category_index(CifHandle *cif,int blockIndex, const char *categoryName)
  /*
   *  Return the index of for 'categoryName' in the data block 
   *  specified by 'blockIndex'.
   */
{
    int icat, i;
    icat = -1;
    for (i=0; i< (*cif).datablocks[blockIndex].numCategory; i++) {
        if (!strcmp(categoryName,(*cif).datablocks[blockIndex].categories[i].categoryName)) {
            icat = i;
            break;
        }
    }
    return(icat);
}
