Skip to content

LigX Functions

LigXRun

Core class in LigX


Key Parameters

linknum (int):
Maximum number of fragment linkings. (default: 5)
outnum (int):
Number of structures to generate in this run. (default: 2000)
ex_frag (str):
Relative path to the external fragment set specified by the user.
(example: 'external_fragment/fragment')
ex_rollfrag (str): Relative path to the external rollout fragment set specified by the user.
(example: 'external_fragment/rollout_fragment')


Key Attributes

smiles_filter (bool):
Set to False when explicitly turning off the SMILES filter. LigX program automatically sets the value to False in environments where RDkit is unavailable.(default: True)
result_dir (str):
Directory name where output is saved. (default: 'result')
ligand_xyz (str):
Directory name where the ligand (initiator) structure used for input is stored. (default: 'ligand_xyz')
dir_protein_xyz (str):
Directory name where the protein structure used for input is stored. (default: 'protein_xyz')
cutoff_score_init (float):
Initial cutoff value for ligand selection. (default: 15)
cutoff_score_fin (float):
The maximum cutoff value that increases with the number of generated structures. Increases linearly from cutoff_score_init. (default: 15)
sigma_sum_cut (float):
Collision detection value for stetic clash between fragments and initiators. Reducing this value increases structural flexibility, but also makes it easier to generate unreasonable structures. (default: 10)
rollout_num (int):
The number of fragments for which rollout is performed after evaluating the interaction of fragments. (default: 5)
clean (bool):
For MacOS only. LigX detects the operating system. If running on macOS, clean will set to True and the program will execute clean.sh to remove automatically generated files such as .DS_Store and AppleDouble.
Set this option to False if you do not allow the execution of clean.sh.
There is no need to configure this setting on Windows or Linux systems. (default: False)


Source code in LigX/LigX_core.py
  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
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
class LigXRun:
    """Core class in LigX<br>

    ------------------
    ## Key Parameters
    **linknum** (*int*):<br>
        Maximum number of fragment linkings. (default: 5)<br>
    **outnum** (*int*):<br>
        Number of structures to generate in this run. (default: 2000)<br>
    **ex_frag** (*str*):<br>
        Relative path to the external fragment set specified by the user.
        <br>(example: 'external_fragment/fragment')<br>
    **ex_rollfrag** (*str*):
        Relative path to the external rollout fragment set specified by the user.
        <br>(example: 'external_fragment/rollout_fragment')<br>
    ------------------

    ## Key Attributes

    **smiles_filter** (*bool*): <br>
    Set to `False` when explicitly turning off the SMILES filter.
    LigX program automatically sets the value to `False`
    in environments where RDkit is unavailable.(default: `True`)<br>
    **result_dir** (*str*):<br>
    Directory name where output is saved. (default: 'result')<br>
    **ligand_xyz** (*str*):<br>
    Directory name where the ligand (initiator) structure used for
    input is stored. (default: 'ligand_xyz')<br>
    **dir_protein_xyz** (*str*):<br>
    Directory name where the protein structure used for input
    is stored. (default: 'protein_xyz')<br>
    **cutoff_score_init** (*float*):<br>
    Initial cutoff value for ligand selection. (default: 15)<br>
    **cutoff_score_fin** (*float*):<br>
    The maximum cutoff value that increases with the number of
    generated structures. Increases linearly from cutoff_score_init.
    (default: 15)<br>
    **sigma_sum_cut** (*float*):<br>
    Collision detection value for stetic clash
    between fragments and initiators. Reducing this value increases
    structural flexibility, but also makes it easier to generate
    unreasonable structures. (default: 10)<br>
    **rollout_num** (*int*):<br>
    The number of fragments for which rollout is performed after evaluating the interaction of fragments. (default: 5) <br>
    **clean** (*bool*):<br>
    For MacOS only. LigX detects the operating system. If running on macOS, clean will set to `True` and the program will execute clean.sh to remove automatically generated files such as .DS_Store and AppleDouble. <br>
    Set this option to `False` if you do not allow the execution of clean.sh. <br>There is no need to configure this setting on Windows or Linux systems. (default: `False`) <br>

    ------------------
    """
    def __init__(self, linknum = None, outnum = None, ex_frag = None, ex_rollfrag = None, logfile_root = None, result_dir_root = None, paramlog_root = None, ligand_xyz_root = None, dir_protein_xyz_root = None, desired_frag_root = None, desired_frag_rollout_root = None):

        if logfile_root is None:
            self.logfile = 'logfile.log'
        else:
            self.logfile = logfile_root
        if paramlog_root is None:
            self.paramlog = 'Param.log'
        else:
            self.paramlog = paramlog_root
        if result_dir_root is None:
            self.result_dir = 'result'
        else:
            self.result_dir = result_dir_root
        if ligand_xyz_root is None:
            self.ligand_xyz = 'ligand_xyz'
        else:
            self.ligand_xyz = ligand_xyz_root
        if dir_protein_xyz_root is None:
            self.dir_protein_xyz = 'protein_xyz'
        else:
            self.dir_protein_xyz = dir_protein_xyz_root
        if ex_frag is None:
            self.fragment_set = None
        else:
            self.fragment_set = ex_frag
        if ex_rollfrag is None:
            self.rollout_set = None
        else:
            self.rollout_set = ex_rollfrag
        if desired_frag_root is None:
            self.desired_frag = None
        else:
            self.desired_frag = desired_frag_root
        if desired_frag_rollout_root is None:
            self.desired_frag_rollout = None
        else:
            self.desired_frag_rollout = desired_frag_rollout_root

        if linknum is None:
            self.cycle = 5
        else:
            self.cycle = linknum

        if outnum is None:
            self.max_conf = 2000
        else:
            self.max_conf = outnum

        self.des_frag_cycle = -1
        self.max_branch_core = 0
        self.max_branch_frag = 0
        self.result_conf = self.max_conf
        self.evaluation_param = 'LE_interact_E'
        self.evaluation_method = 'highest'
        self.cutoff_score_init = 15
        self.cutoff_score_fin = 15
        self.rollout_num = 5
        self.rollout_frag_max = 3
        self.image_dir_name = 'image_ligx'
        self.count_for_data_processing = -1
        self.rollout_conf_inner = 1
        self.rollout_conf_inner_des = 20
        self.sigma_sum_cut = 10
        self.clean = False
        if platform.system() == 'Darwin':
            self.clean = True
        else:
            self.clean = False



    def exec_ligx(self):

        try:
            from rdkit import Chem
            from rdkit.Chem import rdDetermineBonds
        except Exception as e:
            self.smiles_filter = False
            red_print("SMILES filter disabled (RDKit import failed): " +str(e))
        else:
            self.smiles_filter = True
            from rdkit.Chem import AllChem
            from rdkit.Chem import Draw
            from rdkit import RDLogger
            RDLogger.DisableLog('rdApp.*')
            red_print("SMILES filter enabled")

        logfile = os.path.join(os.getcwd(),self.logfile)
        paramlog = os.path.join(os.getcwd(),self.paramlog)
        result_dir = os.path.join(os.getcwd(),self.result_dir)

        dir_parent = os.path.join(os.getcwd(),self.ligand_xyz)
        dir_protein_xyz = os.path.join(os.getcwd(),self.dir_protein_xyz)

        if self.fragment_set == None:
            fragment_set = str(resources.files(__package__ + ".fragment"))
        else:
            fragment_set = os.path.join(os.getcwd(),self.fragment_set)

        if self.rollout_set == None:
            rollout_set = str(resources.files(__package__ + ".rollout_fragment"))
        else:
            rollout_set = os.path.join(os.getcwd(),self.rollout_set)

        if self.desired_frag == None:
            desired_frag_root = str(resources.files(__package__ + ""))
        else:
            desired_frag_root = os.path.join(os.getcwd(),self.desired_frag)

        if self.desired_frag_rollout == None:
            desired_frag_rollout_root = str(resources.files(__package__ + ""))
        else:
            desired_frag_rollout_root = os.path.join(os.getcwd(),self.desired_frag_rollout)

        cycle = self.cycle
        #desired_fragを入れるcycle(0にしてはだめ,-1だとdesired_fragmentは入らない)
        des_frag_cycle = self.des_frag_cycle

        if des_frag_cycle >= 1:
            desired_frag = [desired_frag_root]
            desired_frag_rollout = [desired_frag_rollout_root]
        else:
            desired_frag = ['']
            desired_frag_rollout = ['']

        #clean_path = os.path.join(os.getcwd(), "clean.sh")
        clean_path = os.path.join(resources.files(__package__),'clean.sh')

        if self.clean == True:
            subprocess.call([clean_path])
        max_branch_core = self.max_branch_core

        max_branch_frag = self.max_branch_frag
        result_conf = self.result_conf

        max_conf = self.max_conf

        cutoff_score_init = self.cutoff_score_init
        cutoff_score_fin = self.cutoff_score_fin

        rollout_num = self.rollout_num

        rollout_frag_max = self.rollout_frag_max

        evaluation_param = self.evaluation_param

        evaluation_method = self.evaluation_method

        count_for_data_processing = self.count_for_data_processing

        image_dir_name = self.image_dir_name
        rollout_conf_inner = self.rollout_conf_inner
        rollout_conf_inner_des = self.rollout_conf_inner_des

        sigma_sum_cut = self.sigma_sum_cut

        if os.path.isdir(result_dir):
            shutil.rmtree(result_dir)

        os.mkdir(result_dir)
        image_dir = result_dir +'/' +image_dir_name
        os.mkdir(image_dir)


        if(os.path.isfile(logfile)):
            os.remove(logfile)

        if(os.path.isfile(paramlog)):
            os.remove(paramlog)
        logger=my_logfile(logfile,'INFO','a')
        logger_notime=my_logfile_notime(logfile,'INFO_notime','a')
        logger_state=my_logfile(paramlog,'Param','a')
        logger_state_notime=my_logfile_notime(paramlog,'Param_notime','a')


        logger.info('Execute LigX program')
        logger_notime.info('\nLigX: Ligand Extending Program for Medicinal Chemistry. (2025)')
        logger_notime.info('\nVersion: ' + str(__version__))

        logger_state.info('Execute LigX program')
        logger_state_notime.info('\nLigX: Ligand Extending Program for Medicinal Chemistry. (2025)')
        logger_state_notime.info('\nVersion: ' + str(__version__))
        logger_state_notime.info('\nDetailed parameters will be written in this file.')

        logger_state_notime.info('\n################################LigXLigX###############################')

        desired_list = []

        if desired_frag != ['']:
            for des in desired_frag:
                if self.clean == True:
                    subprocess.call([clean_path])
                despath_list = os.listdir(des)
                for i in despath_list:
                    if i != 'charge.txt' and i != 'param.txt':
                        desired_list.append(des+'/'+i)

                    des_category = extract_des_category(des)

        if desired_frag == ['']:
            des_category = None

        desired_list_rollout = []
        if desired_frag_rollout != ['']:
            for des in desired_frag_rollout:
                if self.clean == True:
                    subprocess.call([clean_path])
                despath_list_rollout = os.listdir(des)
                for i in despath_list_rollout:
                    if i != 'charge.txt' and i != 'param.txt':
                        desired_list_rollout.append(des+'/'+i)
                    rol_des_category = extract_des_category(des)

        if desired_frag_rollout == ['']:
            rol_des_category = None

        if self.clean == True:
            subprocess.call([clean_path])
        core_dir_list_root = os.listdir(dir_parent)

        core_dir_list = [os.path.join(dir_parent,x) for x in core_dir_list_root]

        protein_xyz_dir_list = os.listdir(dir_protein_xyz)
        protein_xyz_dir_list.sort()

        #Load protein structue
        protein_index = 0
        all_protein_cord = []
        for protein_xyz_dir in protein_xyz_dir_list:
            var_name = 'protein_cord_'+str(protein_index)
            protein_path = os.path.join(dir_protein_xyz,protein_xyz_dir)
            protein_cord,empty_name = read_xyz_data(protein_path)
            exec("{} = protein_cord".format(var_name))
            all_protein_cord.append(protein_cord)
            protein_index +=1

        num_protein_structures = len(all_protein_cord)

        count_generated = 0
        try_num = 0

        all_result = []

        all_score_strategy_0 = ['ID']
        time_init = 0

        for i in range(num_protein_structures):
            x = 'LJ['+str(i)+']'
            all_score_strategy_0.append(x)
        all_score_strategy_0.append('LJ_average')

        for i in range(num_protein_structures):
            x = 'coulomb['+str(i)+']'
            all_score_strategy_0.append(x)
        all_score_strategy_0.append('coulomb_average')

        for i in range(num_protein_structures):
            x = 'interact_E['+str(i)+']'
            all_score_strategy_0.append(x)
        all_score_strategy_0.append('interact_E_average')

        for i in range(num_protein_structures):
            x = 'LE_LJ['+str(i)+']'
            all_score_strategy_0.append(x)
        all_score_strategy_0.append('LE_LJ_average')

        for i in range(num_protein_structures):
            x = 'LE_interact_E['+str(i)+']'
            all_score_strategy_0.append(x)
        all_score_strategy_0.append('LE_interact_E_average')

        for i in range(num_protein_structures):
            x = 'LE_LJ+coulomb['+str(i)+']'
            all_score_strategy_0.append(x)
        all_score_strategy_0.append('LE_LJ+coulomb_average')
        all_score_strategy_0.append('strategy')
        all_score_strategy_0.append('SMILES')
        all_score_strategy =[all_score_strategy_0]

        transform_dict = get_transform_dict_hyd(fragment_set)

        setting_info = sum_set_info(cycle,des_frag_cycle,max_branch_core,max_branch_frag,result_conf,max_conf,cutoff_score_init,cutoff_score_fin,rollout_num,rollout_frag_max,fragment_set,rollout_set,desired_frag,desired_frag_rollout,evaluation_param,evaluation_method,core_dir_list,protein_xyz_dir_list)

        logger_notime.info('\n################################ Setting Info ###############################\n')
        logger_notime.info(setting_info)
        logger_notime.info('############################# Setting Info Ending ###########################\n')


        blue_print('Loading fragments…')
        logger_notime.info('Loading fragments…')
        logger_notime.info('\n################################LigXLigX###############################')

        #Initialization of Fragments

        Frag_F1L = Fragments('F1L',transform_dict)
        Frag_F2L = Fragments('F2L',transform_dict)
        Frag_C1L = Fragments('C1L',transform_dict)
        Frag_C2L = Fragments('C2L',transform_dict)
        Frag_dict = {'F1L': Frag_F1L, 'F2L': Frag_F2L,'C1L': Frag_C1L, 'C2L': Frag_C2L}

        if 'F3L' in os.listdir(fragment_set):
            Frag_F3L = Fragments('F3L',transform_dict)
            Frag_dict['F3L'] = Frag_F3L
        if 'F2LB' in os.listdir(fragment_set):
            Frag_F2LB = Fragments('F2LB',transform_dict)
            Frag_dict['F2LB'] = Frag_F2LB
        if 'C3L' in os.listdir(fragment_set):
            Frag_C3L = Fragments('C3L',transform_dict)
            Frag_dict['C3L'] = Frag_C3L
        if 'XHD' in os.listdir(fragment_set):
            Frag_XHD = Fragments('XHD',transform_dict)
            Frag_dict['XHD'] = Frag_XHD
        if 'XFO' in os.listdir(fragment_set):
            Frag_XFO = Fragments('XFO',transform_dict)
            Frag_dict['XFO'] = Frag_XFO

        Frag_desired = desFragments(desired_list,des_category)
        #Frag_dict = {'F1L': Frag_F1L, 'F2L': Frag_F2L, 'F3L': Frag_F3L, 'F2LB': Frag_F2LB, 'C1LB': Frag_C1LB, 'C1L': Frag_C1L, 'C2L': Frag_C2L, 'C3L': Frag_C3L,'XHD': Frag_XHD, 'XFO': Frag_XFO}
        start = time.time()
        logger_notime.info('Generating molecules…')
        logger_notime.info('\n################################LigXLigX###############################')

        count_for_top = 0
        all_state = [['init',100,1]]

        #Generation
        while count_generated < max_conf:
            cutoff_score = cutoff_score_init + (count_generated*(cutoff_score_fin-cutoff_score_init)/(max_conf))

            current_branch_core = 0
            current_branch_frag = 0
            current_cycle = 1
            depth = 0

            EOF_value = False
            blue_print('Generating molecules…')
            while EOF_value == False:

                if depth == 0:
                    if self.clean == True:
                        subprocess.call([clean_path])
                    each_strategy = []
                    infomation = 'Trying Sequence No.' +str(try_num+1)
                    logger.info(infomation)

                    for dir_core in core_dir_list:

                        current_state = []
                        initial_remaining_data,S,Q,P,total_charge,transform_log_main,transform_log_side,c_for_next,param_for_next,added_frag,ref_frag_list_next = import_core(dir_core)

                        current_state.append(dir_core)

                        state_for_rollout = current_state

                        first_elements = [sublist[0] for sublist in all_state]
                        add_cycle = 0
                        core = Core_mol(initial_remaining_data,current_cycle,cycle,current_branch_core,current_branch_frag,max_branch_core,max_branch_frag,ref_frag_list_next,param_for_next,total_charge,transform_log_main,transform_log_side,c_for_next)
                        Q_atom = core.Q_atom
                        if not any(current_state == item for item in first_elements):

                            if current_cycle != des_frag_cycle:

                                all_atom = [x for i in core.operation for x in Frag_dict[i].atom]
                                all_coord = [x for i in core.operation for x in Frag_dict[i].coord]
                                all_charge = [x for i in core.operation for x in Frag_dict[i].charge]
                                all_each_frag = [x for i in core.operation for x in Frag_dict[i].each_frag]
                                all_len_list = [x for i in core.operation for x in Frag_dict[i].len_list]
                                all_central_atom = [x for i in core.operation for x in Frag_dict[i].central_atom]
                                all_category = [x for i in core.operation for x in Frag_dict[i].category]
                                all_param = [x for i in core.operation for x in Frag_dict[i].param]
                                all_frag = [x for i in core.operation for x in Frag_dict[i].frag_name]
                                all_add_branch_core = [x for i in core.operation for x in Frag_dict[i].add_branch_core]
                                all_add_branch_frag = [x for i in core.operation for x in Frag_dict[i].add_branch_frag]
                                all_ref_frag_list = [x for i in core.operation for x in Frag_dict[i].ref_frag_list]
                                split_ind_list_1,split_ind_list_2 = create_split_ind_list(all_len_list)
                                possibility = True

                            elif current_cycle == des_frag_cycle and any(x == Frag_desired.frag_category_val[0] for x in core.operation):
                                all_atom = [x for x in Frag_desired.atom]
                                all_coord = [x for x in Frag_desired.coord]
                                all_charge = [x for x in Frag_desired.charge]
                                all_each_frag = [x for x in Frag_desired.each_frag]
                                all_len_list = [x for x in Frag_desired.len_list]
                                all_central_atom = [x for x in Frag_desired.central_atom]
                                all_category = [x for x in Frag_desired.category]
                                all_param = [x for x in Frag_desired.param]
                                all_frag = [x for x in Frag_desired.frag_name]
                                all_add_branch_core = [x for x in Frag_desired.add_branch_core]
                                all_add_branch_frag = [x for x in Frag_desired.add_branch_frag]
                                all_ref_frag_list = [x for x in Frag_desired.ref_frag_list]
                                split_ind_list_1,split_ind_list_2 = create_split_ind_list(all_len_list)
                                possibility = True

                            elif current_cycle == des_frag_cycle and not any(x == Frag_desired.frag_category_val[0] for x in core.operation):

                                escape_state = deepcopy(to_frons_state)
                                first_list = [sublist[0] for sublist in all_state]
                                escape_ind= first_list.index(escape_state[0])
                                all_state[escape_ind][1] = 100
                                all_state[escape_ind][2] = 10000000
                                possibility = False

                            if possibility:
                                score = execute_rollout_v2(initial_remaining_data,S,Q,P,total_charge,transform_log_main,transform_log_side,c_for_next,param_for_next,added_frag,ref_frag_list_next,current_cycle,cycle,current_branch_core,current_branch_frag,max_branch_core,max_branch_frag,evaluation_param,evaluation_method,desired_list_rollout,rollout_set,all_protein_cord,add_cycle,state_for_rollout,rol_des_category,des_frag_cycle,dir_core,cutoff_score_init,rollout_conf_inner,rollout_conf_inner_des,sigma_sum_cut)

                                all_state.append([current_state,score,1])

                    core_state = [item for item in all_state if len(item[0]) == 1]
                    core_state = sorted(core_state, key=lambda x: (x[2],x[1]))
                    selected_core_state = []
                    for i in core_state:
                        if i[1] <= cutoff_score and i[2]<10000000:
                            selected_core_state.append(i)

                    if selected_core_state ==[]:
                        if count_generated == 0:

                            print('no possibility in this core')
                            logger.info('no possibility in this core')
                            logger_notime.info('\n################################LigXLigX###############################')
                            sys.exit()
                        else:
                            print('the search is terminated because of no possibility.')
                            logger.info('no possibility in this core')
                            logger_notime.info('\n################################LigXLigX###############################')
                            max_conf = count_generated
                            break

                    else:
                        ubc_ind = calc_UCB(selected_core_state,cutoff_score)
                        core = selected_core_state[ubc_ind][0][0]

                        initial_remaining_data,S,Q,P,total_charge,transform_log_main,transform_log_side,c_for_next,param_for_next,added_frag,ref_frag_list_next = import_core(dir_core)
                        core = Core_mol(initial_remaining_data,current_cycle,cycle,current_branch_core,current_branch_frag,max_branch_core,max_branch_frag,ref_frag_list_next,param_for_next,total_charge,transform_log_main,transform_log_side,c_for_next)


                        first_list = [sublist[0] for sublist in all_state]

                        ind= first_list.index(selected_core_state[ubc_ind][0])

                        all_state[ind][2] +=1

                        depth = 1
                        next_state = selected_core_state[ubc_ind]


                #depth != 0
                else:
                    temp_current_state = []




                    to_frons_state = next_state

                    temp_state = next_state

                    current_state = []

                    if current_cycle != des_frag_cycle:
                        #その時点で連結可能なすべてのフラグメントを処理
                        all_atom = [x for i in core.operation for x in Frag_dict[i].atom]
                        all_coord = [x for i in core.operation for x in Frag_dict[i].coord]
                        all_charge = [x for i in core.operation for x in Frag_dict[i].charge]
                        all_each_frag = [x for i in core.operation for x in Frag_dict[i].each_frag]
                        all_len_list = [x for i in core.operation for x in Frag_dict[i].len_list]
                        all_central_atom = [x for i in core.operation for x in Frag_dict[i].central_atom]
                        all_category = [x for i in core.operation for x in Frag_dict[i].category]
                        all_param = [x for i in core.operation for x in Frag_dict[i].param]
                        all_frag = [x for i in core.operation for x in Frag_dict[i].frag_name]
                        all_add_branch_core = [x for i in core.operation for x in Frag_dict[i].add_branch_core]
                        all_add_branch_frag = [x for i in core.operation for x in Frag_dict[i].add_branch_frag]
                        all_ref_frag_list = [x for i in core.operation for x in Frag_dict[i].ref_frag_list]
                        all_frag_category_val = [x for i in core.operation for x in Frag_dict[i].frag_category_val]
                        split_ind_list_1,split_ind_list_2 = create_split_ind_list(all_len_list)
                        frag_c_for_next = [x for i in core.operation for x in Frag_dict[i].c_for_next]
                        possibility = True
                        desired_check = False


                    elif current_cycle == des_frag_cycle and any(x == Frag_desired.frag_category_val[0] for x in core.operation):

                        all_atom = [x for x in Frag_desired.atom]
                        all_coord = [x for x in Frag_desired.coord]
                        all_charge = [x for x in Frag_desired.charge]
                        all_each_frag = [x for x in Frag_desired.each_frag]
                        all_len_list = [x for x in Frag_desired.len_list]
                        all_central_atom = [x for x in Frag_desired.central_atom]
                        all_category = [x for x in Frag_desired.category]
                        all_param = [x for x in Frag_desired.param]
                        all_frag = [x for x in Frag_desired.frag_name]
                        all_add_branch_core = [x for x in Frag_desired.add_branch_core]
                        all_add_branch_frag = [x for x in Frag_desired.add_branch_frag]
                        all_ref_frag_list = [x for x in Frag_desired.ref_frag_list]
                        all_frag_category_val = [x for x in Frag_desired.frag_category_val]
                        all_frag_category_val = [x for x in Frag_desired.frag_category_val]
                        frag_c_for_next = [x for x in Frag_desired.c_for_next]
                        split_ind_list_1,split_ind_list_2 = create_split_ind_list(all_len_list)
                        possibility = True
                        desired_check = True


                    elif current_cycle == des_frag_cycle and not any(x == Frag_desired.frag_category_val[0] for x in core.operation):
                        escape_state = deepcopy(to_frons_state)
                        first_list = [sublist[0] for sublist in all_state]
                        escape_ind= first_list.index(escape_state[0])
                        all_state[escape_ind][1] = 100
                        all_state[escape_ind][2] = 10000000
                        possibility = False
                        depth = 0

                    if possibility:

                        temp_state_0 = temp_state[0]
                        first_elements_for_check = [sublist[0] for sublist in all_state if len(sublist[0])==(len(temp_state_0)+1) and sublist[0][:len(temp_state_0)]== temp_state_0]


                        int_result = link_result(core,all_atom,all_coord,all_charge,all_central_atom,split_ind_list_1,split_ind_list_2,all_category,all_param,all_len_list,all_frag,all_protein_cord,evaluation_param,evaluation_method,all_ref_frag_list,frag_c_for_next,sigma_sum_cut)
                        if first_elements_for_check == []:

                            int_result_list = int_result.evaluation.tolist()
                            current_state_0 = [[a,b,2] for a,b in zip(all_each_frag,int_result_list)]
                            current_state = deepcopy(current_state_0)
                            sorted_current_state = sorted(current_state, key=lambda x: (x[2],x[1]))
                            sorted_current_state_list = [sublist[1] for sublist in sorted_current_state if sublist[1] <= 0]
                            sorted_current_state_frag_dir = [sublist[0][0].split('/')[-2] for sublist in sorted_current_state if sublist[1] <= 0]
                            if desired_check == False:
                                if len(sorted_current_state_list) >= rollout_num:
                                    new_sorted_current_state_list = []
                                    frag_count_list = []
                                    x = 0
                                    for s,f in zip(sorted_current_state_list,sorted_current_state_frag_dir):
                                        if not any(f == a[0] for a in frag_count_list):
                                            frag_count_list.append([f,1])
                                            new_sorted_current_state_list.append(s)
                                            x += 1
                                            if x >= rollout_num:
                                                break

                                        else:
                                            index_frag = [a[0] for a in frag_count_list].index(f)
                                            if frag_count_list[index_frag][1] <= rollout_frag_max:
                                                frag_count_list[index_frag][1] += 1
                                                new_sorted_current_state_list.append(s)
                                                x += 1
                                                if x >= rollout_num:
                                                    break
                                            else:
                                                frag_count_list[index_frag][1] += 1

                                    sorted_current_state_list = new_sorted_current_state_list

                            #Manipulation of each fragment
                            for i in range(len(sorted_current_state_list)):

                                best_index = current_state.index(sorted_current_state[i])


                                next_frag_atom = int_result.atom[split_ind_list_1[best_index]:split_ind_list_2[best_index]]
                                next_frag_coord = int_result.coord[split_ind_list_1[best_index]:split_ind_list_2[best_index]]
                                EOF_value = int_result.eof_val[best_index]
                                category = int_result.temp_category[best_index]
                                temp_frag = int_result.temp_frag[best_index]
                                ref_frag_list_next = int_result.ref_frag_list_next_list[best_index]

                                first_list = [sublist[0] for sublist in all_state]

                                new_core_structure = create_new_core_structure(core,next_frag_atom,next_frag_coord,category,temp_frag,ref_frag_list_next)

                                current_cycle = core.add_cycle+core.current_cycle
                                current_branch_core = core.current_branch_core + all_add_branch_core[best_index]
                                current_branch_frag = core.current_branch_frag + all_add_branch_frag[best_index]
                                ref_frag_list_next = int_result.ref_frag_list_next_list[best_index]
                                param_for_next = int_result.param_for_next[best_index]
                                total_charge = int_result.total_charge[best_index]
                                transform_log_main = int_result.transform_log_main_list[best_index]
                                transform_log_side = int_result.transform_log_side_list[best_index]

                                if int_result.temp_frag[best_index] != 'sym24_XHD_0' and int_result.temp_frag[best_index] != 'sym24_XHD_1' and int_result.temp_frag[best_index] != 'sym24_XHD_2' and int_result.temp_frag[best_index] != 'sym24_XHD_3' and int_result.temp_frag[best_index] != 'sym24_XHD_4':
                                    c_for_next = int_result.c_for_next_list[best_index]
                                else:
                                    c_for_next = ['XCA_1']

                                if all_frag_category_val[best_index] != 'F1L' and all_frag_category_val[best_index] != 'C1L':

                                    score = execute_rollout_v2(new_core_structure,S,Q,P,total_charge,transform_log_main,transform_log_side,c_for_next,param_for_next,added_frag,ref_frag_list_next,current_cycle,cycle,current_branch_core,current_branch_frag,max_branch_core,max_branch_frag,evaluation_param,evaluation_method,desired_list_rollout,rollout_set,all_protein_cord,add_cycle,state_for_rollout,rol_des_category,des_frag_cycle,dir_core,cutoff_score_init,rollout_conf_inner,rollout_conf_inner_des,sigma_sum_cut)
                                else:
                                    score = current_state[best_index][1]

                                current_state[best_index][1] = score
                                current_state[best_index][2] = 1

                            each_state_0 = [[[next_state[0][0],sublist[0]]] + sublist[1:] if depth == 0 else [next_state[0]+[sublist[0]]] + sublist[1:] for sublist in current_state]
                            each_state = deepcopy(each_state_0)
                            ubc_ind = calc_UCB(current_state,cutoff_score)
                            best_for_opt = deepcopy(each_state[ubc_ind])
                            current_branch_core = core.current_branch_core + all_add_branch_core[ubc_ind]
                            current_branch_frag = core.current_branch_frag + all_add_branch_frag[ubc_ind]
                            ref_frag_list_next = int_result.ref_frag_list_next_list[ubc_ind]
                            param_for_next = int_result.param_for_next[ubc_ind]
                            total_charge = int_result.total_charge[ubc_ind]
                            transform_log_main = int_result.transform_log_main_list[ubc_ind]
                            transform_log_side = int_result.transform_log_side_list[ubc_ind]

                            if int_result.temp_frag[ubc_ind] != 'sym24_XHD_0' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_1' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_2' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_3' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_4':
                                c_for_next = int_result.c_for_next_list[ubc_ind]
                            else:
                                c_for_next = ['XCA_1']

                            if int_result.temp_frag[ubc_ind] != 'sym24_XHD_0' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_1' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_2' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_3' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_4':

                                for_opt_state = [best_for_opt]

                                plus_15_deg_frag = deepcopy(best_for_opt)
                                plus_15_deg_frag[0][-1][2] += 10
                                for_opt_state.append(plus_15_deg_frag)


                                minus_15_deg_frag = deepcopy(best_for_opt)
                                minus_15_deg_frag[0][-1][2] -= 10
                                for_opt_state.append(minus_15_deg_frag)

                                for_opt_score = []

                                for i in range(len(for_opt_state)):

                                    transforming_core_point_int = create_structure_from_strategy(for_opt_state,i,cycle,max_branch_core,max_branch_frag)
                                    if all_frag_category_val[ubc_ind] != 'F1L' and all_frag_category_val[ubc_ind] != 'C1L':
                                        score = execute_rollout_v2(transforming_core_point_int,S,Q,P,total_charge,transform_log_main,transform_log_side,c_for_next,param_for_next,added_frag,ref_frag_list_next,current_cycle,cycle,current_branch_core,current_branch_frag,max_branch_core,max_branch_frag,evaluation_param,evaluation_method,desired_list_rollout,rollout_set,all_protein_cord,add_cycle,state_for_rollout,rol_des_category,des_frag_cycle,dir_core,cutoff_score_init,rollout_conf_inner,rollout_conf_inner_des,sigma_sum_cut)
                                    else:
                                        atom_list = [x[0] for x in transforming_core_point_int]
                                        transforming_core_point_int_2= transforming_core_point_int[atom_list.index(core.Q_atom)+1:]
                                        LJ_array_sum,coulomb_array_sum,int_E_array_sum = calc_interaction_from_strategy(transforming_core_point_int_2,all_protein_cord)
                                        heavy_atom_list = [calc_heavy_atoms(transforming_core_point_int_2)]
                                        score = calc_evaluation_v2(LJ_array_sum,coulomb_array_sum,int_E_array_sum,evaluation_param,evaluation_method,heavy_atom_list)[0]

                                    for_opt_score.append(score)

                                opt_index = for_opt_score.index(min(for_opt_score))
                                each_state[ubc_ind][0][-1][2] = for_opt_state[opt_index][0][-1][2]
                                each_state[ubc_ind][1] = for_opt_score[opt_index]
                                current_state[ubc_ind][0][2] = for_opt_state[opt_index][0][-1][2]
                                current_state[ubc_ind][1] = for_opt_score[opt_index]

                        else:
                            int_state = [sublist for sublist in all_state if len(sublist[0])==(len(temp_state_0)+1) and sublist[0][:len(temp_state_0)]== temp_state_0]
                            current_state = [[a[0][-1],a[1],a[2]] for a in int_state]
                            each_state = [[[next_state[0][0],sublist[0]]] + sublist[1:] if depth == 0 else [next_state[0]+[sublist[0]]] + sublist[1:] for sublist in current_state]

                        current_score = [sublist[1] for sublist in current_state]
                        current_selection = [sublist[2] for sublist in current_state]
                        ubc_ind = calc_UCB(current_state,cutoff_score)


                        if current_score[ubc_ind] <= cutoff_score and current_selection[ubc_ind] < 10000:

                            #Update initiator
                            if first_elements_for_check == []:
                                all_state.extend(each_state)
                            first_list = [sublist[0] for sublist in all_state]
                            all_state_ind = first_list.index(next_state[0]+[current_state[ubc_ind][0]])

                            EOF_value = int_result.eof_val[ubc_ind]
                            next_frag_atom = int_result.atom[split_ind_list_1[ubc_ind]:split_ind_list_2[ubc_ind]]
                            next_frag_coord = int_result.coord[split_ind_list_1[ubc_ind]:split_ind_list_2[ubc_ind]]
                            category = int_result.temp_category[ubc_ind]
                            temp_frag = int_result.temp_frag[ubc_ind]
                            ref_frag_list_next = int_result.ref_frag_list_next_list[ubc_ind]
                            current_branch_core = core.current_branch_core + all_add_branch_core[ubc_ind]
                            current_branch_frag = core.current_branch_frag + all_add_branch_frag[ubc_ind]
                            ref_frag_list_next = int_result.ref_frag_list_next_list[ubc_ind]
                            param_for_next = int_result.param_for_next[ubc_ind]
                            total_charge = int_result.total_charge[ubc_ind]
                            transform_log_main = int_result.transform_log_main_list[ubc_ind]
                            transform_log_side = int_result.transform_log_side_list[ubc_ind]

                            if int_result.temp_frag[ubc_ind] != 'sym24_XHD_0' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_1' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_2' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_3' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_4':
                                c_for_next = int_result.c_for_next_list[ubc_ind]
                            else:
                                c_for_next = ['XCA_1']

                            core_name = each_state[ubc_ind][0][0]
                            frag_angle = each_state[ubc_ind][0][-1][-2]
                            score_info = "{:.2f}".format(current_score[ubc_ind])

                            infomation = '\tcore: '+str(core_name)+', depth: '+str(depth)+', fragment: '+str(temp_frag)+', dihedral: '+str(frag_angle)+', score: '+str(score_info)
                            logger_notime.info(infomation)

                            transforming_core_point_int = create_structure_from_strategy(each_state,ubc_ind,cycle,max_branch_core,max_branch_frag)

                            if EOF_value == False:
                                score = execute_rollout_v2(transforming_core_point_int,S,Q,P,total_charge,transform_log_main,transform_log_side,c_for_next,param_for_next,added_frag,ref_frag_list_next,current_cycle,cycle,current_branch_core,current_branch_frag,max_branch_core,max_branch_frag,evaluation_param,evaluation_method,desired_list_rollout,rollout_set,all_protein_cord,add_cycle,state_for_rollout,rol_des_category,des_frag_cycle,dir_core,cutoff_score_init,rollout_conf_inner,rollout_conf_inner_des,sigma_sum_cut)
                            elif EOF_value == True:
                                atom_list = [x[0] for x in transforming_core_point_int]
                                transforming_core_point_int_2= transforming_core_point_int[atom_list.index(core.Q_atom)+1:]
                                LJ_array_sum,coulomb_array_sum,int_E_array_sum = calc_interaction_from_strategy(transforming_core_point_int_2,all_protein_cord)
                                heavy_atom_list = [calc_heavy_atoms(transforming_core_point_int_2)]
                                score = calc_evaluation_v2(LJ_array_sum,coulomb_array_sum,int_E_array_sum,evaluation_param,evaluation_method,heavy_atom_list)[0]

                            if EOF_value == False:
                                all_state[all_state_ind][2] +=1
                                depth += 1
                                next_state = all_state[all_state_ind]

                                category = int_result.temp_category[ubc_ind]
                                temp_frag = int_result.temp_frag[ubc_ind]
                                ref_frag_list_next = int_result.ref_frag_list_next_list[ubc_ind]
                                current_cycle = core.add_cycle+core.current_cycle
                                current_branch_core = core.current_branch_core + all_add_branch_core[ubc_ind]
                                current_branch_frag = core.current_branch_frag + all_add_branch_frag[ubc_ind]
                                ref_frag_list_next = int_result.ref_frag_list_next_list[ubc_ind]
                                param_for_next = int_result.param_for_next[ubc_ind]
                                total_charge = int_result.total_charge[ubc_ind]
                                transform_log_main = int_result.transform_log_main_list[ubc_ind]

                                transform_log_side = int_result.transform_log_side_list[ubc_ind]

                                if int_result.temp_frag[ubc_ind] != 'sym24_XHD_0' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_1' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_2' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_3' and int_result.temp_frag[ubc_ind] != 'sym24_XHD_4':
                                    c_for_next = int_result.c_for_next_list[ubc_ind]

                                else:
                                    c_for_next = ['XCA_1']

                                core = Core_mol(transforming_core_point_int,current_cycle,cycle,current_branch_core,current_branch_frag,max_branch_core,max_branch_frag,ref_frag_list_next,param_for_next,total_charge,transform_log_main,transform_log_side,c_for_next)

                                infomation_for_param = '#LigX# @Sequence No.' +str(try_num+1)
                                logger_state.info(infomation_for_param)
                                param_log = param_for_logfile(transforming_core_point_int,current_cycle,current_branch_core,current_branch_frag,ref_frag_list_next,param_for_next,total_charge,transform_log_main,transform_log_side,c_for_next,temp_frag,next_frag_atom)
                                logger_state_notime.info('')
                                logger_state_notime.info(param_log)

                                logger_state_notime.info('\n################################LigXLigX###############################')

                            elif EOF_value == True:
                                infomation_for_param = '#LigX# @Sequence No.' +str(try_num+1)
                                logger_state.info(infomation_for_param)
                                param_log = param_for_logfile(transforming_core_point_int,current_cycle,current_branch_core,current_branch_frag,ref_frag_list_next,param_for_next,total_charge,transform_log_main,transform_log_side,c_for_next,temp_frag,next_frag_atom)
                                logger_state.info('')
                                logger_state_notime.info(param_log)
                                logger_state_notime.info('\n#LigX# EOF_value == True')


                                logger_state_notime.info('\n################################LigXLigX###############################')
                                logger_state_notime.info('################################LigXLigX###############################')
                                logger_state_notime.info('################################LigXLigX###############################')

                                count_generated +=1
                                try_num +=1
                                all_state[all_state_ind][2] +=10000
                                created = True

                        else:
                            first_list_to_escape = [sublist[0] for sublist in all_state]

                            escape_ind = first_list_to_escape.index(temp_state_0)
                            all_state[escape_ind][1] = 100
                            all_state[escape_ind][2] = 10000
                            depth = 0
                            try_num +=1
                            created = False

                            green_print('Not generated in this sequence. Node pruning')

                            logger_notime.info('Not generated in this sequence. Removed by cutoff')
                            state_for_logfile = '\tstate:'+str(each_state[ubc_ind])
                            logger_notime.info('')
                            logger_notime.info(state_for_logfile)
                            logger_notime.info('\n################################LigXLigX###############################')

                            break

                    elif possibility == False:
                        temp_state_0 = temp_state[0]
                        first_list_to_escape = [sublist[0] for sublist in all_state]

                        escape_ind = first_list_to_escape.index(temp_state_0)
                        all_state[escape_ind][1] = 100
                        all_state[escape_ind][2] = 10000
                        depth = 0
                        green_print('Not generated in this sequence. Possibility: False. Node pruning')

                        logger_notime.info('Not generated in this sequence. Possibility: False')
                        logger_notime.info('\n################################LigXLigX###############################')
                        try_num +=1
                        created = False
                        break

            #Save Data
            if created == False:
                continue
            depth = 0




            transforming_core_point_int = create_structure_from_strategy(each_state,ubc_ind,cycle,max_branch_core,max_branch_frag)
            LJ_list = [x[0] for x in LJ_array_sum]
            coulomb_list = [x[0] for x in coulomb_array_sum]
            int_E_list = [x[0] for x in int_E_array_sum]
            heavy_atom = heavy_atom_list[0]
            if heavy_atom == 0:
                heavy_atom = 1

            LE_LJ =[float(x)/int(heavy_atom) for x in LJ_list]
            LE_LJ_coulomb = []
            for a,b in zip(LE_LJ,coulomb_list):
                LE_LJ_coulomb.append(a+float(b))

            transforming_core_point =atomname_change_final(transforming_core_point_int)
            final_total_atom = [str(int(len(transforming_core_point)))]
            each_name = ['ID_'+str(count_generated)]
            temp_structure = []

            temp_structure.append(final_total_atom)
            temp_structure.append(each_name)

            for j in transforming_core_point:
                float_point =[round(value.item(), 4) if isinstance(value, np.float64) else value for value in j]
                temp_structure.append(float_point)

            smiles = get_smiles(temp_structure,total_charge,self.smiles_filter)

            if smiles != 'no_SMILES' and current_cycle-1 >= des_frag_cycle:

                all_result.append(final_total_atom)
                all_result.append(each_name)

                for j in transforming_core_point:
                    float_point =[round(value.item(), 4) if isinstance(value, np.float64) else value for value in j]
                    all_result.append(float_point)

                all_result_name = 'result_all_total_conf_'+str(count_generated)
                save_csv(all_result,all_result_name,result_dir)
                if count_generated >1:
                    remove_file = result_dir+'/result_all_total_conf_'+str(count_generated-1)+'.xyz'
                    os.remove(remove_file)

                each_result = [count_generated]

                for i in range(num_protein_structures):
                    each_result.append(LJ_list[i])

                LJ_average = statistics.mean(LJ_list)
                each_result.append(LJ_average)

                for i in range(num_protein_structures):
                    each_result.append(coulomb_list[i])
                coulomb_average = statistics.mean(coulomb_list)
                each_result.append(coulomb_average)

                for i in range(num_protein_structures):
                    each_result.append(int_E_list[i])
                int_E_average = statistics.mean(int_E_list)
                each_result.append(int_E_average)

                for i in range(num_protein_structures):
                    each_result.append(float(LJ_list[i])/int(heavy_atom))
                LJ_LE_average = statistics.mean(LJ_list)/int(heavy_atom)
                each_result.append(LJ_LE_average)

                for i in range(num_protein_structures):
                    each_result.append(float(int_E_list[i])/int(heavy_atom))
                int_E_LE_average = statistics.mean(int_E_list)/int(heavy_atom)
                each_result.append(int_E_LE_average)

                for i in range(num_protein_structures):
                    each_result.append(LE_LJ_coulomb[i])

                LE_LJ_coulomb_LJ_average = statistics.mean(LE_LJ_coulomb)
                each_result.append(LE_LJ_coulomb_LJ_average)

                each_result.append(each_state[ubc_ind])
                each_result.append(smiles)

                all_score_strategy.append(each_result)

                score_name = 'result_score'
                save_csv_2(all_score_strategy,score_name,result_dir)

                score_for_info =  "{:.3f}".format(score)
                generated_time = time.time()

                elapsed_time = generated_time-start

                performance = calc_performance(count_generated,elapsed_time)

                generated_info = '#LigX# '+str(count_generated)+' conf generated (' +performance +'), score = '+str(score_for_info)

                red_print(generated_info)

                structure_info = '\tName: '+str(each_name[0])+', SMILES: '+str(smiles)
                state_for_logfile = '\tstate:'+str(each_state[ubc_ind])
                logger_notime.info('')
                logger_notime.info(state_for_logfile)
                logger_notime.info('')
                logger.info(generated_info)
                logger_notime.info(structure_info)
                logger_notime.info('\n################################LigXLigX###############################')

                if smiles != 'without_SMILES_conversion':
                    for_fig = Chem.MolFromSmiles(smiles)
                    Draw.MolToFile(for_fig,image_dir+'/'+str(each_name[0])+'.png',size=(900, 900))

            else:
                green_print('Removed by SMILES filter')
                state_for_logfile = '\tstate:'+str(each_state[ubc_ind])
                logger_notime.info('')
                logger_notime.info(state_for_logfile)
                logger_notime.info('Removed by SMILES filter')
                logger_notime.info('\n################################LigXLigX###############################')
                count_generated-=1

            if (count_generated)//count_for_data_processing > (count_generated-1)//count_for_data_processing and smiles != 'no_SMILES':


                if os.path.isdir(result_dir+'/result_conf_'+str(count_for_top)):
                    shutil.rmtree(result_dir+'/result_conf_'+str(count_for_top))
                    del count_for_top

                count_for_top = deepcopy(count_generated)

                print('creating top at conf '+ str(count_for_top))
                os.mkdir(result_dir+'/result_conf_'+str(count_for_top))
                index_list = get_reconstract_strategy(num_protein_structures,all_score_strategy)
                all_score_strategy_cut = all_score_strategy[1:]
                if self.clean == True:
                    subprocess.call([clean_path])


                for index_name,index in index_list[1:-1]:
                    sorted_all_score_strategy_cut = sorted(all_score_strategy_cut, key=lambda x: x[index])
                    count_sel = 0
                    ranking_list = []
                    each_ranking_dir = result_dir+'/result_conf_'+str(count_generated)+'/'+index_name
                    os.mkdir(each_ranking_dir)

                    for sorted_list in sorted_all_score_strategy_cut:

                        count_sel += 1
                        if count_sel >result_conf:
                            break
                        depth = 0
                        ID_for_reult =sorted_list[0]

                        each_strategy = sorted_list[-2]
                        each_strategy_fin = [each_strategy]

                        final_charge = get_core_charge(each_strategy_fin[0][0][0])


                        for i in each_strategy_fin[0][0][1:]:
                            final_charge += int(i[-1])
                            #print(i[-1])

                        transforming_core_point = create_structure_from_strategy(each_strategy_fin,0,cycle,max_branch_core,max_branch_frag)

                        transforming_core_point =atomname_change_final(transforming_core_point)
                        final_total_atom = [str(int(len(transforming_core_point)))]
                        rank_num = str(count_sel).zfill(4)
                        each_name = ['rank_'+rank_num+'_'+index_name+'_ID_'+str(ID_for_reult)]

                        each_list = []

                        each_list.append(final_total_atom)
                        each_list.append(each_name)
                        ranking_list.append(final_total_atom)
                        ranking_list.append(each_name)

                        for j in transforming_core_point:
                            float_point =[round(value.item(), 4) if isinstance(value, np.float64) else value for value in j]
                            each_list.append(float_point)
                            ranking_list.append(float_point)

                        each_result ='result_conf_'+str(count_generated)+'/'+index_name+'/'+str(each_name[0])+'/'+str(each_name[0])
                        os.mkdir(each_ranking_dir+'/'+str(each_name[0]))
                        dir_charge = each_ranking_dir+'/'+str(each_name[0])

                        save_csv(each_list,each_result,result_dir)
                        save_charge(final_charge,dir_charge)
                    result_name ='result_conf_'+str(count_generated)+'/'+'ranking_'+index_name+'_top'
                    save_csv(ranking_list,result_name,result_dir)


        if os.path.isdir(result_dir+'/result_conf_'+str(count_for_top)):
            shutil.rmtree(result_dir+'/result_conf_'+str(count_for_top))
            del count_for_top

        count_for_top = deepcopy(count_generated)

        print('creating top at conf '+ str(count_generated))
        os.mkdir(result_dir+'/result_conf_'+str(count_generated))
        index_list = get_reconstract_strategy(num_protein_structures,all_score_strategy)
        all_score_strategy_cut = all_score_strategy[1:]
        if self.clean == True:
            subprocess.call([clean_path])

        for index_name,index in index_list[1:-1]:
            sorted_all_score_strategy_cut = sorted(all_score_strategy_cut, key=lambda x: x[index])
            count_sel = 0
            ranking_list = []
            each_ranking_dir = result_dir+'/result_conf_'+str(count_generated)+'/'+index_name
            os.mkdir(each_ranking_dir)

            for sorted_list in sorted_all_score_strategy_cut:

                count_sel += 1
                if count_sel >result_conf:
                    break
                depth = 0
                ID_for_reult =sorted_list[0]

                each_strategy = sorted_list[-2]
                each_strategy_fin = [each_strategy]

                final_charge = get_core_charge(each_strategy_fin[0][0][0])

                for i in each_strategy_fin[0][0][1:]:
                    final_charge += int(i[-1])

                transforming_core_point = create_structure_from_strategy(each_strategy_fin,0,cycle,max_branch_core,max_branch_frag)

                transforming_core_point =atomname_change_final(transforming_core_point)
                final_total_atom = [str(int(len(transforming_core_point)))]
                rank_num = str(count_sel).zfill(4)
                each_name = ['rank_'+rank_num+'_'+index_name+'_ID_'+str(ID_for_reult)]

                each_list = []

                each_list.append(final_total_atom)
                each_list.append(each_name)
                ranking_list.append(final_total_atom)
                ranking_list.append(each_name)

                for j in transforming_core_point:
                    float_point =[round(value.item(), 4) if isinstance(value, np.float64) else value for value in j]
                    each_list.append(float_point)
                    ranking_list.append(float_point)

                each_result ='result_conf_'+str(count_generated)+'/'+index_name+'/'+str(each_name[0])+'/'+str(each_name[0])
                os.mkdir(each_ranking_dir+'/'+str(each_name[0]))
                dir_charge = each_ranking_dir+'/'+str(each_name[0])

                save_csv(each_list,each_result,result_dir)
                save_charge(final_charge,dir_charge)
            result_name ='result_conf_'+str(count_generated)+'/'+'ranking_'+index_name+'_top'
            save_csv(ranking_list,result_name,result_dir)
        print('Normal termination')
        logger.info('Normal termination')
        logger_notime.info('\n################################LigXLigX###############################')