-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrxn_standardize.cc
1395 lines (1154 loc) · 38.8 KB
/
rxn_standardize.cc
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
/*
standardizes a reaction
*/
#include <iostream>
#include <memory>
using std::cerr;
using std::endl;
#include "cmdline.h"
#include "accumulator.h"
#include "misc.h"
#include "istream_and_type.h"
#include "rxn_file.h"
#include "aromatic.h"
#include "iwstandard.h"
#include "mdl_molecule.h"
const char * prog_name = NULL;
static int verbose = 0;
static int reactions_read = 0;
static Chemical_Standardisation chemical_standardisation;
static int reduce_to_largest_product = 0;
static int reduce_to_largest_reactant = 0;
static int eliminate_reagents_not_participating = 0;
static int remove_fragments_not_participating = 0;
static int ignore_bad_reactions = 0;
static int bad_reactions_ignored = 0;
static IWString_and_File_Descriptor stream_for_discarded_reactions;
static int skip_reactions_with_multiple_reactants = 0;
static int skip_reactions_with_no_common_mapped_atoms = 0;
static int reactions_with_multiple_reactants_skipped = 0;
static int skip_reactions_with_multiple_products = 0;
static int reactions_with_multiple_products_skipped = 0;
static int auto_fix_orphans = 0;
static int discard_reactions_with_isotopes = 0;
static int reactions_discarded_for_isotopic_atoms = 0;
static int remove_duplicate_reactants = 0;
static int remove_duplicate_products = 0;
static int remove_duplicate_agents = 0;
static int remove_all_agents = 0;
static int remove_unmapped_components = 0;
static int remove_unchanged_components = 0;
static int use_first_token_of_name = 0;
static int gsub_reaction_names = 0;
static int compress_consecutive_underscores = 0;
//static int take_first_of_multiple_reagents = 0;
static int fix_kekule_problems = 0;
static int reactions_with_kekule_problems_fixed = 0;
static int reactions_with_fragments_not_participating = 0;
static int reactions_with_reagents_not_participating = 0;
static int reactions_with_duplicate_reagents = 0;
static int reactions_with_reagent_count_changed = 0;
static int reactions_with_no_common_mapped_atoms = 0;
static int reactions_with_orphan_atoms = 0;
static int reactions_with_counterions_moved_to_orphan = 0;
static int reactions_with_duplicates_atom_maps_ignored = 0;
static int reactions_with_no_reagents = 0;
static int reactions_with_no_products = 0;
static int reactions_written = 0;
static bool use_all_plusses = false;
static bool use_all_dots = false;
static Accumulator_Int<int> acc_natoms;
static int max_atoms_in_reagent_and_product = 0;
static int reactions_discarded_for_too_many_atoms = 0;
static int reading_multi_reaction_rdfile = 0;
static int do_automatic_atom_mapping = 0;
static int discard_reactions_containing_duplicate_atom_map_numbers = 0;
static int reactions_containing_duplicate_atom_map_numbers = 0;
static int unmap_duplicate_atom_map_numbers = 0;
static int assign_unmapped_atoms = 0;
static int skip_reactions_where_largest_fragment_is_unchanged = 0;
static int skip_reactions_containing_aromatic_bonds = 0;
static int reactions_containing_aromatic_bonds = 0;
static int reactions_where_largest_fragment_does_not_change = 0;
//static int input_is_reaction_smiles = 1;
static void
usage()
{
// cerr << __FILE__ << " compiled " << __DATE__ << " " << __TIME__ << endl;
// cerr << "Converts an mdl reaction file to reaction smiles\n";
// cerr << " -R reading multi-reaction RDfile\n";
// cerr << " -s reading an already formed reaction smiles\n";
// cerr << " -o create reagent fragments that are orphans\n";
// cerr << " -m skip reactions that have multiple reagents\n";
// cerr << " -I discard reactions containing isotopic atoms\n";
// cerr << " -K fix alternating Kekule issues\n";
// cerr << " -e move small fragments that show up on products to orphan status\n";
// cerr << " -U <fname> write discarded reactions to <fname>\n";
// cerr << " -b remove duplicate reagents, even if atom maps scrambled\n";
// cerr << " -f trunc truncate reaction names to the first token\n";
// cerr << " -f gsub replace unusual characters in reaction names with _\n";
// cerr << " -f __ if the name is changed, compress consecutive underscores\n";
// cerr << " -d r separate reagents and products with . rather than +\n";
// cerr << " -d o separate orphan atoms from reagents with . rather than +\n";
// cerr << " -C <natoms> discard any reaction where the largest reagent has more than <natoms> atoms\n";
// cerr << " -X ... miscellaneous options, enter -X help for help\n";
// cerr << " -c discard chirality on input\n";
// cerr << " -D x discard reactions containing duplicate atom map numbers\n";
// cerr << " -D u unmap all atoms with duplicate atom map numbers\n";
// cerr << " -l reduce to largest fragment\n";
// cerr << " -g ... chemical standardisation options\n";
// cerr << " -E ... standard element specifications\n";
// cerr << " -A ... standard aromaticity specifications\n";
// cerr << " -v verbose output\n";
cerr << __FILE__ << " compiled " << __DATE__ << " " << __TIME__ << endl;
cerr << "Standarizes a reaction file supplied in MDL format or in Lilly SMILES format\n";
cerr << " -R reading multi-reaction RDfile\n";
cerr << " -s reading a reaction smiles (default is -s)\n";
cerr << " -c discard chirality on input\n";
cerr << " -K fix alternating Kekule issues\n";
cerr << " -o create reactant fragments that are orphans\n";
cerr << " -d d separate components with . rather than +\n";
cerr << " -d p separate components with + rather than .\n";
cerr << " -C <natoms> discard any reaction where the largest reagent has more than <natoms> atoms\n";
cerr << " -f trunc truncate reaction names to the first token\n";
cerr << " -f gsub replace unusual characters in reaction names with _\n";
cerr << " -f __ if the name is changed, compress consecutive underscores\n";
cerr << " -X ... options related to discarding reactions, enter -X help for help\n";
cerr << " -D ... options related to discarding reaction components, enter -D help for help\n";
cerr << " -U <fname> write discarded reactions to <fname>\n";
cerr << " -g ... chemical standardisation options\n";
cerr << " -E ... standard element specifications\n";
cerr << " -A ... standard aromaticity specifications\n";
cerr << " -M generate atom maps\n";
cerr << " -q quiet; suppress warnings about no mtched atoms\n";
cerr << " -v verbose output\n";
exit(1);
}
static void
process_reaction_name(IWString & s)
{
if (! gsub_reaction_names && ! use_first_token_of_name) // nothing to do
return;
//cerr << "Starting - name is : " << s << endl;
const int n = s.number_elements();
int contains_whitespace = 0;
int contains_non_alpha = 0;
int changes_made = 0;
for (int i = 0; i < n; ++i)
{
const char c = s[i];
if (isalnum(c))
continue;
if ('_' == c || '.' == c || '-' == c)
continue;
if (use_first_token_of_name)
{
s.iwtruncate(i);
changes_made++;
//cerr << "Truncated: " << s << endl;
break;
}
else if (gsub_reaction_names)
{
s[i] = '_';
changes_made++;
//cerr << "fixing a char - name is now: " << s << endl;
}
else if (isspace(c))
contains_whitespace++;
else
contains_non_alpha++;
}
//cerr <<"name after fixes:: " << s << endl;
if (contains_whitespace || contains_non_alpha)
cerr << "Warning reaction '" << s << "' whitespace or other characters\n";
if (! changes_made)
return;
//cerr << "compress_consecutive_underscores: " << compress_consecutive_underscores << endl;
if (! compress_consecutive_underscores)
return;
bool previous_was_underscore = false;
int ndx = 0;
for (int i = 0; i < n; ++i)
{
//cerr << "name: " << s << endl;
//cerr << "thisChar: " << s[i] << endl;
if ('_' == s[i])
{
if (! previous_was_underscore)
{
s[ndx] = s[i];
ndx++;
previous_was_underscore = true;
}
//else
//cerr << "skipping" << endl;
}
else
{
s[ndx] = s[i];
ndx++;
previous_was_underscore = false;
}
}
s.iwtruncate(ndx);
//cerr << "Name set to '" << s << "'\n";
return;
}
static int
contains_aromatic_bonds(const ISIS_RXN_FILE_Molecule & m)
{
const int nedges = m.nedges();
for (int i = 0; i < nedges; ++i)
{
const Bond * b = m.bondi(i);
if (b->is_single_bond())
;
else if (b->is_double_bond())
;
else if (b->is_triple_bond())
;
else if (b->is_aromatic())
return 1;
}
return 0;
}
static int
contains_aromatic_bonds(RXN_File & rxn)
{
const int ns = rxn.number_reagents();
for (int i = 0; i < ns; ++i)
{
if (contains_aromatic_bonds(rxn.reagent(i)))
return 1;
}
return 0;
}
static int
echo_bad_data(iwstring_data_source & input,
const off_t initial_offset,
IWString_and_File_Descriptor & output)
{
const auto current_offset = input.tellg();
input.seekg(initial_offset);
input.echo(output, (current_offset - initial_offset));
input.seekg(current_offset);
output.write_if_buffer_holds_more_than(4096);
return input.tellg() == current_offset;
}
static int
rxn_standardize(RXN_File & rxn,
iwstring_data_source & input,
const off_t initial_offset,
IWString_and_File_Descriptor & output)
{
const int initial_nr = rxn.number_reagents();
if (verbose > 1)
cerr << endl << "Read '" << rxn.name() << endl << "' with " << initial_nr << " reagents\n";
IWString tmp(rxn.name());
process_reaction_name(tmp);
rxn.set_name(tmp);
if (0 == initial_nr)
{
cerr << "Skipping reaction with no reagents " << rxn.name() << endl;
reactions_with_no_reagents++;
if (stream_for_discarded_reactions.is_open())
echo_bad_data(input, initial_nr, stream_for_discarded_reactions);
return 1;
}
if (0 == rxn.number_products())
{
cerr << "Skipping reaction with no products " << rxn.name() << endl;
reactions_with_no_products++;
if (stream_for_discarded_reactions.is_open())
echo_bad_data(input, initial_nr, stream_for_discarded_reactions);
return 1;
}
#ifdef SHOW_ATOM_NUMBERED_REACTIONS
for (int i = 0; i < rxn.number_reagents(); ++i)
{
ISIS_RXN_FILE_Molecule & r = rxn.reagent(i);
r.set_isotope_to_atom_number_no_perturb_canonical_ordering();
cerr << r.smiles() << " " << rxn.name() << " reagent " << i << endl;
}
for (int i = 0; i < rxn.number_products(); ++i)
{
ISIS_RXN_FILE_Molecule & p = rxn.product(i);
p.set_isotope_to_atom_number_no_perturb_canonical_ordering();
cerr << p.smiles() << " " << rxn.name() << " product " << i << endl;
}
for (int i = 0; i < rxn.number_reagents(); ++i)
{
ISIS_RXN_FILE_Molecule & r = rxn.reagent(i);
if (i > 0)
cerr << '.';
cerr << r.smiles();
r.transform_to_non_isotopic_form();
}
cerr << ">>";
for (int i = 0; i < rxn.number_products(); ++i)
{
ISIS_RXN_FILE_Molecule & p = rxn.product(i);
if (i > 0)
cerr << '.';
cerr << p.smiles();
p.transform_to_non_isotopic_form();
}
cerr << " " << rxn.name() << endl;
#endif
// the order of operations is important - For example: if duplicate reactant are to be removed, then we should do that before checking for
// duplicated mapping numbers.
//#define DEBUG_RXN_STANDARDIZE
#ifdef DEBUG_RXN_STANDARDIZE
cerr << "LINE " << __LINE__ << endl;
rxn.debug_print(cerr);
#endif
if (max_atoms_in_reagent_and_product > 0 || verbose) // do first because it may stop various other failures (hide our problems!)
{
const int x = rxn.max_atom_in_any_reagent();
const int y = rxn.max_atom_in_any_product();
//cerr << "Reagent atom count: " << x << " Product atom count: " <<y << endl;
if (verbose)
acc_natoms.extra(x);
if (max_atoms_in_reagent_and_product > 0 && x > max_atoms_in_reagent_and_product && y > max_atoms_in_reagent_and_product)
{
if (verbose > 1)
cerr << rxn.name() << " too many atoms in a reagent and a product " << x << endl;
reactions_discarded_for_too_many_atoms++;
if (stream_for_discarded_reactions.is_open())
echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
return 1;
}
}
#ifdef DEBUG_RXN_STANDARDIZE
cerr << "LINE " << __LINE__ << endl;
rxn.debug_print(cerr);
#endif
if (assign_unmapped_atoms)
{
rxn.assign_unmapped_atoms();
}
if (unmap_duplicate_atom_map_numbers && rxn.contains_duplicate_atom_map_numbers())
{
rxn.unmap_duplicate_atom_map_numbers();
}
#ifdef DEBUG_RXN_STANDARDIZE
cerr << "LINE " << __LINE__ << endl;
rxn.debug_print(cerr);
#endif
if (remove_fragments_not_participating && rxn.remove_fragments_not_participating())
{
if (verbose > 1)
cerr << "Removed fragments not reacting from '" << rxn.name() << ", now " << rxn.number_reagents() << " reagents\n";
reactions_with_fragments_not_participating++;
if (0 == rxn.number_reagents())
{
cerr << "After removing fragments not reacting from " << rxn.name() << ", no reactants remain\n";
if (stream_for_discarded_reactions.is_open())
echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
return 1;
}
}
#ifdef DEBUG_RXN_STANDARDIZE
cerr << "LINE " << __LINE__ << endl;
rxn.debug_print(cerr);
#endif
if (eliminate_reagents_not_participating && rxn.eliminate_reagents_not_participating())
{
if (verbose > 1)
cerr << "Removed reagents not reacting from '" << rxn.name() << "', now " << rxn.number_reagents() << " reagents\n";
reactions_with_reagents_not_participating++;
if (0 == rxn.number_reagents())
{
cerr << "After removing reactants not reacting from " << rxn.name() << ", no reactants remain\n";
if (stream_for_discarded_reactions.is_open())
echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
return 1;
}
}
#ifdef DEBUG_RXN_STANDARDIZE
cerr << "LINE " << __LINE__ << endl;
rxn.debug_print(cerr);
#endif
// if (rxn.all_reagents_the_same())
// {
// if (verbose > 1)
// cerr << "Removed duplicate reagents from '" << rxn.name() << "' now " << rxn.number_reagents() << " reagents\n";
//
// reactions_with_duplicate_reagents++;
// }
#ifdef DEBUG_RXN_STANDARDIZE
cerr << "LINE " << __LINE__ << endl;
rxn.debug_print(cerr);
#endif
if (reduce_to_largest_product && rxn.reduce_to_largest_product())
{
if (verbose > 1)
cerr << "Reduced to the largest product fragment from '" << rxn.name() << "\n";
}
if (reduce_to_largest_reactant && rxn.reduce_to_largest_reactant())
{
if (verbose > 1)
cerr << "Reduced to the largest reactant fragment from '" << rxn.name() << "\n";
}
if (remove_duplicate_reactants && rxn.remove_duplicate_reagents_ignore_atom_map())
{
if (verbose > 1)
cerr << "Removed duplicate reactants from '" << rxn.name() << "' now " << rxn.number_reagents() << " reactants\n";
}
if (remove_duplicate_products && rxn.remove_duplicate_products_ignore_atom_map()) //qqqwwwqqq
{
if (verbose > 1)
cerr << "Removed duplicate products from '" << rxn.name() << "' now " << rxn.number_reagents() << " reactants\n";
}
if (remove_duplicate_agents && rxn.remove_duplicate_agents())
{
if (verbose > 1)
cerr << "Removed duplicate agents from '" << rxn.name() << "' now " << rxn.number_agents() << " agents\n";
}
if (remove_all_agents && rxn.remove_all_agents())
{
if (verbose > 1)
cerr << "Removed all agents from '" << rxn.name() << "'\n";
}
if (remove_unmapped_components && rxn.remove_unmapped_components()) //qqqwwwqqq
{
if (verbose > 1)
cerr << "Removed all unmapped components from '" << rxn.name() << "'\n";
}
if (remove_unchanged_components && rxn.remove_unchanging_components())
{
if (verbose > 1)
cerr << "Removed all reactants that also appear as products from '" << rxn.name() << "'\n";
}
#ifdef DEBUG_RXN_STANDARDIZE
cerr << "LINE " << __LINE__ << endl;
rxn.debug_print(cerr);
#endif
if (skip_reactions_with_no_common_mapped_atoms && !rxn.at_least_some_mapped_atoms_common_btw_reagents_and_products())
{
if (verbose > 1)
cerr << "No reagent mapped atoms in products " << rxn.name() << endl;
reactions_with_no_common_mapped_atoms++;
return 1;
}
if (auto_fix_orphans)
{
rxn.set_auto_fix_orphans(1); // set this here because we only want to fix the orphans after other processing
if (rxn.check_for_widows_and_orphans())
{
if (verbose > 1)
cerr << rxn.name() << " identified orphan atoms\n";
}
rxn.set_auto_fix_orphans(0); // undo the set to fix orphans
}
if (fix_kekule_problems)
{
if (rxn.fix_kekule_differences())
{
reactions_with_kekule_problems_fixed++;
if (verbose > 1)
cerr << "Fixed Kekule problems in " << rxn.name() << endl;
}
}
//rxn.print_atom_map_into(cerr);
const int nreagents = rxn.number_reagents();
if (initial_nr != nreagents)
{
if (verbose > 1)
cerr << "Reagent count changed " << initial_nr << " to " << nreagents << " in " << rxn.name() << endl;
reactions_with_reagent_count_changed++;
}
if (rxn.contains_duplicate_atom_map_numbers())
{
reactions_containing_duplicate_atom_map_numbers++;
if (verbose > 1)
cerr << rxn.name() << " contains duplicate atom map numbers\n";
if (discard_reactions_containing_duplicate_atom_map_numbers)
{
if (stream_for_discarded_reactions.is_open())
echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
return 1;
}
}
if (skip_reactions_with_multiple_reactants && rxn.number_reagents() > 1)
{
reactions_with_multiple_reactants_skipped++;
if (stream_for_discarded_reactions.is_open())
echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
return 1;
}
if (skip_reactions_with_multiple_products && rxn.number_products() > 1)
{
reactions_with_multiple_products_skipped++;
if (stream_for_discarded_reactions.is_open())
echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
return 1;
}
if (skip_reactions_where_largest_fragment_is_unchanged && ! rxn.largest_fragment_is_changed())
{
reactions_where_largest_fragment_does_not_change++;
if (stream_for_discarded_reactions)
echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
return 1;
}
if (skip_reactions_containing_aromatic_bonds && contains_aromatic_bonds(rxn))
{
reactions_containing_aromatic_bonds++;
if (stream_for_discarded_reactions)
echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
return 1;
}
if (discard_reactions_with_isotopes && rxn.contains_isotopic_reagent_atoms())
{
if (verbose > 1)
cerr << rxn.name() << " contains isotopic atoms\n";
reactions_discarded_for_isotopic_atoms++;
if (stream_for_discarded_reactions.is_open())
echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
return 1;
}
if (rxn.contains_orphan_atoms())
{
if (verbose > 1)
cerr << rxn.name() << " contains " << rxn.contains_orphan_atoms() << " orphan atoms\n";
reactions_with_orphan_atoms++;
}
Reaction_Smiles_Options opts;
if (use_all_plusses)
{
opts.set_reagent_product_plus_rather_than_dot(true); // controls char between components
setUseThisCharAsDotInSmiles('+'); // controls char between fragments of each component
}
else if (use_all_dots)
{
opts.set_reagent_product_plus_rather_than_dot(false); // controls char between components
setUseThisCharAsDotInSmiles('.'); // controls char between fragments of each component
}
else // normal, defalt mode - plusses between cdomponents and dots between fragments of each component
{
opts.set_reagent_product_plus_rather_than_dot(true); // controls char between components
setUseThisCharAsDotInSmiles('.'); // controls char between fragments of each component
}
// old way
//opts.set_reagent_product_plus_rather_than_dot(use_all_plusses);
//opts.set_orphan_use_all_plusses(orphan_use_all_plusses);
rxn.write_rxn_smiles(opts, output);
output.write_if_buffer_holds_more_than(4096);
reactions_written++;
return 1;
}
static void
initialise_reaction(RXN_File & rxn)
{
rxn.set_do_automatic_atom_mapping(do_automatic_atom_mapping);
// if (auto_fix_orphans)
// rxn.set_auto_fix_orphans(1);
return;
}
//static int
//rxn_standardize(iwstring_data_source & input,
// IWString_and_File_Descriptor & output)
//{
// RXN_File rxn;
//
// initialise_reaction(rxn);
//
// const auto initial_offset = input.tellg();
//
// if (rxn.do_read(input))
// ;
// else if (input.eof())
// return 1;
// else if (ignore_bad_reactions)
// {
// bad_reactions_ignored++;
//
// if (stream_for_discarded_reactions.is_open())
// echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
// return 1;
// }
// else
// {
// cerr << "Fatal error reading reaction, now at line " << input.lines_read() << endl;
// return 0;
// }
//
// reactions_read++;
//
// return rxn_standardize(rxn, input, initial_offset, output);
//}
static int
rxn_standardize_reaction_smiles(iwstring_data_source & input,
IWString_and_File_Descriptor & output)
{
const_IWSubstring buffer;
auto initial_offset = input.tellg(); // most likely 0
while (input.next_record(buffer))
{
RXN_File rxn;
// cerr << "Processing " << buffer << endl;
initialise_reaction(rxn);
if (! rxn.build_from_reaction_smiles(buffer))
{
cerr << "Cannot interpret " << buffer << endl;
if (ignore_bad_reactions)
{
bad_reactions_ignored++;
continue;
}
return 0;
}
reactions_read++;
if (! rxn_standardize(rxn, input, initial_offset, output))
{
cerr << "Cannot process " << buffer << endl;
return 0;
}
initial_offset = input.tellg();
}
return 1;
}
/*
RDfile input looks like
RDFILE 1
$DATM 2016/09/16 21:58
$RFMT $RIREG 1
$RXN
EE3-E15410-047
-NextMove-0822160938
1 1 3
$MOL
EE3-E15410-047
RDKit 2D
19 19 0 0 0 0 0 0 0 0999 V2000
12.6282 -6.1179 0.0000 C 0 0 0 0 0 0 0 0 1 0 0 0
11.7646 -4.6121 0.0000 C 0 0 0 0 0 0 0 0 1 0 0 0
...
$RFMT $RIREG 2
*/
static int
get_reaction (iwstring_data_source & input,
off_t & initial_offset,
RXN_File & rxn)
{
const_IWSubstring buffer;
while (input.next_record(buffer))
{
if (buffer.starts_with("$RFMT $RIREG "))
break;
}
//cerr << "Starting reaction " << buffer << endl;
if (input.eof())
return 0;
initial_offset = input.tellg();
return rxn.do_read(input);
}
static int
rxn_standardize_rdfile (iwstring_data_source & input,
IWString_and_File_Descriptor & output)
{
const_IWSubstring buffer;
if (! input.next_record(buffer))
{
cerr << "rxn_standardize_rdfile:cannot read header\n";
return 0;
}
if ("$RDFILE" != buffer)
cerr << "rxn_standardize_rdfile:warning header not 'RDFILE', got '" << buffer << "'\n";
while (true)
{
RXN_File rxn;
initialise_reaction(rxn);
off_t initial_offset = 0;
if (get_reaction(input, initial_offset, rxn))
;
else if (input.eof())
return 1;
else if (ignore_bad_reactions)
{
bad_reactions_ignored++;
if (stream_for_discarded_reactions.active())
{
echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
return 1;
}
}
else
{
cerr << "Error reading reaction, line " << input.lines_read() << endl;
if (stream_for_discarded_reactions.active())
echo_bad_data(input, initial_offset, stream_for_discarded_reactions);
return 0;
}
reactions_read++;
if (! rxn_standardize(rxn, input, initial_offset, output))
return 0;
}
return reactions_read;
}
static void
display_misc_X_options(std::ostream & output)
{
output << " -X rmmr discard reactions that have multiple reactants\n";
output << " -X rmmp discard reactions that have multiple products\n";
output << " -X rmiso discard reactions containing isotopic atoms\n";
output << " -X igbad discard bad reactions (default is to exit)\n";
output << " -X nclf discard reactions where there is no change in the largest fragment\n";
output << " -X rmncma discard reactions that have no common mapped atoms in the reactants and product\n";
output << " -X rmab discard reactions that contain aromatic bonds\n";
output << " -X rmdmap discard reactions containing duplicate atom map numbers\n";
exit(0);
}
static void
display_misc_D_options(std::ostream & output)
{
output << " -D rmdr remove duplicate reactants\n";
output << " -D rmdp remove duplicate products\n";
output << " -D rmda remove duplicate agents\n";
output << " -D rmaa remove all agents\n";
output << " -D rmnmap remove any component with no atom mapping at all\n";
output << " -D rmdup remove components that are shown as both reactants and products\n";
output << " -D rdlp reduce product to largest product\n";
output << " -D rdlr reduce reactant to largest reactant\n";
output << " -D rmrnp remove reactants not participating\n";
output << " -D rmfnp remove any reactant fragments not participating\n";
output << " -D unmap unmap all atoms with duplicate atom map numbers\n";
output << " -D fmap add unique mapping numbers for any atoms that are not mapped\n";
exit(0);
}
static int rxn_standardize(const char * fname, IWString_and_File_Descriptor & output);
static int
rxn_standardize_file_containing_reaction_files(iwstring_data_source & input,
IWString_and_File_Descriptor & output)
{
IWString buffer;
while (input.next_record(buffer))
{
if (! rxn_standardize(buffer.null_terminated_chars(), output))
return 0;
}
return 1;
}
static int
rxn_standardize_file_containing_reaction_files(const char * fname,
IWString_and_File_Descriptor & output)
{
IWString tmp(fname);
assert (tmp.starts_with("F:"));
tmp.remove_leading_chars(2);
iwstring_data_source input(tmp.null_terminated_chars());
if (! input.good())
{
cerr << "Cannot open file of reactions '" << tmp << "'\n";
return 0;
}
return rxn_standardize_file_containing_reaction_files(input, output);
}
static int
rxn_standardize (const char * fname,
IWString_and_File_Descriptor & output)
{
assert(NULL != fname);
const_IWSubstring tmp(fname);
if (tmp.starts_with("F:"))
return rxn_standardize_file_containing_reaction_files(fname, output);
iwstring_data_source input(fname);
if (! input.good())
{
cerr << prog_name << ": cannot open '" << fname << "'\n";
return 0;
}
if (reading_multi_reaction_rdfile)
return rxn_standardize_rdfile(input, output);
//if (input_is_reaction_smiles)
return rxn_standardize_reaction_smiles(input, output);
//return rxn_standardize(input, output);
}
static int
rxn_standardize (int argc, char ** argv)
{
//Command_Line cl(argc, argv, "vA:E:g:lomU:Ibf:Kd:C:eRX:cD:s");
Command_Line cl(argc, argv, "scKod:f:D:F:C:X:U:Rg:E:A:Mqv");
if (cl.unrecognised_options_encountered())
{
cerr << "Unrecognised options encountered\n";
usage();
}
verbose = cl.option_count('v');
if (cl.option_present('A'))
{
if (! process_standard_aromaticity_options(cl, verbose, 'A'))
{
cerr << "Cannot initialise aromaticity specifications\n";
usage();
}
}
else
set_global_aromaticity_type(Daylight);
if (cl.option_present('E'))
{
if (! process_elements(cl, verbose, 'E'))
{
cerr << "Cannot initialise elements\n";
usage();
}
}
if (cl.option_present('g'))
{
if (! chemical_standardisation.construct_from_command_line(cl, verbose > 1, 'g'))
{
cerr << "Cannot process chemical standardisation options (-g)\n";