Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Command Line Interface (CLI) #46

Open
JanCBrammer opened this issue Mar 18, 2022 · 2 comments
Open

Implement Command Line Interface (CLI) #46

JanCBrammer opened this issue Mar 18, 2022 · 2 comments
Assignees
Labels

Comments

@JanCBrammer
Copy link
Collaborator

No description provided.

@JanCBrammer JanCBrammer self-assigned this Mar 21, 2022
@schatzsc
Copy link
Collaborator

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
Copy link
Collaborator

schatzsc commented Jul 4, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants