File: MolSGroupParsing.cpp

package info (click to toggle)
rdkit 202009.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 129,624 kB
  • sloc: cpp: 288,030; python: 75,571; java: 6,999; ansic: 5,481; sql: 1,968; yacc: 1,842; lex: 1,254; makefile: 572; javascript: 461; xml: 229; fortran: 183; sh: 134; cs: 93
file content (1007 lines) | stat: -rw-r--r-- 34,280 bytes parent folder | download
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
//
//  Copyright (C) 2002-2018 Greg Landrum and T5 Informatics GmbH
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//

#include "FileParsers.h"
#include "FileParserUtils.h"
#include "MolSGroupParsing.h"

#include <RDGeneral/FileParseException.h>

namespace RDKit {
namespace SGroupParsing {

/* ------------------ V2000 Utils  ------------------ */

unsigned int ParseSGroupIntField(const std::string &text, unsigned int line,
                                 unsigned int &pos, bool isFieldCounter) {
  ++pos;  // Account for separation space
  unsigned int fieldValue;
  size_t len = 3 - isFieldCounter;  // field counters are smaller
  try {
    fieldValue = FileParserUtils::toInt(text.substr(pos, len));
  } catch (boost::bad_lexical_cast &) {
    std::ostringstream errout;
    errout << "Cannot convert '" << text.substr(pos, len) << "' to int on line "
           << line;
    throw FileParseException(errout.str());
  } catch (const std::out_of_range &) {
    std::ostringstream errout;
    errout << "SGroup line too short: '" << text << "' on line " << line;
    throw FileParseException(errout.str());
  }
  pos += len;
  return fieldValue;
}

double ParseSGroupDoubleField(const std::string &text, unsigned int line,
                              unsigned int &pos) {
  size_t len = 10;
  double fieldValue;
  try {
    fieldValue = FileParserUtils::toDouble(text.substr(pos, len));
  } catch (boost::bad_lexical_cast &) {
    std::ostringstream errout;
    errout << "Cannot convert '" << text.substr(pos, len)
           << "' to double on line " << line;
    throw FileParseException(errout.str());
  } catch (const std::out_of_range &) {
    std::ostringstream errout;
    errout << "SGroup line too short: '" << text << "' on line " << line;
    throw FileParseException(errout.str());
  }
  pos += len;
  return fieldValue;
}

void ParseSGroupV2000STYLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  STY", "bad STY line");

  unsigned int pos = 6;
  unsigned int nent = ParseSGroupIntField(text, line, pos, true);

  for (unsigned int ie = 0; ie < nent; ++ie) {
    if (text.size() < pos + 8) {
      std::ostringstream errout;
      errout << "SGroup STY line too short: '" << text << "' on line " << line;
      throw FileParseException(errout.str());
    }

    int nbr = ParseSGroupIntField(text, line, pos);

    std::string typ = text.substr(pos + 1, 3);
    if (SubstanceGroupChecks::isValidType(typ)) {
      sGroupMap.emplace(nbr, SubstanceGroup(mol, typ));
    } else {
      std::ostringstream errout;
      errout << "S group " << typ << " on line " << line;
      throw MolFileUnhandledFeatureException(errout.str());
    }
    pos += 4;
  }
}

void ParseSGroupV2000VectorDataLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                                    const std::string &text,
                                    unsigned int line) {
  PRECONDITION(mol, "bad mol");

  std::string typ = text.substr(3, 3);

  void (SubstanceGroup::*sGroupAddIndexedElement)(const int) = nullptr;

  if (typ == "SAL") {
    sGroupAddIndexedElement = &SubstanceGroup::addAtomWithBookmark;
  } else if (typ == "SBL") {
    sGroupAddIndexedElement = &SubstanceGroup::addBondWithBookmark;
  } else if (typ == "SPA") {
    sGroupAddIndexedElement = &SubstanceGroup::addParentAtomWithBookmark;
  } else {
    std::ostringstream errout;
    errout << "Unsupported SGroup line '" << typ
           << "' passed to Vector Data parser ";
    throw FileParseException(errout.str());
  }

  unsigned int pos = 6;
  unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
  if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
    BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                            << line << " not found." << std::endl;
    return;
  }
  unsigned int nent = ParseSGroupIntField(text, line, pos, true);

  for (unsigned int i = 0; i < nent; ++i) {
    if (text.size() < pos + 4) {
      std::ostringstream errout;
      errout << "SGroup line too short: '" << text << "' on line " << line;
      throw FileParseException(errout.str());
    }

    int nbr = ParseSGroupIntField(text, line, pos);
    (sGroupMap.at(sgIdx).*sGroupAddIndexedElement)(nbr);
  }
}

void ParseSGroupV2000SDILine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SDI", "bad SDI line");

  unsigned int pos = 6;
  unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
  if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
    BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                            << line << " not found." << std::endl;
    return;
  }

  unsigned int nCoords = ParseSGroupIntField(text, line, pos, true);
  if (nCoords != 4) {
    std::ostringstream errout;
    errout << "Unexpected number of coordinates for SDI on line " << line;
    throw FileParseException(errout.str());
  }

  SubstanceGroup::Bracket bracket;
  for (unsigned int i = 0; i < 2; ++i) {
    double x = ParseSGroupDoubleField(text, line, pos);
    double y = ParseSGroupDoubleField(text, line, pos);
    double z = 0.;
    bracket[i] = RDGeom::Point3D(x, y, z);
  }
  bracket[2] = RDGeom::Point3D(0., 0., 0.);
  sGroupMap.at(sgIdx).addBracket(bracket);
}

void ParseSGroupV2000SSTLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SST", "bad SST line");

  unsigned int pos = 6;
  unsigned int nent = ParseSGroupIntField(text, line, pos, true);

  for (unsigned int ie = 0; ie < nent; ++ie) {
    if (text.size() < pos + 8) {
      std::ostringstream errout;
      errout << "SGroup SST line too short: '" << text << "' on line " << line;
      throw FileParseException(errout.str());
    }

    unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
    if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
      BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                              << line << " not found." << std::endl;
      return;
    }

    std::string subType = text.substr(++pos, 3);

    if (!SubstanceGroupChecks::isValidSubType(subType)) {
      std::ostringstream errout;
      errout << "Unsupported SGroup subtype '" << subType << "' on line "
             << line;
      throw FileParseException(errout.str());
    }

    sGroupMap.at(sgIdx).setProp("SUBTYPE", subType);
    pos += 3;
  }
}

void ParseSGroupV2000SMTLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SMT", "bad SMT line");

  unsigned int pos = 6;
  unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
  if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
    BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                            << line << " not found." << std::endl;
    return;
  }
  auto &sgroup = sGroupMap.at(sgIdx);
  ++pos;

  if (pos >= text.length()) {
    std::ostringstream errout;
    errout << "SGroup line too short: '" << text << "' on line " << line;
    throw FileParseException(errout.str());
  }
  std::string label = text.substr(pos, text.length() - pos);

  if (sgroup.getProp<std::string>("TYPE") ==
      "MUL") {  // Case of multiple groups
    sgroup.setProp("MULT", label);

  } else {  // Case of abbreviation groups, but we might not have seen a SCL
            // line yet
    sgroup.setProp("LABEL", label);
  }
}

void ParseSGroupV2000SLBLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SLB", "bad SLB line");

  unsigned int pos = 6;
  unsigned int nent = ParseSGroupIntField(text, line, pos, true);

  for (unsigned int ie = 0; ie < nent; ++ie) {
    if (text.size() < pos + 8) {
      std::ostringstream errout;
      errout << "SGroup SLB line too short: '" << text << "' on line " << line;
      throw FileParseException(errout.str());
    }

    unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
    if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
      BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                              << line << " not found." << std::endl;
      return;
    }
    unsigned int id = ParseSGroupIntField(text, line, pos);

    if (id != 0 && !SubstanceGroupChecks::isSubstanceGroupIdFree(*mol, id)) {
      std::ostringstream errout;
      errout << "SGroup ID '" << id
             << "' is assigned to more than onge SGroup, on line " << line;
      throw FileParseException(errout.str());
    }

    sGroupMap.at(sgIdx).setProp<unsigned int>("ID", id);
  }
}

void ParseSGroupV2000SCNLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SCN", "bad SCN line");

  unsigned int pos = 6;
  unsigned int nent = ParseSGroupIntField(text, line, pos, true);

  for (unsigned int ie = 0; ie < nent; ++ie) {
    if (text.size() < pos + 7) {
      std::ostringstream errout;
      errout << "SGroup SCN line too short: '" << text << "' on line " << line;
      errout << "\n needed: " << pos + 7 << " found: " << text.size();
      throw FileParseException(errout.str());
    }

    unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
    if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
      BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                              << line << " not found." << std::endl;
      return;
    }

    std::string connect = text.substr(++pos, 2);

    if (!SubstanceGroupChecks::isValidConnectType(connect)) {
      std::ostringstream errout;
      errout << "Unsupported SGroup connection type '" << connect
             << "' on line " << line;
      throw FileParseException(errout.str());
    }

    sGroupMap.at(sgIdx).setProp("CONNECT", connect);
    pos += 3;
  }
}

void ParseSGroupV2000SDSLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 10) == "M  SDS EXP", "bad SDS line");

  unsigned int pos = 10;
  unsigned int nent = ParseSGroupIntField(text, line, pos, true);

  for (unsigned int ie = 0; ie < nent; ++ie) {
    if (text.size() < pos + 4) {
      std::ostringstream errout;
      errout << "SGroup SDS line too short: '" << text << "' on line " << line;
      throw FileParseException(errout.str());
    }
    unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
    if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
      BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                              << line << " not found." << std::endl;
      return;
    }

    sGroupMap.at(sgIdx).setProp("ESTATE", "E");
  }
}

void ParseSGroupV2000SBVLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SBV", "bad SBV line");

  unsigned int pos = 6;

  unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
  if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
    BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                            << line << " not found." << std::endl;
    return;
  }
  auto &sgroup = sGroupMap.at(sgIdx);

  unsigned int bondMark = ParseSGroupIntField(text, line, pos);
  Bond *bond = mol->getUniqueBondWithBookmark(bondMark);

  RDGeom::Point3D vector;
  if (sgroup.getProp<std::string>("TYPE") == "SUP") {
    vector.x = ParseSGroupDoubleField(text, line, pos);
    vector.y = ParseSGroupDoubleField(text, line, pos);
    vector.z = 0.;
  }

  sgroup.addCState(bond->getIdx(), vector);
}

void ParseSGroupV2000SDTLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SDT", "bad SDT line");

  unsigned int pos = 6;
  unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
  auto &sgroup = sGroupMap.at(sgIdx);
  if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
    BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                            << line << " not found." << std::endl;
    return;
  }

  std::string fieldName;
  std::string fieldType;
  std::string fieldInfo;
  std::string queryType;
  std::string queryOp;

  try {
    fieldName = text.substr(++pos, 30);
    boost::trim_right(fieldName);
    pos += 30;
    fieldType = text.substr(pos, 2);
    boost::trim_right(fieldType);
    pos += 2;
    fieldInfo = text.substr(pos, 20);
    boost::trim_right(fieldInfo);
    pos += 20;
    queryType = text.substr(pos, 2);
    boost::trim_right(queryType);
    pos += 2;
    queryOp = text.substr(pos, text.length() - pos);
    boost::trim_right(queryOp);
  } catch (const std::out_of_range &) {
    // all kinds of wild things out there... this insulates us from them without
    // making the code super complicated
  }

  if (fieldName.size()) {
    sgroup.setProp("FIELDNAME", fieldName);
    sgroup.setProp("FIELDTYPE", fieldType);
    sgroup.setProp("FIELDINFO", fieldInfo);
    sgroup.setProp("QUERYTYPE", queryType);
    sgroup.setProp("QUERYOP", queryOp);
  }
}

void ParseSGroupV2000SDDLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SDD", "bad SDD line");

  unsigned int pos = 6;
  unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
  if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
    BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                            << line << " not found." << std::endl;
    return;
  }

  // Store the rest of the line as is.
  ++pos;
  if (pos < text.length()) {
    sGroupMap.at(sgIdx).setProp("FIELDDISP",
                                text.substr(pos, text.length() - pos));
  }
}

void ParseSGroupV2000SCDSEDLine(IDX_TO_SGROUP_MAP &sGroupMap,
                                IDX_TO_STR_VECT_MAP &dataFieldsMap, RWMol *mol,
                                const std::string &text, unsigned int line,
                                bool strictParsing, unsigned int &counter,
                                unsigned int &lastDataSGroup,
                                std::ostringstream &currentDataField) {
  PRECONDITION(mol, "bad mol");

  unsigned int pos = 3;
  std::string type = text.substr(pos, 3);
  pos += 3;

  unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
  if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
    BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                            << line << " not found." << std::endl;
    return;
  }

  if (lastDataSGroup != 0 && lastDataSGroup != sgIdx) {
    std::ostringstream errout;
    errout << "Found a Data Field not matching the SGroup of the last Data "
              "Field at line "
           << line;
    throw FileParseException(errout.str());
  } else if (lastDataSGroup == 0 && type == "SCD") {
    lastDataSGroup = sgIdx;
  } else if (type == "SED") {
    lastDataSGroup = 0;
  }

  auto &sgroup = sGroupMap.at(sgIdx);

  // this group must already have seen an SDT line
  if (!sgroup.hasProp("FIELDNAME")) {
    std::ostringstream errout;
    errout << "Found a SCD line without a previous SDT specification at line "
           << line;
    throw FileParseException(errout.str());
  }

  if (strictParsing) {
    if (type == "SCD" && counter > 2) {
      std::ostringstream errout;
      errout << "Found too many consecutive SCD lines, (#" << (counter + 1)
             << " at line " << line << ") for SGroup " << sgIdx;
      throw FileParseException(errout.str());
    }
  }

  if (pos + 1 < text.length()) {
    currentDataField << text.substr(++pos, 69);

    if (type == "SED") {
      std::string trimmedData = boost::trim_right_copy(currentDataField.str());
      dataFieldsMap[sgIdx].push_back(trimmedData.substr(0, 200));
      currentDataField.str("");
      counter = 0;
    } else {
      ++counter;
    }
  }
}

void ParseSGroupV2000SPLLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SPL", "bad SPL line");

  unsigned int pos = 6;
  unsigned int nent = ParseSGroupIntField(text, line, pos, true);

  for (unsigned int ie = 0; ie < nent; ++ie) {
    if (text.size() < pos + 8) {
      std::ostringstream errout;
      errout << "SGroup SPL line too short: '" << text << "' on line " << line;
      throw FileParseException(errout.str());
    }

    unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
    if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
      BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                              << line << " not found." << std::endl;
      return;
    }
    unsigned int parentIdx = ParseSGroupIntField(text, line, pos);

    sGroupMap.at(sgIdx).setProp<unsigned int>("PARENT", parentIdx);
  }
}

void ParseSGroupV2000SNCLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SNC", "bad SNC line");

  unsigned int pos = 6;
  unsigned int nent = ParseSGroupIntField(text, line, pos, true);

  for (unsigned int ie = 0; ie < nent; ++ie) {
    if (text.size() < pos + 8) {
      std::ostringstream errout;
      errout << "SGroup SNC line too short: '" << text << "' on line " << line;
      throw FileParseException(errout.str());
    }

    unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
    if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
      BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                              << line << " not found." << std::endl;
      return;
    }

    unsigned int compno = ParseSGroupIntField(text, line, pos);
    if (compno > 256u) {
      std::ostringstream errout;
      errout << "SGroup SNC value over 256: '" << compno << "' on line "
             << line;
      throw FileParseException(errout.str());
    }
    sGroupMap.at(sgIdx).setProp<unsigned int>("COMPNO", compno);
  }
}

void ParseSGroupV2000SAPLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SAP", "bad SAP line");

  unsigned int pos = 6;

  unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
  if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
    BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                            << line << " not found." << std::endl;
    return;
  }

  unsigned int nent = ParseSGroupIntField(text, line, pos, true);

  for (unsigned int ie = 0; ie < nent; ++ie) {
    if (text.size() < pos + 11) {
      std::ostringstream errout;
      errout << "SGroup SAP line too short: '" << text << "' on line " << line;
      throw FileParseException(errout.str());
    }

    unsigned int aIdxMark = ParseSGroupIntField(text, line, pos);
    unsigned int lvIdxMark = ParseSGroupIntField(text, line, pos);

    unsigned int aIdx = mol->getAtomWithBookmark(aIdxMark)->getIdx();
    int lvIdx = -1;

    if (lvIdxMark != 0) {
      lvIdx = mol->getAtomWithBookmark(lvIdxMark)->getIdx();
    }

    sGroupMap.at(sgIdx).addAttachPoint(aIdx, lvIdx, text.substr(pos + 1, 2));
    pos += 3;
  }
}

void ParseSGroupV2000SCLLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SCL", "bad SCL line");

  unsigned int pos = 6;

  unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
  if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
    BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                            << line << " not found." << std::endl;
    return;
  }
  if (pos + 1 >= text.length()) {
    std::ostringstream errout;
    errout << "SGroup SCL line too short: '" << text << "' on line " << line;
    throw FileParseException(errout.str());
  }

  ++pos;
  sGroupMap.at(sgIdx).setProp("CLASS", text.substr(pos, text.length() - pos));
}

void ParseSGroupV2000SBTLine(IDX_TO_SGROUP_MAP &sGroupMap, RWMol *mol,
                             const std::string &text, unsigned int line) {
  PRECONDITION(mol, "bad mol");
  PRECONDITION(text.substr(0, 6) == "M  SBT", "bad SBT line");

  unsigned int pos = 6;
  unsigned int nent = ParseSGroupIntField(text, line, pos, true);

  for (unsigned int ie = 0; ie < nent; ++ie) {
    if (text.size() < pos + 8) {
      std::ostringstream errout;
      errout << "SGroup SBT line too short: '" << text << "' on line " << line;
      throw FileParseException(errout.str());
    }

    unsigned int sgIdx = ParseSGroupIntField(text, line, pos);
    if (sGroupMap.find(sgIdx) == sGroupMap.end()) {
      BOOST_LOG(rdWarningLog) << "SGroup " << sgIdx << " referenced on line "
                              << line << " not found." << std::endl;
      return;
    }
    unsigned int bracketType = ParseSGroupIntField(text, line, pos);

    auto &sgroup = sGroupMap.at(sgIdx);

    if (bracketType == 0) {
      sgroup.setProp("BRKTYP", "BRACKET");
    } else if (bracketType == 1) {
      sgroup.setProp("BRKTYP", "PAREN");
    } else {
      std::ostringstream errout;
      errout << "Invalid SBT value '" << bracketType << "' on line " << line;
      throw FileParseException(errout.str());
    }
  }
}

/* ------------------ V3000 Utils  ------------------ */

template <class T>
std::vector<T> ParseV3000Array(std::stringstream &stream) {
  auto paren = stream.get();  // discard parentheses
  if (paren != '(') {
    BOOST_LOG(rdWarningLog)
        << "WARNING: first character of V3000 array is not '('" << std::endl;
  }

  unsigned int count;
  stream >> count;
  std::vector<T> values;
  values.reserve(count);
  T value;
  for (unsigned i = 0; i < count; ++i) {
    stream >> value;
    values.push_back(value);
  }
  paren = stream.get();  // discard parentheses
  if (paren != ')') {
    BOOST_LOG(rdWarningLog)
        << "WARNING: final character of V3000 array is not ')'" << std::endl;
  }
  return values;
}

// force instantiation of the versions of this that we use
template std::vector<unsigned int> ParseV3000Array(std::stringstream &stream);
template std::vector<int> ParseV3000Array(std::stringstream &stream);

void ParseV3000CStateLabel(RWMol *mol, SubstanceGroup &sgroup,
                           std::stringstream &stream, unsigned int line) {
  stream.get();  // discard parentheses

  unsigned int count;
  unsigned int bondMark;
  stream >> count >> bondMark;

  std::string type = sgroup.getProp<std::string>("TYPE");

  if ((type != "SUP" && count != 1) || (type == "SUP" && count != 4)) {
    std::ostringstream errout;
    errout << "Unexpected number of fields for CSTATE field on line " << line;
    throw FileParseException(errout.str());
  }

  Bond *bond = mol->getUniqueBondWithBookmark(bondMark);

  RDGeom::Point3D vector;
  if (type == "SUP") {
    stream >> vector.x >> vector.y >> vector.z;
  }
  sgroup.addCState(bond->getIdx(), vector);

  stream.get();  // discard final parentheses
}

void ParseV3000SAPLabel(RWMol *mol, SubstanceGroup &sgroup,
                        std::stringstream &stream) {
  stream.get();  // discard parentheses

  unsigned int count;
  unsigned int aIdxMark;
  std::string lvIdxStr;  // In V3000 this may be a string
  std::string sapIdStr;
  stream >> count >> aIdxMark >> lvIdxStr >> sapIdStr;

  // remove final parentheses that gets parsed into sapIdStr
  sapIdStr.pop_back();

  unsigned int aIdx = mol->getAtomWithBookmark(aIdxMark)->getIdx();
  int lvIdx = -1;

  boost::to_upper(lvIdxStr);
  if (lvIdxStr == "AIDX") {
    lvIdx = aIdx;
  } else {
    unsigned int lvIdxTmp = FileParserUtils::toInt(lvIdxStr);
    if (lvIdxTmp > 0) {
      lvIdx = mol->getAtomWithBookmark(lvIdxTmp)->getIdx();
    }
  }

  sgroup.addAttachPoint(aIdx, lvIdx, sapIdStr);
}

std::string ParseV3000StringPropLabel(std::stringstream &stream) {
  std::string strValue;

  auto nextChar = stream.peek();
  if (nextChar == '"') {
    // skip the opening quote:
    stream.get();

    // this is a bit gross because it's legal to include a \" in a value,
    // but the way that's done is by doubling it. So
    // FIELDINFO=""""
    // should assign the value \" to FIELDINFO
    char chr;
    while (stream.get(chr)) {
      if (chr == '"') {
        nextChar = stream.peek();

        // if the next element in the stream is a \" then we have a quoted \".
        // Otherwise we're done
        if (nextChar != '"') {
          break;
        } else {
          // skip the second \"
          stream.get();
        }
      }
      strValue += chr;
    }
  } else if (nextChar == '\'') {
    std::getline(stream, strValue, '\'');
  } else {
    stream >> strValue;
  }

  boost::trim_right(strValue);
  return strValue;
}

void ParseV3000ParseLabel(const std::string &label,
                          std::stringstream &lineStream, STR_VECT &dataFields,
                          unsigned int &line, SubstanceGroup &sgroup,
                          size_t nSgroups, RWMol *mol, bool &strictParsing) {
  RDUNUSED_PARAM(nSgroups);
  // TODO: we could handle these in a more structured way
  if (label == "XBHEAD" || label == "XBCORR") {
    std::vector<unsigned int> bvect = ParseV3000Array<unsigned int>(lineStream);
    std::transform(bvect.begin(), bvect.end(), bvect.begin(),
                   [](unsigned int v) -> unsigned int { return v - 1; });
    sgroup.setProp(label, bvect);
  } else if (label == "ATOMS") {
    for (auto atomIdx : ParseV3000Array<unsigned int>(lineStream)) {
      sgroup.addAtomWithBookmark(atomIdx);
    }
  } else if (label == "PATOMS") {
    for (auto patomIdx : ParseV3000Array<unsigned int>(lineStream)) {
      sgroup.addParentAtomWithBookmark(patomIdx);
    }
  } else if (label == "CBONDS" || label == "XBONDS") {
    for (auto bondIdx : ParseV3000Array<unsigned int>(lineStream)) {
      sgroup.addBondWithBookmark(bondIdx);
    }
  } else if (label == "BRKXYZ") {
    auto coords = ParseV3000Array<double>(lineStream);
    if (coords.size() != 9) {
      std::ostringstream errout;
      errout << "Unexpected number of coordinates for BRKXYZ on line " << line;
      throw FileParseException(errout.str());
    }

    SubstanceGroup::Bracket bracket;
    for (unsigned int i = 0; i < 3; ++i) {
      bracket[i] = RDGeom::Point3D(*(coords.begin() + (3 * i)),
                                   *(coords.begin() + (3 * i) + 1),
                                   *(coords.begin() + (3 * i) + 2));
    }
    sgroup.addBracket(bracket);
  } else if (label == "CSTATE") {
    ParseV3000CStateLabel(mol, sgroup, lineStream, line);
  } else if (label == "SAP") {
    ParseV3000SAPLabel(mol, sgroup, lineStream);
  } else if (label == "PARENT") {
    // Store relationship until all SGroups have been read
    unsigned int parentIdx;
    lineStream >> parentIdx;
    sgroup.setProp<unsigned int>("PARENT", parentIdx);
  } else if (label == "COMPNO") {
    unsigned int compno;
    lineStream >> compno;
    if (compno > 256u) {
      std::ostringstream errout;
      errout << "SGroup SNC value over 256: '" << compno << "' on line "
             << line;
      throw FileParseException(errout.str());
    }
    sgroup.setProp<unsigned int>("COMPNO", compno);
  } else if (label == "FIELDDATA") {
    auto strValue = ParseV3000StringPropLabel(lineStream);
    if (strictParsing) {
      strValue = strValue.substr(0, 200);
    }
    dataFields.push_back(strValue);

  } else {
    // Parse string props
    auto strValue = ParseV3000StringPropLabel(lineStream);

    if (label == "SUBTYPE" && !SubstanceGroupChecks::isValidSubType(strValue)) {
      std::ostringstream errout;
      errout << "Unsupported SGroup subtype '" << strValue << "' on line "
             << line;
      throw FileParseException(errout.str());
    } else if (label == "CONNECT" &&
               !SubstanceGroupChecks::isValidConnectType(strValue)) {
      std::ostringstream errout;
      errout << "Unsupported SGroup connection type '" << strValue
             << "' on line " << line;
      throw FileParseException(errout.str());
    }

    sgroup.setProp(label, strValue);
  }
}

void ParseV3000SGroupsBlock(std::istream *inStream, unsigned int &line,
                            unsigned int nSgroups, RWMol *mol,
                            bool &strictParsing) {
  std::string tempStr = FileParserUtils::getV3000Line(inStream, line);
  boost::to_upper(tempStr);
  if (tempStr.length() < 12 || tempStr.substr(0, 12) != "BEGIN SGROUP") {
    std::ostringstream errout;
    errout << "BEGIN SGROUP line not found on line " << line;
    throw FileParseException(errout.str());
  }

  unsigned int defaultLineNum = 0;
  std::string defaultString;

  // SGroups may be written in unsorted ID order, according to spec, so we will
  // temporarily store them in a map before adding them to the mol
  IDX_TO_SGROUP_MAP sGroupMap;

  std::unordered_map<std::string, std::stringstream> defaultLabels;

  tempStr = FileParserUtils::getV3000Line(inStream, line);

  // Store defaults
  if (tempStr.substr(0, 7) == "DEFAULT" && tempStr.length() > 8) {
    defaultString = tempStr.substr(7);
    defaultLineNum = line;
    boost::trim_right(defaultString);
    tempStr = FileParserUtils::getV3000Line(inStream, line);
    boost::trim_right(tempStr);
  }

  for (unsigned int si = 0; si < nSgroups; ++si) {
    unsigned int sequenceId;
    unsigned int externalId;
    std::string type;

    std::stringstream lineStream(tempStr);
    lineStream >> sequenceId;
    lineStream >> type;
    lineStream >> externalId;

    std::set<std::string> parsedLabels;

    if (strictParsing && !SubstanceGroupChecks::isValidType(type)) {
      std::ostringstream errout;
      errout << "Unsupported SGroup type '" << type << "' on line " << line;
      throw MolFileUnhandledFeatureException(errout.str());
    }

    SubstanceGroup sgroup(mol, type);
    STR_VECT dataFields;

    sgroup.setProp<unsigned int>("index", sequenceId);
    if (externalId > 0) {
      if (!SubstanceGroupChecks::isSubstanceGroupIdFree(*mol, externalId)) {
        std::ostringstream errout;
        errout << "Existing SGroup ID '" << externalId
               << "' assigned to a second SGroup on line " << line;
        throw FileParseException(errout.str());
      }

      sgroup.setProp<unsigned int>("ID", externalId);
    }

    while (!lineStream.eof() && !lineStream.fail()) {
      char spacer;
      std::string label;

      lineStream.get(spacer);
      if (lineStream.gcount() == 0) {
        continue;
      } else if (spacer != ' ') {
        std::ostringstream errout;
        errout << "Found character '" << spacer
               << "' when expecting a separator (space) on line " << line;
        throw FileParseException(errout.str());
      }

      std::getline(lineStream, label, '=');
      ParseV3000ParseLabel(label, lineStream, dataFields, line, sgroup,
                           nSgroups, mol, strictParsing);
      parsedLabels.insert(label);
    }

    // Process defaults
    lineStream.clear();
    lineStream.str(defaultString);
    while (!lineStream.eof() && !lineStream.fail()) {
      char spacer;
      std::string label;

      lineStream.get(spacer);
      if (lineStream.gcount() == 0) {
        continue;
      } else if (spacer != ' ') {
        std::ostringstream errout;
        errout << "Found character '" << spacer
               << "' when expecting a separator (space) in DEFAULTS on line "
               << defaultLineNum;
        throw FileParseException(errout.str());
      }

      std::getline(lineStream, label, '=');
      if (std::find(parsedLabels.begin(), parsedLabels.end(), label) ==
          parsedLabels.end()) {
        ParseV3000ParseLabel(label, lineStream, dataFields, defaultLineNum,
                             sgroup, nSgroups, mol, strictParsing);
      } else {
        spacer = lineStream.peek();
        if (spacer == ' ') {
          std::ostringstream errout;
          errout << "Found unexpected whitespace at DEFAULT label " << label;
          throw FileParseException(errout.str());
        } else if (spacer == '(') {
          std::getline(lineStream, label, ')');
          lineStream.get(spacer);
        } else if (spacer == '"') {
          lineStream.get(spacer);
          std::getline(lineStream, label, '"');
        } else {
          std::getline(lineStream, label, ' ');
          lineStream.putback(' ');
        }
      }
    }

    sgroup.setProp("DATAFIELDS", dataFields);
    sGroupMap.emplace(sequenceId, sgroup);

    tempStr = FileParserUtils::getV3000Line(inStream, line);
    boost::trim_right(tempStr);
  }

  boost::to_upper(tempStr);
  if (tempStr.length() < 10 || tempStr.substr(0, 10) != "END SGROUP") {
    std::ostringstream errout;
    errout << "END SGROUP line not found on line " << line;
    throw FileParseException(errout.str());
  }

  // SGroups successfully parsed, now add them to the molecule
  for (const auto &sg : sGroupMap) {
    addSubstanceGroup(*mol, sg.second);
  }
}

}  // namespace SGroupParsing
}  // namespace RDKit