/*
FILE:     _validation.h
*/
/*
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. 
*/
#ifndef _VALIDATION_H_
#define _VALIDATION_H_

#include <stdio.h>
#include "_fileobj.h"
#include "contact.h"

#include "plot.h"
#include "check.h"

#define RIBBON_W  0.50
#define RIBBON_H  0.20

#define DOUBLE_HELIX           1
#define PARALLEL_HELIX         2
#define A_DOUBLE_HELIX         4
#define B_DOUBLE_HELIX         8
#define Z_DOUBLE_HELIX        16
#define TRIPLE_HELIX          32
#define QUADRUPLE_HELIX       64
#define MISMATCH             128
#define HAIRPIN_LOOP         256
#define TETRA_LOOP           512
#define INTERNAL_LOOP       1024
#define BULGE_LOOP          2048
#define THREE_STEM          4096
#define FOUR_STEM           8192

typedef struct {
        char FstAtom[5];
        char altLoc1[3];
        int  FstIndex;
        int  FstRes;
        char SndAtom[5];
        char altLoc2[3];
        int  SndIndex;
        int  SndRes;
        char SymOP_1[7];
        char SymOP_2[7];
        char *details;
} _LINK;

typedef struct {
        int  FstIndex;
        int  FstRes;
        int  SndIndex;
        int  SndRes;
} _MISMAT;

typedef struct {
        char FstAtom[5];
        int  FstIndex;
        int  FstRes;
        char SndAtom[5];
        int  SndIndex;
        int  SndRes;
        char Asym_or_Gen[10];
        char Type[35];
} _BSPAIR;

typedef struct {
        _RESIDUE *res;
        int    op, lx, ly, lz;
        int    exist_P;
        int    exist_O;
        int    exist_N;
        int    exist_C;
        COORD  org;
        COORD  orien[3];
        COORD  P_xyz;
        COORD  O_xyz;
        COORD  N_xyz;
        COORD  C_xyz;
} _BASE;

typedef struct {
        int   Mol_ID;
        char  ChainID[3];
        char  ResName[4];
        int   ResNum;
        char  ins_code[2];
        char  Rep_Name[25];
        char  type[15];
        double val;
        double ept;
        double std;
        _ATOM *fst;
        _ATOM *snd;
        _ATOM *trd;
} _BOND;

typedef _BOND _ANGLE;
/*
typedef struct {
        int   Mol_ID;
        char  ChainID[3];
        char  ResName[4];
        int   ResNum;
        char  ins_code[2];
        char  Rep_Name[25];
        char  type[15];
        double val;
        double ept;
        double std;
        _ATOM *fst;
        _ATOM *snd;
        _ATOM *trd;
} _ANGLE;
*/
typedef struct {
        int   Mol_ID;
        char  ChainID[3];
        char  ResName[4];
        int   ResNum;
        char  ins_code[2];
        char  Rep_Name[25];
        char  type[15];
        double val;
        double ept;
        double std;
        _ATOM *fst;
        _ATOM *snd;
        _ATOM *trd;
        _ATOM *fth;
} _TORSION;

typedef struct {
        int res_num;
        double x;
        double y;
} _PAIR_VALUE;

typedef struct {
        char *author;
        char *title;
        char *refn;
} REFN;

typedef struct {
        char *method;
        char *drop;
        char *reservoir;
        char *soaking;
} CRYS;

typedef struct {
        int  isComplex, Num_seqs;
        char struct_id[150];
        char *struct_features;
        char *compound_name;
        char **seqs;
        REFN *refn;
        char spacegroup[100];
        char cell[200];
        CRYS *crys;
        char *refinement;
} ATLAS_TEXT;

typedef struct {
        CifString ChnID_I, ChnID_J;
        char   ChnID_I_PDB[5], Base_I[5], ChnID_J_PDB[5], Base_J[5];
        char   InsCode_I[5], InsCode_J[5], Sym_I[7], Sym_J[7];
        char   _saenger_classification[23];
        char   _leontis_westhof_classification[33];
        int    ResNum_I, ResNum_I_PDB, ResNum_J, ResNum_J_PDB;
        double Shear, Stretch, Stagger, Buckle, Propel, Opening;
} BASE_BASE_PARAMS;

typedef struct {
        CifString ChnID_I1, ChnID_J1, ChnID_I2, ChnID_J2;
        char   ChnID_I1_PDB[5], Base_I1[5], ChnID_J1_PDB[5], Base_J1[5];
        char   ChnID_I2_PDB[5], Base_I2[5], ChnID_J2_PDB[5], Base_J2[5];
        char   InsCode_I1[5], InsCode_J1[5], Sym_I1[7], Sym_J1[7];
        char   InsCode_I2[5], InsCode_J2[5], Sym_I2[7], Sym_J2[7];
        int    ResNum_I1, ResNum_I1_PDB, ResNum_I2, ResNum_I2_PDB;
        int    ResNum_J1, ResNum_J1_PDB, ResNum_J2, ResNum_J2_PDB;
        double Shift, Slide, Rise, Tilt, Roll, Twist;
        double X_disp, Y_disp, H_rise, Incl, Tip, H_twist;
} INTER_BASE_PARAMS;

typedef struct {
        COORD c, n, w;
        COORD p1, p2, p3, p4;
        COORD n1, n2, n3, n4;
        int   index;
} _RIBBON;

typedef struct {
        int n_ribbon;
        _RIBBON *ribbon;
} _RIBBONS;

typedef struct {
        COORD coord[8];
        int   index;
} _BLOCK;

typedef struct {
        _ATOM *a, *b;
        int   color_a, color_b;
} _LINK_BOND;

class _VALIDATION: public _FileObj {
   public:
        _VALIDATION(const int, const int);
        ~_VALIDATION();
        void     Clear();
        void     Clear_Bspair();
        void     Get_Stbiol(const int);
        void     Input_Stbiol();
        void     Reorder_Stbiol();
        void     Input_Link();
        void     update_residues_nomenclature(const int);
        void     match_stbiol_with_molecules();
        void     Insert_a_Link(const char*, const char*, const int, const int,
                               const char*, const char*, const int, const int,
                               const char*, const char*, const char*);
        void     Insert_a_Mismat(const int, const int, const int, const int);
        void     Insert_a_Bspair(const char*, const int, const int, const char*,
                                 const int, const int, const char*, const char*);
        void     find_all_links();
        void     get_bspair_information();
        void     na_classification();
        void     biological_assembly();
        void     calculate_contact_for_nmr();
        CONTACT  **calculate_all_contacts(int &, const double, const double);
        void     calculate_all_contact(const double, const int);
        int      find_a_link(CONTACT*);
        void     generate_symmetry_strands(const int);
        void     insert_bond(const char*, const char*, const int, const char*, const char*,
                             const char*, const double, const double, const double, const int,
                             _ATOM*, _ATOM*);
        void     insert_angle(const char*, const char*, const int, const char*, const char*,
                             const char*, const double, const double, const double, const int,
                             _ATOM*, _ATOM*, _ATOM*);
        void     insert_torsion(const char*, const char*, const int, const char*, const char*,
                             const char*, const double, const int, _ATOM*, _ATOM*, _ATOM*, _ATOM*);
        void     insert_torsion_pair(const char*, const char*, const int, const char*,
                             const double, const double, const int);
        void     insert_pair_z_a_1(const int, const double, const double);
        void     insert_pair_z_b_1(const int, const double, const double);
        void     insert_pair_z_e(const int, const double, const double);
        void     insert_pair_a_g(const int, const double, const double);
        void     insert_pair_z_x(const int, const double, const double);
        void     insert_pair_d_x(const int, const double, const double);
        void     validation(char*, const int, const int, const int, const int);
        void     get_letter(FILE*, const int);
        void     ndb_check_chirality();
        void     ndb_write_atlas_entries(char*, const int);
        void     call_procheck(const char*);
        void     pre_processing_of_ciffile();
        void     pre_processing_of_pdbfile();
        void     check_terminal_atoms(CifString&);
        void     ndb_check_file(FILE*, const int, CHECK_TOKEN_FORMAT*, const int,
                           CHECK_CATEGORY*, const int, const int);
        void     print_base_morphology();
        void     write_check_results(FILE *fp);
   protected:
        void     Clear_Contact();
        int      write_letter_token(FILE*, FILE*, const char*);
        void     write_special_letter_information(FILE*, FILE*, const char*);
        void     find_out_range_waters(FILE*);
        void     move_out_range_waters(FILE*);
        void     output_close_contact_to_letter(const char, FILE*);
        void     output_close_contact(CONTACT**, const int, const int, FILE*);
        void     ndb_find_bond_differences(FILE*);
        void     ndb_find_angle_differences(FILE*);
        void     write_structure_factor(FILE*, FILE*);
        void     write_publication_question(FILE*, FILE*);
        void     ndb_write_letter_misplace_atoms(const int, FILE*, FILE*, const char*);
        void     check_link_records(FILE*);
        int      ndb_check_stereochemistry(FILE*, FILE*);
        int      ndb_check_E_Z_labeling(FILE*, FILE*);
        void     get_molecule_topology();
        void     add_single_values(const int, _RESIDUE*, const int);
        int      add_mutual_values(const int, const int, _RESIDUE*, _RESIDUE*);
        double   add_torsions(_ATOM**, _ATOM**, _ATOM**, _ATOM**, const int, const int,
                        const int, const int, const char*, const char*, const int, const int);
        ATLAS_TEXT* get_atlas_text(const char*, const char*);
        void     get_name_st(CifString&, CifString&);
        void     get_classification_st(CifString&, int&);
        int      get_bspair_information(const int, _BASE**, int*);
        void     insert_base_pairs(const int, CONTACT**);
        void     re_ordering(const int, int*, _BASE**);
        void     get_parameters(const int, int*, _BASE**);
        void     crystal_packing();
        void     make_ribbon(_MOLECULE*, const int, const int);
        void     create_ribbons(_CHAIN*, const int, COORD*, _RESIDUE**, const int);
        void     create_bonds(_CHAIN*, const int);
        void     add_link_bonds(_ATOM*, const int, _ATOM*, const int);
        void     create_blocks(_CHAIN*, const int);
        void     draw_ps_picture(_plot&, const double rot[4][4], COORD*, const double area[4], COORD abc[3]);
        // void     draw_vrml_picture(const char*, const double rot[4][4], const int, COORD*, const int);
        void     write_picture_part(_plot&, const char*, const char*, const int);
        void     write_packing_picture_part(_plot&, const char*, const char*, const int, const int);
        void     draw_pictures(_plot&, const char*, const int);
        void     draw_nmr_pictures(_plot&, const char*);
        void     get_link_record(ISTable *Table, const int is_rcsb_link);
        int      _alloc_link, _n_link;
        _LINK**  _link;
        int      _alloc_mismat, _n_mismat;
        _MISMAT  **_mismat;
        int      _alloc_bspair, _n_bspair;
        _BSPAIR  **_bspair;
        int      _alloc_a_contact, _n_a_contact;
        CONTACT  **_a_contact;
        int      _alloc_s_contact, _n_s_contact;
        CONTACT  **_s_contact;
        int      _alloc_waters, _n_waters;
        CONTACT  **_waters;
        int      _alloc_moving_waters, _n_moving_waters;
        CONTACT  **_moving_waters;
        int      _alloc_speical, _n_speical;
        CONTACT  **_speical;
        int      _alloc_bond, _n_bond;
        _BOND**  _bond;
        int      _alloc_angle, _n_angle;
        _ANGLE** _angle;
        int      _alloc_torsion, _n_torsion;
        _TORSION **_torsion;
        int      _alloc_t_pairs, _n_t_pairs;
        _TORSION **_t_pairs;
        int      _alloc_z_a_1, _n_z_a_1;
        _PAIR_VALUE **_z_a_1;
        int      _alloc_z_b_1, _n_z_b_1;
        _PAIR_VALUE **_z_b_1;
        int      _alloc_z_e, _n_z_e;
        _PAIR_VALUE **_z_e;
        int      _alloc_a_g, _n_a_g;
        _PAIR_VALUE **_a_g;
        int      _alloc_z_x, _n_z_x;
        _PAIR_VALUE **_z_x;
        int      _alloc_d_x, _n_d_x;
        _PAIR_VALUE **_d_x;
        int      _calculated_bond_angle_torsion;
        int _alloc_bb_params, _n_bb_params;
        BASE_BASE_PARAMS **_bb_params;
        int _alloc_ib_params, _n_ib_params;
        INTER_BASE_PARAMS **_ib_params;
        int _alloc_ribbons, _n_ribbons;
        _RIBBONS** _ribbons;
        int _alloc_link_bonds, _n_link_bonds;
        _LINK_BOND** _link_bonds;
        int _alloc_blocks, _n_blocks;
        _BLOCK **_blocks;
        TblIndexObj _bondResIndex;
        TblIndexObj _bondNamIndex;
        TblIndexObj _bondComIndex;
        TblIndexObj _angleResIndex;
        TblIndexObj _angleNamIndex;
        TblIndexObj _angleComIndex;
        TblIndexObj _torsionIndex;
        int _classification;
	CifString _spell_check;
        CifString _large_cell;
        CifString _date_error;
};

extern int is_torsion_out_of_regions(const float phi, const float psi);

extern int get_residue_index(_MOLECULE *mol, ISTable *Table, const int irow,
                const int ndb_comp_id, const int ndb_asym_id, const int ndb_seq_id,
                const int pdb_comp_id, const int pdb_asym_id, const int pdb_seq_id,
                const int ins_code_id, int &FstIndex, int &FstRes);

extern void validation_ndb(const char*, const char*, const int, const int, const int, char*, const int);
extern void validation_pdb(const char*, const char*, const int, const int, const int, char*, FILE*, const int, const int);
extern void validation_cif(const char*, const char*, const int, const int, const int, char*, FILE*, const int, const int, const int);
extern void test_morphology(const char*, const char*);
extern void preprocess_file(const char*, const char*);
extern void get_taxonomylist(const int taxonomy_flag); 

#endif
