/*
FILE:     maxit.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 <unistd.h>

#include "_maxit.h"
#include "dict_exchange.h"
#include "erf_category.h"
#include "erf_util.h"
#include "ndb_misclib.h"
#include "util.h"

void pdb_to_ndb(const char *infilename, const char *realfilename, const char *outfilename, const int format,
         const int public_flag, const int short_version, const char *support_file, const int is_nmr_view)
{
       int fd, i, StrLen;
       char ndb_sid[82], name[100], line[200], templateFileName[9] = "./XXXXXX";
       strcpy(ndb_sid,outfilename);
       StrLen = strlen(ndb_sid);
       for (i = 0; i < StrLen ; i++) {
            if (ndb_sid[i] == '.' ) break;
       }
       ndb_sid[i] = '\0';
       ndb_set_string_to_upper(ndb_sid);

       strcpy(name, templateFileName);
       fd = mkstemp(name);
       close (fd);
       FILE *tlog = fopen(name, "w");

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_PDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       if (short_version)
            obj->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 2, is_nmr_view);
       else {
            obj->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 0, is_nmr_view);
            obj->add_chain_and_solvent();
            obj->seq_alignment(0);
            obj->find_missing_residues();
            obj->ndb_check_file(tlog, NUM_TOKEN, Check_Format, NUM_CHECK_CATEGORY,
                check_category, 1, public_flag);
            obj->input_structural_features(NDB_FILE_FORMAT_PDB);
            obj->get_seqifo(0, 0);
            obj->add_modification_information();
            obj->match_stbiol_with_molecules();
            obj->get_create_secondary_structures_using_promotif(support_file);
            obj->get_entity_card();
            obj->get_origx_card();
            obj->get_scale_card();
            obj->get_title_card();
            obj->get_dchem_and_inhibt_cards();
            obj->update_residues_nomenclature(1);
            obj->get_bspair_information();
            obj->Output_Stbiol();
            obj->Output_Entity();
            obj->update_tokens(NDB_FILE_FORMAT_NDB);
       }
       obj->pdb_to_ndb_general(ndb_sid, short_version);
       obj->get_compnd_card();
       if (!short_version) {
            obj->check_atom_number_count();
            obj->output_structural_features(NDB_FILE_FORMAT_NDB);
       }
       obj->write_ndb_file(format, 0, 1, 0, short_version);
       delete obj;

       fclose (tlog);
       tlog = fopen(name, "r");
       CifString message("");
       while (!feof(tlog)) {
            fgets(line, 199, tlog);
            if (feof(tlog)) break;
            message += line;
       }
       fclose (tlog);
       sprintf(line, "rm -f %s", name);
       system(line);
       if (message != "") ndb_log_message(NDB_MSG_WARN, "%s\n", message.Text());
}

void pdb_to_cif(const char *infilename, const char *realfilename, const char *outfilename, const int is_binary,
         const int public_flag, const int standardized, const int is_iupac,
         const int id_option, const int exchange_out, const int together, const int no_deriv,
         const char *support_file, const int keep_contact_author, const int no_pdbx_strand_id, const int is_nmr_view)
{
       int fd, i, StrLen;
       char ndb_sid[82], name[100], line[200], templateFileName[9] = "./XXXXXX";
       strcpy(ndb_sid,outfilename);
       StrLen = strlen(ndb_sid);
       for (i = 0; i < StrLen ; i++) {
            if (ndb_sid[i] == '.' ) break;
       }
       ndb_sid[i] = '\0';
       ndb_set_string_to_upper(ndb_sid);

       strcpy(name, templateFileName);
       fd = mkstemp(name);
       close (fd);
       FILE *tlog = fopen(name, "w");

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_PDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 0, is_nmr_view);
       obj->add_chain_and_solvent();
       obj->seq_alignment(0);
       obj->find_missing_residues();
       obj->ndb_check_file(tlog, NUM_TOKEN, Check_Format, NUM_CHECK_CATEGORY,
                check_category, 1, public_flag);
       obj->input_structural_features(NDB_FILE_FORMAT_PDB);
       obj->get_seqifo(0, together);
       obj->add_modification_information();
       obj->match_stbiol_with_molecules();
       obj->get_entity_card();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_title_card();
       obj->get_cryst_card();
       obj->get_dchem_and_inhibt_cards();
       obj->update_residues_nomenclature(1);
       obj->get_create_secondary_structures_using_promotif(support_file);
       obj->Output_Stbiol();
       obj->Output_Entity();
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->pdb_to_ndb_general(ndb_sid, 0);
       obj->get_compnd_card();
       obj->check_atom_number_count();
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->open_mmcif_file();
       obj->get_bspair_information();
       obj->update_na_structural_features();
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 0, standardized, keep_contact_author, no_pdbx_strand_id);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, standardized,
                 is_iupac, id_option, exchange_out, together, no_deriv);
       obj->write_mmcif_file(standardized, id_option, exchange_out);
       delete obj;

       fclose (tlog);
       tlog = fopen(name, "r");
       CifString message("");
       while (!feof(tlog)) {
            fgets(line, 199, tlog);
            if (feof(tlog)) break;
            message += line;
       }
       fclose (tlog);
       sprintf(line, "rm -f %s", name);
       system(line);
       if (message != "") ndb_log_message(NDB_MSG_WARN, "%s\n", message.Text());
}

void ndb_to_pdb(const char *infilename, const char *realfilename, const char *outfilename, const int remark,
          const int is_iupac)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->read_ndb_file(0);
       obj->add_chain_and_solvent();
       obj->seq_alignment(0);
       obj->find_missing_residues();
       obj->Input_Stbiol();
       obj->input_structural_features(NDB_FILE_FORMAT_NDB);
       obj->Input_Entity();
       obj->get_seqifo(0, 0);
       obj->add_modification_information();
       obj->match_entity_with_molecules();
       obj->match_stbiol_with_molecules();
       obj->get_entity_card();
       obj->update_residues_nomenclature(1);
       obj->get_bspair_information();
       obj->Output_Stbiol();
       obj->Output_Entity();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_cryst_card();
       obj->get_het_card();
       obj->update_tokens(NDB_FILE_FORMAT_PDB);
       obj->check_atom_number_count();
       obj->output_structural_features(NDB_FILE_FORMAT_PDB);
       obj->ndb_to_pdb_general(remark);
       obj->write_ndb_file(NDB_FILE_FORMAT_PDB, 0, 1, is_iupac, 0);
       delete obj;
}

void ndb_to_ndb(const char *infilename, const char *realfilename, const char *outfilename, const int format)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->add_chain_and_solvent();
       obj->seq_alignment(0);
       obj->find_missing_residues();
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void ndb_to_cif(const char *infilename, const char *realfilename, const char *outfilename, const int is_binary,
         const int standardized, const int is_iupac, const int id_option,
         const int exchange_out, const int together, const int no_deriv,
         const int keep_contact_author, const int no_pdbx_strand_id)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->read_ndb_file(0);
       obj->add_chain_and_solvent();
       obj->seq_alignment(0);
       obj->find_missing_residues();
       obj->Input_Stbiol();
       obj->input_structural_features(NDB_FILE_FORMAT_NDB);
       obj->Input_Entity();
       obj->get_seqifo(0, together);
       obj->add_modification_information();
       obj->match_entity_with_molecules();
       obj->match_stbiol_with_molecules();
       obj->get_entity_card();
       obj->get_compnd_card();
       obj->update_residues_nomenclature(1);
       obj->get_bspair_information();
       obj->Output_Stbiol();
       obj->Output_Entity();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_cryst_card();
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->check_atom_number_count();
       obj->open_mmcif_file();
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 0, standardized, keep_contact_author, no_pdbx_strand_id);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, standardized,
                 is_iupac, id_option, exchange_out, together, no_deriv);
       obj->write_mmcif_file(standardized, id_option, exchange_out);
       delete obj;
}

void cif_to_ndb(const char *infilename, const char *realfilename, const char *outfilename, const int format,
            const int is_binary, const int special, const int exchange,
            const int short_version, const int symmetry_flag, const char *support_file, const int is_nmr_view)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(0, 1, special, 0, exchange, 0, short_version, symmetry_flag, is_nmr_view);
       obj->add_chain_and_solvent();
       obj->add_modification_information();
       obj->update_residues_nomenclature(0);
       obj->get_create_secondary_structures_using_promotif(support_file);
       obj->get_het_card();
       obj->get_dchem_and_inhibt_cards();
       obj->get_bspair_information();
       obj->output_structural_features(NDB_FILE_FORMAT_NDB);
       obj->Output_Entity();
       obj->Output_Stbiol();
       obj->update_encapsulated_cif();
       obj->get_compnd_card();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->check_atom_number_count();
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void cif_to_pdb(const char *infilename, const char *realfilename, const char *outfilename, const int remark,
            const int is_binary, const int special, const int is_iupac,
            const int exchange, const int symmetry_flag, const char *support_file, const int is_nmr_view)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(0, 1, special, 0, exchange, 0, 0, symmetry_flag, is_nmr_view);
       obj->add_chain_and_solvent();
       obj->add_modification_information();
       obj->update_residues_nomenclature(0);
       obj->find_all_links();
       obj->Output_Entity();
       obj->Output_Stbiol();
       obj->update_encapsulated_cif();
       obj->get_compnd_card();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_cryst_card();
       obj->get_create_secondary_structures_using_promotif(support_file);
       obj->get_het_card();
       obj->get_dchem_and_inhibt_cards();
       obj->get_bspair_information();
       obj->update_tokens(NDB_FILE_FORMAT_PDB);
       obj->check_atom_number_count();
       obj->output_structural_features(NDB_FILE_FORMAT_PDB);
       obj->ndb_to_pdb_general(remark);
       obj->write_ndb_file(NDB_FILE_FORMAT_PDB, 0, 1, is_iupac, 0);
       delete obj;
}

void cif_to_cif(const char *infilename, const char *realfilename, const char *outfilename, const int flag,
              const int is_binary, const int standardized, const int special,
              const int delete_terminal_atom, const int is_iupac, const int recover_asym,
              const int id_option, const int exchange_in, const int exchange_out,
              const int together, const int no_deriv, const int symmetry_flag,
              const char *support_file, const int keep_contact_author, const int no_pdbx_strand_id,
              const int is_nmr_view)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       if (special == 1)
            obj->cif_to_ndb_general(1, 1, special, recover_asym, exchange_in, together, 0, symmetry_flag, is_nmr_view);
       else obj->cif_to_ndb_general(1, 1, 0, recover_asym, exchange_in, together, 0, symmetry_flag, is_nmr_view);
       obj->add_chain_and_solvent();
       obj->add_modification_information();
       obj->update_residues_nomenclature(0);
       obj->find_all_links();
       if (!flag) obj->get_create_secondary_structures_using_promotif(support_file);
       obj->get_het_card();
       obj->get_dchem_and_inhibt_cards();
       if (flag) obj->Clear_Bspair();
       obj->get_bspair_information();
       if (flag) obj->delete_na_structural_features();
       obj->update_na_structural_features();
       obj->output_structural_features(NDB_FILE_FORMAT_NDB);
       obj->Output_Entity();
       obj->Output_Stbiol();
       obj->update_encapsulated_cif();
       obj->get_compnd_card();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_cryst_card();
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->check_atom_number_count();
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 1, standardized, keep_contact_author, no_pdbx_strand_id);
       if (delete_terminal_atom) obj->delete_terminal_atoms();
       if (special == 2) obj->correct_atom_name();
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, standardized,
                 is_iupac, id_option, exchange_out, together, no_deriv);
       obj->write_mmcif_file(standardized, id_option, exchange_out);
       delete obj;
}

void pdb_to_encapsulated_cif(const char *infilename, const char *realfilename, const char *outfilename,
        const int id_option, const int exchange_out, const int is_nmr_view)
{
       int i, StrLen;
       char ndb_sid[82];
       strcpy(ndb_sid,outfilename);
       StrLen = strlen(ndb_sid);
       for (i = 0; i < StrLen ; i++) {
            if (ndb_sid[i] == '.' ) break;
       }
       ndb_sid[i] = '\0';
       ndb_set_string_to_upper(ndb_sid);

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_PDB);
       obj->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 1, is_nmr_view);
       obj->get_origx_card();
       obj->get_title_card();
       obj->pdb_to_ndb_general(ndb_sid, 1);
       obj->delete_atom_count();
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 0, 0, 0, 0);
       obj->write_mmcif_file(0, id_option, exchange_out);
       delete obj;
}

void fractional_to_orthogonal(const char *infilename, const char *realfilename,
               const char *outfilename)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->read_mmcif_file(0);
       obj->convert_between_fractional_and_orthogonal(NDB_TRANS_FRAC_TO_ORTHO);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void fractional_to_orthogonal(const char *infilename, const char *realfilename,
              const char *outfilename, const int format)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);
       obj->convert_between_orthogonal_and_fractional(NDB_TRANS_FRAC_TO_ORTHO);
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void orthogonal_to_fractional(const char *infilename, const char *realfilename,
               const char *outfilename, const int format)
{
       int j;
       for (j = 0; j < MxAtomTokens; j++) {
            if (!(is_atom_card(AtomInfo[j].NdbTokenId-1) & AUX_ATOM)) {
                 NdbToken[AtomInfo[j].NdbTokenId-1].FieldList[8].FieldPrec = 4;
                 NdbToken[AtomInfo[j].NdbTokenId-1].FieldList[9].FieldPrec = 4;
                 NdbToken[AtomInfo[j].NdbTokenId-1].FieldList[10].FieldPrec= 4;
            }
       }

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);
       obj->convert_between_orthogonal_and_fractional(NDB_TRANS_ORTHO_TO_FRAC);
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
     
       for (j = 0; j < MxAtomTokens; j++) {
            if (!(is_atom_card(AtomInfo[j].NdbTokenId-1) & AUX_ATOM)) {
                 NdbToken[AtomInfo[j].NdbTokenId-1].FieldList[8].FieldPrec = 3;
                 NdbToken[AtomInfo[j].NdbTokenId-1].FieldList[9].FieldPrec = 3;
                 NdbToken[AtomInfo[j].NdbTokenId-1].FieldList[10].FieldPrec= 3;
            }
       }
}

void generate_sym_strands_NDB(const char *infilename, const char *realfilename,
               const char *outfilename, const int format)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->input_structural_features(NDB_FILE_FORMAT_NDB);
       obj->seq_alignment(0);
       obj->Input_Entity();
       obj->Input_Stbiol();
       obj->get_seqifo(0, 0);
       obj->match_stbiol_with_molecules();
       obj->update_residues_nomenclature(1);
       obj->generate_symmetry_strands(1);
       obj->match_entity_with_molecules();
       obj->get_entity_card();
       obj->get_het_card();
       obj->Output_Stbiol();
       obj->Output_Entity();
       obj->output_structural_features(NDB_FILE_FORMAT_NDB);
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void generate_sym_strands_CIF(const char *infilename, const char *realfilename,
              const char *outfilename, const int is_binary)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 1, 0, 0, 0, 0, 0, 0, 0);
       obj->update_residues_nomenclature(0);
       obj->generate_symmetry_strands(1);
       obj->match_entity_with_molecules();
       obj->get_entity_card();
       obj->get_het_card();
       obj->Output_Entity();
       obj->Output_Stbiol();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_cryst_card();
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 1, 0, 0, 0);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void generate_ncs_strands_NDB(const char *infilename, const char *realfilename,
              const char *outfilename, const int format)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);
       obj->Input_Stbiol();
       obj->Input_Entity();
       obj->get_seqifo(0, 0);
       obj->match_stbiol_with_molecules();
       obj->match_entity_with_molecules();
       obj->update_residues_nomenclature(1);
       obj->generate_ncs_strands();
       obj->get_entity_card();
       obj->get_het_card();
       obj->Output_Stbiol();
       obj->Output_Entity();
       obj->output_structural_features(NDB_FILE_FORMAT_NDB);
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void generate_ncs_strands_CIF(const char *infilename, const char *realfilename,
             const char *outfilename, const int is_binary)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 1, 0, 0, 0, 0, 0, 0, 0);
       obj->update_residues_nomenclature(0);
       obj->generate_ncs_strands();
       obj->update_residues_nomenclature(0);
       obj->get_het_card();
       obj->output_structural_features(NDB_FILE_FORMAT_NDB);
       obj->Output_Entity();
       obj->Output_Stbiol();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_cryst_card();
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 1, 0, 0, 0);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void restraints_correspondences_NDB(const char *infilename, const char *realfilename, const char *outfilename)
{
       FILE *fp = fopen(outfilename, "w");
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->write_restraints_correspondences(fp);
       fclose (fp);
       delete obj;
}

void restraints_correspondences_CIF(const char *infilename, const char *realfilename,
               const char *outfilename, const int is_binary)
{
       FILE *fp = fopen(outfilename, "w");
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(0, 1, 0, 0, 0, 0, 0, 0, 0);
       obj->update_residues_nomenclature(0);
       obj->Output_Entity();
       obj->write_restraints_correspondences(fp);
       fclose (fp);
       delete obj;
}

void remove_hydrogen_NDB(const char *infilename, const char *realfilename,
                 const char *outfilename, const int format)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);
       obj->write_ndb_file(format, 1, 1, 0, 0);
       delete obj;
}

void remove_hydrogen_CIF(const char *infilename, const char *realfilename,
               const char *outfilename, const int is_binary)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       int input_format = NDB_FILE_FORMAT_PDB;
       obj->read_mmcif_file(input_format, 0, 0, 0, 0, 0, 0);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 1, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void remove_Q_atoms_CIF(const char *infilename, const char *realfilename,
              const char *outfilename, const int is_binary)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       int input_format = NDB_FILE_FORMAT_PDB;
       obj->read_mmcif_file(input_format, 0, 0, 0, 0, 0, 0);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 2, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void moving_waters_NDB(const char *infilename, const char *realfilename,
               const char *outfilename, const int format)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);
       obj->moving_waters();
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void moving_waters_CIF(const char *infilename, const char *realfilename,
              const char *outfilename, const int is_binary)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 0, 0, 0, 0, 0, 0, 0, 0);
       obj->update_residues_nomenclature(1);
       obj->moving_waters();
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void correction_chirality_NDB(const char *infilename, const char *realfilename,
                  const char *outfilename, const int format)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);
       obj->ndb_check_chirality();
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void correction_chirality_CIF(const char *infilename, const char *realfilename,
                   const char *outfilename, const int is_binary)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 0, 0, 0, 0, 0, 0, 0, 0);
       obj->update_residues_nomenclature(1);
       obj->ndb_check_chirality();
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void renumber_pdb_number_CIF(const char *infilename, const char *realfilename, const char *outfilename)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 1, 0, 0, 0, 0, 0, 0, 0);
       obj->update_residues_nomenclature(1);
       obj->renumber_pdb_numbers();
       obj->get_het_card();
       obj->output_structural_features(NDB_FILE_FORMAT_NDB);
       obj->Output_Entity();
       obj->Output_Stbiol();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_cryst_card();
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 1, 0, 0, 0);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void delete_atoms_NDB(const char *infilename, const char *realfilename, const char *outfilename,
           const char *support_file, const int format)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->delete_atoms(support_file);
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void delete_atoms_CIF(const char *infilename, const char *realfilename, const char *outfilename,
           const char *support_file, const int is_binary)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 0, 0, 0, 0, 0, 0, 0, 0);
       obj->update_residues_nomenclature(1);
       obj->delete_atoms(support_file);
       obj->update_residues_nomenclature(1);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void insert_atoms_NDB(const char *infilename, const char *realfilename, const char *outfilename,
           const char *support_file, const int format)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->insert_atoms(support_file);
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void insert_atoms_CIF(const char *infilename, const char *realfilename, const char *outfilename,
           const char *support_file, const int is_binary)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 0, 0, 0, 0, 0, 0, 0, 0);
       obj->update_residues_nomenclature(1);
       obj->insert_atoms(support_file);
       obj->update_residues_nomenclature(1);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void delete_residues_NDB(const char *infilename, const char *realfilename, const char *outfilename,
           const char *support_file, const int format)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);
       obj->delete_residues(support_file);
       obj->Input_Stbiol();
       obj->Input_Entity();
       obj->get_seqifo(0, 0);
       obj->match_entity_with_molecules();
       obj->match_stbiol_with_molecules();
       obj->get_entity_card();
       obj->Output_Stbiol();
       obj->Output_Entity();
       obj->output_structural_features(NDB_FILE_FORMAT_NDB);
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void delete_residues_CIF(const char *infilename, const char *realfilename, const char *outfilename,
           const char *support_file, const int is_binary)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 1, 0, 0, 0, 0, 0, 0, 0);
       obj->delete_residues(support_file);
       obj->match_entity_with_molecules();
       obj->match_stbiol_with_molecules();
       obj->get_entity_card();
       obj->update_residues_nomenclature(1);
       obj->Output_Stbiol();
       obj->Output_Entity();
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 1, 0, 0, 0);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void insert_residues_NDB(const char *infilename, const char *realfilename, const char *outfilename,
           const char *support_file, const int format)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);
       obj->insert_residues(support_file);
       obj->Input_Stbiol();
       obj->Input_Entity();
       obj->get_seqifo(0, 0);
       obj->match_entity_with_molecules();
       obj->match_stbiol_with_molecules();
       obj->get_entity_card();
       obj->update_residues_nomenclature(1);
       obj->Output_Stbiol();
       obj->Output_Entity();
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void insert_residues_CIF(const char *infilename, const char *realfilename, const char *outfilename,
           const char *support_file, const int is_binary)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 1, 0, 0, 0, 0, 0, 0, 0);
       obj->insert_residues(support_file);
       obj->match_entity_with_molecules();
       obj->match_stbiol_with_molecules();
       obj->get_entity_card();
       obj->update_residues_nomenclature(1);
       obj->Output_Stbiol();
       obj->Output_Entity();
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 1, 0, 0, 0);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void merge_remarks_NDB(const char *infilename, const char *realfilename, const char *outfilename,
               const char *support_file, const int format)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);

       _MAXIT *rmk = new _MAXIT(MxTokens, MxJrnlTokens);
       rmk->set_input_filename(support_file);
       rmk->set_real_filename(support_file);
       rmk->set_input_format(NDB_FILE_FORMAT_PDB);
       rmk->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 0, 0);
       rmk->set_experiment_type(obj->experiment_type());
       rmk->set_refine_program(obj);
       rmk->pdb_to_ndb_proc_remarks();
       rmk->pdb_to_ndb_postprocessing();

       obj->merge_remarks(rmk);
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
       delete rmk;
}

void merge_remarks_CIF(const char *infilename, const char *realfilename, const char *outfilename,
               const char *support_file, const int is_binary)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(2, 1, 0, 0, 0, 0, 0, 0, 0);

       _MAXIT *rmk = new _MAXIT(MxTokens, MxJrnlTokens);
       rmk->set_input_filename(support_file);
       rmk->set_real_filename(support_file);
       rmk->set_input_format(NDB_FILE_FORMAT_PDB);
       rmk->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 0, 0);
       rmk->set_experiment_type(obj->experiment_type());
       rmk->set_refine_program(obj);
       rmk->pdb_to_ndb_proc_remarks();
       rmk->pdb_to_ndb_postprocessing();

       obj->merge_remarks(rmk);

       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 2, 0, 0, 0);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
       delete rmk;
}

void merge_coordinates_NDB(const char *infilename, const char *realfilename, const char *outfilename,
               const char *support_file, const int format)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);
       obj->input_structural_features(NDB_FILE_FORMAT_NDB);
       obj->output_structural_features(NDB_FILE_FORMAT_PDB);
       obj->remove_secondary_structures();
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->get_seqres_from_mapping(NDB_FILE_FORMAT_PDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       char line[200];
       FILE *in = fopen(support_file, "r");
       FILE *out = fopen("special_cif_output_file", "w");
       while (!feof(in)) {
            fgets(line, 199, in);
            if (feof(in)) break;
            line[strlen(line)-1] = '\0';
            if (!strncmp(line, "HELIX", 5) ||
                !strncmp(line, "SHEET", 5) ||
                !strncmp(line, "TURN", 4) ||
                !strncmp(line, "SSBOND", 6) ||
                !strncmp(line, "LINK", 4) ||
                !strncmp(line, "HYDBND", 6) ||
                !strncmp(line, "SLTBRG", 6) ||
                !strncmp(line, "MODEL", 5) ||
                !strncmp(line, "ATOM", 4) ||
                !strncmp(line, "HETATM", 6) ||
                !strncmp(line, "TER", 3) ||
                !strncmp(line, "ENDMDL", 6) ||
                !strncmp(line, "ATOMP", 5) ||
                !strncmp(line, "ATOMN", 5) ||
                !strncmp(line, "ATOMS", 5) ||
                !strncmp(line, "ATOMMN", 6) ||
                !strncmp(line, "ATOMMP", 6) ||
                !strncmp(line, "HETAD", 5) ||
                !strncmp(line, "HETAI", 5) ||
                !strncmp(line, "HETIC", 5) ||
                !strncmp(line, "HETAC", 5) ||
                !strncmp(line, "HETAIN", 6) ||
                !strncmp(line, "HETAS", 5) ||
                !strncmp(line, "SIGATM", 6) ||
                !strncmp(line, "ANISOU", 6) ||
                !strncmp(line, "SIGUIJ", 6) ||
                !strncmp(line, "END", 3))
                 fprintf(out, "%s\n", line);
       }
       fprintf(out, "END\n");
       fclose (in);
       fclose (out);
       obj->set_input_filename("special_cif_output_file");
       obj->set_real_filename("special_cif_output_file");
       obj->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 0, 0);
       obj->check_chnids();
       obj->set_input_filename(infilename);
       system("rm special_cif_output_file");
       obj->seq_alignment(0);
       obj->find_missing_residues();
       obj->input_structural_features(NDB_FILE_FORMAT_PDB);
       obj->get_seqifo(0, 0);
       obj->match_stbiol_with_molecules();
       obj->get_entity_card();
       obj->get_compnd_card();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_title_card();
       obj->update_residues_nomenclature(1);
       obj->Output_Stbiol();
       obj->Output_Entity();
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->switch_residue_name_from_sequences();
       obj->get_create_secondary_structures_using_promotif("");
       obj->output_structural_features(NDB_FILE_FORMAT_NDB);
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void merge_coordinates_CIF(const char *infilename, const char *realfilename, const char *outfilename,
               const char *support_file, const int is_binary)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 1, 0, 0, 0, 0, 0, 0, 0);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->get_seqres_from_mapping(NDB_FILE_FORMAT_PDB);
       obj->delete_Seqs();
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->output_structural_features(NDB_FILE_FORMAT_PDB);
       obj->remove_secondary_structures();

       char line[200];
       FILE *in = fopen(support_file, "r");
       FILE *out = fopen("special_cif_output_file", "w");
       while (!feof(in)) {
            fgets(line, 199, in);
            if (feof(in)) break;
            line[strlen(line)-1] = '\0';
            if (!strncmp(line, "HELIX", 5) ||
                !strncmp(line, "SHEET", 5) ||
                !strncmp(line, "TURN", 4) ||
                !strncmp(line, "SSBOND", 6) ||
                !strncmp(line, "LINK", 4) ||
                !strncmp(line, "HYDBND", 6) ||
                !strncmp(line, "SLTBRG", 6) ||
                !strncmp(line, "CRYST1", 6) ||
                !strncmp(line, "MODEL", 5) ||
                !strncmp(line, "ATOM", 4) ||
                !strncmp(line, "HETATM", 6) ||
                !strncmp(line, "TER", 3) ||
                !strncmp(line, "ENDMDL", 6) ||
                !strncmp(line, "ATOMP", 5) ||
                !strncmp(line, "ATOMN", 5) ||
                !strncmp(line, "ATOMS", 5) ||
                !strncmp(line, "ATOMMN", 6) ||
                !strncmp(line, "ATOMMP", 6) ||
                !strncmp(line, "HETAD", 5) ||
                !strncmp(line, "HETAI", 5) ||
                !strncmp(line, "HETIC", 5) ||
                !strncmp(line, "HETAC", 5) ||
                !strncmp(line, "HETAIN", 6) ||
                !strncmp(line, "HETAS", 5) ||
                !strncmp(line, "SIGATM", 6) ||
                !strncmp(line, "ANISOU", 6) ||
                !strncmp(line, "SIGUIJ", 6) ||
                !strncmp(line, "END", 3))
                 fprintf(out, "%s\n", line);
       }
       fprintf(out, "END\n");
       fclose (in);
       fclose (out);
       obj->get_biol_unit_formation();
       obj->set_input_filename("special_cif_output_file");
       obj->set_real_filename("special_cif_output_file");
       obj->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 0, 0);
       obj->check_cryst1_card();
       obj->check_chnids();
       obj->set_input_filename(infilename);
       system("rm special_cif_output_file");

       obj->seq_alignment(0);
       obj->find_missing_residues();
       obj->input_structural_features(NDB_FILE_FORMAT_PDB);
       obj->put_biol_unit_formation();
       obj->get_seqifo(0, 0);
       obj->match_stbiol_with_molecules();
       obj->get_entity_card();
       obj->get_compnd_card();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_title_card();
       obj->get_het_card();
       obj->update_residues_nomenclature(0);
       obj->Output_Stbiol();
       obj->Output_Entity();
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->switch_residue_name_from_sequences();
       obj->get_create_secondary_structures_using_promotif("");
       obj->remove_ebi_remark_500();
       obj->check_atom_number_count();
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 1, 0, 0, 0);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void switch_residue_names_NDB(const char *infilename, const char *realfilename,
              const char *outfilename, const int format)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);
       obj->input_structural_features(NDB_FILE_FORMAT_NDB);
       obj->output_structural_features(NDB_FILE_FORMAT_NDB);
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void switch_residue_names_CIF(const char *infilename, const char *realfilename,
             const char *outfilename, const int is_binary)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_is_binary(is_binary);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 1, 0, 0, 0, 0, 0, 0, 0);
       obj->add_modification_information();
       obj->update_residues_nomenclature(0);
       obj->switch_residue_name_from_sequences();
       obj->get_het_card();
       obj->Output_Entity();
       obj->Output_Stbiol();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_cryst_card();
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 1, 0, 0, 0);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void change_coordinates_symm_CIF(const char *infilename, const char *realfilename,
             const char *outfilename, const char *support_file)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       int input_format = NDB_FILE_FORMAT_PDB;
       obj->read_mmcif_file(input_format, 0, 0, 0, 0, 0, 0);
       obj->change_coordinates_symm(support_file);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void change_coordinates_non_symm_CIF(const char *infilename, const char *realfilename,
            const char *outfilename, const char *support_file)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       int input_format = NDB_FILE_FORMAT_PDB;
       obj->read_mmcif_file(input_format, 0, 0, 0, 0, 0, 0);
       obj->change_coordinates_non_symm(support_file);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void get_virus_matrix_from_remark_350(const char *infilename, const char *realfilename,
                const char *outfilename, const int add_identical)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->read_mmcif_file(0);
       obj->get_virus_matrix_from_remark_350(add_identical);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void update_hydrogen_nomenclature(const char *infilename, const char *realfilename, const char *outfilename)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 0, 0);
       obj->add_remark_90();
       obj->ndb_check_chirality();
       obj->switch_hydrogen_labeling();
       obj->write_ndb_file(NDB_FILE_FORMAT_PDB, 0, 0, 1, 0);
       delete obj;
}

void classification(const char *infilename, const char *realfilename, FILE *summary, FILE *summary1)
{
       int i, StrLen;
       char ndb_sid[82], het_list[1000];
       strcpy(ndb_sid, infilename);
       StrLen = strlen(ndb_sid);
       for (i = 0; i < StrLen ; i++) {
            if (ndb_sid[i] == '.' ) break;
       }
       ndb_sid[i] = '\0';
       ndb_set_string_to_upper(ndb_sid);

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_input_format(NDB_FILE_FORMAT_PDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 0, 0);
       obj->seq_alignment(0);
       obj->get_seqifo(0, 0);
       obj->match_stbiol_with_molecules();
       obj->update_residues_nomenclature(1);
       obj->find_all_links();
       obj->pdb_to_ndb_general(ndb_sid, 0);
       obj->classification(summary, summary1, het_list);
       delete obj;

       char *p = NULL;
       if (strcmp(het_list, "")) {
            FILE *fp = fopen("RELEASE_HET_LIST", "a");
            p = het_list;
            while (p) {
                 p = get_word(p, ndb_sid);
                 fprintf(fp, "%s\n", ndb_sid);
            }
            fclose (fp);
       }
}

void check_file(const char *filename, const char *realfilename, FILE *fp, const int public_flag)
{
       CifString message;
       fprintf(fp, "\n%s:\n\n", filename);
       _VALIDATION *obj = new _VALIDATION(MxTokens, MxJrnlTokens);
       obj->set_input_filename(filename);
       obj->set_real_filename(realfilename);
       obj->set_input_format(NDB_FILE_FORMAT_PDB);
       obj->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 0, 0);
       obj->seq_alignment(0);
       obj->ndb_check_file(fp, NUM_TOKEN, Check_Format, NUM_CHECK_CATEGORY,
                check_category, 0, public_flag);
       obj->check_terminal_atoms(message);
       if (strcmp(message.Text(), "")) fprintf(fp, "%s", message.Text());
       delete obj;
}

void cif_layer1_to_layer2(const char *infilename, const char *realfilename, const char *outfilename)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       int input_format = NDB_FILE_FORMAT_PDB;
       obj->read_mmcif_file(input_format, 0, 0, 0, 0, 0, 0);
       obj->cif_process_revdat();
       obj->cif_layer1_to_layer2_revdat();
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void merge_waters_CIF(const char *infilename, const char *realfilename, const char *outfilename)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 1, 0, 0, 0, 0, 0, 0, 0);
       obj->merge_waters();
       obj->get_entity_card();
       obj->add_chain_and_solvent();
       obj->add_modification_information();
       obj->update_residues_nomenclature(0);
       obj->match_entity_with_molecules();
       obj->match_stbiol_with_molecules();
       obj->find_all_links();
       obj->get_het_card();
       obj->get_bspair_information();
       obj->output_structural_features(NDB_FILE_FORMAT_NDB);
       obj->Output_Entity();
       obj->Output_Stbiol();
       obj->update_encapsulated_cif();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_cryst_card();
       obj->update_tokens(NDB_FILE_FORMAT_NDB);
       obj->check_atom_number_count();
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->ndb_to_cif_general(CIF_NDB_TYPE, 1, 0, 0, 0);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void replace_residues_NDB(const char *infilename, const char *realfilename, const char *outfilename,
           const char *support_file, const int format)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_NDB);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->read_ndb_file(0);
       obj->seq_alignment(0);
       obj->replace_residues(support_file);
       obj->update_residues_nomenclature(1);
       obj->write_ndb_file(format, 0, 1, 0, 0);
       delete obj;
}

void replace_residues_CIF(const char *infilename, const char *realfilename, const char *outfilename,
           const char *support_file)
{
       if (support_file == NULL || !strcmp(support_file, "")) return;

       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_NDB);
       obj->cif_to_ndb_general(1, 0, 0, 0, 0, 0, 0, 0, 0);
       obj->update_residues_nomenclature(1);
       obj->replace_residues(support_file);
       obj->update_residues_nomenclature(1);
       obj->ndb_to_cif_coordinates(NDB_FILE_FORMAT_PDB, 0, 0, 0, 0, 0, 0, 0);
       obj->write_mmcif_file(0, 0, 0);
       delete obj;
}

void search_complexity(const char *infilename, const char *realfilename)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->read_mmcif_file(0);
       obj->search_complexity();
       delete obj;
}

void cif_to_biol_unit_pdb(const char *infilename, const char *realfilename, const char *outfilename,
             const int exchange, const int pdbids)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->cif_to_ndb_exchange(exchange, 0);
       obj->output_structural_features(NDB_FILE_FORMAT_PDB);
       obj->get_het_card();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_cryst_card();
       obj->update_tokens(NDB_FILE_FORMAT_PDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->generate_symmetry_and_ncs_related_strands(outfilename, pdbids);
       obj->write_pdb_files(outfilename, pdbids);
       delete obj;
}

void exchange_cif_to_internal_cif(const char *infilename, const char *realfilename, const char *outfilename)
{
       int fd;
       char buffer[100], *diags = NULL, line[100], templateFileName[9] = "./XXXXXX";
       CifFileObj *obj;

       strcpy(buffer, templateFileName);
       fd = mkstemp(buffer);
       close (fd);
       obj = new CifFileObj(buffer, WRITE_MODE);
       obj->Read(realfilename, diags);
       if (diags && strlen(diags)) {
            ndb_log_message(NDB_MSG_ERR, "%s: Read cif file failed\n%s\n",
                         infilename, diags);
            exit (1);
       }
       set_exchange_status(EXCHANGE_TO_INTERNAL);
       obj = dictionary_exchange(obj, buffer);
       obj->Write(outfilename);
       delete obj;
       sprintf(line, "rm -f %s", buffer);
       system(line);
}

void cif_to_packing_pdb(const char *infilename, const char *realfilename,
              const char *outfilename, const int exchange)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_MMCIF);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->cif_to_ndb_exchange(exchange, 0);
       obj->generate_packing();
       obj->output_structural_features(NDB_FILE_FORMAT_PDB);
       obj->get_het_card();
       obj->get_origx_card();
       obj->get_scale_card();
       obj->get_cryst_card();
       obj->update_tokens(NDB_FILE_FORMAT_PDB);
       obj->set_output_format(NDB_FILE_FORMAT_PDB);
       obj->write_pdb_files(outfilename, 0);
       delete obj;
}

void get_one_letter_code_from_coordinates(const char *infilename, const char *realfilename, const char *outfilename)
{
       _MAXIT *obj = new _MAXIT(MxTokens, MxJrnlTokens);
       obj->set_input_filename(infilename);
       obj->set_real_filename(realfilename);
       obj->set_output_filename(outfilename);
       obj->set_input_format(NDB_FILE_FORMAT_PDB);
       obj->read_ndb_pdb_file(NDB_FILE_FORMAT_PDB, 0, 0);
       obj->get_one_letter_code_from_coordinates();
       delete obj;
}

void get_hpub_letter(const char *filename)
{
       int fd, i, index_1, index_2, rowNo;
       char buffer[200], *diags = NULL, Block_ID[100], **blockName = NULL, line[200];
       char templateFileName[9] = "./XXXXXX";
       CifFileObj *fobj = NULL;
       CifString PDB_ID, RCSB_ID, Title, Status, REL_Date, EXP_Type, REL_EXP_Date;
       CifString Author, Jrnl_Title, Jrnl, Volume, Page, Year, cifstring, citation_id;
       FILE *fp = NULL;
       ISTable *Table = NULL;

       PDB_ID.Clear(); RCSB_ID.Clear(); Title.Clear(); Status.Clear(); REL_Date.Clear();
       EXP_Type.Clear(); REL_EXP_Date.Clear(); Author.Clear(); Jrnl_Title.Clear();
       Jrnl.Clear(); Volume.Clear(); Page.Clear(); Year.Clear();

       strcpy(buffer, templateFileName);
       fd = mkstemp(buffer);
       close (fd);
       fobj = new CifFileObj(buffer, WRITE_MODE);
       fobj->Read(filename, diags);
       if (diags && strlen(diags)) {
            fprintf(stdout, "Read cif file %s failed\n%s\n", filename, diags);
            if (diags) delete [] diags;
            if (fobj) delete fobj;
            return;
       }

       blockName = fobj->GetBlockNames(rowNo);
       strcpy(Block_ID, blockName[0]);
       for (i = 0; i < rowNo; i++) delete [] blockName[i];
       delete [] blockName;

       Table = fobj->GetTablePtr(Block_ID, "database_2");
       if (!is_empty_table(Table)) {
            index_1 = Table->GetColumnIndex("database_id");
            index_2 = Table->GetColumnIndex("database_code");
            rowNo = Table->GetNumRows();
            for (i = 0; i < rowNo; i++) {
                 get_value(cifstring, Table, index_1, i);
                 if (cifstring == "PDB") {
                      get_value(PDB_ID, Table, index_2, i);
                 } else if (cifstring == "RCSB") {
                      get_value(RCSB_ID, Table, index_2, i);
                 }
            }
       }

       Table = fobj->GetTablePtr(Block_ID, "struct");
       if (!is_empty_table(Table)) {
            index_1 = Table->GetColumnIndex("title");
            get_value(Title, Table, index_1, 0);
       }

       Table = fobj->GetTablePtr(Block_ID, "ndb_database_status");
       if (!is_empty_table(Table)) {
            index_1 = Table->GetColumnIndex("dep_release_code_coordinates");
            get_value(Status, Table, index_1, 0);
            index_1 = Table->GetColumnIndex("date_of_NDB_release");
            get_value(REL_Date, Table, index_1, 0);
            index_1 = Table->GetColumnIndex("recvd_struct_fact");
            get_value(cifstring, Table, index_1, 0);
            if (cifstring == "Y" || cifstring == "y") {
                 EXP_Type.Copy("X-RAY");
                 index_1 = Table->GetColumnIndex("date_hold_struct_fact");
                 get_value(REL_EXP_Date, Table, index_1, 0);
                 if (REL_EXP_Date == "") REL_EXP_Date.Copy(REL_Date);
            } else {
                 index_1 = Table->GetColumnIndex("recvd_nmr_constraints");
                 get_value(cifstring, Table, index_1, 0);
                 if (cifstring == "Y" || cifstring == "y") {
                      EXP_Type.Copy("NMR");
                      index_1 = Table->GetColumnIndex("date_hold_nmr_constraints");
                      get_value(REL_EXP_Date, Table, index_1, 0);
                      if (REL_EXP_Date == "") REL_EXP_Date.Copy(REL_Date);
                 }
            }
       }

       Table = fobj->GetTablePtr(Block_ID, "citation");
       if (!is_empty_table(Table)) {
            index_1 = Table->GetColumnIndex("title");
            get_value(Jrnl_Title, Table, index_1, 0);
            index_1 = Table->GetColumnIndex("journal_abbrev");
            get_value(Jrnl, Table, index_1, 0);
            index_1 = Table->GetColumnIndex("journal_volume");
            get_value(Volume, Table, index_1, 0);
            index_1 = Table->GetColumnIndex("page_first");
            get_value(Page, Table, index_1, 0);
            index_1 = Table->GetColumnIndex("page_last");
            get_value(cifstring, Table, index_1, 0);
            if (cifstring != "") {
                 if (Page != "") {
                      Page += " - ";
                      Page += cifstring;
                 } else Page.Copy(cifstring);
            }
            index_1 = Table->GetColumnIndex("year");
            get_value(Year, Table, index_1, 0);

            index_1 = Table->GetColumnIndex("id");
            get_value(citation_id, Table, index_1, 0);

            Table = fobj->GetTablePtr(Block_ID, "citation_author");
            if (!is_empty_table(Table)) {
                 index_1 = Table->GetColumnIndex("citation_id");
                 index_2 = Table->GetColumnIndex("name");
                 rowNo = Table->GetNumRows();
                 for (i = 0; i < rowNo; i++) {
                      get_value(cifstring, Table, index_1, i);
                      if (cifstring != citation_id) continue;
                      get_value(cifstring, Table, index_2, i);
                      if (cifstring == "") continue;
                      if (Author != "") Author += ", ";
                      Author += cifstring;
                 }
            }
       }

       delete fobj;
       sprintf(line, "rm -f %s", buffer);
       system(line);

       sprintf(buffer, "%s.HPUB.letter", RCSB_ID.Text());
       fp = fopen(buffer, "w");
       if (strcasecmp(Status.Text(), "HOLD FOR PUBLICATION")) {
            fprintf(fp, "The _ndb_database_status.dep_release_code_coordinates '%s' for %s ",
                           Status.Text(), RCSB_ID.Text());
            fprintf(fp, "is not HOLD FOR PUBLICATION\n");
            fclose (fp);
            return;
       }

       get_date(buffer);
       fprintf(fp, "                                                            %s\n\n", buffer);
       fprintf(fp, "Dear Depositors,\n\n");
       fprintf(fp, "This message is to inform you that your structure PDB ID %s\n", PDB_ID.Text());
       fprintf(fp, "and RCSB ID %s entitled:\n\n", RCSB_ID.Text());
       fprintf(fp, "================================== TITLE ==================================\n");
       diags = Title.Text();
       while (diags) {
            diags = get_max_length_word(diags, buffer, 75);
            if (!strcmp(buffer, "")) continue;
            fprintf(fp, "%s\n", buffer);
       }
       fprintf(fp, "===========================================================================\n\n");
       fprintf(fp, "which was deposited with release instructions, HOLD FOR PUBLICATION, will\n");
       fprintf(fp, "be released on -->%s<--.\n", REL_Date.Text());
       if (EXP_Type == "X-RAY") {
            fprintf(fp, "Your structure factor data will also be released on %s\n", REL_EXP_Date.Text());
       } else if (EXP_Type == "NMR") {
            fprintf(fp, "Your NMR constraints data will also be released on %s\n", REL_EXP_Date.Text());
       }
       fprintf(fp, "\nYour entry has been updated with the following primary citation:\n\n");

       fprintf(fp, "Author(s): ");
       diags = Author.Text();
       index_1 = 1;
       while (diags) {
            diags = get_max_length_word(diags, buffer, 64);
            if (!strcmp(buffer, "")) continue;
            if (index_1) {
                 index_1 = 0;
                 fprintf(fp, "%s\n", buffer);
            } else fprintf(fp, "           %s\n", buffer);
       }
       
       fprintf(fp, "\nTitle:     ");
       diags = Jrnl_Title.Text();
       index_1 = 1;
       while (diags) {
            diags = get_max_length_word(diags, buffer, 64);
            if (!strcmp(buffer, "")) continue;
            if (index_1) {
                 index_1 = 0;
                 fprintf(fp, "%s\n", buffer);
            } else fprintf(fp, "           %s\n", buffer);
       }
                 
       fprintf(fp, "\nJournal:   ");
       diags = Jrnl.Text();
       index_1 = 1;
       while (diags) {
            diags = get_max_length_word(diags, buffer, 64);
            if (!strcmp(buffer, "")) continue;
            if (index_1) {
                 index_1 = 0;
                 fprintf(fp, "%s\n", buffer);
            } else fprintf(fp, "           %s\n", buffer);
       }

       fprintf(fp, "Volume:    %s\n", Volume.Text());
       fprintf(fp, "Page:      %s\n", Page.Text());
       fprintf(fp, "Year:      %s\n", Year.Text());

       fprintf(fp, "\nSincerely,\n\nThe RCSB PDB Annotators\n");
       fclose (fp);
}
