TUCAN-nest / TUCAN

A molecular identifier and descriptor for all domains of chemistry.
https://tucan-nest.github.io
GNU General Public License v3.0
22 stars 5 forks source link

Implement Command Line Interface (CLI) #46

Open JanCBrammer opened 2 years ago

schatzsc commented 2 years ago

Small GUI for stand-alone and web application for demonstration purpose would also be nice but not essential as long as we do not aim (yet) at a general and non-export audience

schatzsc commented 2 years ago

Here a short CLI which uses several different options for conversion of molfile into TUCAN string and vice versa, also allows control of implicit-to-explicit H conversion and graphical output via gravis https://robert-haas.github.io/gravis-docs/

Needs the following libraries:

import argparse import sys from pathlib import Path

#

Program initialization and command line interpretation

#

def get_command_line_parameters(): EXIT_SUCCESS = 0 EXIT_FAILURE = 1 parser = argparse.ArgumentParser() parser.add_argument('-f', '--file', help='input file: either v3000 molfile .mol or textfile containing TUCAN string .tcn', type=argparse.FileType('r')) parser.add_argument('-m2m', '--molfile2molfile', help='Convert molfile to canonical molfile', action ='store_true') parser.add_argument('-m2t', '--molfile2tucan', help='Convert molfile to canonical TUCAN string', action ='store_true') parser.add_argument('-t2m', '--tucan2molfile', help='Convert TUCAN string to canonical molefile', action ='store_true') parser.add_argument('-t2t', '--tucan2tucan', help='Convert TUCAN string to canonical TUCAN string', action ='store_true') parser.add_argument('-i', '--i2e', help='Use implicit-to-explicit hydrogen conversion pre-processor', action ='store_true') parser.add_argument('-g', '--plot_graph', help='Plot graph', action ='store_true') args = parser.parse_args() if not args.file: print("\nno input file specified\n") parser.print_usage() sys.exit(EXIT_FAILURE) path_to_file = Path().absolute() filename = args.file.name filepath = path_to_file/filename conversion_direction = "m2t" # standard conversion mode if args.molfile2molfile: conversion_direction = "m2m" if args.molfile2tucan: conversion_direction = "m2t" if args.tucan2molfile: conversion_direction = "t2m" if args.tucan2tucan: conversion_direction = "t2t" if args.i2e: implicit_to_explicit_H = True else: implicit_to_explicit_H = False if not args.plot_graph: plotgraph = False else: plotgraph = True return filepath, conversion_direction, implicit_to_explicit_H, plotgraph

#

Main program loop

#

filepath, conversion_direction, implicit_to_explicit_H, plot_graph = get_command_line_parameters() print("\n"+'=' 100) print("\nTUCAN - A molecular identifier and descriptor for all domains of chemistry "+version) print("\nJan Brammer (RWTH Aachen) and Ulrich Schatzschneider (Universität Würzburg) within NFDI4Chem") print("\nCC BY-SA 06/2022\n") print('=' 100) print("Processing file:", filepath) print("Conversion mode:", conversion_mode[conversion_direction]) print('-' 100) if((conversion_direction == "m2m") and (filepath.suffix == ".mol")): graph = graph_from_molfile(filepath, conversion_direction) if(implicit_to_explicit_H == True): graph = implicit_to_explicit_hydrogen_preprocessor(graph) cgraph = canonicalize_molecule(graph, 0) print(create_molfile(cgraph)) elif((conversion_direction == "m2t") and (filepath.suffix == ".mol")): graph = graph_from_molfile(filepath, conversion_direction) if(implicit_to_explicit_H == True): graph = implicit_to_explicit_hydrogen_preprocessor(graph) cgraph = canonicalize_molecule(graph, 0) print(serialize_molecule(cgraph)) elif((conversion_direction == "t2m") and (filepath.suffix == ".tcn")): graph = graph_from_tucan_string(filepath, conversion_direction) cgraph = canonicalize_molecule(graph, 0) print(create_molfile(cgraph)) elif((conversion_direction == "t2t") and (filepath.suffix == ".tcn")): graph = graph_from_tucan_string(filepath, conversion_direction) cgraph = canonicalize_molecule(graph, 0) print(serialize_molecule(cgraph)) else: raise IOError("Invalid combination of file format and program option") print('-' 100) print_molecule_statistics(cgraph) print('-' * 100) if(plot_graph == True): plot_graph_with_gravis(cgraph)

#

This is the end

#