Debian Codegraph @

cgecore/argumentparsing.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """ THIS MODULE CONTAINS ALL THE SHARED WRAPPER FUNCTIONS """
3 ################################################################################
4 # CGE FUNCTION MODULE #
5 ################################################################################
6 # This script is part of the CGE Pipeline structure
7 import os, sys
8 from subprocess import Popen, PIPE
9 from argparse import ArgumentParser, RawDescriptionHelpFormatter, SUPPRESS
10
11 # CGE modules
12 from .utility import debug, open_
13
14 def get_string(string):
15 """ This function checks if a path was given as string, and tries to read the
16 file and return the string.
17 """
18 truestring = string
19 if string is not None:
20 if '/' in string:
21 if os.path.isfile(string):
22 try:
23 with open_(string,'r') as f:
24 truestring = ' '.join(line.strip() for line in f)
25 except: pass
26 if truestring.strip() == '': truestring = None
27 return truestring
28
29 def get_arguments(options):
30 """ This function handles and validates the wrapper arguments. """
31 # These the next couple of lines defines the header of the Help output
32 parser = ArgumentParser(
33 formatter_class=RawDescriptionHelpFormatter,
34 usage=("""%(prog)s
35 --------------------------------------------------------------------------------
36 """),
37 description=("""
38 Service Wrapper
39 ===============
40 This is the service wrapper script, which is a part of the CGE services.
41 Read the online manual for help.
42 A list of all published services can be found at:
43 cge.cbs.dtu.dk/services
44
45 """), epilog=("""
46 --------------------------------------------------------------------------------
47 """))
48
49 #ADDING ARGUMENTS
50 setarg = parser.add_argument
51 #SERVICE SPECIFIC ARGUMENTS
52 if isinstance(options, str):
53 options = [[x for i,x in enumerate(line.split()) if i in [1,2]] for line in options.split('\n') if len(line)>0]
54 for o in options:
55 try:
56 setarg(o[1], type=str, dest=o[0], default=None, help=SUPPRESS)
57 except:
58 None
59 else:
60 for o in options:
61 if o[2] is True:
62 # Handle negative flags
63 setarg(o[0], action="store_false", dest=o[1], default=o[2],
64 help=o[3])
65 elif o[2] is False:
66 # Handle positive flags
67 setarg(o[0], action="store_true", dest=o[1], default=o[2],
68 help=o[3])
69 else:
70 help_ = o[3] if o[2] is None else "%s [%s]"%(o[3], '%(default)s')
71 setarg(o[0], type=str, dest=o[1], default=o[2],
72 help=help_)
73 # VALIDATION OF ARGUMENTS
74 args = parser.parse_args()
75 debug.log("ARGS: %s"%args)
76 return args
77
78 def check_file_type(files):
79 """ Check whether the input files are in fasta format, reads format or
80 other/mix formats.
81 """
82 all_are_fasta = True
83 all_are_reads = True
84 all_are_empty = True
85 if sys.version_info < (3, 0):
86 if isinstance(files, (str, unicode)): files = [files]
87 else:
88 if isinstance(files, str): files = [files]
89 for file_ in files:
90 debug.log('Checking file type: %s'%file_)
91 # Check if file is empty
92 if os.stat(file_).st_size == 0: continue
93 else: all_are_empty = False
94 with open_(file_) as f:
95 fc = f.readline()[0]
96 if fc != "@": all_are_reads = False
97 if fc != ">": all_are_fasta = False
98 if all_are_empty: return 'empty'
99 elif all_are_fasta: return 'fasta'
100 elif all_are_reads: return 'fastq'
101 else: return 'other'
102
103 def make_file_list(upload_path):
104 """ This function returns list of files in the given dir """
105 newlist = []
106 for el in sorted(os.listdir(upload_path)):
107 if ' ' in el:
108 raise Exception('Error: Spaces are not allowed in file names!\n')
109 newlist.append(os.path.normpath(upload_path+'/'+el))
110 debug.log('InputFiles: %s\n'%newlist)
111 return newlist