/*
FILE:     pdb2cif_corr.C
*/
/*
VERSION:  1.600
*/
/*
DATE:     2/3/2005
*/
/*
  Comments and Questions to: sw-help@rcsb.rutgers.edu
*/
/*
COPYRIGHT 1999-2005 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. 
*/
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

#include "DICFileObj.h"
#include "erf_iolib.h"
#include "erf_category.h"
#include "erf_util.h"
#include "ndb_misclib.h"

#include "remark.h"
#include "remark_100s.h"
#include "remark_200s.h"
#include "remark_300s.h"
#include "remark_general.h"
#include "remark_3_prolsqs.h"
#include "remark_3_refmac5.h"
#include "remark_3_shelxl.h"
#include "remark_3_tnt.h"
#include "remark_3_xplors.h"
#include "remark_olds.h"

#define OPTION_TOKEN_TO_CIF_TEX    1
#define OPTION_REMARK_TO_CIF_TEX   2
#define OPTION_TOKEN_TO_CIF_HTML   3
#define OPTION_REMARK_TO_CIF_HTML  4
#define OPTION_TOKEN_TO_CIF_HTML1  5
#define OPTION_REMARK_HEAD         6
#define OPTION_REMARK_TO_CIF_TXT   7

typedef struct {
        int num_word;
        char word[50][100];
} WORD;

static DICFileObj *dict = NULL;
static ISTable *aliases = NULL;
static ISTable *items = NULL;
static ReVarCifArray<CifString> queryCol1;
static ReVarCifArray<CifString> queryCol2;
static int iCol1, colIndex;

static int exchange_flag = 0;

static char defalut_dir[] = "/ndbapps";

static void write_pdb_token_to_cif_html(const int pdbOnly);
static void write_pdb_remark_to_cif_html(void);
static void write_pdb_token_to_cif_latex(void);
static void write_pdb_remark_to_cif_latex(void);
static void write_remark_head(void);
static void write_remark_to_cif_txt(void);
static void html_preamble(FILE *fp, const char *title);
static void html_trailer(FILE *fp);
static void write_remark_html(FILE *fp, REMARKS *remark, const int num_remark);
static void write_remark_group_html(FILE *fp, GROUP_REMARKS *group_remark);
static void latex_head(FILE *fp);
static void table_head(FILE *fp);
static void table_head_1(FILE *fp);
static void change_string(char *string);
static void database_stored(const char *token, int field, WORD *cif_category, const int is_explicit);
static void write_remark_latex(FILE *fp, REMARKS *remark, const int numremark);
static void write_remark_group_latex(FILE *fp, GROUP_REMARKS *group_remark);
static void write_remark_old(FILE *fp, REMARKS *remark, const int num_remark);
static void write_remark_group(FILE *fp, GROUP_REMARKS *group_remark);
static void write_remark_cif(FILE *fp, REMARKS *remark, const int num_remark);
static void write_remark_group_cif(FILE *fp, GROUP_REMARKS *group_remark);
static void read_binary_dictionary();
static void find_exchange_items(char *item);

int main(int argc, char **argv)
{
       char temp_c, reply[80], logfile[80];

       root_dir = getenv("RCSBROOT");
       if (root_dir == NULL) root_dir = defalut_dir;

       if (argc == 2 && !strcmp(argv[1], "-exchange")) exchange_flag = 1;

       strcpy(logfile, "pdb2cif_corress.err");
       ndb_set_debug_level(NDB_MSG_INFO);
       ndb_open_log("pdb2cif_corress", logfile);

       if (!read_ndb_tokens()) {
            printf("Read ndb_tokens failed\n");
            return -1;
       }
       if (!read_cif_db() || !read_cif_rms()) {
            printf("Read ndb_cif or ndb_cif_rms failed\n");
            return -1;
       }

       if (exchange_flag) {
            read_binary_dictionary();
            if (aliases && items) {
                 queryCol1.Clear();
                 colIndex = aliases->GetColumnIndex("name");
                 queryCol1.Add("alias_namelc");
                 queryCol1.Add("dictionary");
                 queryCol1.Add("version");
                 aliases->CreateIndex("ind0", queryCol1);

                 queryCol2.Clear();
                 queryCol2.Add("namelc");
                 items->CreateIndex("ind1", queryCol2);
            }
       }

       temp_c = 0;
       memset(reply, 0, 80);
       while (temp_c != 'Q' && temp_c != 'q') {
            system("clear");
            printf("[1] Output PDB/NDB records to mmCIF correspondences (LaTeX format)\n");
            printf("[2] Output PDB remark to mmCIF correspondence (LaTeX format)\n");
            printf("[3] Output PDB/NDB records to mmCIF correspondences (HTML format)\n");
            printf("[4] Output PDB remarks to mmCIF correspondences (HTML format)\n");
            printf("[5] Output PDB records to mmCIF correspondences (HTML format)\n");
            printf("[6] Output PDB remark to NDB internal correspondence\n");
            printf("[7] Output PDB remark to  mmCIF correspondence\n");
            printf("[Q]  Quit\n");
            printf("\nEnter selection -->");
            fflush(stdout);
            fflush(stdin);
            fscanf(stdin, "%s", reply);
            if (reply[0] == 'Q' || reply[0] == 'q') temp_c = 'q';
            else temp_c = atoi(reply);

            if (temp_c == 'Q' || temp_c == 'q') break;

            if (temp_c == OPTION_TOKEN_TO_CIF_TEX)
                 write_pdb_token_to_cif_latex();
            else if (temp_c == OPTION_REMARK_TO_CIF_TEX)
                 write_pdb_remark_to_cif_latex();
            else if (temp_c == OPTION_TOKEN_TO_CIF_HTML)
                 write_pdb_token_to_cif_html(0);
            else if (temp_c == OPTION_REMARK_TO_CIF_HTML)
                 write_pdb_remark_to_cif_html();
            else if (temp_c == OPTION_TOKEN_TO_CIF_HTML1)
                 write_pdb_token_to_cif_html(1);
            else if (temp_c == OPTION_REMARK_HEAD)
                 write_remark_head();
            else if (temp_c == OPTION_REMARK_TO_CIF_TXT)
                 write_remark_to_cif_txt();
       }
       ndb_close_log();
       return 0;
}

static void write_pdb_token_to_cif_html(const int pdbOnly)
{
       int  i, j, k;
       FILE *fp;
       char buffer[100], str_or[10], str_type[20];
       WORD cif_category;
       char itempath[]="http://ndb-mirror-0.rutgers.edu:2010/jw/cif_ndb.dic/Items/";

       if (pdbOnly)
            fp = fopen("fftoken_pdb_to_cif.html", "w");
       else fp = fopen("fftoken_to_cif.html", "w");

       if (pdbOnly) {
            html_preamble(fp,"PDB -> mmCIF Data Item Correspondences");
            fprintf(fp,"<br>\n");
/*
            fprintf(fp,"<center><i>\n");
            fprintf(fp,"John Westbrook and Helen M. Berman<br>\n");
            fprintf(fp,"Nucleic Acid Database, Rutgers University<br>\n");
            fprintf(fp,"<br>\n");
            fprintf(fp,"Paula M. D. Fitzgerald<br>\n");
            fprintf(fp,"Merck Research Laboratories, Rahway, New Jersey<br>\n");
            fprintf(fp,"</i></center>\n");
            fprintf(fp,"<br>      \n");
*/
            fprintf(fp,"<p>\n");
            fprintf(fp,"This document tabulates the correspondences between data\n");
            fprintf(fp,"items in the structured records of the PDB file format \n");
            fprintf(fp,"and data items defined in the mmCIF dictionary.  The \n");
            fprintf(fp,"structured records in the following list are linked \n");
            fprintf(fp,"to tables which provide the data item correspondences.\n");
            fprintf(fp,"The mmCIF data items are in term linked to their definitions\n");
            fprintf(fp,"in the HTML mmCIF dictionary.  \n");
            fprintf(fp,"</p>\n");
            fprintf(fp,"<br>\n");
       } else {
            html_preamble(fp,"NDB Internal File -> mmCIF Correspondences");
            fprintf(fp,"<br>\n");
/*
            fprintf(fp,"<center><i>\n");
            fprintf(fp,"John Westbrook and Helen M. Berman<br>\n");
            fprintf(fp,"Nucleic Acid Database, Rutgers University<br>\n");
            fprintf(fp,"</i></center>\n");
*/
            fprintf(fp,"<br>      \n");
            fprintf(fp,"<p>\n");
            fprintf(fp,"This document tabulates the correspondences between data\n");
            fprintf(fp,"items in the structured records of the NDB file format \n");
            fprintf(fp,"and data items defined in the mmCIF dictionary.  The \n");
            fprintf(fp,"structured records in the following list are linked \n");
            fprintf(fp,"to tables which provide the data item correspondences.\n");
            fprintf(fp,"The mmCIF data items are in term linked to their definitions\n");
            fprintf(fp,"in the HTML mmCIF dictionary.  \n");
            fprintf(fp,"</p>\n");
            fprintf(fp,"<br>\n");
       }

       fprintf(fp, "<H3>Flat File Structured Record List</H3>\n");
       fprintf(fp, "<BR><BR>\n");

       fprintf(fp,"<UL>\n");
       for (i = 0; i < MxTokens; i++) {
            if (pdbOnly && NdbToken[i].NdbOnly) continue; 
            fprintf(fp, "<LI ><A HREF=\"#%s\">Record %s </A></LI>\n",
                              NdbToken[i].TokenName, NdbToken[i].TokenName);
       }
       fprintf(fp,"</UL>\n");

       fprintf(fp,"<CENTER>\n");
       for (i = 0; i < MxTokens; i++) {
            if (pdbOnly && NdbToken[i].NdbOnly) continue; 

            if (NdbToken[i].NdbOnly)
                       strcpy(str_type,"NDB");
            else strcpy(str_type,"NDB/PDB");

            if (pdbOnly) strcpy(str_type,"PDB");
            fprintf(fp, "<H2><A NAME=\"%s\"> %s record %s </A></H2>\n",
                      NdbToken[i].TokenName, str_type, NdbToken[i].TokenName);

            fprintf(fp,"<TABLE BORDER>\n");           
            fprintf(fp, "<TR><TH> Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n");

            for (j = 0; j < NdbToken[i].NumField; j++) {
                 if (pdbOnly && NdbToken[i].FieldList[j].NdbOnly) continue;
                 database_stored(NdbToken[i].TokenName, j+1, &cif_category, 0);

                 strcpy(buffer, NdbToken[i].FieldList[j].FieldLabel);
                 fprintf(fp, "<TR> <TD> &nbsp %s &nbsp </TD> ", buffer);

                 for (k = 0; k < cif_category.num_word; k++) {
                      if (k < cif_category.num_word - 1) 
                           strcpy(str_or," - and/or - ");
                      else strcpy(str_or," &nbsp ");
                      ndb_set_string_to_lower(cif_category.word[k]);
                      if (k) fprintf(fp, " <TD>&nbsp</TD> <TD><A HREF=\"%s_%s.html\">&nbsp _%s &nbsp</A></TD> <TD>%s</TD> </TR>\n", 
                                 itempath, cif_category.word[k], cif_category.word[k],str_or);

                      else   fprintf(fp, " <TD><A HREF=\"%s_%s.html\">&nbsp _%s &nbsp</A></TD> <TD>%s</TD> </TR>\n", 
                                 itempath, cif_category.word[k], cif_category.word[k], str_or);
                 }
                 if (cif_category.num_word == 0) fprintf(fp, "<TD>&nbsp n.a. &nbsp </TD><TD>&nbsp</TD></TR>\n");
            }
            fprintf(fp,"</TABLE>\n");           
            fprintf(fp,"<BR><BR>\n");           
       }
       fprintf(fp,"</CENTER>\n");
       html_trailer(fp);

       fclose (fp);
}

static void write_pdb_remark_to_cif_html(void)
{
       FILE *fp;
       int  i;
       char name[100], source[100];
       WORD cif_category;
       char itempath[]="http://ndb-mirror-0.rutgers.edu:2010/jw/cif_ndb.dic/Items/";
     
       fp = fopen("enum_remark_to_cif.html", "w");

       html_preamble(fp,"PDB -> mmCIF Data Item Correspondences");
       fprintf(fp,"<br>\n");
/*
       fprintf(fp,"<center><i>\n");
       fprintf(fp,"John Westbrook and Helen M. Berman<br>\n");
       fprintf(fp,"Nucleic Acid Database, Rutgers University<br>\n");
       fprintf(fp,"<br>\n");
       fprintf(fp,"Paula M. D. Fitzgerald<br>\n");
       fprintf(fp,"Merck Research Laboratories, Rahway, New Jersey<br>\n");
       fprintf(fp,"</i></center>\n");
       fprintf(fp,"<br>      \n");
*/
       fprintf(fp,"<p>\n");
       fprintf(fp,"This document tabulates the correspondences between data\n");
       fprintf(fp,"items in the structured records of the PDB file format \n");
       fprintf(fp,"and data items defined in the mmCIF dictionary.  The \n");
       fprintf(fp,"structured records in the following list are linked \n");
       fprintf(fp,"to tables which provide the data item correspondences.\n");
       fprintf(fp,"The mmCIF data items are in term linked to their definitions\n");
       fprintf(fp,"in the HTML mmCIF dictionary.  \n");
       fprintf(fp,"</p>\n");
       fprintf(fp,"<br>\n");

       fprintf(fp,"<TABLE BORDER>\n");           
       fprintf(fp, "<TR><TH> Enumeration Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n"); 

       strcpy(name, "");
       i = 0;
       while (PdbSource_All[i].pdbname) {
            database_stored(PdbSource_All[i].NdbTokenName, PdbSource_All[i].NdbFieldNo, &cif_category, 0);
            ndb_set_string_to_lower(cif_category.word[0]);
            if (strcmp(name, PdbSource_All[i].pdbname)) {
                 strcpy(source, PdbSource_All[i].pdbname);
                 fprintf(fp, "<TR><TD> %s </TD><TD><A HREF=\"%s_%s.html\">&nbsp _%s</A></TD><TD> &nbsp</TD> </TR>\n", 
                                  source, itempath, cif_category.word[0], cif_category.word[0]);
            } else {
                       fprintf(fp, "<TR><TD>&nbsp</TD><TD><A HREF=\"%s_%s.html\">&nbsp _%s</A></TD><TD> - and/or - </TD></TR>\n", 
                                  itempath, cif_category.word[0], cif_category.word[0]);
            }
            strcpy(name, PdbSource_All[i].pdbname);
            i++;
       }
       fprintf(fp,"</TABLE>\n");           

       fprintf(fp,"<TABLE BORDER>\n");           
       fprintf(fp, "<TR><TH> Remark Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n"); 
       fprintf(fp, "<TR><TD> REMARK 3: Refinement using X-PLOR</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD> REFINEMENT. </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD> PROGRAM : X-PLOR</TD> <TD><A HREF=\"%s_computing.structure_refinement.html\"> _computing.structure_refinement</TD> <TD></TD></TR>\n",itempath);
       fprintf(fp, "<TR><TD> AUTHORS : BRUNGER </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       write_remark_group_html(fp, &Remark_XPlor);
       fprintf(fp,"</TABLE>\n");           

       fprintf(fp,"<TABLE BORDER>\n");           
       fprintf(fp, "<TR><TH> Remark Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n"); 
       fprintf(fp, "<TR><TD> REMARK 3: Refinement using CNS</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD> REFINEMENT. </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD> PROGRAM : CNS</TD> <TD><A HREF=\"%s_computing.structure_refinement.html\"> _computing.structure_refinement</TD> <TD></TD></TR>\n",itempath);
       fprintf(fp, "<TR><TD> AUTHORS : BRUNGER,ADAMS,CLORE,DELANO, </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD>           GROS,GROSSE-KUNSTLEVE,JIANG,</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD>           KUSZEWSKI,NILGES,PANNU,READ,</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD>           RICE,SIMONSON,WARREN </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       write_remark_group_html(fp, &Remark_CNS);
       fprintf(fp,"</TABLE>\n");           

       fprintf(fp,"<TABLE BORDER>\n");           
       fprintf(fp, "<TR><TH> Remark Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n"); 
       fprintf(fp, "<TR><TD>  REMARK 3: Refinement using NUCLSQ</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR> \n");
       fprintf(fp, "<TR><TD>  REFINEMENT.</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR> \n");
       fprintf(fp, "<TR><TD>  PROGRAM : NUCLSQ </TD><TD><A HREF=\"%s_computing.structure_refinement.html\">_computing.structure_refinement</A></TD> <TD></TD></TR>\n",itempath);
       fprintf(fp, "<TR><TD>  AUTHORS : WESTHOF,DUMAS,MORAS </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR> \n");
       write_remark_group_html(fp, &Remark_Nuclsq);
       fprintf(fp,"</TABLE>\n");           

       fprintf(fp,"<TABLE BORDER>\n");           
       fprintf(fp, "<TR><TH> Remark Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n"); 
       fprintf(fp, "<TR><TD>  REMARK 3: Refinement using PROLSQ,</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR> \n");
       fprintf(fp, "<TR><TD>            CCP4, PROFFT, GPRLSA, ...</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR> \n");
       fprintf(fp, "<TR><TD>  REFINEMENT.</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD>  PROGRAM : PROLSQ </TD><TD><A HREF=\"%s_computing.structure_refinement.html\"> _computing.structure_refinement</A></TD> <TD></TD></TR> \n",itempath);
       fprintf(fp, "<TR><TD>  AUTHORS : </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       write_remark_group_html(fp, &Remark_Prolsq);
       fprintf(fp,"</TABLE>\n");           

       fprintf(fp,"<TABLE BORDER>\n");           
       fprintf(fp, "<TR><TH> Remark Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n"); 
       fprintf(fp, "<TR><TD>  REMARK 3: Refinement using REFMAC</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR> \n");
       fprintf(fp, "<TR><TD>  REFINEMENT.</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD>  PROGRAM : REFMAC </TD><TD><A HREF=\"%s_computing.structure_refinement.html\"> _computing.structure_refinement</A></TD> <TD></TD></TR> \n",itempath);
       fprintf(fp, "<TR><TD>  AUTHORS : MURSHUDOV,VAGIN,DODSON</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       write_remark_group_html(fp, &Remark_Refmac);
       fprintf(fp,"</TABLE>\n");           

       fprintf(fp,"<TABLE BORDER>\n");           
       fprintf(fp, "<TR><TH> Remark Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n"); 
       fprintf(fp, "<TR><TD>  REMARK 3: Refinement using REFMAC 5.XXX</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR> \n");
       fprintf(fp, "<TR><TD>  REFINEMENT.</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD>  PROGRAM : REFMAC </TD><TD><A HREF=\"%s_computing.structure_refinement.html\"> _computing.structure_refinement</A></TD> <TD></TD></TR> \n",itempath);
       fprintf(fp, "<TR><TD>  AUTHORS : MURSHUDOV,VAGIN,DODSON</TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       write_remark_group_html(fp, &Remark_Refmac_5_0);
       fprintf(fp,"</TABLE>\n");           

       fprintf(fp,"<TABLE BORDER>\n");           
       fprintf(fp, "<TR><TH> Remark Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n"); 
       fprintf(fp, "<TR><TD>  REMARK 3: Refinement using SHELXL </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD>  REFINEMENT. </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD>  PROGRAM : SHELXL </TD><TD><A HREF=\"%s_computing.structure_refinement.html\"> _computing.structure_refinement</A> </TD> <TD></TD></TR>\n",itempath);
       fprintf(fp, "<TR><TD>  AUTHORS : G.M.SHELDRICK </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       write_remark_group_html(fp, &Remark_Shelxl);
       fprintf(fp,"</TABLE>\n");           

       fprintf(fp,"<TABLE BORDER>\n");           
       fprintf(fp, "<TR><TH> Remark Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n"); 
       fprintf(fp, "<TR><TD>  REMARK 3: Refinement using TNT </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD>  REFINEMENT. </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       fprintf(fp, "<TR><TD>  PROGRAM : TNT </TD><TD> <A HREF=\"%s_computing.structure_refinement.html\">_computing.structure_refinement</A></TD> <TD></TD></TR> \n",itempath);
       fprintf(fp, "<TR><TD>  AUTHORS : TRONRUD,TEN EYCK,MATTHEWS </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       write_remark_group_html(fp, &Remark_Tnt);
       fprintf(fp,"</TABLE>\n");           

       fprintf(fp,"<TABLE BORDER>\n");           
       fprintf(fp, "<TR><TH> Remark Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n"); 
       fprintf(fp, "<TR><TD>  REMARK 200, X-ray Diffraction Experimental Details </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       write_remark_html(fp, Remark_200, Num_Remark_200);
       fprintf(fp,"</TABLE>\n");           

       fprintf(fp,"<TABLE BORDER>\n");           
       fprintf(fp, "<TR><TH> Remark Field Name </TH> <TH> mmCIF Data Item</TH> <TH>&nbsp</TH></TR>\n"); 
       fprintf(fp, "<TR><TD>  REMARK 280, Crystal </TD> <TD>&nbsp</TD><TD>&nbsp</TD></TR>\n");
       write_remark_html(fp, Remark_280, Num_Remark_280);
       fprintf(fp,"</TABLE>\n");           

       fclose (fp);
}

static void write_pdb_token_to_cif_latex(void)
{
       int  i, j, k;
       FILE *fp;
       char buffer[100], str_or[10], str_type[20];
       WORD cif_category;

       fp = fopen("fftoken_to_cif.tex", "w");

       latex_head(fp);
       table_head_1(fp);

       for (i = 0; i < MxTokens; i++) {
            if (NdbToken[i].NdbOnly)
                 strcpy(str_type," [NDB]");
            else strcpy(str_type," [NDB/PDB]");

            fprintf(fp, "\\hline \n");
            fprintf(fp, "\\multicolumn{3}{|l|}{{\\textbf\\LARGE %s %s}}\\\\\n",
                   NdbToken[i].TokenName,str_type);
            fprintf(fp, " &  & \\\\\n");
            fprintf(fp, "{\\it Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
            fprintf(fp, "\\hline \n");
 
            for (j = 0; j < NdbToken[i].NumField; j++) {
                 database_stored(NdbToken[i].TokenName, j+1, &cif_category, 0);
                 strcpy(buffer, NdbToken[i].FieldList[j].FieldLabel);
                 change_string(buffer);
                 fprintf(fp, "{\\tt %s} & ", buffer);

                 for (k = 0; k < cif_category.num_word; k++) {
                      change_string(cif_category.word[k]);
                      if (k < cif_category.num_word - 1) 
                           strcpy(str_or,"{\\it - and/or - }");
                      else strcpy(str_or," ");
                    
                      if (k) fprintf(fp, " & {\\tt \\_%s} & {\\tt %s}\\\\ \n", cif_category.word[k],str_or);
                      else   fprintf(fp, " {\\tt \\_%s} & {\\tt %s}\\\\ \n", cif_category.word[k], str_or);
                 }
                 if (cif_category.num_word == 0) fprintf(fp, " & \\\\ \n");
            }
            fprintf(fp, "\\hline \n");
            fprintf(fp, "\\multicolumn{3}{c}{}\\\\\n");
       }

       fprintf(fp, "\\end{longtableside}\n");
       fprintf(fp, "\\end{sloppypar}\n");
       fprintf(fp, "\\end{document}\n");

       fclose (fp);
}

static void write_pdb_remark_to_cif_latex(void)
{
       FILE *fp;
       int  i;
       char name[100], source[100];
       WORD cif_category;
     
       fp = fopen("enum_remark_to_cif.tex", "w");
       
       latex_head(fp);
       table_head(fp);


       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{|l|}{{\\textbf\\LARGE SOURCE ENUMERATIONS}}\\\\\n");
       fprintf(fp, " &  & \\\\\n");
       fprintf(fp, "{\\it Enumeration Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
       fprintf(fp, "\\hline \n");

       strcpy(name, "");
       i = 0;
       while (PdbSource_All[i].pdbname) {
            database_stored(PdbSource_All[i].NdbTokenName, PdbSource_All[i].NdbFieldNo, 
                      &cif_category, 0);
            change_string(cif_category.word[0]);
            if (strcmp(name, PdbSource_All[i].pdbname)) {
                 strcpy(source, PdbSource_All[i].pdbname);
                 change_string(source);
                 fprintf(fp, "{\\tt %s} & {\\tt \\_%s} & \\\\ \n", source, cif_category.word[0]);
            } else fprintf(fp, "    & {\\tt \\_%s } & {\\tt - and/or -}\\\\ \n", cif_category.word[0]);
            strcpy(name, PdbSource_All[i].pdbname);
            i++;
       }
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{c}{}\\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\it Remark Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\tt REMARK 3: Refinement using X-PLOR} & & \\\\ \n");
       fprintf(fp, "{\\tt REFINEMENT. }&  & \\\\ \n");
       fprintf(fp, "{\\tt PROGRAM : X-PLOR }& {\\tt \\_computing.structure\\_refinement} &  \\\\ \n");
       fprintf(fp, "{\\tt AUTHORS : BRUNGER} &  &  \\\\ \n");
       write_remark_group_latex(fp, &Remark_XPlor);

       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{c}{}\\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\it Remark Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\tt REMARK 3: Refinement using CNS} & & \\\\ \n");
       fprintf(fp, "{\\tt REFINEMENT. }&  & \\\\ \n");
       fprintf(fp, "{\\tt PROGRAM : CNS }& {\\tt \\_computing.structure\\_refinement} &  \\\\ \n");
       fprintf(fp, "{\\tt AUTHORS : BRUNGER,ADAMS,CLORE,DELANO,} &  &  \\\\ \n");
       fprintf(fp, "{\\tt           GROS,GROSSE-KUNSTLEVE,JIANG,} &  &  \\\\ \n");
       fprintf(fp, "{\\tt           KUSZEWSKI,NILGES,PANNU,READ,} &  &  \\\\ \n");
       fprintf(fp, "{\\tt           RICE,SIMONSON,WARREN} &  &  \\\\ \n");
       write_remark_group_latex(fp, &Remark_CNS);

       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{c}{}\\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\it Remark Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\tt REMARK 3: Refinement using NUCLSQ} &  &\\\\ \n");
       fprintf(fp, "{\\tt REFINEMENT.} & & \\\\ \n");
       fprintf(fp, "{\\tt PROGRAM : NUCLSQ }& {\\tt \\_computing.structure\\_refinement} & \\\\ \n");
       fprintf(fp, "{\\tt AUTHORS : WESTHOF,DUMAS,MORAS} & & \\\\ \n");
       write_remark_group_latex(fp, &Remark_Nuclsq);

       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{c}{}\\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\it Remark Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\tt REMARK 3: Refinement using PROLSQ,} & &  \\\\ \n");
       fprintf(fp, "{\\tt           CCP4, PROFFT, GPRLSA, ...} & &  \\\\ \n");
       fprintf(fp, "{\\tt REFINEMENT.} &  & \\\\ \n");
       fprintf(fp, "{\\tt PROGRAM : PROLSQ } & {\\tt \\_computing.structure\\_refinement} & \\\\ \n");
       fprintf(fp, "{\\tt AUTHORS : }& & \\\\ \n");
       write_remark_group_latex(fp, &Remark_Prolsq);

       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{c}{}\\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\it Remark Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\tt REMARK 3: Refinement using REFMAC} & &  \\\\ \n");
       fprintf(fp, "{\\tt REFINEMENT.} &  & \\\\ \n");
       fprintf(fp, "{\\tt PROGRAM : REFMAC } & {\\tt \\_computing.structure\\_refinement} & \\\\ \n");
       fprintf(fp, "{\\tt AUTHORS : MURSHUDOV,VAGIN,DODSON}& & \\\\ \n");
       write_remark_group_latex(fp, &Remark_Refmac);

       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{c}{}\\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\it Remark Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\tt REMARK 3: Refinement using REFMAC 5.XXX} & &  \\\\ \n");
       fprintf(fp, "{\\tt REFINEMENT.} &  & \\\\ \n");
       fprintf(fp, "{\\tt PROGRAM : REFMAC } & {\\tt \\_computing.structure\\_refinement} & \\\\ \n");
       fprintf(fp, "{\\tt AUTHORS : MURSHUDOV,VAGIN,DODSON}& & \\\\ \n");
       write_remark_group_latex(fp, &Remark_Refmac_5_0);

       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{c}{}\\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\it Remark Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\tt REMARK 3: Refinement using SHELXL} & & \\\\ \n");
       fprintf(fp, "{\\tt REFINEMENT.} & &\\\\ \n");
       fprintf(fp, "{\\tt PROGRAM : SHELXL} & {\\tt \\_computing.structure\\_refinement} & \\\\ \n");
       fprintf(fp, "{\\tt AUTHORS : G.M.SHELDRICK} & & \\\\ \n");
       write_remark_group_latex(fp, &Remark_Shelxl);

       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{|c|}{}\\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\it Remark Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\tt REMARK 3: Refinement using TNT} & & \\\\ \n");
       fprintf(fp, "{\\tt REFINEMENT.} & & \\\\ \n");
       fprintf(fp, "{\\tt PROGRAM : TNT} & {\\tt \\_computing.structure\\_refinement} &  \\\\ \n");
       fprintf(fp, "{\\tt AUTHORS : TRONRUD,TEN EYCK,MATTHEWS} &  & \\\\ \n");
       write_remark_group_latex(fp, &Remark_Tnt);

       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{c}{}\\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\it Remark Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\tt REMARK 200, X-ray Diffraction Experimental Details} & &\\\\ \n");
       write_remark_latex(fp, Remark_200, Num_Remark_200);
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{c}{}\\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\it Remark Field Name} &   {\\it mmCIF Data Item} & \\\\ \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "{\\tt REMARK 280, Crystal} & & \\\\ \n");
       write_remark_latex(fp, Remark_280, Num_Remark_280);
       fprintf(fp, "\\hline \n");
       
       fprintf(fp, "\\end{longtableside}\n");
       fprintf(fp, "\\end{sloppypar}\n");
       fprintf(fp, "\\end{document}\n");

       fclose (fp);
}

static void write_remark_head(void)
{
       FILE *fp;

       fp = fopen("remark_to_ndbtoken.txt", "w");

       fprintf(fp, "Remark_3_prolsq_old:\n");
       write_remark_old(fp, Remark_3_prolsq_old, Num_Remark_3_prolsq_old);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Remark_3_nuclsq_old:\n");
       write_remark_old(fp, Remark_3_nuclsq_old, Num_Remark_3_nuclsq_old);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Remark_3_xplor_old:\n");
       write_remark_old(fp, Remark_3_xplor_old, Num_Remark_3_xplor_old);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Remark_3_shelx_old:\n");
       write_remark_old(fp, Remark_3_shelx_old, Num_Remark_3_shelx_old);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Remark_3_tnt_old:\n");
       write_remark_old(fp, Remark_3_tnt_old, Num_Remark_3_tnt_old);
       fprintf(fp, "\014\n");

       fprintf(fp, "Remark_3_xplor_new:\n");
       write_remark_group(fp, &Remark_XPlor);
       fprintf(fp, "\014\n");

       fprintf(fp, "Remark_3_CNS:\n");
       write_remark_group(fp, &Remark_CNS);
       fprintf(fp, "\014\n");

       fprintf(fp, "Remark_3_nuclsq_new:\n");
       write_remark_group(fp, &Remark_Nuclsq);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Remark_3_prolsq_new:\n");
       write_remark_group(fp, &Remark_Prolsq);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Remark_3_Refmac:\n");
       write_remark_group(fp, &Remark_Refmac);
       fprintf(fp, "\014\n");

       fprintf(fp, "Remark_3_Refmac5.0:\n");
       write_remark_group(fp, &Remark_Refmac_5_0);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Remark_3_shelx_new:\n");
       write_remark_group(fp, &Remark_Shelxl);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Remark_3_tnt_new:\n");
       write_remark_group(fp, &Remark_Tnt);
       fprintf(fp, "\014\n");

       fprintf(fp, "Remark_18:\n");
       write_remark_old(fp, Remark_18, 12);
       fprintf(fp, "\014\n");

       fprintf(fp, "Remark_200:\n");
       write_remark_old(fp, Remark_200, Num_Remark_200);
       fprintf(fp, "\014\n");

       fprintf(fp, "Remark_210:\n");
       write_remark_old(fp, Remark_210, Num_Remark_210);
       fprintf(fp, "\014\n");

       fprintf(fp, "Remark_280:\n");
       write_remark_old(fp, Remark_280, Num_Remark_280);
       fprintf(fp, "\014\n");

       fprintf(fp, "Remark_3_Program:\n");
       write_remark_old(fp, Remark_3_1, Num_Remark_3_1);
       fprintf(fp, "\n\n");
       write_remark_old(fp, Remark_3_2, Num_Remark_3_2);
       fprintf(fp, "\n\n");
       fprintf(fp, "Remark_350:\n");
       write_remark_old(fp, &Remark_SYMMA, 1);
       fprintf(fp, "\n\n");
       fprintf(fp, "Remark_800:\n");
       write_remark_old(fp, Remark_800, Num_Remark_800);
       fprintf(fp, "\n\n");
       fprintf(fp, "Remark_101:\n");
       write_remark_old(fp, &Remark_101, 1);
       fprintf(fp, "\n\n");
       fprintf(fp, "Remark_102:\n");
       write_remark_old(fp, &Remark_102, 1);
       fprintf(fp, "\n\n");
/*
       fprintf(fp, "Remark_103_1:\n");
       write_remark_old(fp, Remark_103_1, Num_Remark_103_1);
       fprintf(fp, "\n\n");
       fprintf(fp, "Remark_103_2:\n");
       write_remark_old(fp, Remark_103_2, Num_Remark_103_2);
       fprintf(fp, "\n\n");
*/
       fprintf(fp, "Remark_104:\n");
       write_remark_old(fp, &Remark_104, 1);
       fprintf(fp, "\n\n");

       fclose(fp);
}

static void write_remark_to_cif_txt(void)
{
       FILE *fp;

       fp = fopen("remark_to_mmcif.txt", "w");

       fprintf(fp, "Xplor:\n");
       write_remark_group_cif(fp, &Remark_XPlor);
       fprintf(fp, "\014\n");

       fprintf(fp, "CNS:\n");
       write_remark_group_cif(fp, &Remark_CNS);
       fprintf(fp, "\014\n");

       fprintf(fp, "Nuclsq:\n");
       write_remark_group_cif(fp, &Remark_Nuclsq);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Prolsq:\n");
       write_remark_group_cif(fp, &Remark_Prolsq);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Refmac:\n");
       write_remark_group_cif(fp, &Remark_Refmac);
       fprintf(fp, "\014\n");

       fprintf(fp, "Refmac5.0 & plus:\n");
       write_remark_group_cif(fp, &Remark_Refmac_5_0);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Shelx:\n");
       write_remark_group_cif(fp, &Remark_Shelxl);
       fprintf(fp, "\014\n");
       
       fprintf(fp, "Tnt:\n");
       write_remark_group_cif(fp, &Remark_Tnt);
       fprintf(fp, "\014\n");

       fprintf(fp, "Remark_200:\n");
       write_remark_cif(fp, Remark_200, Num_Remark_200);
       fprintf(fp, "\014\n");

       fprintf(fp, "Remark_210:\n");
       write_remark_cif(fp, Remark_210, Num_Remark_210);

       fclose(fp);
}

static void html_preamble(FILE *fp, const char *title)
{
       fprintf(fp,"<HTML>\n");
       fprintf(fp,"<HEAD><TITLE> %s</TITLE></HEAD>\n",title);
       fprintf(fp,"<BODY BGCOLOR=\"#ffffff\">\n");
       fprintf(fp,"<CENTER><P><H1>%s</H1></CENTER>\n",title);
}

static void html_trailer(FILE *fp)
{
       fprintf(fp,"<CENTER>\n");
       fprintf(fp,"<FONT SIZE=2>\n"); 
       fprintf(fp,"<ADDRESS><BR>\n");
       fprintf(fp,"Nucleic Acid Database Project <BR>\n");
       fprintf(fp,"Rutgers University, Department of Chemistry<BR>\n");
       fprintf(fp,"New Brunswick, New Jersey\n");
       fprintf(fp,"</ADDRESS>\n");
       fprintf(fp,"</CENTER>\n");
       fprintf(fp,"</BODY>\n");
       fprintf(fp,"</HTML>\n");
}

static void write_remark_html(FILE *fp, REMARKS *remark, const int num_remark)
{
       int  i, j;
       char *p, line[500], word[100];
       WORD cif_category;
       char itempath[]="http://ndb-mirror-0.rutgers.edu:2010/jw/cif_ndb.dic/Items/";

       for (i = 0; i < num_remark; i++) {
            line[0] = '\0';
            word[0] = '\0';
            for (j = 0; j < remark[i].NumField; j++) {
                 if (!strcmp(remark[i].FieldList[j].TokenName, "")) {
                      if (!strcmp(remark[i].FieldList[j].Text, ";")) continue;
                      sprintf(&line[strlen(line)], " %s",
                                 remark[i].FieldList[j].Text);
                 } else {
                      if (!strcmp(remark[i].FieldList[j].TokenName, "XXX")) continue;
                      database_stored(remark[i].FieldList[j].TokenName,
                          remark[i].FieldList[j].FieldId, &cif_category, 0);
                      strcpy(word, cif_category.word[0]);
                 }
            }
            ndb_set_string_to_lower(word);
            if (remark[i].NumField > 2) {
                 p = strstr(word, ".");
                 if (p) *p = '\0';
                 fprintf(fp, "<TR><TD> %s</TD> <TD> an instance of %s</TD><TD>&nbsp</TD>\n", line, word);
            } else {
                 if (strlen(word) > 0)
                      fprintf(fp, "<TR><TD> %s</TD><TD><A HREF=\"%s_%s.html\"> _%s</A></TD> <TD>&nbsp</TD>\n", 
                              line, itempath,word,word);
                 else fprintf(fp, "<TR><TD> %s<TD>&nbsp</TD><TD>&nbsp</TD>\n", line);
            }
       }
}

static void write_remark_group_html(FILE *fp, GROUP_REMARKS *group_remark)
{
       int n;

       for (n = 0; n < group_remark->Remarks_No; n++) {
            write_remark_html(fp, group_remark->remarks[n], group_remark->num_remarks[n]);
       }
}

static void latex_head(FILE *fp)
{
       fprintf(fp, "\\documentclass[twoside]{article}\n");
       fprintf(fp, "\\usepackage{longtableside, epsfig}\n");
       fprintf(fp, "\\pagestyle{plain}\n");
       fprintf(fp, "\\thispagestyle{empty}\n");
       fprintf(fp, "\\raggedbottom\n");
       fprintf(fp, "\\oddsidemargin 0in\n");
       fprintf(fp, "\\evensidemargin 0in\n");
       fprintf(fp, "\\topmargin 0in\n");
       fprintf(fp, "\\textwidth=6in\n");
       fprintf(fp, "\\textheight=8.5in\n");
       fprintf(fp, "\\renewcommand{\\arraystretch}{1.01}\n");
       fprintf(fp, "\\begin{document}\n");
       fprintf(fp, "\n");
       fprintf(fp, "\\setlongtables\n");
       fprintf(fp, "\\begin{sloppypar}\n");
       fprintf(fp, "\\begin{longtableside}{|lll|}\n");
       fprintf(fp, "\n");
}

static void table_head(FILE *fp)
{
       fprintf(fp, "\\newpage \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{|c|}{} \\\\\n");
       fprintf(fp, "\\multicolumn{3}{|c|}{{\\huge Enumerations/Remarks $\\rightarrow$ mmCIF Correspondences}} \\\\\n");
       fprintf(fp, "\\multicolumn{3}{|c|}{} \\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\endfirsthead\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{|l|}{\\small\\sl continued from previous page}");
       fprintf(fp, " \\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\endhead \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{|r|}{\\small\\sl continued on next page} \\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\endfoot\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\endlastfoot\n");
}

static void table_head_1(FILE *fp)
{
       fprintf(fp, "\\newpage \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{|c|}{} \\\\\n");
       fprintf(fp, "\\multicolumn{3}{|c|}{{\\huge Flat File $\\rightarrow$ mmCIF Correspondences}} \\\\\n");
       fprintf(fp, "\\multicolumn{3}{|c|}{} \\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\endfirsthead\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{|l|}{\\small\\sl continued from previous page}");
       fprintf(fp, " \\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\endhead \n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\multicolumn{3}{|r|}{\\small\\sl continued on next page} \\\\\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\endfoot\n");
       fprintf(fp, "\\hline \n");
       fprintf(fp, "\\endlastfoot\n");
}

static void change_string(char *string)
{
       char buffer[200];
       unsigned int  i, j;

       strcpy(buffer, string);
       ndb_clean_string(buffer);
       i = 0;
       for (j = 0; j < strlen(buffer); j++) {
            if (buffer[j] == '_' || buffer[j] == '%') {
                 string[i++] = '\\';
                 string[i++] = buffer[j];
            } else string[i++] = buffer[j];
       }
       string[i] = '\0';
}

static void database_stored(const char *token, int field, WORD *cif_category, const int is_explicit)
{
       char name[20];
       int  id, i, j, k, card_id;

       if (!cif_category) return;

       if ((!strcmp(token, "ALTSQN") || !strcmp(token, "ALTSQR") ||
            !strcmp(token, "PDBSQN") || !strcmp(token, "PDBSQR") ||
            !strcmp(token, "NDBSQN") || !strcmp(token, "SEQRES")) &&
            field > 5) field = 5;
          
       cif_category->num_word = 0;
       for (id = 0; id < 50; id++) 
            cif_category->word[id][0] = '\0';
       if (!strcmp(token, "ATOMMN") || !strcmp(token, "ATOMMP") ||
           !strcmp(token, "ATOMP") || !strcmp(token, "HETAC") ||
           !strcmp(token, "HETAD") || !strcmp(token, "HETAI") ||
           !strcmp(token, "HETAS") || !strcmp(token, "HETIC") ||
           !strcmp(token, "HETAIN"))
            strcpy(name, "ATOMN");
       else strcpy(name, token);
       for (i = 0; i < MxCategory; i++) {
            for (j = 0; j < CifCategory[i].nkeywords; j++) {
                 for (k = 0; k < CifCategory[i].keywords[j].numNdbInfo; k++) {
                      if (!strcmp(CifCategory[i].keywords[j].NdbInfo[k].NdbTokenName, name) &&
                          strcmp(CifCategory[i].category, "rcsb_nmr_refine") &&
                          field == CifCategory[i].keywords[j].NdbInfo[k].NdbFieldNo) {
                           sprintf(cif_category->word[cif_category->num_word],
                                 "%s.%s", CifCategory[i].category,
                                 CifCategory[i].keywords[j].name);
                           if (exchange_flag) find_exchange_items(cif_category->word[cif_category->num_word]);
                           cif_category->num_word += 1;
                      }
                 }
            }
       }
       if (cif_category->num_word == 0) {
            card_id = find_card_id(token);
            for (i = 0; i < mxRmsRecords; i++) {
                 for (j = 0; j < NDB_CIF_RMS_ITEMS; j++) {
                      if (card_id == rmsRecords[i].tokens[j] &&
                          (field - 1) == rmsRecords[i].fields[j]) {
                           if (j == 0) 
                                strcpy(cif_category->word[cif_category->num_word],
                                   "refine_ls_restr.dev_ideal");
                           if (j == 1)
                                strcpy(cif_category->word[cif_category->num_word],
                                  "refine_ls_restr.dev_ideal_target");
                           if (j == 2)
                                strcpy(cif_category->word[cif_category->num_word],
                                  "refine_ls_restr.ndb_weight");
                           if (j == 3)
                                strcpy(cif_category->word[cif_category->num_word],
                                  "refine_ls_restr.ndb_count");
                           if (is_explicit) {
                                strcpy(cif_category->word[cif_category->num_word],
                                   "refine_ls_restr.type \"");
                                strcat(cif_category->word[cif_category->num_word], rmsRecords[i].type);
                                strcat(cif_category->word[cif_category->num_word], "\"");
                           } else if (exchange_flag)
                                find_exchange_items(cif_category->word[cif_category->num_word]);
                           cif_category->num_word += 1;
                           return;
                      }
                 }
            }
       }
}

static void write_remark_latex(FILE *fp, REMARKS *remark, const int num_remark)
{
       int  i, j;
       char *p, line[500], word[100];
       WORD cif_category;

       for (i = 0; i < num_remark; i++) {
            line[0] = '\0';
            word[0] = '\0';
            for (j = 0; j < remark[i].NumField; j++) {
                 if (!strcmp(remark[i].FieldList[j].TokenName, "")) {
                      if (!strcmp(remark[i].FieldList[j].Text, ";")) continue;
                      sprintf(&line[strlen(line)], " %s",
                                 remark[i].FieldList[j].Text);
                 } else {
                      if (!strcmp(remark[i].FieldList[j].TokenName, "XXX")) continue;
                      database_stored(remark[i].FieldList[j].TokenName,
                          remark[i].FieldList[j].FieldId, &cif_category, 0);
                      strcpy(word, cif_category.word[0]);
                 }
            }
            change_string(line);
            change_string(word);
            if (remark[i].NumField > 2) {
                 p = strstr(word, ".");
                 if (p) *p = '\0';
                 fprintf(fp, "{\\tt %s} & {\\tt an instance of %s} & \\\\ \n", line, word);
            } else {
              if (strlen(word) > 0)
                  fprintf(fp, "{\\tt %s} & {\\tt \\_%s} & \\\\\n", line, word);
              else
                  fprintf(fp, "{\\tt %s} & & \\\\\n", line);

           }
       }
}

static void write_remark_group_latex(FILE *fp, GROUP_REMARKS *group_remark)
{
       int n;

       for (n = 0; n < group_remark->Remarks_No; n++) {
            write_remark_latex(fp, group_remark->remarks[n], group_remark->num_remarks[n]);
       }
}

static void write_remark_old(FILE *fp, REMARKS *remark, const int num_remark)
{
       int i, j;

       for (i = 0; i < num_remark; i++) {
            for (j = 0; j < remark[i].NumField; j++) {
                 if (!strcmp(remark[i].FieldList[j].TokenName, ""))
                      fprintf(fp, "%s", remark[i].FieldList[j].Text);
                 else fprintf(fp, "   \"%s %d\"   ", remark[i].FieldList[j].TokenName,
                            remark[i].FieldList[j].FieldId);
            }
            fprintf(fp, "\n");
       }
}

static void write_remark_group(FILE *fp, GROUP_REMARKS *group_remark)
{
       int n;

       for (n = 0; n < group_remark->Remarks_No; n++) {
            fprintf(fp, ">>>>>>\n");
            write_remark_old(fp, group_remark->remarks[n], group_remark->num_remarks[n]);
            fprintf(fp, "<<<<<<\n\n");
       }
}

static void write_remark_cif(FILE *fp, REMARKS *remark, const int num_remark)
{
       int i, j, k;
       char line[500], word[100];
       WORD cif_category;

       for (i = 0; i < num_remark; i++) {
            line[0] = '\0';
            word[0] = '\0';
            sprintf(line, "REMARK %3d", remark[i].Remark_No);
            for (j = 0; j < remark[i].NumField; j++) {
                 if (!strcmp(remark[i].FieldList[j].TokenName, "")) {
                      if (!strcmp(remark[i].FieldList[j].Text, ";")) continue;
                      for (k = 0; k < remark[i].FieldList[j].FieldStCol; k++) {
                           strcat(line, " ");
                      }
                      sprintf(&line[strlen(line)], "%s",
                                 remark[i].FieldList[j].Text);
                 } else {
                      if (!strcmp(remark[i].FieldList[j].TokenName, "XXX")) continue;
                      database_stored(remark[i].FieldList[j].TokenName,
                          remark[i].FieldList[j].FieldId, &cif_category, 1);
                      strcpy(word, cif_category.word[0]);
                 }
            }
            ndb_set_string_to_lower(word);
            fprintf(fp, "%s %s\n", line, word);
       }
}

static void write_remark_group_cif(FILE *fp, GROUP_REMARKS *group_remark)
{
       int n;

       for (n = 0; n < group_remark->Remarks_No; n++) {
            write_remark_cif(fp, group_remark->remarks[n], group_remark->num_remarks[n]);
            fprintf(fp, "\n");
       }
}

static void read_binary_dictionary()
{
       unsigned int i;
       int iCol2, iCol3, iCol4, iCol5, iCol6;
       ReVarCifArray<CifString> *col0 = NULL, *col1 = NULL, *col2 = NULL;
       char binaryfile[300], *BlockName = NULL;
       struct stat stat1;

       sprintf(binaryfile, "%s/data/binary/%s", root_dir, "mmcif_pdbx.odb");
       if (!stat(binaryfile, &stat1)) {
            dict = new DICFileObj(binaryfile, READ_MODE);
            BlockName = dict->GetBlockName(0);
            aliases = dict->GetTablePtr(BlockName, "item_aliases");
            items   = dict->GetTablePtr(BlockName, "item");
            if (BlockName) delete [] BlockName;

            iCol1 = items->GetColumnIndex("name");
            col0 = items->GetColumn(iCol1);
            for (i = 0; i < col0->Length(); i++) {
              (*col0)[i].ToLower();
            }
            items->AddColumn("namelc");
            iCol2 = items->GetColumnIndex("namelc");
            items->FillColumn(*col0, iCol2);

            iCol3 = aliases->GetColumnIndex("name");
            col1 = aliases->GetColumn(iCol3);
            for (i = 0; i < col1->Length(); i++) {
              (*col1)[i].ToLower();
            }
            aliases->AddColumn("namelc");
            iCol4 = aliases->GetColumnIndex("namelc");
            aliases->FillColumn(*col1, iCol4);

            iCol5 = aliases->GetColumnIndex("alias_name");
            col2 = aliases->GetColumn(iCol5);
            for (i = 0; i < col2->Length(); i++) {
              (*col2)[i].ToLower();
            }
            aliases->AddColumn("alias_namelc");
            iCol6 = aliases->GetColumnIndex("alias_namelc");
            aliases->FillColumn(*col2, iCol6);
            if (col0) delete col0;
            if (col1) delete col1;
            if (col2) delete col2;
       }
}

static void find_exchange_items(char *item)
{
       int errCode;
       CifString cs, itemName, aliasName;
       ReVarCifArray<CifString> queryTarget1, queryTarget2;
       ReVarPCifArray<int> *queryResult1 = NULL, *queryResult2 = NULL;

       if (!items || !aliases) return;

       cs.Clear(); cs.Copy("_"); cs += item; cs.ToLower();
       queryTarget1.Clear();
       queryTarget1.Add(cs);
       queryTarget1.Add("cif_rcsb.dic");
       queryTarget1.Add("1.1");
       queryResult1 = aliases->Search(queryTarget1, queryCol1, errCode);
       if (errCode < 0) {
            queryTarget2.Clear();
            queryTarget2.Add(cs);
            queryResult2 = items->Search(queryTarget2, queryCol2, errCode);
            if (errCode >= 0) {
                 itemName.Clear();
                 items->GetCell(itemName, iCol1, (*queryResult2)[0]);
                 strcpy(item,  &(itemName.Text()[1]));
            }
            if (queryResult2) delete queryResult2;
       } else {
            aliasName.Clear();
            aliases->GetCell(aliasName, colIndex, (*queryResult1)[0]);
            strcpy(item, &(aliasName.Text()[1]));
       }
       if (queryResult1) delete queryResult1;
}
