#!/usr/bin/env python3

# This file is part of Discretizer.

# Copyright (c) 2017 Jan Plhak
# https://github.com/loschmidt/discretizer

# Discretizer is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# Discretizer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public License
# along with Discretizer.  If not, see <https://www.gnu.org/licenses/>.

"""Molecule tunnel discretization tool.

Usage:
  discretizer -f | --file <in-filename> [--delta <delta>] [-o <out-filename>] [-t <threads>]

Options:
  -h --help                         Show this help.
  -f --file                         File containing information about tunnel in molecule in PDB format.
  -o --output-file <out-filename>   Dump disks to file in dsd format.
  --delta <delta>                   Maximal distance between disks.
  -t --threads <threads>            Number of threads used for calculation. Default: number of cores.

"""

import random
import json
import sys
import multiprocessing

from docopt import docopt
from discretizer.digger import *
from discretizer.io import load_tunnel_from_pdb


if __name__ == '__main__':
    arguments = docopt(__doc__)
    filename = arguments['<in-filename>']
    tunnel = load_tunnel_from_pdb(filename)

    delta = float(arguments["--delta"] or 0.3)
    disks = dig_tunnel(tunnel, DigOpts(delta, filename, int(arguments["--threads"] or multiprocessing.cpu_count())))

    output_path = arguments.get("--output-file")
    if output_path:
        with open(output_path, "w") as output_file:
            for disk in disks:
                line = "{} {} {} {} {} {} {}\n".format(disk.center[0],
                    disk.center[1], disk.center[2], disk.normal[0], disk.normal[1],
                    disk.normal[2], disk.radius)
                output_file.write(line)
