/*
FILE:     text_parser.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. 
*/
/* the input coordinate file either is PDF format or CIF */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#include "util.h"

void values_in_bracket(FILE *inp, char *first_item, char *value, int *key);
void extract_infor_for_xray(FILE *fout, char *inpfile);
void extract_infor_for_common(FILE *fout, char *inpfile);
void write_values(FILE *fout, FILE *finp, int nitem, char *id1, char *id2,
                  char *id3, char *id4, char *id5, char *id6, char *id7,
                  char *id8, char *id9);
void initialize_id(char *id1,char *id2,char *id3,char *id4,char *id5,
                   char *id6, char *id7,char *id8, char *id9);
char *make_string(char *str1, char *str2, char *str3);
void test_sequence_correction(char *value);


//----------------------------------------------------------------//

void parse_adit_text_to_mmcif(FILE *fout, char *inpfile)
// parse information from text (in lib) to mmcif
{
    extract_infor_for_common(fout, inpfile);
    extract_infor_for_xray(fout, inpfile);
}

void values_in_bracket(FILE *inp, char *first_item, char *value, int *key)
/* first_item: the identifier.  
   value: the total characters in " " <>.
   key: =0 not the end, =1 end of file
*/
{
    int i=0,j=0,n=1, m=0,k=0;
    int c;
    char *tmp=NULL, lett[10000];
    
    *key=0;
/* skip every letter until < is met */
    for(j=0; (c=getc(inp)) != '<' ; j++){
        if(c==EOF)  {
            *key=1;
            return;
        }
    }
/*take off the space between < and the first letter */    
    for(j=0; (c=getc(inp)) == ' ' ; j++){
        if(c==EOF)  {
            *key=1;
            return;
        }
    }
    lett[0]=c; 
    for(j=0; (c=getc(inp)) != '>' ; j++){
        if(c==EOF)  {
            *key=1;
            return;
        }
//        if(c!='\n'){   /* omit \n, put all the chars in one line.*/                
            lett[n]=c;
            n=n+1;
//        }
    }
    lett[n] = '\0';
    if(pattern5(lett, "=", "\"", "\"", "", "")<=0)return;
    tmp = new char[n+1];

    i=strcspn(lett,"="); /* get the first item */
    m=0;
    for (k=0;k<i; ++k){
        if(isspace(lett[k])) continue;
        
        first_item[m]=lett[k];
        m++;
    }
    first_item[m]='\0';
        

    strcpy(tmp, strchr(lett,'=')+1);  /*get string after the first word */
    j=0;
    n=strlen(tmp);
    for (i=0;i<n; ++i){
        if((tmp[i]!='\"')){
            value[j]=tmp[i];
            j++;
        }
    }    
    value[j]='\0';
    rid_of_front_space(value);
    rid_of_end_space(value);
    delete [] tmp;
    
}


void extract_infor_for_common(FILE *fout, char *inpfile)
{
    char *value, item[80], tmp[60];
    char id1[60],id2[60],id3[60],id4[60],id5[60],id6[60],id7[60],id8[60],id9[60];
    int key, n1=0, n2=0, nitem=0, m1, num_citation=0;
    FILE *finp=NULL;

    value = new char[10000];
    if((finp=fopen(inpfile, "r")) ==NULL)
        open_file_error(inpfile, "extract_infor_for_common");
    
//CATEGORY 2:   Molecular Entity Sequence
    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_entity_poly.entity_id\n");
    fprintf(fout,"_entity_poly.type\n");
    fprintf(fout,"_entity_poly.pdbx_seq_one_letter_code\n");
    fprintf(fout,"_entity_poly.pdbx_strand_id\n");
    
    nitem = 4;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"entity_id");
    strcpy(id2,"entity_type");
    strcpy(id3,"one_letter");
    strcpy(id4,"chain_id");
    
    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7 ,id8,id9 );

//CATEGORY 3, (Contact Authors for PI & others)
    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_audit_contact_author.name\n");
    fprintf(fout,"_audit_contact_author.email\n");
    fprintf(fout,"_audit_contact_author.phone\n");
    fprintf(fout,"_audit_contact_author.fax\n");
    fprintf(fout,"_audit_contact_author.address\n");

    nitem = 5;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"contact_author_PI_name");
    strcpy(id2,"contact_author_PI_email");
    strcpy(id3,"contact_author_PI_phone");
    strcpy(id4,"contact_author_PI_fax");
    strcpy(id5,"contact_author_PI_address");

    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7,id8,id9 );

    nitem = 5;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"contact_author_name");
    strcpy(id2,"contact_author_email");
    strcpy(id3,"contact_author_phone");
    strcpy(id4,"contact_author_fax");
    strcpy(id5,"contact_author_address");

    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7,id8,id9  );

//CATEGORY 4,  (Release Status)    
    rewind(finp);
    while(1) { 
        values_in_bracket(finp,  item, value,  &key);
        if(key>0)break;
        if( strlen(value)<=0 ) continue;

        if(strstr_case(item, "Release_status_for_coordinates")){
            fprintf(fout,"_pdbx_database_status.dep_release_code_coordinates ");
            fprintf(fout,"\n;%s\n; \n",value);
        }else if(strstr_case(item, "Release_status_for_NMR_constraints")){
            fprintf(fout,"_pdbx_database_status.dep_release_code_nmr_constraints ");
            fprintf(fout," \n;%s\n;\n",value);
        }else if(strstr_case(item, "Release_status_for_structure_factor")){
            fprintf(fout,"_pdbx_database_status.dep_release_code_struct_fact ");
            fprintf(fout," \n;%s\n;\n",value);
        }else if(strstr_case(item, "Release_status_for_sequence")){
            fprintf(fout,"_pdbx_database_status.dep_release_code_sequence ");
            fprintf(fout," \n;%s\n;\n",value);
       
//CATEGORY 5 (Structure Title)        
        }else if(!strcmp_case(item, "structure_title")){  
            fprintf(fout,"\n_struct.title \n;%s\n;\n",value);

        }       
    }

//CATEGORY 6, (Citation Authors)
    
    n1=0;
    n2=0;
    rewind(finp); 
    strcpy(value,"");
    
    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_citation_author.citation_id\n");
    fprintf(fout,"_citation_author.ordinal\n");
    fprintf(fout,"_citation_author.name\n");
    
    while(1) { 
        values_in_bracket(finp,  item, value,  &key);
        if(key>0)break;
        if( strlen(value)<=0 ) continue;
        
        if(strstr_case(item, "primary_citation_author_name")){
            n1++;
            fprintf(fout,"primary  %d  \n;%s\n;\n", n1, value);
        }

        if(pattern5(item, "citation_", "_author_name_", "", "", "")>0 &&
           strlen(value)>0 && !strstr_case(item, "primary")){
            n2++;
            m1 = get_int(item, "citation_", "_author_name");
//            m2 = atoi(strstr_case(item,"name_")+5);
            fprintf(fout,"%d  %d  \n;%s\n;\n", m1, n2, value);
            num_citation = m1;
            
        }
        
    }
    if(n1==0 && n2==0){
        fprintf(fout,"? ? ?\n");
    }
    

//CATEGORY 7, (Citation Article)
    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_citation.id \n");
    fprintf(fout,"_citation.journal_abbrev \n");
    fprintf(fout,"_citation.title \n");
    fprintf(fout,"_citation.year \n");
    fprintf(fout,"_citation.journal_volume \n");
    fprintf(fout,"_citation.page_first \n");
    fprintf(fout,"_citation.page_last\n");
    
    nitem = 6;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"primary_citation_journal_abbrev");
    strcpy(id2,"primary_citation_title");
    strcpy(id3,"primary_citation_year");
    strcpy(id4,"primary_citation_journal_volume");
    strcpy(id5,"primary_citation_page_first");
    strcpy(id6,"primary_citation_page_last");
    
    fprintf(fout, "primary  ");
    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7 ,id8,id9 );
    

    for(m1=1; m1<=num_citation; m1++){
        sprintf(tmp, "%d", m1);
        
        nitem = 6;
        initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
        strcpy(id1, make_string("citation_", (const char*)tmp, "_journal_abbrev"));
        strcpy(id2, make_string("citation_", (const char*)tmp, "_title"));
        strcpy(id3, make_string("citation_", (const char*)tmp, "_year"));
        strcpy(id4, make_string("citation_", (const char*)tmp, "_journal_volume"));
        strcpy(id5, make_string("citation_", (const char*)tmp, "_page_first"));
        strcpy(id6, make_string("citation_", (const char*)tmp, "_page_last"));

        fprintf(fout, "%d  ", m1);
        
        write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7 ,id8,id9 );
    }
    

//CATEGORY 8:   Molecule Names
    n1=0;
    rewind(finp); 
    while(1) { 
        values_in_bracket(finp,  item, value,  &key);
        if(key>0)break;
        if( strlen(value)<=0 ) continue;
        
        if(strstr_case(item, "molecule_name")){
            n1++;
            if(n1==1){
                fprintf(fout,"#\nloop_\n");
                fprintf(fout,"_entity.id\n");
                fprintf(fout,"_entity.pdbx_description\n");
            }
            fprintf(fout," %d  \n;%s\n;\n", n1, value);
        } 
    }


//CATEGORY 9:   Molecule Details
    
    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_entity_keywords.entity_id\n");
    fprintf(fout,"_entity_keywords.pdbx_fragment\n");
    fprintf(fout,"_entity_keywords.pdbx_mutation\n");
    fprintf(fout,"_entity_keywords.pdbx_ec\n");

    
    nitem = 4;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"molecular_entity_id");
    strcpy(id2,"Fragment_name");
    strcpy(id3,"Specific_mutation");
    strcpy(id4,"Enzyme_Comission_number");

    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7 ,id8,id9 );
    
//CATEGORY 10 (Genetically Manipulated Source)    

    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_entity_src_gen.entity_id\n");
    fprintf(fout,"_entity_src_gen.pdbx_gene_src_scientific_name\n");
    fprintf(fout,"_entity_src_gen.pdbx_gene_src_gene\n");
    fprintf(fout,"_entity_src_gen.pdbx_host_org_scientific_name\n");
    fprintf(fout,"_entity_src_gen.pdbx_host_org_strain\n");
    fprintf(fout,"_entity_src_gen.pdbx_host_org_vector_type\n");
    fprintf(fout,"_entity_src_gen.plasmid_name\n");
    fprintf(fout,"_entity_src_gen.gene_src_details\n");
    
    nitem = 8;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"Manipulated_entity_id");
    strcpy(id2,"Source_organism_scientific_name");
    strcpy(id3,"Source_organism_gene");
    strcpy(id4,"Expression_system_scientific_name");
    strcpy(id5,"Expression_system_strain");
    strcpy(id6,"Expression_system_vector_type");
    strcpy(id7,"Expression_system_plasmid_name");
    strcpy(id8,"Manipulated_source_details");
    
    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7 ,id8,id9);

 //CATEGORY 11 (Category Natural Source)

    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_entity_src_nat.entity_id \n");
    fprintf(fout,"_entity_src_nat.pdbx_organism_scientific\n");
    fprintf(fout,"_entity_src_nat.details\n");
    
    nitem = 3;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"natural_source_entity_id");
    strcpy(id2,"natural_source_scientific_name");
    strcpy(id3,"natural_source_details");
    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7 ,id8,id9);

 //CATEGORY 12 (Keywords), 
    rewind(finp);
    while(1) { 
        values_in_bracket(finp,  item, value,  &key);
        if(key>0)break;
        if( strlen(value)<=0 ) continue;
        if(!strcmp_case(item, "structure_keywords")){
            fprintf(fout,"\n_struct_keywords.text \n;%s\n;\n\n",value);
            break;
        }
    }
    fclose(finp);
    if(value) delete [] value;
    
}

void extract_infor_for_xray(FILE *fout, char *inpfile)
// extracting the information related only to Xray    
{
    char *value, item[80];
    char id1[60],id2[60],id3[60],id4[60],id5[60],id6[60],id7[60],id8[60],id9[60];
    int key, n1=0, nitem=0;
    FILE *finp=NULL;

   
    if((finp=fopen(inpfile, "r")) ==NULL)
        open_file_error(inpfile, "extract_infor_for_xray");

    value = new char[10000];

//CATEGORY 1  (Cell Parameters)    
    while(1) { 
        values_in_bracket(finp,  item, value,  &key);
        if(key>0)break;
        if( strlen(value)<=0 ) continue;
        
        if(!strcmp_case(item, "space_group")){

            fprintf(fout,"_symmetry.space_group_name_H-M  '%s'\n",value);
            
        }else if(!strcmp_case(item,"space_group_number")){
            fprintf(fout,"_symmetry.Int_Tables_number  '%s'\n",value);

            
        }else if(!strcmp_case(item, "unit_cell_a") ||
                 !strcmp_case(item, "a")){
            fprintf(fout,"_cell.length_a   \"%s\"\n",value);
            
        }else if(!strcmp_case(item, "unit_cell_b")||
                 !strcmp_case(item, "b")){
            fprintf(fout,"_cell.length_b   \"%s\"\n",value);
            
        }else if(!strcmp_case(item, "unit_cell_c")||
                 !strcmp_case(item, "c")){
            fprintf(fout,"_cell.length_c   \"%s\"\n",value);
            
        }else if(!strcmp_case(item, "unit_cell_alpha")||
                 !strcmp_case(item, "alpha")){
            fprintf(fout,"_cell.angle_alpha      \"%s\"\n",value);
            
        }else if(!strcmp_case(item, "unit_cell_beta")||
                 !strcmp_case(item, "beta")){
            fprintf(fout,"_cell.angle_beta       \"%s\"\n",value);
            
        }else if(!strcmp_case(item, "unit_cell_gamma")||
                 !strcmp_case(item, "gamma")){
            fprintf(fout,"_cell.angle_gamma      \"%s\"\n",value);
        }
    }
    
//CATEGORY 13  (Biological Assembly)
    n1=0;
    rewind(finp); 
    while(1) { 
        values_in_bracket(finp,  item, value,  &key);
        if(key>0)break;
        if( strlen(value)<=0 ) continue;
        
        if(strstr_case(item, "biological_assembly")){
            n1++;
            if(n1==1){
                fprintf(fout,"#\nloop_\n");
                fprintf(fout,"_struct_biol.id\n");
                fprintf(fout,"_struct_biol.details\n");
            }
            fprintf(fout," %d  \n;%s\n;\n", n1, value);
        } 
    }

// CATEGORY 14 (Crystals),    
    rewind(finp);
    while(1) { 
        values_in_bracket(finp,  item, value,  &key);
        if(key>0)break;
        if( strlen(value)<=0 ) continue;
            
        if(!strcmp_case(item, "number_of_crystals")){
            fprintf(fout,"\n_exptl.crystals_number \n;%s\n;\n",value);
            fprintf(fout,"_exptl.method   'X-RAY DIFFRACTION'\n\n");
            break;
        }
        
    }

//CATEGORY 15 (Methods and Conditions)
    
    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_exptl_crystal_grow.crystal_id \n");
    fprintf(fout,"_exptl_crystal_grow.method\n");
    fprintf(fout,"_exptl_crystal_grow.pH \n");
    fprintf(fout,"_exptl_crystal_grow.temp\n");
    fprintf(fout,"_exptl_crystal_grow.pdbx_details\n");
    
    nitem = 5;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"crystal_number");
    strcpy(id2,"crystallization_method");
    strcpy(id3,"crystallization_pH");
    strcpy(id4,"crystallization_temperature");
    strcpy(id5,"crystallization_components");

    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7,id8,id9);

//CATEGORY 16 (Crystal Property)
    
    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_exptl_crystal.id \n");
    fprintf(fout,"_exptl_crystal.density_percent_sol\n");
    fprintf(fout,"_exptl_crystal.density_Matthews\n");
    fprintf(fout,"_exptl_crystal.pdbx_mosaicity\n");

    nitem = 4;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"crystals_number");
    strcpy(id2,"crystals_solvent_content");
    strcpy(id3,"crystals_matthews_coefficient");
    strcpy(id4,"crystals_mosaicity");
    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7,id8,id9 );

//CATEGORY 17 (Radiation Source)
    
    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_diffrn_source.diffrn_id\n");
    fprintf(fout,"_diffrn_source.source\n");
    fprintf(fout,"_diffrn_source.type\n");
    fprintf(fout,"_diffrn_source.rcsb_wavelength_list\n");

    nitem = 4;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"radiation_experiment");
    strcpy(id2,"radiation_source_type");
    strcpy(id3,"radiation_source_name");
    strcpy(id4,"radiation_wavelengths");
    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7, id8,id9 );

//---    

    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_diffrn_detector.diffrn_id \n");
    fprintf(fout,"_diffrn_detector.detector \n");
    fprintf(fout,"_diffrn_detector.type \n");

    
    nitem = 3;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"radiation_experiment");
    strcpy(id2,"radiation_detector_type");
    strcpy(id3,"radiation_detector_name");
    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7, id8,id9 );

//---    
    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_diffrn_radiation.diffrn_id \n");
    fprintf(fout,"_diffrn_radiation.rcsb_diffrn_protocol \n");

    
    nitem = 2;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"radiation_experiment");
    strcpy(id2,"radiation_protocol");

    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7, id8,id9 );
                
//---    

    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_diffrn_radiation_wavelength.id \n");
    fprintf(fout,"_diffrn_radiation_wavelength.wavelength \n");
    
    nitem = 2;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"radiation_experiment");
    strcpy(id2,"radiation_wavelengths");
    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7, id8,id9 );

//CATEGORY 18  (Collection Temperature)
    n1=0;
    rewind(finp); 
    while(1) { 
        values_in_bracket(finp,  item, value,  &key);
        if(key>0)break;
        if( strlen(value)<=0 ) continue;
        
        if(strstr_case(item, "collection_temperature_crystal")){
            n1++;
            if(n1==1){
                fprintf(fout,"#\nloop_\n");
                fprintf(fout,"_diffrn.id\n");
                fprintf(fout,"_diffrn.ambient_temp\n");
            }
            fprintf(fout," %d  \n;%s\n;\n", n1, value);
        } 
    }


//CATEGORY 19 (Structure Genomics)
    
    fprintf(fout,"#\nloop_\n");
    fprintf(fout," _pdbx_SG_project.id\n");
    fprintf(fout," _pdbx_SG_project.project_name\n");
    fprintf(fout," _pdbx_SG_project.full_name_of_center\n");
    fprintf(fout," _pdbx_SG_project.initial_of_center\n");

    nitem = 4;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"SG_project_id");
    strcpy(id2,"SG_project_name");
    strcpy(id3,"full_name_of_SG_center");
    strcpy(id4,"initial_of_SG_center");
    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7,id8,id9 );



    fclose(finp);
    if(value) delete [] value;
}


void extract_infor_for_nmr(FILE *fout, char *inpfile)
// extracting the information related only to NMR    
{
    char *value, item[80];
    char id1[60],id2[60],id3[60],id4[60],id5[60],id6[60],id7[60],id8[60],id9[60];
    int key,  nitem=0;
    FILE *finp=NULL;


    
    if((finp=fopen(inpfile, "r")) ==NULL)
        open_file_error(inpfile, "extract_infor_for_nmr");

    value = new char[10000];
    
//CATEGORY 12:   Ensemble

    rewind(finp);
    while(1) { 
        values_in_bracket(finp,  item, value,  &key);
        if(key>0)break;
        if( strlen(value)<=0 ) continue;
        if(!strcmp_case(item, "conformers_calculated_total_number")){
            fprintf(fout,"\n _pdbx_nmr_ensemble.conformers_calculated_total_number\n;%s\n;\n\n",value);
            
        }else if(!strcmp_case(item, "conformers_submitted_total_number")){
            fprintf(fout,"\n _pdbx_nmr_ensemble.conformers_submitted_total_number\n;%s\n;\n\n",value);
            
        }else if(!strcmp_case(item, "conformers_selection_criteria")){
            fprintf(fout,"\n_pdbx_nmr_ensemble.conformer_selection_criteria \n;%s\n;\n\n",value);
        }
        
    }

//CATEGORY 13:   Representative Conformers
    rewind(finp);
    while(1) { 
        values_in_bracket(finp,  item, value,  &key);
        if(key>0)break;
        if( strlen(value)<=0 ) continue;
        if(!strcmp_case(item, "conformer_id")){
            fprintf(fout,"\n_pdbx_nmr_representative.conformer_id \n;%s\n;\n\n",value);
            
        }else if(!strcmp_case(item, "conformer_selection_criteria")){
            fprintf(fout,"\n_pdbx_nmr_representative.selection_criteria \n;%s\n;\n\n",value);
        }
    }

//CATEGORY 14:   Sample Details
    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_pdbx_nmr_sample_details.solution_id\n");
    fprintf(fout,"_pdbx_nmr_sample_details.contents\n");
    fprintf(fout,"_pdbx_nmr_sample_details.solvent_system\n");
    
    nitem = 3;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"solution_id");
    strcpy(id2,"solution_content");
    strcpy(id3,"solvent_system");

    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7 ,id8,id9);

//CATEGORY 15:   Sample Conditions

     fprintf(fout,"#\nloop_\n");
     fprintf(fout,"_pdbx_nmr_exptl_sample_conditions.conditions_id\n");
     fprintf(fout,"_pdbx_nmr_exptl_sample_conditions.temperature\n");
     fprintf(fout,"_pdbx_nmr_exptl_sample_conditions.pressure\n");
     fprintf(fout,"_pdbx_nmr_exptl_sample_conditions.pH\n");
     fprintf(fout,"_pdbx_nmr_exptl_sample_conditions.ionic_strength\n");
   
    
    nitem = 5;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"Conditions_id");
    strcpy(id2,"Temperature");
    strcpy(id3,"Pressure");
    strcpy(id4,"pH_value");
    strcpy(id5,"Ionic_strength");

    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7,id8,id9 );

// CATEGORY 16:   Spectrometer
    
    fprintf(fout,"#\nloop_\n");
    fprintf(fout,"_pdbx_nmr_spectrometer.spectrometer_id\n");
    fprintf(fout,"_pdbx_nmr_spectrometer.manufacturer\n");
    fprintf(fout,"_pdbx_nmr_spectrometer.model\n");
    fprintf(fout,"_pdbx_nmr_spectrometer.field_strength\n");

    nitem = 4;
    initialize_id(id1,id2,id3,id4,id5,id6,id7,id8,id9);
    strcpy(id1,"experiment_type_id");
    strcpy(id2,"solution_type_id");
    strcpy(id3,"conditions_type_id");
    strcpy(id4,"Experiment_type");
    write_values(fout, finp,  nitem, id1,id2,id3,id4,id5,id6,id7,id8,id9 );

//CATEGORY 18:   Method and Details

    rewind(finp);
    while(1) { 
        values_in_bracket(finp,  item, value,  &key);
        if(key>0)break;
        if( strlen(value)<=0 ) continue;
        if(!strcmp_case(item, "NMR_method")){
            fprintf(fout,"\n_pdbx_nmr_refine.method \n;%s\n;\n\n",value);
            
        }else if(!strcmp_case(item, "NMR_details")){
            fprintf(fout,"\n_pdbx_nmr_refine.details \n;%s\n;\n\n",value);
        }
    }
    fclose(finp);
    if(value) delete [] value;
}

void initialize_id(char *id1,char *id2,char *id3,char *id4,char *id5,
                   char *id6, char *id7,char *id8, char *id9)
{
    strcpy(id1," ?? ");
    strcpy(id2," ?? ");
    strcpy(id3," ?? ");
    strcpy(id4," ?? ");
    strcpy(id5," ?? ");
    strcpy(id6," ?? ");
    strcpy(id7," ?? ");
    strcpy(id8," ?? ");
    strcpy(id9," ?? ");
}

void test_sequence_correction(char *value)
{
    int i=0, n;
    
    n = strlen(value);
    for(i=0; i<n-1; i++){
        if(value[i] == '?'){
            printf("\nSequence error occurred!\n");
            printf("There are \'????\' in your chemical sequences in the file(data_template.text).\n");
            printf("Please do the following corrections, before proceeding.\n");
            printf("If \'????\' are caused by missing residues, substitute them by the missing residue names\n");
            printf("Otherwise, please delete the \'????\'.\n");
            
            exit(0);
        }
    }
}

void write_values(FILE *fout, FILE *finp, int nitem, char *id1,char *id2,
                  char *id3,char *id4,char *id5, char *id6, char *id7,
                  char *id8, char *id9)
/*A general route for writting values */
{
    char item[80], *value=NULL;
    char *str1=NULL, *str2=NULL, *str3=NULL, *str4=NULL;
    char *str5=NULL, *str6=NULL, *str7=NULL, *str8=NULL, *str9=NULL;
    int  n1=0, n2=0, n3=0, n4=0, n5=0, n6=0, n7=0, n8=0, n9=0;
    int  nn1=0, key=0, len=0;
   
    rewind(finp);
    value= new char[10000]; //max values
    
    while(1) {
        values_in_bracket(finp,  item, value,  &key);

        rid_of_front_end_space(value);
        
        len=strlen(value)+2;
        
        if(key>0)break;

            
        if(strstr_case(item, id1)){
            str1=new char[len];
            strcpy(str1, value);
            n1++;
        }else if(strstr_case(item, id2)){
            str2=new char[len+2];
            strcpy(str2, value);
            n2++;
        }else if(strstr_case(item, id3)){
            str3=new char[len+2];
            strcpy(str3, value);
            n3++;
        }else if(strstr_case(item, id4)){
            str4=new char[len+2];
            strcpy(str4, value);
            n4++;
//            printf("hello %d, %s\n", n4, str4);
            
        }else if(strstr_case(item, id5)){
            str5=new char[len+2];
            strcpy(str5, value);
            n5++; 
        }else if(strstr_case(item, id6)){
            str6=new char[len+2];
            strcpy(str6, value);
            n6++; 
        }else if(strstr_case(item, id7)){
            str7=new char[len+2];
            strcpy(str7, value);
            n7++; 
        }else if(strstr_case(item, id8)){
            str8=new char[len+2];
            strcpy(str8, value);
            n8++; 
        }else if(strstr_case(item, id9)){
            str9=new char[len+2];
            strcpy(str9, value);
            n9++; 
        }
            
/* use strstr_case to include the former examples!
        if( !strncmp_case(item,id1, strlen(id1)) ){
            str1=new char[len];
            strcpy(str1, value);
            n1++;
        }else if(!strncmp_case(item,id2, strlen(id2)) ){
            str2=new char[len+2];
            strcpy(str2, value);
            n2++;
        }else if(!strncmp_case(item,id3, strlen(id3)) ){
            str3=new char[len+2];
            strcpy(str3, value);
            n3++;
        }else if(!strncmp_case(item,id4, strlen(id4)) ){
            str4=new char[len+2];
            strcpy(str4, value);
            n4++; 
        }else if(!strncmp_case(item,id5, strlen(id5)) ){
            str5=new char[len+2];
            strcpy(str5, value);
            n5++; 
        }else if(!strncmp_case(item,id6, strlen(id6)) ){
            str6=new char[len+2];
            strcpy(str6, value);
            n6++; 
        }else if(!strncmp_case(item,id7, strlen(id7)) ){
            str7=new char[len+2];
            strcpy(str7, value);
            n7++; 
        }else if(!strncmp_case(item,id8, strlen(id8)) ){
            str8=new char[len+2];
            strcpy(str8, value);
            n8++; 
        }else if(!strncmp_case(item,id9, strlen(id9)) ){
            str9=new char[len+2];
            strcpy(str9, value);
            n9++; 
        }
*/        
        if(nitem==1 &&  n1>0){
            if(strlen(str1)>0){
                nn1++;
                fprintf(fout,"\n;%s\n; ", str1);
                n1=0; 
                str1=NULL;
            }else{
                n1=0; 
                str1=NULL;
            }
            fprintf(fout,"\n");
            
        }else if(nitem==2 &&  n1>0 && n2>0){
            if(strlen(str1)>0){
                nn1++;
                fprintf(fout,"\n;%s\n; ", str1);
                (strlen(str2)>0) ? fprintf(fout,"\n;%s\n; ",str2):fprintf(fout,"\n;?\n;");
                n1=0; n2=0;
                str1=NULL; str2=NULL;
            }else{
                n1=0; n2=0;
                str1=NULL; str2=NULL;
            }
            fprintf(fout,"\n");
            
        }else if (nitem==3 &&  n1>0 && n2>0 && n3>0){
            if(strlen(str1)>0){
                nn1++;
                fprintf(fout,"\n;%s\n; ", str1);
                (strlen(str2)>0) ? fprintf(fout,"\n;%s\n; ",str2):fprintf(fout,"\n;?\n;");
                (strlen(str3)>0) ? fprintf(fout,"\n;%s\n; ",str3):fprintf(fout,"\n;?\n;");
                n1=0; n2=0; n3=0;
                str1=NULL; str2=NULL; str3=NULL;
            }else{
                n1=0; n2=0; n3=0;
                str1=NULL; str2=NULL; str3=NULL;
            }
            fprintf(fout,"\n");
            
        }else if (nitem==4 && n1>0 && n2>0 && n3>0 && n4>0){
            
            if(strstr_case(id1,"entity_id") && strstr_case(id2,"entity_type")&&
               strstr_case(id3,"one_letter") && strstr_case(id4, "chain_id"))
                test_sequence_correction(str3);
            
                                     
            if(strlen(str1)>0){
                
                nn1++;
                fprintf(fout,"\n;%s\n; ",str1);
                (strlen(str2)>0) ? fprintf(fout,"\n;%s\n; ",str2):fprintf(fout,"\n;?\n;");
                (strlen(str3)>0) ? fprintf(fout,"\n;%s\n; ",str3):fprintf(fout,"\n;?\n;");
                (strlen(str4)>0) ? fprintf(fout,"\n;%s\n; ",str4):fprintf(fout,"\n;?\n;");
                n1=0; n2=0; n3=0; n4=0;
                str1=NULL; str2=NULL; str3=NULL; str4=NULL;
            }else{
                str1=NULL; str2=NULL; str3=NULL; str4=NULL;
                n1=0; n2=0; n3=0; n4=0;  
            }
            fprintf(fout,"\n");
            
        }else if (nitem==5 &&  n1>0 && n2>0 && n3>0 && n4>0 && n5>0 ){
            if(strlen(str1)>0){
                nn1++;
                fprintf(fout,"\n;%s\n; ", str1);
                (strlen(str2)>0) ? fprintf(fout,"\n;%s\n; ",str2):fprintf(fout,"\n;?\n;");
                (strlen(str3)>0) ? fprintf(fout,"\n;%s\n; ",str3):fprintf(fout,"\n;?\n;");
                (strlen(str4)>0) ? fprintf(fout,"\n;%s\n; ",str4):fprintf(fout,"\n;?\n;");
                (strlen(str5)>0) ? fprintf(fout,"\n;%s\n; ",str5):fprintf(fout,"\n;?\n;");
                n1=0; n2=0; n3=0; n4=0;n5=0;
                str1=NULL; str2=NULL; str3=NULL; str4=NULL; str5=NULL;
            }else{
                n1=0; n2=0; n3=0; n4=0;n5=0;
                str1=NULL; str2=NULL; str3=NULL; str4=NULL; str5=NULL;
            }
            fprintf(fout,"\n");
            
        }else if (nitem==6 && n1>0 && n2>0 && n3>0 && n4>0 && n5>0 && n6>0){
            if(strlen(str1)>0){
                nn1++;
                fprintf(fout,"\n;%s\n; ", str1);
                (strlen(str2)>0) ? fprintf(fout,"\n;%s\n; ",str2):fprintf(fout,"\n;?\n;");
                (strlen(str3)>0) ? fprintf(fout,"\n;%s\n; ",str3):fprintf(fout,"\n;?\n;");
                (strlen(str4)>0) ? fprintf(fout,"\n;%s\n; ",str4):fprintf(fout,"\n;?\n;");
                (strlen(str5)>0) ? fprintf(fout,"\n;%s\n; ",str5):fprintf(fout,"\n;?\n;");
                (strlen(str6)>0) ? fprintf(fout,"\n;%s\n; ",str6):fprintf(fout,"\n;?\n;");
                n1=0; n2=0; n3=0; n4=0;n5=0;n6=0;
                str1=NULL; str2=NULL; str3=NULL; str4=NULL; str5=NULL;str6=NULL;
            }else{
                n1=0; n2=0; n3=0; n4=0;n5=0;n6=0;
                str1=NULL; str2=NULL; str3=NULL; str4=NULL; str5=NULL;str6=NULL;
            }
            fprintf(fout,"\n");
            
        }else if (nitem==7 && n1>0 && n2>0 && n3>0 && n4>0 && n5>0 && n6>0 && n7>0){
            if(strlen(str1)>0){
                nn1++;
                fprintf(fout,"\n;%s\n; ", str1);
                (strlen(str2)>0) ? fprintf(fout,"\n;%s\n; ",str2):fprintf(fout,"\n;?\n;");
                (strlen(str3)>0) ? fprintf(fout,"\n;%s\n; ",str3):fprintf(fout,"\n;?\n;");
                (strlen(str4)>0) ? fprintf(fout,"\n;%s\n; ",str4):fprintf(fout,"\n;?\n;");
                (strlen(str5)>0) ? fprintf(fout,"\n;%s\n; ",str5):fprintf(fout,"\n;?\n;");
                (strlen(str6)>0) ? fprintf(fout,"\n;%s\n; ",str6):fprintf(fout,"\n;?\n;");
                (strlen(str7)>0) ? fprintf(fout,"\n;%s\n; ",str7):fprintf(fout,"\n;?\n;");
                n1=0; n2=0; n3=0; n4=0;n5=0;n6=0;n7=0;
                str1=NULL; str2=NULL; str3=NULL; str4=NULL; str5=NULL;str6=NULL;str7=NULL;
            }else{
                n1=0; n2=0; n3=0; n4=0;n5=0;n6=0;n7=0;
                str1=NULL; str2=NULL; str3=NULL; str4=NULL; str5=NULL;str6=NULL;str7=NULL;
            }
            fprintf(fout,"\n");
            
        }else if (nitem==8 && n1>0 && n2>0 && n3>0 && n4>0 && n5>0 && n6>0 && n7>0 && n8>0){
            if(strlen(str1)>0){
                nn1++;
                fprintf(fout,"\n;%s\n; ", str1);
                (strlen(str2)>0) ? fprintf(fout,"\n;%s\n; ",str2):fprintf(fout,"\n;?\n;");
                (strlen(str3)>0) ? fprintf(fout,"\n;%s\n; ",str3):fprintf(fout,"\n;?\n;");
                (strlen(str4)>0) ? fprintf(fout,"\n;%s\n; ",str4):fprintf(fout,"\n;?\n;");
                (strlen(str5)>0) ? fprintf(fout,"\n;%s\n; ",str5):fprintf(fout,"\n;?\n;");
                (strlen(str6)>0) ? fprintf(fout,"\n;%s\n; ",str6):fprintf(fout,"\n;?\n;");
                (strlen(str7)>0) ? fprintf(fout,"\n;%s\n; ",str7):fprintf(fout,"\n;?\n;");
                (strlen(str8)>0) ? fprintf(fout,"\n;%s\n; ",str8):fprintf(fout,"\n;?\n;");
                n1=0; n2=0; n3=0; n4=0;n5=0;n6=0;n7=0;n8=0;
                str1=NULL; str2=NULL; str3=NULL; str4=NULL; str5=NULL;str6=NULL;str7=NULL;str8=NULL;
            }else{
                n1=0; n2=0; n3=0; n4=0;n5=0;n6=0;n7=0;n8=0;
                str1=NULL; str2=NULL; str3=NULL; str4=NULL; str5=NULL;str6=NULL;str7=NULL;str8=NULL;
            }
            fprintf(fout,"\n");
            
        }else if (nitem==9 && n1>0 && n2>0 && n3>0 && n4>0 && n5>0 && n6>0 && n7>0 && n8>0 && n9>0){
            if(strlen(str1)>0){
                nn1++;
                fprintf(fout,"\n;%s\n; ", str1);
                (strlen(str2)>0) ? fprintf(fout,"\n;%s\n; ",str2):fprintf(fout,"\n;?\n;");
                (strlen(str3)>0) ? fprintf(fout,"\n;%s\n; ",str3):fprintf(fout,"\n;?\n;");
                (strlen(str4)>0) ? fprintf(fout,"\n;%s\n; ",str4):fprintf(fout,"\n;?\n;");
                (strlen(str5)>0) ? fprintf(fout,"\n;%s\n; ",str5):fprintf(fout,"\n;?\n;");
                (strlen(str6)>0) ? fprintf(fout,"\n;%s\n; ",str6):fprintf(fout,"\n;?\n;");
                (strlen(str7)>0) ? fprintf(fout,"\n;%s\n; ",str7):fprintf(fout,"\n;?\n;");
                (strlen(str8)>0) ? fprintf(fout,"\n;%s\n; ",str8):fprintf(fout,"\n;?\n;");
                (strlen(str9)>0) ? fprintf(fout,"\n;%s\n; ",str9):fprintf(fout,"\n;?\n;");
                n1=0; n2=0; n3=0; n4=0;n5=0;n6=0;n7=0;n8=0;n9=0;
                str1=NULL; str2=NULL; str3=NULL; str4=NULL; str5=NULL;str6=NULL;str7=NULL;str8=NULL;str9=NULL;
            }else{
                n1=0; n2=0; n3=0; n4=0;n5=0;n6=0;n7=0;n8=0;n9=0;
                str1=NULL; str2=NULL; str3=NULL; str4=NULL; str5=NULL;str6=NULL;str7=NULL;str8=NULL;str9=NULL;
            }
            fprintf(fout,"\n"); 
        }
        
    }
    
    if(nitem== 1 && nn1==0 ){
       fprintf(fout,"? \n"); 
    }else if(nitem==2  && nn1==0 ){
       fprintf(fout,"? ? \n"); 
    }else if(nitem==3  && nn1==0 ){
       fprintf(fout,"? ? ? \n"); 
    }else if(nitem==4  && nn1==0 ){
       fprintf(fout,"? ? ? ? \n"); 
    }else if(nitem==5  && nn1==0 ){
       fprintf(fout,"? ? ? ? ?\n"); 
    }else if(nitem==6  && nn1==0 ){
       fprintf(fout,"? ? ? ? ? ? \n"); 
    }else if(nitem==7  && nn1==0 ){
       fprintf(fout,"? ? ? ? ? ? ?\n"); 
    }else if(nitem==8  && nn1==0 ){
       fprintf(fout,"? ? ? ? ? ? ? ?\n"); 
    }else if(nitem==9  && nn1==0 ){
       fprintf(fout,"? ? ? ? ? ? ? ? ?\n"); 
    }    
}
