File: indigo_match.cpp

package info (click to toggle)
indigo 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,936 kB
  • sloc: ansic: 332,816; cpp: 169,470; python: 20,033; java: 13,701; cs: 9,979; asm: 8,475; sql: 6,743; xml: 6,354; javascript: 1,245; sh: 555; php: 506; makefile: 54
file content (1012 lines) | stat: -rw-r--r-- 35,709 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
1008
1009
1010
1011
1012
/****************************************************************************
 * Copyright (C) from 2009 to Present EPAM Systems.
 *
 * This file is part of Indigo toolkit.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ***************************************************************************/

#include "indigo_match.h"
#include "base_cpp/scanner.h"
#include "indigo_mapping.h"
#include "indigo_molecule.h"
#include "indigo_reaction.h"
#include "molecule/elements.h"
#include "molecule/molecule_exact_matcher.h"
#include "molecule/molecule_tautomer_matcher.h"
#include "reaction/reaction_automapper.h"
#include "reaction/reaction_exact_matcher.h"
#include "reaction/reaction_substructure_matcher.h"

void _indigoParseTauCondition(const char* list_ptr, int& aromaticity, Array<int>& label_list)
{
    if (list_ptr == 0 || *list_ptr == 0)
        throw IndigoError("null or empty tautomer rule description is not allowed");

    if (isdigit(*list_ptr))
    {
        if (*list_ptr == '1')
            aromaticity = 1;
        else if (*list_ptr == '0')
            aromaticity = 0;
        else
            throw IndigoError("bad tautomer rule format");
        list_ptr++;
    }
    else
    {
        aromaticity = -1;
    }

    label_list.clear();

    QS_DEF(Array<char>, buf);
    buf.clear();

    while (*list_ptr != 0)
    {
        if (isalpha((unsigned)*list_ptr))
            buf.push(*list_ptr);
        else if (*list_ptr == ',')
        {
            buf.push(0);
            label_list.push(Element::fromString(buf.ptr()));
            buf.clear();
        }
        else
            throw IndigoError("bad label list format in the tautomer rule");
        list_ptr++;
    }

    buf.push(0);
    label_list.push(Element::fromString(buf.ptr()));
}

CEXPORT int indigoSetTautomerRule(int n, const char* beg,
                                  const char* end){INDIGO_BEGIN{if (n < 1 || n >= 32) throw IndigoError("tautomer rule index %d is out of range", n);

AutoPtr<TautomerRule> rule(new TautomerRule());

_indigoParseTauCondition(beg, rule->aromaticity1, rule->list1);
_indigoParseTauCondition(end, rule->aromaticity2, rule->list2);

self.tautomer_rules.expand(n);
self.tautomer_rules.reset(n - 1, rule.release());
return 1;
}
INDIGO_END(-1)
}

CEXPORT int indigoClearTautomerRules(){INDIGO_BEGIN{self.tautomer_rules.clear();
return 1;
}
INDIGO_END(-1)
}

CEXPORT int indigoRemoveTautomerRule(int n){INDIGO_BEGIN{self.tautomer_rules.remove(n - 1);
return 1;
}
INDIGO_END(-1)
}

DLLEXPORT bool _indigoParseTautomerFlags(const char* flags, IndigoTautomerParams& params)
{
    if (flags == 0)
        return false;

    BufferScanner scanner(flags);

    scanner.skipSpace();

    QS_DEF(Array<char>, word);

    if (scanner.isEOF())
        return false;

    scanner.readWord(word, 0);

    if (strcasecmp(word.ptr(), "TAU") != 0)
        return false;

    MoleculeTautomerMatcher::parseConditions(flags, params.conditions, params.force_hydrogens, params.ring_chain, params.method);

    return true;
}

DLLEXPORT int _indigoParseExactFlags(const char* flags, bool reaction, float* rms_threshold)
{
    if (flags == 0)
        throw IndigoError("_indigoParseExactFlags(): zero string pointer");

    if (!reaction && rms_threshold == 0)
        throw IndigoError("_indigoParseExactFlags(): zero float pointer");

    static struct
    {
        const char* token;
        int type; // 1 -- molecule, 2 -- reaction, 3 -- both
        int value;
    } token_list[] = {{"ELE", 3, MoleculeExactMatcher::CONDITION_ELECTRONS}, {"MAS", 3, MoleculeExactMatcher::CONDITION_ISOTOPE},
                      {"STE", 3, MoleculeExactMatcher::CONDITION_STEREO},    {"FRA", 1, MoleculeExactMatcher::CONDITION_FRAGMENTS},
                      {"AAM", 2, ReactionExactMatcher::CONDITION_AAM},       {"RCT", 2, ReactionExactMatcher::CONDITION_REACTING_CENTERS}};

    int i, res = 0, count = 0;
    bool had_float = false;
    bool had_none = false;
    bool had_all = false;

    if (!reaction)
        *rms_threshold = 0;

    BufferScanner scanner(flags);

    QS_DEF(Array<char>, word);
    while (1)
    {
        scanner.skipSpace();
        if (scanner.isEOF())
            break;
        if (had_float)
            throw IndigoError("_indigoParseExactFlags(): no value is allowed after the number");
        scanner.readWord(word, 0);

        if (strcasecmp(word.ptr(), "NONE") == 0)
        {
            if (had_all)
                throw IndigoError("_indigoParseExactFlags(): NONE conflicts with ALL");
            had_none = true;
            count++;
            continue;
        }
        if (strcasecmp(word.ptr(), "ALL") == 0)
        {
            if (had_none)
                throw IndigoError("_indigoParseExactFlags(): ALL conflicts with NONE");
            had_all = true;
            res = MoleculeExactMatcher::CONDITION_ALL;
            if (reaction)
                res |= ReactionExactMatcher::CONDITION_ALL;
            count++;
            continue;
        }
        if (strcasecmp(word.ptr(), "TAU") == 0)
            // should have been handled before
            throw IndigoError("_indigoParseExactFlags(): no flags are allowed together with TAU");

        for (i = 0; i < NELEM(token_list); i++)
        {
            if (strcasecmp(token_list[i].token, word.ptr()) == 0)
            {
                if (!reaction && !(token_list[i].type & 1))
                    throw IndigoError("_indigoParseExactFlags(): %s flag is allowed only for reaction matching", word.ptr());
                if (reaction && !(token_list[i].type & 2))
                    throw IndigoError("_indigoParseExactFlags(): %s flag is allowed only for molecule matching", word.ptr());
                if (had_all)
                    throw IndigoError("_indigoParseExactFlags(): only negative flags are allowed together with ALL");
                res |= token_list[i].value;
                break;
            }
            if (word[0] == '-' && strcasecmp(token_list[i].token, word.ptr() + 1) == 0)
            {
                if (!reaction && !(token_list[i].type & 1))
                    throw IndigoError("_indigoParseExactFlags(): %s flag is allowed only for reaction matching", word.ptr());
                if (reaction && !(token_list[i].type & 2))
                    throw IndigoError("_indigoParseExactFlags(): %s flag is allowed only for molecule matching", word.ptr());
                res &= ~token_list[i].value;
                break;
            }
        }
        if (i == NELEM(token_list))
        {
            BufferScanner scanner2(word.ptr());

            if (!reaction && scanner2.tryReadFloat(*rms_threshold))
            {
                res |= MoleculeExactMatcher::CONDITION_3D;
                had_float = true;
            }
            else
                throw IndigoError("_indigoParseExactFlags(): unknown token %s", word.ptr());
        }
        else
            count++;
    }

    if (had_none && count > 1)
        throw IndigoError("_indigoParseExactFlags(): no flags are allowed together with NONE");

    if (count == 0)
        res |= MoleculeExactMatcher::CONDITION_ALL | ReactionExactMatcher::CONDITION_ALL;

    return res;
}

CEXPORT int indigoExactMatch(int handler1, int handler2, const char* flags)
{
    INDIGO_BEGIN
    {
        IndigoObject& obj1 = self.getObject(handler1);
        IndigoObject& obj2 = self.getObject(handler2);

        if (flags == 0)
            flags = "";

        if (IndigoBaseMolecule::is(obj1))
        {
            Molecule& mol1 = obj1.getMolecule();
            Molecule& mol2 = obj2.getMolecule();
            IndigoTautomerParams params;
            AutoPtr<IndigoMapping> mapping(new IndigoMapping(mol1, mol2));

            if (_indigoParseTautomerFlags(flags, params))
            {
                MoleculeTautomerMatcher matcher(mol2, false);

                matcher.arom_options = self.arom_options;
                matcher.setRulesList(&self.tautomer_rules);
                matcher.setRules(params.conditions, params.force_hydrogens, params.ring_chain, params.method);
                matcher.setQuery(mol1);

                if (!matcher.find())
                    return 0;

                mapping->mapping.copy(matcher.getQueryMapping(), mol1.vertexEnd());
                return self.addObject(mapping.release());
            }

            MoleculeExactMatcher matcher(mol1, mol2);
            matcher.flags = _indigoParseExactFlags(flags, false, &matcher.rms_threshold);

            if (!matcher.find())
                return 0;

            mapping->mapping.copy(matcher.getQueryMapping(), mol1.vertexEnd());
            return self.addObject(mapping.release());
        }
        if (IndigoBaseReaction::is(obj1))
        {
            Reaction& rxn1 = obj1.getReaction();
            Reaction& rxn2 = obj2.getReaction();
            int i;

            ReactionExactMatcher matcher(rxn1, rxn2);
            matcher.flags = _indigoParseExactFlags(flags, true, 0);

            if (!matcher.find())
                return 0;

            AutoPtr<IndigoReactionMapping> mapping(new IndigoReactionMapping(rxn1, rxn2));
            mapping->mol_mapping.clear_resize(rxn1.end());
            mapping->mol_mapping.fffill();
            mapping->mappings.expand(rxn1.end());

            for (i = rxn1.begin(); i != rxn1.end(); i = rxn1.next(i))
            {
                if (rxn1.getSideType(i) == BaseReaction::CATALYST)
                    continue;
                mapping->mol_mapping[i] = matcher.getTargetMoleculeIndex(i);
                mapping->mappings[i].copy(matcher.getQueryMoleculeMapping(i), rxn1.getBaseMolecule(i).vertexEnd());
            }

            return self.addObject(mapping.release());
        }

        throw IndigoError("indigoExactMatch(): %s is neither a molecule nor a reaction", obj1.debugInfo());
    }
    INDIGO_END(-1);
}

IndigoMoleculeSubstructureMatchIter::IndigoMoleculeSubstructureMatchIter(Molecule& target_, QueryMolecule& query_, Molecule& original_target_, bool resonance,
                                                                         bool disable_folding_query_h)
    : IndigoObject(MOLECULE_SUBSTRUCTURE_MATCH_ITER), matcher(target_), target(target_), original_target(original_target_), query(query_)
{
    matcher.disable_folding_query_h = disable_folding_query_h;
    matcher.setQuery(query);
    matcher.fmcache = &fmcache;

    matcher.use_pi_systems_matcher = resonance;

    _initialized = false;
    _found = false;
    _need_find = true;
    _embedding_index = 0;
}

IndigoMoleculeSubstructureMatchIter::~IndigoMoleculeSubstructureMatchIter()
{
}

const char* IndigoMoleculeSubstructureMatchIter::debugInfo()
{
    return "<molecule substructure match iterator>";
}

IndigoObject* IndigoMoleculeSubstructureMatchIter::next()
{
    if (!hasNext())
        return 0;

    AutoPtr<IndigoMapping> mptr(new IndigoMapping(query, original_target));

    // Expand mapping to fit possible implicit hydrogens
    mapping.expandFill(target.vertexEnd(), -1);

    if (!matcher.getEmbeddingsStorage().isEmpty())
    {
        const GraphEmbeddingsStorage& storage = matcher.getEmbeddingsStorage();
        int count;
        const int* query_mapping = storage.getMappingSub(_embedding_index, count);
        mptr->mapping.copy(query_mapping, query.vertexEnd());
    }
    else
        mptr->mapping.copy(matcher.getQueryMapping(), query.vertexEnd());

    for (int v = query.vertexBegin(); v != query.vertexEnd(); v = query.vertexNext(v))
    {
        int mapped = mptr->mapping[v];

        if (mapped >= 0)
            mptr->mapping[v] = mapping[mapped];
    }
    _need_find = true;
    return mptr.release();
}

bool IndigoMoleculeSubstructureMatchIter::hasNext()
{
    if (!_need_find)
        return _found;

    if (!_initialized)
    {
        _initialized = true;
        _found = matcher.find();
    }
    else
    {
        _embedding_index++;
        int cur_count = matcher.getEmbeddingsStorage().count();
        if (_embedding_index < cur_count)
            _found = true;
        else
            _found = matcher.findNext();
    }
    if (_embedding_index >= max_embeddings)
        throw IndigoError("Number of embeddings exceeded maximum allowed limit (%d). "
                          "Adjust options to raise this limit.",
                          max_embeddings);

    _need_find = false;
    return _found;
}

IndigoTautomerSubstructureMatchIter::IndigoTautomerSubstructureMatchIter(Molecule& target_, QueryMolecule& query_, Molecule& tautomerFound_,
                                                                         TautomerMethod method)
    : IndigoObject(MOLECULE_SUBSTRUCTURE_MATCH_ITER), matcher(target_, method), query(query_), tautomerFound(tautomerFound_)
{
    matcher.setQuery(query);
    _initialized = false;
    _found = false;
    _need_find = true;
    _embedding_index = 0;
    _mask_index = 0;
}

IndigoTautomerSubstructureMatchIter::~IndigoTautomerSubstructureMatchIter()
{
}

const char* IndigoTautomerSubstructureMatchIter::debugInfo()
{
    return "<tautomer substructure match iterator>";
}

IndigoObject* IndigoTautomerSubstructureMatchIter::next()
{
    if (!hasNext())
        return NULL;

    matcher.getTautomerFound(tautomerFound, _embedding_index, _mask_index);
    AutoPtr<IndigoMapping> mptr(new IndigoMapping(query, tautomerFound));

    // Expand mapping to fit possible implicit hydrogens
    mapping.expandFill(tautomerFound.vertexEnd(), -1);

    if (!matcher.getEmbeddingsStorage().isEmpty())
    {
        const GraphEmbeddingsStorage& storage = matcher.getEmbeddingsStorage();
        int count;
        const int* query_mapping = storage.getMappingSub(_embedding_index, count);
        mptr->mapping.copy(query_mapping, query.vertexEnd());
    }
    else
        mptr->mapping.copy(matcher.getQueryMapping(), query.vertexEnd());

    for (int v = query.vertexBegin(); v != query.vertexEnd(); v = query.vertexNext(v))
    {
        int mapped = mptr->mapping[v];

        if (mapped >= 0)
            mptr->mapping[v] = mapping[mapped];
    }
    _need_find = true;
    return mptr.release();
}

bool IndigoTautomerSubstructureMatchIter::hasNext()
{
    if (!_need_find)
        return _found;

    if (!_initialized)
    {
        _initialized = true;
        _found = matcher.find();
        if (_found)
        {
            _embedding_index = 0;
            _mask_index = matcher.getMask(_embedding_index).nextSetBit(0);
        }
    }
    else
    {
        int cur_count = matcher.getEmbeddingsStorage().count();
        _mask_index = matcher.getMask(_embedding_index).nextSetBit(_mask_index + 1);
        if (_mask_index == -1)
        {
            ++_embedding_index;
        }
        if (_embedding_index < cur_count)
            _found = true;
        else
        {
            _found = matcher.findNext();
            if (_found)
            {
                _mask_index = matcher.getMask(_embedding_index).nextSetBit(0);
            }
        }
    }
    if (_embedding_index >= max_embeddings)
        throw IndigoError("Number of embeddings exceeded maximum allowed limit (%d). "
                          "Adjust options to raise this limit.",
                          max_embeddings);

    _need_find = false;
    return _found;
}

struct MatchCountContext
{
    int embeddings_count, max_count;
};

static bool _matchCountEmbeddingsCallback(Graph& sub, Graph& super, const int* core1, const int* core2, void* context_)
{
    MatchCountContext* context = (MatchCountContext*)context_;
    context->embeddings_count++;
    if (context->embeddings_count >= context->max_count)
        return false;
    return true;
}

int IndigoMoleculeSubstructureMatchIter::countMatches(int embeddings_limit)
{
    if (max_embeddings <= 0)
        throw IndigoError("Maximum allowed embeddings limit must be positive "
                          "Adjust options to raise this limit.");

    MatchCountContext context;
    context.embeddings_count = 0;
    if (embeddings_limit != 0)
        context.max_count = __min(max_embeddings, embeddings_limit);
    else
        context.max_count = max_embeddings;

    matcher.find_all_embeddings = true;
    matcher.cb_embedding = _matchCountEmbeddingsCallback;
    matcher.cb_embedding_context = &context;
    matcher.find();
    if (embeddings_limit != 0 && context.embeddings_count >= embeddings_limit)
        return embeddings_limit;
    if (context.embeddings_count >= max_embeddings)
        throw IndigoError("Number of embeddings exceeded maximum allowed limit (%d). "
                          "Adjust options to raise this limit.",
                          max_embeddings);
    return context.embeddings_count;
}

IndigoMoleculeSubstructureMatcher::IndigoMoleculeSubstructureMatcher(Molecule& target, int mode_) : IndigoObject(MOLECULE_SUBSTRUCTURE_MATCHER), target(target)
{
    _arom_h_unfolded_prepared = false;
    _arom_prepared = false;
    _aromatized = false;
    mode = mode_;
}

IndigoMoleculeSubstructureMatcher::~IndigoMoleculeSubstructureMatcher()
{
}

const char* IndigoMoleculeSubstructureMatcher::debugInfo()
{
    return "<molecule substructure matcher>";
}

void IndigoMoleculeSubstructureMatcher::ignoreAtom(int atom_index)
{
    _ignored_atoms.push(atom_index);
}

void IndigoMoleculeSubstructureMatcher::unignoreAtom(int atom_index)
{
    int pos = _ignored_atoms.find(atom_index);
    if (pos == -1)
        throw IndigoError("Atom with index %d wasn't ignored", atom_index);
    _ignored_atoms.remove(pos);
}

void IndigoMoleculeSubstructureMatcher::unignoreAllAtoms()
{
    _ignored_atoms.clear();
}

IndigoMoleculeSubstructureMatchIter* IndigoMoleculeSubstructureMatcher::iterateQueryMatches(IndigoObject& query_object, bool embedding_edges_uniqueness,
                                                                                            bool find_unique_embeddings, bool for_iteration, int max_embeddings)
{
    QueryMolecule& query = query_object.getQueryMolecule();

    Molecule* target_prepared;
    Array<int>* mapping;
    bool* prepared;
    MoleculeAtomNeighbourhoodCounters* nei_counters;

    // If max_embeddings is 1 then it is only check for substructure
    // and not enumeration of number of matches
    if (MoleculeSubstructureMatcher::shouldUnfoldTargetHydrogens(query, max_embeddings != 1))
    {
        if (!_arom_h_unfolded_prepared)
            _target_arom_h_unfolded.clone(target, &_mapping_arom_h_unfolded, 0);

        target_prepared = &_target_arom_h_unfolded;
        mapping = &_mapping_arom_h_unfolded;
        prepared = &_arom_h_unfolded_prepared;
        nei_counters = &_nei_counters_h_unfolded;
    }
    else
    {
        if (!_arom_prepared)
            _target_arom.clone(target, &_mapping_arom, 0);
        target_prepared = &_target_arom;
        mapping = &_mapping_arom;
        prepared = &_arom_prepared;
        nei_counters = &_nei_counters;
    }
    if (!*prepared)
    {
        if (!target.isAromatized())
        {
            Indigo& indigo = indigoGetInstance();
            target_prepared->aromatize(indigo.arom_options);
        }
        nei_counters->calculate(*target_prepared);
        *prepared = true;
    }

    AutoPtr<IndigoMoleculeSubstructureMatchIter> iter(
        new IndigoMoleculeSubstructureMatchIter(*target_prepared, query, target, (mode == RESONANCE), max_embeddings != 1));

    if (query_object.type == IndigoObject::QUERY_MOLECULE)
    {
        IndigoQueryMolecule& qm_object = (IndigoQueryMolecule&)query_object;
        iter->matcher.setNeiCounters(&qm_object.getNeiCounters(), nei_counters);
    }

    Indigo& indigo = indigoGetInstance();
    iter->matcher.arom_options = indigo.arom_options;

    iter->matcher.find_unique_embeddings = find_unique_embeddings;
    iter->matcher.find_unique_by_edges = embedding_edges_uniqueness;
    iter->matcher.save_for_iteration = for_iteration;

    for (int i = 0; i < _ignored_atoms.size(); i++)
        iter->matcher.ignoreTargetAtom(mapping->at(_ignored_atoms[i]));

    iter->matcher.restore_unfolded_h = false;
    iter->mapping.copy(*mapping);
    iter->max_embeddings = max_embeddings;

    return iter.release();
}

IndigoTautomerSubstructureMatchIter* IndigoMoleculeSubstructureMatcher::iterateTautomerQueryMatches(IndigoObject& query_object, bool embedding_edges_uniqueness,
                                                                                                    bool find_unique_embeddings, bool for_iteration,
                                                                                                    int max_embeddings, TautomerMethod method)
{
    QueryMolecule& query = query_object.getQueryMolecule();

    Molecule* target_prepared;
    Array<int>* mapping;
    bool* prepared;
    MoleculeAtomNeighbourhoodCounters* nei_counters;

    {
        _target_arom_h_unfolded.clone(target, &_mapping_arom_h_unfolded, 0);

        target_prepared = &_target_arom_h_unfolded;
        mapping = &_mapping_arom_h_unfolded;
        prepared = &_arom_h_unfolded_prepared;
        nei_counters = &_nei_counters_h_unfolded;
    }

    AutoPtr<IndigoTautomerSubstructureMatchIter> iter(new IndigoTautomerSubstructureMatchIter(target, query, moleculeFound, method));

    iter->matcher.find_unique_embeddings = find_unique_embeddings;
    iter->matcher.find_unique_by_edges = embedding_edges_uniqueness;
    iter->matcher.save_for_iteration = for_iteration;

    Array<int> simpleMapping;
    simpleMapping.expand(mapping->size());
    for (int i = 0; i < simpleMapping.size(); ++i)
    {
        simpleMapping[i] = i;
    }
    iter->mapping.copy(simpleMapping);
    iter->max_embeddings = max_embeddings;

    return iter.release();
}

bool IndigoMoleculeSubstructureMatcher::findTautomerMatch(QueryMolecule& query, PtrArray<TautomerRule>& tautomer_rules, Array<int>& mapping_out)
{
    // shameless copy-paste from the method above
    bool* prepared;
    Molecule* target_prepared;
    Array<int>* mapping;
    if (MoleculeSubstructureMatcher::shouldUnfoldTargetHydrogens(query, false))
    {
        if (!_arom_h_unfolded_prepared)
            _target_arom_h_unfolded.clone(target, &_mapping_arom_h_unfolded, 0);
        target_prepared = &_target_arom_h_unfolded;
        mapping = &_mapping_arom_h_unfolded;
        prepared = &_arom_h_unfolded_prepared;
    }
    else
    {
        if (!_arom_prepared)
            _target_arom.clone(target, &_mapping_arom, 0);
        target_prepared = &_target_arom;
        mapping = &_mapping_arom;
        prepared = &_arom_prepared;
    }
    Indigo& indigo = indigoGetInstance();
    if (!target.isAromatized() && !*prepared)
        target_prepared->aromatize(indigo.arom_options);
    *prepared = true;

    if (tau_matcher.get() == 0)
    {
        bool substructure = true;
        tau_matcher.create(*target_prepared, substructure);
    }

    tau_matcher->setRulesList(&tautomer_rules);
    tau_matcher->setRules(tau_params.conditions, tau_params.force_hydrogens, tau_params.ring_chain, tau_params.method);
    tau_matcher->setQuery(query);
    tau_matcher->arom_options = indigo.arom_options;
    if (!tau_matcher->find())
        return false;

    mapping_out.clear_resize(query.vertexEnd());
    mapping_out.fffill();

    const int* qm = tau_matcher->getQueryMapping();

    for (int i = query.vertexBegin(); i != query.vertexEnd(); i = query.vertexNext(i))
        if (qm[i] >= 0)
            mapping_out[i] = mapping->at(qm[i]);

    return true;
}

CEXPORT int indigoSubstructureMatcher(int target, const char* mode_str){INDIGO_BEGIN{IndigoObject& obj = self.getObject(target);

if (IndigoBaseMolecule::is(obj))
{
    Molecule& mol = obj.getMolecule();
    int mode = IndigoMoleculeSubstructureMatcher::NORMAL;
    IndigoTautomerParams tau_params;

    if (mode_str != 0 && *mode_str != 0)
    {
        if (_indigoParseTautomerFlags(mode_str, tau_params))
            mode = IndigoMoleculeSubstructureMatcher::TAUTOMER;
        else if (strcasecmp(mode_str, "RES") == 0)
            mode = IndigoMoleculeSubstructureMatcher::RESONANCE;
        else
            throw IndigoError("indigoSubstructureMatcher(): unsupported mode %s", mode_str);
    }

    AutoPtr<IndigoMoleculeSubstructureMatcher> matcher(new IndigoMoleculeSubstructureMatcher(mol, mode));

    if (mode == IndigoMoleculeSubstructureMatcher::TAUTOMER)
        matcher->tau_params = tau_params;

    return self.addObject(matcher.release());
}
if (IndigoBaseReaction::is(obj))
{
    Reaction& rxn = obj.getReaction();
    bool daylight_aam = false;

    if (mode_str != 0 && *mode_str != 0)
    {
        if (strcasecmp(mode_str, "DAYLIGHT-AAM") == 0)
            daylight_aam = true;
        else
            throw IndigoError("reaction substructure matcher: unknown mode %s", mode_str);
    }

    AutoPtr<IndigoReactionSubstructureMatcher> matcher(new IndigoReactionSubstructureMatcher(rxn));
    matcher->daylight_aam = daylight_aam;
    return self.addObject(matcher.release());
}
throw IndigoError("indigoSubstructureMatcher(): %s is neither a molecule not a reaction", obj.debugInfo());
}
INDIGO_END(-1)
}

IndigoMoleculeSubstructureMatcher& IndigoMoleculeSubstructureMatcher::cast(IndigoObject& obj)
{
    if (obj.type != IndigoObject::MOLECULE_SUBSTRUCTURE_MATCHER)
        throw IndigoError("%s is not a matcher object", obj.debugInfo());

    return (IndigoMoleculeSubstructureMatcher&)obj;
}

IndigoMoleculeSubstructureMatchIter* IndigoMoleculeSubstructureMatcher::getMatchIterator(Indigo& self, int query, bool for_iteration, int max_embeddings)
{
    return iterateQueryMatches(self.getObject(query), self.embedding_edges_uniqueness, self.find_unique_embeddings, for_iteration, max_embeddings);
}

IndigoTautomerSubstructureMatchIter* IndigoMoleculeSubstructureMatcher::getTautomerMatchIterator(Indigo& self, int query, bool for_iteration,
                                                                                                 int max_embeddings, TautomerMethod method)
{
    return iterateTautomerQueryMatches(self.getObject(query), self.embedding_edges_uniqueness, self.find_unique_embeddings, for_iteration, max_embeddings,
                                       method);
}

CEXPORT int indigoIgnoreAtom(int target_matcher, int atom_object){
    INDIGO_BEGIN{IndigoMoleculeSubstructureMatcher& matcher = IndigoMoleculeSubstructureMatcher::cast(self.getObject(target_matcher));

IndigoAtom& ia = IndigoAtom::cast(self.getObject(atom_object));
matcher.ignoreAtom(ia.idx);
return 0;
}
INDIGO_END(-1)
}

// Ignore target atom in the substructure matcher
CEXPORT int indigoUnignoreAtom(int target_matcher, int atom_object){
    INDIGO_BEGIN{IndigoMoleculeSubstructureMatcher& matcher = IndigoMoleculeSubstructureMatcher::cast(self.getObject(target_matcher));

IndigoAtom& ia = IndigoAtom::cast(self.getObject(atom_object));
matcher.unignoreAtom(ia.idx);
return 0;
}
INDIGO_END(-1)
}

CEXPORT int indigoUnignoreAllAtoms(int target_matcher){
    INDIGO_BEGIN{IndigoMoleculeSubstructureMatcher& matcher = IndigoMoleculeSubstructureMatcher::cast(self.getObject(target_matcher));
matcher.unignoreAllAtoms();
return 0;
}
INDIGO_END(-1)
}

CEXPORT int indigoMatch(int target_matcher, int query)
{
    INDIGO_BEGIN
    {
        IndigoObject& obj = self.getObject(target_matcher);

        if (obj.type == IndigoObject::MOLECULE_SUBSTRUCTURE_MATCHER)
        {
            IndigoMoleculeSubstructureMatcher& matcher = IndigoMoleculeSubstructureMatcher::cast(obj);

            if (matcher.mode == IndigoMoleculeSubstructureMatcher::TAUTOMER)
            {
                switch (matcher.tau_params.method)
                {
                case BASIC: {
                    QueryMolecule& qmol = self.getObject(query).getQueryMolecule();
                    AutoPtr<IndigoMapping> mptr(new IndigoMapping(qmol, matcher.target));
                    if (!matcher.findTautomerMatch(qmol, self.tautomer_rules, mptr->mapping))
                        return 0;

                    return self.addObject(mptr.release());
                }
                case INCHI:
                case RSMARTS: {
                    AutoPtr<IndigoTautomerSubstructureMatchIter> match_iter(matcher.getTautomerMatchIterator(self, query, true, 1, matcher.tau_params.method));

                    match_iter->matcher.find_unique_embeddings = false;

                    if (!match_iter->hasNext())
                        return 0;
                    return self.addObject(match_iter->next());
                }
                }
            }
            else // NORMAL or RESONANCE
            {
                AutoPtr<IndigoMoleculeSubstructureMatchIter> match_iter(matcher.getMatchIterator(self, query, false, 1));

                match_iter->matcher.find_unique_embeddings = false;

                if (!match_iter->hasNext())
                    return 0;
                return self.addObject(match_iter->next());
            }
        }
        if (obj.type == IndigoObject::REACTION_SUBSTRUCTURE_MATCHER)
        {
            IndigoReactionSubstructureMatcher& matcher = IndigoReactionSubstructureMatcher::cast(obj);
            QueryReaction& qrxn = self.getObject(query).getQueryReaction();
            int i, j;

            ReactionAutomapper ram(qrxn);
            ram.arom_options = self.arom_options;
            ram.correctReactingCenters(true);

            for (i = qrxn.begin(); i != qrxn.end(); i = qrxn.next(i))
                if (MoleculeSubstructureMatcher::shouldUnfoldTargetHydrogens(qrxn.getQueryMolecule(i), false))
                    break;

            if (i != qrxn.end())
            {
                matcher.target.unfoldHydrogens();
                // expand mappings to include unfolded hydrogens
                for (i = matcher.target.begin(); i != matcher.target.end(); i = matcher.target.next(i))
                    matcher.mappings[i].expandFill(matcher.target.getBaseMolecule(i).vertexEnd(), -1);
            }

            if (matcher.matcher.get() == 0)
                matcher.matcher.create(matcher.target);

            matcher.matcher->use_daylight_aam_mode = matcher.daylight_aam;
            matcher.matcher->setQuery(qrxn);
            matcher.matcher->arom_options = self.arom_options;

            if (!matcher.matcher->find())
                return 0;

            AutoPtr<IndigoReactionMapping> mapping(new IndigoReactionMapping(qrxn, matcher.original_target));
            mapping->mol_mapping.clear_resize(qrxn.end());
            mapping->mol_mapping.fffill();
            mapping->mappings.expand(qrxn.end());

            for (i = qrxn.begin(); i != qrxn.end(); i = qrxn.next(i))
            {
                if (qrxn.getSideType(i) == BaseReaction::CATALYST)
                    continue;
                int tmol_idx = matcher.matcher->getTargetMoleculeIndex(i);
                mapping->mol_mapping[i] = matcher.mol_mapping[tmol_idx];
                BaseMolecule& qm = qrxn.getBaseMolecule(i);
                mapping->mappings[i].clear_resize(qm.vertexEnd());
                mapping->mappings[i].fffill();
                for (j = qm.vertexBegin(); j != qm.vertexEnd(); j = qm.vertexNext(j))
                {
                    int mapped = matcher.matcher->getQueryMoleculeMapping(i)[j];

                    if (mapped >= 0) // hydrogens are ignored
                        mapping->mappings[i][j] = matcher.mappings[tmol_idx][mapped];
                }
            }

            return self.addObject(mapping.release());
        }
        throw IndigoError("indigoIterateMatches(): expected a matcher, got %s", obj.debugInfo());
    }
    INDIGO_END(-1)
}

int indigoCountMatches(int target_matcher, int query)
{
    return indigoCountMatchesWithLimit(target_matcher, query, 0);
}

CEXPORT int indigoCountMatchesWithLimit(int target_matcher, int query, int embeddings_limit)
{
    INDIGO_BEGIN
    {
        IndigoObject& obj = self.getObject(target_matcher);

        if (obj.type == IndigoObject::MOLECULE_SUBSTRUCTURE_MATCHER)
        {
            IndigoMoleculeSubstructureMatcher& matcher = IndigoMoleculeSubstructureMatcher::cast(obj);

            if (matcher.mode == IndigoMoleculeSubstructureMatcher::TAUTOMER)
                throw IndigoError("count matches: not supported in this mode");

            if (embeddings_limit > self.max_embeddings)
                throw IndigoError("count matches: embeddings limit is more then maximum "
                                  "allowed embeddings specified by options");

            AutoPtr<IndigoMoleculeSubstructureMatchIter> match_iter(matcher.getMatchIterator(self, query, false, self.max_embeddings));

            return match_iter->countMatches(embeddings_limit);
        }
        if (obj.type == IndigoObject::REACTION_SUBSTRUCTURE_MATCHER)
            throw IndigoError("count matches: can not work with reactions");
        throw IndigoError("count matches: expected a matcher, got %s", obj.debugInfo());
    }
    INDIGO_END(-1)
}

int indigoIterateMatches(int target_matcher, int query)
{
    INDIGO_BEGIN
    {
        IndigoObject& obj = self.getObject(target_matcher);

        if (obj.type == IndigoObject::MOLECULE_SUBSTRUCTURE_MATCHER)
        {
            IndigoMoleculeSubstructureMatcher& matcher = IndigoMoleculeSubstructureMatcher::cast(obj);

            if (matcher.tau_params.method != BASIC && matcher.mode == IndigoMoleculeSubstructureMatcher::TAUTOMER)
            {
                AutoPtr<IndigoTautomerSubstructureMatchIter> match_iter(
                    matcher.getTautomerMatchIterator(self, query, true, self.max_embeddings, matcher.tau_params.method));
                return self.addObject(match_iter.release());
            }
            else if (matcher.mode == IndigoMoleculeSubstructureMatcher::TAUTOMER)
                throw IndigoError("indigoIterateMatches(): not supported in this mode");

            AutoPtr<IndigoMoleculeSubstructureMatchIter> match_iter(matcher.getMatchIterator(self, query, true, self.max_embeddings));

            return self.addObject(match_iter.release());
        }
        if (obj.type == IndigoObject::REACTION_SUBSTRUCTURE_MATCHER)
            throw IndigoError("indigoIterateMatches(): can not work with reactions");
        throw IndigoError("indigoIterateMatches(): expected a matcher, got %s", obj.debugInfo());
    }
    INDIGO_END(-1)
}

const char* IndigoReactionSubstructureMatcher::debugInfo()
{
    return "<reaction substructure matcher>";
}

IndigoReactionSubstructureMatcher::IndigoReactionSubstructureMatcher(Reaction& target_) : IndigoObject(REACTION_SUBSTRUCTURE_MATCHER), original_target(target_)
{
    target.clone(target_, &mol_mapping, &mappings, 0);

    Indigo& indigo = indigoGetInstance();
    target.aromatize(indigo.arom_options);
    daylight_aam = false;
}

IndigoReactionSubstructureMatcher::~IndigoReactionSubstructureMatcher()
{
}

IndigoReactionSubstructureMatcher& IndigoReactionSubstructureMatcher::cast(IndigoObject& obj)
{
    if (obj.type != IndigoObject::REACTION_SUBSTRUCTURE_MATCHER)
        throw IndigoError("%s is not a reaction matcher object", obj.debugInfo());

    return (IndigoReactionSubstructureMatcher&)obj;
}