Skip to content

Commit

Permalink
Implement support for --init option.
Browse files Browse the repository at this point in the history
  • Loading branch information
rekado committed Mar 5, 2018
1 parent 6d64b3e commit 3c04877
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 38 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions etc/sample_sheet.csv.example
Original file line number Diff line number Diff line change
@@ -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
116 changes: 78 additions & 38 deletions pigx-rnaseq.in
Original file line number Diff line number Diff line change
Expand Up @@ -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="""\
Expand Down Expand Up @@ -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 '.'
Expand All @@ -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:
Expand Down Expand Up @@ -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'))

Expand Down

0 comments on commit 3c04877

Please sign in to comment.