
import os

def main():
    file_path = "./results/folders.txt"

    cells = {}
    with open(file_path) as f:
        for l in f:
            parts = l.strip().split("/")
            cell_folder = parts[0]
            start_para = parts[1]
            score = float(start_para.split('_')[-1])
            if cell_folder not in cells:
                cells[cell_folder] = [score, start_para]
            else:
                if cells[cell_folder][0] > score:
                    cells[cell_folder] = [score, start_para]

    for k in sorted(cells.keys()):
        print(k, cells[k][1])
    remotehost = "alex@kraken.am28.uni-tuebingen.de"
    remote_base = "P-unit_model/results/sam_cells/"
    folders_to_copy = [remote_base + k + "/" + cells[k][1] + "/ " for k in sorted(cells.keys())]
    remote_files = ""
    for i in range(len(folders_to_copy)):
        remote_files += folders_to_copy[i]

    local_base = "./results/sam_cells_only_best/"
    # os.system('scp -r  "%s:%s" "%s"' % (remotehost, remote_files, local_base))

    # create folders

    for k in sorted(cells.keys()):
        cell_folder = "./results/sam_cells_only_best/" + k + "/"
        os.makedirs(cell_folder)

        os.rename("./results/sam_cells_only_best/best/" + cells[k][1], cell_folder + cells[k][1])


def read_file(path):
    pass


if __name__ == '__main__':
    main()