File: CifFile.h

package info (click to toggle)
librcsb-core-wrapper 1.005-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,576 kB
  • sloc: xml: 122,915; cpp: 25,250; ansic: 3,736; makefile: 1,033; sh: 772; lex: 294; yacc: 235; perl: 213; python: 121; csh: 30
file content (699 lines) | stat: -rw-r--r-- 22,319 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
//$$FILE$$
//$$VERSION$$
//$$DATE$$
//$$LICENSE$$


/*!
** \file CifFile.h
**
** \brief Header file for CifFile class.
*/


/* 
  PURPOSE:    Base class for read/write cif files
*/


#ifndef CIFFILE_H
#define CIFFILE_H


#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <sstream>

#include "GenString.h"
#include "CifString.h"
#include "TableFile.h"
#include "CifParentChild.h"



/**
**  \class CifFile
**
**  \brief Public class that represents a CIF file, composed of blocks with
**    tables.
**
**  This class represents a CIF file. In addition to inherited methods from
**  \e TableFile class, this class provides methods for writing the data to
**  a text file, along with methods for controlling how the data is written,
**  and a method for verifying the CIF file against dictionary.
*/
class CifFile : public TableFile
{
  public:
    std::string _parsingDiags;
    std::string _checkingDiags;

    static const unsigned int STD_CIF_LINE_LENGTH = 80;

    enum eQuoting
    {
        eSINGLE = 0,
        eDOUBLE
    };

    /**
    **  Constructs a CIF file.
    **
    **  \param[in] fileMode - CIF file mode. Possible values are
    **    read-only, create, update and virtual. Detailed description of
    **    file mode is given in \e TableFile documentation.
    **  \param[in] fileName - relative or absolute name of the file
    **    where object persistency is maintained. If \e fileMode specifies
    **    virtual mode, this parameter is ignored.
    **  \param[in] verbose - optional parameter that indicates whether
    **    logging should be turned on (if true) or off (if false).
    **    If \e verbose is not specified, logging is turned off.
    **  \param[in] caseSense - optional parameter that indicates case
    **    sensitivity of table names. Possible values are case sensitive
    **    and case in-sensitive. If not specified, case sensitive table
    **    names are assumed.
    **  \param[in] maxLineLength - optional parameter that indicates the
    **    maximum number of written characters in one line in the written
    **    text file. If not specified, \e STD_CIF_LINE_LENGTH is used.
    **  \param[in] nullValue - optional parameter that indicates the
    **    character that is to be used to denote unknown value in the written
    **    CIF file. If not specified, \e CifString::UnknownValue is used.
    **
    **  \return Not applicable
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    CifFile(const eFileMode fileMode, const std::string& fileName,
      const bool verbose = false, const Char::eCompareType
      caseSense = Char::eCASE_SENSITIVE,
      const unsigned int maxLineLength = STD_CIF_LINE_LENGTH,
      const std::string& nullValue = CifString::UnknownValue);

    /**
    **  Constructs a CIF file in virtual mode.
    **
    **  \param[in] verbose - optional parameter that indicates whether
    **    logging should be turned on (if true) or off (if false).
    **    If \e verbose is not specified, logging is turned off.
    **  \param[in] caseSense - optional parameter that indicates case
    **    sensitivity of table names. Possible values are case sensitive
    **    and case in-sensitive. If not specified, case sensitive table
    **    names are assumed.
    **  \param[in] maxLineLength - optional parameter that indicates the
    **    maximum number of written characters in one line in the written
    **    text file. If not specified, \e STD_CIF_LINE_LENGTH is used.
    **  \param[in] nullValue - optional parameter that indicates the
    **    character that is to be used to denote unknown value in the written
    **    CIF file. If not specified, \e CifString::UnknownValue is used.
    **
    **  \return Not applicable
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    CifFile(const bool verbose = false, const Char::eCompareType
      caseSense = Char::eCASE_SENSITIVE,
      const unsigned int maxLineLength = STD_CIF_LINE_LENGTH,
      const std::string& nullValue = CifString::UnknownValue);

    /**
    **  Destructs a CIF file, by releasing all consumed resources.
    **
    **  \param: Not applicable
    **
    **  \return Not applicable
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    ~CifFile();

    /**
    **  Sets file name of a file that was the source of the object data.
    **
    **  \param srcFileName - The name of the source data file.
    **
    **  \return None
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    void SetSrcFileName(const std::string& srcFileName);


    /**
    **  Retrieves source file name.
    **
    **  \param: None
    **
    **  \return - source file name
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    const std::string& GetSrcFileName();


    /**
    **  Retrieves logging option.
    **
    **  \param: None
    **
    **  \return true - if logging is turned on
    **  \return false - if logging is turned off
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    inline bool GetVerbose();

    /**
    **  Sets smart printing option. Smart printing is used to beautify the
    **  output of a written text file.
    **
    **  \param smartPrint - smart printing. If false, smart printing is
    **    disabled. If true, smart printing is enabled. If not specified,
    **    smart printing is enabled.
    **
    **  \return None
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    inline void SetSmartPrint(bool smartPrint = true);


    /**
    **  Retrieves smart printing option.
    **
    **  \param: None
    **
    **  \return true - if smart printing is enabled
    **  \return false - if smart printing is disabled
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    inline bool IsSmartPrint();

    /**
    **  Sets quoting option. This option is used in order to
    **  select the type of quoting to be used in the written text file.
    **
    **  \param quoting - type of quoting. If \e eSINGLE, single quotes are
    **    used. If \e eDOUBLE, double quotes are used.
    **
    **  \return None
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    void SetQuoting(eQuoting quoting);

    /**
    **  Retrieves quoting option.
    **
    **  \param: None
    **
    **  \return eSINGLE - if single quotes are used
    **  \return eDOUBLE - if double quotes are used
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    unsigned int GetQuoting();
 
    /**
    **  This method is used in order to control how single row categories are
    **  written: in form of a "loop_" construct or as an item-value pair.
    **
    **  \param catName - category name
    **  \param looping - category looping option. If false and the
    **    category is a single row category, that category will not be
    **    written with "loop_" construct. Otherwise, if true, single row
    **    category will be written with "loop_" construct.
    **
    **  \return None
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    void SetLooping(const std::string& catName, bool looping = false);

    /**
    **  Retrieves looping option of a category.
    **
    **  \param catName - category name
    **
    **  \return - category looping option, as described in SetLooping() method.
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    bool GetLooping(const std::string& catName);
 
    /**
    **  Writes the data out to a text file.
    **
    **  \param[in] cifFileName - relative or absolute name of the text file
    **    to which the data from \e CifFile object is to be written to.
    **  \param[in] sortTables - optional parameter that indicates whether
    **    written tables should be sorted (if true) or not sorted (if false).
    **    If \e sortTables is not specified, tables are not sorted prior to
    **    writing them.
    **  \param[in] writeEmptyTables - optional parameter that indicates
    **    whether empty tables (0 rows) are to be written (if true) or not
    **    written (if false). If \e writeEmptyTables is not specified, empty
    **    tables are not written.
    **
    **  \return None
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    void Write(const std::string& cifFileName, const bool sortTables = false,
      const bool writeEmptyTables = false);

    /**
    **  Writes the data out to a text file.
    **
    **  \param[in] cifFileName - relative or absolute name of the text file
    **    to which the data from \e CifFile object is to be written to.
    **  \param[in] tableOrder - vector of table names that indicates the
    **    order of written tables.
    **  \param[in] writeEmptyTables - optional parameter that indicates
    **    whether empty tables (0 rows) are to be written (if true) or not
    **    written (if false). If \e writeEmptyTables is not specified, empty
    **    tables are not written.
    **
    **  \return None
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    void Write(const std::string& cifFileName,
      const std::vector<std::string>& tableOrder,
      const bool writeEmptyTables = false);

    /**
    **  Writes the data out to an output stream.
    **
    **  \param[in] outStream - a reference to the output stream
    **  \param[in] sortTables - optional parameter that indicates whether
    **    written tables should be sorted (if true) or not sorted (if false).
    **    If \e sortTables is not specified, tables are not sorted prior to
    **    writing them.
    **  \param[in] writeEmptyTables - optional parameter that indicates
    **    whether empty tables (0 rows) are to be written (if true) or not
    **    written (if false). If \e writeEmptyTables is not specified, empty
    **    tables are not written.
    **
    **  \return None
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    void Write(std::ostream& outStream, const bool sortTables = false,
      const bool writeEmptyTables = false);

    /**
    **  Writes the data out to a text file in NMR-STAR format.
    **
    **  \param[in] nmrStarFileName - relative or absolute name of the text file
    **    to which the data from \e CifFile object is to be written to.
    **  \param[in] globalBlockName - the name of the global NMR-STAR block.
    **  \param[in] sortTables - optional parameter that indicates whether
    **    written tables should be sorted (if true) or not sorted (if false).
    **    If \e sortTables is not specified, tables are not sorted prior to
    **    writing them.
    **  \param[in] writeEmptyTables - optional parameter that indicates
    **    whether empty tables (0 rows) are to be written (if true) or not
    **    written (if false). If \e writeEmptyTables is not specified, empty
    **    tables are not written.
    **
    **  \return None
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    void WriteNmrStar(const std::string& nmrStarFileName,
      const std::string& globalBlockName,  const bool sortTables = false,
      const bool writeEmptyTables = false);

    /**
    **  Checks a CIF file (all blocks in it) against the dictionary.
    **
    **  \param[in] dicRef - reference to a dictionary file. The check is
    **    done against the first block in the dictionary file.
    **  \param[in] diagFileName - relative or absolute name of the file,
    **    where diagnostic information is stored.
    **  \param[in] extraDictChecks - optional parameter that indicates whether
    **    to perform additional, non-standard, dictionary checks. If not
    **    specified, those checks are not performed.
    **  \param[in] extraCifChecks - optional parameter that indicates whether
    **    to perform additional, non-standard, CIF checks. If not specified,
    **    those checks are not performed.
    **
    **  \return 0 - if all checks passed
    **  \return different than 0 - if checks failed
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    int DataChecking(CifFile& dicRef, const std::string& diagFileName,
      const bool extraDictChecks = false, const bool extraCifChecks = false);

    /**
    **  Checks a block of CIF file against the specified reference block.
    **
    **  \param[in] block - reference to a block that is to be checked
    **  \param[in] refBlock - reference to a reference block against which
    **    \e block is to be checked
    **  \param[out] diagBuf - diagnostics buffer that holds checking results
    **  \param[in] extraDictChecks - optional parameter that indicates whether
    **    to perform additional, non-standard, checks. If not specified, those
    **    checks are not performed.
    **  \param[in] extraCifChecks - optional parameter that indicates whether
    **    to perform additional, non-standard, CIF checks. If not specified,
    **    those checks are not performed.
    **
    **  \return 0 - if all checks passed
    **  \return different than 0 - if checks failed
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    int DataChecking(Block& block, Block& refBlock, std::ostringstream& buf,
      const bool extraDictChecks = false, const bool extraCifChecks = false);

    /**
    **  Sets enumerations checking option for case-insensitive types.
    **
    **  \param caseSense - case sensitivity of enumeration values check. If
    **    false, enumeration values of case-insensitive types will be checked
    **    as case-insensitive. If true, enumeration values of case-insensitive
    **    types will be checked as case-sensitive.
    **
    **  \return None
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    void SetEnumCheck(bool caseSense = false);

    /**
    **  Retrieves enumerations checking option for case-insensitive types.
    **
    **  \param: None
    **
    **  \return true - if case-sensitive enumeration check is enabled
    **  \return false - if case-insensitive enumeration check is enabled
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    bool GetEnumCheck();

    /**
    **  Gets parsing diagnostics.
    **
    **  \param: None
    **
    **  \return - reference to parsing diagnostics
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    const std::string& GetParsingDiags();


    /**
    **  Finds indices of rows that contain all CIF null values. A CIF null
    **  value is defined as a "?" or "".
    **
    **  \param[out] nullRowsIndices - vector of null rows indices.
    **  \param[in] isTable - table reference
    **
    **  \return None
    **
    **  \pre None
    **
    **  \post None
    **
    **  \exception: None
    */
    void FindCifNullRows(std::vector<unsigned int>& nullRowsIndices,
      const ISTable& isTable);

    void GetAttributeValue(std::string& attribVal, const std::string& blockId,
      const std::string& category, const std::string& attribute);
    void GetAttributeValueIf(std::string& attribVal, const std::string& blockId,
      const std::string& category, const std::string& attributeA,
      const std::string& attributeB, const std::string& valB);
    bool IsAttributeValueDefined(const std::string& blockId,
      const std::string& category, const std::string& attribute);

    void SetAttributeValue(const std::string& blockId,
      const std::string& category,
      const std::string& attribute, const std::string& value,
      const bool create = false);
    void SetAttributeValueIf(const std::string& blockId,
      const std::string& category, const std::string& attributeA,
      const std::string& valA,
      const std::string& attributeB, const std::string& valB,
       const bool create = false);
    void SetAttributeValueIfNull(const std::string& blockId,
      const std::string& category, const std::string& attribute,
      const std::string& value);

    void GetAttributeValues(std::vector<std::string>& strings,
      const std::string& blockId,
      const std::string& category, const std::string& attribute);
    void GetAttributeValuesIf(std::vector<std::string>& strings, 
      const std::string& blockId, const std::string& category,
      const std::string& attributeA, 
      const std::string& attributeB, const std::string& valB);

    void SetAttributeValues(const std::string& blockId,
      const std::string& category, const std::string& attribute,
      const std::vector<std::string>& values);

#ifdef VLAD_TO_CIF_FILE_NOT_USED
    void del_attribute_value_where(CifFile *fobj, const char *blockId,
      const char *category, const char *attributeB, const char *valB);
#endif // VLAD_TO_CIF_FILE_NOT_USED not defined

    int CheckCategories(Block& block, Block& refBlock, std::ostringstream& log);
    void CheckCategoryKey(Block& block, std::ostringstream& log);
    void CheckItemsTable(Block& block, std::ostringstream& log);
    int CheckItems(Block& block, Block& refBlock, std::ostringstream& log);


  protected:
    static const unsigned int STD_PRINT_SPACING = 3;
    static const unsigned int SMART_PRINT_SPACING = 1;
    static const unsigned int HEADER_SPACING = 40;

    enum eIdentType
    {
        eNONE = 0,
        eLEFT,
        eRIGHT
    };

    std::string _beginDataKeyword;
    std::string _endDataKeyword;

    std::string _beginLoopKeyword;
    std::string _endLoopKeyword;

    unsigned int _maxCifLineLength;
    std::string _nullValue;
    bool _verbose;
    bool _smartPrint;
    std::string _quotes;
    std::map<std::string, bool> _looping;
    bool _enumCaseSense;

    int _IsQuotableText(const std::string& itemValue);
    eIdentType _FindPrintType(const std::vector<std::string>& values);

    void _PrintItemIdent(std::ostream& cifo, unsigned int& linePos);
    void _PrintItemName(std::ostream& cifo, const std::string& category,
      const std::string& itemName, unsigned int& linePos);
    void _PrintPostItemSeparator(std::ostream& cifo, unsigned int& linePos,
      const bool ident = false, const unsigned int numSpaces = 1);

    int _PrintItemValue(std::ostream& cifo, const std::string& itemValue,
      unsigned int& linePos, const eIdentType identType = eNONE,
      const unsigned int width = 0);

    int _PrintItemNameInHeader(std::ostream& cifo, const std::string& itemValue,
      unsigned int& linePos, const eIdentType identType = eNONE,
      const unsigned int width = 0);

    void _PrintHeaderedItems(std::ostream& cifo,
      const std::vector<std::string>& colNames,
      const std::vector<unsigned int>& colWidths,
      const std::vector<eIdentType> colPrintType);

    void Write(std::ostream& cifo, const std::vector<std::string>& catOrder,
      const bool writeEmptyTables = false);

    void Write(std::ostream& cifo, std::vector<unsigned int>& tables,
      const bool writeEmptyTables = false);


  private:
    std::string _srcFileName;

    bool _extraDictChecks;
    bool _extraCifChecks;

    void Init();

    bool IsCatDefinedInRef(const std::string& catName, ISTable& catTable);
    bool IsItemDefinedInRef(const std::string& catName,
      const std::string& itemName, ISTable& refItemTable);
    void CheckKeyItems(const std::string& blockName, ISTable& catTable,
      ISTable& keyTable, std::ostringstream& log);
    void CheckKeyValues(const std::vector<std::string>& keyItems,
      ISTable& catTable, std::ostringstream& log);

    void GetKeyAttributes(std::vector<std::string>& keyAttributes,
      const std::string& catTableName, ISTable& catKeyTable);
    void CheckKeyItems(const std::string& blockName, ISTable& catTable,
      const std::vector<std::string>& keyAttributes, std::ostringstream& log);

    void CheckMandatoryItems(const std::string& blockName, ISTable& catTable,
      ISTable& refItemTable, const std::vector<std::string>& keyItems,
      std::ostringstream& log);

    void CheckAndRectifyItemTypeCode(Block& block, std::ostringstream& log);
    void RectifyItemTypeCode(std::string& retItemTypeCode,
      std::ostringstream& log, Block& block, CifParentChild& cifParentChild,
      const std::string& cifItemName);

    int CheckRegExpRangeEnum(Block& block, ISTable& catTable,
      const std::string& attribName, ISTable& itemTypeTable,
      ISTable& itemTypeListTable, ISTable* itemRangeTableP,
      ISTable* itemEnumTableP, ISTable& parChildTable, ISTable* itemAliasesP,
      std::ostringstream& log);

    int CheckCellRange(const std::string& cell, const std::string& typeCode,
      const std::vector<std::string>& minlist,
      const std::vector<std::string>& maxlist);

    int CheckCellEnum(const std::string& cell, const std::string& typeCode,
      const std::string& primCode, const std::vector<std::string>& enumlist);

    int CheckCellFloatRange(const std::string& cell,
      const std::vector<std::string>& minlist,
      const std::vector<std::string>& maxlist);

    int CheckCellIntRange(const std::string& cell,
      const std::vector<std::string>& minlist,
      const std::vector<std::string>& maxlist);

    int CheckCellFloatEnum(const std::string& cell,
      const std::vector<std::string>& enumlist);

    int CheckCellIntEnum(const std::string& cell,
      const std::vector<std::string>& enumlist);

    int CheckCellOtherEnum(const std::string& cell, const std::string& primCode,
      const std::vector<std::string>& enumlist);

    void GetItemTypeCode(std::string& typeCode, const std::string& cifItemName,
      ISTable& itemTypeTable);

    void ConvertEscapedString(const std::string& inString,
      std::string& outString);
};


inline bool CifFile::GetVerbose()
{
    return(_verbose);
}


inline void CifFile::SetSmartPrint(bool smartPrint)
{
    _smartPrint = smartPrint;
}


inline bool CifFile::IsSmartPrint()
{
    return(_smartPrint);
}


#endif