From 3c048772973a3075c50e63cecb3cc47011dc3270 Mon Sep 17 00:00:00 2001 From: Ricardo Wurmus Date: Mon, 5 Mar 2018 23:44:21 +0100 Subject: [PATCH] Implement support for --init option. --- Makefile.am | 1 + etc/sample_sheet.csv.example | 7 +++ pigx-rnaseq.in | 116 +++++++++++++++++++++++------------ 3 files changed, 86 insertions(+), 38 deletions(-) create mode 100644 etc/sample_sheet.csv.example diff --git a/Makefile.am b/Makefile.am index fb4416b..0283201 100644 --- a/Makefile.am +++ b/Makefile.am @@ -16,6 +16,7 @@ dist_pkglibexec_scripts_SCRIPTS = \ scripts/validate_input.py dist_pkgdata_DATA = \ + etc/sample_sheet.csv.example \ etc/settings.yaml \ etc/pretty.txt \ images/Logo_PiGx.png diff --git a/etc/sample_sheet.csv.example b/etc/sample_sheet.csv.example new file mode 100644 index 0000000..06f6860 --- /dev/null +++ b/etc/sample_sheet.csv.example @@ -0,0 +1,7 @@ +name,reads,reads2,sample_type +HBR_Rep1,HBR_Rep1.read1.fastq.gz,HBR_Rep1.read2.fastq.gz,HBR +HBR_Rep2,HBR_Rep2.read1.fastq.gz,HBR_Rep2.read2.fastq.gz,HBR +HBR_Rep3,HBR_Rep3.read1.fastq.gz,HBR_Rep3.read2.fastq.gz,HBR +UHR_Rep1,UHR_Rep1.read1.fastq.gz,UHR_Rep1.read2.fastq.gz,UHR +UHR_Rep2,UHR_Rep2.read1.fastq.gz,UHR_Rep2.read2.fastq.gz,UHR +UHR_Rep3,UHR_Rep3.read1.fastq.gz,UHR_Rep3.read2.fastq.gz,UHR diff --git a/pigx-rnaseq.in b/pigx-rnaseq.in index 37ec41d..a137d7e 100644 --- a/pigx-rnaseq.in +++ b/pigx-rnaseq.in @@ -61,13 +61,16 @@ parser = argparse.ArgumentParser(description=description, parser.add_argument('-v', '--version', action='version', version=version) -parser.add_argument('samplesheet', +parser.add_argument('sample_sheet', nargs='?', default='sample_sheet.csv', help="""\ The sample sheet containing sample data in CSV format.\ """) -parser.add_argument('-s', '--settings', dest='settings', required=True, - help='A YAML file for settings that deviate from the defaults.') +group = parser.add_mutually_exclusive_group(required=True) +group.add_argument('--init', dest='init', choices=['settings', 'sample-sheet', 'both'], const='both', nargs='?', + help='Generate a template SETTINGS file, a SAMPLE-SHEET. Leave empty for both.') +group.add_argument('-s', '--settings', dest='settings', + help='A YAML file for settings that deviate from the defaults.') parser.add_argument('-c', '--configfile', dest='configfile', default='./config.json', help="""\ @@ -116,38 +119,11 @@ def bail(msg): exit(1) -def generate_config(configfile, samplesheet, settingsfile): +def generate_config(configfile, sample_sheet, settingsfile, dirs): """Generate a new configuration file CONFIGFILE using SAMPLESHEET and -SETTINGSFILE as inputs.""" - dirs = {} - if os.getenv('PIGX_UNINSTALLED'): - here = os.getenv('srcdir') if os.getenv('srcdir') else os.getcwd() - dirs['locations'] = { - 'prefix' : here, - 'exec_prefix' : here, - 'libexecdir' : here, - 'pkglibexecdir': here, - 'datarootdir' : here, - 'pkgdatadir' : here - } - else: - # Expand and store autoconf directory variables - prefix = '@prefix@' - exec_prefix = '@exec_prefix@'[1:].format(prefix=prefix) - libexecdir = '@libexecdir@'[1:].format(exec_prefix=exec_prefix) - pkglibexecdir = '{libexecdir}/@PACKAGE@'.format(libexecdir=libexecdir) - datarootdir = '@datarootdir@'[1:].format(prefix=prefix) - pkgdatadir = '@datadir@/@PACKAGE@'[1:].format(datarootdir=datarootdir) - - dirs['locations'] = { - 'prefix' : '@prefix@', - 'exec_prefix' : exec_prefix, - 'libexecdir' : libexecdir, - 'pkglibexecdir': pkglibexecdir, - 'datarootdir' : datarootdir, - 'pkgdatadir' : pkgdatadir - } - +SETTINGSFILE as inputs. Use the locations in DIRS to find default +settings. + """ # Load defaults if os.getenv('PIGX_UNINSTALLED'): where = os.getenv('srcdir') if os.getenv('srcdir') else '.' @@ -168,14 +144,14 @@ SETTINGSFILE as inputs.""" settings['locations'].update(dirs['locations']) # Resolve relative paths in the locations section - root = path.dirname(samplesheet) + root = path.dirname(sample_sheet) here = os.getenv('srcdir') if os.getenv('srcdir') else os.getcwd() for key in settings['locations']: settings['locations'][key] = path.normpath(path.join(here, root, settings['locations'][key])) # Record the location of the sample sheet. - settings['locations']['sample-sheet'] = path.abspath(samplesheet) + settings['locations']['sample-sheet'] = path.abspath(sample_sheet) # Write the config file with open(configfile, 'w') as outfile: @@ -258,10 +234,74 @@ def display_logo(): +# Determine locations +dirs = {} +if os.getenv('PIGX_UNINSTALLED'): + here = os.getenv('srcdir') if os.getenv('srcdir') else os.getcwd() + dirs['locations'] = { + 'prefix' : here, + 'exec_prefix' : here, + 'libexecdir' : here, + 'pkglibexecdir': here, + 'datarootdir' : here, + 'pkgdatadir' : here + } +else: + # Expand and store autoconf directory variables + prefix = '@prefix@' + exec_prefix = '@exec_prefix@'[1:].format(prefix=prefix) + libexecdir = '@libexecdir@'[1:].format(exec_prefix=exec_prefix) + pkglibexecdir = '{libexecdir}/@PACKAGE@'.format(libexecdir=libexecdir) + datarootdir = '@datarootdir@'[1:].format(prefix=prefix) + pkgdatadir = '@datadir@/@PACKAGE@'[1:].format(datarootdir=datarootdir) + + dirs['locations'] = { + 'prefix' : '@prefix@', + 'exec_prefix' : exec_prefix, + 'libexecdir' : libexecdir, + 'pkglibexecdir': pkglibexecdir, + 'datarootdir' : datarootdir, + 'pkgdatadir' : pkgdatadir + } + +# Init? +if args.init: + init_settings = False + init_sample_sheet = False + if os.getenv('PIGX_UNINSTALLED'): + base = os.getcwd() + '/etc/' + else: + base = dirs['locations']['pkgdatadir'] + + if args.init == 'both': + init_settings = True + init_sample_sheet = True + + if args.init == 'settings' or init_settings: + name = 'settings.yaml' + if path.exists(name): + print('Refusing to overwrite existing {}.'.format(name)) + else: + with open(name, 'w') as outfile: + with open(path.join(base, name), 'r') as infile: + for line in infile: + outfile.write('# ' + line) + print('Generated {} template.'.format(name)) + + if args.init == 'sample-sheet' or init_sample_sheet: + name = 'sample_sheet.csv' + if path.exists(name): + print('Refusing to overwrite existing sample_sheet.csv.') + else: + shutil.copy(path.join(base, name + '.example'), name) + print('Generated {} template.'.format(name)) + exit(0) + # Run snakemake! generate_config(args.configfile, - args.samplesheet, - args.settings) + args.sample_sheet, + args.settings, + dirs) config = json.load(open(args.configfile, 'r'))