def main(): """main function """ parser = argparse.ArgumentParser( description=__doc__.format(PIPELINE_NAME=PIPELINE_NAME, PIPELINE_VERSION=get_pipeline_version()) ) # generic args parser.add_argument("-o", "--outdir", required=True, help="Output directory (may not exist)") parser.add_argument("--name", help="Give this analysis run a name (used in email and report)") parser.add_argument("--no-mail", action="store_true", help="Don't send mail on completion") # site = get_site() default = get_default_queue("slave") parser.add_argument( "-w", "--slave-q", default=default, help="Queue to use for slave jobs (default: {})".format(default) ) default = get_default_queue("master") parser.add_argument( "-m", "--master-q", default=default, help="Queue to use for master job (default: {})".format(default) ) parser.add_argument("-n", "--no-run", action="store_true") parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity") parser.add_argument("-q", "--quiet", action="count", default=0, help="Decrease verbosity") cfg_group = parser.add_argument_group("Configuration files (advanced)") cfg_group.add_argument( "--sample-cfg", help="Config-file (YAML) listing samples and readunits." " Collides with -1, -2 and -s" ) for name, descr in [("references", "reference sequences"), ("params", "parameters"), ("modules", "modules")]: default = os.path.abspath(os.path.join(CFG_DIR, "{}.yaml".format(name))) cfg_group.add_argument( "--{}-cfg".format(name), default=default, help="Config-file (yaml) for {}. (default: {})".format(descr, default), ) # pipeline specific args parser.add_argument( "--normal-fq1", nargs="+", help="Normal FastQ file/s (gzip only)." " Multiple input files supported (auto-sorted)." " Note: each file (or pair) gets a unique read-group id." " Collides with --sample-cfg.", ) parser.add_argument("--normal-fq2", nargs="+", help="Normal FastQ file/s (if paired) (gzip only). See also --fq1") parser.add_argument( "--tumor-fq1", nargs="+", help="Tumor FastQ file/s (gzip only)." " Multiple input files supported (auto-sorted)." " Note: each file (or pair) gets a unique read-group id." " Collides with --sample-cfg.", ) parser.add_argument("--tumor-fq2", nargs="+", help="Tumor FastQ file/s (if paired) (gzip only). See also --fq1") parser.add_argument("-t", "--seqtype", required=True, choices=["WGS", "WES", "targeted"], help="Sequencing type") parser.add_argument( "-l", "--intervals", help="Intervals file (e.g. bed file) listing regions of interest." " Required for WES and targeted sequencing.", ) parser.add_argument("-D", "--dont-mark-dups", action="store_true", help="Don't mark duplicate reads") parser.add_argument( "--normal-bam", help="Advanced: Injects normal BAM (overwrites normal-fq options)." " WARNING: reference and postprocessing need to match pipeline requirements", ) parser.add_argument( "--tumor-bam", help="Advanced: Injects tumor BAM (overwrites tumor-fq options)." " WARNING: reference and postprocessing need to match pipeline requirements", ) args = parser.parse_args() # Repeateable -v and -q for setting logging level. # See https://www.reddit.com/r/Python/comments/3nctlm/what_python_tools_should_i_be_using_on_every/ # and https://gist.github.com/andreas-wilm/b6031a84a33e652680d4 # script -vv -> DEBUG # script -v -> INFO # script -> WARNING # script -q -> ERROR # script -qq -> CRITICAL # script -qqq -> no logging at all logger.setLevel(logging.WARN + 10 * args.quiet - 10 * args.verbose) aux_logger.setLevel(logging.WARN + 10 * args.quiet - 10 * args.verbose) if os.path.exists(args.outdir): logger.fatal("Output directory %s already exists", args.outdir) sys.exit(1) # samples is a dictionary with sample names as key (mostly just # one) and readunit keys as value. readunits is a dict with # readunits (think: fastq pairs with attributes) as value if args.sample_cfg: if any([args.normal_fq1, args.normal_fq2, args.tumor_fq1, args.tumor_fq2, args.normal_bam, args.tumor_bam]): logger.fatal("Config file overrides fastq and sample input arguments." " Use one or the other") sys.exit(1) if not os.path.exists(args.sample_cfg): logger.fatal("Config file %s does not exist", args.sample_cfg) sys.exit(1) samples, readunits = get_samples_and_readunits_from_cfgfile(args.sample_cfg) else: samples = dict() if args.normal_bam: normal_readunits = dict() samples["normal"] = [] assert os.path.exists(args.normal_bam) else: if not all([args.normal_fq1, args.tumor_fq1]): logger.fatal("Need at least fq1 and sample without config file") sys.exit(1) normal_readunits = get_readunits_from_args(args.normal_fq1, args.normal_fq2) samples["normal"] = list(normal_readunits.keys()) if args.tumor_bam: tumor_readunits = dict() samples["tumor"] = [] assert os.path.exists(args.tumor_bam) else: tumor_readunits = get_readunits_from_args(args.tumor_fq1, args.tumor_fq2) samples["tumor"] = list(tumor_readunits.keys()) readunits = dict(normal_readunits) readunits.update(tumor_readunits) assert sorted(samples) == sorted(["normal", "tumor"]) # FIXME howt to # if not os.path.exists(reffa): # logger.fatal("Reference '%s' doesn't exist", reffa) # sys.exit(1) # # for p in ['bwa', 'samtools']: # if not ref_is_indexed(reffa, p): # logger.fatal("Reference '%s' doesn't appear to be indexed with %s", reffa, p) # sys.exit(1) if args.seqtype in ["WES", "targeted"]: if not args.intervals: logger.fatal("Analysis of exome and targeted sequence runs requires a bed file") sys.exit(1) else: if not os.path.exists(args.intervals): logger.fatal("Intervals file %s does not exist", args.sample_cfg) sys.exit(1) logger.warning("Compatilibity between interval file and" " reference not checked") # FIXME # turn arguments into user_data that gets merged into pipeline config # # generic data first user_data = dict() user_data["mail_on_completion"] = not args.no_mail user_data["readunits"] = readunits user_data["samples"] = samples if args.name: user_data["analysis_name"] = args.name user_data["seqtype"] = args.seqtype user_data["intervals"] = args.intervals user_data["mark_dups"] = not args.dont_mark_dups pipeline_handler = PipelineHandler( PIPELINE_NAME, PIPELINE_BASEDIR, args.outdir, user_data, master_q=args.master_q, slave_q=args.slave_q, params_cfgfile=args.params_cfg, modules_cfgfile=args.modules_cfg, refs_cfgfile=args.references_cfg, cluster_cfgfile=get_cluster_cfgfile(CFG_DIR), ) pipeline_handler.setup_env() # inject existing BAM by symlinking (everything upstream is temporary anyway) for sample, bam in [("normal", args.normal_bam), ("tumor", args.tumor_bam)]: if bam: # target as defined in Snakefile! target = os.path.join(args.outdir, "out", sample, "{}.bwamem.lofreq.dedup.lacer.bam".format(sample)) os.makedirs(os.path.dirname(target)) os.symlink(os.path.abspath(bam), target) pipeline_handler.submit(args.no_run)
def main(): """main function """ parser = argparse.ArgumentParser(description=__doc__.format( PIPELINE_NAME=PIPELINE_NAME, PIPELINE_VERSION=get_pipeline_version())) # generic args parser.add_argument('-o', "--outdir", required=True, help="Output directory (must not exist)") parser.add_argument('--name', help="Give this analysis run a name (used in email and report)") parser.add_argument('--no-mail', action='store_true', help="Don't send mail on completion") #site = get_site() default = get_default_queue('slave') parser.add_argument('-w', '--slave-q', default=default, help="Queue to use for slave jobs (default: {})".format(default)) default = get_default_queue('master') parser.add_argument('-m', '--master-q', default=default, help="Queue to use for master job (default: {})".format(default)) parser.add_argument('-n', '--no-run', action='store_true') parser.add_argument('-v', '--verbose', action='count', default=0, help="Increase verbosity") parser.add_argument('-q', '--quiet', action='count', default=0, help="Decrease verbosity") cfg_group = parser.add_argument_group('Configuration files (advanced)') cfg_group.add_argument('--sample-cfg', help="Config-file (YAML) listing samples and readunits." " Collides with -1, -2 and -s") for name, descr in [("references", "reference sequences"), ("params", "parameters"), ("modules", "modules")]: default = os.path.abspath(os.path.join(CFG_DIR, "{}.yaml".format(name))) cfg_group.add_argument('--{}-cfg'.format(name), default=default, help="Config-file (yaml) for {}. (default: {})".format(descr, default)) # pipeline specific args parser.add_argument('-1', "--fq1", nargs="+", help="FastQ file/s (gzip only)." " Multiple input files supported (auto-sorted)." " Note: each file (or pair) gets a unique read-group id." " Collides with --sample-cfg.") parser.add_argument('-2', "--fq2", nargs="+", help="FastQ file/s (if paired) (gzip only). See also --fq1") parser.add_argument('-s', "--sample", help="Sample name. Collides with --sample-cfg.") parser.add_argument('-C', "--cuffdiff", action='store_true', dest="run_cuffdiff", help="Also run cuffdiff") parser.add_argument('-S', '--stranded', action='store_true', help="Stranded library prep (default is unstranded)") args = parser.parse_args() # Repeateable -v and -q for setting logging level. # See https://www.reddit.com/r/Python/comments/3nctlm/what_python_tools_should_i_be_using_on_every/ # and https://gist.github.com/andreas-wilm/b6031a84a33e652680d4 # script -vv -> DEBUG # script -v -> INFO # script -> WARNING # script -q -> ERROR # script -qq -> CRITICAL # script -qqq -> no logging at all logger.setLevel(logging.WARN + 10*args.quiet - 10*args.verbose) aux_logger.setLevel(logging.WARN + 10*args.quiet - 10*args.verbose) if os.path.exists(args.outdir): logger.fatal("Output directory %s already exists", args.outdir) sys.exit(1) # samples is a dictionary with sample names as key (mostly just # one) and readunit keys as value. readunits is a dict with # readunits (think: fastq pairs with attributes) as value if args.sample_cfg: if any([args.fq1, args.fq2, args.sample]): logger.fatal("Config file overrides fastq and sample input arguments." " Use one or the other") sys.exit(1) if not os.path.exists(args.sample_cfg): logger.fatal("Config file %s does not exist", args.sample_cfg) sys.exit(1) samples, readunits = get_samples_and_readunits_from_cfgfile(args.sample_cfg) else: if not all([args.fq1, args.sample]): logger.fatal("Need at least fq1 and sample without config file") sys.exit(1) readunits = get_readunits_from_args(args.fq1, args.fq2) # all readunits go into this one sample specified on the command-line samples = dict() samples[args.sample] = list(readunits.keys()) # FIXME checks on reffa index (currently not exposed via args) # turn arguments into user_data that gets merged into pipeline config # # generic data first user_data = dict() user_data['mail_on_completion'] = not args.no_mail user_data['readunits'] = readunits user_data['samples'] = samples if args.name: user_data['analysis_name'] = args.name user_data['stranded'] = args.stranded user_data['run_cuffdiff'] = args.run_cuffdiff user_data['paired_end'] = any(ru.get('fq2') for ru in readunits.values()) if user_data['paired_end']: assert all(ru.get('fq2') for ru in readunits.values()), ( "Can't handle mix of paired-end and single-end") pipeline_handler = PipelineHandler( PIPELINE_NAME, PIPELINE_BASEDIR, args.outdir, user_data, master_q=args.master_q, slave_q=args.slave_q, params_cfgfile=args.params_cfg, modules_cfgfile=args.modules_cfg, refs_cfgfile=args.references_cfg, cluster_cfgfile=get_cluster_cfgfile(CFG_DIR)) pipeline_handler.setup_env() pipeline_handler.submit(args.no_run)
def main(): """main function """ parser = argparse.ArgumentParser(description=__doc__.format( PIPELINE_NAME=PIPELINE_NAME, PIPELINE_VERSION=get_pipeline_version())) # generic args parser.add_argument('-o', "--outdir", required=True, help="Output directory (may not exist)") parser.add_argument('--name', help="Give this analysis run a name (used in email and report)") parser.add_argument('--no-mail', action='store_true', help="Don't send mail on completion") #site = get_site() default = get_default_queue('slave') parser.add_argument('-w', '--slave-q', default=default, help="Queue to use for slave jobs (default: {})".format(default)) default = get_default_queue('master') parser.add_argument('-m', '--master-q', default=default, help="Queue to use for master job (default: {})".format(default)) parser.add_argument('-n', '--no-run', action='store_true') parser.add_argument('-v', '--verbose', action='count', default=0, help="Increase verbosity") parser.add_argument('-q', '--quiet', action='count', default=0, help="Decrease verbosity") cfg_group = parser.add_argument_group('Configuration files (advanced)') cfg_group.add_argument('--sample-cfg', help="Config-file (YAML) listing samples and readunits." " Collides with -1, -2 and -s") for name, descr in [("references", "reference sequences"), ("params", "parameters"), ("modules", "modules")]: default = os.path.abspath(os.path.join(CFG_DIR, "{}.yaml".format(name))) cfg_group.add_argument('--{}-cfg'.format(name), default=default, help="Config-file (yaml) for {}. (default: {})".format(descr, default)) # pipeline specific args parser.add_argument('-1', "--fq1", nargs="+", help="FastQ file/s (gzip only)." " Multiple input files supported (auto-sorted)." " Note: each file (or pair) gets a unique read-group id." " Collides with --sample-cfg.") parser.add_argument('-2', "--fq2", nargs="+", help="FastQ file/s (if paired) (gzip only). See also --fq1") parser.add_argument('-s', "--sample", help="Sample name. Collides with --sample-cfg.") parser.add_argument('-t', "--seqtype", required=True, choices=['WGS', 'WES', 'targeted'], help="Sequencing type") parser.add_argument('-l', "--intervals", help="Intervals file (e.g. bed file) listing regions of interest." " Required for WES and targeted sequencing.") parser.add_argument('-D', '--dont-mark-dups', action='store_true', help="Don't mark duplicate reads") args = parser.parse_args() # Repeateable -v and -q for setting logging level. # See https://www.reddit.com/r/Python/comments/3nctlm/what_python_tools_should_i_be_using_on_every/ # and https://gist.github.com/andreas-wilm/b6031a84a33e652680d4 # script -vv -> DEBUG # script -v -> INFO # script -> WARNING # script -q -> ERROR # script -qq -> CRITICAL # script -qqq -> no logging at all logger.setLevel(logging.WARN + 10*args.quiet - 10*args.verbose) aux_logger.setLevel(logging.WARN + 10*args.quiet - 10*args.verbose) if os.path.exists(args.outdir): logger.fatal("Output directory %s already exists", args.outdir) sys.exit(1) # samples is a dictionary with sample names as key (mostly just # one) and readunit keys as value. readunits is a dict with # readunits (think: fastq pairs with attributes) as value if args.sample_cfg: if any([args.fq1, args.fq2, args.sample]): logger.fatal("Config file overrides fastq and sample input arguments." " Use one or the other") sys.exit(1) if not os.path.exists(args.sample_cfg): logger.fatal("Config file %s does not exist", args.sample_cfg) sys.exit(1) samples, readunits = get_samples_and_readunits_from_cfgfile(args.sample_cfg) else: if not all([args.fq1, args.sample]): logger.fatal("Need at least fq1 and sample without config file") sys.exit(1) readunits = get_readunits_from_args(args.fq1, args.fq2) # all readunits go into this one sample specified on the command-line samples = dict() samples[args.sample] = list(readunits.keys()) # FIXME how to? #for p in ['bwa', 'samtools']: # if not ref_is_indexed(args.reffa, p): # logger.fatal("Reference '%s' doesn't appear to be indexed with %s", args.reffa, p) # sys.exit(1) if args.seqtype in ['WES', 'targeted']: if not args.intervals: logger.fatal("Analysis of exome and targeted sequence runs requires a bed file") sys.exit(1) else: if not os.path.exists(args.intervals): logger.fatal("Intervals file %s does not exist", args.sample_cfg) sys.exit(1) logger.warning("Compatilibity between interval file and" " reference not checked")# FIXME # turn arguments into user_data that gets merged into pipeline config # # generic data first user_data = dict() user_data['mail_on_completion'] = not args.no_mail user_data['readunits'] = readunits user_data['samples'] = samples if args.name: user_data['analysis_name'] = args.name user_data['seqtype'] = args.seqtype user_data['intervals'] = args.intervals user_data['mark_dups'] = not args.dont_mark_dups pipeline_handler = PipelineHandler( PIPELINE_NAME, PIPELINE_BASEDIR, args.outdir, user_data, master_q=args.master_q, slave_q=args.slave_q, params_cfgfile=args.params_cfg, modules_cfgfile=args.modules_cfg, refs_cfgfile=args.references_cfg, cluster_cfgfile=get_cluster_cfgfile(CFG_DIR)) pipeline_handler.setup_env() pipeline_handler.submit(args.no_run)
def main(): """main function """ # FIXME ugly and code duplication in bcl2fastq_dbupdate.py mongo_status_script = os.path.abspath(os.path.join( os.path.dirname(sys.argv[0]), "mongo_status.py")) assert os.path.exists(mongo_status_script) parser = argparse.ArgumentParser(description=__doc__.format( PIPELINE_NAME=PIPELINE_NAME, PIPELINE_VERSION=get_pipeline_version())) parser.add_argument('-r', "--runid", help="Run ID plus flowcell ID (clashes with -d)") parser.add_argument('-d', "--rundir", help="BCL input directory (clashes with -r)") parser.add_argument('-o', "--outdir", help="Output directory (must not exist; required if called by user)") parser.add_argument('-t', "--testing", action='store_true', help="Use MongoDB test server") parser.add_argument('--no-archive', action='store_true', help="Don't archieve this analysis") parser.add_argument('--name', help="Give this analysis run a name (used in email and report)") parser.add_argument('--no-mail', action='store_true', help="Don't send mail on completion") default = get_default_queue('slave') parser.add_argument('-w', '--slave-q', default=default, help="Queue to use for slave jobs (default: {})".format(default)) default = get_default_queue('master') parser.add_argument('-m', '--master-q', default=default, help="Queue to use for master job (default: {})".format(default)) parser.add_argument('-l', '--lanes', type=int, nargs="*", help="Limit run to given lane/s (multiples separated by space") parser.add_argument('-i', '--mismatches', type=int, help="Max. number of allowed barcode mismatches (0>=x<=2)" " setting a value here overrides the default settings read from ELM)") parser.add_argument('-n', '--no-run', action='store_true') parser.add_argument('-v', '--verbose', action='count', default=0, help="Increase verbosity") parser.add_argument('-q', '--quiet', action='count', default=0, help="Decrease verbosity") cfg_group = parser.add_argument_group('Configuration files (advanced)') for name, descr in [("modules", "modules")]: default = os.path.abspath(os.path.join(CFG_DIR, "{}.yaml".format(name))) cfg_group.add_argument('--{}-cfg'.format(name), default=default, help="Config-file (yaml) for {}. (default: {})".format(descr, default)) args = parser.parse_args() # Repeateable -v and -q for setting logging level. # See https://www.reddit.com/r/Python/comments/3nctlm/what_python_tools_should_i_be_using_on_every/ # and https://gist.github.com/andreas-wilm/b6031a84a33e652680d4 # script -vv -> DEBUG # script -v -> INFO # script -> WARNING # script -q -> ERROR # script -qq -> CRITICAL # script -qqq -> no logging at all logger.setLevel(logging.WARN + 10*args.quiet - 10*args.verbose) aux_logger.setLevel(logging.WARN + 10*args.quiet - 10*args.verbose) if args.mismatches is not None: if args.mismatches > 2 or args.mismatches < 0: logger.fatal("Number of mismatches must be between 0-2") sys.exit(1) lane_info = '' lane_nos = [] if args.lanes: lane_info = '--tiles ' for lane in args.lanes: if lane > 8 or lane < 1: logger.fatal("Lane number must be between 1-8") sys.exit(1) else: lane_info += 's_{}'.format(lane)+',' lane_info = lane_info.rstrip() lane_info = lane_info[:-1] lane_nos = list(args.lanes) if args.runid and args.rundir: logger.fatal("Cannot use run-id and input directory arguments simultaneously") sys.exit(1) elif args.runid: rundir = run_folder_for_run_id(args.runid) elif args.rundir: rundir = os.path.abspath(args.rundir) else: logger.fatal("Need either run-id or input directory") sys.exit(1) if not os.path.exists(rundir): logger.fatal("Expected run directory %s does not exist", rundir) logger.info("Rundir is %s", rundir) if not args.outdir: outdir = get_bcl2fastq_outdir(args.runid) else: outdir = args.outdir if os.path.exists(outdir): logger.fatal("Output directory %s already exists", outdir) sys.exit(1) # create now so that generate_bcl2fastq_cfg.py can run os.makedirs(outdir) # catch cases where rundir was user provided and looks weird try: _, runid, flowcellid = get_machine_run_flowcell_id(rundir) run_num = runid + "_" + flowcellid except: run_num = "UNKNOWN-" + rundir.split("/")[-1] # call generate_bcl2fastq_cfg # # FIXME ugly assumes same directory (just like import above). better to import and run main()? generate_bcl2fastq = os.path.join( os.path.dirname(sys.argv[0]), "generate_bcl2fastq_cfg.py") assert os.path.exists(generate_bcl2fastq) cmd = [generate_bcl2fastq, '-r', rundir, '-o', outdir] if args.testing: cmd.append("-t") logger.debug("Executing %s", ' ' .join(cmd)) try: res = subprocess.check_output(cmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: logger.fatal("The following command failed with return code %s: %s", e.returncode, ' '.join(cmd)) logger.fatal("Output: %s", e.output.decode()) logger.fatal("Exiting") sys.exit(1) # generate_bcl2fastq is normally quiet. if there's output, make caller aware of it # use sys instead of logger to avoid double logging if res: sys.stderr.write(res.decode()) # just created files muxinfo_cfg = os.path.join(outdir, MUXINFO_CFG) samplesheet_csv = os.path.join(outdir, SAMPLESHEET_CSV) usebases_cfg = os.path.join(outdir, USEBASES_CFG) # NOTE: signal for failed runs is exit 0 from generate_bcl2fastq and missing output files # if any([not os.path.exists(x) for x in [muxinfo_cfg, samplesheet_csv, usebases_cfg]]): # one missing means all should be missing assert all([not os.path.exists(x) for x in [muxinfo_cfg, samplesheet_csv, usebases_cfg]]) seqrunfailed(mongo_status_script, run_num, outdir, args.testing) sys.exit(0) # turn arguments into user_data that gets merged into pipeline config user_data = {'rundir': rundir, 'lanes_arg': lane_info, 'samplesheet_csv': samplesheet_csv, 'no_archive': args.no_archive, 'mail_on_completion': not args.no_mail, 'run_num': run_num} if args.name: user_data['analysis_name'] = args.name usebases_arg = '' with open(usebases_cfg, 'r') as stream: try: d = yaml.load(stream) assert 'usebases' in d assert len(d) == 1# make sure usebases is only key for ub in d['usebases']: #print (ub) usebases_arg += '--use-bases-mask {} '.format(ub) #user_data = {'usebases_arg' : usebases_arg} except yaml.YAMLError as exc: logger.fatal(exc) raise user_data['usebases_arg'] = usebases_arg os.unlink(usebases_cfg) mux_units = get_mux_units_from_cfgfile(muxinfo_cfg, lane_nos) if args.mismatches is not None: mux_units = [mu._replace(barcode_mismatches=args.mismatches) for mu in mux_units] os.unlink(muxinfo_cfg) user_data['units'] = dict() for mu in mux_units: # special case: mux split across multiple lanes. make lanes a list # and add in extra lanes if needed. k = mu.mux_dir mu_dict = dict(mu._asdict()) user_data['units'][k] = mu_dict # create mongodb update command, used later, after submission mongo_update_cmd = "{} -r {} -s STARTED".format(mongo_status_script, user_data['run_num']) mongo_update_cmd += " -a $ANALYSIS_ID -o {}".format(outdir)# set in run.sh if args.testing: mongo_update_cmd += " -t" pipeline_handler = PipelineHandler( PIPELINE_NAME, PIPELINE_BASEDIR, outdir, user_data, logger_cmd=mongo_update_cmd, master_q=args.master_q, slave_q=args.slave_q, modules_cfgfile=args.modules_cfg, cluster_cfgfile=get_cluster_cfgfile(CFG_DIR)) pipeline_handler.setup_env() pipeline_handler.submit(args.no_run)