-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathselfies.py
2640 lines (2278 loc) · 130 KB
/
selfies.py
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
import math
def selfies_alphabet():
alphabet=['[Branch1_1]','[Branch1_2]','[Branch1_3]','[Ring1]','[Branch2_1]','[Branch2_2]','[Branch2_3]','[Ring2]','[Branch3_1]','[Branch3_2]','[Branch3_3]','[Ring3]','[O]','[=O]','[N]','[=N]','[C]','[=C]','[#C]','[S]','[=S]','[P]','[F]','[C@Hexpl]','[C@@Hexpl]','[C@expl]','[C@@expl]','[H]','[NHexpl]'];
return alphabet
def _make_brackets_around_atoms(smiles): # first function in the encoder: All atoms are itemized via brackets.
# for example: C1=CO1O -> [C]1[=C][O][O].
# Meaning that bond information is treated as part of the atom, to ensure semantical validity
# Brackets define an element of the alphabet, and for each of them there is a rule vector in the grammar.
ii=0
smiles=smiles.replace(' ','')
current_new_smiles=''
while ii<len(smiles):
if smiles[ii]=='[':
small_smiles=smiles[ii:]
closing_bracket=small_smiles.find(']')
in_bracket_content=small_smiles[0:closing_bracket+1]
current_new_smiles+=in_bracket_content[0:-1]+'expl]'
ii+=len(in_bracket_content)
elif (smiles[ii]>='A' and smiles[ii]<='Z') or (smiles[ii]>='a' and smiles[ii]<='z') or smiles[ii]=='*':
smiles=smiles+' '
if smiles[ii:ii+2]=='Br' or smiles[ii:ii+2]=='Cl':
current_new_smiles+='['+smiles[ii:ii+2]+']'
ii+=2
else:
current_new_smiles+='['+smiles[ii:ii+1]+']'
ii+=1
smiles=smiles.replace(' ','')
elif smiles[ii]=='(' or smiles[ii]==')' or smiles[ii]=='.' or smiles[ii]=='%' or (smiles[ii]>='0' and smiles[ii]<='9'): # these symbols, for rings, brackets and divisions between molecules, will be outside the brackets
current_new_smiles+=smiles[ii]
ii+=1
elif smiles[ii]=='=' or smiles[ii]=='#' or smiles[ii]=='\\' or smiles[ii]=='/' or smiles[ii]=='-': # these symbols will be part of the brackets. The sterochemical information could potentially be outside the bracket, by introducing a new rule vector for them. What is more advantageos need to be investigated later.
pre_symbol=smiles[ii]
ii+=1
if smiles[ii]=='[':
small_smiles=smiles[ii:]
closing_bracket=small_smiles.find(']')
in_bracket_content=small_smiles[1:closing_bracket]
current_new_smiles+='['+pre_symbol+in_bracket_content+'expl]'
ii+=len(in_bracket_content)+2
elif (smiles[ii]>='A' and smiles[ii]<='Z') or (smiles[ii]>='a' and smiles[ii]<='z') or smiles[ii]=='*': # upper and lower case letters, as well as the wildcard symbol is part of the bracket.
smiles=smiles+' '
if smiles[ii:ii+2]=='Br' or smiles[ii:ii+2]=='Cl': # Chlor and Brom are usually written without brackets in SMILES strings, so need to be treated differently
current_new_smiles+='['+pre_symbol+smiles[ii:ii+2]+']'
ii+=2
else:
current_new_smiles+='['+pre_symbol+smiles[ii:ii+1]+']' # Add brackets around element
ii+=1
smiles=smiles.replace(' ','')
elif smiles[ii]=='%' or (smiles[ii]>='0' and smiles[ii]<='9'): # explicit bond-number with ring
current_new_smiles+=pre_symbol+smiles[ii]
ii+=1
else:
raise ValueError('_make_brackets_around_atoms: Unknown symbol in the string.')
else:
raise ValueError('_make_brackets_around_atoms: Unknown symbol in the string.')
return current_new_smiles
def _reconfigure_smiles_numbers1(smiles): # All rings get a unique identifiers
# list of unique identifiers, for purpose of speed, this list is hardcoded
available_nums=['@$aa','@$ab','@$ac','@$ad','@$ae','@$af','@$ag','@$ah','@$ai','@$aj','@$ak','@$al','@$am','@$an','@$ao','@$ap','@$aq','@$ar','@$as','@$at','@$au','@$av','@$aw','@$ax','@$ay','@$az','@$ba','@$bb','@$bc','@$bd','@$be','@$bf','@$bg','@$bh','@$bi','@$bj','@$bk','@$bl','@$bm','@$bn','@$bo','@$bp','@$bq','@$br','@$bs','@$bt','@$bu','@$bv','@$bw','@$bx','@$by','@$bz','@$ca','@$cb','@$cc','@$cd','@$ce','@$cf','@$cg','@$ch','@$ci','@$cj','@$ck','@$cl','@$cm','@$cn','@$co','@$cp','@$cq','@$cr','@$cs','@$ct','@$cu','@$cv','@$cw','@$cx','@$cy','@$cz','@$da','@$db','@$dc','@$dd','@$de','@$df','@$dg','@$dh','@$di','@$dj','@$dk','@$dl','@$dm','@$dn','@$do','@$dp','@$dq','@$dr','@$ds','@$dt','@$du','@$dv']
all_smiles=smiles.split('.')
all_smiles_new=''
for current_smiles in all_smiles: # make all rings of the form '%NNNN'
current_smiles_empty=current_smiles
in_bracket=0
jj=0
while jj<len(current_smiles_empty):
if current_smiles_empty[jj]=='[':
in_bracket=1
elif current_smiles_empty[jj]==']':
in_bracket=0
else:
if in_bracket==1:
current_smiles_empty=current_smiles_empty[0:jj]+' '+current_smiles_empty[jj+1:]
if current_smiles_empty[jj]>='0' and current_smiles_empty[jj]<='9':
current_smiles_empty=current_smiles_empty[0:jj]+'%0'+current_smiles_empty[jj:]
current_smiles=current_smiles[0:jj]+'%0'+current_smiles[jj:]
jj+=2
if current_smiles_empty[jj]=='%':
jj+=2
jj+=1
an_idx=0
cc=0
loop_count=0
while cc<len(current_smiles_empty): # replace all rings with a unique identifier
if cc<0:
raise ValueError('_reconfigure_smiles_numbers1: Malformed ring.')
letter=current_smiles_empty[cc]
if letter=='%':
current_num=current_smiles_empty[cc:cc+3]
for jj in range(2):
pos_of_num=current_smiles_empty.find(current_num)
current_smiles=current_smiles[0:pos_of_num]+available_nums[an_idx]+current_smiles[pos_of_num+3:]
current_smiles_empty=current_smiles_empty[0:pos_of_num]+available_nums[an_idx]+current_smiles_empty[pos_of_num+3:]
cc-=2
an_idx+=1
cc+=1
loop_count+=1
if loop_count>10000:
raise ValueError('_reconfigure_smiles_numbers1: Malformed ring.')
# bugfix for non canonical smiles - 26.08.2019
for symbol in available_nums:
find_identifier1=current_smiles.find(symbol)
if find_identifier1==-1:
break
else:
find_identifier1_single=current_smiles.find('-'+symbol)
if find_identifier1_single==find_identifier1-1:
current_smiles_tmp=current_smiles[0:find_identifier1]+'x'+current_smiles[find_identifier1+1:]
find_identifier2=current_smiles_tmp.find(symbol)
if current_smiles_tmp[find_identifier2-1]!='-':
all_smiles_new=current_smiles[0:find_identifier2]+'-'+current_smiles[find_identifier2:] # add explicit single bond from second identifier
current_smiles=current_smiles[0:find_identifier1_single]+current_smiles[find_identifier1:] # remove explicit single bond from first identifier
find_identifier1_single=current_smiles.find('='+symbol)
if find_identifier1_single==find_identifier1-1:
current_smiles_tmp=current_smiles[0:find_identifier1]+'x'+current_smiles[find_identifier1+1:]
find_identifier2=current_smiles_tmp.find(symbol)
if current_smiles_tmp[find_identifier2-1]!='=':
all_smiles_new=current_smiles[0:find_identifier2]+'-'+current_smiles[find_identifier2:] # add explicit single bond from second identifier
current_smiles=current_smiles[0:find_identifier1_single]+current_smiles[find_identifier1:] # remove explicit single bond from first identifier
# ##
all_smiles_new=all_smiles_new+'.'+current_smiles
return all_smiles_new[1:]
def _reconfigure_smiles_numbers2(smiles): # pairs of unique identifiers will be transformed into symbols which referes to the relative distance between them. all of them are of the form '%NNN'
available_nums=['@$aa','@$ab','@$ac','@$ad','@$ae','@$af','@$ag','@$ah','@$ai','@$aj','@$ak','@$al','@$am','@$an','@$ao','@$ap','@$aq','@$ar','@$as','@$at','@$au','@$av','@$aw','@$ax','@$ay','@$az','@$ba','@$bb','@$bc','@$bd','@$be','@$bf','@$bg','@$bh','@$bi','@$bj','@$bk','@$bl','@$bm','@$bn','@$bo','@$bp','@$bq','@$br','@$bs','@$bt','@$bu','@$bv','@$bw','@$bx','@$by','@$bz','@$ca','@$cb','@$cc','@$cd','@$ce','@$cf','@$cg','@$ch','@$ci','@$cj','@$ck','@$cl','@$cm','@$cn','@$co','@$cp','@$cq','@$cr','@$cs','@$ct','@$cu','@$cv','@$cw','@$cx','@$cy','@$cz','@$da','@$db','@$dc','@$dd','@$de','@$df','@$dg','@$dh','@$di','@$dj','@$dk','@$dl','@$dm','@$dn','@$do','@$dp','@$dq','@$dr','@$ds','@$dt','@$du','@$dv']
all_smiles=smiles.split('.')
all_smiles_new=''
for current_smiles in all_smiles:
for jj in range(len(available_nums)):
find_num_1=current_smiles.find(available_nums[jj])
if find_num_1>=0:
tmp_current_smiles=current_smiles[0:find_num_1]+'X'+current_smiles[find_num_1+1:]
find_num_2=tmp_current_smiles.find(available_nums[jj])
sub_smiles=current_smiles[find_num_1+4:find_num_2]
ring_size=len(sub_smiles)-len(sub_smiles.replace('[',''))
str_ring_size=str(ring_size)
if len(str_ring_size)==1:
ring_sizeSymbol='%000'+str_ring_size
elif len(str_ring_size)==2:
ring_sizeSymbol='%00'+str_ring_size
elif len(str_ring_size)==3:
ring_sizeSymbol='%0'+str_ring_size
elif len(str_ring_size)==4:
ring_sizeSymbol='%'+str_ring_size
else:
raise ValueError('_reconfigure_smiles_numbers2: Very long ring is not implemented.') # Rings larger than 9.999 Elements cannot be translated (can easily be extended if necessary)
current_smiles=current_smiles[0:find_num_1]+current_smiles[find_num_1+4:find_num_2]+ring_sizeSymbol+current_smiles[find_num_2+4:]
else:
break
all_smiles_new=all_smiles_new+'.'+current_smiles
return all_smiles_new[1:]
def _cumsum(int_list,cum_offset=0): # cumulative sum, without numpy (such that we dont need to include numpy at all)
cum_list=[cum_offset]
for x in int_list:
cum_list.append(cum_list[-1]+x)
return cum_list[1:]
def _smiles_to_selfies(smiles): # translating the prepared string into SELFIES.
# the start_alphabet is used by Ring and Branch functions for evaluating numbers. i.e. [epsilon] stands for 0, [Ring1] stands for 1, ...
# these functions use base 20, for example when a ring of size 5 is produces, it is [Ring1]N, where N='[Branch1_2]'. This alphabet is independent of the alphabet that is used for the whole derivation (which is defined in the main-file).
# that means, it could happen that symbols of SELFIES, that are not part of the main alphabet (for example, my_alphabet in selfies_main.py), are still used in order to represent numbers. If necessary, here there is slight potential to reduce the number of elements of the alphabet.
# [Ring1] means the next SELFIES symbol represents a number, [Ring2] means that the next two symbols represent numbers (in base 20, such that rings of size 400 are possible). [BranchN_M] stand for branches where the next N numbers give the branch size, and M is a property of the branch (i.e. which state the recursive function starts in, and which state the original derivation continues)
start_alphabet=['[epsilon]','[Ring1]','[Ring2]','[Branch1_1]','[Branch1_2]','[Branch1_3]','[Branch2_1]','[Branch2_2]','[Branch2_3]','[F]','[O]','[=O]','[N]','[=N]','[#N]','[C]','[=C]','[#C]','[S]','[=S]'];
all_smiles=smiles.split('.')
all_smiles_new=''
for current_smiles in all_smiles:
transitions=''
tmp_smiles=current_smiles+' '
while True:
current_symbol=tmp_smiles[0]
tmp_smiles=tmp_smiles[1:]
if current_symbol==' ':
break
if current_symbol=='[':
pos_of_close=tmp_smiles.find(']')
current_symbol=tmp_smiles[0:pos_of_close] # without the brackets
tmp_smiles=tmp_smiles[pos_of_close+1:]
transitions=transitions+'['+current_symbol+']'
elif current_symbol=='%': # this identifies a Ring
next_symbol=tmp_smiles[0:4]
tmp_smiles=tmp_smiles[4:]
current_num=int(next_symbol)
if current_num>=2 and current_num<=8000:
if current_num<=20:
ring_symbol=start_alphabet[current_num-2]
transitions=transitions+'[Ring1]'+ring_symbol
elif current_num<=400:
ring_num_1=int(math.floor(current_num/20.))
ring_num_2=current_num%20
ring_symbol1=start_alphabet[ring_num_1-1]
ring_symbol2=start_alphabet[ring_num_2]
transitions=transitions+'[Ring2]'+ring_symbol1+ring_symbol2
else:
ring_num_1=int(math.floor(current_num/400.))
current_num1=current_num-ring_num_1*400
ring_num_2=int(math.floor(current_num1/20.))
ring_num_3=current_num1-ring_num_2*20
ring_symbol1=start_alphabet[ring_num_1-1]
ring_symbol2=start_alphabet[ring_num_2]
ring_symbol3=start_alphabet[ring_num_3]
transitions=transitions+'[Ring3]'+ring_symbol1+ring_symbol2+ring_symbol3
else:
if current_num<2:
raise ValueError('_smiles_to_selfies: Malformed Ring.')
else:
raise ValueError('_smiles_to_selfies: Very large ring is not implemented.')
elif (current_symbol=='=' or current_symbol=='#' or current_symbol=='\\' or current_symbol=='/' or current_symbol=='-') and tmp_smiles[0]=='%':
# explicit bond number for ring, for example C1CCC=1C leads to '[C][C][C][C][Expl=Ring1][Ring1][C]', where [Expl=Ring1] shows that it involves an explicit double bond,
# and that the next symbol shows the size of the ring
pre_symbol=current_symbol
next_symbol=tmp_smiles[1:5]
tmp_smiles=tmp_smiles[5:]
current_num=int(next_symbol)
if current_num>=2 and current_num<=8000:
if current_num<=20:
ring_symbol=start_alphabet[current_num-2]
transitions=transitions+'[Expl'+pre_symbol+'Ring1]'+ring_symbol
elif current_num<=400:
ring_num_1=int(math.floor(current_num/20.))
ring_num_2=current_num%20
ring_symbol1=start_alphabet[ring_num_1-1]
ring_symbol2=start_alphabet[ring_num_2]
transitions=transitions+'[Expl'+pre_symbol+'Ring2]'+ring_symbol1+ring_symbol2
else:
ring_num_1=int(math.floor(current_num/400.))
current_num1=current_num-ring_num_1*400
ring_num_2=int(math.floor(current_num1/20.))
ring_num_3=current_num1-ring_num_2*20
ring_symbol1=start_alphabet[ring_num_1-1]
ring_symbol2=start_alphabet[ring_num_2]
ring_symbol3=start_alphabet[ring_num_3]
transitions=transitions+'[Expl'+pre_symbol+'Ring3]'+ring_symbol1+ring_symbol2+ring_symbol3
else:
if current_num<2:
raise ValueError('_smiles_to_selfies: Malformed Ring.')
else:
raise ValueError('_smiles_to_selfies: Very large ring is not implemented.')
elif current_symbol=='(': # branches are derived in a recursive way
open_vec=[0]*len(tmp_smiles)
close_vec=[0]*len(tmp_smiles)
tmp_smiles2=tmp_smiles
while True:
open_bracket=tmp_smiles2.find('(')
if open_bracket>=0:
tmp_smiles2=tmp_smiles2[0:open_bracket]+' '+tmp_smiles2[open_bracket+1:]
open_vec[open_bracket]=1
else:
break
while True:
close_bracket=tmp_smiles2.find(')')
if close_bracket>=0:
tmp_smiles2=tmp_smiles2[0:close_bracket]+' '+tmp_smiles2[close_bracket+1:]
close_vec[close_bracket]=1
else:
break
difference_of_list=[x - y for x, y in zip(open_vec, close_vec)]
layer_of_brackets=_cumsum(difference_of_list,1) # using cummulative sum to identify end of bracket
if 0 in layer_of_brackets:
sub_smiles=tmp_smiles[0:layer_of_brackets.index(0)]
tmp_smiles=tmp_smiles[layer_of_brackets.index(0)+1:]
else:
sub_smiles=tmp_smiles
tmp_smiles=[]
sub_ds=_smiles_to_selfies(sub_smiles) # recursive function call, to derive sub-string of SELFIES of branch
current_num=len(sub_ds)-len(sub_ds.replace('[',''))
if current_num<=20:
ring_symbol=start_alphabet[current_num-1]
if tmp_smiles[1]=='=' or tmp_smiles[1]=='#' or tmp_smiles[0]=='(':
if sub_smiles[1]=='=' or sub_smiles[1]=='#':
transitions=transitions+'[Branch1_1]'+ring_symbol+sub_ds
else:
transitions=transitions+'[Branch1_2]'+ring_symbol+sub_ds
else:
transitions=transitions+'[Branch1_3]'+ring_symbol+sub_ds
elif current_num<=400:
ring_num_1=int(math.floor(current_num/20.))
ring_num_2=current_num%20
ring_symbol1=start_alphabet[ring_num_1-1]
ring_symbol2=start_alphabet[ring_num_2]
if len(tmp_smiles)<=2:
transitions=transitions+'[Branch2_3]'+ring_symbol1+ring_symbol2+sub_ds
else:
if tmp_smiles[1]=='=' or tmp_smiles[1]=='#' or tmp_smiles[0]=='(':
if sub_smiles[1]=='=' or sub_smiles[1]=='#':
transitions=transitions+'[Branch2_1]'+ring_symbol1+ring_symbol2+sub_ds
else:
transitions=transitions+'[Branch2_2]'+ring_symbol1+ring_symbol2+sub_ds
else:
transitions=transitions+'[Branch2_3]'+ring_symbol1+ring_symbol2+sub_ds
elif current_num<=8000: # we can derive branches of up to 8000 SELFIES symbols. actually, PubChem involves many molecules with sizes beyond 400 selfies symbols.
ring_num_1=int(math.floor(current_num/400.))
current_num1=current_num-ring_num_1*400
ring_num_2=int(math.floor(current_num1/20.))
ring_num_3=current_num1-ring_num_2*20
ring_symbol1=start_alphabet[ring_num_1-1]
ring_symbol2=start_alphabet[ring_num_2]
ring_symbol3=start_alphabet[ring_num_3]
if len(tmp_smiles)<=2:
transitions=transitions+'[Branch3_3]'+ring_symbol1+ring_symbol2+ring_symbol3+sub_ds
else:
if tmp_smiles[1]=='=' or tmp_smiles[1]=='#' or tmp_smiles[0]=='(':
if sub_smiles[1]=='=' or sub_smiles[1]=='#':
transitions=transitions+'[Branch3_1]'+ring_symbol1+ring_symbol2+ring_symbol3+sub_ds
else:
transitions=transitions+'[Branch3_2]'+ring_symbol1+ring_symbol2+ring_symbol3+sub_ds
else:
transitions=transitions+'[Branch3_3]'+ring_symbol1+ring_symbol2+ring_symbol3+sub_ds
else:
raise ValueError('_smiles_to_selfies: Very large branch is not implemented (current_num='+str(current_num)+').')
else:
raise ValueError('_smiles_to_selfies: Unknown Symbol: '+str(current_symbol))
all_smiles_new=all_smiles_new+'.'+transitions
return all_smiles_new[1:]
def _get_next_selfies_symbol(tmp_ds): # get the next selfies symbol
next_symbol=''
tmp_ds_new=tmp_ds
if len(tmp_ds)<=2:
return [next_symbol, tmp_ds_new]
if tmp_ds[0]!='[':
raise ValueError('_get_next_selfies_symbol: Decoding Problem 1: '+tmp_ds)
end_of_symbol=tmp_ds.find(']')
if end_of_symbol==-1:
raise ValueError('_get_next_selfies_symbol: Decoding Problem 2: '+tmp_ds)
else:
next_symbol=tmp_ds[0:end_of_symbol+1]
tmp_ds_new=tmp_ds_new[end_of_symbol+1:]
return [next_symbol, tmp_ds_new]
def __selfies_to_smiles_derive(selfies,smiles,N_restrict=True):
# Elements of start_alphabet, again, stand for integers (see comments in _smiles_to_selfies function for more details)
start_alphabet=['[epsilon]','[Ring1]','[Ring2]','[Branch1_1]','[Branch1_2]','[Branch1_3]','[Branch2_1]','[Branch2_2]','[Branch2_3]','[F]','[O]','[=O]','[N]','[=N]','[#N]','[C]','[=C]','[#C]','[S]','[=S]'];
tmp_ds=selfies.replace('X','Z!') # X will be used as states of the derivation
next_X=smiles.find('X');
while next_X>=0:
before_smiles=smiles[0:next_X] # smiles before the non-terminal
if smiles[next_X+1]=='9':
state=int(smiles[next_X+1:next_X+5]) # states after branches are called X999...
after_smiles=smiles[next_X+6:] # smiles after the non-terminal
else:
state=int(smiles[next_X+1]) # the state is given by the nonterminal symbol X_n, where n=state
after_smiles=smiles[next_X+2:] # smiles after the non-terminal
[current_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds) # the current selfies symbol gives the rule vector, and the current state indentifies the one specific, current rule.
# The semantic informations of this set of rules could be significantly extended and more details could be added. Here, we have semantic rules for the most important molecules in organic chemistry, Carbon, Oxygen, Nitrogen, Flour.
# Other elements get a generic (very weak) restrictions
if state==0:
if current_symbol=='[epsilon]':
new_smiles_symbol='X0'
elif current_symbol.find('Ring')>=0 or current_symbol.find('Branch')>=0:
new_smiles_symbol='X0'
[_,tmp_ds]=_get_next_selfies_symbol(tmp_ds) # ignore next symbol
elif current_symbol=='[F]' or current_symbol=='[H]':
new_smiles_symbol=current_symbol+'X1'
elif current_symbol=='[Cl]':
new_smiles_symbol='[Cl]X1'
elif current_symbol=='[Br]':
new_smiles_symbol='[Br]X1'
elif current_symbol=='[O]' or current_symbol=='[NHexpl]':
new_smiles_symbol=current_symbol+'X2'
elif current_symbol=='[=O]':
new_smiles_symbol='[O]X2'
elif current_symbol=='[N]':
if N_restrict:
new_smiles_symbol='[N]X3'
else:
new_smiles_symbol='[N]X6'
elif current_symbol=='[=N]':
if N_restrict:
new_smiles_symbol='[N]X3'
else:
new_smiles_symbol='[N]X6'
elif current_symbol=='[#N]':
if N_restrict:
new_smiles_symbol='[N]X3'
else:
new_smiles_symbol='[N]X6'
elif current_symbol=='[C]':
new_smiles_symbol='[C]X4'
elif current_symbol=='[=C]':
new_smiles_symbol='[C]X4'
elif current_symbol=='[#C]':
new_smiles_symbol='[C]X4'
elif current_symbol=='[C@expl]' or current_symbol=='[C@@expl]':
new_smiles_symbol=current_symbol+'X4'
elif current_symbol=='[C@Hexpl]' or current_symbol=='[C@@Hexpl]':
new_smiles_symbol=current_symbol+'X3'
elif current_symbol=='[S]':
new_smiles_symbol='[S]X6'
elif current_symbol=='[=S]':
new_smiles_symbol='[S]X6'
else:
new_smiles_symbol=current_symbol+'X6'
smiles=before_smiles+new_smiles_symbol+after_smiles
if state==1:
if current_symbol=='[epsilon]':
new_smiles_symbol=''
elif current_symbol.find('Branch')>=0:
new_smiles_symbol='X1'
[_,tmp_ds]=_get_next_selfies_symbol(tmp_ds) # ignore next symbol
elif current_symbol.find('Ring1]')>=0:
pre_symbol=''
if current_symbol[1:5]=='Expl': # Explicit Bond Information
pre_symbol=current_symbol[5]
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if next_symbol in start_alphabet:
ring_num=str(start_alphabet.index(next_symbol)+2)
else:
ring_num='5'
if len(ring_num)==1:
new_smiles_symbol=pre_symbol+'%000'+ring_num
elif len(ring_num)==2:
new_smiles_symbol=pre_symbol+'%00'+ring_num
elif len(ring_num)==3:
new_smiles_symbol=pre_symbol+'%0'+ring_num
elif len(ring_num)==4:
new_smiles_symbol=pre_symbol+'%'+ring_num
else:
raise ValueError('__selfies_to_smiles_derive: Problem with deriving very long ring.')
elif current_symbol.find('Ring2]')>=0:
pre_symbol=''
if current_symbol[1:5]=='Expl': # Explicit Bond Information
pre_symbol=current_symbol[5]
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet):
ring_num_1=(start_alphabet.index(next_symbol1)+1)*20
ring_num_2=start_alphabet.index(next_symbol2)
ring_num=str(ring_num_1+ring_num_2)
else:
ring_num='5'
if len(ring_num)==1:
new_smiles_symbol=pre_symbol+'%000'+ring_num
elif len(ring_num)==2:
new_smiles_symbol=pre_symbol+'%00'+ring_num
elif len(ring_num)==3:
new_smiles_symbol=pre_symbol+'%0'+ring_num
elif len(ring_num)==4:
new_smiles_symbol=pre_symbol+'%'+ring_num
else:
raise ValueError('__selfies_to_smiles_derive: Problem with deriving very long ring.')
elif current_symbol.find('Ring3]')>=0:
pre_symbol=''
if current_symbol[1:5]=='Expl': # Explicit Bond Information
pre_symbol=current_symbol[5]
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol3,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet) and (next_symbol3 in start_alphabet):
ring_num_1=(start_alphabet.index(next_symbol1)+1)*400
ring_num_2=(start_alphabet.index(next_symbol2))*20
ring_num_3=start_alphabet.index(next_symbol3)
ring_num=str(ring_num_1+ring_num_2+ring_num_3)
else:
ring_num='5'
if len(ring_num)==1:
new_smiles_symbol=pre_symbol+'%000'+ring_num
elif len(ring_num)==2:
new_smiles_symbol=pre_symbol+'%00'+ring_num
elif len(ring_num)==3:
new_smiles_symbol=pre_symbol+'%0'+ring_num
elif len(ring_num)==4:
new_smiles_symbol=pre_symbol+'%'+ring_num
else:
raise ValueError('__selfies_to_smiles_derive: Problem with deriving very long ring.')
elif current_symbol=='[F]' or current_symbol=='[H]':
new_smiles_symbol=current_symbol
elif current_symbol=='[Cl]':
new_smiles_symbol='[Cl]'
elif current_symbol=='[Br]':
new_smiles_symbol='[Br]'
elif current_symbol=='[O]' or current_symbol=='[NHexpl]':
new_smiles_symbol=current_symbol+'X1'
elif current_symbol=='[=O]':
new_smiles_symbol='[O]'
elif current_symbol=='[N]':
if N_restrict:
new_smiles_symbol='[N]X2'
else:
new_smiles_symbol='[N]X6'
elif current_symbol=='[=N]':
if N_restrict:
new_smiles_symbol='[N]X2'
else:
new_smiles_symbol='[N]X6'
elif current_symbol=='[#N]':
if N_restrict:
new_smiles_symbol='[N]X2'
else:
new_smiles_symbol='[N]X6'
elif current_symbol=='[C]':
new_smiles_symbol='[C]X3'
elif current_symbol=='[=C]':
new_smiles_symbol='[C]X3'
elif current_symbol=='[#C]':
new_smiles_symbol='[C]X3'
elif current_symbol=='[C@expl]' or current_symbol=='[C@@expl]':
new_smiles_symbol=current_symbol+'X3'
elif current_symbol=='[C@Hexpl]' or current_symbol=='[C@@Hexpl]':
new_smiles_symbol=current_symbol+'X2'
elif current_symbol=='[S]':
new_smiles_symbol='[S]X5'
elif current_symbol=='[=S]':
new_smiles_symbol='[S]X5'
else:
new_smiles_symbol=current_symbol+'X6'
smiles=before_smiles+new_smiles_symbol+after_smiles
if state==2:
if current_symbol=='[epsilon]':
new_smiles_symbol=''
elif current_symbol.find('Ring1]')>=0:
pre_symbol=''
if current_symbol[1:5]=='Expl': # Explicit Bond Information
pre_symbol=current_symbol[5]
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if next_symbol in start_alphabet:
ring_num=str(start_alphabet.index(next_symbol)+2)
else:
ring_num='5'
if len(ring_num)==1:
new_smiles_symbol=pre_symbol+'%000'+ring_num+'X1'
elif len(ring_num)==2:
new_smiles_symbol=pre_symbol+'%00'+ring_num+'X1'
elif len(ring_num)==3:
new_smiles_symbol=pre_symbol+'%0'+ring_num+'X1'
elif len(ring_num)==4:
new_smiles_symbol=pre_symbol+'%'+ring_num+'X1'
else:
raise ValueError('__selfies_to_smiles_derive: Problem with deriving very long ring.')
elif current_symbol.find('Ring2]')>=0:
pre_symbol=''
if current_symbol[1:5]=='Expl': # Explicit Bond Information
pre_symbol=current_symbol[5]
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet):
ring_num_1=(start_alphabet.index(next_symbol1)+1)*20
ring_num_2=start_alphabet.index(next_symbol2)
ring_num=str(ring_num_1+ring_num_2)
else:
ring_num='5'
if len(ring_num)==1:
new_smiles_symbol=pre_symbol+'%000'+ring_num+'X1'
elif len(ring_num)==2:
new_smiles_symbol=pre_symbol+'%00'+ring_num+'X1'
elif len(ring_num)==3:
new_smiles_symbol=pre_symbol+'%0'+ring_num+'X1'
elif len(ring_num)==4:
new_smiles_symbol=pre_symbol+'%'+ring_num+'X1'
else:
raise ValueError('__selfies_to_smiles_derive: Problem with deriving very long ring.')
elif current_symbol.find('Ring3]')>=0:
pre_symbol=''
if current_symbol[1:5]=='Expl': # Explicit Bond Information
pre_symbol=current_symbol[5]
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol3,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet) and (next_symbol3 in start_alphabet):
ring_num_1=(start_alphabet.index(next_symbol1)+1)*400
ring_num_2=(start_alphabet.index(next_symbol2))*20
ring_num_3=start_alphabet.index(next_symbol3)
ring_num=str(ring_num_1+ring_num_2+ring_num_3)
else:
ring_num='5'
if len(ring_num)==1:
new_smiles_symbol=pre_symbol+'%000'+ring_num+'X1'
elif len(ring_num)==2:
new_smiles_symbol=pre_symbol+'%00'+ring_num+'X1'
elif len(ring_num)==3:
new_smiles_symbol=pre_symbol+'%0'+ring_num+'X1'
elif len(ring_num)==4:
new_smiles_symbol=pre_symbol+'%'+ring_num+'X1'
else:
raise ValueError('__selfies_to_smiles_derive: Problem with deriving very long ring.')
elif current_symbol=='[Branch1_1]' or current_symbol=='[Branch1_2]' or current_symbol=='[Branch1_3]':
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if next_symbol in start_alphabet:
branch_num=start_alphabet.index(next_symbol)+1
else:
branch_num=1
branch_str=''
for bii in range(branch_num):
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
branch_str+=next_symbol
branch_smiles=__selfies_to_smiles_derive(branch_str,'X9991',N_restrict)
new_smiles_symbol=''
if len(branch_smiles)>0:
new_smiles_symbol='('+branch_smiles+')X1'
elif current_symbol=='[Branch2_1]' or current_symbol=='[Branch2_2]' or current_symbol=='[Branch2_3]':
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet):
branch_num1=(start_alphabet.index(next_symbol1)+1)*20
branch_num2=start_alphabet.index(next_symbol2)
branch_num=branch_num1+branch_num2
else:
branch_num=1
branch_str=''
for bii in range(branch_num):
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
branch_str+=next_symbol
branch_smiles=__selfies_to_smiles_derive(branch_str,'X9991',N_restrict)
new_smiles_symbol=''
if len(branch_smiles)>0:
new_smiles_symbol='('+branch_smiles+')X1'
elif current_symbol=='[Branch3_1]' or current_symbol=='[Branch3_2]' or current_symbol=='[Branch3_3]':
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol3,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet) and (next_symbol3 in start_alphabet):
branch_num1=(start_alphabet.index(next_symbol1)+1)*400
branch_num2=(start_alphabet.index(next_symbol2))*20
branch_num3=start_alphabet.index(next_symbol3)
branch_num=branch_num1+branch_num2+branch_num3
else:
branch_num=1
branch_str=''
for bii in range(branch_num):
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
branch_str+=next_symbol
branch_smiles=__selfies_to_smiles_derive(branch_str,'X9991',N_restrict)
new_smiles_symbol=''
if len(branch_smiles)>0:
new_smiles_symbol='('+branch_smiles+')X1'
elif current_symbol=='[F]' or current_symbol=='[H]':
new_smiles_symbol=current_symbol
elif current_symbol=='[Cl]':
new_smiles_symbol='[Cl]'
elif current_symbol=='[Br]':
new_smiles_symbol='[Br]'
elif current_symbol=='[O]' or current_symbol=='[NHexpl]':
new_smiles_symbol=current_symbol+'X1'
elif current_symbol=='[=O]':
new_smiles_symbol='[=O]'
elif current_symbol=='[N]':
if N_restrict:
new_smiles_symbol='[N]X2'
else:
new_smiles_symbol='[N]X6'
elif current_symbol=='[=N]':
if N_restrict:
new_smiles_symbol='[=N]X1'
else:
new_smiles_symbol='[=N]X6'
elif current_symbol=='[#N]':
if N_restrict:
new_smiles_symbol='[=N]X1'
else:
new_smiles_symbol='[=N]X6'
elif current_symbol=='[C]':
new_smiles_symbol='[C]X3'
elif current_symbol=='[=C]':
new_smiles_symbol='[=C]X2'
elif current_symbol=='[#C]':
new_smiles_symbol='[=C]X2'
elif current_symbol=='[C@expl]' or current_symbol=='[C@@expl]':
new_smiles_symbol=current_symbol+'X3'
elif current_symbol=='[C@Hexpl]' or current_symbol=='[C@@Hexpl]':
new_smiles_symbol=current_symbol+'X2'
elif current_symbol=='[S]':
new_smiles_symbol='[S]X5'
elif current_symbol=='[=S]':
new_smiles_symbol='[=S]X4'
else:
new_smiles_symbol=current_symbol+'X6'
smiles=before_smiles+new_smiles_symbol+after_smiles
if state==3:
if current_symbol=='[epsilon]':
new_smiles_symbol=''
elif current_symbol.find('Ring1]')>=0:
pre_symbol=''
if current_symbol[1:5]=='Expl': # Explicit Bond Information
pre_symbol=current_symbol[5]
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if next_symbol in start_alphabet:
ring_num=str(start_alphabet.index(next_symbol)+2)
else:
ring_num='5'
if len(ring_num)==1:
new_smiles_symbol=pre_symbol+'%000'+ring_num+'X2'
elif len(ring_num)==2:
new_smiles_symbol=pre_symbol+'%00'+ring_num+'X2'
elif len(ring_num)==3:
new_smiles_symbol=pre_symbol+'%0'+ring_num+'X2'
elif len(ring_num)==4:
new_smiles_symbol=pre_symbol+'%'+ring_num+'X2'
else:
raise ValueError('__selfies_to_smiles_derive: Problem with deriving very long ring.')
elif current_symbol.find('Ring2]')>=0:
pre_symbol=''
if current_symbol[1:5]=='Expl': # Explicit Bond Information
pre_symbol=current_symbol[5]
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet):
ring_num_1=(start_alphabet.index(next_symbol1)+1)*20
ring_num_2=start_alphabet.index(next_symbol2)
ring_num=str(ring_num_1+ring_num_2)
else:
ring_num='5'
if len(ring_num)==1:
new_smiles_symbol=pre_symbol+'%000'+ring_num+'X2'
elif len(ring_num)==2:
new_smiles_symbol=pre_symbol+'%00'+ring_num+'X2'
elif len(ring_num)==3:
new_smiles_symbol=pre_symbol+'%0'+ring_num+'X2'
elif len(ring_num)==4:
new_smiles_symbol=pre_symbol+'%'+ring_num+'X2'
else:
raise ValueError('__selfies_to_smiles_derive: Problem with deriving very long ring.')
elif current_symbol.find('Ring3]')>=0:
pre_symbol=''
if current_symbol[1:5]=='Expl': # Explicit Bond Information
pre_symbol=current_symbol[5]
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol3,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet) and (next_symbol3 in start_alphabet):
ring_num_1=(start_alphabet.index(next_symbol1)+1)*400
ring_num_2=(start_alphabet.index(next_symbol2))*20
ring_num_3=start_alphabet.index(next_symbol3)
ring_num=str(ring_num_1+ring_num_2+ring_num_3)
else:
ring_num='5'
if len(ring_num)==1:
new_smiles_symbol=pre_symbol+'%000'+ring_num+'X2'
elif len(ring_num)==2:
new_smiles_symbol=pre_symbol+'%00'+ring_num+'X2'
elif len(ring_num)==3:
new_smiles_symbol=pre_symbol+'%0'+ring_num+'X2'
elif len(ring_num)==4:
new_smiles_symbol=pre_symbol+'%'+ring_num+'X2'
else:
raise ValueError('__selfies_to_smiles_derive: Problem with deriving very long ring.')
elif current_symbol=='[Branch1_1]' or current_symbol=='[Branch1_2]':
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if next_symbol in start_alphabet:
branch_num=start_alphabet.index(next_symbol)+1
else:
branch_num=1
branch_str=''
for bii in range(branch_num):
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
branch_str+=next_symbol
branch_smiles=__selfies_to_smiles_derive(branch_str,'X9991',N_restrict)
new_smiles_symbol=''
if len(branch_smiles)>0:
new_smiles_symbol='('+branch_smiles+')X2'
elif current_symbol=='[Branch1_3]':
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if next_symbol in start_alphabet:
branch_num=start_alphabet.index(next_symbol)+1
else:
branch_num=1
branch_str=''
for bii in range(branch_num):
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
branch_str+=next_symbol
branch_smiles=__selfies_to_smiles_derive(branch_str,'X9992',N_restrict)
new_smiles_symbol=''
if len(branch_smiles)>0:
new_smiles_symbol='('+branch_smiles+')X1'
elif current_symbol=='[Branch2_1]' or current_symbol=='[Branch2_2]':
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet):
branch_num1=(start_alphabet.index(next_symbol1)+1)*20
branch_num2=start_alphabet.index(next_symbol2)
branch_num=branch_num1+branch_num2
else:
branch_num=1
branch_str=''
for bii in range(branch_num):
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
branch_str+=next_symbol
branch_smiles=__selfies_to_smiles_derive(branch_str,'X9991',N_restrict)
new_smiles_symbol=''
if len(branch_smiles)>0:
new_smiles_symbol='('+branch_smiles+')X2'
elif current_symbol=='[Branch2_3]':
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet):
branch_num1=(start_alphabet.index(next_symbol1)+1)*20
branch_num2=start_alphabet.index(next_symbol2)
branch_num=branch_num1+branch_num2
else:
branch_num=1
branch_str=''
for bii in range(branch_num):
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
branch_str+=next_symbol
branch_smiles=__selfies_to_smiles_derive(branch_str,'X9992',N_restrict)
new_smiles_symbol=''
if len(branch_smiles)>0:
new_smiles_symbol='('+branch_smiles+')X1'
elif current_symbol=='[Branch3_1]' or current_symbol=='[Branch3_2]':
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol3,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet) and (next_symbol3 in start_alphabet):
branch_num1=(start_alphabet.index(next_symbol1)+1)*400
branch_num2=(start_alphabet.index(next_symbol2))*20
branch_num3=start_alphabet.index(next_symbol3)
branch_num=branch_num1+branch_num2+branch_num3
else:
branch_num=1
branch_str=''
for bii in range(branch_num):
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
branch_str+=next_symbol
branch_smiles=__selfies_to_smiles_derive(branch_str,'X9991',N_restrict)
new_smiles_symbol=''
if len(branch_smiles)>0:
new_smiles_symbol='('+branch_smiles+')X2'
elif current_symbol=='[Branch3_3]':
[next_symbol1,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol2,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
[next_symbol3,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
if (next_symbol1 in start_alphabet) and (next_symbol2 in start_alphabet) and (next_symbol3 in start_alphabet):
branch_num1=(start_alphabet.index(next_symbol1)+1)*400
branch_num2=(start_alphabet.index(next_symbol2))*20
branch_num3=start_alphabet.index(next_symbol3)
branch_num=branch_num1+branch_num2+branch_num3
else:
branch_num=1
branch_str=''
for bii in range(branch_num):
[next_symbol,tmp_ds]=_get_next_selfies_symbol(tmp_ds)
branch_str+=next_symbol
branch_smiles=__selfies_to_smiles_derive(branch_str,'X9992',N_restrict)
new_smiles_symbol=''
if len(branch_smiles)>0:
new_smiles_symbol='('+branch_smiles+')X1'
elif current_symbol=='[F]' or current_symbol=='[H]':
new_smiles_symbol=current_symbol
elif current_symbol=='[Cl]':
new_smiles_symbol='[Cl]'
elif current_symbol=='[Br]':
new_smiles_symbol='[Br]'
elif current_symbol=='[O]' or current_symbol=='[NHexpl]':
new_smiles_symbol=current_symbol+'X1'
elif current_symbol=='[=O]':
new_smiles_symbol='[=O]'
elif current_symbol=='[N]':
if N_restrict:
new_smiles_symbol='[N]X2'
else:
new_smiles_symbol='[N]X6'
elif current_symbol=='[=N]':
if N_restrict:
new_smiles_symbol='[=N]X1'
else:
new_smiles_symbol='[=N]X6'
elif current_symbol=='[#N]':
if N_restrict:
new_smiles_symbol='[#N]'
else:
new_smiles_symbol='[#N]X6'
elif current_symbol=='[C]':
new_smiles_symbol='[C]X3'
elif current_symbol=='[=C]':
new_smiles_symbol='[=C]X2'
elif current_symbol=='[#C]':
new_smiles_symbol='[#C]X1'
elif current_symbol=='[C@expl]' or current_symbol=='[C@@expl]':
new_smiles_symbol=current_symbol+'X3'
elif current_symbol=='[C@Hexpl]' or current_symbol=='[C@@Hexpl]':